-
Notifications
You must be signed in to change notification settings - Fork 0
/
edwards.c
498 lines (454 loc) · 10.5 KB
/
edwards.c
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Edwards curve support
// Use python scripts to generate code for ED25519 or ED448, or your own curve
// Ax*2+y^2=1+B*x^2*y^2
// Assumes A constant is -1 or +1
//
// Mike Scott 16th July 2024
// TII
//
// code for 32/64-bit processor for ED25519 curve can be generated by
//
// python curve.py 32/64 ED25519
//
// code for 32/64-bit processor for ED448 curve can be generated by
//
// python curve.py 32/64 ED448
// make sure decoration and generic are both set to False in monty.py or pseudo.py
/*** Insert automatically generated code for modulus field.c here ***/
@field@
/*** End of automatically generated code ***/
#include "curve.h"
#define BYTES Nbytes
#define LIMBS Nlimbs
#define TOPBIT (8*sizeof(int)-1)
/*** Insert automatically generated curve definition curve.c here ***/
@curve@
/*** End of automatically generated code ***/
// return 1 if b==c, no branching
static int teq(int b, int c)
{
int x = b ^ c;
x -= 1; // if x=0, x now -1
return ((x >> TOPBIT) & 1);
}
// copy point
void ecncpy(point *Q,point *P)
{
modcpy(Q->x,P->x);
modcpy(Q->y,P->y);
modcpy(Q->z,P->z);
}
// negate P
void ecnneg(point *P)
{
modneg(P->x,P->x);
}
// add Q to P
// standard projective method from EFD - https://www.hyperelliptic.org/EFD/
void ecnadd(point *Q,point *P)
{
spint A[Nlimbs],B[Nlimbs],C[Nlimbs],D[Nlimbs],E[Nlimbs],F[Nlimbs],G[Nlimbs];
modmul(Q->z,P->z,A);
modsqr(A,B);
modmul(Q->x,P->x,C);
modmul(Q->y,P->y,D);
modmul(C,D,E);
#ifdef CONSTANT_B
#if CONSTANT_B>0
modmli(E,CONSTANT_B,E);
modsub(B,E,F);
modadd(B,E,G);
#else
modmli(E,-CONSTANT_B,E);
modadd(B,E,F);
modsub(B,E,G);
#endif
#else
modmul(E,constant_b,E);
modsub(B,E,F);
modadd(B,E,G);
#endif
modadd(P->x,P->y,B);
modadd(Q->x,Q->y,E);
modmul(B,E,P->x);
modsub(P->x,C,P->x);
modsub(P->x,D,P->x);
modmul(P->x,F,P->x);
modmul(P->x,A,P->x);
#if CONSTANT_A==-1
modadd(D,C,P->y);
#else
modsub(D,C,P->y);
#endif
modmul(P->y,A,P->y);
modmul(P->y,G,P->y);
modmul(F,G,P->z);
}
// subtract Q from P
void ecnsub(point *Q,point *P)
{
point W;
ecncpy(Q,&W); ecnneg(&W);
ecnadd(&W,P);
}
// double P
// standard projective method from EFD - https://www.hyperelliptic.org/EFD/
void ecndbl(point *P)
{
spint B[Nlimbs],C[Nlimbs],D[Nlimbs],E[Nlimbs],F[Nlimbs],H[Nlimbs],J[Nlimbs];
modadd(P->x,P->y,B);
modsqr(B,B);
modsqr(P->x,C);
modsqr(P->y,D);
modsqr(P->z,H);
modadd(H,H,H);
#if CONSTANT_A==-1
modneg(C,E);
#else
modcpy(C,E);
#endif
modadd(E,D,F);
modsub(F,H,J);
modsub(B,C,P->x);
modsub(P->x,D,P->x);
modmul(P->x,J,P->x);
modsub(E,D,P->y);
modmul(P->y,F,P->y);
modmul(F,J,P->z);
}
/*
B = (X1+Y1)^2
C = X1^2
D = Y1^2
H = 2Z1^2
E = C
F = E+D F=C+D
J = F-H J=F-H
X3 = (B-C-D)*J X3=(B-F)*J
Y3 = F*(E-D) Y3=(C-D)*F
Z3 = F*J Z3=F*J
E = -C
F = E+D F=-C+D F=D-C F=D-C
J = F-H J=F-H J=-F-H J=F+H
X3 = (B-C-D)*J X3=(B-C-D)*J X3=(B-C-D)*J X3=(C+D-B)*J
Y3 = F*(E-D) Y3=F*(-C-D) Y3=F*(C+D) Y3=F*(C+D)
Z3 = F*J Z3=F*J Z3=-F*J Z3=F*J
*/
// set to infinity
void ecninf(point *P)
{
modzer(P->x);
modone(P->y);
modone(P->z);
}
// test for infinity
int ecnisinf(point *P)
{
return (modis0(P->x) && modcmp(P->y,P->z));
}
// set to affine
void ecnaffine(point *P)
{
spint I[Nlimbs];
if (modis0(P->z)) {
ecninf(P);
return;
}
modinv(P->z,NULL,I);
modone(P->z);
modmul(P->x,I,P->x);
modmul(P->y,I,P->y);
}
// move Q to P if d=1
void ecncmv(int d,point *Q,point *P)
{
modcmv(d,Q->x,P->x);
modcmv(d,Q->y,P->y);
modcmv(d,Q->z,P->z);
}
// return 1 if equal, else 0
int ecncmp(point *P,point *Q)
{
spint a[Nlimbs],b[Nlimbs];
modmul(P->x,Q->z,a);
modmul(Q->x,P->z,b);
if (!modcmp(a,b)) return 0;
modmul(P->y,Q->z,a);
modmul(Q->y,P->z,b);
if (!modcmp(a,b)) return 0;
return 1;
}
// extract (x,y) from point, if y is NULL compress and just return x and sign of y, if x is NULL compress and just return y and sign of x
int ecnget(point *P,char *x,char *y)
{
spint X[Nlimbs],Y[Nlimbs];
ecnaffine(P);
if (x!=NULL)
{
modcpy(P->x,X);
modexp(X,x);
}
if (y!=NULL)
{
modcpy(P->y,Y);
modexp(Y,y);
}
if (y==NULL) return modsign(P->y);
if (x==NULL) return modsign(P->x);
return 0;
}
// general purpose set point function
// sets P=O if point not on curve
// if x and y are not NULL tries to set (x,y)
// if y==NULL tries to set from x and sign s of y (decompression)
// if x==NULL tries to set from y and sign s of x
static void setxy(int s,const spint *x,const spint *y,point *P)
{
spint X[Nlimbs],Y[Nlimbs],O[Nlimbs],U[Nlimbs],V[Nlimbs],H[Nlimbs];
modone(O);
if (x!=NULL && y!=NULL)
{
modsqr(x,X);
modsqr(y,Y);
#if CONSTANT_A==-1
modsub(Y,X,U);
#else
modadd(Y,X,U); //lhs
#endif
modmul(X,Y,V); //rhs
#ifdef CONSTANT_B
#if CONSTANT_B>0
modmli(V,CONSTANT_B,V);
modadd(O,V,V); // V=1+dx^2
#else
modmli(V,-CONSTANT_B,V);
modsub(O,V,V); // V=1-dx^2
#endif
#else
modmul(V,constant_b,V);
modadd(O,V,V);
#endif
modmul(U,O,U); modmul(V,O,V);
if (modcmp(U,V)) {
modcpy(x,P->x);
modcpy(y,P->y);
modone(P->z);
} else {
ecninf(P);
}
return;
}
if (y==NULL)
{
modsqr(x,X);
#if CONSTANT_A==-1 // U=1-ax^2
modadd(O,X,U);
#else
modsub(O,X,U);
#endif
modcpy(X,V); // V=x^2
} else {
modsqr(y,Y);
modsub(O,Y,U); // U=1-y^2
#if CONSTANT_A==-1
modneg(O,O); // O=-1
#endif
modcpy(Y,V); // V=y^2
}
#ifdef CONSTANT_B
#if CONSTANT_B>0
modmli(V,CONSTANT_B,V);
modsub(O,V,V); // V=1-dV^2
#else
modmli(V,-CONSTANT_B,V);
modadd(O,V,V); // V=1+dV^2
#endif
#else
modmul(V,constant_b,V);
modsub(O,V,V); // V=1-dV^2
#endif
modsqr(U,O); // O=U^2
modmul(U,O,U); // U=U^3
modmul(U,V,U); // U=U^3*V
modpro(U,H);
if (!modqr(H,U))
{ // point not on curve
ecninf(P);
return;
}
modsqrt(U,H,V); // V=sqrt
modinv(U,H,U); // U=inv
modmul(U,V,U);
modmul(U,O,U);
int d=(modsign(U)-s)&1;
modneg(U,V);
modcmv(d,V,U);
if (y==NULL)
{
modcpy(U,P->y);
modcpy(x,P->x);
} else {
modcpy(U,P->x);
modcpy(y,P->y);
}
modone(P->z);
}
// multiply point by small curve cofactor (here assumed to be 4 or 8)
void ecncof(point *P)
{
int i;
for (i=0;i<COF;i++)
ecndbl(P);
}
// Is (x,y) of the right order? Must be checked by calling program!
// api visible version, x and y are big endian byte arrays
void ecnset(int s,const char *x,const char *y,point *P)
{
spint X[Nlimbs],Y[Nlimbs];
if (x!=NULL && y!=NULL)
{
modimp(x,X);
modimp(y,Y);
setxy(s,X,Y,P);
return;
}
if (x!=NULL)
{
modimp(x,X);
setxy(s,X,NULL,P);
}
if (y!=NULL)
{
modimp(y,Y);
setxy(s,NULL,Y,P);
}
}
// set generator
void ecngen(point *P)
{
#ifdef CONSTANT_X
spint X[Nlimbs];
modint(CONSTANT_X,X);
setxy(0,X,NULL,P);
#else
setxy(0,constant_x,constant_y,P);
#endif
}
// select point from precomputed array in constant time
static void select(int b,point W[],point *P)
{
point MP;
int m = b >> TOPBIT;
int babs = (b ^ m) - m;
ecncmv(teq(babs, 0),&W[0],P); // conditional move
ecncmv(teq(babs, 1),&W[1],P);
ecncmv(teq(babs, 2),&W[2],P);
ecncmv(teq(babs, 3),&W[3],P);
ecncmv(teq(babs, 4),&W[4],P);
ecncmv(teq(babs, 5),&W[5],P);
ecncmv(teq(babs, 6),&W[6],P);
ecncmv(teq(babs, 7),&W[7],P);
ecncmv(teq(babs, 8),&W[8],P);
ecncpy(P,&MP);
ecnneg(&MP); // minus P
ecncmv((int)(m & 1),&MP,P);
}
// convert to double naf form
static void dnaf(const char *e,const char *f, char *w)
{
int i,j,t;
unsigned char ce=0;
unsigned char cf=0;
unsigned char m,n,p,q;
for (i=0;i<Nbytes;i++)
{
m=n=e[Nbytes-i-1];
t=3*(int)n+ce;
ce=(unsigned char)(t>>8);
n=(unsigned char)(t&0xff);
p=q=f[Nbytes-i-1];
t=3*(int)q+cf;
cf=(unsigned char)(t>>8);
q=(unsigned char)(t&0xff);
for (j=0;j<8;j++)
{
w[8*i+j]=(n&1)-(m&1)+3*((q&1)-(p&1));
n>>=1; m>>=1; p>>=1; q>>=1;
}
}
for (j=0;j<8;j++)
{
w[8*Nbytes+j]=(ce&1)+3*(cf&1);
ce>>=1; cf>>=1;
}
}
// multiply point by scalar
// constant time
void ecnmul(const char *e,point *P)
{
int i,j;
point Q,W[9];
signed char w[2*Nbytes+1];
ecninf(&W[0]); // O
ecncpy(P,&W[1]); // P
ecncpy(P,&W[2]); ecndbl(&W[2]); // 2P
ecncpy(&W[2],&W[3]); ecnadd(P,&W[3]); // 3P
ecncpy(&W[2],&W[4]); ecndbl(&W[4]); // 4P
ecncpy(&W[4],&W[5]); ecnadd(P,&W[5]); // 5P
ecncpy(&W[3],&W[6]); ecndbl(&W[6]); // 6P
ecncpy(&W[6],&W[7]); ecnadd(P,&W[7]); // 7P
ecncpy(&W[4],&W[8]); ecndbl(&W[8]); // 8P
// convert exponent to signed digit
for (i=j=0;i<Nbytes;i++,j+=2)
{
char c=e[Nbytes-i-1];
w[j]=c&0xf;
w[j+1]=(c>>4)&0xf;
}
w[2*Nbytes]=0;
for (j=0;j<2*Nbytes;j++)
{
int t=7-w[j];
int m=(t>>4)&1;
w[j]-=(m<<4);
w[j+1]+=m;
}
// printf("w= ");
// for (i=0;i<2*Nbytes+1;i++) printf(" %d",(int)w[i]);
// printf("\n");
select(w[2*Nbytes],W,P);
for (i = 2*Nbytes - 1; i >= 0; i--)
{
select(w[i],W,&Q);
ecndbl(P);
ecndbl(P);
ecndbl(P);
ecndbl(P);
ecnadd(&Q,P);
}
}
// double point multiplication R=eP+fQ
// not constant time
void ecnmul2(const char *e,point *P,const char *f,point *Q,point *R)
{
int i,j;
point T,W[5];
signed char w[8*Nbytes+8];
ecninf(&W[0]); // O
ecncpy(P,&W[1]); // P
ecncpy(Q,&W[3]); // Q
ecncpy(Q,&W[2]); ecnsub(P,&W[2]); // Q-P
ecncpy(Q,&W[4]); ecnadd(P,&W[4]); // Q+P
dnaf(e,f,w);
i=8*Nbytes+7;
while (w[i]==0) i--; // ignore leading zeros
ecninf(R);
while (i>=1)
{
ecndbl(R);
j=w[i];
if (j>0) ecnadd(&W[j],R);
if (j<0) ecnsub(&W[-j],R);
i--;
}
}