aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorJ08nY2023-11-07 17:16:03 +0100
committerJ08nY2023-11-10 12:21:27 +0100
commita523a8874c8d13c0e6f64dbe4b3cea1bf0771688 (patch)
tree2c10515c97b714a55b86a1e2da7ca6e8018f2b2b /test
parent2e64f22c3c388b4765893c729713fcaf0937f14a (diff)
downloadpyecsca-a523a8874c8d13c0e6f64dbe4b3cea1bf0771688.tar.gz
pyecsca-a523a8874c8d13c0e6f64dbe4b3cea1bf0771688.tar.zst
pyecsca-a523a8874c8d13c0e6f64dbe4b3cea1bf0771688.zip
Add formula similarity metric.
Diffstat (limited to 'test')
-rw-r--r--test/data/formulas/__init__.py0
-rw-r--r--test/data/formulas/add-bc-r1rv761
-rw-r--r--test/data/formulas/add-bc-r1rv76.op323
-rw-r--r--test/sca/test_structural.py36
4 files changed, 60 insertions, 0 deletions
diff --git a/test/data/formulas/__init__.py b/test/data/formulas/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/data/formulas/__init__.py
diff --git a/test/data/formulas/add-bc-r1rv76 b/test/data/formulas/add-bc-r1rv76
new file mode 100644
index 0000000..c33b915
--- /dev/null
+++ b/test/data/formulas/add-bc-r1rv76
@@ -0,0 +1 @@
+source BouncyCastle r1rv76 https://github.com/bcgit/bc-java/blob/r1rv76/core/src/main/java/org/bouncycastle/math/ec/ECPoint.java#L749
diff --git a/test/data/formulas/add-bc-r1rv76.op3 b/test/data/formulas/add-bc-r1rv76.op3
new file mode 100644
index 0000000..5e7f521
--- /dev/null
+++ b/test/data/formulas/add-bc-r1rv76.op3
@@ -0,0 +1,23 @@
+Z1Squared = Z1^2
+U2 = Z1Squared * X2
+Z1Cubed = Z1Squared * Z1
+S2 = Z1Cubed * Y2
+Z2Squared = Z2^2
+U1 = Z2Squared * X1
+Z2Cubed = Z2Squared * Z2
+S1 = Z2Cubed * Y1
+H = U1 - U2
+R = S1 - S2
+HSquared = H^2
+G = HSquared * H
+V = HSquared * U1
+t0 = 2 * V
+t1 = R^2
+t2 = t1 + G
+X3 = t2 - t0
+t3 = V - X3
+t4 = G * S1
+t5 = t3 * R
+Y3 = t5 - t4
+Z3 = H * Z1
+Z3 = Z3 * Z2
diff --git a/test/sca/test_structural.py b/test/sca/test_structural.py
new file mode 100644
index 0000000..db75b12
--- /dev/null
+++ b/test/sca/test_structural.py
@@ -0,0 +1,36 @@
+from importlib_resources import files, as_file
+import test.data.formulas
+from pyecsca.ec.formula import AdditionEFDFormula
+from pyecsca.ec.model import ShortWeierstrassModel
+from pyecsca.ec.params import get_params
+from pyecsca.sca.re.structural import formula_similarity, formula_similarity_fuzz
+
+
+def test_formula_match():
+ model = ShortWeierstrassModel()
+ coords = model.coordinates["jacobian"]
+ secp128r1 = get_params("secg", "secp128r1", "jacobian")
+ with as_file(
+ files(test.data.formulas).joinpath("add-bc-r1rv76")
+ ) as meta_path, as_file(
+ files(test.data.formulas).joinpath("add-bc-r1rv76.op3")
+ ) as op3_path:
+ bc_formula = AdditionEFDFormula(meta_path, op3_path, "add-bc-r1rv76", coords)
+ print()
+ for other_name, other_formula in coords.formulas.items():
+ if not other_name.startswith("add"):
+ continue
+ print(other_name, "fuzz", formula_similarity_fuzz(other_formula, bc_formula, secp128r1.curve, 1000), "symbolic", formula_similarity(other_formula, bc_formula))
+
+
+def test_efd_formula_match():
+ model = ShortWeierstrassModel()
+ coords = model.coordinates["modified"]
+ print()
+ for other_name, other_formula in coords.formulas.items():
+ if not other_name.startswith("add"):
+ continue
+ for one_name, one_formula in coords.formulas.items():
+ if not one_name.startswith("add"):
+ continue
+ print(one_name, other_name, formula_similarity(one_formula, other_formula))