aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca
diff options
context:
space:
mode:
authorJ08nY2024-07-11 19:28:02 +0200
committerJ08nY2024-07-11 19:28:02 +0200
commit85696e1dc07fbba488e506fd91960207b07e596f (patch)
tree757a5bbff1e1e2273ed9e4b88476c5a307950a7d /pyecsca
parent455a335103f207d0471d1ce952a62e83df985ffb (diff)
downloadpyecsca-85696e1dc07fbba488e506fd91960207b07e596f.tar.gz
pyecsca-85696e1dc07fbba488e506fd91960207b07e596f.tar.zst
pyecsca-85696e1dc07fbba488e506fd91960207b07e596f.zip
Use fast sqrtmod in Flint.
Diffstat (limited to 'pyecsca')
-rw-r--r--pyecsca/ec/mod.py18
1 files changed, 6 insertions, 12 deletions
diff --git a/pyecsca/ec/mod.py b/pyecsca/ec/mod.py
index 978b853..487dc03 100644
--- a/pyecsca/ec/mod.py
+++ b/pyecsca/ec/mod.py
@@ -767,23 +767,17 @@ if has_flint:
return FlintMod(res, self._ctx, ensure=False)
def is_residue(self) -> bool:
- mod = self.n
- if not _fmpz_is_prime(mod):
- raise NotImplementedError
- if self.x == 0:
- return True
- if mod == 2:
- return self.x in (0, 1)
- legendre_symbol = jacobi(int(self.x), int(mod))
- return legendre_symbol == 1
+ res = self.sqrt()
+ return res is not None
def sqrt(self) -> "FlintMod":
mod = self.n
if not _fmpz_is_prime(mod):
raise NotImplementedError
- if self.x == 0:
- return FlintMod(self._ctx(0), self._ctx, ensure=False)
- if not self.is_residue():
+ try:
+ res = flint.fmpz(int(self.x)).sqrtmod(mod)
+ return FlintMod(self._ctx(res), self._ctx, ensure=False)
+ except ValueError:
raise_non_residue()
if mod % 4 == 3: