Skip to content

Commit 57895bb

Browse files
authored
API cleanup (#172)
* Remove deprecated SASCA * Cleanup function arguments (names, unnecessary arguments): SNR, Ttest, LDA, RLDA * Allow non-contiguous trace arrays * Fix the execution of the `examples/` in pytest (was not running).
1 parent 1b043f1 commit 57895bb

20 files changed

+280
-1356
lines changed

CHANGELOG.rst

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Changelog
55
Not released
66
------------
77

8+
* **Breaking** change parameter names ``l`` to ``traces`` for many functions.
9+
* **Breaking** remove unnecessary parameters from initializers (e.g. trace
10+
length where it can be inferred).
11+
812
v0.5.8 (2024/05/21)
913
-------------------
1014
* Add ``scalib.preprocessing.Quantizer`` for conversions of floating-point

examples/aes_attack.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from scalib.metrics import SNR, Ttest
1+
from scalib.metrics import SNR
22
from scalib.modeling import LDAClassifier
33
from scalib.attacks import FactorGraph, BPState
44
from scalib.postprocessing import rank_accuracy
@@ -39,7 +39,7 @@ def main():
3939
x[:, i] = labels_p[f"x{i}"]
4040

4141
# estimate SNR
42-
snr = SNR(nc=nc, ns=ns, np=16)
42+
snr = SNR(nc=nc)
4343
snr.fit_u(traces_p, x)
4444
snr_val = snr.get_snr()
4545

@@ -53,8 +53,11 @@ def main():
5353
print(" 3.1 Build LDAClassifier for each xi")
5454
models = []
5555
for i in range(16):
56-
lda = LDAClassifier(nc=nc, ns=npoi, p=1)
57-
lda.fit_u(l=traces_p[:, pois[i]], x=labels_p[f"x{i}"].astype(np.uint16))
56+
lda = LDAClassifier(nc=nc, p=1)
57+
lda.fit_u(
58+
traces=np.ascontiguousarray(traces_p[:, pois[i]]),
59+
x=labels_p[f"x{i}"].astype(np.uint16),
60+
)
5861
lda.solve()
5962
models.append(lda)
6063

@@ -113,7 +116,7 @@ def main():
113116
distribution = bp.get_distribution(f"k{i}")
114117

115118
guess_key.append(np.argmax(distribution))
116-
ranks.append(256 - np.where(np.argsort(distribution) == sk)[0])
119+
ranks.append(256 - (np.argsort(distribution) == sk).nonzero()[0][0])
117120

118121
secret_key.append(sk)
119122
key_distribution.append(distribution)
@@ -124,7 +127,7 @@ def main():
124127
print(" key byte ranks :", " ".join(["%3d" % (x) for x in ranks]))
125128
print("")
126129

127-
print(f" 5.2 Estimate full log2 key rank:")
130+
print(" 5.2 Estimate full log2 key rank:")
128131
key_distribution = np.array(key_distribution)
129132

130133
# Scores are negative log-likelihoods.
@@ -139,4 +142,5 @@ def main():
139142

140143

141144
if __name__ == "__main__":
145+
print("attack")
142146
main()

examples/aes_info.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
from scalib.metrics import SNR, Ttest
21
from scalib.modeling import LDAClassifier
3-
from scalib.attacks import FactorGraph, BPState
4-
from scalib.postprocessing import rank_accuracy
52

6-
from utils import sbox, gen_traces
3+
from utils import gen_traces
74
import numpy as np
85

96

107
def main():
118
nc = 256
12-
npoi = 2
139

1410
# Parameters
1511
std = 2
@@ -30,13 +26,11 @@ def main():
3026
ntraces_v, std, random_key=True, random_plaintext=True
3127
)
3228

33-
_, ns = traces_p.shape
34-
3529
print("2. Profiling the value of the first Sbox.")
3630
print("")
3731

38-
lda = LDAClassifier(nc=256, ns=ns, p=1)
39-
lda.fit_u(l=traces_p, x=labels_p["x0"].astype(np.uint16))
32+
lda = LDAClassifier(nc=256, p=1)
33+
lda.fit_u(traces=traces_p, x=labels_p["x0"].astype(np.uint16))
4034
lda.solve()
4135

4236
def eval_info(test_traces, test_labels):

examples/aes_tvla.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
from scalib.metrics import SNR, Ttest
2-
from scalib.modeling import LDAClassifier
3-
from scalib.attacks import FactorGraph, BPState
4-
from scalib.postprocessing import rank_accuracy
1+
from scalib.metrics import Ttest
52

6-
from utils import sbox, gen_traces
3+
from utils import gen_traces
74
import numpy as np
85

96

@@ -18,7 +15,7 @@ def main():
1815
traces_rk, _ = gen_traces(ntraces, std, random_key=True, random_plaintext=False)
1916
traces_fk, _ = gen_traces(ntraces, std, random_key=False, random_plaintext=False)
2017
# Ttest needs the lenght of the traces, and the test order (here, two)
21-
ttest = Ttest(traces_rk.shape[1], 2)
18+
ttest = Ttest(2)
2219
# Ttest can be updated multiple times, and the order of the updates does not matter.
2320
ttest.fit_u(traces_rk, np.zeros((traces_rk.shape[0],), dtype=np.uint16))
2421
ttest.fit_u(traces_fk, np.ones((traces_fk.shape[0],), dtype=np.uint16))

examples/test_examples.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ def example_path(example_file):
1212

1313
def test_examples():
1414
for p in ["aes_attack.py", "aes_tvla.py", "aes_info.py"]:
15-
runpy.run_path(example_path(p))
15+
print(f"####### {p} #######")
16+
runpy.run_path(example_path(p), run_name="__main__")

src/scalib/attacks/__init__.py

-13
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,8 @@
1212
FactorGraph
1313
GenFactor
1414
BPState
15-
16-
Deprecated
17-
~~~~~~~~~~
18-
19-
.. currentmodule:: scalib.attacks
20-
21-
.. autosummary::
22-
:toctree:
23-
:recursive:
24-
:nosignatures:
25-
26-
SASCAGraph
2715
"""
2816

2917
__all__ = ["FactorGraph", "BPState", "GenFactor"]
3018

31-
from .sascagraph import SASCAGraph
3219
from .factor_graph import FactorGraph, BPState, GenFactor

0 commit comments

Comments
 (0)