| 1 | """ |
| 2 | Finite field elements implemented via PARI's FFELT type |
| 3 | |
| 4 | AUTHORS: |
| 5 | |
| 6 | - Peter Bruin (June 2013): initial version, based on |
| 7 | element_ext_pari.py by William Stein et al. and |
| 8 | element_ntl_gf2e.pyx by Martin Albrecht. |
| 9 | """ |
| 10 | |
| 11 | #***************************************************************************** |
| 12 | # Copyright (C) 2013 Peter Bruin <peter.bruin@math.uzh.ch> |
| 13 | # |
| 14 | # Distributed under the terms of the GNU General Public License (GPL) |
| 15 | # as published by the Free Software Foundation; either version 2 of |
| 16 | # the License, or (at your option) any later version. |
| 17 | # http://www.gnu.org/licenses/ |
| 18 | #***************************************************************************** |
| 19 | |
| 20 | |
| 21 | include "sage/ext/stdsage.pxi" |
| 22 | include "sage/ext/interrupt.pxi" |
| 23 | |
| 24 | from element_base cimport FinitePolyExtElement |
| 25 | from integer_mod import is_IntegerMod |
| 26 | |
| 27 | import sage.libs.pari |
| 28 | from sage.interfaces.gap import is_GapElement |
| 29 | from sage.libs.pari.gen cimport gen as pari_gen, PariInstance |
| 30 | from sage.modules.free_module_element import FreeModuleElement |
| 31 | from sage.rings.integer cimport Integer |
| 32 | from sage.rings.polynomial.polynomial_element import Polynomial |
| 33 | from sage.rings.polynomial.multi_polynomial_element import MPolynomial |
| 34 | from sage.rings.rational import Rational |
| 35 | from sage.structure.element cimport Element, ModuleElement, RingElement |
| 36 | |
| 37 | cdef PariInstance pari = sage.libs.pari.gen.pari |
| 38 | |
| 39 | cdef extern from "sage/libs/pari/misc.h": |
| 40 | int gcmp_sage(GEN x, GEN y) |
| 41 | |
| 42 | cdef extern GEN Flx_to_F2x(GEN x) |
| 43 | |
| 44 | cdef extern from "pari/paripriv.h": |
| 45 | extern int t_FF_FpXQ, t_FF_Flxq, t_FF_F2xq |
| 46 | |
| 47 | |
| 48 | cdef class FiniteFieldElement_pari_ffelt(FinitePolyExtElement): |
| 49 | """ |
| 50 | An element of a finite field. |
| 51 | |
| 52 | EXAMPLE:: |
| 53 | |
| 54 | sage: K = FiniteField(10007^10, 'a', impl='pari_ffelt') |
| 55 | sage: a = K.gen(); a |
| 56 | a |
| 57 | sage: type(a) |
| 58 | <type 'sage.rings.finite_rings.element_pari_ffelt.FiniteFieldElement_pari_ffelt'> |
| 59 | |
| 60 | TESTS:: |
| 61 | |
| 62 | sage: n = 63 |
| 63 | sage: m = 3; |
| 64 | sage: K.<a> = GF(2^n, impl='pari_ffelt') |
| 65 | sage: f = conway_polynomial(2, n) |
| 66 | sage: f(a) == 0 |
| 67 | True |
| 68 | sage: e = (2^n - 1) / (2^m - 1) |
| 69 | sage: conway_polynomial(2, m)(a^e) == 0 |
| 70 | True |
| 71 | |
| 72 | sage: K.<a> = FiniteField(2^16, impl='pari_ffelt') |
| 73 | sage: K(0).is_zero() |
| 74 | True |
| 75 | sage: (a - a).is_zero() |
| 76 | True |
| 77 | sage: a - a |
| 78 | 0 |
| 79 | sage: a == a |
| 80 | True |
| 81 | sage: a - a == 0 |
| 82 | True |
| 83 | sage: a - a == K(0) |
| 84 | True |
| 85 | sage: TestSuite(a).run() |
| 86 | """ |
| 87 | |
| 88 | def __init__(FiniteFieldElement_pari_ffelt self, object parent, object x): |
| 89 | """ |
| 90 | Create an empty finite field element with the given parent. |
| 91 | |
| 92 | This is called when constructing elements from Python. |
| 93 | """ |
| 94 | # FinitePolyExtElement.__init__(self, parent) |
| 95 | self._parent = parent |
| 96 | self.construct_from(x) |
| 97 | |
| 98 | def __cinit__(FiniteFieldElement_pari_ffelt self): |
| 99 | """ |
| 100 | Cython constructor. |
| 101 | """ |
| 102 | self.block = NULL |
| 103 | |
| 104 | def __dealloc__(FiniteFieldElement_pari_ffelt self): |
| 105 | """ |
| 106 | Cython deconstructor. |
| 107 | """ |
| 108 | if self.block: |
| 109 | sage_free(self.block) |
| 110 | |
| 111 | cdef FiniteFieldElement_pari_ffelt _new(FiniteFieldElement_pari_ffelt self): |
| 112 | """ |
| 113 | Create an empty element with the same parent as ``self``. |
| 114 | |
| 115 | This is the Cython replacement for __init__. |
| 116 | """ |
| 117 | cdef FiniteFieldElement_pari_ffelt x |
| 118 | x = FiniteFieldElement_pari_ffelt.__new__(FiniteFieldElement_pari_ffelt) |
| 119 | x._parent = self._parent |
| 120 | return x |
| 121 | |
| 122 | cdef void construct(FiniteFieldElement_pari_ffelt self, GEN g): |
| 123 | """ |
| 124 | Initialise ``self`` to the FFELT ``g``, reset the PARI stack, |
| 125 | and call sig_off(). |
| 126 | |
| 127 | This should be called exactly once on every instance. |
| 128 | """ |
| 129 | self.val = pari.deepcopy_to_python_heap(g, <pari_sp*>&self.block) |
| 130 | pari.clear_stack() |
| 131 | |
| 132 | cdef void construct_from(FiniteFieldElement_pari_ffelt self, object x) except *: |
| 133 | """ |
| 134 | Initialise ``self`` to an FFELT constructed from the Sage |
| 135 | object `x`. |
| 136 | """ |
| 137 | cdef GEN f, x_GEN, self_GEN, zero_GEN |
| 138 | cdef long i, n, t |
| 139 | |
| 140 | if isinstance(x, pari_gen): |
| 141 | x_GEN = (<pari_gen>x).g |
| 142 | self_GEN = (<pari_gen>self._parent._gen_pari).g |
| 143 | |
| 144 | sig_on() |
| 145 | if gequal0(x_GEN): |
| 146 | self.construct(FF_zero(self_GEN)) |
| 147 | return |
| 148 | elif gequal1(x_GEN): |
| 149 | self.construct(FF_1(self_GEN)) |
| 150 | return |
| 151 | |
| 152 | t = typ(x_GEN) |
| 153 | if t == t_FFELT and FF_samefield(x_GEN, self_GEN): |
| 154 | self.construct(x_GEN) |
| 155 | return |
| 156 | |
| 157 | # We do not initialise zero_GEN earlier because the code |
| 158 | # before this point is also used during initialisation of |
| 159 | # FiniteField_pari_elt. |
| 160 | zero_GEN = (<FiniteFieldElement_pari_ffelt>self._parent._zero_element).val |
| 161 | |
| 162 | if t == t_INT: |
| 163 | self.construct(FF_Z_add(zero_GEN, x_GEN)) |
| 164 | elif t == t_INTMOD and gequal0(modii(<GEN>x_GEN[1], FF_p_i(self_GEN))): |
| 165 | self.construct(FF_Z_add(zero_GEN, <GEN>x_GEN[2])) |
| 166 | elif t == t_FRAC and not gequal0(modii(<GEN>x_GEN[2], FF_p_i(self_GEN))): |
| 167 | self.construct(FF_Q_add(zero_GEN, x_GEN)) |
| 168 | else: |
| 169 | sig_off() |
| 170 | raise TypeError("no coercion defined") |
| 171 | |
| 172 | elif isinstance(x, FiniteFieldElement_pari_ffelt): |
| 173 | if self._parent is (<FiniteFieldElement_pari_ffelt>x)._parent: |
| 174 | sig_on() |
| 175 | self.construct((<FiniteFieldElement_pari_ffelt>x).val) |
| 176 | else: |
| 177 | # This is where we *would* do coercion from one finite field to another... |
| 178 | raise TypeError("no coercion defined") |
| 179 | |
| 180 | elif isinstance(x, (int, long, Integer, Rational)): |
| 181 | if x == 0: |
| 182 | self_GEN = (<pari_gen>self._parent._gen_pari).g |
| 183 | sig_on() |
| 184 | self.construct(FF_zero(self_GEN)) |
| 185 | return |
| 186 | elif x == 1: |
| 187 | sig_on() |
| 188 | self_GEN = (<pari_gen>self._parent._gen_pari).g |
| 189 | self.construct(FF_1(self_GEN)) |
| 190 | return |
| 191 | else: |
| 192 | # The following could be optimised. |
| 193 | self.construct_from(pari(x)) |
| 194 | |
| 195 | elif is_IntegerMod(x): |
| 196 | self.construct_from(pari(x)) |
| 197 | |
| 198 | elif isinstance(x, Polynomial): |
| 199 | if x.base_ring() is not self._parent.base_ring(): |
| 200 | x = x.change_ring(self._parent.base_ring()) |
| 201 | self.construct_from(x.substitute(self._parent.gen())) |
| 202 | |
| 203 | elif isinstance(x, MPolynomial) and x.is_constant(): |
| 204 | self.construct_from(x.constant_coefficient()) |
| 205 | |
| 206 | elif (isinstance(x, FreeModuleElement) |
| 207 | and x.parent() is self._parent.vector_space()): |
| 208 | self_GEN = (<pari_gen>self._parent._gen_pari).g |
| 209 | t = self_GEN[1] # codeword: t_FF_FpXQ, t_FF_Flxq, t_FF_F2xq |
| 210 | d = len(x) - 1 # degree of x as a polynomial |
| 211 | while d >= 0 and x[d] == 0: |
| 212 | d -= 1 |
| 213 | sig_on() |
| 214 | if d == -1: |
| 215 | self.construct(FF_zero(self_GEN)) |
| 216 | return |
| 217 | if t == t_FF_FpXQ: |
| 218 | f = cgetg(d + 3, t_POL) |
| 219 | (<GEN *>f)[1] = gel(<GEN>self_GEN[2], 1) |
| 220 | for i in xrange(d + 1): |
| 221 | (<GEN *>f)[i + 2] = stoi(long(x[i])) |
| 222 | elif t == t_FF_Flxq or t == t_FF_F2xq: |
| 223 | f = cgetg(d + 3, t_VECSMALL) |
| 224 | (<GEN *>f)[1] = gel(<GEN>self_GEN[2], 1) |
| 225 | for i in xrange(d + 1): |
| 226 | f[i + 2] = long(x[i]) |
| 227 | if t == t_FF_F2xq: |
| 228 | f = Flx_to_F2x(f) |
| 229 | else: |
| 230 | sig_off() |
| 231 | raise TypeError("unknown PARI finite field type") |
| 232 | x_GEN = cgetg(5, t_FFELT) |
| 233 | x_GEN[1] = t |
| 234 | (<GEN *>x_GEN)[2] = f |
| 235 | (<GEN *>x_GEN)[3] = gel(self_GEN, 3) # modulus |
| 236 | (<GEN *>x_GEN)[4] = gel(self_GEN, 4) # p |
| 237 | self.construct(x_GEN) |
| 238 | |
| 239 | elif isinstance(x, str): |
| 240 | self.construct_from(self._parent.polynomial_ring()(x)) |
| 241 | |
| 242 | elif isinstance(x, list): |
| 243 | if len(x) == self._parent.degree(): |
| 244 | self.construct_from(self._parent.vector_space()(x)) |
| 245 | else: |
| 246 | Fp = self._parent.base_ring() |
| 247 | self.construct_from(self._parent.polynomial_ring()([Fp(y) for y in x])) |
| 248 | |
| 249 | elif is_GapElement(x): |
| 250 | from sage.interfaces.gap import gfq_gap_to_sage |
| 251 | try: |
| 252 | self.construct_from(gfq_gap_to_sage(x, self._parent)) |
| 253 | except (ValueError, IndexError, TypeError): |
| 254 | raise TypeError("no coercion defined") |
| 255 | |
| 256 | else: |
| 257 | raise TypeError("no coercion defined") |
| 258 | |
| 259 | def _repr_(FiniteFieldElement_pari_ffelt self): |
| 260 | """ |
| 261 | Return the string representation of ``self``. |
| 262 | |
| 263 | EXAMPLE:: |
| 264 | |
| 265 | sage: k.<c> = GF(3^17, impl='pari_ffelt') |
| 266 | sage: c^20 # indirect doctest |
| 267 | c^4 + 2*c^3 |
| 268 | """ |
| 269 | sig_on() |
| 270 | return pari.new_gen_to_string(self.val) |
| 271 | |
| 272 | def __hash__(FiniteFieldElement_pari_ffelt self): |
| 273 | """ |
| 274 | Return the hash of ``self``. This is by definition equal to |
| 275 | the hash of ``self.polynomial()``. |
| 276 | |
| 277 | EXAMPLE:: |
| 278 | |
| 279 | sage: k.<a> = GF(3^15, impl='pari_ffelt') |
| 280 | sage: R = GF(3)['a']; aa = R.gen() |
| 281 | sage: hash(a^2 + 1) == hash(aa^2 + 1) |
| 282 | True |
| 283 | """ |
| 284 | return hash(self.polynomial()) |
| 285 | |
| 286 | def __reduce__(FiniteFieldElement_pari_ffelt self): |
| 287 | """ |
| 288 | For pickling. |
| 289 | |
| 290 | TEST: |
| 291 | |
| 292 | sage: K = FiniteField(10007^10, 'a', impl='pari_ffelt') |
| 293 | sage: a = K.gen() |
| 294 | sage: loads(a.dumps()) == a |
| 295 | True |
| 296 | """ |
| 297 | return unpickle_FiniteFieldElement_pari_ffelt, (self._parent, str(self)) |
| 298 | |
| 299 | def __copy__(FiniteFieldElement_pari_ffelt self): |
| 300 | """ |
| 301 | Return a copy of ``self``. |
| 302 | |
| 303 | TESTS:: |
| 304 | |
| 305 | sage: k = FiniteField(3^3, 'a', impl='pari_ffelt') |
| 306 | sage: a = k.gen() |
| 307 | sage: a |
| 308 | a |
| 309 | sage: b = copy(a); b |
| 310 | a |
| 311 | sage: a == b |
| 312 | True |
| 313 | sage: a is b |
| 314 | False |
| 315 | """ |
| 316 | cdef FiniteFieldElement_pari_ffelt x = self._new() |
| 317 | sig_on() |
| 318 | x.construct(self.val) |
| 319 | return x |
| 320 | |
| 321 | cdef int _cmp_c_impl(FiniteFieldElement_pari_ffelt self, Element other) except -2: |
| 322 | """ |
| 323 | Comparison of finite field elements. |
| 324 | |
| 325 | TESTS:: |
| 326 | |
| 327 | sage: a = FiniteField(3^3, 'a', impl='pari_ffelt').gen() |
| 328 | sage: a == 1 |
| 329 | False |
| 330 | sage: a**0 == 1 |
| 331 | True |
| 332 | sage: a == a |
| 333 | True |
| 334 | sage: a < a**2 |
| 335 | True |
| 336 | sage: a > a**2 |
| 337 | False |
| 338 | """ |
| 339 | return gcmp_sage(self.val, (<FiniteFieldElement_pari_ffelt>other).val) |
| 340 | |
| 341 | def __richcmp__(FiniteFieldElement_pari_ffelt left, object right, int op): |
| 342 | """ |
| 343 | Rich comparison of finite field elements. |
| 344 | |
| 345 | EXAMPLE:: |
| 346 | |
| 347 | sage: k.<a> = GF(2^20, impl='pari_ffelt') |
| 348 | sage: e = k.random_element() |
| 349 | sage: f = loads(dumps(e)) |
| 350 | sage: e is f |
| 351 | False |
| 352 | sage: e == f |
| 353 | True |
| 354 | sage: e != (e + 1) |
| 355 | True |
| 356 | |
| 357 | .. NOTE:: |
| 358 | |
| 359 | Finite fields are unordered. However, for the purpose of |
| 360 | this function, we adopt the lexicographic ordering on the |
| 361 | representing polynomials. |
| 362 | |
| 363 | EXAMPLE:: |
| 364 | |
| 365 | sage: K.<a> = GF(2^100, impl='pari_ffelt') |
| 366 | sage: a < a^2 |
| 367 | True |
| 368 | sage: a > a^2 |
| 369 | False |
| 370 | sage: a+1 > a^2 |
| 371 | False |
| 372 | sage: a+1 < a^2 |
| 373 | True |
| 374 | sage: a+1 < a |
| 375 | False |
| 376 | sage: a+1 == a |
| 377 | False |
| 378 | sage: a == a |
| 379 | True |
| 380 | """ |
| 381 | return (<Element>left)._richcmp(right, op) |
| 382 | |
| 383 | cpdef ModuleElement _add_(FiniteFieldElement_pari_ffelt self, ModuleElement right): |
| 384 | """ |
| 385 | Addition. |
| 386 | |
| 387 | EXAMPLE:: |
| 388 | |
| 389 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 390 | sage: a + a^2 # indirect doctest |
| 391 | a^2 + a |
| 392 | """ |
| 393 | cdef FiniteFieldElement_pari_ffelt x = self._new() |
| 394 | sig_on() |
| 395 | x.construct(FF_add((<FiniteFieldElement_pari_ffelt>self).val, |
| 396 | (<FiniteFieldElement_pari_ffelt>right).val)) |
| 397 | return x |
| 398 | |
| 399 | cpdef ModuleElement _sub_(FiniteFieldElement_pari_ffelt self, ModuleElement right): |
| 400 | """ |
| 401 | Subtraction. |
| 402 | |
| 403 | EXAMPLE:: |
| 404 | |
| 405 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 406 | sage: a - a # indirect doctest |
| 407 | 0 |
| 408 | """ |
| 409 | cdef FiniteFieldElement_pari_ffelt x = self._new() |
| 410 | sig_on() |
| 411 | x.construct(FF_sub((<FiniteFieldElement_pari_ffelt>self).val, |
| 412 | (<FiniteFieldElement_pari_ffelt>right).val)) |
| 413 | return x |
| 414 | |
| 415 | cpdef RingElement _mul_(FiniteFieldElement_pari_ffelt self, RingElement right): |
| 416 | """ |
| 417 | Multiplication. |
| 418 | |
| 419 | EXAMPLE:: |
| 420 | |
| 421 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 422 | sage: (a^12 + 1)*(a^15 - 1) # indirect doctest |
| 423 | a^15 + 2*a^12 + a^11 + 2*a^10 + 2 |
| 424 | """ |
| 425 | cdef FiniteFieldElement_pari_ffelt x = self._new() |
| 426 | sig_on() |
| 427 | x.construct(FF_mul((<FiniteFieldElement_pari_ffelt>self).val, |
| 428 | (<FiniteFieldElement_pari_ffelt>right).val)) |
| 429 | return x |
| 430 | |
| 431 | cpdef RingElement _div_(FiniteFieldElement_pari_ffelt self, RingElement right): |
| 432 | """ |
| 433 | Division. |
| 434 | |
| 435 | EXAMPLE:: |
| 436 | |
| 437 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 438 | sage: (a - 1) / (a + 1) # indirect doctest |
| 439 | 2*a^16 + a^15 + 2*a^14 + a^13 + 2*a^12 + a^11 + 2*a^10 + a^9 + 2*a^8 + a^7 + 2*a^6 + a^5 + 2*a^4 + a^3 + 2*a^2 + a + 1 |
| 440 | """ |
| 441 | if FF_equal0((<FiniteFieldElement_pari_ffelt>right).val): |
| 442 | raise ZeroDivisionError |
| 443 | cdef FiniteFieldElement_pari_ffelt x = self._new() |
| 444 | sig_on() |
| 445 | x.construct(FF_div((<FiniteFieldElement_pari_ffelt>self).val, |
| 446 | (<FiniteFieldElement_pari_ffelt>right).val)) |
| 447 | return x |
| 448 | |
| 449 | def is_zero(FiniteFieldElement_pari_ffelt self): |
| 450 | """ |
| 451 | Return ``True`` if ``self`` equals 0. |
| 452 | |
| 453 | EXAMPLE:: |
| 454 | |
| 455 | sage: F.<a> = FiniteField(5^3, impl='pari_ffelt') |
| 456 | sage: a.is_zero() |
| 457 | False |
| 458 | sage: (a - a).is_zero() |
| 459 | True |
| 460 | """ |
| 461 | return bool(FF_equal0(self.val)) |
| 462 | |
| 463 | def is_one(FiniteFieldElement_pari_ffelt self): |
| 464 | """ |
| 465 | Return ``True`` if ``self`` equals 1. |
| 466 | |
| 467 | EXAMPLE:: |
| 468 | |
| 469 | sage: F.<a> = FiniteField(5^3, impl='pari_ffelt') |
| 470 | sage: a.is_one() |
| 471 | False |
| 472 | sage: (a/a).is_one() |
| 473 | True |
| 474 | """ |
| 475 | return bool(FF_equal1(self.val)) |
| 476 | |
| 477 | def is_unit(FiniteFieldElement_pari_ffelt self): |
| 478 | """ |
| 479 | Return ``True`` if ``self`` is non-zero. |
| 480 | |
| 481 | EXAMPLE:: |
| 482 | |
| 483 | sage: F.<a> = FiniteField(5^3, impl='pari_ffelt') |
| 484 | sage: a.is_unit() |
| 485 | True |
| 486 | """ |
| 487 | return not bool(FF_equal0(self.val)) |
| 488 | |
| 489 | __nonzero__ = is_unit |
| 490 | |
| 491 | def __pos__(FiniteFieldElement_pari_ffelt self): |
| 492 | """ |
| 493 | Unitary positive operator... |
| 494 | |
| 495 | EXAMPLE:: |
| 496 | |
| 497 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 498 | sage: +a |
| 499 | a |
| 500 | """ |
| 501 | return self |
| 502 | |
| 503 | def __neg__(FiniteFieldElement_pari_ffelt self): |
| 504 | """ |
| 505 | Negation. |
| 506 | |
| 507 | EXAMPLE:: |
| 508 | |
| 509 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 510 | sage: -a |
| 511 | 2*a |
| 512 | """ |
| 513 | cdef FiniteFieldElement_pari_ffelt x = self._new() |
| 514 | sig_on() |
| 515 | x.construct(FF_neg_i((<FiniteFieldElement_pari_ffelt>self).val)) |
| 516 | return x |
| 517 | |
| 518 | def __invert__(FiniteFieldElement_pari_ffelt self): |
| 519 | """ |
| 520 | Return the multiplicative inverse of ``self``. |
| 521 | |
| 522 | EXAMPLE:: |
| 523 | |
| 524 | sage: a = FiniteField(3^2, 'a', impl='pari_ffelt').gen() |
| 525 | sage: ~a |
| 526 | a + 2 |
| 527 | sage: (a+1)*a |
| 528 | 2*a + 1 |
| 529 | sage: ~((2*a)/a) |
| 530 | 2 |
| 531 | """ |
| 532 | if FF_equal0(self.val): |
| 533 | raise ZeroDivisionError |
| 534 | cdef FiniteFieldElement_pari_ffelt x = self._new() |
| 535 | sig_on() |
| 536 | x.construct(FF_inv((<FiniteFieldElement_pari_ffelt>self).val)) |
| 537 | return x |
| 538 | |
| 539 | def __pow__(FiniteFieldElement_pari_ffelt self, object exp, object other): |
| 540 | """ |
| 541 | Exponentiation. |
| 542 | |
| 543 | TESTS:: |
| 544 | |
| 545 | sage: K.<a> = GF(5^10, impl='pari_ffelt') |
| 546 | sage: n = (2*a)/a |
| 547 | sage: n^-15 |
| 548 | 2 |
| 549 | |
| 550 | Large exponents are not a problem:: |
| 551 | |
| 552 | sage: e = 3^10000 |
| 553 | sage: a^e |
| 554 | 2*a^9 + a^5 + 4*a^4 + 4*a^3 + a^2 + 3*a |
| 555 | sage: a^(e % (5^10 - 1)) |
| 556 | 2*a^9 + a^5 + 4*a^4 + 4*a^3 + a^2 + 3*a |
| 557 | """ |
| 558 | if exp == 0: |
| 559 | return self._parent.one_element() |
| 560 | if exp < 0 and FF_equal0(self.val): |
| 561 | raise ZeroDivisionError |
| 562 | exp = Integer(exp) # or convert to Z/(q - 1)Z if we are in F_q... |
| 563 | cdef FiniteFieldElement_pari_ffelt x = self._new() |
| 564 | sig_on() |
| 565 | x.construct(FF_pow(self.val, (<pari_gen>(pari(exp))).g)) |
| 566 | return x |
| 567 | |
| 568 | def polynomial(FiniteFieldElement_pari_ffelt self): |
| 569 | """ |
| 570 | Return the unique representative of ``self`` as a polynomial |
| 571 | over the prime field whose degree is less than the degree of |
| 572 | the finite field over its prime field. |
| 573 | |
| 574 | EXAMPLES:: |
| 575 | |
| 576 | sage: k = FiniteField(3^2, 'a', impl='pari_ffelt') |
| 577 | sage: k.gen().polynomial() |
| 578 | a |
| 579 | |
| 580 | sage: k = FiniteField(3^4, 'alpha', impl='pari_ffelt') |
| 581 | sage: a = k.gen() |
| 582 | sage: a.polynomial() |
| 583 | alpha |
| 584 | sage: (a**2 + 1).polynomial() |
| 585 | alpha^2 + 1 |
| 586 | sage: (a**2 + 1).polynomial().parent() |
| 587 | Univariate Polynomial Ring in alpha over Finite Field of size 3 |
| 588 | """ |
| 589 | sig_on() |
| 590 | return self._parent.polynomial_ring()(pari.new_gen(FF_to_FpXQ_i(self.val))) |
| 591 | |
| 592 | def charpoly(FiniteFieldElement_pari_ffelt self, object var='x'): |
| 593 | """ |
| 594 | Return the characteristic polynomial of ``self``. |
| 595 | |
| 596 | INPUT: |
| 597 | |
| 598 | - ``var`` -- string (default: 'x'): variable name to use. |
| 599 | |
| 600 | EXAMPLE:: |
| 601 | |
| 602 | sage: R.<x> = PolynomialRing(FiniteField(3)) |
| 603 | sage: F.<a> = FiniteField(3^2, modulus=x^2 + 1) |
| 604 | sage: a.charpoly('y') |
| 605 | y^2 + 1 |
| 606 | """ |
| 607 | sig_on() |
| 608 | return self._parent.polynomial_ring(var)(pari.new_gen(FF_charpoly(self.val))) |
| 609 | |
| 610 | def is_square(FiniteFieldElement_pari_ffelt self): |
| 611 | """ |
| 612 | Return ``True`` if and only if ``self`` is a square in the |
| 613 | finite field. |
| 614 | |
| 615 | EXAMPLES:: |
| 616 | |
| 617 | sage: k = FiniteField(3^2, 'a', impl='pari_ffelt') |
| 618 | sage: a = k.gen() |
| 619 | sage: a.is_square() |
| 620 | False |
| 621 | sage: (a**2).is_square() |
| 622 | True |
| 623 | |
| 624 | sage: k = FiniteField(2^2, 'a', impl='pari_ffelt') |
| 625 | sage: a = k.gen() |
| 626 | sage: (a**2).is_square() |
| 627 | True |
| 628 | |
| 629 | sage: k = FiniteField(17^5, 'a', impl='pari_ffelt'); a = k.gen() |
| 630 | sage: (a**2).is_square() |
| 631 | True |
| 632 | sage: a.is_square() |
| 633 | False |
| 634 | sage: k(0).is_square() |
| 635 | True |
| 636 | """ |
| 637 | cdef long i |
| 638 | sig_on() |
| 639 | i = FF_issquare(self.val) |
| 640 | sig_off() |
| 641 | return bool(i) |
| 642 | |
| 643 | def sqrt(FiniteFieldElement_pari_ffelt self, extend=False, all=False): |
| 644 | """ |
| 645 | Return a square root of ``self``, if it exists. |
| 646 | |
| 647 | INPUT: |
| 648 | |
| 649 | - ``extend`` -- bool (default: ``False``) |
| 650 | |
| 651 | .. WARNING:: |
| 652 | |
| 653 | This option is not implemented. |
| 654 | |
| 655 | - ``all`` - bool (default: ``False``) |
| 656 | |
| 657 | OUTPUT: |
| 658 | |
| 659 | A square root of ``self``, if it exists. If ``all`` is |
| 660 | ``True``, a list containing all square roots of ``self`` |
| 661 | (of length zero, one or two) is returned instead. |
| 662 | |
| 663 | If ``extend`` is ``True``, a square root is chosen in an |
| 664 | extension field if necessary. If ``extend`` is ``False``, a |
| 665 | ValueError is raised if the element is not a square in the |
| 666 | base field. |
| 667 | |
| 668 | .. WARNING:: |
| 669 | |
| 670 | The ``extend`` option is not implemented (yet). |
| 671 | |
| 672 | EXAMPLES:: |
| 673 | |
| 674 | sage: F = FiniteField(7^2, 'a', impl='pari_ffelt') |
| 675 | sage: F(2).sqrt() |
| 676 | 4 |
| 677 | sage: F(3).sqrt() |
| 678 | 5*a + 1 |
| 679 | sage: F(3).sqrt()**2 |
| 680 | 3 |
| 681 | sage: F(4).sqrt(all=True) |
| 682 | [2, 5] |
| 683 | |
| 684 | sage: K = FiniteField(7^3, 'alpha', impl='pari_ffelt') |
| 685 | sage: K(3).sqrt() |
| 686 | Traceback (most recent call last): |
| 687 | ... |
| 688 | ValueError: element is not a square |
| 689 | sage: K(3).sqrt(all=True) |
| 690 | [] |
| 691 | |
| 692 | sage: K.<a> = GF(3^17, impl='pari_ffelt') |
| 693 | sage: (a^3 - a - 1).sqrt() |
| 694 | a^16 + 2*a^15 + a^13 + 2*a^12 + a^10 + 2*a^9 + 2*a^8 + a^7 + a^6 + 2*a^5 + a^4 + 2*a^2 + 2*a + 2 |
| 695 | """ |
| 696 | if extend: |
| 697 | raise NotImplementedError |
| 698 | cdef GEN s |
| 699 | cdef FiniteFieldElement_pari_ffelt x, mx |
| 700 | sig_on() |
| 701 | if FF_issquareall(self.val, &s): |
| 702 | x = self._new() |
| 703 | x.construct(s) |
| 704 | if not all: |
| 705 | return x |
| 706 | elif gequal0(x.val) or self._parent.characteristic() == 2: |
| 707 | return [x] |
| 708 | else: |
| 709 | sig_on() |
| 710 | mx = self._new() |
| 711 | mx.construct(FF_neg_i(x.val)) |
| 712 | return [x, mx] |
| 713 | else: |
| 714 | sig_off() |
| 715 | if all: |
| 716 | return [] |
| 717 | else: |
| 718 | raise ValueError("element is not a square") |
| 719 | |
| 720 | def log(FiniteFieldElement_pari_ffelt self, object base): |
| 721 | """ |
| 722 | Return a discrete logarithm of ``self`` with respect to the |
| 723 | given base. |
| 724 | |
| 725 | INPUT: |
| 726 | |
| 727 | - ``base`` -- non-zero field element |
| 728 | |
| 729 | OUTPUT: |
| 730 | |
| 731 | An integer `x` such that ``self`` equals ``base`` raised to |
| 732 | the power `x`. If no such `x` exists, a ``ValueError`` is |
| 733 | raised. |
| 734 | |
| 735 | EXAMPLES:: |
| 736 | |
| 737 | sage: F = FiniteField(2^10, 'a', impl='pari_ffelt') |
| 738 | sage: g = F.gen() |
| 739 | sage: b = g; a = g^37 |
| 740 | sage: a.log(b) |
| 741 | 37 |
| 742 | sage: b^37; a |
| 743 | a^8 + a^7 + a^4 + a + 1 |
| 744 | a^8 + a^7 + a^4 + a + 1 |
| 745 | |
| 746 | sage: F.<a> = FiniteField(5^2, impl='pari_ffelt') |
| 747 | sage: F(-1).log(F(2)) |
| 748 | 2 |
| 749 | """ |
| 750 | # We have to specify the order of the base of the logarithm |
| 751 | # because PARI assumes by default that this element generates |
| 752 | # the multiplicative group. |
| 753 | cdef GEN x, order |
| 754 | base = self._parent(base) |
| 755 | sig_on() |
| 756 | order = FF_order((<FiniteFieldElement_pari_ffelt>base).val, NULL) |
| 757 | x = FF_log(self.val, (<FiniteFieldElement_pari_ffelt>base).val, order) |
| 758 | return Integer(pari.new_gen(x)) |
| 759 | |
| 760 | def multiplicative_order(FiniteFieldElement_pari_ffelt self): |
| 761 | """ |
| 762 | Returns the order of ``self`` in the multiplicative group. |
| 763 | |
| 764 | EXAMPLE:: |
| 765 | |
| 766 | sage: a = FiniteField(5^3, 'a', impl='pari_ffelt').0 |
| 767 | sage: a.multiplicative_order() |
| 768 | 124 |
| 769 | sage: a**124 |
| 770 | 1 |
| 771 | """ |
| 772 | if self.is_zero(): |
| 773 | raise ArithmeticError("Multiplicative order of 0 not defined.") |
| 774 | cdef GEN order |
| 775 | sig_on() |
| 776 | order = FF_order(self.val, NULL) |
| 777 | return Integer(pari.new_gen(order)) |
| 778 | |
| 779 | def lift(FiniteFieldElement_pari_ffelt self): |
| 780 | """ |
| 781 | If ``self`` is an element of the prime field, return a lift of |
| 782 | this element to an integer. |
| 783 | |
| 784 | EXAMPLE:: |
| 785 | |
| 786 | sage: k = FiniteField(next_prime(10^10)^2, 'u', impl='pari_ffelt') |
| 787 | sage: a = k(17)/k(19) |
| 788 | sage: b = a.lift(); b |
| 789 | 7894736858 |
| 790 | sage: b.parent() |
| 791 | Integer Ring |
| 792 | """ |
| 793 | if FF_equal0(self.val): |
| 794 | return Integer(0) |
| 795 | f = self.polynomial() |
| 796 | if f.degree() == 0: |
| 797 | return f.constant_coefficient().lift() |
| 798 | else: |
| 799 | raise ValueError("element is not in the prime field") |
| 800 | |
| 801 | def _integer_(self, ZZ=None): |
| 802 | """ |
| 803 | Lift to a Sage integer, if possible. |
| 804 | |
| 805 | EXAMPLE:: |
| 806 | |
| 807 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 808 | sage: b = k(2) |
| 809 | sage: b._integer_() |
| 810 | 2 |
| 811 | sage: a._integer_() |
| 812 | Traceback (most recent call last): |
| 813 | ... |
| 814 | ValueError: element is not in the prime field |
| 815 | """ |
| 816 | return self.lift() |
| 817 | |
| 818 | def __int__(self): |
| 819 | """ |
| 820 | Lift to a python int, if possible. |
| 821 | |
| 822 | EXAMPLE:: |
| 823 | |
| 824 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 825 | sage: b = k(2) |
| 826 | sage: int(b) |
| 827 | 2 |
| 828 | sage: int(a) |
| 829 | Traceback (most recent call last): |
| 830 | ... |
| 831 | ValueError: element is not in the prime field |
| 832 | """ |
| 833 | return int(self.lift()) |
| 834 | |
| 835 | def __long__(self): |
| 836 | """ |
| 837 | Lift to a python long, if possible. |
| 838 | |
| 839 | EXAMPLE:: |
| 840 | |
| 841 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 842 | sage: b = k(2) |
| 843 | sage: long(b) |
| 844 | 2L |
| 845 | """ |
| 846 | return long(self.lift()) |
| 847 | |
| 848 | def __float__(self): |
| 849 | """ |
| 850 | Lift to a python float, if possible. |
| 851 | |
| 852 | EXAMPLE:: |
| 853 | |
| 854 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 855 | sage: b = k(2) |
| 856 | sage: float(b) |
| 857 | 2.0 |
| 858 | """ |
| 859 | return float(self.lift()) |
| 860 | |
| 861 | def _pari_(self, var=None): |
| 862 | """ |
| 863 | Return a PARI object representing ``self``. |
| 864 | |
| 865 | INPUT: |
| 866 | |
| 867 | - var -- ignored |
| 868 | |
| 869 | EXAMPLE:: |
| 870 | |
| 871 | sage: k = FiniteField(3^3, 'a', impl='pari_ffelt') |
| 872 | sage: a = k.gen() |
| 873 | sage: b = a**2 + 2*a + 1 |
| 874 | sage: b._pari_() |
| 875 | a^2 + 2*a + 1 |
| 876 | """ |
| 877 | sig_on() |
| 878 | return pari.new_gen(self.val) |
| 879 | |
| 880 | def _pari_init_(self): |
| 881 | """ |
| 882 | Return a string representing ``self`` in PARI. |
| 883 | |
| 884 | EXAMPLE:: |
| 885 | |
| 886 | sage: k.<a> = GF(3^17, impl='pari_ffelt') |
| 887 | sage: a._pari_init_() |
| 888 | 'a' |
| 889 | |
| 890 | .. NOTE:: |
| 891 | |
| 892 | To use the output as a field element in the PARI/GP |
| 893 | interpreter, the finite field must have been defined |
| 894 | previously. This can be done using the GP command |
| 895 | ``a = ffgen(f*Mod(1, p))``, |
| 896 | where `p` is the characteristic, `f` is the defining |
| 897 | polynomial and `a` is the name of the generator. |
| 898 | """ |
| 899 | sig_on() |
| 900 | return pari.new_gen_to_string(self.val) |
| 901 | |
| 902 | def _magma_init_(self, magma): |
| 903 | """ |
| 904 | Return a string representing ``self`` in Magma. |
| 905 | |
| 906 | EXAMPLE:: |
| 907 | |
| 908 | sage: GF(7)(3)._magma_init_(magma) # optional - magma |
| 909 | 'GF(7)!3' |
| 910 | """ |
| 911 | k = self._parent |
| 912 | km = magma(k) |
| 913 | return str(self).replace(k.variable_name(), km.gen(1).name()) |
| 914 | |
| 915 | def _gap_init_(self): |
| 916 | r""" |
| 917 | Return the a string representing ``self`` in GAP. |
| 918 | |
| 919 | .. NOTE:: |
| 920 | |
| 921 | The order of the parent field must be `\leq 65536`. This |
| 922 | function can be slow since elements of non-prime finite |
| 923 | fields are represented in GAP as powers of a generator for |
| 924 | the multiplicative group, so a discrete logarithm must be |
| 925 | computed. |
| 926 | |
| 927 | EXAMPLE:: |
| 928 | |
| 929 | sage: F = FiniteField(2^3, 'a', impl='pari_ffelt') |
| 930 | sage: a = F.multiplicative_generator() |
| 931 | sage: gap(a) # indirect doctest |
| 932 | Z(2^3) |
| 933 | sage: b = F.multiplicative_generator() |
| 934 | sage: a = b^3 |
| 935 | sage: gap(a) |
| 936 | Z(2^3)^3 |
| 937 | sage: gap(a^3) |
| 938 | Z(2^3)^2 |
| 939 | |
| 940 | You can specify the instance of the Gap interpreter that is used:: |
| 941 | |
| 942 | sage: F = FiniteField(next_prime(200)^2, 'a', impl='pari_ffelt') |
| 943 | sage: a = F.multiplicative_generator () |
| 944 | sage: a._gap_ (gap) |
| 945 | Z(211^2) |
| 946 | sage: (a^20)._gap_(gap) |
| 947 | Z(211^2)^20 |
| 948 | |
| 949 | Gap only supports relatively small finite fields:: |
| 950 | |
| 951 | sage: F = FiniteField(next_prime(1000)^2, 'a', impl='pari_ffelt') |
| 952 | sage: a = F.multiplicative_generator () |
| 953 | sage: gap._coerce_(a) |
| 954 | Traceback (most recent call last): |
| 955 | ... |
| 956 | TypeError: order must be at most 65536 |
| 957 | """ |
| 958 | F = self._parent |
| 959 | if F.order() > 65536: |
| 960 | raise TypeError("order must be at most 65536") |
| 961 | |
| 962 | if self == 0: |
| 963 | return '0*Z(%s)'%F.order() |
| 964 | assert F.degree() > 1 |
| 965 | g = F.multiplicative_generator() |
| 966 | n = self.log(g) |
| 967 | return 'Z(%s)^%s'%(F.order(), n) |
| 968 | |
| 969 | |
| 970 | def unpickle_FiniteFieldElement_pari_ffelt(parent, elem): |
| 971 | """ |
| 972 | EXAMPLE:: |
| 973 | |
| 974 | sage: k.<a> = GF(2^20, impl='pari_ffelt') |
| 975 | sage: e = k.random_element() |
| 976 | sage: f = loads(dumps(e)) # indirect doctest |
| 977 | sage: e == f |
| 978 | True |
| 979 | """ |
| 980 | return parent(elem) |