Skip to content

Commit 2217e9e

Browse files
author
Release Manager
committed
gh-37172: Fix three polynomial SIGABRT bugs #37169 and #37317 Fix #37169. Fix #37317. Changes: adding `sig_on()` and `sig_off()` where appropriate. URL: #37172 Reported by: grhkm21 Reviewer(s): Giacomo Pope, grhkm21
2 parents 3aa2bde + 4d7c349 commit 2217e9e

File tree

5 files changed

+85
-16
lines changed

5 files changed

+85
-16
lines changed

src/sage/libs/flint/nmod_poly_linkage.pxi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,13 @@ cdef inline int celement_gcd(nmod_poly_t res, nmod_poly_t a, nmod_poly_t b, unsi
582582
nmod_poly_set(res, a)
583583
return 0
584584

585-
nmod_poly_gcd(res, a, b)
586-
cdef unsigned long leadcoeff = nmod_poly_get_coeff_ui(res, nmod_poly_degree(res))
587-
cdef unsigned long modulus = nmod_poly_modulus(res)
588-
if n_gcd(modulus,leadcoeff) == 1:
589-
nmod_poly_make_monic(res, res)
585+
# A check that the leading coefficients are invertible is *not* sufficient
586+
try:
587+
sig_on()
588+
nmod_poly_gcd(res, a, b)
589+
sig_off()
590+
except RuntimeError:
591+
raise ValueError("non-invertible elements encountered during GCD")
590592

591593
cdef inline int celement_xgcd(nmod_poly_t res, nmod_poly_t s, nmod_poly_t t, nmod_poly_t a, nmod_poly_t b, unsigned long n) except -2:
592594
"""

src/sage/libs/ntl/ntl_ZZ_pEX_linkage.pxi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,11 @@ cdef inline int celement_pow(ZZ_pEX_c* res, ZZ_pEX_c* x, long e, ZZ_pEX_c *modul
367367
sig_off()
368368
else:
369369
if ZZ_pEX_deg(modulus[0]) == 1:
370-
ZZ_pEX_rem(y, x[0], modulus[0])
371-
sig_on()
372-
ZZ_pEX_power(res[0], y, e)
373-
sig_off()
374-
return 0
370+
ZZ_pEX_rem(y, x[0], modulus[0])
371+
sig_on()
372+
ZZ_pEX_power(res[0], y, e)
373+
sig_off()
374+
return 0
375375
ZZ_pEX_Modulus_build(mod, modulus[0])
376376
if ZZ_pEX_deg(x[0]) < ZZ_pEX_deg(modulus[0]):
377377
sig_on()

src/sage/rings/polynomial/polynomial_template.pxi

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,35 @@ cdef class Polynomial_template(Polynomial):
352352
x + 1
353353
sage: f.gcd(x^2)
354354
x
355+
356+
TESTS:
357+
358+
Ensure non-invertible elements does not crash Sage (:issue:`37317`)::
359+
360+
sage: R.<x> = Zmod(4)[]
361+
sage: f = R(2 * x)
362+
sage: f.gcd(f)
363+
Traceback (most recent call last):
364+
...
365+
ValueError: leading coefficient must be invertible
366+
367+
::
368+
369+
sage: f = x^2 + 3 * x + 1
370+
sage: g = x^2 + x + 1
371+
sage: f.gcd(g)
372+
Traceback (most recent call last):
373+
...
374+
ValueError: non-invertible elements encountered during GCD
355375
"""
356-
if(celement_is_zero(&self.x, (<Polynomial_template>self)._cparent)):
376+
if celement_is_zero(&self.x, (<Polynomial_template>self)._cparent):
357377
return other
358-
if(celement_is_zero(&other.x, (<Polynomial_template>self)._cparent)):
378+
if celement_is_zero(&other.x, (<Polynomial_template>self)._cparent):
359379
return self
380+
if celement_equal(&self.x, &other.x, (<Polynomial_template>self)._cparent):
381+
# note: gcd(g, g) "canonicalizes" the generator i.e. make polynomials monic
382+
# c.f. ring/ring.pyx:445
383+
return self.monic()
360384

361385
cdef type T = type(self)
362386
cdef Polynomial_template r = <Polynomial_template>T.__new__(T)
@@ -569,7 +593,6 @@ cdef class Polynomial_template(Polynomial):
569593
"""
570594
EXAMPLES::
571595
572-
sage: P.<x> = GF(2)[]
573596
sage: P.<x> = GF(2)[]
574597
sage: x^1000
575598
x^1000
@@ -583,6 +606,28 @@ cdef class Polynomial_template(Polynomial):
583606
x^9 + x^8 + x^7 + x^5 + x^3
584607
sage: pow(f, 2, h)
585608
x^9 + x^8 + x^7 + x^5 + x^3
609+
610+
TESTS:
611+
612+
Ensure modulo `0` and modulo `1` does not crash (:issue:`37169`)::
613+
614+
sage: R.<x> = GF(2)[]
615+
sage: pow(x + 1, 2, R.zero())
616+
Traceback (most recent call last):
617+
...
618+
ZeroDivisionError: modulus must be nonzero
619+
sage: pow(x + 1, 2, R.one())
620+
0
621+
622+
::
623+
624+
sage: R.<x> = GF(2^8)[]
625+
sage: pow(x + 1, 2, R.zero())
626+
Traceback (most recent call last):
627+
...
628+
ZeroDivisionError: modulus must be nonzero
629+
sage: pow(x + 1, 2, R.one())
630+
0
586631
"""
587632
if not isinstance(self, Polynomial_template):
588633
raise NotImplementedError("%s^%s not defined."%(ee,self))
@@ -615,6 +660,10 @@ cdef class Polynomial_template(Polynomial):
615660
else:
616661
if parent is not (<Polynomial_template>modulus)._parent and parent != (<Polynomial_template>modulus)._parent:
617662
modulus = parent.coerce(modulus)
663+
if celement_is_zero(&(<Polynomial_template>modulus).x, (<Polynomial_template>self)._cparent):
664+
raise ZeroDivisionError("modulus must be nonzero")
665+
if celement_is_one(&(<Polynomial_template>modulus).x, (<Polynomial_template>self)._cparent):
666+
return parent.zero()
618667
celement_pow(&r.x, &(<Polynomial_template>self).x, e, &(<Polynomial_template>modulus).x, (<Polynomial_template>self)._cparent)
619668

620669
#assert(r._parent(pari(self)**ee) == r)

src/sage/rings/polynomial/polynomial_zmod_flint.pyx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,16 @@ cdef class Polynomial_zmod_flint(Polynomial_template):
532532
(3*x^4 + 2*x^3 + x^2 + 2*x, x^4 + 3*x^3 + x^2 + x)
533533
sage: (p*d % x^9) == n
534534
True
535+
536+
Check that :issue:`37169` is fixed - it does not throw an error::
537+
538+
sage: R.<x> = Zmod(4)[]
539+
sage: _.<z> = R.quotient_ring(x^2 - 1)
540+
sage: c = 2 * z + 1
541+
sage: c * Zmod(2).zero()
542+
Traceback (most recent call last):
543+
...
544+
RuntimeError: Aborted
535545
"""
536546
if n_deg < 0 or d_deg < 0:
537547
raise ValueError("The degree bounds n_deg and d_deg should be positive.")
@@ -554,7 +564,11 @@ cdef class Polynomial_zmod_flint(Polynomial_template):
554564
while nmod_poly_length(&t1.x) != 0 and n_deg < nmod_poly_degree(&t1.x):
555565
q = self._new()
556566
r1 = self._new()
567+
568+
sig_on()
557569
nmod_poly_divrem(&q.x, &r1.x, &s1.x, &t1.x)
570+
sig_off()
571+
558572
r0 = s0 - q*t0
559573
s0 = t0
560574
s1 = t1
@@ -743,9 +757,12 @@ cdef class Polynomial_zmod_flint(Polynomial_template):
743757
...
744758
ValueError: leading coefficient must be invertible
745759
"""
746-
if self.base_ring().characteristic().gcd(
747-
self.leading_coefficient().lift()) != 1:
760+
cdef unsigned long leadcoeff, modulus
761+
leadcoeff = nmod_poly_get_coeff_ui(&self.x, nmod_poly_degree(&self.x))
762+
modulus = nmod_poly_modulus(&self.x)
763+
if leadcoeff > 1 and n_gcd(modulus, leadcoeff) != 1:
748764
raise ValueError("leading coefficient must be invertible")
765+
749766
cdef Polynomial_zmod_flint res = self._new()
750767
nmod_poly_make_monic(&res.x, &self.x)
751768
return res

src/sage/rings/ring.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,8 @@ cdef class Ring(ParentWithGens):
441441
g = gens[0]
442442
if len(gens) == 1:
443443
try:
444-
g = g.gcd(g) # note: we set g = gcd(g, g) to "canonicalize" the generator: make polynomials monic, etc.
444+
# note: we set g = gcd(g, g) to "canonicalize" the generator: make polynomials monic, etc.
445+
g = g.gcd(g)
445446
except (AttributeError, NotImplementedError):
446447
pass
447448
else:

0 commit comments

Comments
 (0)