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
|
"""Provides formulas wrapping the [EFD]_."""
from copy import copy
from public import public
from importlib_resources.abc import Traversable
from typing import Any
from pyecsca.ec.formula.code import CodeFormula
from pyecsca.ec.formula.base import (
Formula,
CodeOp,
AdditionFormula,
DoublingFormula,
TriplingFormula,
NegationFormula,
ScalingFormula,
DifferentialAdditionFormula,
LadderFormula,
)
from pyecsca.misc.utils import pexec, peval
@public
class EFDFormula(Formula):
"""Formula from the [EFD]_."""
def __new__(cls, *args, **kwargs):
_, _, name, coordinate_model = args
if name in coordinate_model.formulas:
return coordinate_model.formulas[name]
return object.__new__(cls)
def __init__(
self,
meta_path: Traversable,
op3_path: Traversable,
name: str,
coordinate_model: Any,
):
self.name = name
self.coordinate_model = coordinate_model
self.meta = {}
self.parameters = []
self.assumptions = []
self.code = []
self.unified = False
self.__read_meta_file(meta_path)
self.__read_op3_file(op3_path)
def __read_meta_file(self, path: Traversable):
with path.open("rb") as f:
line = f.readline().decode("ascii").rstrip()
while line:
if line.startswith("source"):
self.meta["source"] = line[7:]
elif line.startswith("parameter"):
self.parameters.append(line[10:])
elif line.startswith("assume"):
self.assumptions.append(
peval(line[7:].replace("=", "==").replace("^", "**"))
)
elif line.startswith("unified"):
self.unified = True
line = f.readline().decode("ascii").rstrip()
def __read_op3_file(self, path: Traversable):
with path.open("rb") as f:
for line in f.readlines():
code_module = pexec(line.decode("ascii").replace("^", "**"))
self.code.append(CodeOp(code_module))
def to_code(self) -> CodeFormula:
for klass in CodeFormula.__subclasses__():
if self.shortname == klass.shortname:
return klass(
self.name,
copy(self.code),
self.coordinate_model,
copy(self.parameters),
copy(self.assumptions),
self.unified,
)
raise TypeError(f"CodeFormula not found for {self.__class__}.")
def __getnewargs__(self):
return None, None, self.name, self.coordinate_model
def __getstate__(self):
return {}
def __setstate__(self, state):
pass
def __eq__(self, other):
if not isinstance(other, EFDFormula):
return False
return (
self.name == other.name and self.coordinate_model == other.coordinate_model
)
def __hash__(self):
return hash((self.coordinate_model, self.name))
@public
class AdditionEFDFormula(AdditionFormula, EFDFormula):
pass
@public
class DoublingEFDFormula(DoublingFormula, EFDFormula):
pass
@public
class TriplingEFDFormula(TriplingFormula, EFDFormula):
pass
@public
class NegationEFDFormula(NegationFormula, EFDFormula):
pass
@public
class ScalingEFDFormula(ScalingFormula, EFDFormula):
pass
@public
class DifferentialAdditionEFDFormula(DifferentialAdditionFormula, EFDFormula):
pass
@public
class LadderEFDFormula(LadderFormula, EFDFormula):
pass
|