From 7976afc6c0b765fe78e032c19c7c40c7cee11f51 Mon Sep 17 00:00:00 2001 From: J08nY Date: Fri, 24 Oct 2025 22:56:08 +0200 Subject: Add test for multiple_graph with countermeasures. --- test/ec/test_countermeasures.py | 62 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/test/ec/test_countermeasures.py b/test/ec/test_countermeasures.py index c3a6ee3..b802525 100644 --- a/test/ec/test_countermeasures.py +++ b/test/ec/test_countermeasures.py @@ -1,5 +1,4 @@ from itertools import product -from copy import copy import pytest @@ -13,6 +12,7 @@ from pyecsca.ec.countermeasures import ( ) from pyecsca.ec.mod import mod from pyecsca.ec.mult import * +from pyecsca.sca.re.rpa import multiple_graph @pytest.fixture(params=["add-1998-cmo-2", "add-2015-rcb"]) @@ -328,6 +328,66 @@ def test_combination(scalar, one, two, secp128r1): assert raw.equals(masked) +@pytest.mark.parametrize( + "scalar", + [ + 3253857902090173296443513219124437746, + 1234567893141592653589793238464338327, + 86728612699079982903603364383639280149, + 60032993417060801067503559426926851620, + ], +) +@pytest.mark.parametrize( + "one,two", + product( + ( + GroupScalarRandomization, + AdditiveSplitting, + MultiplicativeSplitting, + EuclideanSplitting, + BrumleyTuveri, + PointBlinding, + ), + repeat=2, + ), +) +def test_combination_multiples(scalar, one, two, secp128r1): + if one == two: + pytest.skip("Skip identical combinations.") + if one == PointBlinding or two == PointBlinding: + pytest.xfail("PointBlinding will never work with multiple graphs.") + + for i in range(2**two.nmults): + bits = format(i, f"0{two.nmults}b") + + def partial(*args, **kwargs): + mult = LTRMultiplier(*args, **kwargs) + + add = FakeAdditionFormula(secp128r1.curve.coordinate_model) + neg = FakeNegationFormula(secp128r1.curve.coordinate_model) + + if one in (AdditiveSplitting, EuclideanSplitting): + layer_one = one.from_single(mult, add=add) + elif one == PointBlinding: + layer_one = one.from_single(mult, neg=neg) + else: + layer_one = one.from_single(mult) + + if two in (AdditiveSplitting, EuclideanSplitting): + kws = {"add": add} + elif two == PointBlinding: + kws = {"neg": neg} + else: + kws = {} + + args = [layer_one if bit == "1" else mult for bit in bits] + combo = two(*args, **kws) + return combo + + res = multiple_graph(scalar, secp128r1, LTRMultiplier, partial) + assert res is not None + + @pytest.mark.parametrize( "scalar", [ -- cgit v1.2.3-70-g09d2