aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJ08nY2025-03-20 10:01:50 +0100
committerJ08nY2025-03-20 10:01:50 +0100
commit3169390d69e41ba07f7825efc78c86284f3cb4e1 (patch)
tree3d8e6f8e057b98adbee0196f3810c6c657129794
parent49ea01816d5e4acaa8996da04072936206bbb58f (diff)
downloadpyecsca-3169390d69e41ba07f7825efc78c86284f3cb4e1.tar.gz
pyecsca-3169390d69e41ba07f7825efc78c86284f3cb4e1.tar.zst
pyecsca-3169390d69e41ba07f7825efc78c86284f3cb4e1.zip
Allow Mod formatting.
-rw-r--r--pyecsca/ec/mod/base.py3
-rw-r--r--test/ec/test_mod.py4
2 files changed, 6 insertions, 1 deletions
diff --git a/pyecsca/ec/mod/base.py b/pyecsca/ec/mod/base.py
index f5aab09..b7aa48a 100644
--- a/pyecsca/ec/mod/base.py
+++ b/pyecsca/ec/mod/base.py
@@ -262,6 +262,9 @@ class Mod:
def __str__(self):
return str(self.x)
+ def __format__(self, format_spec):
+ return format(int(self), format_spec)
+
@public
class Undefined(Mod):
diff --git a/test/ec/test_mod.py b/test/ec/test_mod.py
index 07f5bdf..18eeeac 100644
--- a/test/ec/test_mod.py
+++ b/test/ec/test_mod.py
@@ -20,6 +20,7 @@ from pyecsca.ec.mod import (
jacobi,
)
from pyecsca.ec.mod.gmp import has_gmp
+from pyecsca.ec.mod.flint import has_flint
from pyecsca.misc.cfg import getconfig, TemporaryConfig
@@ -149,6 +150,7 @@ def test_other():
b = mod(3, 7)
assert int(-a) == 2
assert str(a) == "5"
+ assert f"{a:02x}" == "05"
assert 6 - a == mod(1, 7)
assert a != b
assert a / b == mod(4, 7)
@@ -199,7 +201,7 @@ def test_undefined():
def test_implementation():
- if not has_gmp:
+ if not has_gmp or has_flint:
pytest.skip("Only makes sense if more Mod implementations are available.")
with TemporaryConfig() as cfg:
cfg.ec.mod_implementation = "python"