@@ -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)
0 commit comments