aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorJ08nY2023-12-05 14:19:39 +0100
committerJ08nY2023-12-05 14:19:39 +0100
commitafe53219410458137e214e2ce6fd68f421797552 (patch)
tree5752a7d1eb55f49c444124ad0c02e5ae3a57d824 /test
parent3860a7ca356e334e4a5c7419e774e49f79408905 (diff)
downloadpyecsca-afe53219410458137e214e2ce6fd68f421797552.tar.gz
pyecsca-afe53219410458137e214e2ce6fd68f421797552.tar.zst
pyecsca-afe53219410458137e214e2ce6fd68f421797552.zip
Unify formula gen and formula tests.
Diffstat (limited to 'test')
-rw-r--r--test/ec/test_formula.py105
-rw-r--r--test/sca/test_structural.py315
2 files changed, 94 insertions, 326 deletions
diff --git a/test/ec/test_formula.py b/test/ec/test_formula.py
index 0e19d91..d452b97 100644
--- a/test/ec/test_formula.py
+++ b/test/ec/test_formula.py
@@ -1,4 +1,5 @@
import pickle
+from operator import itemgetter
from typing import Tuple
import pytest
@@ -7,6 +8,7 @@ from importlib_resources import files, as_file
import test.data.formulas
from pyecsca.ec.formula.fliparoo import generate_fliparood_formulas
from pyecsca.ec.formula.graph import rename_ivs
+from pyecsca.ec.formula.metrics import formula_similarity
from pyecsca.ec.formula.partitions import (
reduce_all_adds,
expand_all_muls,
@@ -19,15 +21,13 @@ from pyecsca.ec.error import UnsatisfiedAssumptionError
from pyecsca.ec.params import get_params, DomainParameters
from pyecsca.ec.point import Point
from pyecsca.ec.model import ShortWeierstrassModel, MontgomeryModel, TwistedEdwardsModel
-from pyecsca.ec.formula import (
+from pyecsca.ec.formula.efd import (
AdditionEFDFormula,
DoublingEFDFormula,
LadderEFDFormula,
- Formula,
- AdditionFormula,
- DoublingFormula,
- LadderFormula,
+ EFDFormula,
)
+from pyecsca.ec.formula import AdditionFormula, DoublingFormula, LadderFormula
@pytest.fixture()
@@ -134,6 +134,17 @@ def test_pickle(add, dbl):
assert add == pickle.loads(pickle.dumps(add))
+def test_formula_similarity(secp128r1):
+ add_bl = secp128r1.curve.coordinate_model.formulas["add-2007-bl"]
+ add_rcb = secp128r1.curve.coordinate_model.formulas["add-2015-rcb"]
+ out = formula_similarity(add_bl, add_rcb)
+ assert out["output"] == 0
+ assert out["ivs"] < 0.5
+ out_same = formula_similarity(add_bl, add_bl)
+ assert out_same["output"] == 1
+ assert out_same["ivs"] == 1
+
+
LIBRARY_FORMULAS = [
[
"add-bc-r1rv76-jac",
@@ -282,11 +293,81 @@ LIBRARY_FORMULAS = [
("other", "Curve25519"),
LadderEFDFormula,
],
+ [
+ "add-bearssl-v06",
+ ShortWeierstrassModel,
+ "jacobian",
+ ("secg", "secp256r1"),
+ AdditionEFDFormula,
+ ],
+ [
+ "dbl-bearssl-v06",
+ ShortWeierstrassModel,
+ "jacobian",
+ ("secg", "secp256r1"),
+ DoublingEFDFormula,
+ ],
+ [
+ "add-libgcrypt-v1102",
+ ShortWeierstrassModel,
+ "jacobian",
+ ("secg", "secp256r1"),
+ AdditionEFDFormula,
+ ],
+ [
+ "dbl-libgcrypt-v1102",
+ ShortWeierstrassModel,
+ "jacobian",
+ ("secg", "secp256r1"),
+ DoublingEFDFormula,
+ ],
+ [
+ "ladd-go-1214",
+ MontgomeryModel,
+ "xz",
+ ("other", "Curve25519"),
+ LadderEFDFormula,
+ ],
+ [
+ "add-gecc-322",
+ ShortWeierstrassModel,
+ "jacobian-3",
+ ("secg", "secp256r1"),
+ AdditionEFDFormula,
+ ],
+ [
+ "dbl-gecc-321",
+ ShortWeierstrassModel,
+ "jacobian-3",
+ ("secg", "secp256r1"),
+ DoublingEFDFormula,
+ ],
+ [
+ "ladd-boringssl-x25519",
+ MontgomeryModel,
+ "xz",
+ ("other", "Curve25519"),
+ LadderEFDFormula,
+ ],
+ [
+ "dbl-ipp-x25519",
+ MontgomeryModel,
+ "xz",
+ ("other", "Curve25519"),
+ DoublingEFDFormula,
+ ],
+ [
+ "ladd-botan-x25519",
+ MontgomeryModel,
+ "xz",
+ ("other", "Curve25519"),
+ LadderEFDFormula,
+ ],
]
-@pytest.fixture(params=LIBRARY_FORMULAS)
-def library_formula_params(request) -> Tuple[Formula, DomainParameters]:
+@pytest.fixture(params=LIBRARY_FORMULAS, ids=list(map(itemgetter(0), LIBRARY_FORMULAS)))
+def library_formula_params(request) -> Tuple[EFDFormula, DomainParameters]:
name, model, coords_name, param_spec, formula_type = request.param
model = model()
coordinate_model = model.coordinates[coords_name]
@@ -340,10 +421,7 @@ def test_expansions(library_formula_params):
def do_test_formula(formula, params):
- try:
- do_test_formula0(formula, params)
- except AssertionError:
- print(formula.name)
+ do_test_formula0(formula, params)
def do_test_formula0(formula, params):
@@ -408,3 +486,8 @@ def do_test_formula0(formula, params):
scale(params.curve.prime, res[0], **params.curve.parameters)[0]
== Q2
)
+
+
+def test_formula_correctness(library_formula_params):
+ formula, params = library_formula_params
+ do_test_formula0(formula, params)
diff --git a/test/sca/test_structural.py b/test/sca/test_structural.py
deleted file mode 100644
index 2a883cc..0000000
--- a/test/sca/test_structural.py
+++ /dev/null
@@ -1,315 +0,0 @@
-import pytest
-from importlib_resources import files, as_file
-import test.data.formulas
-from pyecsca.ec.formula import (
- AdditionEFDFormula,
- LadderEFDFormula,
- DoublingEFDFormula,
- AdditionFormula,
- DoublingFormula,
- LadderFormula,
-)
-from pyecsca.ec.model import ShortWeierstrassModel, MontgomeryModel, TwistedEdwardsModel
-from pyecsca.ec.params import get_params
-from pyecsca.ec.formula.metrics import formula_similarity
-
-
-def test_formula_similarity(secp128r1):
- add_bl = secp128r1.curve.coordinate_model.formulas["add-2007-bl"]
- add_rcb = secp128r1.curve.coordinate_model.formulas["add-2015-rcb"]
- out = formula_similarity(add_bl, add_rcb)
- assert out["output"] == 0
- assert out["ivs"] < 0.5
- out_same = formula_similarity(add_bl, add_bl)
- assert out_same["output"] == 1
- assert out_same["ivs"] == 1
-
-
-@pytest.mark.parametrize(
- "name,model,coords,param_spec,formula_type",
- [
- [
- "add-bc-r1rv76-jac",
- ShortWeierstrassModel,
- "jacobian",
- ("secg", "secp128r1"),
- AdditionEFDFormula,
- ],
- [
- "add-bc-r1rv76-mod",
- ShortWeierstrassModel,
- "modified",
- ("secg", "secp128r1"),
- AdditionEFDFormula,
- ],
- [
- "dbl-bc-r1rv76-jac",
- ShortWeierstrassModel,
- "jacobian",
- ("secg", "secp128r1"),
- DoublingEFDFormula,
- ],
- [
- "dbl-bc-r1rv76-mod",
- ShortWeierstrassModel,
- "modified",
- ("secg", "secp128r1"),
- DoublingEFDFormula,
- ],
- [
- "dbl-bc-r1rv76-x25519",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- DoublingEFDFormula,
- ],
- [
- "ladd-bc-r1rv76-x25519",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- LadderEFDFormula,
- ],
- [
- "dbl-boringssl-p224",
- ShortWeierstrassModel,
- "jacobian-3",
- ("secg", "secp224r1"),
- DoublingEFDFormula,
- ],
- [
- "add-boringssl-p224",
- ShortWeierstrassModel,
- "jacobian-3",
- ("secg", "secp224r1"),
- AdditionEFDFormula,
- ],
- [
- "add-libressl-v382",
- ShortWeierstrassModel,
- "jacobian",
- ("secg", "secp128r1"),
- AdditionEFDFormula,
- ],
- [
- "dbl-libressl-v382",
- ShortWeierstrassModel,
- "jacobian",
- ("secg", "secp128r1"),
- DoublingEFDFormula,
- ],
- [
- "dbl-secp256k1-v040",
- ShortWeierstrassModel,
- "jacobian",
- ("secg", "secp256k1"),
- DoublingEFDFormula,
- ],
- [
- "add-openssl-z256",
- ShortWeierstrassModel,
- "jacobian-3",
- ("secg", "secp256r1"),
- AdditionEFDFormula,
- ],
- [
- "add-openssl-z256a",
- ShortWeierstrassModel,
- "jacobian-3",
- ("secg", "secp256r1"),
- AdditionEFDFormula,
- ],
- [
- "ladd-openssl-x25519",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- LadderEFDFormula,
- ],
- [
- "ladd-hacl-x25519",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- LadderEFDFormula,
- ],
- [
- "dbl-hacl-x25519",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- DoublingEFDFormula,
- ],
- [
- "dbl-sunec-v21",
- ShortWeierstrassModel,
- "projective-3",
- ("secg", "secp256r1"),
- DoublingEFDFormula,
- ],
- [
- "add-sunec-v21",
- ShortWeierstrassModel,
- "projective-3",
- ("secg", "secp256r1"),
- AdditionEFDFormula,
- ],
- [
- "add-sunec-v21-ed25519",
- TwistedEdwardsModel,
- "extended",
- ("other", "Ed25519"),
- AdditionEFDFormula,
- ],
- [
- "dbl-sunec-v21-ed25519",
- TwistedEdwardsModel,
- "extended",
- ("other", "Ed25519"),
- DoublingEFDFormula,
- ],
- [
- "ladd-rfc7748",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- LadderEFDFormula,
- ],
- [
- "add-bearssl-v06",
- ShortWeierstrassModel,
- "jacobian",
- ("secg", "secp256r1"),
- AdditionEFDFormula,
- ],
- [
- "dbl-bearssl-v06",
- ShortWeierstrassModel,
- "jacobian",
- ("secg", "secp256r1"),
- DoublingEFDFormula,
- ],
- [
- "add-libgcrypt-v1102",
- ShortWeierstrassModel,
- "jacobian",
- ("secg", "secp256r1"),
- AdditionEFDFormula,
- ],
- [
- "dbl-libgcrypt-v1102",
- ShortWeierstrassModel,
- "jacobian",
- ("secg", "secp256r1"),
- DoublingEFDFormula,
- ],
- [
- "ladd-go-1214",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- LadderEFDFormula,
- ],
- [
- "add-gecc-322",
- ShortWeierstrassModel,
- "jacobian-3",
- ("secg", "secp256r1"),
- AdditionEFDFormula,
- ],
- [
- "dbl-gecc-321",
- ShortWeierstrassModel,
- "jacobian-3",
- ("secg", "secp256r1"),
- DoublingEFDFormula,
- ],
- [
- "ladd-boringssl-x25519",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- LadderEFDFormula,
- ],
- [
- "dbl-ipp-x25519",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- DoublingEFDFormula,
- ],
- [
- "ladd-botan-x25519",
- MontgomeryModel,
- "xz",
- ("other", "Curve25519"),
- LadderEFDFormula,
- ],
- ],
-)
-def test_formula_correctness(name, model, coords, param_spec, formula_type):
- model = model()
- coordinate_model = model.coordinates[coords]
- with as_file(files(test.data.formulas).joinpath(name)) as meta_path, as_file(
- files(test.data.formulas).joinpath(name + ".op3")
- ) as op3_path:
- formula = formula_type(meta_path, op3_path, name, coordinate_model)
- params = get_params(*param_spec, coords)
- scale = coordinate_model.formulas.get("z", None)
- if scale is None:
- scale = coordinate_model.formulas.get("scale", None)
- for _ in range(10):
- Paff = params.curve.affine_random()
- P2aff = params.curve.affine_double(Paff)
- Qaff = params.curve.affine_random()
- Q2aff = params.curve.affine_double(Qaff)
- Raff = params.curve.affine_add(Paff, Qaff)
- R2aff = params.curve.affine_double(Raff)
- QRaff = params.curve.affine_add(Qaff, Raff)
- P = Paff.to_model(coordinate_model, params.curve)
- P2 = P2aff.to_model(coordinate_model, params.curve)
- Q = Qaff.to_model(coordinate_model, params.curve)
- Q2 = Q2aff.to_model(coordinate_model, params.curve)
- R = Raff.to_model(coordinate_model, params.curve)
- R2 = R2aff.to_model(coordinate_model, params.curve) # noqa
- QR = QRaff.to_model(coordinate_model, params.curve)
- inputs = (P, Q, R)[: formula.num_inputs]
- res = formula(params.curve.prime, *inputs, **params.curve.parameters)
- if issubclass(formula_type, AdditionFormula):
- try:
- assert res[0].to_affine() == Raff
- except NotImplementedError:
- assert (
- scale(params.curve.prime, res[0], **params.curve.parameters)[0] == R
- )
- elif issubclass(formula_type, DoublingFormula):
- try:
- assert res[0].to_affine() == P2aff
- except NotImplementedError:
- assert (
- scale(params.curve.prime, res[0], **params.curve.parameters)[0]
- == P2
- )
- elif issubclass(formula_type, LadderFormula):
- try:
- assert res[0].to_affine() == Q2aff
- assert res[1].to_affine() == QRaff
- except NotImplementedError:
- # print(scale(params.curve.prime, res[0], **params.curve.parameters)[0])
- # print(scale(params.curve.prime, res[1], **params.curve.parameters)[0])
- # print(P)
- # print(Q)
- # print(R)
- # print(P2)
- # print(Q2)
- # print(R2)
- # print(QR)
- # print("------------------------------------")
- assert (
- scale(params.curve.prime, res[1], **params.curve.parameters)[0]
- == QR
- )
- assert (
- scale(params.curve.prime, res[0], **params.curve.parameters)[0]
- == Q2
- )