diff options
| author | J08nY | 2025-03-28 22:30:51 +0100 |
|---|---|---|
| committer | J08nY | 2025-03-28 22:30:51 +0100 |
| commit | 96d2af41e0321b9fecf2fb4644dfa6a4a4cf0823 (patch) | |
| tree | 6491e0769496dbe46b437626a79daba5d3c0a160 /test/ec | |
| parent | 7afddf743cfdadbaff1a3bf2581c039c6e0816bb (diff) | |
| download | pyecsca-96d2af41e0321b9fecf2fb4644dfa6a4a4cf0823.tar.gz pyecsca-96d2af41e0321b9fecf2fb4644dfa6a4a4cf0823.tar.zst pyecsca-96d2af41e0321b9fecf2fb4644dfa6a4a4cf0823.zip | |
Diffstat (limited to 'test/ec')
| -rw-r--r-- | test/ec/test_mod.py | 38 | ||||
| -rw-r--r-- | test/ec/test_point.py | 36 |
2 files changed, 73 insertions, 1 deletions
diff --git a/test/ec/test_mod.py b/test/ec/test_mod.py index 18eeeac..724ea42 100644 --- a/test/ec/test_mod.py +++ b/test/ec/test_mod.py @@ -17,7 +17,7 @@ from pyecsca.ec.mod import ( miller_rabin, RawMod, SymbolicMod, - jacobi, + jacobi, cube_roots, ) from pyecsca.ec.mod.gmp import has_gmp from pyecsca.ec.mod.flint import has_flint @@ -116,6 +116,42 @@ def test_sqrt(): getconfig().ec.non_residue_action = "error" +def test_is_cubic_residue(): + # p is 2 mod 3 so all are residues + p = 11 + for i in range(11): + assert mod(i, p).is_cubic_residue() + p = 13 + assert not mod(4, p).is_cubic_residue() + assert mod(5, p).is_cubic_residue() + assert not mod(6, p).is_cubic_residue() + assert not mod(7, p).is_cubic_residue() + + +def test_cube_root(): + p = 11 + for i in range(11): + assert len(cube_roots(mod(i, p))) == 1 + assert mod(2, p).cube_root() == 7 + assert mod(3, p).cube_root() == 9 + assert mod(4, p).cube_root() == 5 + assert mod(5, p).cube_root() == 3 + assert mod(6, p).cube_root() == 8 + assert mod(7, p).cube_root() == 6 + + p = 13 + assert mod(1, p).cube_root() == 1 + assert mod(0, p).cube_root() == 0 + assert mod(5, p).cube_root() == 8 + assert mod(8, p).cube_root() == 5 + assert mod(12, p).cube_root() == 12 + + assert cube_roots(mod(0, p)) == {mod(0, p)} + assert cube_roots(mod(1, p)) == {mod(1, p), mod(3, p), mod(9, p)} + assert cube_roots(mod(5, p)) == {mod(8, p), mod(11, p), mod(7, p)} + assert cube_roots(mod(8, p)) == {mod(5, p), mod(2, p), mod(6, p)} + + def test_eq(): assert mod(1, 7) == 1 assert mod(1, 7) != "1" diff --git a/test/ec/test_point.py b/test/ec/test_point.py index 6dfced5..789b4f1 100644 --- a/test/ec/test_point.py +++ b/test/ec/test_point.py @@ -8,6 +8,7 @@ from pyecsca.ec.mod import mod from pyecsca.ec.model import ShortWeierstrassModel, MontgomeryModel from pyecsca.ec.params import get_params from pyecsca.ec.point import Point, InfinityPoint +from pyecsca.ec.error import UnsatisfiedAssumptionError @pytest.fixture() @@ -129,15 +130,23 @@ def test_equals(secp128r1, secp128r1_coords): assert pt.equals_affine(other) assert not pt.equals_scaled(third) + assert pt.equals_homog(pt) + assert pt.equals_homog(other) + assert other.equals_homog(pt) + assert not pt.equals_homog(third) + assert not third.equals_homog(pt) + infty_one = InfinityPoint(secp128r1_coords) infty_other = InfinityPoint(secp128r1_coords) assert infty_one.equals(infty_other) assert infty_one.equals_affine(infty_other) assert infty_one.equals_scaled(infty_other) + assert infty_one.equals_homog(infty_other) assert infty_one == infty_other assert not pt.equals(infty_one) assert not pt.equals_affine(infty_one) assert not pt.equals_scaled(infty_one) + assert not pt.equals_homog(infty_one) mont = MontgomeryModel() different = Point( @@ -152,6 +161,33 @@ def test_equals(secp128r1, secp128r1_coords): assert pt != different +def test_homog(): + model = ShortWeierstrassModel() + for coords_name, coords in model.coordinates.items(): + try: + params = get_params("secg", "secp128r1", coords_name, infty=True) + except UnsatisfiedAssumptionError: + continue + infty = params.curve.neutral + rand_aff = params.curve.affine_random() + one1 = rand_aff.to_model(coords, params.curve) + one2 = rand_aff.to_model(coords, params.curve, randomized=True) + one3 = rand_aff.to_model(coords, params.curve, randomized=True) + assert one1.equals_homog(one2) + assert one1.equals_homog(one3) + assert one2.equals_homog(one3) + assert not one1.equals_homog(infty) + assert infty.equals_homog(infty) + while True: + other_aff = params.curve.affine_random() + if other_aff != rand_aff: + break + other = other_aff.to_model(coords, params.curve) + assert not one1.equals_homog(other) + assert not one2.equals_homog(other) + assert not one3.equals_homog(other) + + def test_bytes(secp128r1, secp128r1_coords): pt = Point( secp128r1_coords, |
