aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/sca
diff options
context:
space:
mode:
authorAndrej Bátora2023-12-14 22:40:05 +0100
committerGitHub2023-12-14 22:40:05 +0100
commit2b7bda7b57f0f66102cf92526ceee78f11ea29d4 (patch)
tree92df965a2bc4afb5ea3d38ce69703cc4c39ae7d4 /pyecsca/sca
parentc69aebde34d3bdf8a1ff789b3aa172040b59f1d7 (diff)
parentcee7e6b3196739b9ceaf12da3a11d5f5486b16bb (diff)
downloadpyecsca-2b7bda7b57f0f66102cf92526ceee78f11ea29d4.tar.gz
pyecsca-2b7bda7b57f0f66102cf92526ceee78f11ea29d4.tar.zst
pyecsca-2b7bda7b57f0f66102cf92526ceee78f11ea29d4.zip
Merge branch 'J08nY:master' into CPA_correlations_tracking
Diffstat (limited to 'pyecsca/sca')
-rw-r--r--pyecsca/sca/re/rpa.py3
-rw-r--r--pyecsca/sca/re/structural.py76
-rw-r--r--pyecsca/sca/target/leakage.py (renamed from pyecsca/sca/target/emulator.py)6
3 files changed, 6 insertions, 79 deletions
diff --git a/pyecsca/sca/re/rpa.py b/pyecsca/sca/re/rpa.py
index db88dae..b1661e6 100644
--- a/pyecsca/sca/re/rpa.py
+++ b/pyecsca/sca/re/rpa.py
@@ -34,8 +34,11 @@ class MultipleContext(Context):
"""Context that traces the multiples of points computed."""
base: Optional[Point]
+ """The base point that all the multiples are counted from."""
points: MutableMapping[Point, int]
+ """The mapping of points to the multiples they represent (e.g., base -> 1)."""
parents: MutableMapping[Point, List[Point]]
+ """The mapping of points to their parent they were computed from."""
inside: bool
def __init__(self):
diff --git a/pyecsca/sca/re/structural.py b/pyecsca/sca/re/structural.py
index c79e604..f3b0d32 100644
--- a/pyecsca/sca/re/structural.py
+++ b/pyecsca/sca/re/structural.py
@@ -1,77 +1 @@
""""""
-import warnings
-from typing import Dict
-from public import public
-
-from ...ec.curve import EllipticCurve
-from ...ec.formula import Formula
-from ...ec.context import DefaultContext, local
-from .zvp import unroll_formula
-from operator import itemgetter, attrgetter
-
-
-@public
-def formula_similarity(one: Formula, other: Formula) -> Dict[str, float]:
- if one.coordinate_model != other.coordinate_model:
- warnings.warn("Mismatched coordinate model.")
-
- one_unroll = unroll_formula(one)
- other_unroll = unroll_formula(other)
- one_results = {}
- for name, value in one_unroll:
- if name in one.outputs:
- one_results[name] = value
- other_results = {}
- for name, value in other_unroll:
- if name in other.outputs:
- other_results[name] = value
- one_result_polys = set(one_results.values())
- other_result_polys = set(other_results.values())
- one_polys = set(map(itemgetter(1), one_unroll))
- other_polys = set(map(itemgetter(1), other_unroll))
- return {
- "output": len(one_result_polys.intersection(other_result_polys))
- / max(len(one_result_polys), len(other_result_polys)),
- "ivs": len(one_polys.intersection(other_polys))
- / max(len(one_polys), len(other_polys)),
- }
-
-
-@public
-def formula_similarity_fuzz(
- one: Formula, other: Formula, curve: EllipticCurve, samples: int = 1000
-) -> Dict[str, float]:
- if one.coordinate_model != other.coordinate_model:
- warnings.warn("Mismatched coordinate model.")
-
- output_matches = 0.0
- iv_matches = 0.0
- for _ in range(samples):
- Paff = curve.affine_random()
- Qaff = curve.affine_random()
- Raff = curve.affine_add(Paff, Qaff)
- P = Paff.to_model(one.coordinate_model, curve)
- Q = Qaff.to_model(one.coordinate_model, curve)
- R = Raff.to_model(one.coordinate_model, curve)
- inputs = (P, Q, R)[: one.num_inputs]
- with local(DefaultContext()) as ctx:
- res_one = one(curve.prime, *inputs, **curve.parameters)
- action_one = ctx.actions.get_by_index([0])
- ivs_one = set(
- map(attrgetter("value"), sum(action_one[0].intermediates.values(), []))
- )
- with local(DefaultContext()) as ctx:
- res_other = other(curve.prime, *inputs, **curve.parameters)
- action_other = ctx.actions.get_by_index([0])
- ivs_other = set(
- map(attrgetter("value"), sum(action_other[0].intermediates.values(), []))
- )
- iv_matches += len(ivs_one.intersection(ivs_other)) / max(
- len(ivs_one), len(ivs_other)
- )
- one_coords = set(res_one)
- other_coords = set(res_other)
- output_matches += len(one_coords.intersection(other_coords)) / max(
- len(one_coords), len(other_coords)
- )
- return {"output": output_matches / samples, "ivs": iv_matches / samples}
diff --git a/pyecsca/sca/target/emulator.py b/pyecsca/sca/target/leakage.py
index bca27cf..8a8acd6 100644
--- a/pyecsca/sca/target/emulator.py
+++ b/pyecsca/sca/target/leakage.py
@@ -18,7 +18,7 @@ import numpy as np
@public
-class EmulatorTarget(Target):
+class LeakageTarget(Target):
model: CurveModel
coords: CoordinateModel
@@ -48,7 +48,7 @@ class EmulatorTarget(Target):
context.actions.walk(callback)
return Trace(np.array(temp_trace))
- def emulate_scalar_mult_traces(self, num_of_traces: int, scalar: int) -> Tuple[list[Point], list[Trace]]:
+ def simulate_scalar_mult_traces(self, num_of_traces: int, scalar: int) -> Tuple[list[Point], list[Trace]]:
if self.params is None:
raise ValueError("Missing DomainParameters")
points = [self.params.curve.affine_random().to_model(self.coords, self.params.curve) for _ in range(num_of_traces)]
@@ -58,7 +58,7 @@ class EmulatorTarget(Target):
traces.append(trace)
return points, traces
- def emulate_ecdh_traces(self, num_of_traces: int) -> Tuple[list[Point], list[Trace]]:
+ def simulate_ecdh_traces(self, num_of_traces: int) -> Tuple[list[Point], list[Trace]]:
if self.params is None:
raise ValueError("Missing DomainParameters")
other_pubs = [self.params.curve.affine_random().to_model(self.coords, self.params.curve) for _ in range(num_of_traces)]