6 * Arithmetic on the twisted Edwards curve -x^2 + y^2 = 1 + dx^2y^2
7 * with d = -(121665/121666) = 37095705934669439343138083508754565189542113879843219016388785533085940283555
8 * Base point: (15112221349535400772501151409588531511454012693041857206046113283949847762202,46316835694926478169428394003475163141307993866256225615783033603165251855960);
26 #define ge25519_p3 ge25519
28 /* Windowsize for fixed-window scalar multiplication */
29 #define WINDOWSIZE 2 /* Should be 1,2, or 4 */
30 #define WINDOWMASK ((1<<WINDOWSIZE)-1)
32 /* packed parameter d in the Edwards curve equation */
33 static const unsigned char ecd[32] = {0xA3, 0x78, 0x59, 0x13, 0xCA, 0x4D, 0xEB, 0x75, 0xAB, 0xD8, 0x41, 0x41, 0x4D, 0x0A, 0x70, 0x00,
34 0x98, 0xE8, 0x79, 0x77, 0x79, 0x40, 0xC7, 0x8C, 0x73, 0xFE, 0x6F, 0x2B, 0xEE, 0x6C, 0x03, 0x52};
36 /* Packed coordinates of the base point */
37 static const unsigned char ge25519_base_x[32] = {0x1A, 0xD5, 0x25, 0x8F, 0x60, 0x2D, 0x56, 0xC9, 0xB2, 0xA7, 0x25, 0x95, 0x60, 0xC7, 0x2C, 0x69,
38 0x5C, 0xDC, 0xD6, 0xFD, 0x31, 0xE2, 0xA4, 0xC0, 0xFE, 0x53, 0x6E, 0xCD, 0xD3, 0x36, 0x69, 0x21};
39 static const unsigned char ge25519_base_y[32] = {0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
40 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66};
41 static const unsigned char ge25519_base_z[32] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
42 static const unsigned char ge25519_base_t[32] = {0xA3, 0xDD, 0xB7, 0xA5, 0xB3, 0x8A, 0xDE, 0x6D, 0xF5, 0x52, 0x51, 0x77, 0x80, 0x9F, 0xF0, 0x20,
43 0x7D, 0xE3, 0xAB, 0x64, 0x8E, 0x4E, 0xEA, 0x66, 0x65, 0x76, 0x8B, 0xD7, 0x0F, 0x5F, 0x87, 0x67};
45 /* Packed coordinates of the neutral element */
46 static const unsigned char ge25519_neutral_x[32] = {0};
47 static const unsigned char ge25519_neutral_y[32] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
48 static const unsigned char ge25519_neutral_z[32] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
49 static const unsigned char ge25519_neutral_t[32] = {0};
51 static void p1p1_to_p2(ge25519_p2 *r, const ge25519_p1p1 *p)
53 fe25519_mul(&r->x, &p->x, &p->t);
54 fe25519_mul(&r->y, &p->y, &p->z);
55 fe25519_mul(&r->z, &p->z, &p->t);
58 static void p1p1_to_p3(ge25519_p3 *r, const ge25519_p1p1 *p)
60 p1p1_to_p2((ge25519_p2 *)r, p);
61 fe25519_mul(&r->t, &p->x, &p->y);
64 /* Constant-time version of: if(b) r = p */
65 static void cmov_p3(ge25519_p3 *r, const ge25519_p3 *p, unsigned char b)
67 fe25519_cmov(&r->x, &p->x, b);
68 fe25519_cmov(&r->y, &p->y, b);
69 fe25519_cmov(&r->z, &p->z, b);
70 fe25519_cmov(&r->t, &p->t, b);
73 /* See http://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html#doubling-dbl-2008-hwcd */
74 static void dbl_p1p1(ge25519_p1p1 *r, const ge25519_p2 *p)
77 fe25519_square(&a, &p->x);
78 fe25519_square(&b, &p->y);
79 fe25519_square(&c, &p->z);
80 fe25519_add(&c, &c, &c);
83 fe25519_add(&r->x, &p->x, &p->y);
84 fe25519_square(&r->x, &r->x);
85 fe25519_sub(&r->x, &r->x, &a);
86 fe25519_sub(&r->x, &r->x, &b);
87 fe25519_add(&r->z, &d, &b);
88 fe25519_sub(&r->t, &r->z, &c);
89 fe25519_sub(&r->y, &d, &b);
92 static void add_p1p1(ge25519_p1p1 *r, const ge25519_p3 *p, const ge25519_p3 *q)
94 fe25519 a, b, c, d, t, fd;
95 fe25519_unpack(&fd, ecd);
97 fe25519_sub(&a, &p->y, &p->x); // A = (Y1-X1)*(Y2-X2)
98 fe25519_sub(&t, &q->y, &q->x);
99 fe25519_mul(&a, &a, &t);
100 fe25519_add(&b, &p->x, &p->y); // B = (Y1+X1)*(Y2+X2)
101 fe25519_add(&t, &q->x, &q->y);
102 fe25519_mul(&b, &b, &t);
103 fe25519_mul(&c, &p->t, &q->t); //C = T1*k*T2
104 fe25519_mul(&c, &c, &fd);
105 fe25519_add(&c, &c, &c); //XXX: Can save this addition by precomputing 2*ecd
106 fe25519_mul(&d, &p->z, &q->z); //D = Z1*2*Z2
107 fe25519_add(&d, &d, &d);
108 fe25519_sub(&r->x, &b, &a); // E = B-A
109 fe25519_sub(&r->t, &d, &c); // F = D-C
110 fe25519_add(&r->z, &d, &c); // G = D+C
111 fe25519_add(&r->y, &b, &a); // H = B+A
114 /* ********************************************************************
116 ******************************************************************** */
118 /* return 0 on success, -1 otherwise */
119 int ge25519_unpack_vartime(ge25519_p3 *r, const unsigned char p[32])
125 fe25519_setone(&r->z);
126 fe25519_unpack(&fd, ecd);
128 fe25519_unpack(&r->y, p);
129 fe25519_square(&r->x, &r->y);
130 fe25519_mul(&t, &r->x, &fd);
131 fe25519_sub(&r->x, &r->x, &r->z);
132 fe25519_add(&t, &r->z, &t);
133 fe25519_invert(&t, &t);
134 fe25519_mul(&r->x, &r->x, &t);
135 ret = fe25519_sqrt_vartime(&r->x, &r->x, par);
136 fe25519_mul(&r->t, &r->x, &r->y);
140 void ge25519_pack(unsigned char r[32], const ge25519_p3 *p)
143 fe25519_invert(&zi, &p->z);
144 fe25519_mul(&tx, &p->x, &zi);
145 fe25519_mul(&ty, &p->y, &zi);
146 fe25519_pack(r, &ty);
147 r[31] ^= fe25519_getparity(&tx) << 7;
150 void ge25519_add(ge25519_p3 *r, const ge25519_p3 *p, const ge25519_p3 *q)
153 add_p1p1(&grp1p1, p, q);
154 p1p1_to_p3(r, &grp1p1);
157 void ge25519_double(ge25519_p3 *r, const ge25519_p3 *p)
160 dbl_p1p1(&grp1p1, (const ge25519_p2 *)p);
161 p1p1_to_p3(r, &grp1p1);
164 void ge25519_scalarmult(ge25519_p3 *r, const ge25519_p3 *p, const sc25519 *s)
168 ge25519_p3 pre[(1 << WINDOWSIZE)];
172 unsigned char sb[32];
174 fe25519_unpack(&g.x, ge25519_neutral_x);
175 fe25519_unpack(&g.y, ge25519_neutral_y);
176 fe25519_unpack(&g.z, ge25519_neutral_z);
177 fe25519_unpack(&g.t, ge25519_neutral_t);
179 sc25519_to32bytes(sb, s);
184 for(i=2;i<(1<<WINDOWSIZE);i+=2)
186 dbl_p1p1(&tp1p1, (ge25519_p2 *)(pre+i/2));
187 p1p1_to_p3(pre+i, &tp1p1);
188 add_p1p1(&tp1p1, pre+i, pre+1);
189 p1p1_to_p3(pre+i+1, &tp1p1);
192 // Fixed-window scalar multiplication
195 for(j=8-WINDOWSIZE;j>=0;j-=WINDOWSIZE)
197 for(k=0;k<WINDOWSIZE-1;k++)
199 dbl_p1p1(&tp1p1, (ge25519_p2 *)&g);
200 p1p1_to_p2((ge25519_p2 *)&g, &tp1p1);
202 dbl_p1p1(&tp1p1, (ge25519_p2 *)&g);
203 p1p1_to_p3(&g, &tp1p1);
204 // Cache-timing resistant loading of precomputed value:
205 w = (sb[i-1]>>j) & WINDOWMASK;
207 for(k=1;k<(1<<WINDOWSIZE);k++)
208 cmov_p3(&t, &pre[k], k==w);
210 add_p1p1(&tp1p1, &g, &t);
211 if(j != 0) p1p1_to_p2((ge25519_p2 *)&g, &tp1p1);
212 else p1p1_to_p3(&g, &tp1p1); /* convert to p3 representation at the end */
221 void ge25519_scalarmult_base(ge25519_p3 *r, const sc25519 *s)
223 /* XXX: Better algorithm for known-base-point scalar multiplication */
225 fe25519_unpack(&t.x, ge25519_base_x);
226 fe25519_unpack(&t.y, ge25519_base_y);
227 fe25519_unpack(&t.z, ge25519_base_z);
228 fe25519_unpack(&t.t, ge25519_base_t);
229 ge25519_scalarmult(r, &t, s);