aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/configuration.py
blob: a8c5fee6d6a334f8fd60ffd694fdc3ddb3c9e1f3 (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
"""Provides a way to work with and enumerate implementation configurations."""

import warnings
from abc import ABC
from dataclasses import dataclass
from enum import Enum
from itertools import product
from typing import (
    get_type_hints,
    Union,
    get_origin,
    get_args,
    Generator,
    FrozenSet,
)

from public import public

from pyecsca.ec.coordinates import CoordinateModel
from pyecsca.ec.formula import Formula
from pyecsca.ec.model import CurveModel
from pyecsca.ec.mult import ScalarMultiplier


@public
class EnumDefine(Enum):
    def __str__(self):
        return self.value

    def __repr__(self):
        return self.value

    @classmethod
    def names(cls):
        return [e.name for e in cls]

    @classmethod
    def values(cls):
        return [e.value for e in cls]


@public
class Multiplication(EnumDefine):
    """Base multiplication algorithm to use."""

    TOOM_COOK = "MUL_TOOM_COOK"
    KARATSUBA = "MUL_KARATSUBA"
    COMBA = "MUL_COMBA"
    BASE = "MUL_BASE"


@public
class Squaring(EnumDefine):
    """Base squaring algorithm to use."""

    TOOM_COOK = "SQR_TOOM_COOK"
    KARATSUBA = "SQR_KARATSUBA"
    COMBA = "SQR_COMBA"
    BASE = "SQR_BASE"


@public
class Reduction(EnumDefine):
    """Modular reduction method used."""

    BARRETT = "RED_BARRETT"
    MONTGOMERY = "RED_MONTGOMERY"
    BASE = "RED_BASE"


@public
class Inversion(EnumDefine):
    """Inversion algorithm used."""

    GCD = "INV_GCD"
    EULER = "INV_EULER"


@public
class HashType(EnumDefine):
    """Hash algorithm used in ECDH and ECDSA."""

    NONE = "HASH_NONE"
    SHA1 = "HASH_SHA1"
    SHA224 = "HASH_SHA224"
    SHA256 = "HASH_SHA256"
    SHA384 = "HASH_SHA384"
    SHA512 = "HASH_SHA512"


@public
class RandomMod(EnumDefine):
    """Method of sampling a uniform integer modulo order."""

    SAMPLE = "MOD_RAND_SAMPLE"
    REDUCE = "MOD_RAND_REDUCE"


@public
@dataclass(frozen=True, eq=True)
class Configuration:
    """An ECC implementation configuration."""

    model: CurveModel
    coords: CoordinateModel
    formulas: FrozenSet[Formula]
    scalarmult: ScalarMultiplier
    hash_type: HashType
    mod_rand: RandomMod
    mult: Multiplication
    sqr: Squaring
    red: Reduction
    inv: Inversion


@public
def all_configurations(**kwargs) -> Generator[Configuration, None, None]:
    """
    Get all implementation configurations that match the given `kwargs`.

    The keys in :paramref:`~.all_configurations.kwargs` should be some of the attributes in the :py:class:`Configuration`,
    and the values limit the returned configurations to configuration matching them.

    .. note::
        The ``formulas`` attribute is unsupported and formulas should be provided using the ``scalarmult``
        attribute, which is either a subclass of the :py:class:`~.ScalarMultiplier` class or an instance
        of it or a dictionary giving arguments to a constructor of some :py:class:`~.ScalarMultiplier`
        subclass.

    .. warning::
        The returned number of configurations might be quite large and take up significant
        memory space. Use this generator and do not store the results.

    :param kwargs: The configuration parameters to match.
    :return: A generator of the configurations.
    """

    def is_optional(arg_type):
        return (
            get_origin(arg_type) == Union
            and len(get_args(arg_type)) == 2
            and get_args(arg_type)[1] is type(None)  # noqa
        )

    def leaf_subclasses(cls):
        subs = cls.__subclasses__()
        result = set()
        for subclass in subs:
            if subclass.__subclasses__():
                result.update(leaf_subclasses(subclass))
            elif ABC not in subclass.__bases__:
                result.add(subclass)
        return result

    def independents(kwargs):
        options = {
            "hash_type": HashType,
            "mod_rand": RandomMod,
            "mult": Multiplication,
            "sqr": Squaring,
            "red": Reduction,
            "inv": Inversion,
        }
        keys = list(filter(lambda key: key not in kwargs, options.keys()))
        values = [options[key] for key in keys]
        fixed_args = {key: kwargs[key] for key in kwargs if key in options}
        for value_choice in product(*values):
            yield dict(zip(keys, value_choice), **fixed_args)

    def multipliers(mult_classes, coords_formulas, fixed_args=None):
        for mult_cls in mult_classes:
            if (
                fixed_args is not None
                and "cls" in fixed_args
                and mult_cls != fixed_args["cls"]
            ):
                continue
            arg_options = {}
            for name, required_type in get_type_hints(mult_cls.__init__).items():
                if fixed_args is not None and name in fixed_args:
                    arg_options[name] = [fixed_args[name]]
                    continue
                if is_optional(required_type):
                    opt_type = get_args(required_type)[0]
                    if issubclass(opt_type, Formula):
                        options = [
                            formula
                            for formula in coords_formulas
                            if isinstance(formula, opt_type)
                        ] + [None]
                    else:
                        options = [None]  # TODO: anything here?
                elif get_origin(required_type) is None and issubclass(
                    required_type, Formula
                ):
                    options = [
                        formula
                        for formula in coords_formulas
                        if isinstance(formula, required_type)
                    ]
                elif get_origin(required_type) is None and issubclass(
                    required_type, bool
                ):
                    options = [True, False]
                elif get_origin(required_type) is None and issubclass(
                    required_type, Enum
                ):
                    options = list(required_type)
                elif (
                    get_origin(required_type) is None
                    and issubclass(required_type, int)
                    and name == "width"
                ):
                    # Magic numbers from library analysis, comb/window width
                    options = [4, 5, 6, 7]
                elif (
                    get_origin(required_type) is None
                    and issubclass(required_type, int)
                    and name == "m"
                ):
                    # Magic numbers from library analysis, comb/window width
                    options = [2**4, 2**5, 2**6, 2**7]
                else:
                    warnings.warn(
                        RuntimeWarning(f"Unknown scalarmult option range = {name}")
                    )
                    options = []
                arg_options[name] = options
            keys = arg_options.keys()
            values = arg_options.values()
            for combination in product(*values):
                try:
                    mult = mult_cls(**dict(zip(keys, combination)))
                except Exception:
                    continue
                yield mult

    for model_cls in leaf_subclasses(CurveModel):
        model = model_cls()
        if "model" in kwargs and model != kwargs["model"]:
            continue
        for coords in model.coordinates.values():
            if "coords" in kwargs and coords != kwargs["coords"]:
                continue
            coords_formulas = coords.formulas.values()
            mult_classes = leaf_subclasses(ScalarMultiplier)

            if "scalarmult" in kwargs:
                if isinstance(kwargs["scalarmult"], ScalarMultiplier):
                    mults = [kwargs["scalarmult"]]
                    if not set(kwargs["scalarmult"].formulas.values()).issubset(
                        coords_formulas
                    ):
                        continue
                elif isinstance(kwargs["scalarmult"], type) and issubclass(
                    kwargs["scalarmult"], ScalarMultiplier
                ):
                    mult_classes = list(
                        filter(
                            lambda mult: issubclass(mult, kwargs["scalarmult"]),
                            mult_classes,
                        )
                    )
                    mults = multipliers(mult_classes, coords_formulas)
                else:
                    mults = multipliers(
                        mult_classes, coords_formulas, kwargs["scalarmult"]
                    )
            else:
                mults = multipliers(mult_classes, coords_formulas)
            for mult in mults:
                formulas = frozenset(mult.formulas.values())
                for independent_args in independents(kwargs):
                    yield Configuration(
                        model, coords, formulas, mult, **independent_args
                    )