aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/model.py
blob: 15affa2adf3d21165024169671e9ac64fcbd5d37 (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
"""Provides curve model classes for the supported curve models."""

from ast import parse, Expression, Module
from typing import List, MutableMapping
from importlib_resources import files, as_file
from importlib_resources.abc import Traversable

from public import public

from pyecsca.ec.coordinates import EFDCoordinateModel, CoordinateModel


@public
class CurveModel:
    """Model(form) of an elliptic curve."""

    name: str
    shortname: str
    coordinates: MutableMapping[str, CoordinateModel]
    parameter_names: List[str]
    coordinate_names: List[str]
    equation: Expression
    ysquared: Expression
    base_addition: List[Module]
    base_doubling: List[Module]
    base_negation: List[Module]
    base_neutral: List[Module]
    full_weierstrass: List[Module]
    to_weierstrass: List[Module]
    from_weierstrass: List[Module]


class EFDCurveModel(CurveModel):
    """A curve model from [EFD]_ data."""

    _efd_name: str
    _loaded: bool = False

    def __init__(self, efd_name: str):
        self._efd_name = efd_name
        self.shortname = efd_name
        if self._loaded:
            return
        self.__load(efd_name)

    def __load(self, efd_name: str):
        self.__class__._loaded = True  # skipcq: PYL-W0212
        self.__class__.coordinates = {}
        self.__class__.parameter_names = []
        self.__class__.coordinate_names = []
        self.__class__.base_addition = []
        self.__class__.base_doubling = []
        self.__class__.base_negation = []
        self.__class__.base_neutral = []
        self.__class__.full_weierstrass = []
        self.__class__.to_weierstrass = []
        self.__class__.from_weierstrass = []

        for entry in files("pyecsca.ec").joinpath("efd", efd_name).iterdir():
            with as_file(entry) as file_path:
                if entry.is_dir():
                    self.__read_coordinate_dir(
                        self.__class__, file_path, file_path.stem
                    )
                else:
                    self.__read_curve_file(self.__class__, file_path)

    def __read_curve_file(self, cls, file_path: Traversable):
        def format_eq(line, mode="exec"):
            return parse(line.replace("^", "**"), mode=mode)

        with file_path.open("rb") as f:
            for raw in f.readlines():
                line = raw.decode("ascii").rstrip()
                if line.startswith("name"):
                    cls.name = line[5:]
                elif line.startswith("parameter"):
                    cls.parameter_names.append(line[10:])
                elif line.startswith("coordinate"):
                    cls.coordinate_names.append(line[11:])
                elif line.startswith("satisfying"):
                    cls.equation = format_eq(line[11:], mode="eval")
                elif line.startswith("ysquared"):
                    cls.ysquared = format_eq(line[9:], mode="eval")
                elif line.startswith("addition"):
                    cls.base_addition.append(format_eq(line[9:]))
                elif line.startswith("doubling"):
                    cls.base_doubling.append(format_eq(line[9:]))
                elif line.startswith("negation"):
                    cls.base_negation.append(format_eq(line[9:]))
                elif line.startswith("neutral"):
                    cls.base_neutral.append(format_eq(line[8:]))
                elif line.startswith("toweierstrass"):
                    cls.to_weierstrass.append(format_eq(line[14:]))
                elif line.startswith("fromweierstrass"):
                    cls.from_weierstrass.append(format_eq(line[16:]))
                else:
                    cls.full_weierstrass.append(format_eq(line))

    def __read_coordinate_dir(self, cls, dir_path: Traversable, name: str):
        cls.coordinates[name] = EFDCoordinateModel(dir_path, name, self)

    def __eq__(self, other):
        if not isinstance(other, EFDCurveModel):
            return False
        return self._efd_name == other._efd_name

    def __getstate__(self):
        return self.__dict__.copy()

    def __setstate__(self, state):
        if not self.__class__._loaded:
            self.__load(state["_efd_name"])
        self.__dict__.update(state)

    def __hash__(self):
        return hash(self._efd_name)

    def __str__(self):
        return f"{self.__class__.__name__.replace('Model', '')}"

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


@public
class ShortWeierstrassModel(EFDCurveModel):
    """
    Short-Weierstrass curve model.

    .. math::

       y^2 = x^3 + a x + b
    """

    def __init__(self):
        super().__init__("shortw")


@public
class MontgomeryModel(EFDCurveModel):
    """
    Montgomery curve model.

    .. math::

       B y^2 = x^3 + A x^2 + x
    """

    def __init__(self):
        super().__init__("montgom")


@public
class EdwardsModel(EFDCurveModel):
    """
    Edwards curve model.

    .. math::

       x^2 + y^2 = c^2 (1 + d x^2 y^2)
    """

    def __init__(self):
        super().__init__("edwards")


@public
class TwistedEdwardsModel(EFDCurveModel):
    """
    Twisted-Edwards curve model.

    .. math::

       a x^2 + y^2 = 1 + d x^2 y^2
    """

    def __init__(self):
        super().__init__("twisted")


_dirs = list(files("pyecsca.ec").joinpath("efd").iterdir())
if not _dirs:
    import warnings

    warnings.warn(
        "EFD not available, pyecsca is mis-installed. "
        "Make sure that you check out the git submodules prior to install (when installing from git)."
    )