diff options
| author | J08nY | 2024-07-15 18:15:45 +0200 |
|---|---|---|
| committer | J08nY | 2024-07-15 18:15:45 +0200 |
| commit | 06e005a48af4a704b38f933f500f03a0af2630d3 (patch) | |
| tree | 1712620fc37c5d5798913af491a7e2b851251fd5 /test/ec | |
| parent | ba894fe889d003f2766b7bb90503960fd0429cd5 (diff) | |
| download | pyecsca-06e005a48af4a704b38f933f500f03a0af2630d3.tar.gz pyecsca-06e005a48af4a704b38f933f500f03a0af2630d3.tar.zst pyecsca-06e005a48af4a704b38f933f500f03a0af2630d3.zip | |
Diffstat (limited to 'test/ec')
| -rw-r--r-- | test/ec/test_curve.py | 20 | ||||
| -rw-r--r-- | test/ec/test_formula.py | 4 | ||||
| -rw-r--r-- | test/ec/test_key_agreement.py | 18 | ||||
| -rw-r--r-- | test/ec/test_mod.py | 91 | ||||
| -rw-r--r-- | test/ec/test_mult.py | 16 | ||||
| -rw-r--r-- | test/ec/test_op.py | 12 | ||||
| -rw-r--r-- | test/ec/test_params.py | 8 | ||||
| -rw-r--r-- | test/ec/test_point.py | 54 | ||||
| -rw-r--r-- | test/ec/test_regress.py | 36 | ||||
| -rw-r--r-- | test/ec/test_signature.py | 10 |
10 files changed, 135 insertions, 134 deletions
diff --git a/test/ec/test_curve.py b/test/ec/test_curve.py index cd0639a..42bbcf6 100644 --- a/test/ec/test_curve.py +++ b/test/ec/test_curve.py @@ -4,7 +4,7 @@ import pytest from pyecsca.ec.coordinates import AffineCoordinateModel from pyecsca.ec.curve import EllipticCurve from pyecsca.ec.error import UnsatisfiedAssumptionError -from pyecsca.ec.mod import Mod +from pyecsca.ec.mod import Mod, mod from pyecsca.ec.model import MontgomeryModel from pyecsca.ec.point import Point, InfinityPoint @@ -34,7 +34,7 @@ def test_init(secp128r1): secp128r1.curve.coordinate_model, 15, InfinityPoint(secp128r1.curve.coordinate_model), - parameters={"a": Mod(1, 5), "b": Mod(2, 5)}, + parameters={"a": mod(1, 5), "b": mod(2, 5)}, ) @@ -64,17 +64,17 @@ def test_is_on_curve(secp128r1, curve25519): assert secp128r1.curve.is_on_curve(secp128r1.curve.neutral) pt = Point( secp128r1.curve.coordinate_model, - X=Mod(0x161FF7528B899B2D0C28607CA52C5B86, secp128r1.curve.prime), - Y=Mod(0xCF5AC8395BAFEB13C02DA292DDED7A83, secp128r1.curve.prime), - Z=Mod(1, secp128r1.curve.prime), + X=mod(0x161FF7528B899B2D0C28607CA52C5B86, secp128r1.curve.prime), + Y=mod(0xCF5AC8395BAFEB13C02DA292DDED7A83, secp128r1.curve.prime), + Z=mod(1, secp128r1.curve.prime), ) assert secp128r1.curve.is_on_curve(pt) assert secp128r1.curve.is_on_curve(pt.to_affine()) other = Point( secp128r1.curve.coordinate_model, - X=Mod(0x161FF7528B899B2D0C28607CA52C5B86, secp128r1.curve.prime), - Y=Mod(0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, secp128r1.curve.prime), - Z=Mod(1, secp128r1.curve.prime), + X=mod(0x161FF7528B899B2D0C28607CA52C5B86, secp128r1.curve.prime), + Y=mod(0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, secp128r1.curve.prime), + Z=mod(1, secp128r1.curve.prime), ) assert not secp128r1.curve.is_on_curve(other) assert not secp128r1.curve.is_on_curve(curve25519.generator) @@ -83,8 +83,8 @@ def test_is_on_curve(secp128r1, curve25519): def test_affine_add(secp128r1): pt = Point( AffineCoordinateModel(secp128r1.curve.model), - x=Mod(0xEB916224EDA4FB356421773573297C15, secp128r1.curve.prime), - y=Mod(0xBCDAF32A2C08FD4271228FEF35070848, secp128r1.curve.prime), + x=mod(0xEB916224EDA4FB356421773573297C15, secp128r1.curve.prime), + y=mod(0xBCDAF32A2C08FD4271228FEF35070848, secp128r1.curve.prime), ) affine_base = secp128r1.generator.to_affine() assert secp128r1.curve.affine_add(affine_base, pt) is not None diff --git a/test/ec/test_formula.py b/test/ec/test_formula.py index 7b0bee1..4f64d36 100644 --- a/test/ec/test_formula.py +++ b/test/ec/test_formula.py @@ -21,7 +21,7 @@ from pyecsca.ec.formula.partitions import ( generate_partitioned_formulas, ) from pyecsca.ec.formula.switch_sign import generate_switched_formulas -from pyecsca.ec.mod import SymbolicMod, Mod +from pyecsca.ec.mod import SymbolicMod, Mod, mod from pyecsca.misc.cfg import TemporaryConfig from pyecsca.ec.error import UnsatisfiedAssumptionError from pyecsca.ec.params import get_params, DomainParameters @@ -157,7 +157,7 @@ def test_symbolic(secp128r1, dbl): symbolic_val = symbolic_val.subs( inner_var, int(getattr(secp128r1.generator, inner_var).x) ) - assert Mod(int(symbolic_val), p) == Mod(generator_val, p) + assert mod(int(symbolic_val), p) == mod(generator_val, p) def test_pickle(add, dbl): diff --git a/test/ec/test_key_agreement.py b/test/ec/test_key_agreement.py index 248d172..60e48f8 100644 --- a/test/ec/test_key_agreement.py +++ b/test/ec/test_key_agreement.py @@ -13,7 +13,7 @@ from pyecsca.ec.key_agreement import ( ECDH_SHA384, ECDH_SHA512, ) -from pyecsca.ec.mod import Mod +from pyecsca.ec.mod import Mod, mod from pyecsca.ec.mult import LTRMultiplier import test.data.ec from pyecsca.ec.params import get_params @@ -29,7 +29,7 @@ def mult(secp128r1): @pytest.fixture() def keypair_a(secp128r1, mult): - priv_a = Mod(0xDEADBEEF, secp128r1.order) + priv_a = mod(0xDEADBEEF, secp128r1.order) mult.init(secp128r1, secp128r1.generator) pub_a = mult.multiply(int(priv_a)) return priv_a, pub_a @@ -37,7 +37,7 @@ def keypair_a(secp128r1, mult): @pytest.fixture() def keypair_b(secp128r1, mult): - priv_b = Mod(0xCAFEBABE, secp128r1.order) + priv_b = mod(0xCAFEBABE, secp128r1.order) mult.init(secp128r1, secp128r1.generator) pub_b = mult.multiply(int(priv_b)) return priv_b, pub_b @@ -58,15 +58,15 @@ def test_ka_secg(): add = secp160r1.curve.coordinate_model.formulas["add-2015-rcb"] dbl = secp160r1.curve.coordinate_model.formulas["dbl-2015-rcb"] mult = LTRMultiplier(add, dbl) - privA = Mod(int(secg_data["keyA"]["priv"], 16), secp160r1.order) + privA = mod(int(secg_data["keyA"]["priv"], 16), secp160r1.order) pubA_affine = Point(affine_model, - x=Mod(int(secg_data["keyA"]["pub"]["x"], 16), secp160r1.curve.prime), - y=Mod(int(secg_data["keyA"]["pub"]["y"], 16), secp160r1.curve.prime)) + x=mod(int(secg_data["keyA"]["pub"]["x"], 16), secp160r1.curve.prime), + y=mod(int(secg_data["keyA"]["pub"]["y"], 16), secp160r1.curve.prime)) pubA = pubA_affine.to_model(secp160r1.curve.coordinate_model, secp160r1.curve) - privB = Mod(int(secg_data["keyB"]["priv"], 16), secp160r1.order) + privB = mod(int(secg_data["keyB"]["priv"], 16), secp160r1.order) pubB_affine = Point(affine_model, - x=Mod(int(secg_data["keyB"]["pub"]["x"], 16), secp160r1.curve.prime), - y=Mod(int(secg_data["keyB"]["pub"]["y"], 16), secp160r1.curve.prime)) + x=mod(int(secg_data["keyB"]["pub"]["x"], 16), secp160r1.curve.prime), + y=mod(int(secg_data["keyB"]["pub"]["y"], 16), secp160r1.curve.prime)) pubB = pubB_affine.to_model(secp160r1.curve.coordinate_model, secp160r1.curve) algoAB = ECDH_SHA1(copy(mult), secp160r1, pubA, privB) diff --git a/test/ec/test_mod.py b/test/ec/test_mod.py index 9a6cef0..13f8850 100644 --- a/test/ec/test_mod.py +++ b/test/ec/test_mod.py @@ -4,16 +4,17 @@ import pytest from sympy import FF, symbols from pyecsca.ec.mod import ( + mod, Mod, gcd, extgcd, Undefined, miller_rabin, - has_gmp, RawMod, SymbolicMod, jacobi, ) +from pyecsca.ec.mod.gmp import has_gmp from pyecsca.ec.error import ( NonInvertibleError, NonResidueError, @@ -46,120 +47,120 @@ def test_miller_rabin(): def test_inverse(): p = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF - assert Mod( + assert mod( 0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1, p - ).inverse() == Mod(0x1CB2E5274BBA085C4CA88EEDE75AE77949E7A410C80368376E97AB22EB590F9D, p) + ).inverse() == mod(0x1CB2E5274BBA085C4CA88EEDE75AE77949E7A410C80368376E97AB22EB590F9D, p) with pytest.raises(NonInvertibleError): - Mod(0, p).inverse() + mod(0, p).inverse() with pytest.raises(NonInvertibleError): - Mod(5, 10).inverse() + mod(5, 10).inverse() getconfig().ec.no_inverse_action = "warning" with warnings.catch_warnings(record=True) as w: - Mod(0, p).inverse() + mod(0, p).inverse() assert issubclass(w[0].category, NonInvertibleWarning) with warnings.catch_warnings(record=True) as w: - Mod(5, 10).inverse() + mod(5, 10).inverse() assert issubclass(w[0].category, NonInvertibleWarning) getconfig().ec.no_inverse_action = "ignore" - Mod(0, p).inverse() - Mod(5, 10).inverse() + mod(0, p).inverse() + mod(5, 10).inverse() getconfig().ec.no_inverse_action = "error" def test_is_residue(): - assert Mod(4, 11).is_residue() - assert not Mod(11, 31).is_residue() - assert Mod(0, 7).is_residue() - assert Mod(1, 2).is_residue() + assert mod(4, 11).is_residue() + assert not mod(11, 31).is_residue() + assert mod(0, 7).is_residue() + assert mod(1, 2).is_residue() def test_bit_length(): - x = Mod(3, 5) + x = mod(3, 5) assert x.bit_length() == 2 def test_sqrt(): p = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF - assert Mod( + assert mod( 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC, p ).sqrt() in ( 0x9ADD512515B70D9EC471151C1DEC46625CD18B37BDE7CA7FB2C8B31D7033599D, 0x6522AED9EA48F2623B8EEAE3E213B99DA32E74C9421835804D374CE28FCCA662, ) with pytest.raises(NonResidueError): - Mod( + mod( 0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1, p ).sqrt() getconfig().ec.non_residue_action = "warning" with warnings.catch_warnings(record=True) as w: - Mod( + mod( 0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1, p ).sqrt() assert issubclass(w[0].category, NonResidueWarning) getconfig().ec.non_residue_action = "ignore" - Mod( + mod( 0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1, p ).sqrt() with TemporaryConfig() as cfg: cfg.ec.non_residue_action = "warning" with warnings.catch_warnings(record=True) as w: - Mod( + mod( 0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1, p, ).sqrt() assert issubclass(w[0].category, NonResidueWarning) - assert Mod(0, p).sqrt() == Mod(0, p) + assert mod(0, p).sqrt() == mod(0, p) q = 0x75D44FEE9A71841AE8403C0C251FBAD - assert Mod(0x591E0DB18CF1BD81A11B2985A821EB3, q).sqrt() in \ + assert mod(0x591E0DB18CF1BD81A11B2985A821EB3, q).sqrt() in \ (0x113B41A1A2B73F636E73BE3F9A3716E, 0x64990E4CF7BA44B779CC7DCC8AE8A3F) getconfig().ec.non_residue_action = "error" def test_eq(): - assert Mod(1, 7) == 1 - assert Mod(1, 7) != "1" - assert Mod(1, 7) == Mod(1, 7) - assert Mod(1, 7) != Mod(5, 7) - assert Mod(1, 7) != Mod(1, 5) + assert mod(1, 7) == 1 + assert mod(1, 7) != "1" + assert mod(1, 7) == mod(1, 7) + assert mod(1, 7) != mod(5, 7) + assert mod(1, 7) != mod(1, 5) def test_pow(): - a = Mod(5, 7) + a = mod(5, 7) assert a ** (-1) == a.inverse() - assert a ** 0 == Mod(1, 7) + assert a ** 0 == mod(1, 7) assert a ** (-2) == a.inverse() ** 2 def test_wrong_mod(): - a = Mod(5, 7) - b = Mod(4, 11) + a = mod(5, 7) + b = mod(4, 11) with pytest.raises(ValueError): a + b def test_wrong_pow(): - a = Mod(5, 7) - c = Mod(4, 11) + a = mod(5, 7) + c = mod(4, 11) with pytest.raises(TypeError): a ** c def test_other(): - a = Mod(5, 7) - b = Mod(3, 7) + a = mod(5, 7) + b = mod(3, 7) assert int(-a) == 2 assert str(a) == "5" - assert 6 - a == Mod(1, 7) + assert 6 - a == mod(1, 7) assert a != b - assert a / b == Mod(4, 7) - assert a // b == Mod(4, 7) - assert 5 / b == Mod(4, 7) - assert 5 // b == Mod(4, 7) - assert a / 3 == Mod(4, 7) - assert a // 3 == Mod(4, 7) - assert a + b == Mod(1, 7) - assert 5 + b == Mod(1, 7) - assert a + 3 == Mod(1, 7) + assert a / b == mod(4, 7) + assert a // b == mod(4, 7) + assert 5 / b == mod(4, 7) + assert 5 // b == mod(4, 7) + assert a / 3 == mod(4, 7) + assert a // 3 == mod(4, 7) + assert a + b == mod(1, 7) + assert 5 + b == mod(1, 7) + assert a + 3 == mod(1, 7) assert a != 6 assert hash(a) is not None @@ -198,7 +199,7 @@ def test_implementation(): pytest.skip("Only makes sense if more Mod implementations are available.") with TemporaryConfig() as cfg: cfg.ec.mod_implementation = "python" - assert isinstance(Mod(5, 7), RawMod) + assert isinstance(mod(5, 7), RawMod) def test_symbolic(): diff --git a/test/ec/test_mult.py b/test/ec/test_mult.py index fb10b13..e477b07 100644 --- a/test/ec/test_mult.py +++ b/test/ec/test_mult.py @@ -1,9 +1,9 @@ from itertools import product -from typing import Sequence +from typing import Sequence, List import pytest -from pyecsca.ec.mod import Mod +from pyecsca.ec.mod import Mod, mod from pyecsca.ec.mult import ( DoubleAndAddMultiplier, LTRMultiplier, @@ -191,21 +191,21 @@ def test_ladder(curve25519): ) def test_ladder_full(curve25519, scalar, x, res): p = curve25519.curve.prime - point = Point(curve25519.curve.coordinate_model, X=Mod(x, p), Z=Mod(1, p)) - result = Point(curve25519.curve.coordinate_model, X=Mod(res, p), Z=Mod(1, p)) + point = Point(curve25519.curve.coordinate_model, X=mod(x, p), Z=mod(1, p)) + result = Point(curve25519.curve.coordinate_model, X=mod(res, p), Z=mod(1, p)) mult = LadderMultiplier( curve25519.curve.coordinate_model.formulas["ladd-1987-m"], curve25519.curve.coordinate_model.formulas["dbl-1987-m"], # complete=False ) - fixed = int(Mod(scalar, curve25519.order)) + fixed = int(mod(scalar, curve25519.order)) mult.init(curve25519, point) computed = mult.multiply(fixed) - point_aff = list(curve25519.curve.affine_lift_x(Mod(x, p)))[0] - result_aff = list(curve25519.curve.affine_lift_x(Mod(res, p)))[0] + point_aff = list(curve25519.curve.affine_lift_x(mod(x, p)))[0] + result_aff = list(curve25519.curve.affine_lift_x(mod(res, p)))[0] computed_aff = curve25519.curve.affine_multiply(point_aff, scalar) scale = curve25519.curve.coordinate_model.formulas["scale"] @@ -480,7 +480,7 @@ def test_basic_multipliers(secp128r1, num, add, dbl): + bgmws + combs ) - results = [] + results: List[Point] = [] for mult in mults: mult.init(secp128r1, secp128r1.generator) res = mult.multiply(num) diff --git a/test/ec/test_op.py b/test/ec/test_op.py index ece8cd9..8d6996b 100644 --- a/test/ec/test_op.py +++ b/test/ec/test_op.py @@ -3,7 +3,7 @@ from ast import parse import pytest from pyecsca.ec.formula import OpResult -from pyecsca.ec.mod import Mod +from pyecsca.ec.mod import Mod, mod from pyecsca.ec.op import CodeOp, OpType @@ -21,8 +21,8 @@ def test_str(name, module, result, op_type): @pytest.mark.parametrize("name,module,locals,result", - [("add", "x = a+b", {"a": Mod(5, 21), "b": Mod(7, 21)}, Mod(12, 21)), - ("sub", "x = a-b", {"a": Mod(7, 21), "b": Mod(5, 21)}, Mod(2, 21)), ]) + [("add", "x = a+b", {"a": mod(5, 21), "b": mod(7, 21)}, mod(12, 21)), + ("sub", "x = a-b", {"a": mod(7, 21), "b": mod(5, 21)}, mod(2, 21)), ]) def test_call(name, module, locals, result): code = parse(module, mode="exec") op = CodeOp(code) @@ -31,9 +31,9 @@ def test_call(name, module, locals, result): def test_opresult_repr(): - res = OpResult("a", Mod(7, 11), OpType.Neg, "b") + res = OpResult("a", mod(7, 11), OpType.Neg, "b") assert repr(res) == "a = -b" - res = OpResult("a", Mod(5, 7), OpType.Add, "c", 3) + res = OpResult("a", mod(5, 7), OpType.Add, "c", 3) assert repr(res) == "a = c+3" - res = OpResult("a", Mod(3, 11), OpType.Inv, "d") + res = OpResult("a", mod(3, 11), OpType.Inv, "d") assert repr(res) == "a = 1/d" diff --git a/test/ec/test_params.py b/test/ec/test_params.py index 8ca7252..891a7ca 100644 --- a/test/ec/test_params.py +++ b/test/ec/test_params.py @@ -5,7 +5,7 @@ from importlib_resources import files, as_file import pytest import test.data.ec -from pyecsca.ec.mod import Mod +from pyecsca.ec.mod import Mod, mod from pyecsca.ec.point import Point, InfinityPoint from pyecsca.misc.cfg import TemporaryConfig from pyecsca.ec.coordinates import AffineCoordinateModel @@ -131,11 +131,11 @@ def test_custom_params(): model = ShortWeierstrassModel() coords = model.coordinates["projective"] p = 0xd7d1247f - a = Mod(0xa4a44016, p) - b = Mod(0x73f76716, p) + a = mod(0xa4a44016, p) + b = mod(0x73f76716, p) n = 0xd7d2a475 h = 1 - gx, gy, gz = Mod(0x54eed6d7, p), Mod(0x6f1e55ac, p), Mod(1, p) + gx, gy, gz = mod(0x54eed6d7, p), mod(0x6f1e55ac, p), mod(1, p) generator = Point(coords, X=gx, Y=gy, Z=gz) neutral = InfinityPoint(coords) diff --git a/test/ec/test_point.py b/test/ec/test_point.py index 9513840..f83cc14 100644 --- a/test/ec/test_point.py +++ b/test/ec/test_point.py @@ -2,7 +2,7 @@ import pickle from contextlib import nullcontext as does_not_raise from pyecsca.ec.coordinates import AffineCoordinateModel from pyecsca.ec.params import get_params -from pyecsca.ec.mod import Mod +from pyecsca.ec.mod import Mod, mod from pyecsca.ec.model import ShortWeierstrassModel, MontgomeryModel from pyecsca.ec.point import Point, InfinityPoint import pytest @@ -28,9 +28,9 @@ def test_construction(secp128r1_coords): def test_to_affine(secp128r1, secp128r1_coords, affine_model): pt = Point( secp128r1_coords, - X=Mod(0x161FF7528B899B2D0C28607CA52C5B86, secp128r1.curve.prime), - Y=Mod(0xCF5AC8395BAFEB13C02DA292DDED7A83, secp128r1.curve.prime), - Z=Mod(1, secp128r1.curve.prime), + X=mod(0x161FF7528B899B2D0C28607CA52C5B86, secp128r1.curve.prime), + Y=mod(0xCF5AC8395BAFEB13C02DA292DDED7A83, secp128r1.curve.prime), + Z=mod(1, secp128r1.curve.prime), ) affine = pt.to_affine() @@ -55,8 +55,8 @@ def test_to_affine(secp128r1, secp128r1_coords, affine_model): def test_to_model(secp128r1, secp128r1_coords, affine_model): affine = Point( affine_model, - x=Mod(0xABCD, secp128r1.curve.prime), - y=Mod(0xEF, secp128r1.curve.prime), + x=mod(0xABCD, secp128r1.curve.prime), + y=mod(0xEF, secp128r1.curve.prime), ) other = affine.to_model(secp128r1_coords, secp128r1.curve) @@ -64,7 +64,7 @@ def test_to_model(secp128r1, secp128r1_coords, affine_model): assert set(other.coords.keys()) == set(secp128r1_coords.variables) assert other.coords["X"] == affine.coords["x"] assert other.coords["Y"] == affine.coords["y"] - assert other.coords["Z"] == Mod(1, secp128r1.curve.prime) + assert other.coords["Z"] == mod(1, secp128r1.curve.prime) infty = InfinityPoint(AffineCoordinateModel(secp128r1.curve.model)) other_infty = infty.to_model(secp128r1_coords, secp128r1.curve) @@ -101,21 +101,21 @@ def test_to_from_affine(category, curve, coords, raises): def test_equals(secp128r1, secp128r1_coords): pt = Point( secp128r1_coords, - X=Mod(0x4, secp128r1.curve.prime), - Y=Mod(0x6, secp128r1.curve.prime), - Z=Mod(2, secp128r1.curve.prime), + X=mod(0x4, secp128r1.curve.prime), + Y=mod(0x6, secp128r1.curve.prime), + Z=mod(2, secp128r1.curve.prime), ) other = Point( secp128r1_coords, - X=Mod(0x2, secp128r1.curve.prime), - Y=Mod(0x3, secp128r1.curve.prime), - Z=Mod(1, secp128r1.curve.prime), + X=mod(0x2, secp128r1.curve.prime), + Y=mod(0x3, secp128r1.curve.prime), + Z=mod(1, secp128r1.curve.prime), ) third = Point( secp128r1_coords, - X=Mod(0x5, secp128r1.curve.prime), - Y=Mod(0x3, secp128r1.curve.prime), - Z=Mod(1, secp128r1.curve.prime), + X=mod(0x5, secp128r1.curve.prime), + Y=mod(0x3, secp128r1.curve.prime), + Z=mod(1, secp128r1.curve.prime), ) assert pt.equals(other) assert pt != other @@ -140,11 +140,11 @@ def test_equals(secp128r1, secp128r1_coords): mont = MontgomeryModel() different = Point( mont.coordinates["xz"], - X=Mod( + X=mod( 0x64DACCD2656420216545E5F65221EB, 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, ), - Z=Mod(1, 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA), + Z=mod(1, 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA), ) assert not pt.equals(different) assert pt != different @@ -153,9 +153,9 @@ def test_equals(secp128r1, secp128r1_coords): def test_bytes(secp128r1, secp128r1_coords): pt = Point( secp128r1_coords, - X=Mod(0x4, secp128r1.curve.prime), - Y=Mod(0x6, secp128r1.curve.prime), - Z=Mod(2, secp128r1.curve.prime), + X=mod(0x4, secp128r1.curve.prime), + Y=mod(0x6, secp128r1.curve.prime), + Z=mod(2, secp128r1.curve.prime), ) assert bytes(pt) == \ b"\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" @@ -165,9 +165,9 @@ def test_bytes(secp128r1, secp128r1_coords): def test_iter(secp128r1, secp128r1_coords): pt = Point( secp128r1_coords, - X=Mod(0x4, secp128r1.curve.prime), - Y=Mod(0x6, secp128r1.curve.prime), - Z=Mod(2, secp128r1.curve.prime), + X=mod(0x4, secp128r1.curve.prime), + Y=mod(0x6, secp128r1.curve.prime), + Z=mod(2, secp128r1.curve.prime), ) t = tuple(pt) assert len(t) == 3 @@ -180,9 +180,9 @@ def test_iter(secp128r1, secp128r1_coords): def test_pickle(secp128r1, secp128r1_coords): pt = Point( secp128r1_coords, - X=Mod(0x4, secp128r1.curve.prime), - Y=Mod(0x6, secp128r1.curve.prime), - Z=Mod(2, secp128r1.curve.prime), + X=mod(0x4, secp128r1.curve.prime), + Y=mod(0x6, secp128r1.curve.prime), + Z=mod(2, secp128r1.curve.prime), ) pickle.dumps(secp128r1_coords) assert pt == pickle.loads(pickle.dumps(pt)) diff --git a/test/ec/test_regress.py b/test/ec/test_regress.py index 8d54e98..cbfb08b 100644 --- a/test/ec/test_regress.py +++ b/test/ec/test_regress.py @@ -7,7 +7,7 @@ from pyecsca.ec.coordinates import AffineCoordinateModel from pyecsca.ec.curve import EllipticCurve from pyecsca.ec.error import UnsatisfiedAssumptionError from pyecsca.ec.formula import AdditionFormula, DoublingFormula, ScalingFormula -from pyecsca.ec.mod import Mod, SymbolicMod +from pyecsca.ec.mod import Mod, SymbolicMod, mod from pyecsca.ec.model import MontgomeryModel, EdwardsModel from pyecsca.ec.params import get_params from pyecsca.ec.mult import LTRMultiplier @@ -48,13 +48,13 @@ def test_issue_9(): model = MontgomeryModel() coords = model.coordinates["xz"] p = 19 - neutral = Point(coords, X=Mod(1, p), Z=Mod(0, p)) - curve = EllipticCurve(model, coords, p, neutral, {"a": Mod(8, p), "b": Mod(1, p)}) - base = Point(coords, X=Mod(12, p), Z=Mod(1, p)) + neutral = Point(coords, X=mod(1, p), Z=mod(0, p)) + curve = EllipticCurve(model, coords, p, neutral, {"a": mod(8, p), "b": mod(1, p)}) + base = Point(coords, X=mod(12, p), Z=mod(1, p)) formula = coords.formulas["dbl-1987-m-2"] res = formula(p, base, **curve.parameters)[0] assert res is not None - affine_base = Point(AffineCoordinateModel(model), x=Mod(12, p), y=Mod(2, p)) + affine_base = Point(AffineCoordinateModel(model), x=mod(12, p), y=mod(2, p)) dbase = curve.affine_double(affine_base).to_model(coords, curve) ladder = coords.formulas["ladd-1987-m-3"] one, other = ladder(p, base, dbase, base, **curve.parameters) @@ -67,14 +67,14 @@ def test_issue_10(): coords = model.coordinates["yz"] coords_sqr = model.coordinates["yzsquared"] p = 0x1D - c = Mod(1, p) - d = Mod(0x1C, p) + c = mod(1, p) + d = mod(0x1C, p) r = d.sqrt() - neutral = Point(coords, Y=c * r, Z=Mod(1, p)) + neutral = Point(coords, Y=c * r, Z=mod(1, p)) curve = EllipticCurve(model, coords, p, neutral, {"c": c, "d": d, "r": r}) - neutral_affine = Point(AffineCoordinateModel(model), x=Mod(0, p), y=c) + neutral_affine = Point(AffineCoordinateModel(model), x=mod(0, p), y=c) assert neutral == neutral_affine.to_model(coords, curve) - neutral_sqr = Point(coords_sqr, Y=c ** 2 * r, Z=Mod(1, p)) + neutral_sqr = Point(coords_sqr, Y=c ** 2 * r, Z=mod(1, p)) assert neutral_sqr == neutral_affine.to_model(coords_sqr, curve) @@ -103,23 +103,23 @@ def test_issue_14(): with pytest.raises(UnsatisfiedAssumptionError): # p is 3 mod 4, so there is no square root of -1 p = 19 - c = Mod(2, p) - d = Mod(10, p) + c = mod(2, p) + d = mod(10, p) curve = EllipticCurve(model, coords, p, InfinityPoint(coords), {"c": c, "d": d}) - Paff = Point(affine, x=Mod(0xD, p), y=Mod(0x9, p)) + Paff = Point(affine, x=mod(0xD, p), y=mod(0x9, p)) P = Paff.to_model(coords, curve) - Qaff = Point(affine, x=Mod(0x4, p), y=Mod(0x12, p)) + Qaff = Point(affine, x=mod(0x4, p), y=mod(0x12, p)) Q = Qaff.to_model(coords, curve) formula(p, P, Q, **curve.parameters)[0] # p is 1 mod 4, so there is a square root of -1 p = 29 - c = Mod(2, p) - d = Mod(10, p) + c = mod(2, p) + d = mod(10, p) curve = EllipticCurve(model, coords, p, InfinityPoint(coords), {"c": c, "d": d}) - Paff = Point(affine, x=Mod(0xD, p), y=Mod(0x9, p)) + Paff = Point(affine, x=mod(0xD, p), y=mod(0x9, p)) P = Paff.to_model(coords, curve) - Qaff = Point(affine, x=Mod(0x4, p), y=Mod(0x12, p)) + Qaff = Point(affine, x=mod(0x4, p), y=mod(0x12, p)) Q = Qaff.to_model(coords, curve) PQaff = curve.affine_add(Paff, Qaff) R = formula(p, P, Q, **curve.parameters)[0] diff --git a/test/ec/test_signature.py b/test/ec/test_signature.py index 1244d6e..df00aa6 100644 --- a/test/ec/test_signature.py +++ b/test/ec/test_signature.py @@ -4,7 +4,7 @@ import pytest from importlib_resources import files import test.data.ec from pyecsca.ec.coordinates import AffineCoordinateModel -from pyecsca.ec.mod import Mod +from pyecsca.ec.mod import Mod, mod from pyecsca.ec.mult import LTRMultiplier from pyecsca.ec.params import get_params from pyecsca.ec.point import Point @@ -33,7 +33,7 @@ def mult(secp128r1, add): @pytest.fixture() def keypair(secp128r1, mult): - priv = Mod(0xDEADBEEF, secp128r1.order) + priv = mod(0xDEADBEEF, secp128r1.order) mult.init(secp128r1, secp128r1.generator) pub = mult.multiply(int(priv)) return priv, pub @@ -126,11 +126,11 @@ def test_ecdsa_nist(): add = P192.curve.coordinate_model.formulas["add-2015-rcb"] dbl = P192.curve.coordinate_model.formulas["dbl-2015-rcb"] mult = LTRMultiplier(add, dbl) - priv = Mod(int(nist_data["priv"], 16), P192.order) + priv = mod(int(nist_data["priv"], 16), P192.order) pub_affine = Point(affine_model, - x=Mod(int(nist_data["pub"]["x"], 16), P192.curve.prime), - y=Mod(int(nist_data["pub"]["y"], 16), P192.curve.prime)) + x=mod(int(nist_data["pub"]["x"], 16), P192.curve.prime), + y=mod(int(nist_data["pub"]["y"], 16), P192.curve.prime)) pub = pub_affine.to_model(P192.curve.coordinate_model, P192.curve) signer = ECDSA_SHA1(mult, P192, add, pub, priv) |
