diff options
| author | J08nY | 2025-03-26 19:15:35 +0100 |
|---|---|---|
| committer | J08nY | 2025-04-16 12:25:07 +0200 |
| commit | d5a6848b75f2184ce3804f093e37125d82473948 (patch) | |
| tree | 3912a33fbddd33ef088004d9642df5160d6118c7 | |
| parent | 29c818c0c96b993f9677aebcb87d39eeca3e09ea (diff) | |
| download | ECTester-d5a6848b75f2184ce3804f093e37125d82473948.tar.gz ECTester-d5a6848b75f2184ce3804f093e37125d82473948.tar.zst ECTester-d5a6848b75f2184ce3804f093e37125d82473948.zip | |
Fixup some commons, add bnaf with always.
| -rw-r--r-- | epare/common.py | 53 | ||||
| -rw-r--r-- | epare/probmaps.py | 35 |
2 files changed, 51 insertions, 37 deletions
diff --git a/epare/common.py b/epare/common.py index 328d28a..59ac506 100644 --- a/epare/common.py +++ b/epare/common.py @@ -7,12 +7,14 @@ from datetime import timedelta from contextlib import contextmanager from dataclasses import dataclass -from functools import partial, cached_property +from functools import partial, cached_property, total_ordering from importlib import import_module, invalidate_caches from pathlib import Path from typing import Type, Any, Optional from enum import Enum +from statsmodels.stats.proportion import proportion_confint + from pyecsca.ec.params import DomainParameters, get_params from pyecsca.ec.mult import * from pyecsca.ec.countermeasures import GroupScalarRandomization, AdditiveSplitting, MultiplicativeSplitting, EuclideanSplitting, BrumleyTuveri @@ -40,6 +42,7 @@ def enable_spawn(func): @dataclass(frozen=True) +@total_ordering class MultIdent: klass: Type[ScalarMultiplier] args: list[Any] @@ -85,6 +88,11 @@ class MultIdent: countermeasure = f"+{self.countermeasure}" if self.countermeasure is not None else "" return f"{name}{args}{kwargs}{countermeasure}" + def __lt__(self, other): + if not isinstance(other, MultIdent): + return NotImplemented + return str(self) < str(other) + def __repr__(self): return str(self) @@ -201,8 +209,10 @@ naf_mults = [ MultIdent(WindowNAFMultiplier, width=4), MultIdent(WindowNAFMultiplier, width=5), MultIdent(WindowNAFMultiplier, width=6), - MultIdent(BinaryNAFMultiplier, direction=ProcessingDirection.LTR), - MultIdent(BinaryNAFMultiplier, direction=ProcessingDirection.RTL) + MultIdent(BinaryNAFMultiplier, always=False, direction=ProcessingDirection.LTR), + MultIdent(BinaryNAFMultiplier, always=False, direction=ProcessingDirection.RTL) + MultIdent(BinaryNAFMultiplier, always=True, direction=ProcessingDirection.LTR), + MultIdent(BinaryNAFMultiplier, always=True, direction=ProcessingDirection.RTL) ] comb_mults = [ MultIdent(CombMultiplier, width=2, always=True), @@ -248,3 +258,40 @@ other_mults = [ all_mults = window_mults + naf_mults + binary_mults + other_mults + comb_mults all_mults_with_ctr = [mult.with_countermeasure(ctr) for mult in all_mults for ctr in (None, "gsr", "additive", "multiplicative", "euclidean", "bt")] + + +def powers_of(k, max_power=20): + return [k**i for i in range(1, max_power)] + +def prod_combine(one, other): + return [a * b for a, b in itertools.product(one, other)] + +small_primes = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] +medium_primes = [211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397] +large_primes = [401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997] +all_integers = list(range(1, 400)) +all_even = list(range(2, 400, 2)) +all_odd = list(range(1, 400, 2)) +all_primes = small_primes + medium_primes + large_primes + +divisor_map = { + "small_primes": small_primes, + "medium_primes": medium_primes, + "large_primes": large_primes, + "all_primes": all_primes, + "all_integers": all_integers, + "all_even": all_even, + "all_odd": all_odd, + "powers_of_2": powers_of(2), + "powers_of_2_large": powers_of(2, 256), + "powers_of_2_large_3": [i * 3 for i in powers_of(2, 256)], + "powers_of_2_large_p1": [i + 1 for i in powers_of(2, 256)], + "powers_of_2_large_m1": [i - 1 for i in powers_of(2, 256)], + "powers_of_2_large_pmautobus": sorted(set([i + j for i in powers_of(2, 256) for j in range(-5,5) if i+j > 0])), + "powers_of_3": powers_of(3), +} +divisor_map["all"] = list(sorted(set().union(*[v for v in divisor_map.values()]))) + + +def conf_interval(p: float, samples: int, alpha: float = 0.05) -> tuple[float, float]: + return proportion_confint(round(p*samples), samples, alpha, method="wilson")
\ No newline at end of file diff --git a/epare/probmaps.py b/epare/probmaps.py index 980f9ed..0ad13a3 100644 --- a/epare/probmaps.py +++ b/epare/probmaps.py @@ -43,41 +43,7 @@ def load_chunk(fname: str, divisors: set[int], kind: str) -> dict[MultIdent, Pro return res -def powers_of(k, max_power=20): - return [k**i for i in range(1, max_power)] - - -def prod_combine(one, other): - return [a * b for a, b in itertools.product(one, other)] - - if __name__ == "__main__": - small_primes = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] - medium_primes = [211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397] - large_primes = [401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997] - all_integers = list(range(1, 400)) - all_even = list(range(2, 400, 2)) - all_odd = list(range(1, 400, 2)) - all_primes = small_primes + medium_primes + large_primes - - divisor_map = { - "small_primes": small_primes, - "medium_primes": medium_primes, - "large_primes": large_primes, - "all_primes": all_primes, - "all_integers": all_integers, - "all_even": all_even, - "all_odd": all_odd, - "powers_of_2": powers_of(2), - "powers_of_2_large": powers_of(2, 256), - "powers_of_2_large_3": [i * 3 for i in powers_of(2, 256)], - "powers_of_2_large_p1": [i + 1 for i in powers_of(2, 256)], - "powers_of_2_large_m1": [i - 1 for i in powers_of(2, 256)], - "powers_of_2_large_pmautobus": sorted(set([i + j for i in powers_of(2, 256) for j in range(-5,5) if i+j > 0])), - "powers_of_3": powers_of(3), - } - divisor_map["all"] = list(sorted(set().union(*[v for v in divisor_map.values()]))) - distributions_mults = {} bits = 256 num_workers = int(sys.argv[1]) if len(sys.argv) > 1 else 32 @@ -106,6 +72,7 @@ if __name__ == "__main__": distributions_mults[mult] = prob_map for mult, prob_map in distributions_mults.items(): print(f"Got {prob_map.samples} for {mult}.") + # Save with open(f"{divisor_name}_{kind}_distrs.pickle", "wb") as f: pickle.dump(distributions_mults, f) |
