aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/formula/code.py
blob: 6eca838a13c35db86202e7b7fd35712003a671a3 (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
"""Provides a concrete class of a formula that has a constructor and some code."""

from typing import List, Any
from ast import Expression
from astunparse import unparse
from public import public

from pyecsca.ec.formula.base import (
    Formula,
    AdditionFormula,
    DoublingFormula,
    LadderFormula,
    TriplingFormula,
    NegationFormula,
    ScalingFormula,
    DifferentialAdditionFormula,
)
from pyecsca.ec.op import CodeOp
from pyecsca.misc.utils import peval


@public
class CodeFormula(Formula):
    """A basic formula class that can be directly initialized with the code and other attributes."""

    def __init__(
        self,
        name: str,
        code: List[CodeOp],
        coordinate_model: Any,
        parameters: List[str],
        assumptions: List[Expression],
        unified: bool = False,
    ):
        self.name = name
        self.code = code
        self.coordinate_model = coordinate_model
        self.meta = {}
        self.parameters = parameters
        self.assumptions = assumptions
        self.unified = unified

    def __hash__(self):
        return hash(
            (
                self.name,
                self.coordinate_model,
                tuple(self.code),
                tuple(self.parameters),
                tuple(self.assumptions_str),
                self.unified,
            )
        )

    def __eq__(self, other):
        if not isinstance(other, CodeFormula):
            return False
        return (
            self.name == other.name
            and self.coordinate_model == other.coordinate_model
            and self.code == other.code
            and self.parameters == other.parameters
            and self.assumptions_str == other.assumptions_str
            and self.unified == other.unified
        )

    def __getstate__(self):
        state = self.__dict__.copy()
        state["assumptions"] = list(map(unparse, state["assumptions"]))
        return state

    def __setstate__(self, state):
        state["assumptions"] = list(map(peval, state["assumptions"]))
        self.__dict__.update(state)


@public
class CodeAdditionFormula(AdditionFormula, CodeFormula):
    pass


@public
class CodeDoublingFormula(DoublingFormula, CodeFormula):
    pass


@public
class CodeLadderFormula(LadderFormula, CodeFormula):
    pass


@public
class CodeTriplingFormula(TriplingFormula, CodeFormula):
    pass


@public
class CodeNegationFormula(NegationFormula, CodeFormula):
    pass


@public
class CodeScalingFormula(ScalingFormula, CodeFormula):
    pass


@public
class CodeDifferentialAdditionFormula(DifferentialAdditionFormula, CodeFormula):
    pass