1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
import multiprocessing
import inspect
import tempfile
import sys
import os
from datetime import timedelta
from contextlib import contextmanager
from dataclasses import dataclass
from functools import partial, cached_property
from importlib import import_module, invalidate_caches
from pathlib import Path
from typing import Type, Any, Optional
from enum import Enum
from pyecsca.ec.params import DomainParameters, get_params
from pyecsca.ec.mult import *
from pyecsca.ec.countermeasures import GroupScalarRandomization, AdditiveSplitting, MultiplicativeSplitting, EuclideanSplitting
spawn_context = multiprocessing.get_context("spawn")
# Allow to use "spawn" multiprocessing method for function defined in a Jupyter notebook.
# https://neuromancer.sk/article/35
@contextmanager
def enable_spawn(func):
invalidate_caches()
source = inspect.getsource(func)
current_file_path = os.path.abspath(__file__)
with open(current_file_path, 'r') as self, tempfile.NamedTemporaryFile(suffix=".py", mode="w") as f:
f.write(self.read())
f.write(source)
f.flush()
path = Path(f.name)
directory = str(path.parent)
sys.path.append(directory)
module = import_module(str(path.stem))
yield getattr(module, func.__name__)
sys.path.remove(directory)
@dataclass(frozen=True)
class MultIdent:
klass: Type[ScalarMultiplier]
args: list[Any]
kwargs: dict[str, Any]
countermeasure: Optional[str] = None
def __init__(self, klass: Type[ScalarMultiplier], *args, **kwargs):
object.__setattr__(self, "klass", klass)
object.__setattr__(self, "args", args if args is not None else [])
if kwargs is not None and "countermeasure" in kwargs:
object.__setattr__(self, "countermeasure", kwargs["countermeasure"])
del kwargs["countermeasure"]
object.__setattr__(self, "kwargs", kwargs if kwargs is not None else {})
@cached_property
def partial(self):
func = partial(self.klass, *self.args, **self.kwargs)
if self.countermeasure is None:
return func
if self.countermeasure == "gsr":
return lambda *args, **kwargs: GroupScalarRandomization(func(*args, **kwargs))
elif self.countermeasure == "additive":
return lambda *args, **kwargs: AdditiveSplitting(func(*args, **kwargs))
elif self.countermeasure == "multiplicative":
return lambda *args, **kwargs: MultiplicativeSplitting(func(*args, **kwargs))
elif self.countermeasure == "euclidean":
return lambda *args, **kwargs: EuclideanSplitting(func(*args, **kwargs))
def with_countermeasure(self, countermeasure: str | None):
if countermeasure not in (None, "gsr", "additive", "multiplicative", "euclidean"):
raise ValueError(f"Unknown countermeasure: {countermeasure}")
return MultIdent(self.klass, *self.args, **self.kwargs, countermeasure=countermeasure)
def __str__(self):
args = ("_" + ",".join(list(map(str, self.args)))) if self.args else ""
kwargs = ("_" + ",".join(f"{str(k)}:{v.name if isinstance(v, Enum) else str(v)}" for k,v in self.kwargs.items())) if self.kwargs else ""
countermeasure = f"+{self.countermeasure}" if self.countermeasure is not None else ""
return f"{self.klass.__name__}{args}{kwargs}{countermeasure}"
def __repr__(self):
return str(self)
def __hash__(self):
return hash((self.klass, self.countermeasure, tuple(self.args), tuple(self.kwargs.keys()), tuple(self.kwargs.values())))
@dataclass
class MultResults:
multiplications: list[set[int]]
samples: int
duration: Optional[float] = None
kind: Optional[str] = None
def merge(self, other: "MultResults"):
self.multiplications.extend(other.multiplications)
self.samples += other.samples
def __len__(self):
return self.samples
def __iter__(self):
yield from self.multiplications
def __getitem__(self, i):
return self.multiplications[i]
def __str__(self):
duration = timedelta(seconds=int(self.duration)) if self.duration is not None else ""
kind = self.kind if self.kind is not None else ""
return f"MultResults({self.samples},{duration},{kind})"
def __repr__(self):
return str(self)
@dataclass
class ProbMap:
probs: dict[int, float]
samples: int
kind: Optional[str] = None
def __len__(self):
return len(self.probs)
def __iter__(self):
yield from self.probs
def __getitem__(self, i):
return self.probs[i]
def keys(self):
return self.probs.keys()
def values(self):
return self.probs.values()
def items(self):
return self.probs.items()
def narrow(self, divisors: set[int]):
self.probs = {k:v for k, v in self.probs.items() if k in divisors}
def merge(self, other: "ProbMap") -> None:
if self.kind != other.kind:
raise ValueError("Merging ProbMaps of different kinds leads to unexpected results.")
new_keys = set(self.keys()).union(other.keys())
result = {}
for key in new_keys:
if key in self and key in other:
result[key] = (self[key] * self.samples + other[key] * other.samples) / (self.samples + other.samples)
elif key in self:
result[key] = self[key]
elif key in other:
result[key] = other[key]
self.probs = result
self.samples += other.samples
def enrich(self, other: "ProbMap") -> None:
if self.samples != other.samples:
raise ValueError("Enriching can only work on equal amount of samples (same run, different divisors)")
if self.kind != other.kind:
raise ValueError("Enriching ProbMaps of different kinds leads to unexpected results.")
self.probs.update(other.probs)
# All dbl-and-add multipliers from https://github.com/J08nY/pyecsca/blob/master/pyecsca/ec/mult
window_mults = [
MultIdent(SlidingWindowMultiplier, width=3),
MultIdent(SlidingWindowMultiplier, width=4),
MultIdent(SlidingWindowMultiplier, width=5),
MultIdent(SlidingWindowMultiplier, width=6),
MultIdent(FixedWindowLTRMultiplier, m=2**4),
MultIdent(FixedWindowLTRMultiplier, m=2**5),
MultIdent(FixedWindowLTRMultiplier, m=2**6),
MultIdent(WindowBoothMultiplier, width=3),
MultIdent(WindowBoothMultiplier, width=4),
MultIdent(WindowBoothMultiplier, width=5),
MultIdent(WindowBoothMultiplier, width=6)
]
naf_mults = [
MultIdent(WindowNAFMultiplier, width=3),
MultIdent(WindowNAFMultiplier, width=4),
MultIdent(WindowNAFMultiplier, width=5),
MultIdent(WindowNAFMultiplier, width=6),
MultIdent(BinaryNAFMultiplier, direction=ProcessingDirection.LTR),
MultIdent(BinaryNAFMultiplier, direction=ProcessingDirection.RTL)
]
comb_mults = [
MultIdent(CombMultiplier, width=2),
MultIdent(CombMultiplier, width=3),
MultIdent(CombMultiplier, width=4),
MultIdent(CombMultiplier, width=5),
MultIdent(CombMultiplier, width=6),
MultIdent(BGMWMultiplier, width=2, direction=ProcessingDirection.LTR),
MultIdent(BGMWMultiplier, width=3, direction=ProcessingDirection.LTR),
MultIdent(BGMWMultiplier, width=4, direction=ProcessingDirection.LTR),
MultIdent(BGMWMultiplier, width=5, direction=ProcessingDirection.LTR),
MultIdent(BGMWMultiplier, width=6, direction=ProcessingDirection.LTR),
MultIdent(BGMWMultiplier, width=2, direction=ProcessingDirection.RTL),
MultIdent(BGMWMultiplier, width=3, direction=ProcessingDirection.RTL),
MultIdent(BGMWMultiplier, width=4, direction=ProcessingDirection.RTL),
MultIdent(BGMWMultiplier, width=5, direction=ProcessingDirection.RTL),
MultIdent(BGMWMultiplier, width=6, direction=ProcessingDirection.RTL)
]
binary_mults = [
MultIdent(LTRMultiplier, always=False),
MultIdent(LTRMultiplier, always=True),
MultIdent(RTLMultiplier, always=False),
MultIdent(RTLMultiplier, always=True),
MultIdent(CoronMultiplier)
]
other_mults = [
MultIdent(FullPrecompMultiplier, always=False),
MultIdent(FullPrecompMultiplier, always=True),
MultIdent(SimpleLadderMultiplier, complete=True),
MultIdent(SimpleLadderMultiplier, complete=False)
]
all_mults = window_mults + naf_mults + binary_mults + other_mults + comb_mults
|