aboutsummaryrefslogtreecommitdiff
path: root/test/sca/test_rpa.py
blob: 7027a7de529cedd7b8958f5ae3b52a407059b2c1 (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
from functools import partial
from math import isqrt

import pytest

from pyecsca.ec.context import local
from pyecsca.ec.curve import EllipticCurve
from pyecsca.ec.mod import mod
from pyecsca.ec.model import ShortWeierstrassModel
from pyecsca.ec.mult import (
    LTRMultiplier,
    RTLMultiplier,
    BinaryNAFMultiplier,
    WindowNAFMultiplier,
    SimpleLadderMultiplier,
    AccumulationOrder,
    ProcessingDirection,
    SlidingWindowMultiplier,
    FixedWindowLTRMultiplier,
    FullPrecompMultiplier,
    BGMWMultiplier,
    CombMultiplier,
    WindowBoothMultiplier,
    LadderMultiplier,
    DifferentialLadderMultiplier,
)
from pyecsca.ec.params import DomainParameters
from pyecsca.ec.point import Point
from pyecsca.sca.re.rpa import (
    MultipleContext,
    rpa_point_0y,
    rpa_point_x0,
    rpa_distinguish,
    multiples_computed,
)


@pytest.fixture()
def model():
    return ShortWeierstrassModel()


@pytest.fixture()
def coords(model):
    return model.coordinates["projective"]


@pytest.fixture()
def add(coords):
    return coords.formulas["add-2007-bl"]


@pytest.fixture()
def dbl(coords):
    return coords.formulas["dbl-2007-bl"]


@pytest.fixture()
def neg(coords):
    return coords.formulas["neg"]


@pytest.fixture()
def rpa_params(model, coords):
    p = 0x85D265945A4F5681
    a = mod(0x7FC57B4110698BC0, p)
    b = mod(0x37113EA591B04527, p)
    gx = mod(0x80D2D78FDDB97597, p)
    gy = mod(0x5586D818B7910930, p)

    infty = Point(coords, X=mod(0, p), Y=mod(1, p), Z=mod(0, p))
    g = Point(coords, X=gx, Y=gy, Z=mod(1, p))
    curve = EllipticCurve(model, coords, p, infty, dict(a=a, b=b))
    return DomainParameters(curve, g, 0x85D265932D90785C, 1)


def test_multiples(rpa_params):
    multiples = multiples_computed(
        17, rpa_params, LTRMultiplier, LTRMultiplier, True, True
    )
    assert 1 in multiples
    assert 17 in multiples
    assert 0 not in multiples


def test_multiples_kind(rpa_params):
    multiples_all = multiples_computed(
        17, rpa_params, RTLMultiplier, RTLMultiplier, True, True,
        kind="all"
    )
    multiples_input = multiples_computed(
        17, rpa_params, RTLMultiplier, RTLMultiplier, True, True,
        kind="input"
    )
    multiples_necessary = multiples_computed(
        17, rpa_params, RTLMultiplier, RTLMultiplier, True, True,
        kind="necessary"
    )
    multiples_precomp = multiples_computed(
        17, rpa_params, RTLMultiplier, RTLMultiplier, True, True,
        kind="precomp+necessary"
    )
    assert multiples_all != multiples_input
    assert multiples_all != multiples_necessary
    assert multiples_input != multiples_necessary
    assert multiples_precomp == multiples_necessary

    wnaf = partial(WindowNAFMultiplier, width=4)
    multiples_all = multiples_computed(
        0xff, rpa_params, WindowNAFMultiplier, wnaf, True, True,
        kind="all"
    )
    multiples_input = multiples_computed(
        0xff, rpa_params, WindowNAFMultiplier, wnaf, True, True,
        kind="input"
    )
    multiples_necessary = multiples_computed(
        0xff, rpa_params, WindowNAFMultiplier, wnaf, True, True,
        kind="necessary"
    )
    multiples_precomp = multiples_computed(
        0xff, rpa_params, WindowNAFMultiplier, wnaf, True, True,
        kind="precomp+necessary"
    )
    assert multiples_all != multiples_input
    assert multiples_all != multiples_necessary
    assert multiples_input != multiples_necessary
    assert multiples_precomp != multiples_necessary


def test_x0_point(rpa_params):
    res = rpa_point_x0(rpa_params)
    assert res is not None
    assert res.y == 0


def test_0y_point(rpa_params):
    res = rpa_point_0y(rpa_params)
    assert res is not None
    assert res.x == 0


@pytest.fixture()
def distinguish_params_sw(model, coords):
    p = 0xCB5E1D94A6168511
    a = mod(0xB166CA7D2DFBF69F, p)
    b = mod(0x855BB40CB6937C4B, p)
    gx = mod(0x253B2638BD13D6F4, p)
    gy = mod(0x1E91A1A182287E71, p)

    infty = Point(coords, X=mod(0, p), Y=mod(1, p), Z=mod(0, p))
    g = Point(coords, X=gx, Y=gy, Z=mod(1, p))
    curve = EllipticCurve(model, coords, p, infty, dict(a=a, b=b))
    return DomainParameters(curve, g, 0xCB5E1D94601A3AC5, 1)


def test_distinguish_basic(distinguish_params_sw, add, dbl, neg):
    multipliers = [
        LTRMultiplier(add, dbl, None, False, AccumulationOrder.PeqPR, True, True),
        LTRMultiplier(add, dbl, None, True, AccumulationOrder.PeqPR, True, True),
        RTLMultiplier(add, dbl, None, False, AccumulationOrder.PeqPR, True),
        RTLMultiplier(add, dbl, None, True, AccumulationOrder.PeqPR, False),
        SimpleLadderMultiplier(add, dbl, None, True, True),
        BinaryNAFMultiplier(
            add, dbl, neg, None, ProcessingDirection.LTR, AccumulationOrder.PeqPR, True
        ),
        BinaryNAFMultiplier(
            add, dbl, neg, None, ProcessingDirection.RTL, AccumulationOrder.PeqPR, True
        ),
        WindowNAFMultiplier(
            add, dbl, neg, 3, None, AccumulationOrder.PeqPR, True, True
        ),
        WindowNAFMultiplier(
            add, dbl, neg, 4, None, AccumulationOrder.PeqPR, True, True
        ),
        WindowNAFMultiplier(
            add, dbl, neg, 5, None, AccumulationOrder.PeqPR, True, True
        ),
        WindowBoothMultiplier(
            add, dbl, neg, 3, None, AccumulationOrder.PeqPR, True, True
        ),
        WindowBoothMultiplier(
            add, dbl, neg, 4, None, AccumulationOrder.PeqPR, True, True
        ),
        WindowBoothMultiplier(
            add, dbl, neg, 5, None, AccumulationOrder.PeqPR, True, True
        ),
        SlidingWindowMultiplier(
            add, dbl, 3, None, ProcessingDirection.LTR, AccumulationOrder.PeqPR, True
        ),
        SlidingWindowMultiplier(
            add, dbl, 4, None, ProcessingDirection.LTR, AccumulationOrder.PeqPR, True
        ),
        SlidingWindowMultiplier(
            add, dbl, 5, None, ProcessingDirection.LTR, AccumulationOrder.PeqPR, True
        ),
        SlidingWindowMultiplier(
            add, dbl, 3, None, ProcessingDirection.RTL, AccumulationOrder.PeqPR, True
        ),
        SlidingWindowMultiplier(
            add, dbl, 4, None, ProcessingDirection.RTL, AccumulationOrder.PeqPR, True
        ),
        SlidingWindowMultiplier(
            add, dbl, 5, None, ProcessingDirection.RTL, AccumulationOrder.PeqPR, True
        ),
        FixedWindowLTRMultiplier(add, dbl, 3, None, AccumulationOrder.PeqPR, True),
        FixedWindowLTRMultiplier(add, dbl, 4, None, AccumulationOrder.PeqPR, True),
        FixedWindowLTRMultiplier(add, dbl, 5, None, AccumulationOrder.PeqPR, True),
        FixedWindowLTRMultiplier(add, dbl, 8, None, AccumulationOrder.PeqPR, True),
        FixedWindowLTRMultiplier(add, dbl, 16, None, AccumulationOrder.PeqPR, True),
        FullPrecompMultiplier(
            add,
            dbl,
            None,
            True,
            ProcessingDirection.LTR,
            AccumulationOrder.PeqPR,
            True,
            True,
        ),
        FullPrecompMultiplier(
            add,
            dbl,
            None,
            False,
            ProcessingDirection.LTR,
            AccumulationOrder.PeqPR,
            True,
            True,
        ),
        BGMWMultiplier(
            add, dbl, 2, None, ProcessingDirection.LTR, AccumulationOrder.PeqPR, True
        ),
        BGMWMultiplier(
            add, dbl, 3, None, ProcessingDirection.LTR, AccumulationOrder.PeqPR, True
        ),
        BGMWMultiplier(
            add, dbl, 4, None, ProcessingDirection.LTR, AccumulationOrder.PeqPR, True
        ),
        BGMWMultiplier(
            add, dbl, 5, None, ProcessingDirection.LTR, AccumulationOrder.PeqPR, True
        ),
        CombMultiplier(add, dbl, 2, None, AccumulationOrder.PeqPR, True),
        CombMultiplier(add, dbl, 3, None, AccumulationOrder.PeqPR, True),
        CombMultiplier(add, dbl, 4, None, AccumulationOrder.PeqPR, True),
        CombMultiplier(add, dbl, 5, None, AccumulationOrder.PeqPR, True),
    ]
    for real_mult in multipliers:

        def simulated_oracle(scalar, affine_point):
            point = affine_point.to_model(
                distinguish_params_sw.curve.coordinate_model,
                distinguish_params_sw.curve,
            )
            with local(MultipleContext()) as ctx:
                real_mult.init(distinguish_params_sw, point)
                real_mult.multiply(scalar)
            return any(
                map(lambda P: P.X == 0 or P.Y == 0, sum(ctx.parents.values(), []))
            )

        result = rpa_distinguish(distinguish_params_sw, multipliers, simulated_oracle)
        assert real_mult in result
        assert 1 == len(result)


def test_distinguish_ladders(curve25519):
    ladd = curve25519.curve.coordinate_model.formulas["ladd-1987-m"]
    dbl = curve25519.curve.coordinate_model.formulas["dbl-1987-m"]
    dadd = curve25519.curve.coordinate_model.formulas["dadd-1987-m"]

    multipliers = [
        LadderMultiplier(ladd, None, None, True, False, False),
        LadderMultiplier(ladd, dbl, None, False, False, False),
        LadderMultiplier(ladd, None, None, False, False, True),
        DifferentialLadderMultiplier(dadd, dbl, None, True, False, False),
        DifferentialLadderMultiplier(dadd, dbl, None, False, False, True),
        DifferentialLadderMultiplier(dadd, dbl, None, False, False, False),
    ]
    for real_mult in multipliers:

        def simulated_oracle(scalar, affine_point):
            point = affine_point.to_model(
                curve25519.curve.coordinate_model, curve25519.curve
            )
            with local(MultipleContext()) as ctx:
                real_mult.init(curve25519, point)
                real_mult.multiply(scalar)
            return any(map(lambda P: P.X == 0, sum(ctx.parents.values(), [])))

        result = rpa_distinguish(
            curve25519, multipliers, simulated_oracle, bound=isqrt(curve25519.order)
        )
        assert real_mult in result
        # These multipliers are not distinguishable by a binary RPA oracle.
        # assert 1 == len(result)