Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 0815aa0

Browse files
committed
trac 30797 fix sqrt corner case for Zmod integers
1 parent f976c52 commit 0815aa0

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/sage/rings/finite_rings/integer_mod.pyx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,12 +1047,10 @@ cdef class IntegerMod_abstract(FiniteRingElement):
10471047

10481048
def sqrt(self, extend=True, all=False):
10491049
r"""
1050-
Returns square root or square roots of ``self`` modulo
1051-
`n`.
1050+
Return square root or square roots of ``self`` modulo `n`.
10521051
10531052
INPUT:
10541053
1055-
10561054
- ``extend`` - bool (default: ``True``);
10571055
if ``True``, return a square root in an extension ring,
10581056
if necessary. Otherwise, raise a ``ValueError`` if the
@@ -1062,7 +1060,6 @@ cdef class IntegerMod_abstract(FiniteRingElement):
10621060
``True``, return {all} square roots of self, instead of
10631061
just one.
10641062
1065-
10661063
ALGORITHM: Calculates the square roots mod `p` for each of
10671064
the primes `p` dividing the order of the ring, then lifts
10681065
them `p`-adically and uses the CRT to find a square root
@@ -1163,7 +1160,7 @@ cdef class IntegerMod_abstract(FiniteRingElement):
11631160

11641161
if not self.is_square_c():
11651162
if extend:
1166-
y = 'sqrt%s'%self
1163+
y = 'sqrt%s' % self
11671164
R = self.parent()['x']
11681165
modulus = R.gen()**2 - R(self)
11691166
if self._parent.is_field():
@@ -2796,12 +2793,10 @@ cdef class IntegerMod_int(IntegerMod_abstract):
27962793

27972794
def sqrt(self, extend=True, all=False):
27982795
r"""
2799-
Returns square root or square roots of ``self`` modulo
2800-
`n`.
2796+
Return square root or square roots of ``self`` modulo `n`.
28012797
28022798
INPUT:
28032799
2804-
28052800
- ``extend`` - bool (default: ``True``);
28062801
if ``True``, return a square root in an extension ring,
28072802
if necessary. Otherwise, raise a ``ValueError`` if the
@@ -2811,7 +2806,6 @@ cdef class IntegerMod_int(IntegerMod_abstract):
28112806
``True``, return {all} square roots of self, instead of
28122807
just one.
28132808
2814-
28152809
ALGORITHM: Calculates the square roots mod `p` for each of
28162810
the primes `p` dividing the order of the ring, then lifts
28172811
them `p`-adically and uses the CRT to find a square root
@@ -2896,6 +2890,13 @@ cdef class IntegerMod_int(IntegerMod_abstract):
28962890
[23, 41, 87, 105]
28972891
sage: [x for x in R if x^2==17]
28982892
[23, 41, 87, 105]
2893+
2894+
TESTS:
2895+
2896+
Check for :trac:`30797`::
2897+
2898+
sage: GF(103)(-1).sqrt(extend=False, all=True)
2899+
[]
28992900
"""
29002901
cdef int_fast32_t i, n = self.__modulus.int32
29012902
if n > 100:
@@ -2906,15 +2907,17 @@ cdef class IntegerMod_int(IntegerMod_abstract):
29062907
if jacobi_int(self.ivalue, self.__modulus.int32) == 1:
29072908
# it's a non-zero square, sqrt(a) = a^(p+1)/4
29082909
i = mod_pow_int(self.ivalue, (self.__modulus.int32+1)/4, n)
2909-
if i > n/2:
2910-
i = n-i
2910+
if i > n / 2:
2911+
i = n - i
29112912
if all:
29122913
return [self._new_c(i), self._new_c(n-i)]
29132914
else:
29142915
return self._new_c(i)
29152916
elif self.ivalue == 0:
29162917
return [self] if all else self
29172918
elif not extend:
2919+
if all:
2920+
return []
29182921
raise ValueError("self must be a square")
29192922
# Now we use a heuristic to guess whether or not it will
29202923
# be faster to just brute-force search for squares in a c loop...

0 commit comments

Comments
 (0)