-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsupplementary.sage
396 lines (333 loc) · 11.2 KB
/
supplementary.sage
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
# Copyright (c) 2020 Rambus Inc.
# All rights reserved.
#
# Supplementary material for ladder paper:
# SAGE Implementation of the two main ladders,
# with optional zero avoidance
proof.arithmetic(false)
def random_nonzero(F):
"""Return a random nonzero element of F"""
r = F(0)
while r==F(0): r = F.random_element()
return r
def setup_ladder(xP,ga,gb):
"""Set up the Montgomery/Joye ladder"""
# This construction is homogeneous, and so
# should work even if the multiplies are montmuls.
r = random_nonzero(xP.parent())
xP *= r
r *= 1
rr = r^2
yP = 4*((xP^2 + ga*rr)*xP + gb*r*rr)
xP3 = 3*xP
m = xP*xP3 + ga*rr
xP3 *= yP
yP = yP^2
xRP = m^2 - xP3
yR = 2*m*xRP + yP
return (xRP,yP,yR,m)
def finish_ladder(xPQ,xRQ,yQ,m,ga,gb):
"""
Finish the ladder and output: xP / Z^2
Uses the "improved" technique which doesn't require initial x
But does require ga != 0 and gb != 0
"""
# Avoid m/2, y/2 by scaling other coordinates
xPQ *= 4
xRQ *= 4
yQ *= 4
# Recover coordinates multiplied by 3
xQ = m^2 - xPQ - xRQ
xP = 3*xPQ + xQ
xR = 3*xRQ + xQ
# The way to do this in 5 registers (not counting 3,ga,gb) is to compute
# xP, xQ, xR, m, yQ
# xP, xQ, xQ*xR, m, yQ
# xP, xQ*xR, m, c; have to clobber xQ with the multiply
# xQ+xR = 3*m^2 - xP
# xP, xQ*xR, xQ+xR, m, c
# rest can be done in the obvious way
c = 3*yQ - m*xQ
num = (xP*(xQ+xR) + xQ*xR + 6*c*m) * gb
den = (3*c^2 - xP*(xQ*xR)) * ga
# Check curve invariants
if num^3 != den^2 * ga*gb: return None
if den == 0: return "Bug or on twist: 0/0!"
# Could now: return xP*num*den
# fancier version with twist check...
xP *= 3*num^2
den *= 3*num
if not (den.is_square()): return "On twist!"
return xP/den
def montgomery_ladder_avoid_zero_complete_steps(state,key,steps,ga,gb,safe_steps):
"""
Montgomery ladder with zero avoidance.
This performs the part of the ladder that would not be safe without
the zero-avoidance steps.
"""
(yP,yQ,yR,m,xQP,xRP,prevswap) = state
dodge = start_dodge = False
for st in range(steps-safe_steps-1,-1,-1):
# Step which avoids the neutral point at additional cost
swap = not bool(key & (1<<st))
prevswap ^^= swap
swap ^^= dodge
prevstart = start_dodge
start_dodge = xQP==xRP and not dodge
if dodge: rotate = not prevswap
else: rotate = start_dodge
dodge = (dodge and prevswap) or start_dodge
if prevswap^^rotate:
# Swap Q,R
xQP,xRP = xRP,xQP
yQ,yR = yR,yQ
xRP = xQP-xRP
if prevstart:
# Swap P,R
(yP,yR) = (yR,yP)
(xQP,xRP) = (xRP,xQP)
# Rename
xRQ = xRP
xRP -= xQP
if rotate:
# Swap P,Q
(xRP,xRQ) = (xRQ,xRP)
(yP,yQ) = (yQ,yP)
g = xRQ^2
xRP *= g
yP *= yR*xRQ*g
k = yR^2
l = yR*yQ
m = k+l + 2*xRP
xRP = xRP^2 + yP
xQP = k*l
yP *= k
yQ = yP - m*xQP
yR = yP - m*xRP
prevswap = swap
if dodge and not prevswap:
return "infinity!"
if prevswap:
# Swap Q,R. Don't need y-coordinates
(xQP,xRP) = (xRP,xQP)
# Output Q
return finish_ladder(xQP,xRP,-yP,m,ga,gb)
def montgomery_ladder_avoid_zero(xP,key,steps,ga,gb,safe_steps):
"""
Montgomery ladder with zero avoidance.
Performs non-zero-avoidant steps, and then calls zero-avoidant routine.
"""
key = int(key)
(xRP,yP,_yR,m) = setup_ladder(xP,ga,gb)
xQP = 0
prevswap = False
for st in range(steps-1,steps-safe_steps-1,-1):
# Step which doesn't avoid neutral point
# Cost: 1 condswap, 8M + 3S + 7A
swap = not bool(key & (1<<st))
prevswap ^^= swap
if prevswap:
(xQP,xRP) = (xRP,xQP)
yR = yP + 2*m*xRP
E = xQP - xRP
F = yR*E
G = E^2
xRP *= G
m *= F
yP *= F*G
H = yR^2
K = H + m
L = K + m
m = xRP - K
xRP = xRP^2 + yP
xQP = H*L
yP *= H
prevswap = swap
# Expand yQ, yR so we can swap with temp
# Also this part of ladder uses negated y-coords to save a negation
m *= -2
yQ = yP - m*xQP
yR = yP - m*xRP
return montgomery_ladder_avoid_zero_complete_steps(
(yP,yQ,yR,m,xQP,xRP,prevswap),
key,steps,ga,gb,safe_steps
)
def qr_ladder_avoid_zero(xP,key,steps,ga,gb,safe_steps):
"""
(xQP,xRP,yQ,yR) Montgomery ladder.
Performs non-zero-avoidant steps, and then calls zero-avoidant routine.
"""
key = int(key)
(xRP,yQ,yR,m) = setup_ladder(xP,ga,gb)
m *= 2
xQP = 0
G = xRP^2
prevswap = False
for st in range(steps-1,steps-safe_steps-1,-1):
# Step which doesn't avoid neutral point
# Multiplies/squares can be parallelized 4 ways
swap = not bool(key & (1<<st))
prevswap ^^= swap
if prevswap:
(xQP,xRP) = (xRP,xQP)
(yR,yQ) = (yQ,yR)
(xQP, xRP, yQ, yR) = \
(xQP*G, xRP*G, yQ*yR, yR^2)
# Serial version: total 8M + 3S + 7A
J = xRP - yQ
m = J + xRP - yR
(za, zb, xQP, yQ2) = \
(xRP*J, xQP*yR, yR*yQ, J^2)
xRP = za + zb
xRQ = xRP - xQP
yQ3 = xRQ - yQ2
# # Alternative parallel version: total 9M+2S+8A
# # Adds parallelized 3 ways, for a total round
# # latency of 3M4 + 3A3
# J = xRP - yQ
# t2 = xQP - yQ
# t1 = yR - xRP
#
# (za, zc, zb, xQP) = \
# (xRP*J, yQ*J, t2*yR, yQ*yR)
#
# m = J - t1
# xRQ = za + zb
# yQ3 = zc + zb
#
# xRP = xRQ + xQP # postpone until after mul
(yQ, yRQ, G) = \
(yQ3*yR, m*xRQ, xRQ^2)
yR = yRQ + yQ
prevswap = swap
# Expand yP, yQ, yR so we can swap with temp
# Also this part of ladder uses negated y-coords to save a negation
yQ = -yQ
yP = yQ + m*xQP
yR = -yR
return montgomery_ladder_avoid_zero_complete_steps(
(yP,yQ,yR,m,xQP,xRP,prevswap),
key,steps,ga,gb,safe_steps
)
def joye_ladder_avoid_zero(xP,key,steps,ga,gb,safe_steps):
"""
Joye ladder with zero avoidance.
Unlike the Montgomery version, this uses a unified ladder step
which performs a few extra tweaks if zero avoidance is necessary.
"""
key = int(key)
(xRP,yP,yR,_m) = setup_ladder(xP,ga,gb)
(xRQ,yQ) = (xRP,yP)
dodge = prevswap = finish_dodge = False
for st in range(steps):
swap = bool(key&1<<st)
prevswap ^^= swap
if prevswap: (xRP,xRQ) = (xRQ,xRP)
safe = st < safe_steps
if safe:
# We know that we can't hit zero
# (unless maybe the input point was on the twist)
if prevswap: (yP,yQ) = (yQ,yP)
prevswap = swap
else:
# Play it safe, check for neutral point
if dodge: dodge_nxt = not prevswap
else: dodge_nxt = xRQ==0
dodge_cur = dodge or dodge_nxt
if prevswap^^dodge_cur:
# Swap P,Q
(yP,yQ) = (yQ,yP)
prevswap = swap^^dodge_cur^^dodge
# If dodge_cur: swap P,Q again (did y above, must do x)
# Then swap P,R
# To swap x, could use:
# if dodge_cur: (xRP, xRQ) = (xRQ, xRP-xRQ)
# But we can do it with no extra regs by:
xRQ = xRP-xRQ
if dodge_cur:
(xRP,xRQ) = (xRQ,xRP)
xRQ = xRP-xRQ
if dodge_cur:
(xRP,xRQ) = (xRQ,xRP)
(yP,yR) = (yR,yP)
# Ladder body
yP *= xRQ
xRQ = xRQ^2
xRP *= xRQ
yP *= xRQ
yP *= yR
yQ *= yR
h = yR^2
m = h + yQ - 2*xRP
xRP = xRP^2 - yP
yP *= h
yQ *= h
if not safe:
if dodge:
# results in swapping Q,R
xRP,yQ = yQ,xRP
dodge = dodge_nxt
xRQ = xRP - yQ
yQ = m*yQ + yP
yR = m*xRP + yP
if dodge and not prevswap:
return "infinity!"
if prevswap:
# swap P,Q
(xRP,xRQ) = (xRQ,xRP)
(yP,yQ) = (yQ,yP)
xPQ = xRQ-xRP
if dodge:
# swap P,R
xPQ,xRQ = xRQ,xPQ
# Output P
return finish_ladder(xPQ,xRQ,yQ,m,ga,gb)
if __name__ == "__main__":
for c in range(100):
while True:
p = random_prime(2^8,lbound=2^4)
F = GF(p)
ga = random_nonzero(F)
gb = random_nonzero(F)
try: E = EllipticCurve(F,[ga,gb])
except ArithmeticError: continue
q = E.cardinality()
if gcd(q,6) == 1: break
lgk = 20
lgks = ceil(log(min([fac for fac,_ in factor(q)]),2)) # largest prime factor
k = randint(0,2**lgk-1)
while True:
P = E.random_element()
if P != 0*P: break
Q = (2*k+1)*P
if Q.is_zero(): xQR = "infinity!"
else: xQR = Q.xy()[0]
xQ = joye_ladder_avoid_zero(P.xy()[0],k,lgk,ga,gb,lgks-2)
if xQ != xQR:
print("FAIL Joye: k=%d, right=%10s, wrong=%10s, p=%d, q=%d, o=%d"
% (k,xQR,str(xQ),p,q,P.order()))
Q = (k+2^lgk)*P
if Q.is_zero(): xQR = "infinity!"
else: xQR = Q.xy()[0]
xQ = montgomery_ladder_avoid_zero(P.xy()[0],k,lgk,ga,gb,lgks-2)
if xQ != xQR:
print("FAIL Mont: k=%d, right=%10s, wrong=%10s, p=%d, q=%d, o=%d"
% (k,xQR,str(xQ),p,q,P.order()))
xQ = qr_ladder_avoid_zero(P.xy()[0],k,lgk,ga,gb,lgks-2)
if xQ != xQR:
print("FAIL QR: k=%d, right=%10s, wrong=%10s, p=%d, q=%d, o=%d"
% (k,xQR,str(xQ),p,q,P.order()))
xP = F.random_element()
while E.is_x_coord(xP): xP = F.random_element()
xQ = joye_ladder_avoid_zero(xP,k,lgk,ga,gb,lgks-2)
if xQ not in ["On twist!", "infinity!", "Bug or on twist: 0/0!"]:
print("FAIL Joye twist: k=%d, right=%10s, wrong=%10s, p=%d, q=%d, o=%d"
% (k,xQR,str(xQ),p,q,P.order()))
xQ = montgomery_ladder_avoid_zero(xP,k,lgk,ga,gb,lgks-2)
if xQ not in ["On twist!", "infinity!", "Bug or on twist: 0/0!"]:
print("FAIL Mont twist: k=%d, right=%10s, wrong=%10s, p=%d, q=%d, o=%d"
% (k,xQR,str(xQ),p,q,P.order()))
xQ = qr_ladder_avoid_zero(xP,k,lgk,ga,gb,lgks-2)
if xQ not in ["On twist!", "infinity!", "Bug or on twist: 0/0!"]:
print("FAIL QR twist: k=%d, right=%10s, wrong=%10s, p=%d, q=%d, o=%d"
% (k,xQR,str(xQ),p,q,P.order()))