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
|
"""
This module provides functions for runtime configuration of the toolkit.
This includes how errors are handled, or which :py:class:`~pyecsca.ec.mod.Mod` implementation is used.
"""
from copy import deepcopy
from contextvars import ContextVar, Token
from public import public
@public
class ECConfig:
"""Configuration for the :py:mod:`pyecsca.ec` package."""
_no_inverse_action: str = "error"
_non_residue_action: str = "error"
_unsatisfied_formula_assumption_action: str = "error"
_unsatisfied_coordinate_assumption_action: str = "error"
_mod_implementation: str = "gmp"
@property
def no_inverse_action(self) -> str:
"""
Return or set the action to take when a non-invertible element is to be inverted.
One of:
- ``"error"``: Raise :py:class:`pyecsca.ec.error.NonInvertibleError`.
- ``"warning"``: Raise :py:class:`pyecsca.ec.error.NonInvertibleWarning`.
- ``"ignore"``: Ignore the event and compute as if nothing happened.
"""
return self._no_inverse_action
@no_inverse_action.setter
def no_inverse_action(self, value: str):
if value not in ("error", "warning", "ignore"):
raise ValueError("Action has to be one of 'error', 'warning', 'ignore'.")
self._no_inverse_action = value
@property
def non_residue_action(self) -> str:
"""
Return or set the action to take when a the square-root of a non-residue is to be computed.
One of:
- ``"error"``: Raise :py:class:`pyecsca.ec.error.NonResidueError`.
- ``"warning"``: Raise :py:class:`pyecsca.ec.error.NonResidueWarning`.
- ``"ignore"``: Ignore the event and compute as if nothing happened.
"""
return self._non_residue_action
@non_residue_action.setter
def non_residue_action(self, value: str):
if value not in ("error", "warning", "ignore"):
raise ValueError("Action has to be one of 'error', 'warning', 'ignore'.")
self._non_residue_action = value
@property
def unsatisfied_formula_assumption_action(self) -> str:
"""
Return or set the action to take when a formula assumption is unsatisfied during execution.
This works for assumption that can be ignored without a fatal error,
which are those that are not used to compute a value of an undefined parameter.
For example, things of the form ``Z1 = 1``.
One of:
- ``"error"``: Raise :py:class:`pyecsca.ec.error.UnsatisfiedAssumptionError`.
- ``"warning"``: Raise :py:class:`pyecsca.ec.error.UnsatisfiedAssumptionWarning`.
- ``"ignore"``: Ignore the event and compute as if nothing happened.
"""
return self._unsatisfied_formula_assumption_action
@unsatisfied_formula_assumption_action.setter
def unsatisfied_formula_assumption_action(self, value: str):
if value not in ("error", "warning", "ignore"):
raise ValueError("Action has to be one of 'error', 'warning', 'ignore'.")
self._unsatisfied_formula_assumption_action = value
@property
def unsatisfied_coordinate_assumption_action(self) -> str:
"""
Return or set the action to take when a coordinate assumption is unsatisfied during curve creation.
This works for assumption that can be ignored without a fatal error,
which are those that are not used to compute a value of an undefined parameter.
For example, things of the form ``a = -1``.
One of:
- ``"error"``: Raise :py:class:`pyecsca.ec.error.UnsatisfiedAssumptionError`.
- ``"warning"``: Raise :py:class:`pyecsca.ec.error.UnsatisfiedAssumptionWarning`.
- ``"ignore"``: Ignore the event and compute as if nothing happened.
"""
return self._unsatisfied_coordinate_assumption_action
@unsatisfied_coordinate_assumption_action.setter
def unsatisfied_coordinate_assumption_action(self, value: str):
if value not in ("error", "warning", "ignore"):
raise ValueError("Action has to be one of 'error', 'warning', 'ignore'.")
self._unsatisfied_coordinate_assumption_action = value
@property
def mod_implementation(self) -> str:
"""
Return or set the selected :py:class:`pyecsca.ec.mod.Mod` implementation.
One of:
- ``"gmp"``: Requires the GMP library and `gmpy2` package.
- ``"python"``: Doesn't require anything.
"""
return self._mod_implementation
@mod_implementation.setter
def mod_implementation(self, value: str):
if value not in ("python", "gmp"):
raise ValueError("Bad Mod implementaiton, can be one of 'python' or 'gmp'.")
self._mod_implementation = value
@public
class Config:
"""Runtime configuration for the library."""
ec: ECConfig
"""Configuration for the :py:mod:`pyecsca.ec` package."""
def __init__(self):
self.ec = ECConfig()
_config: ContextVar[Config] = ContextVar("config", default=Config())
@public
def getconfig() -> Config:
"""
Get the current config.
:return: The current config.
"""
return _config.get()
@public
def setconfig(cfg: Config) -> Token:
"""
Set the current config.
:param cfg: The config to set.
:return: A token that can be used to reset the config to the previous one.
"""
return _config.set(cfg)
@public
def resetconfig(token: Token) -> None:
"""
Reset the config to the previous one.
:param token: A token from :py:func:`setconfig()`.
"""
_config.reset(token)
@public
class TemporaryConfig:
"""
Temporary config context manager.
Can be entered as follows:
.. code-block:: python
with TemporaryConfig() as cfg:
cfg.some_property = some_value
...
"""
def __init__(self):
self.token = None
self.new_config = deepcopy(getconfig())
def __enter__(self) -> Config:
self.token = setconfig(self.new_config)
return self.new_config
def __exit__(self, t, v, tb):
resetconfig(self.token)
|