aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/formula/base.py
blob: a556367733a7e5d0e168d9fd7125104bdf353e6f (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"""Provides an abstract base class of a formula."""

from abc import ABC
from ast import Expression
from functools import cached_property
from itertools import product

from astunparse import unparse
from typing import List, Any, ClassVar, MutableMapping, Tuple, Union, Dict

from public import public
from sympy import FF, symbols, Poly

from pyecsca.ec.context import ResultAction
from pyecsca.ec import context
from pyecsca.ec.error import UnsatisfiedAssumptionError, raise_unsatisified_assumption
from pyecsca.ec.mod import Mod, mod, SymbolicMod
from pyecsca.ec.op import CodeOp, OpType
from pyecsca.misc.cfg import getconfig
from pyecsca.misc.cache import sympify


@public
class OpResult:
    """Result of an operation."""

    parents: Tuple
    op: OpType
    name: str
    value: Mod

    def __init__(self, name: str, value: Mod, op: OpType, *parents: Any):
        if len(parents) != op.num_inputs:
            raise ValueError(
                f"Wrong number of parents ({len(parents)}) to OpResult: {op} ({op.num_inputs})."
            )
        self.parents = tuple(parents)
        self.name = name
        self.value = value
        self.op = op

    def __str__(self):
        return self.name

    def __repr__(self):
        char = self.op.op_str
        if self.op == OpType.Inv:
            parents = "1" + char + str(self.parents[0])
        elif self.op == OpType.Neg:
            parents = char + str(self.parents[0])
        else:
            parents = char.join(str(parent) for parent in self.parents)
        return f"{self.name} = {parents}"


@public
class FormulaAction(ResultAction):
    """Execution of a formula, on some input points and parameters, with some outputs."""

    formula: "Formula"
    """The formula that was executed."""
    inputs: MutableMapping[str, Mod]
    """The input variables (point coordinates and parameters)."""
    input_points: List[Any]
    """The input points."""
    intermediates: MutableMapping[str, List[OpResult]]
    """Intermediates computed during execution."""
    op_results: List[OpResult]
    """The intermediates but ordered as they were computed."""
    outputs: MutableMapping[str, OpResult]
    """The output variables."""
    output_points: List[Any]
    """The output points."""

    def __init__(self, formula: "Formula", *points: Any, **inputs: Mod):
        super().__init__()
        self.formula = formula
        self.inputs = inputs
        self.intermediates = {}
        self.op_results = []
        self.outputs = {}
        self.input_points = list(points)
        self.output_points = []

    def add_operation(self, op: CodeOp, value: Mod):
        parents: List[Union[int, Mod, OpResult]] = []
        for parent in op.parents:
            if isinstance(parent, str):
                if parent in self.intermediates:
                    parents.append(self.intermediates[parent][-1])
                elif parent in self.inputs:
                    parents.append(self.inputs[parent])
            else:
                parents.append(parent)
        result = OpResult(op.result, value, op.operator, *parents)
        li = self.intermediates.setdefault(op.result, [])
        li.append(result)
        self.op_results.append(result)

    def add_result(self, point: Any, **outputs: Mod):
        for k in outputs:
            self.outputs[k] = self.intermediates[k][-1]
        self.output_points.append(point)

    def __str__(self):
        return f"{self.__class__.__name__}({self.formula})"

    def __repr__(self):
        return f"{self.__class__.__name__}({self.formula}, {self.input_points}) = {self.output_points}"


_assumption_cache: Dict[Tuple[str, str, FF, Tuple[Mod, ...]], Mod] = {}


@public
class Formula(ABC):
    """Formula operating on points."""

    name: str
    """Name of the formula."""
    shortname: ClassVar[str]
    """A shortname for the type of the formula."""
    coordinate_model: Any
    """Coordinate model of the formula."""
    meta: MutableMapping[str, Any]
    """Meta information about the formula, such as its source."""
    parameters: List[str]
    """Formula parameters (i.e. new parameters introduced by the formula, like `half = 1/2`)."""
    assumptions: List[Expression]
    """Assumptions of the formula (e.g. `Z1 == 1` or `2*half == 1`)."""
    code: List[CodeOp]
    """The collection of ops that constitute the code of the formula."""
    num_inputs: ClassVar[int]
    """Number of inputs (points) of the formula."""
    num_outputs: ClassVar[int]
    """Number of outputs (points) of the formula."""
    unified: bool
    """Whether the formula specifies that it is unified."""

    @cached_property
    def assumptions_str(self):
        return [unparse(assumption)[1:-2] for assumption in self.assumptions]

    def __validate_params(self, field, params):
        for key, value in params.items():
            if not isinstance(value, Mod) or value.n != field:
                raise ValueError(f"Wrong param input {key} = {value}.")

    def __validate_points(self, field, points, params):
        # Validate number of inputs.
        if len(points) != self.num_inputs:
            raise ValueError(f"Wrong number of inputs for {self}.")
        # Validate input points and unroll them into input params.
        for i, point in enumerate(points):
            if point.coordinate_model != self.coordinate_model:
                raise ValueError(f"Wrong coordinate model of point {point}.")
            for coord, value in point.coords.items():
                if not isinstance(value, Mod) or value.n != field:
                    raise ValueError(
                        f"Wrong coordinate input {coord} = {value} of point {i}."
                    )
                params[coord + str(i + 1)] = value

    def __validate_assumption_point(self, assumption, params):
        # Handle an assumption check on value of input points.
        alocals: Dict[str, Union[Mod, int]] = {**params}
        compiled = compile(assumption, "", mode="eval")
        holds = eval(compiled, None, alocals)  # eval is OK here, skipcq: PYL-W0123
        return holds

    def __validate_assumption_simple(self, lhs, rhs, field, params):
        # Handle a simple parameter assignment (lhs is an unassigned parameter of the formula).
        expr = sympify(rhs, evaluate=False)
        used_symbols = sorted(expr.free_symbols)
        used_params = []
        for symbol in used_symbols:
            if (value := params.get(symbol.name, None)) is not None:
                used_params.append(value)
                if isinstance(value, SymbolicMod):
                    expr = expr.xreplace({symbol: value.x})
                else:
                    expr = expr.xreplace({symbol: int(value)})
            else:
                return False
        cache_key = (lhs, rhs, field, tuple(used_params))
        if cache_key in _assumption_cache:
            params[lhs] = _assumption_cache[cache_key]
        else:
            if any(isinstance(x, SymbolicMod) for x in params.values()):
                params[lhs] = SymbolicMod(expr, field)
            else:
                domain = FF(field)
                numerator, denominator = expr.as_numer_denom()
                val = int(domain.from_sympy(numerator) / domain.from_sympy(denominator))
                params[lhs] = mod(val, field)
            _assumption_cache[cache_key] = params[lhs]
        return True

    def __validate_assumption_generic(self, lhs, rhs, field, params, assumption_string):
        # Handle a generic parameter assignment (parameter may be anyway in the assumption).
        expr = sympify(f"{rhs} - {lhs}", evaluate=False)
        remaining = []
        for symbol in expr.free_symbols:
            if (value := params.get(symbol.name, None)) is not None:
                if isinstance(value, SymbolicMod):
                    expr = expr.xreplace({symbol: value.x})
                else:
                    expr = expr.xreplace({symbol: int(value)})
            else:
                remaining.append(symbol)
        if len(remaining) > 1 or (param := remaining[0].name) not in self.parameters:
            raise ValueError(
                f"This formula couldn't be executed due to an unsupported assumption ({assumption_string})."
            )
        numerator, _ = expr.as_numer_denom()
        domain = FF(field)
        poly = Poly(numerator, symbols(param), domain=domain)
        roots = poly.ground_roots()
        for root in roots:
            params[param] = mod(int(domain.from_sympy(root)), field)
            return
        raise UnsatisfiedAssumptionError(
            f"Unsatisfied assumption in the formula ({assumption_string}).\n"
            f"'{expr}' has no roots in the base field GF({field})."
        )

    def __validate_assumptions(self, field, params):
        # Validate assumptions and compute formula parameters.
        # TODO: Should this also validate coordinate assumptions and compute their parameters?
        for assumption, assumption_string in zip(
            self.assumptions, self.assumptions_str
        ):
            lhs, rhs = assumption_string.split(" == ")
            if lhs in params:
                if not self.__validate_assumption_point(assumption, params):
                    raise_unsatisified_assumption(
                        getconfig().ec.unsatisfied_formula_assumption_action,
                        f"Unsatisfied assumption in the formula ({assumption_string}).",
                    )
            elif lhs in self.parameters:
                if not self.__validate_assumption_simple(lhs, rhs, field, params):
                    raise_unsatisified_assumption(
                        getconfig().ec.unsatisfied_formula_assumption_action,
                        f"Unsatisfied assumption in the formula ({assumption_string}).",
                    )
            else:
                self.__validate_assumption_generic(
                    lhs, rhs, field, params, assumption_string
                )

    def __call__(self, field: int, *points: Any, **params: Mod) -> Tuple[Any, ...]:
        """
        Execute a formula.

        :param field: The field over which the computation is performed.
        :param points: Points to pass into the formula.
        :param params: Parameters of the curve.
        :return: The resulting point(s).
        """
        from pyecsca.ec.point import Point

        self.__validate_params(field, params)
        self.__validate_points(field, points, params)
        if self.assumptions:
            self.__validate_assumptions(field, params)
        # Execute the actual formula.
        with FormulaAction(self, *points, **params) as action:
            for op in self.code:
                op_result = op(**params)
                # This check and cast fixes the issue when the op is `Z3 = 1`.
                # TODO: This is not general enough, if for example the op is `t = 1/2`, it will be float.
                #       Temporarily, add an assertion that this does not happen so we do not give bad results.
                if isinstance(op_result, float):
                    raise AssertionError(
                        f"Bad stuff happened in op {op}, floats will pollute the results."
                    )
                if not isinstance(op_result, Mod):
                    op_result = mod(op_result, field)
                if context.current is not None:
                    action.add_operation(op, op_result)
                params[op.result] = op_result
            result = []
            # Go over the outputs and construct the resulting points.
            for i in range(self.num_outputs):
                ind = str(i + self.output_index)
                resulting = {}
                full_resulting = {}
                for variable in self.coordinate_model.variables:
                    full_variable = variable + ind
                    resulting[variable] = params[full_variable]
                    full_resulting[full_variable] = params[full_variable]
                point = Point(self.coordinate_model, **resulting)

                if context.current is not None:
                    action.add_result(point, **full_resulting)
                result.append(point)
            return action.exit(tuple(result))

    def __lt__(self, other):
        if not isinstance(other, Formula):
            raise TypeError("Cannot compare.")
        if self.name is None:
            return True
        if other.name is None:
            return False
        return self.name < other.name

    def __str__(self):
        return f"{self.coordinate_model!s}/{self.name}"

    def __repr__(self):
        return f"{self.__class__.__name__}({self.name} for {self.coordinate_model})"

    @cached_property
    def input_index(self):
        """Return the starting index where this formula reads its inputs."""
        return 1

    @cached_property
    def output_index(self):
        """Return the starting index where this formula stores its outputs."""
        return max(self.num_inputs + 1, 3)

    @cached_property
    def inputs(self):
        """Return the input variables of the formula."""
        return {
            var + str(i)
            for var, i in product(
                self.coordinate_model.variables, range(1, 1 + self.num_inputs)
            )
        }

    @cached_property
    def outputs(self):
        """Return the output variables of the formula."""
        return {
            var + str(i)
            for var, i in product(
                self.coordinate_model.variables,
                range(self.output_index, self.output_index + self.num_outputs),
            )
        }

    @property
    def num_operations(self) -> int:
        """Return the number of operations."""
        return len(list(filter(lambda op: op.operator is not None, self.code)))

    @property
    def num_multiplications(self) -> int:
        """Return the number of multiplications."""
        return len(list(filter(lambda op: op.operator == OpType.Mult, self.code)))

    @property
    def num_divisions(self) -> int:
        """Return the number of divisions."""
        return len(list(filter(lambda op: op.operator == OpType.Div, self.code)))

    @property
    def num_inversions(self) -> int:
        """Return the number of inversions."""
        return len(list(filter(lambda op: op.operator == OpType.Inv, self.code)))

    @property
    def num_powers(self) -> int:
        """Return the number of powers."""
        return len(list(filter(lambda op: op.operator == OpType.Pow, self.code)))

    @property
    def num_squarings(self) -> int:
        """Return the number of squarings."""
        return len(list(filter(lambda op: op.operator == OpType.Sqr, self.code)))

    @property
    def num_addsubs(self) -> int:
        """Return the number of additions and subtractions."""
        return len(
            list(filter(lambda op: op.operator in (OpType.Add, OpType.Sub), self.code))
        )


@public
class AdditionFormula(Formula, ABC):
    """Formula that adds two points."""

    shortname = "add"
    num_inputs = 2
    num_outputs = 1


@public
class DoublingFormula(Formula, ABC):
    """Formula that doubles a point."""

    shortname = "dbl"
    num_inputs = 1
    num_outputs = 1


@public
class TriplingFormula(Formula, ABC):
    """Formula that triples a point."""

    shortname = "tpl"
    num_inputs = 1
    num_outputs = 1


@public
class NegationFormula(Formula, ABC):
    """Formula that negates a point."""

    shortname = "neg"
    num_inputs = 1
    num_outputs = 1


@public
class ScalingFormula(Formula, ABC):
    """Formula that somehow scales the point (to a given representative of a projective class)."""

    shortname = "scl"
    num_inputs = 1
    num_outputs = 1


@public
class DifferentialAdditionFormula(Formula, ABC):
    """
    Differential addition formula that adds two points with a known difference.

    The first input point is the difference of the third input and the second input (`P[0] = P[2] - P[1]`).
    """

    shortname = "dadd"
    num_inputs = 3
    num_outputs = 1


@public
class LadderFormula(Formula, ABC):
    """
    Ladder formula for simultaneous addition of two points and doubling of the one of them, with a known difference.

    The first input point is the difference of the third input and the second input (`P[0] = P[2] - P[1]`).
    The first output point is the doubling of the second input point (`O[0] = 2 * P[1]`).
    The second output point is the addition of the second and third input points (`O[1] = P[1] + P[2]`).
    """

    shortname = "ladd"
    num_inputs = 3
    num_outputs = 2