aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJ08nY2024-07-11 19:41:51 +0200
committerJ08nY2024-07-11 19:41:51 +0200
commitc6d8fb19e2708f226f2d455f1fa94d7555be5c24 (patch)
tree1ccd6f42db3eb64cde7be050d01dd812af6cc5a8
parent85696e1dc07fbba488e506fd91960207b07e596f (diff)
downloadpyecsca-c6d8fb19e2708f226f2d455f1fa94d7555be5c24.tar.gz
pyecsca-c6d8fb19e2708f226f2d455f1fa94d7555be5c24.tar.zst
pyecsca-c6d8fb19e2708f226f2d455f1fa94d7555be5c24.zip
Fix is_residue in Flint.
-rw-r--r--pyecsca/ec/mod.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/pyecsca/ec/mod.py b/pyecsca/ec/mod.py
index 487dc03..3ac9004 100644
--- a/pyecsca/ec/mod.py
+++ b/pyecsca/ec/mod.py
@@ -7,15 +7,22 @@ dispatches to the implementation chosen by the runtime configuration of the libr
:py:class:`RawMod`. A symbolic implementation based on sympy is available under :py:class:`SymbolicMod`. If
`gmpy2` is installed, a GMP based implementation is available under :py:class:`GMPMod`.
"""
+import contextlib
import random
import secrets
+import warnings
from functools import wraps, lru_cache
from typing import Type, Dict, Any, Tuple, Union
from public import public
from sympy import Expr
-from pyecsca.ec.error import raise_non_invertible, raise_non_residue
+from pyecsca.ec.error import (
+ raise_non_invertible,
+ raise_non_residue,
+ NonResidueError,
+ NonResidueWarning,
+)
from pyecsca.ec.context import ResultAction
from pyecsca.misc.cfg import getconfig
@@ -767,8 +774,16 @@ if has_flint:
return FlintMod(res, self._ctx, ensure=False)
def is_residue(self) -> bool:
- res = self.sqrt()
- return res is not None
+ try:
+ with warnings.catch_warnings(
+ record=True, category=NonResidueWarning
+ ) as warns:
+ self.sqrt()
+ if warns:
+ return False
+ except NonResidueError:
+ return False
+ return True
def sqrt(self) -> "FlintMod":
mod = self.n