diff options
| author | J08nY | 2025-03-08 16:31:16 +0100 |
|---|---|---|
| committer | J08nY | 2025-03-08 16:31:16 +0100 |
| commit | 86768f810937382983c426a8151a26698afda3e8 (patch) | |
| tree | 0f91a119fc9c2bc128c1e86353f2219bf9aca5b0 /pyecsca/sca | |
| parent | 81f4d8ff8dab2a7884f0d74f23b3d78142eb176a (diff) | |
| download | pyecsca-86768f810937382983c426a8151a26698afda3e8.tar.gz pyecsca-86768f810937382983c426a8151a26698afda3e8.tar.zst pyecsca-86768f810937382983c426a8151a26698afda3e8.zip | |
Diffstat (limited to 'pyecsca/sca')
| -rw-r--r-- | pyecsca/sca/re/rpa.py | 47 | ||||
| -rw-r--r-- | pyecsca/sca/re/zvp.py | 27 |
2 files changed, 49 insertions, 25 deletions
diff --git a/pyecsca/sca/re/rpa.py b/pyecsca/sca/re/rpa.py index c65c3f6..395f698 100644 --- a/pyecsca/sca/re/rpa.py +++ b/pyecsca/sca/re/rpa.py @@ -4,11 +4,13 @@ Provides functionality inspired by the Refined-Power Analysis attack by Goubin [ from copy import copy, deepcopy from public import public -from typing import MutableMapping, Optional, Callable, List, Set, cast +from typing import MutableMapping, Optional, Callable, List, Set, cast, Type, Tuple from sympy import FF, sympify, Poly, symbols from pyecsca.ec.error import NonInvertibleError +from pyecsca.ec.formula.fake import FakePoint +from pyecsca.ec.mult.fake import fake_mult from pyecsca.sca.re.base import RE from pyecsca.sca.re.tree import Tree, Map from pyecsca.ec.coordinates import AffineCoordinateModel @@ -19,8 +21,7 @@ from pyecsca.ec.formula import ( TriplingFormula, NegationFormula, DifferentialAdditionFormula, - LadderFormula, -) + LadderFormula, ) from pyecsca.ec.mod import Mod, mod from pyecsca.ec.mult import ( ScalarMultiplicationAction, @@ -380,3 +381,43 @@ class RPA(RE): log([mult.__class__.__name__ for mult in mults]) log() return mults + + +@public +def multiples_computed( + scalar: int, + params: DomainParameters, + mult_class: Type[ScalarMultiplier], + mult_factory: Callable, + use_init: bool = False, + use_multiply: bool = True +) -> set[int]: + """ + Compute the multiples computed for a given scalar and multiplier (quickly). + + :param scalar: The scalar to compute for. + :param params: The domain parameters to use. + :param mult_class: The class of the scalar multiplier to use. + :param mult_factory: A callable that takes the formulas and instantiates the multiplier. + :param use_init: Whether to consider the point multiples that happen in scalarmult initialization. + :param use_multiply: Whether to consider the point multiples that happen in scalarmult multiply (after initialization). + :return: A list of tuples, where the first element is the formula shortname (e.g. "add") and the second is a tuple of the dlog + relationships to the input of the input points to the formula. + """ + mult = fake_mult(mult_class, mult_factory, params) + ctx = MultipleContext() + if use_init: + with local(ctx, copy=False): + mult.init(params, FakePoint(params.curve.coordinate_model)) + else: + mult.init(params, FakePoint(params.curve.coordinate_model)) + + if use_multiply: + with local(ctx, copy=False): + mult.multiply(scalar) + else: + mult.multiply(scalar) + + return set(ctx.points.values()) - {0} + + diff --git a/pyecsca/sca/re/zvp.py b/pyecsca/sca/re/zvp.py index 74c8902..b143260 100644 --- a/pyecsca/sca/re/zvp.py +++ b/pyecsca/sca/re/zvp.py @@ -3,7 +3,7 @@ Provides functionality inspired by the Zero-value point attack [ZVP]_. Implements ZVP point construction from [FFD]_. """ -from typing import List, Set, Tuple, Dict, Type +from typing import List, Set, Tuple, Dict, Type, Callable from public import public import warnings from astunparse import unparse @@ -25,7 +25,7 @@ from pyecsca.ec.formula import ( from pyecsca.ec.formula.fake import FakePoint, FakeFormula from pyecsca.ec.formula.unroll import unroll_formula from pyecsca.ec.mod import Mod, mod -from pyecsca.ec.mult import ScalarMultiplier +from pyecsca.ec.mult import ScalarMultiplier, fake_mult from pyecsca.ec.params import DomainParameters from pyecsca.ec.point import Point @@ -585,11 +585,12 @@ def solve_hard_dcp_cypari( return res +@public def addition_chain( scalar: int, params: DomainParameters, mult_class: Type[ScalarMultiplier], - mult_factory, + mult_factory: Callable, use_init: bool = False, use_multiply: bool = True ) -> List[Tuple[str, Tuple[int, ...]]]: @@ -605,26 +606,8 @@ def addition_chain( :return: A list of tuples, where the first element is the formula shortname (e.g. "add") and the second is a tuple of the dlog relationships to the input of the input points to the formula. """ - formula_classes: List[Type[Formula]] = list( - filter( - lambda klass: klass in mult_class.requires, - [ - AdditionFormula, - DifferentialAdditionFormula, - DoublingFormula, - LadderFormula, - NegationFormula, - ], - ) - ) - formulas = [] - for formula in formula_classes: - for subclass in formula.__subclasses__(): - if issubclass(subclass, FakeFormula): - formulas.append(subclass(params.curve.coordinate_model)) - + mult = fake_mult(mult_class, mult_factory, params) ctx = MultipleContext() - mult = mult_factory(*formulas) if use_init: with local(ctx, copy=False): mult.init(params, FakePoint(params.curve.coordinate_model)) |
