aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/mod.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyecsca/ec/mod.py')
-rw-r--r--pyecsca/ec/mod.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/pyecsca/ec/mod.py b/pyecsca/ec/mod.py
index a142b71..2b16a07 100644
--- a/pyecsca/ec/mod.py
+++ b/pyecsca/ec/mod.py
@@ -1,7 +1,10 @@
+import secrets
from functools import wraps
from public import public
+from .context import Action
+
@public
def gcd(a, b):
@@ -48,6 +51,18 @@ def check(func):
return method
+@public
+class RandomModAction(Action):
+ """A random sampling from Z_n."""
+ order: int
+
+ def __init__(self, order: int):
+ super().__init__()
+ self.order = order
+
+ def __repr__(self):
+ return f"{self.__class__.__name__}({self.order:x})"
+
@public
class Mod(object):
@@ -120,6 +135,11 @@ class Mod(object):
q, r = divmod(self.x, divisor.x)
return Mod(q, self.n), Mod(r, self.n)
+ @staticmethod
+ def random(n: int):
+ with RandomModAction(n):
+ return Mod(secrets.randbelow(n), n)
+
def __int__(self):
return self.x