-
Notifications
You must be signed in to change notification settings - Fork 8
/
MontCurve.java
386 lines (277 loc) · 8.05 KB
/
MontCurve.java
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
package sidh;
/**************************************************************************************************
*
* Implements classes for Montgomery curves and points on the curve over GF(p) and GF(p^2).
* There are three classes for points on the curve: over Fp in both projective and affine
* coordinates and over Fp^2 in projective coordinates.
*
**************************************************************************************************/
import java.math.BigInteger;
class F2Point {
/* Points with coordinates in Fp^2 using (x:z) coordinates */
private F2elm x;
private F2elm z;
public static final F2Point INFINITY = new F2Point (F2elm.ONE, F2elm.ZERO);
public F2Point (F2elm xc, F2elm zc) {
x = new F2elm (xc);
z = new F2elm (zc);
}
public F2Point (Felm x0, Felm x1, Felm z0, Felm z1) {
x = new F2elm (x0, x1);
z = new F2elm (z0, z1);
}
public F2Point (F2Point p) {
x = new F2elm (p.x);
z = new F2elm (p.z);
}
public F2elm getX() {
return x;
}
public F2elm getZ() {
return z;
}
public void setX (F2elm xval) {
x = xval;
}
public F2Point swapPoints (F2Point q, BigInteger option) {
// If option = 0 then this <- this and q <- q, else this <- q and q <- this
F2elm qx, qz;
qx = x.f2Swap(q.x, option);
qz = z.f2Swap(q.z, option);
return new F2Point (qx, qz);
}
public void normalize () {
z.f2InverseInPlace ();
x.f2MultInPlace (z);
z = new F2elm (F2elm.ONE);
}
public String toString() {
return "(" + x + ": " + z + ")";
}
}
class MontCurve {
/*
* Montgomery curves are of the form By^2 = x^3 + (A/C)x^2 + x (using projective coefficients).
* It is assumed that B = 1 so it is omitted.
*/
protected F2elm a;
protected F2elm c;
// Useful precomputed values:
protected F2elm aMinus2c;
protected F2elm aPlus2c;
protected F2elm a24;
protected F2elm c4;
public MontCurve() {
// Default curve: y^2 = x^3 + x, ie set a = 0, b = c = 1.
// Precomputed values are set when needed
a = new F2elm (F2elm.ZERO);
c = new F2elm (F2elm.ONE);
}
public MontCurve (F2elm aA, F2elm cC) {
a = aA;
c = cC;
}
public MontCurve (MontCurve curveIn) {
a = new F2elm (curveIn.a);
c = new F2elm (curveIn.c);
}
public void initializeConstants () {
updateA24();
updateC4();
updatePlusMinus();
}
public void updateA24 () {
a24 = F2elm.leftShift (F2elm.ONE, 1);
a24.f2AddInPlace (a);
a24.f2Div2InPlace ();
a24.f2Div2InPlace ();
}
public void updateC4 () {
c4 = F2elm.leftShift (c, 2);
}
public void updatePlusMinus () {
F2elm c2 = F2elm.leftShift (c, 1);
aPlus2c = F2elm.add (a, c2);
aMinus2c = F2elm.sub (a, c2);
}
public void updateAC (int order) {
if (order == 3) {
a = F2elm.add (aPlus2c, aMinus2c);
a.f2LeftShiftInPlace (1);
c = F2elm.sub (aPlus2c, aMinus2c);
}
else {
c = F2elm.div2 (c4);
a = F2elm.sub (aPlus2c, c);
c.f2Div2InPlace ();
}
}
public F2elm getAPlus2c () {
return aPlus2c;
}
public void setAPlus2c (F2elm invalue) {
aPlus2c = invalue;
}
public void setAMinus2c (F2elm invalue) {
aMinus2c = invalue;
}
public void setC4 (F2elm invalue) {
c4 = invalue;
}
public static F2elm recoverA (F2elm px, F2elm qx, F2elm dx) {
F2elm t0, t1, ra;
t1 = F2elm.add (px, qx);
t0 = F2elm.mult (px, qx);
ra = F2elm.mult (dx, t1);
ra.f2AddInPlace (t0);
t0.f2MultInPlace (dx);
ra.f2SubInPlace (F2elm.ONE);
t0.f2LeftShiftInPlace (2);
t1.f2AddInPlace (dx);
ra.f2SqrInPlace ();
t0.f2InverseInPlace ();
ra.f2MultInPlace (t0);
ra.f2SubInPlace (t1);
return ra;
}
public F2Point xDbl (F2Point p) {
F2elm t0, t1, qx, qz, px, pz;
px = p.getX();
pz = p.getZ();
t0 = F2elm.sub (px, pz);
t1 = F2elm.add (px, pz);
t0.f2SqrInPlace ();
t1.f2SqrInPlace ();
qz = F2elm.mult (c4, t0);
qx = F2elm.mult (t1, qz);
t1.f2SubInPlace (t0);
t0 = F2elm.mult (aPlus2c, t1);
qz.f2AddInPlace (t0);
qz.f2MultInPlace (t1);
return new F2Point (qx, qz);
}
public F2Point xDble (F2Point p, int e) {
// Computes [2^e](px:pz) via e repeated doublings
F2Point q = p;
int i;
for (i = 0; i < e; i++)
q = xDbl (q);
return q;
}
public F2Point xTpl (F2Point p) {
// Given point p compute 3*p using the point tripling algorithm in "A Faster Software
// Implementation of the Supersingular Isogeny Diffie-Hellman Key Exchange Protocol" by
// Faz-Hernandez, Lopez, Ochoa-Jimenez, Rodriguez-Henriquez
F2elm t0, t1, t2, t3, t4, t5, t6, px, pz, qx, qz;
px = p.getX();
pz = p.getZ();
t0 = F2elm.sub (px, pz);
t2 = F2elm.sqr (t0);
t1 = F2elm.add (px, pz);
t3 = F2elm.sqr (t1);
t4 = F2elm.leftShift (px, 1);
t0 = F2elm.leftShift (pz, 1);
t1 = F2elm.sqr (t4);
t1.f2SubInPlace (t3);
t1.f2SubInPlace (t2);
t5 = F2elm.mult (t3, aPlus2c);
t3.f2MultInPlace (t5);
t6 = F2elm.mult (t2, aMinus2c);
t2.f2MultInPlace (t6);
t3 = F2elm.sub (t2, t3);
t2 = F2elm.sub (t5, t6);
t1.f2MultInPlace (t2);
t2 = F2elm.add (t1, t3);
t2.f2SqrInPlace ();
qx = F2elm.mult (t4, t2);
t1 = F2elm.sub (t3, t1);
t1.f2SqrInPlace ();
qz = F2elm.mult (t0, t1);
return new F2Point (qx, qz);
}
public F2Point xTple (F2Point p, int e) {
// Computes [3^e](px:pz) via e repeated triplings
int i;
F2Point q = p;
for (i = 0; i < e; i++)
q = xTpl (q);
return q;
}
public F2Point[] xDblAdd (F2Point p, F2Point q, F2elm xpq) {
// Simultaneous double and differential addition.
// Outputs: Array of points = [2*p, p+q]
F2elm t0, t1, t2, qx, qz, px, pz;
F2Point pq[];
px = new F2elm (p.getX());
pz = new F2elm (p.getZ());
qx = new F2elm (q.getX());
qz = new F2elm (q.getZ());
t0 = F2elm.add (px, pz);
t1 = F2elm.sub (px, pz);
px = F2elm.sqr (t0);
t2 = F2elm.sub (qx, qz);
qx.f2AddInPlace (qz);
t0.f2MultInPlace (t2);
pz = F2elm.sqr (t1);
t1.f2MultInPlace (qx);
t2 = F2elm.sub (px, pz);
px.f2MultInPlace (pz);
qx = F2elm.mult (t2, a24);
qz = F2elm.sub (t0, t1);
pz.f2AddInPlace (qx);
qx = F2elm.add (t0, t1);
pz.f2MultInPlace (t2);
qz.f2SqrInPlace ();
qx.f2SqrInPlace ();
qz.f2MultInPlace (xpq);
pq = new F2Point[2];
pq[0] = new F2Point (px, pz);
pq[1] = new F2Point (qx, qz);
return pq;
}
public F2Point ladder3pt (F2elm xp, F2elm xq, F2elm xpq, BigInteger m, int obits) {
// Computes P + m[Q] via x-only arithmetic.
F2Point rs[], r;
int i;
BigInteger swap, bit, prevbit = BigInteger.ZERO;
F2elm xval;
rs = new F2Point[2];
rs[0] = new F2Point (xq, F2elm.ONE);
rs[1] = new F2Point (xpq, F2elm.ONE);
r = new F2Point (xp, F2elm.ONE);
for (i = 0; i < obits; i++) {
bit = m.testBit(i) ? BigInteger.ONE : BigInteger.ZERO;
swap = bit.xor(prevbit);
prevbit = bit;
r = rs[1].swapPoints (r, swap);
rs = xDblAdd(rs[0], rs[1], r.getX());
rs[1].setX (F2elm.mult (rs[1].getX(), r.getZ()));
}
return r;
}
public F2elm jInv () {
// Computes the j-invariant of a Montgomery curve
F2elm t0, t1, jinv;
jinv = F2elm.sqr (a);
t1 = F2elm.sqr (c);
t0 = F2elm.leftShift (t1, 1);
t0 = F2elm.sub (jinv, t0);
t0.f2SubInPlace (t1);
jinv = F2elm.sub (t0, t1);
t1.f2SqrInPlace ();
jinv.f2MultInPlace (t1);
t0.f2LeftShiftInPlace (2);
t1 = F2elm.sqr (t0);
t0.f2MultInPlace (t1);
t0 = F2elm.leftShift (t0, 2);
jinv.f2InverseInPlace ();
jinv.f2MultInPlace (t0);
return jinv;
}
public String toString() {
F2elm adivc;
adivc = F2elm.inverse (c);
adivc.f2MultInPlace (a);
return "y^2 = x^3 + " + adivc + " x^2 + " + c + " x";
}
}