Skip to content

Commit 8dbc8e7

Browse files
author
Release Manager
committed
sagemathgh-40273: Avoid PariError on finite field construction previously the following raise error ``` sage: F.<I> = GF((2^128+51)^2); F ``` now it doesn't. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40273 Reported by: user202729 Reviewer(s): Frédéric Chapoton
2 parents 57437ee + bc996fa commit 8dbc8e7

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/sage/rings/finite_rings/element_pari_ffelt.pyx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,17 @@ cdef class FiniteFieldElement_pari_ffelt(FinitePolyExtElement):
538538
sage: k.<c> = GF(3^17, impl='pari_ffelt')
539539
sage: c^20 # indirect doctest
540540
c^4 + 2*c^3
541+
542+
TESTS::
543+
544+
sage: F.<I> = GF((2^128+51)^2)
545+
sage: I
546+
I
541547
"""
542-
return str(new_gen_noclear(self.val))
548+
s = str(new_gen_noclear(self.val))
549+
if self._parent._need_replace_varname:
550+
s = s.replace("x", self._parent._names[0]) # .variable_name() is slower
551+
return s
543552

544553
def __hash__(self):
545554
"""
@@ -1304,7 +1313,17 @@ cdef class FiniteFieldElement_pari_ffelt(FinitePolyExtElement):
13041313
sage: b = a**2 + 2*a + 1
13051314
sage: b.__pari__()
13061315
a^2 + 2*a + 1
1316+
1317+
TESTS::
1318+
1319+
sage: F.<I> = GF((2^128+51)^2)
1320+
sage: pari(I)
1321+
Traceback (most recent call last):
1322+
...
1323+
ValueError: variable name illegal in PARI
13071324
"""
1325+
if self._parent._need_replace_varname:
1326+
raise ValueError("variable name illegal in PARI")
13081327
return new_gen_noclear(self.val)
13091328

13101329
def _pari_init_(self):

src/sage/rings/finite_rings/finite_field_pari_ffelt.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#*****************************************************************************
1818

1919

20+
from cypari2.handle_error import PariError
2021
from .element_pari_ffelt import FiniteFieldElement_pari_ffelt
2122
from .finite_field_base import FiniteField
2223
from .finite_field_constructor import GF
@@ -110,6 +111,13 @@ def __init__(self, p, modulus, name=None):
110111
sage: R.<x> = PolynomialRing(GF(3))
111112
sage: k = FiniteField_pari_ffelt(3, x^2 + 2*x + 2, 'a'); k
112113
Finite Field in a of size 3^2
114+
115+
TESTS::
116+
117+
sage: F.<I> = GF((2^128+51)^2); F
118+
Finite Field in I of size 340282366920938463463374607431768211507^2
119+
sage: type(F)
120+
<class 'sage.rings.finite_rings.finite_field_pari_ffelt.FiniteField_pari_ffelt_with_category'>
113121
"""
114122
n = modulus.degree()
115123
if n < 2:
@@ -120,7 +128,13 @@ def __init__(self, p, modulus, name=None):
120128
self._modulus = modulus
121129
self._degree = n
122130

123-
self._gen_pari = modulus._pari_with_name(self._names[0]).ffgen()
131+
self._need_replace_varname = False
132+
try:
133+
modulus_pari = modulus._pari_with_name(self.variable_name())
134+
except PariError:
135+
self._need_replace_varname = True
136+
modulus_pari = modulus._pari_with_name()
137+
self._gen_pari = modulus_pari.ffgen()
124138
self._zero_element = self.element_class(self, 0)
125139
self._one_element = self.element_class(self, 1)
126140
self._gen = self.element_class(self, self._gen_pari)

src/sage/rings/polynomial/polynomial_element.pyx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7150,6 +7150,10 @@ cdef class Polynomial(CommutativePolynomial):
71507150
Return polynomial as a PARI object with topmost variable
71517151
``name``. By default, use 'x' for the variable name.
71527152
7153+
It is not recommended to pass a user-specified value
7154+
as the variable name, because some names such as ``I``
7155+
are reserved. See :issue:`20631`, :issue:`40273`.
7156+
71537157
For internal use only.
71547158
71557159
EXAMPLES::
@@ -7159,6 +7163,13 @@ cdef class Polynomial(CommutativePolynomial):
71597163
2*x^2 + x
71607164
sage: (2*a^2 + a)._pari_with_name('y') # needs sage.libs.pari
71617165
2*y^2 + y
7166+
7167+
TESTS::
7168+
7169+
sage: (2*a^2 + a)._pari_with_name('I') # needs sage.libs.pari
7170+
Traceback (most recent call last):
7171+
...
7172+
cypari2.handle_error.PariError: I already exists with incompatible valence
71627173
"""
71637174
vals = [x.__pari__() for x in self.list()]
71647175
return pari(vals).Polrev(name)

0 commit comments

Comments
 (0)