Crypto++  8.0
Free C++ class library of cryptographic schemes
donna_64.cpp
1 // donna_64.cpp - written and placed in public domain by Jeffrey Walton
2 // Crypto++ specific implementation wrapped around Andrew
3 // Moon's public domain curve25519-donna and ed25519-donna,
4 // https://github.com/floodyberry/curve25519-donna and
5 // https://github.com/floodyberry/ed25519-donna.
6 
7 // The curve25519 and ed25519 source files multiplex different repos and
8 // architectures using namespaces. The repos are Andrew Moon's
9 // curve25519-donna and ed25519-donna. The architectures are 32-bit, 64-bit
10 // and SSE. For example, 32-bit x25519 uses symbols from Donna::X25519 and
11 // Donna::Arch32.
12 
13 // A fair amount of duplication happens below, but we could not directly
14 // use curve25519 for both x25519 and ed25519. A close examination reveals
15 // slight differences in the implementation. For example, look at the
16 // two curve25519_sub functions.
17 
18 // If needed, see Moon's commit "Go back to ignoring 256th bit [sic]",
19 // https://github.com/floodyberry/curve25519-donna/commit/57a683d18721a658
20 
21 #include "pch.h"
22 
23 #include "config.h"
24 #include "donna.h"
25 #include "secblock.h"
26 #include "sha.h"
27 #include "misc.h"
28 #include "cpu.h"
29 
30 // Squash MS LNK4221 and libtool warnings
31 extern const char DONNA64_FNAME[] = __FILE__;
32 
33 #if defined(CRYPTOPP_CURVE25519_64BIT)
34 
35 #include "donna_64.h"
36 
37 ANONYMOUS_NAMESPACE_BEGIN
38 
39 using CryptoPP::byte;
40 using CryptoPP::word64;
41 using CryptoPP::GetWord;
42 using CryptoPP::PutWord;
44 
45 inline word64 U8TO64_LE(const byte* p)
46 {
47  return GetWord<word64>(false, LITTLE_ENDIAN_ORDER, p);
48 }
49 
50 inline void U64TO8_LE(byte* p, word64 w)
51 {
52  PutWord(false, LITTLE_ENDIAN_ORDER, p, w);
53 }
54 
55 ANONYMOUS_NAMESPACE_END
56 
57 NAMESPACE_BEGIN(CryptoPP)
58 NAMESPACE_BEGIN(Donna)
59 NAMESPACE_BEGIN(X25519)
60 ANONYMOUS_NAMESPACE_BEGIN
61 
62 using CryptoPP::byte;
63 using CryptoPP::word32;
64 using CryptoPP::sword32;
65 using CryptoPP::word64;
66 using CryptoPP::sword64;
67 
68 using CryptoPP::GetBlock;
70 
71 // Bring in all the symbols from the 64-bit header
72 using namespace CryptoPP::Donna::Arch64;
73 
74 /* out = in */
75 inline void
76 curve25519_copy(bignum25519 out, const bignum25519 in) {
77  out[0] = in[0]; out[1] = in[1];
78  out[2] = in[2]; out[3] = in[3];
79  out[4] = in[4];
80 }
81 
82 /* out = a + b */
83 inline void
84 curve25519_add(bignum25519 out, const bignum25519 a, const bignum25519 b) {
85  out[0] = a[0] + b[0];
86  out[1] = a[1] + b[1];
87  out[2] = a[2] + b[2];
88  out[3] = a[3] + b[3];
89  out[4] = a[4] + b[4];
90 }
91 
92 /* out = a - b */
93 inline void
94 curve25519_sub(bignum25519 out, const bignum25519 a, const bignum25519 b) {
95  out[0] = a[0] + two54m152 - b[0];
96  out[1] = a[1] + two54m8 - b[1];
97  out[2] = a[2] + two54m8 - b[2];
98  out[3] = a[3] + two54m8 - b[3];
99  out[4] = a[4] + two54m8 - b[4];
100 }
101 
102 /* out = (in * scalar) */
103 inline void
104 curve25519_scalar_product(bignum25519 out, const bignum25519 in, const word64 scalar) {
105  word128 a;
106  word64 c;
107 
108 #if defined(CRYPTOPP_WORD128_AVAILABLE)
109  a = ((word128) in[0]) * scalar; out[0] = (word64)a & reduce_mask_51; c = (word64)(a >> 51);
110  a = ((word128) in[1]) * scalar + c; out[1] = (word64)a & reduce_mask_51; c = (word64)(a >> 51);
111  a = ((word128) in[2]) * scalar + c; out[2] = (word64)a & reduce_mask_51; c = (word64)(a >> 51);
112  a = ((word128) in[3]) * scalar + c; out[3] = (word64)a & reduce_mask_51; c = (word64)(a >> 51);
113  a = ((word128) in[4]) * scalar + c; out[4] = (word64)a & reduce_mask_51; c = (word64)(a >> 51);
114  out[0] += c * 19;
115 #else
116  mul64x64_128(a, in[0], scalar) out[0] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
117  mul64x64_128(a, in[1], scalar) add128_64(a, c) out[1] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
118  mul64x64_128(a, in[2], scalar) add128_64(a, c) out[2] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
119  mul64x64_128(a, in[3], scalar) add128_64(a, c) out[3] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
120  mul64x64_128(a, in[4], scalar) add128_64(a, c) out[4] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
121  out[0] += c * 19;
122 #endif
123 }
124 
125 /* out = a * b */
126 inline void
127 curve25519_mul(bignum25519 out, const bignum25519 a, const bignum25519 b) {
128 #if !defined(CRYPTOPP_WORD128_AVAILABLE)
129  word128 mul;
130 #endif
131  word128 t[5];
132  word64 r0,r1,r2,r3,r4,s0,s1,s2,s3,s4,c;
133 
134  r0 = b[0]; r1 = b[1]; r2 = b[2]; r3 = b[3]; r4 = b[4];
135  s0 = a[0]; s1 = a[1]; s2 = a[2]; s3 = a[3]; s4 = a[4];
136 
137 #if defined(CRYPTOPP_WORD128_AVAILABLE)
138  t[0] = ((word128) r0) * s0;
139  t[1] = ((word128) r0) * s1 + ((word128) r1) * s0;
140  t[2] = ((word128) r0) * s2 + ((word128) r2) * s0 + ((word128) r1) * s1;
141  t[3] = ((word128) r0) * s3 + ((word128) r3) * s0 + ((word128) r1) * s2 + ((word128) r2) * s1;
142  t[4] = ((word128) r0) * s4 + ((word128) r4) * s0 + ((word128) r3) * s1 + ((word128) r1) * s3 + ((word128) r2) * s2;
143 #else
144  mul64x64_128(t[0], r0, s0)
145  mul64x64_128(t[1], r0, s1) mul64x64_128(mul, r1, s0) add128(t[1], mul)
146  mul64x64_128(t[2], r0, s2) mul64x64_128(mul, r2, s0) add128(t[2], mul) mul64x64_128(mul, r1, s1) add128(t[2], mul)
147  mul64x64_128(t[3], r0, s3) mul64x64_128(mul, r3, s0) add128(t[3], mul) mul64x64_128(mul, r1, s2) add128(t[3], mul) mul64x64_128(mul, r2, s1) add128(t[3], mul)
148  mul64x64_128(t[4], r0, s4) mul64x64_128(mul, r4, s0) add128(t[4], mul) mul64x64_128(mul, r3, s1) add128(t[4], mul) mul64x64_128(mul, r1, s3) add128(t[4], mul) mul64x64_128(mul, r2, s2) add128(t[4], mul)
149 #endif
150 
151  r1 *= 19; r2 *= 19; r3 *= 19; r4 *= 19;
152 
153 #if defined(CRYPTOPP_WORD128_AVAILABLE)
154  t[0] += ((word128) r4) * s1 + ((word128) r1) * s4 + ((word128) r2) * s3 + ((word128) r3) * s2;
155  t[1] += ((word128) r4) * s2 + ((word128) r2) * s4 + ((word128) r3) * s3;
156  t[2] += ((word128) r4) * s3 + ((word128) r3) * s4;
157  t[3] += ((word128) r4) * s4;
158 #else
159  mul64x64_128(mul, r4, s1) add128(t[0], mul) mul64x64_128(mul, r1, s4) add128(t[0], mul) mul64x64_128(mul, r2, s3) add128(t[0], mul) mul64x64_128(mul, r3, s2) add128(t[0], mul)
160  mul64x64_128(mul, r4, s2) add128(t[1], mul) mul64x64_128(mul, r2, s4) add128(t[1], mul) mul64x64_128(mul, r3, s3) add128(t[1], mul)
161  mul64x64_128(mul, r4, s3) add128(t[2], mul) mul64x64_128(mul, r3, s4) add128(t[2], mul)
162  mul64x64_128(mul, r4, s4) add128(t[3], mul)
163 #endif
164 
165  r0 = lo128(t[0]) & reduce_mask_51; shr128(c, t[0], 51);
166  add128_64(t[1], c) r1 = lo128(t[1]) & reduce_mask_51; shr128(c, t[1], 51);
167  add128_64(t[2], c) r2 = lo128(t[2]) & reduce_mask_51; shr128(c, t[2], 51);
168  add128_64(t[3], c) r3 = lo128(t[3]) & reduce_mask_51; shr128(c, t[3], 51);
169  add128_64(t[4], c) r4 = lo128(t[4]) & reduce_mask_51; shr128(c, t[4], 51);
170  r0 += c * 19; c = r0 >> 51; r0 = r0 & reduce_mask_51;
171  r1 += c;
172 
173  out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
174 }
175 
176 /* out = in^(2 * count) */
177 inline void
178 curve25519_square_times(bignum25519 out, const bignum25519 in, word64 count) {
179 #if !defined(CRYPTOPP_WORD128_AVAILABLE)
180  word128 mul;
181 #endif
182  word128 t[5];
183  word64 r0,r1,r2,r3,r4,c;
184  word64 d0,d1,d2,d4,d419;
185 
186  r0 = in[0]; r1 = in[1]; r2 = in[2]; r3 = in[3]; r4 = in[4];
187 
188  do {
189  d0 = r0 * 2; d1 = r1 * 2;
190  d2 = r2 * 2 * 19;
191  d419 = r4 * 19; d4 = d419 * 2;
192 
193 #if defined(CRYPTOPP_WORD128_AVAILABLE)
194  t[0] = ((word128) r0) * r0 + ((word128) d4) * r1 + (((word128) d2) * (r3 ));
195  t[1] = ((word128) d0) * r1 + ((word128) d4) * r2 + (((word128) r3) * (r3 * 19));
196  t[2] = ((word128) d0) * r2 + ((word128) r1) * r1 + (((word128) d4) * (r3 ));
197  t[3] = ((word128) d0) * r3 + ((word128) d1) * r2 + (((word128) r4) * (d419 ));
198  t[4] = ((word128) d0) * r4 + ((word128) d1) * r3 + (((word128) r2) * (r2 ));
199 #else
200  mul64x64_128(t[0], r0, r0) mul64x64_128(mul, d4, r1) add128(t[0], mul) mul64x64_128(mul, d2, r3) add128(t[0], mul)
201  mul64x64_128(t[1], d0, r1) mul64x64_128(mul, d4, r2) add128(t[1], mul) mul64x64_128(mul, r3, r3 * 19) add128(t[1], mul)
202  mul64x64_128(t[2], d0, r2) mul64x64_128(mul, r1, r1) add128(t[2], mul) mul64x64_128(mul, d4, r3) add128(t[2], mul)
203  mul64x64_128(t[3], d0, r3) mul64x64_128(mul, d1, r2) add128(t[3], mul) mul64x64_128(mul, r4, d419) add128(t[3], mul)
204  mul64x64_128(t[4], d0, r4) mul64x64_128(mul, d1, r3) add128(t[4], mul) mul64x64_128(mul, r2, r2) add128(t[4], mul)
205 #endif
206 
207  r0 = lo128(t[0]) & reduce_mask_51; shr128(c, t[0], 51);
208  add128_64(t[1], c) r1 = lo128(t[1]) & reduce_mask_51; shr128(c, t[1], 51);
209  add128_64(t[2], c) r2 = lo128(t[2]) & reduce_mask_51; shr128(c, t[2], 51);
210  add128_64(t[3], c) r3 = lo128(t[3]) & reduce_mask_51; shr128(c, t[3], 51);
211  add128_64(t[4], c) r4 = lo128(t[4]) & reduce_mask_51; shr128(c, t[4], 51);
212  r0 += c * 19; c = r0 >> 51; r0 = r0 & reduce_mask_51;
213  r1 += c;
214  } while(--count);
215 
216  out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
217 }
218 
219 inline void
220 curve25519_square(bignum25519 out, const bignum25519 in) {
221 #if !defined(CRYPTOPP_WORD128_AVAILABLE)
222  word128 mul;
223 #endif
224  word128 t[5];
225  word64 r0,r1,r2,r3,r4,c;
226  word64 d0,d1,d2,d4,d419;
227 
228  r0 = in[0]; r1 = in[1]; r2 = in[2]; r3 = in[3]; r4 = in[4];
229 
230  d0 = r0 * 2; d1 = r1 * 2;
231  d2 = r2 * 2 * 19;
232  d419 = r4 * 19; d4 = d419 * 2;
233 
234 #if defined(CRYPTOPP_WORD128_AVAILABLE)
235  t[0] = ((word128) r0) * r0 + ((word128) d4) * r1 + (((word128) d2) * (r3 ));
236  t[1] = ((word128) d0) * r1 + ((word128) d4) * r2 + (((word128) r3) * (r3 * 19));
237  t[2] = ((word128) d0) * r2 + ((word128) r1) * r1 + (((word128) d4) * (r3 ));
238  t[3] = ((word128) d0) * r3 + ((word128) d1) * r2 + (((word128) r4) * (d419 ));
239  t[4] = ((word128) d0) * r4 + ((word128) d1) * r3 + (((word128) r2) * (r2 ));
240 #else
241  mul64x64_128(t[0], r0, r0) mul64x64_128(mul, d4, r1) add128(t[0], mul) mul64x64_128(mul, d2, r3) add128(t[0], mul)
242  mul64x64_128(t[1], d0, r1) mul64x64_128(mul, d4, r2) add128(t[1], mul) mul64x64_128(mul, r3, r3 * 19) add128(t[1], mul)
243  mul64x64_128(t[2], d0, r2) mul64x64_128(mul, r1, r1) add128(t[2], mul) mul64x64_128(mul, d4, r3) add128(t[2], mul)
244  mul64x64_128(t[3], d0, r3) mul64x64_128(mul, d1, r2) add128(t[3], mul) mul64x64_128(mul, r4, d419) add128(t[3], mul)
245  mul64x64_128(t[4], d0, r4) mul64x64_128(mul, d1, r3) add128(t[4], mul) mul64x64_128(mul, r2, r2) add128(t[4], mul)
246 #endif
247 
248  r0 = lo128(t[0]) & reduce_mask_51; shr128(c, t[0], 51);
249  add128_64(t[1], c) r1 = lo128(t[1]) & reduce_mask_51; shr128(c, t[1], 51);
250  add128_64(t[2], c) r2 = lo128(t[2]) & reduce_mask_51; shr128(c, t[2], 51);
251  add128_64(t[3], c) r3 = lo128(t[3]) & reduce_mask_51; shr128(c, t[3], 51);
252  add128_64(t[4], c) r4 = lo128(t[4]) & reduce_mask_51; shr128(c, t[4], 51);
253  r0 += c * 19; c = r0 >> 51; r0 = r0 & reduce_mask_51;
254  r1 += c;
255 
256  out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
257 }
258 
259 /* Take a little-endian, 32-byte number and expand it into polynomial form */
260 inline void
261 curve25519_expand(bignum25519 out, const byte *in) {
262  word64 x0,x1,x2,x3;
264  block(x0)(x1)(x2)(x3);
265 
266  out[0] = x0 & reduce_mask_51; x0 = (x0 >> 51) | (x1 << 13);
267  out[1] = x0 & reduce_mask_51; x1 = (x1 >> 38) | (x2 << 26);
268  out[2] = x1 & reduce_mask_51; x2 = (x2 >> 25) | (x3 << 39);
269  out[3] = x2 & reduce_mask_51; x3 = (x3 >> 12);
270  out[4] = x3 & reduce_mask_51; /* ignore the top bit */
271 }
272 
273 /* Take a fully reduced polynomial form number and contract it into a
274  * little-endian, 32-byte array
275  */
276 inline void
277 curve25519_contract(byte *out, const bignum25519 input) {
278  word64 t[5];
279  word64 f, i;
280 
281  t[0] = input[0];
282  t[1] = input[1];
283  t[2] = input[2];
284  t[3] = input[3];
285  t[4] = input[4];
286 
287  #define curve25519_contract_carry() \
288  t[1] += t[0] >> 51; t[0] &= reduce_mask_51; \
289  t[2] += t[1] >> 51; t[1] &= reduce_mask_51; \
290  t[3] += t[2] >> 51; t[2] &= reduce_mask_51; \
291  t[4] += t[3] >> 51; t[3] &= reduce_mask_51;
292 
293  #define curve25519_contract_carry_full() curve25519_contract_carry() \
294  t[0] += 19 * (t[4] >> 51); t[4] &= reduce_mask_51;
295 
296  #define curve25519_contract_carry_final() curve25519_contract_carry() \
297  t[4] &= reduce_mask_51;
298 
299  curve25519_contract_carry_full()
300  curve25519_contract_carry_full()
301 
302  /* now t is between 0 and 2^255-1, properly carried. */
303  /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
304  t[0] += 19;
305  curve25519_contract_carry_full()
306 
307  /* now between 19 and 2^255-1 in both cases, and offset by 19. */
308  t[0] += 0x8000000000000 - 19;
309  t[1] += 0x8000000000000 - 1;
310  t[2] += 0x8000000000000 - 1;
311  t[3] += 0x8000000000000 - 1;
312  t[4] += 0x8000000000000 - 1;
313 
314  /* now between 2^255 and 2^256-20, and offset by 2^255. */
315  curve25519_contract_carry_final()
316 
317  #define write51full(n,shift) \
318  f = ((t[n] >> shift) | (t[n+1] << (51 - shift))); \
319  for (i = 0; i < 8; i++, f >>= 8) *out++ = (byte)f;
320  #define write51(n) write51full(n,13*n)
321 
322  write51(0)
323  write51(1)
324  write51(2)
325  write51(3)
326 
327  #undef curve25519_contract_carry
328  #undef curve25519_contract_carry_full
329  #undef curve25519_contract_carry_final
330  #undef write51full
331  #undef write51
332 }
333 
334 /*
335  * Swap the contents of [qx] and [qpx] iff @swap is non-zero
336  */
337 inline void
338 curve25519_swap_conditional(bignum25519 x, bignum25519 qpx, word64 iswap) {
339  const word64 swap = (word64)(-(sword64)iswap);
340  word64 x0,x1,x2,x3,x4;
341 
342  x0 = swap & (x[0] ^ qpx[0]); x[0] ^= x0; qpx[0] ^= x0;
343  x1 = swap & (x[1] ^ qpx[1]); x[1] ^= x1; qpx[1] ^= x1;
344  x2 = swap & (x[2] ^ qpx[2]); x[2] ^= x2; qpx[2] ^= x2;
345  x3 = swap & (x[3] ^ qpx[3]); x[3] ^= x3; qpx[3] ^= x3;
346  x4 = swap & (x[4] ^ qpx[4]); x[4] ^= x4; qpx[4] ^= x4;
347 }
348 
349 /*
350  * In: b = 2^5 - 2^0
351  * Out: b = 2^250 - 2^0
352  */
353 void
354 curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b) {
355  ALIGN(16) bignum25519 t0,c;
356 
357  /* 2^5 - 2^0 */ /* b */
358  /* 2^10 - 2^5 */ curve25519_square_times(t0, b, 5);
359  /* 2^10 - 2^0 */ curve25519_mul(b, t0, b);
360  /* 2^20 - 2^10 */ curve25519_square_times(t0, b, 10);
361  /* 2^20 - 2^0 */ curve25519_mul(c, t0, b);
362  /* 2^40 - 2^20 */ curve25519_square_times(t0, c, 20);
363  /* 2^40 - 2^0 */ curve25519_mul(t0, t0, c);
364  /* 2^50 - 2^10 */ curve25519_square_times(t0, t0, 10);
365  /* 2^50 - 2^0 */ curve25519_mul(b, t0, b);
366  /* 2^100 - 2^50 */ curve25519_square_times(t0, b, 50);
367  /* 2^100 - 2^0 */ curve25519_mul(c, t0, b);
368  /* 2^200 - 2^100 */ curve25519_square_times(t0, c, 100);
369  /* 2^200 - 2^0 */ curve25519_mul(t0, t0, c);
370  /* 2^250 - 2^50 */ curve25519_square_times(t0, t0, 50);
371  /* 2^250 - 2^0 */ curve25519_mul(b, t0, b);
372 }
373 
374 /*
375  * z^(p - 2) = z(2^255 - 21)
376  */
377 void
378 curve25519_recip(bignum25519 out, const bignum25519 z) {
379  ALIGN(16) bignum25519 a, t0, b;
380 
381  /* 2 */ curve25519_square(a, z); /* a = 2 */
382  /* 8 */ curve25519_square_times(t0, a, 2);
383  /* 9 */ curve25519_mul(b, t0, z); /* b = 9 */
384  /* 11 */ curve25519_mul(a, b, a); /* a = 11 */
385  /* 22 */ curve25519_square(t0, a);
386  /* 2^5 - 2^0 = 31 */ curve25519_mul(b, t0, b);
387  /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
388  /* 2^255 - 2^5 */ curve25519_square_times(b, b, 5);
389  /* 2^255 - 21 */ curve25519_mul(out, b, a);
390 }
391 
392 ANONYMOUS_NAMESPACE_END
393 NAMESPACE_END // X25519
394 NAMESPACE_END // Donna
395 NAMESPACE_END // CryptoPP
396 
397 //******************************* ed25519 *******************************//
398 
399 NAMESPACE_BEGIN(CryptoPP)
400 NAMESPACE_BEGIN(Donna)
401 NAMESPACE_BEGIN(Ed25519)
402 ANONYMOUS_NAMESPACE_BEGIN
403 
404 using CryptoPP::byte;
405 using CryptoPP::word32;
406 using CryptoPP::sword32;
407 using CryptoPP::word64;
408 using CryptoPP::sword64;
409 
410 using CryptoPP::GetBlock;
411 using CryptoPP::LittleEndian;
412 
413 using CryptoPP::SHA512;
414 
415 // Bring in all the symbols from the 64-bit header
416 using namespace CryptoPP::Donna::Arch64;
417 
418 /* out = in */
419 inline void
420 curve25519_copy(bignum25519 out, const bignum25519 in) {
421  out[0] = in[0]; out[1] = in[1];
422  out[2] = in[2]; out[3] = in[3];
423  out[4] = in[4];
424 }
425 
426 /* out = a + b */
427 inline void
428 curve25519_add(bignum25519 out, const bignum25519 a, const bignum25519 b) {
429  out[0] = a[0] + b[0]; out[1] = a[1] + b[1];
430  out[2] = a[2] + b[2]; out[3] = a[3] + b[3];
431  out[4] = a[4] + b[4];
432 }
433 
434 /* out = a + b, where a and/or b are the result of a basic op (add,sub) */
435 inline void
436 curve25519_add_after_basic(bignum25519 out, const bignum25519 a, const bignum25519 b) {
437  out[0] = a[0] + b[0]; out[1] = a[1] + b[1];
438  out[2] = a[2] + b[2]; out[3] = a[3] + b[3];
439  out[4] = a[4] + b[4];
440 }
441 
442 inline void
443 curve25519_add_reduce(bignum25519 out, const bignum25519 a, const bignum25519 b) {
444  word64 c;
445  out[0] = a[0] + b[0] ; c = (out[0] >> 51); out[0] &= reduce_mask_51;
446  out[1] = a[1] + b[1] + c; c = (out[1] >> 51); out[1] &= reduce_mask_51;
447  out[2] = a[2] + b[2] + c; c = (out[2] >> 51); out[2] &= reduce_mask_51;
448  out[3] = a[3] + b[3] + c; c = (out[3] >> 51); out[3] &= reduce_mask_51;
449  out[4] = a[4] + b[4] + c; c = (out[4] >> 51); out[4] &= reduce_mask_51;
450  out[0] += c * 19;
451 }
452 
453 /* out = a - b */
454 inline void
455 curve25519_sub(bignum25519 out, const bignum25519 a, const bignum25519 b) {
456  out[0] = a[0] + twoP0 - b[0];
457  out[1] = a[1] + twoP1234 - b[1];
458  out[2] = a[2] + twoP1234 - b[2];
459  out[3] = a[3] + twoP1234 - b[3];
460  out[4] = a[4] + twoP1234 - b[4];
461 }
462 
463 /* out = a - b, where a and/or b are the result of a basic op (add,sub) */
464 inline void
465 curve25519_sub_after_basic(bignum25519 out, const bignum25519 a, const bignum25519 b) {
466  out[0] = a[0] + fourP0 - b[0];
467  out[1] = a[1] + fourP1234 - b[1];
468  out[2] = a[2] + fourP1234 - b[2];
469  out[3] = a[3] + fourP1234 - b[3];
470  out[4] = a[4] + fourP1234 - b[4];
471 }
472 
473 inline void
474 curve25519_sub_reduce(bignum25519 out, const bignum25519 a, const bignum25519 b) {
475  word64 c;
476  out[0] = a[0] + fourP0 - b[0] ; c = (out[0] >> 51); out[0] &= reduce_mask_51;
477  out[1] = a[1] + fourP1234 - b[1] + c; c = (out[1] >> 51); out[1] &= reduce_mask_51;
478  out[2] = a[2] + fourP1234 - b[2] + c; c = (out[2] >> 51); out[2] &= reduce_mask_51;
479  out[3] = a[3] + fourP1234 - b[3] + c; c = (out[3] >> 51); out[3] &= reduce_mask_51;
480  out[4] = a[4] + fourP1234 - b[4] + c; c = (out[4] >> 51); out[4] &= reduce_mask_51;
481  out[0] += c * 19;
482 }
483 
484 /* out = -a */
485 inline void
486 curve25519_neg(bignum25519 out, const bignum25519 a) {
487  word64 c;
488  out[0] = twoP0 - a[0] ; c = (out[0] >> 51); out[0] &= reduce_mask_51;
489  out[1] = twoP1234 - a[1] + c; c = (out[1] >> 51); out[1] &= reduce_mask_51;
490  out[2] = twoP1234 - a[2] + c; c = (out[2] >> 51); out[2] &= reduce_mask_51;
491  out[3] = twoP1234 - a[3] + c; c = (out[3] >> 51); out[3] &= reduce_mask_51;
492  out[4] = twoP1234 - a[4] + c; c = (out[4] >> 51); out[4] &= reduce_mask_51;
493  out[0] += c * 19;
494 }
495 
496 /* out = a * b */
497 inline void
498 curve25519_mul(bignum25519 out, const bignum25519 in2, const bignum25519 in) {
499 #if !defined(CRYPTOPP_WORD128_AVAILABLE)
500  word128 mul;
501 #endif
502  word128 t[5];
503  word64 r0,r1,r2,r3,r4,s0,s1,s2,s3,s4,c;
504 
505  r0 = in[0]; r1 = in[1];
506  r2 = in[2]; r3 = in[3];
507  r4 = in[4];
508 
509  s0 = in2[0]; s1 = in2[1];
510  s2 = in2[2]; s3 = in2[3];
511  s4 = in2[4];
512 
513 #if defined(CRYPTOPP_WORD128_AVAILABLE)
514  t[0] = ((word128) r0) * s0;
515  t[1] = ((word128) r0) * s1 + ((word128) r1) * s0;
516  t[2] = ((word128) r0) * s2 + ((word128) r2) * s0 + ((word128) r1) * s1;
517  t[3] = ((word128) r0) * s3 + ((word128) r3) * s0 + ((word128) r1) * s2 + ((word128) r2) * s1;
518  t[4] = ((word128) r0) * s4 + ((word128) r4) * s0 + ((word128) r3) * s1 + ((word128) r1) * s3 + ((word128) r2) * s2;
519 #else
520  mul64x64_128(t[0], r0, s0)
521  mul64x64_128(t[1], r0, s1) mul64x64_128(mul, r1, s0) add128(t[1], mul)
522  mul64x64_128(t[2], r0, s2) mul64x64_128(mul, r2, s0) add128(t[2], mul) mul64x64_128(mul, r1, s1) add128(t[2], mul)
523  mul64x64_128(t[3], r0, s3) mul64x64_128(mul, r3, s0) add128(t[3], mul) mul64x64_128(mul, r1, s2) add128(t[3], mul) mul64x64_128(mul, r2, s1) add128(t[3], mul)
524  mul64x64_128(t[4], r0, s4) mul64x64_128(mul, r4, s0) add128(t[4], mul) mul64x64_128(mul, r3, s1) add128(t[4], mul) mul64x64_128(mul, r1, s3) add128(t[4], mul) mul64x64_128(mul, r2, s2) add128(t[4], mul)
525 #endif
526 
527  r1 *= 19; r2 *= 19;
528  r3 *= 19; r4 *= 19;
529 
530 #if defined(CRYPTOPP_WORD128_AVAILABLE)
531  t[0] += ((word128) r4) * s1 + ((word128) r1) * s4 + ((word128) r2) * s3 + ((word128) r3) * s2;
532  t[1] += ((word128) r4) * s2 + ((word128) r2) * s4 + ((word128) r3) * s3;
533  t[2] += ((word128) r4) * s3 + ((word128) r3) * s4;
534  t[3] += ((word128) r4) * s4;
535 #else
536  mul64x64_128(mul, r4, s1) add128(t[0], mul) mul64x64_128(mul, r1, s4) add128(t[0], mul) mul64x64_128(mul, r2, s3) add128(t[0], mul) mul64x64_128(mul, r3, s2) add128(t[0], mul)
537  mul64x64_128(mul, r4, s2) add128(t[1], mul) mul64x64_128(mul, r2, s4) add128(t[1], mul) mul64x64_128(mul, r3, s3) add128(t[1], mul)
538  mul64x64_128(mul, r4, s3) add128(t[2], mul) mul64x64_128(mul, r3, s4) add128(t[2], mul)
539  mul64x64_128(mul, r4, s4) add128(t[3], mul)
540 #endif
541 
542  r0 = lo128(t[0]) & reduce_mask_51; shr128(c, t[0], 51);
543  add128_64(t[1], c) r1 = lo128(t[1]) & reduce_mask_51; shr128(c, t[1], 51);
544  add128_64(t[2], c) r2 = lo128(t[2]) & reduce_mask_51; shr128(c, t[2], 51);
545  add128_64(t[3], c) r3 = lo128(t[3]) & reduce_mask_51; shr128(c, t[3], 51);
546  add128_64(t[4], c) r4 = lo128(t[4]) & reduce_mask_51; shr128(c, t[4], 51);
547  r0 += c * 19; c = r0 >> 51; r0 = r0 & reduce_mask_51;
548  r1 += c;
549 
550  out[0] = r0; out[1] = r1;
551  out[2] = r2; out[3] = r3;
552  out[4] = r4;
553 }
554 
555 void
556 curve25519_mul_noinline(bignum25519 out, const bignum25519 in2, const bignum25519 in) {
557  curve25519_mul(out, in2, in);
558 }
559 
560 /* out = in^(2 * count) */
561 void
562 curve25519_square_times(bignum25519 out, const bignum25519 in, word64 count) {
563 #if !defined(CRYPTOPP_WORD128_AVAILABLE)
564  word128 mul;
565 #endif
566  word128 t[5];
567  word64 r0,r1,r2,r3,r4,c;
568  word64 d0,d1,d2,d4,d419;
569 
570  r0 = in[0]; r1 = in[1];
571  r2 = in[2]; r3 = in[3];
572  r4 = in[4];
573 
574  do {
575  d0 = r0 * 2;
576  d1 = r1 * 2;
577  d2 = r2 * 2 * 19;
578  d419 = r4 * 19;
579  d4 = d419 * 2;
580 
581 #if defined(CRYPTOPP_WORD128_AVAILABLE)
582  t[0] = ((word128) r0) * r0 + ((word128) d4) * r1 + (((word128) d2) * (r3 ));
583  t[1] = ((word128) d0) * r1 + ((word128) d4) * r2 + (((word128) r3) * (r3 * 19));
584  t[2] = ((word128) d0) * r2 + ((word128) r1) * r1 + (((word128) d4) * (r3 ));
585  t[3] = ((word128) d0) * r3 + ((word128) d1) * r2 + (((word128) r4) * (d419 ));
586  t[4] = ((word128) d0) * r4 + ((word128) d1) * r3 + (((word128) r2) * (r2 ));
587 #else
588  mul64x64_128(t[0], r0, r0) mul64x64_128(mul, d4, r1) add128(t[0], mul) mul64x64_128(mul, d2, r3) add128(t[0], mul)
589  mul64x64_128(t[1], d0, r1) mul64x64_128(mul, d4, r2) add128(t[1], mul) mul64x64_128(mul, r3, r3 * 19) add128(t[1], mul)
590  mul64x64_128(t[2], d0, r2) mul64x64_128(mul, r1, r1) add128(t[2], mul) mul64x64_128(mul, d4, r3) add128(t[2], mul)
591  mul64x64_128(t[3], d0, r3) mul64x64_128(mul, d1, r2) add128(t[3], mul) mul64x64_128(mul, r4, d419) add128(t[3], mul)
592  mul64x64_128(t[4], d0, r4) mul64x64_128(mul, d1, r3) add128(t[4], mul) mul64x64_128(mul, r2, r2) add128(t[4], mul)
593 #endif
594 
595  r0 = lo128(t[0]) & reduce_mask_51;
596  r1 = lo128(t[1]) & reduce_mask_51; shl128(c, t[0], 13); r1 += c;
597  r2 = lo128(t[2]) & reduce_mask_51; shl128(c, t[1], 13); r2 += c;
598  r3 = lo128(t[3]) & reduce_mask_51; shl128(c, t[2], 13); r3 += c;
599  r4 = lo128(t[4]) & reduce_mask_51; shl128(c, t[3], 13); r4 += c;
600  shl128(c, t[4], 13); r0 += c * 19;
601  c = r0 >> 51; r0 &= reduce_mask_51;
602  r1 += c ; c = r1 >> 51; r1 &= reduce_mask_51;
603  r2 += c ; c = r2 >> 51; r2 &= reduce_mask_51;
604  r3 += c ; c = r3 >> 51; r3 &= reduce_mask_51;
605  r4 += c ; c = r4 >> 51; r4 &= reduce_mask_51;
606  r0 += c * 19;
607  } while(--count);
608 
609  out[0] = r0; out[1] = r1;
610  out[2] = r2; out[3] = r3;
611  out[4] = r4;
612 }
613 
614 inline void
615 curve25519_square(bignum25519 out, const bignum25519 in) {
616 #if !defined(CRYPTOPP_WORD128_AVAILABLE)
617  word128 mul;
618 #endif
619  word128 t[5];
620  word64 r0,r1,r2,r3,r4,c;
621  word64 d0,d1,d2,d4,d419;
622 
623  r0 = in[0]; r1 = in[1];
624  r2 = in[2]; r3 = in[3];
625  r4 = in[4];
626 
627  d0 = r0 * 2; d1 = r1 * 2;
628  d2 = r2 * 2 * 19;
629  d419 = r4 * 19;
630  d4 = d419 * 2;
631 
632 #if defined(CRYPTOPP_WORD128_AVAILABLE)
633  t[0] = ((word128) r0) * r0 + ((word128) d4) * r1 + (((word128) d2) * (r3 ));
634  t[1] = ((word128) d0) * r1 + ((word128) d4) * r2 + (((word128) r3) * (r3 * 19));
635  t[2] = ((word128) d0) * r2 + ((word128) r1) * r1 + (((word128) d4) * (r3 ));
636  t[3] = ((word128) d0) * r3 + ((word128) d1) * r2 + (((word128) r4) * (d419 ));
637  t[4] = ((word128) d0) * r4 + ((word128) d1) * r3 + (((word128) r2) * (r2 ));
638 #else
639  mul64x64_128(t[0], r0, r0) mul64x64_128(mul, d4, r1) add128(t[0], mul) mul64x64_128(mul, d2, r3) add128(t[0], mul)
640  mul64x64_128(t[1], d0, r1) mul64x64_128(mul, d4, r2) add128(t[1], mul) mul64x64_128(mul, r3, r3 * 19) add128(t[1], mul)
641  mul64x64_128(t[2], d0, r2) mul64x64_128(mul, r1, r1) add128(t[2], mul) mul64x64_128(mul, d4, r3) add128(t[2], mul)
642  mul64x64_128(t[3], d0, r3) mul64x64_128(mul, d1, r2) add128(t[3], mul) mul64x64_128(mul, r4, d419) add128(t[3], mul)
643  mul64x64_128(t[4], d0, r4) mul64x64_128(mul, d1, r3) add128(t[4], mul) mul64x64_128(mul, r2, r2) add128(t[4], mul)
644 #endif
645 
646  r0 = lo128(t[0]) & reduce_mask_51; shr128(c, t[0], 51);
647  add128_64(t[1], c) r1 = lo128(t[1]) & reduce_mask_51; shr128(c, t[1], 51);
648  add128_64(t[2], c) r2 = lo128(t[2]) & reduce_mask_51; shr128(c, t[2], 51);
649  add128_64(t[3], c) r3 = lo128(t[3]) & reduce_mask_51; shr128(c, t[3], 51);
650  add128_64(t[4], c) r4 = lo128(t[4]) & reduce_mask_51; shr128(c, t[4], 51);
651  r0 += c * 19; c = r0 >> 51; r0 = r0 & reduce_mask_51;
652  r1 += c;
653 
654  out[0] = r0; out[1] = r1;
655  out[2] = r2; out[3] = r3;
656  out[4] = r4;
657 }
658 
659 /* Take a little-endian, 32-byte number and expand it into polynomial form */
660 inline void
661 curve25519_expand(bignum25519 out, const byte *in) {
662  word64 x0,x1,x2,x3;
664  block(x0)(x1)(x2)(x3);
665 
666  out[0] = x0 & reduce_mask_51; x0 = (x0 >> 51) | (x1 << 13);
667  out[1] = x0 & reduce_mask_51; x1 = (x1 >> 38) | (x2 << 26);
668  out[2] = x1 & reduce_mask_51; x2 = (x2 >> 25) | (x3 << 39);
669  out[3] = x2 & reduce_mask_51; x3 = (x3 >> 12);
670  out[4] = x3 & reduce_mask_51;
671 }
672 
673 /* Take a fully reduced polynomial form number and contract it into a
674  * little-endian, 32-byte array
675  */
676 inline void
677 curve25519_contract(byte *out, const bignum25519 input) {
678  word64 t[5];
679  word64 f, i;
680 
681  t[0] = input[0];
682  t[1] = input[1];
683  t[2] = input[2];
684  t[3] = input[3];
685  t[4] = input[4];
686 
687  #define curve25519_contract_carry() \
688  t[1] += t[0] >> 51; t[0] &= reduce_mask_51; \
689  t[2] += t[1] >> 51; t[1] &= reduce_mask_51; \
690  t[3] += t[2] >> 51; t[2] &= reduce_mask_51; \
691  t[4] += t[3] >> 51; t[3] &= reduce_mask_51;
692 
693  #define curve25519_contract_carry_full() curve25519_contract_carry() \
694  t[0] += 19 * (t[4] >> 51); t[4] &= reduce_mask_51;
695 
696  #define curve25519_contract_carry_final() curve25519_contract_carry() \
697  t[4] &= reduce_mask_51;
698 
699  curve25519_contract_carry_full()
700  curve25519_contract_carry_full()
701 
702  /* now t is between 0 and 2^255-1, properly carried. */
703  /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
704  t[0] += 19;
705  curve25519_contract_carry_full()
706 
707  /* now between 19 and 2^255-1 in both cases, and offset by 19. */
708  t[0] += (reduce_mask_51 + 1) - 19;
709  t[1] += (reduce_mask_51 + 1) - 1;
710  t[2] += (reduce_mask_51 + 1) - 1;
711  t[3] += (reduce_mask_51 + 1) - 1;
712  t[4] += (reduce_mask_51 + 1) - 1;
713 
714  /* now between 2^255 and 2^256-20, and offset by 2^255. */
715  curve25519_contract_carry_final()
716 
717  #define write51full(n,shift) \
718  f = ((t[n] >> shift) | (t[n+1] << (51 - shift))); \
719  for (i = 0; i < 8; i++, f >>= 8) *out++ = (byte)f;
720  #define write51(n) write51full(n,13*n)
721  write51(0)
722  write51(1)
723  write51(2)
724  write51(3)
725 }
726 
727 #if !defined(ED25519_GCC_64BIT_CHOOSE)
728 
729 /* out = (flag) ? in : out */
730 inline void
731 curve25519_move_conditional_bytes(uint8_t out[96], const uint8_t in[96], word64 flag) {
732  const word64 nb = flag - 1, b = ~nb;
733  const word64 *inq = (const word64 *)in;
734  word64 *outq = (word64 *)out;
735  outq[0] = (outq[0] & nb) | (inq[0] & b);
736  outq[1] = (outq[1] & nb) | (inq[1] & b);
737  outq[2] = (outq[2] & nb) | (inq[2] & b);
738  outq[3] = (outq[3] & nb) | (inq[3] & b);
739  outq[4] = (outq[4] & nb) | (inq[4] & b);
740  outq[5] = (outq[5] & nb) | (inq[5] & b);
741  outq[6] = (outq[6] & nb) | (inq[6] & b);
742  outq[7] = (outq[7] & nb) | (inq[7] & b);
743  outq[8] = (outq[8] & nb) | (inq[8] & b);
744  outq[9] = (outq[9] & nb) | (inq[9] & b);
745  outq[10] = (outq[10] & nb) | (inq[10] & b);
746  outq[11] = (outq[11] & nb) | (inq[11] & b);
747 }
748 
749 /* if (iswap) swap(a, b) */
750 inline void
751 curve25519_swap_conditional(bignum25519 a, bignum25519 b, word64 iswap) {
752  const word64 swap = (word64)(-(int64_t)iswap);
753  word64 x0,x1,x2,x3,x4;
754 
755  x0 = swap & (a[0] ^ b[0]); a[0] ^= x0; b[0] ^= x0;
756  x1 = swap & (a[1] ^ b[1]); a[1] ^= x1; b[1] ^= x1;
757  x2 = swap & (a[2] ^ b[2]); a[2] ^= x2; b[2] ^= x2;
758  x3 = swap & (a[3] ^ b[3]); a[3] ^= x3; b[3] ^= x3;
759  x4 = swap & (a[4] ^ b[4]); a[4] ^= x4; b[4] ^= x4;
760 }
761 
762 #endif /* ED25519_GCC_64BIT_CHOOSE */
763 
764 // ************************************************************************************
765 
766 inline void
767 ed25519_hash(byte *hash, const byte *in, size_t inlen) {
768  SHA512().CalculateDigest(hash, in, inlen);
769 }
770 
771 inline void
772 ed25519_extsk(hash_512bits extsk, const byte sk[32]) {
773  ed25519_hash(extsk, sk, 32);
774  extsk[0] &= 248;
775  extsk[31] &= 127;
776  extsk[31] |= 64;
777 }
778 
779 void
780 ed25519_hram(hash_512bits hram, const byte RS[64], const byte pk[32], const unsigned char *m, size_t mlen) {
781  SHA512 hash;
782  hash.Update(RS, 32);
783  hash.Update(pk, 32);
784  hash.Update(m, mlen);
785  hash.Final(hram);
786 }
787 
788 bignum256modm_element_t
789 lt_modm(bignum256modm_element_t a, bignum256modm_element_t b) {
790  return (a - b) >> 63;
791 }
792 
793 void
794 reduce256_modm(bignum256modm r) {
795  bignum256modm t;
796  bignum256modm_element_t b = 0, pb, mask;
797 
798  /* t = r - m */
799  pb = 0;
800  pb += modm_m[0]; b = lt_modm(r[0], pb); t[0] = (r[0] - pb + (b << 56)); pb = b;
801  pb += modm_m[1]; b = lt_modm(r[1], pb); t[1] = (r[1] - pb + (b << 56)); pb = b;
802  pb += modm_m[2]; b = lt_modm(r[2], pb); t[2] = (r[2] - pb + (b << 56)); pb = b;
803  pb += modm_m[3]; b = lt_modm(r[3], pb); t[3] = (r[3] - pb + (b << 56)); pb = b;
804  pb += modm_m[4]; b = lt_modm(r[4], pb); t[4] = (r[4] - pb + (b << 32));
805 
806  /* keep r if r was smaller than m */
807  mask = b - 1;
808 
809  r[0] ^= mask & (r[0] ^ t[0]);
810  r[1] ^= mask & (r[1] ^ t[1]);
811  r[2] ^= mask & (r[2] ^ t[2]);
812  r[3] ^= mask & (r[3] ^ t[3]);
813  r[4] ^= mask & (r[4] ^ t[4]);
814 }
815 
816 void
817 barrett_reduce256_modm(bignum256modm r, const bignum256modm q1, const bignum256modm r1) {
818  bignum256modm q3, r2;
819  word128 c, mul;
820  bignum256modm_element_t f, b, pb;
821 
822  /* q1 = x >> 248 = 264 bits = 5 56 bit elements
823  q2 = mu * q1
824  q3 = (q2 / 256(32+1)) = q2 / (2^8)^(32+1) = q2 >> 264 */
825  mul64x64_128(c, modm_mu[0], q1[3]) mul64x64_128(mul, modm_mu[3], q1[0]) add128(c, mul) mul64x64_128(mul, modm_mu[1], q1[2]) add128(c, mul) mul64x64_128(mul, modm_mu[2], q1[1]) add128(c, mul) shr128(f, c, 56);
826  mul64x64_128(c, modm_mu[0], q1[4]) add128_64(c, f) mul64x64_128(mul, modm_mu[4], q1[0]) add128(c, mul) mul64x64_128(mul, modm_mu[3], q1[1]) add128(c, mul) mul64x64_128(mul, modm_mu[1], q1[3]) add128(c, mul) mul64x64_128(mul, modm_mu[2], q1[2]) add128(c, mul)
827  f = lo128(c); q3[0] = (f >> 40) & 0xffff; shr128(f, c, 56);
828  mul64x64_128(c, modm_mu[4], q1[1]) add128_64(c, f) mul64x64_128(mul, modm_mu[1], q1[4]) add128(c, mul) mul64x64_128(mul, modm_mu[2], q1[3]) add128(c, mul) mul64x64_128(mul, modm_mu[3], q1[2]) add128(c, mul)
829  f = lo128(c); q3[0] |= (f << 16) & 0xffffffffffffff; q3[1] = (f >> 40) & 0xffff; shr128(f, c, 56);
830  mul64x64_128(c, modm_mu[4], q1[2]) add128_64(c, f) mul64x64_128(mul, modm_mu[2], q1[4]) add128(c, mul) mul64x64_128(mul, modm_mu[3], q1[3]) add128(c, mul)
831  f = lo128(c); q3[1] |= (f << 16) & 0xffffffffffffff; q3[2] = (f >> 40) & 0xffff; shr128(f, c, 56);
832  mul64x64_128(c, modm_mu[4], q1[3]) add128_64(c, f) mul64x64_128(mul, modm_mu[3], q1[4]) add128(c, mul)
833  f = lo128(c); q3[2] |= (f << 16) & 0xffffffffffffff; q3[3] = (f >> 40) & 0xffff; shr128(f, c, 56);
834  mul64x64_128(c, modm_mu[4], q1[4]) add128_64(c, f)
835  f = lo128(c); q3[3] |= (f << 16) & 0xffffffffffffff; q3[4] = (f >> 40) & 0xffff; shr128(f, c, 56);
836  q3[4] |= (f << 16);
837 
838  mul64x64_128(c, modm_m[0], q3[0])
839  r2[0] = lo128(c) & 0xffffffffffffff; shr128(f, c, 56);
840  mul64x64_128(c, modm_m[0], q3[1]) add128_64(c, f) mul64x64_128(mul, modm_m[1], q3[0]) add128(c, mul)
841  r2[1] = lo128(c) & 0xffffffffffffff; shr128(f, c, 56);
842  mul64x64_128(c, modm_m[0], q3[2]) add128_64(c, f) mul64x64_128(mul, modm_m[2], q3[0]) add128(c, mul) mul64x64_128(mul, modm_m[1], q3[1]) add128(c, mul)
843  r2[2] = lo128(c) & 0xffffffffffffff; shr128(f, c, 56);
844  mul64x64_128(c, modm_m[0], q3[3]) add128_64(c, f) mul64x64_128(mul, modm_m[3], q3[0]) add128(c, mul) mul64x64_128(mul, modm_m[1], q3[2]) add128(c, mul) mul64x64_128(mul, modm_m[2], q3[1]) add128(c, mul)
845  r2[3] = lo128(c) & 0xffffffffffffff; shr128(f, c, 56);
846  mul64x64_128(c, modm_m[0], q3[4]) add128_64(c, f) mul64x64_128(mul, modm_m[4], q3[0]) add128(c, mul) mul64x64_128(mul, modm_m[3], q3[1]) add128(c, mul) mul64x64_128(mul, modm_m[1], q3[3]) add128(c, mul) mul64x64_128(mul, modm_m[2], q3[2]) add128(c, mul)
847  r2[4] = lo128(c) & 0x0000ffffffffff;
848 
849  pb = 0;
850  pb += r2[0]; b = lt_modm(r1[0], pb); r[0] = (r1[0] - pb + (b << 56)); pb = b;
851  pb += r2[1]; b = lt_modm(r1[1], pb); r[1] = (r1[1] - pb + (b << 56)); pb = b;
852  pb += r2[2]; b = lt_modm(r1[2], pb); r[2] = (r1[2] - pb + (b << 56)); pb = b;
853  pb += r2[3]; b = lt_modm(r1[3], pb); r[3] = (r1[3] - pb + (b << 56)); pb = b;
854  pb += r2[4]; b = lt_modm(r1[4], pb); r[4] = (r1[4] - pb + (b << 40));
855 
856  reduce256_modm(r);
857  reduce256_modm(r);
858 }
859 
860 
861 void
862 add256_modm(bignum256modm r, const bignum256modm x, const bignum256modm y) {
863  bignum256modm_element_t c;
864 
865  c = x[0] + y[0]; r[0] = c & 0xffffffffffffff; c >>= 56;
866  c += x[1] + y[1]; r[1] = c & 0xffffffffffffff; c >>= 56;
867  c += x[2] + y[2]; r[2] = c & 0xffffffffffffff; c >>= 56;
868  c += x[3] + y[3]; r[3] = c & 0xffffffffffffff; c >>= 56;
869  c += x[4] + y[4]; r[4] = c;
870 
871  reduce256_modm(r);
872 }
873 
874 void
875 mul256_modm(bignum256modm r, const bignum256modm x, const bignum256modm y) {
876  bignum256modm q1, r1;
877  word128 c, mul;
878  bignum256modm_element_t f;
879 
880  mul64x64_128(c, x[0], y[0])
881  f = lo128(c); r1[0] = f & 0xffffffffffffff; shr128(f, c, 56);
882  mul64x64_128(c, x[0], y[1]) add128_64(c, f) mul64x64_128(mul, x[1], y[0]) add128(c, mul)
883  f = lo128(c); r1[1] = f & 0xffffffffffffff; shr128(f, c, 56);
884  mul64x64_128(c, x[0], y[2]) add128_64(c, f) mul64x64_128(mul, x[2], y[0]) add128(c, mul) mul64x64_128(mul, x[1], y[1]) add128(c, mul)
885  f = lo128(c); r1[2] = f & 0xffffffffffffff; shr128(f, c, 56);
886  mul64x64_128(c, x[0], y[3]) add128_64(c, f) mul64x64_128(mul, x[3], y[0]) add128(c, mul) mul64x64_128(mul, x[1], y[2]) add128(c, mul) mul64x64_128(mul, x[2], y[1]) add128(c, mul)
887  f = lo128(c); r1[3] = f & 0xffffffffffffff; shr128(f, c, 56);
888  mul64x64_128(c, x[0], y[4]) add128_64(c, f) mul64x64_128(mul, x[4], y[0]) add128(c, mul) mul64x64_128(mul, x[3], y[1]) add128(c, mul) mul64x64_128(mul, x[1], y[3]) add128(c, mul) mul64x64_128(mul, x[2], y[2]) add128(c, mul)
889  f = lo128(c); r1[4] = f & 0x0000ffffffffff; q1[0] = (f >> 24) & 0xffffffff; shr128(f, c, 56);
890  mul64x64_128(c, x[4], y[1]) add128_64(c, f) mul64x64_128(mul, x[1], y[4]) add128(c, mul) mul64x64_128(mul, x[2], y[3]) add128(c, mul) mul64x64_128(mul, x[3], y[2]) add128(c, mul)
891  f = lo128(c); q1[0] |= (f << 32) & 0xffffffffffffff; q1[1] = (f >> 24) & 0xffffffff; shr128(f, c, 56);
892  mul64x64_128(c, x[4], y[2]) add128_64(c, f) mul64x64_128(mul, x[2], y[4]) add128(c, mul) mul64x64_128(mul, x[3], y[3]) add128(c, mul)
893  f = lo128(c); q1[1] |= (f << 32) & 0xffffffffffffff; q1[2] = (f >> 24) & 0xffffffff; shr128(f, c, 56);
894  mul64x64_128(c, x[4], y[3]) add128_64(c, f) mul64x64_128(mul, x[3], y[4]) add128(c, mul)
895  f = lo128(c); q1[2] |= (f << 32) & 0xffffffffffffff; q1[3] = (f >> 24) & 0xffffffff; shr128(f, c, 56);
896  mul64x64_128(c, x[4], y[4]) add128_64(c, f)
897  f = lo128(c); q1[3] |= (f << 32) & 0xffffffffffffff; q1[4] = (f >> 24) & 0xffffffff; shr128(f, c, 56);
898  q1[4] |= (f << 32);
899 
900  barrett_reduce256_modm(r, q1, r1);
901 }
902 
903 void
904 expand256_modm(bignum256modm out, const byte *in, size_t len) {
905  byte work[64] = {0};
906  bignum256modm_element_t x[16];
907  bignum256modm q1;
908 
909  memcpy(work, in, len);
910  x[0] = U8TO64_LE(work + 0);
911  x[1] = U8TO64_LE(work + 8);
912  x[2] = U8TO64_LE(work + 16);
913  x[3] = U8TO64_LE(work + 24);
914  x[4] = U8TO64_LE(work + 32);
915  x[5] = U8TO64_LE(work + 40);
916  x[6] = U8TO64_LE(work + 48);
917  x[7] = U8TO64_LE(work + 56);
918 
919  /* r1 = (x mod 256^(32+1)) = x mod (2^8)(31+1) = x & ((1 << 264) - 1) */
920  out[0] = ( x[0]) & 0xffffffffffffff;
921  out[1] = ((x[ 0] >> 56) | (x[ 1] << 8)) & 0xffffffffffffff;
922  out[2] = ((x[ 1] >> 48) | (x[ 2] << 16)) & 0xffffffffffffff;
923  out[3] = ((x[ 2] >> 40) | (x[ 3] << 24)) & 0xffffffffffffff;
924  out[4] = ((x[ 3] >> 32) | (x[ 4] << 32)) & 0x0000ffffffffff;
925 
926  /* under 252 bits, no need to reduce */
927  if (len < 32)
928  return;
929 
930  /* q1 = x >> 248 = 264 bits */
931  q1[0] = ((x[ 3] >> 56) | (x[ 4] << 8)) & 0xffffffffffffff;
932  q1[1] = ((x[ 4] >> 48) | (x[ 5] << 16)) & 0xffffffffffffff;
933  q1[2] = ((x[ 5] >> 40) | (x[ 6] << 24)) & 0xffffffffffffff;
934  q1[3] = ((x[ 6] >> 32) | (x[ 7] << 32)) & 0xffffffffffffff;
935  q1[4] = ((x[ 7] >> 24) );
936 
937  barrett_reduce256_modm(out, q1, out);
938 }
939 
940 void
941 expand_raw256_modm(bignum256modm out, const byte in[32]) {
942  bignum256modm_element_t x[4];
943 
944  x[0] = U8TO64_LE(in + 0);
945  x[1] = U8TO64_LE(in + 8);
946  x[2] = U8TO64_LE(in + 16);
947  x[3] = U8TO64_LE(in + 24);
948 
949  out[0] = ( x[0]) & 0xffffffffffffff;
950  out[1] = ((x[ 0] >> 56) | (x[ 1] << 8)) & 0xffffffffffffff;
951  out[2] = ((x[ 1] >> 48) | (x[ 2] << 16)) & 0xffffffffffffff;
952  out[3] = ((x[ 2] >> 40) | (x[ 3] << 24)) & 0xffffffffffffff;
953  out[4] = ((x[ 3] >> 32) ) & 0x000000ffffffff;
954 }
955 
956 void
957 contract256_modm(byte out[32], const bignum256modm in) {
958  U64TO8_LE(out + 0, (in[0] ) | (in[1] << 56));
959  U64TO8_LE(out + 8, (in[1] >> 8) | (in[2] << 48));
960  U64TO8_LE(out + 16, (in[2] >> 16) | (in[3] << 40));
961  U64TO8_LE(out + 24, (in[3] >> 24) | (in[4] << 32));
962 }
963 
964 void
965 contract256_window4_modm(signed char r[64], const bignum256modm in) {
966  char carry;
967  signed char *quads = r;
968  bignum256modm_element_t i, j, v, m;
969 
970  for (i = 0; i < 5; i++) {
971  v = in[i];
972  m = (i == 4) ? 8 : 14;
973  for (j = 0; j < m; j++) {
974  *quads++ = (v & 15);
975  v >>= 4;
976  }
977  }
978 
979  /* making it signed */
980  carry = 0;
981  for(i = 0; i < 63; i++) {
982  r[i] += carry;
983  r[i+1] += (r[i] >> 4);
984  r[i] &= 15;
985  carry = (r[i] >> 3);
986  r[i] -= (carry << 4);
987  }
988  r[63] += carry;
989 }
990 
991 void
992 contract256_slidingwindow_modm(signed char r[256], const bignum256modm s, int windowsize) {
993  int i,j,k,b;
994  int m = (1 << (windowsize - 1)) - 1, soplen = 256;
995  signed char *bits = r;
996  bignum256modm_element_t v;
997 
998  /* first put the binary expansion into r */
999  for (i = 0; i < 4; i++) {
1000  v = s[i];
1001  for (j = 0; j < 56; j++, v >>= 1)
1002  *bits++ = (v & 1);
1003  }
1004  v = s[4];
1005  for (j = 0; j < 32; j++, v >>= 1)
1006  *bits++ = (v & 1);
1007 
1008  /* Making it sliding window */
1009  for (j = 0; j < soplen; j++) {
1010  if (!r[j])
1011  continue;
1012 
1013  for (b = 1; (b < (soplen - j)) && (b <= 6); b++) {
1014  if ((r[j] + (r[j + b] << b)) <= m) {
1015  r[j] += r[j + b] << b;
1016  r[j + b] = 0;
1017  } else if ((r[j] - (r[j + b] << b)) >= -m) {
1018  r[j] -= r[j + b] << b;
1019  for (k = j + b; k < soplen; k++) {
1020  if (!r[k]) {
1021  r[k] = 1;
1022  break;
1023  }
1024  r[k] = 0;
1025  }
1026  } else if (r[j + b]) {
1027  break;
1028  }
1029  }
1030  }
1031 }
1032 
1033 /*
1034  * In: b = 2^5 - 2^0
1035  * Out: b = 2^250 - 2^0
1036  */
1037 void
1038 curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b) {
1039  ALIGN(16) bignum25519 t0,c;
1040 
1041  /* 2^5 - 2^0 */ /* b */
1042  /* 2^10 - 2^5 */ curve25519_square_times(t0, b, 5);
1043  /* 2^10 - 2^0 */ curve25519_mul_noinline(b, t0, b);
1044  /* 2^20 - 2^10 */ curve25519_square_times(t0, b, 10);
1045  /* 2^20 - 2^0 */ curve25519_mul_noinline(c, t0, b);
1046  /* 2^40 - 2^20 */ curve25519_square_times(t0, c, 20);
1047  /* 2^40 - 2^0 */ curve25519_mul_noinline(t0, t0, c);
1048  /* 2^50 - 2^10 */ curve25519_square_times(t0, t0, 10);
1049  /* 2^50 - 2^0 */ curve25519_mul_noinline(b, t0, b);
1050  /* 2^100 - 2^50 */ curve25519_square_times(t0, b, 50);
1051  /* 2^100 - 2^0 */ curve25519_mul_noinline(c, t0, b);
1052  /* 2^200 - 2^100 */ curve25519_square_times(t0, c, 100);
1053  /* 2^200 - 2^0 */ curve25519_mul_noinline(t0, t0, c);
1054  /* 2^250 - 2^50 */ curve25519_square_times(t0, t0, 50);
1055  /* 2^250 - 2^0 */ curve25519_mul_noinline(b, t0, b);
1056 }
1057 
1058 /*
1059  * z^(p - 2) = z(2^255 - 21)
1060  */
1061 void
1062 curve25519_recip(bignum25519 out, const bignum25519 z) {
1063  ALIGN(16) bignum25519 a,t0,b;
1064 
1065  /* 2 */ curve25519_square_times(a, z, 1); /* a = 2 */
1066  /* 8 */ curve25519_square_times(t0, a, 2);
1067  /* 9 */ curve25519_mul_noinline(b, t0, z); /* b = 9 */
1068  /* 11 */ curve25519_mul_noinline(a, b, a); /* a = 11 */
1069  /* 22 */ curve25519_square_times(t0, a, 1);
1070  /* 2^5 - 2^0 = 31 */ curve25519_mul_noinline(b, t0, b);
1071  /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
1072  /* 2^255 - 2^5 */ curve25519_square_times(b, b, 5);
1073  /* 2^255 - 21 */ curve25519_mul_noinline(out, b, a);
1074 }
1075 
1076 /*
1077  * z^((p-5)/8) = z^(2^252 - 3)
1078  */
1079 void
1080 curve25519_pow_two252m3(bignum25519 two252m3, const bignum25519 z) {
1081  ALIGN(16) bignum25519 b,c,t0;
1082 
1083  /* 2 */ curve25519_square_times(c, z, 1); /* c = 2 */
1084  /* 8 */ curve25519_square_times(t0, c, 2); /* t0 = 8 */
1085  /* 9 */ curve25519_mul_noinline(b, t0, z); /* b = 9 */
1086  /* 11 */ curve25519_mul_noinline(c, b, c); /* c = 11 */
1087  /* 22 */ curve25519_square_times(t0, c, 1);
1088  /* 2^5 - 2^0 = 31 */ curve25519_mul_noinline(b, t0, b);
1089  /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
1090  /* 2^252 - 2^2 */ curve25519_square_times(b, b, 2);
1091  /* 2^252 - 3 */ curve25519_mul_noinline(two252m3, b, z);
1092 }
1093 
1094 inline void
1095 ge25519_p1p1_to_partial(ge25519 *r, const ge25519_p1p1 *p) {
1096  curve25519_mul(r->x, p->x, p->t);
1097  curve25519_mul(r->y, p->y, p->z);
1098  curve25519_mul(r->z, p->z, p->t);
1099 }
1100 
1101 inline void
1102 ge25519_p1p1_to_full(ge25519 *r, const ge25519_p1p1 *p) {
1103  curve25519_mul(r->x, p->x, p->t);
1104  curve25519_mul(r->y, p->y, p->z);
1105  curve25519_mul(r->z, p->z, p->t);
1106  curve25519_mul(r->t, p->x, p->y);
1107 }
1108 
1109 void
1110 ge25519_full_to_pniels(ge25519_pniels *p, const ge25519 *r) {
1111  curve25519_sub(p->ysubx, r->y, r->x);
1112  curve25519_add(p->xaddy, r->y, r->x);
1113  curve25519_copy(p->z, r->z);
1114  curve25519_mul(p->t2d, r->t, ge25519_ec2d);
1115 }
1116 
1117 void
1118 ge25519_add_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519 *q) {
1119  bignum25519 a,b,c,d,t,u;
1120 
1121  curve25519_sub(a, p->y, p->x);
1122  curve25519_add(b, p->y, p->x);
1123  curve25519_sub(t, q->y, q->x);
1124  curve25519_add(u, q->y, q->x);
1125  curve25519_mul(a, a, t);
1126  curve25519_mul(b, b, u);
1127  curve25519_mul(c, p->t, q->t);
1128  curve25519_mul(c, c, ge25519_ec2d);
1129  curve25519_mul(d, p->z, q->z);
1130  curve25519_add(d, d, d);
1131  curve25519_sub(r->x, b, a);
1132  curve25519_add(r->y, b, a);
1133  curve25519_add_after_basic(r->z, d, c);
1134  curve25519_sub_after_basic(r->t, d, c);
1135 }
1136 
1137 void
1138 ge25519_double_p1p1(ge25519_p1p1 *r, const ge25519 *p) {
1139  bignum25519 a,b,c;
1140 
1141  curve25519_square(a, p->x);
1142  curve25519_square(b, p->y);
1143  curve25519_square(c, p->z);
1144  curve25519_add_reduce(c, c, c);
1145  curve25519_add(r->x, p->x, p->y);
1146  curve25519_square(r->x, r->x);
1147  curve25519_add(r->y, b, a);
1148  curve25519_sub(r->z, b, a);
1149  curve25519_sub_after_basic(r->x, r->x, r->y);
1150  curve25519_sub_after_basic(r->t, c, r->z);
1151 }
1152 
1153 void
1154 ge25519_nielsadd2_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519_niels *q, byte signbit) {
1155  const bignum25519 *qb = (const bignum25519 *)q;
1156  bignum25519 *rb = (bignum25519 *)r;
1157  bignum25519 a,b,c;
1158 
1159  curve25519_sub(a, p->y, p->x);
1160  curve25519_add(b, p->y, p->x);
1161  curve25519_mul(a, a, qb[signbit]); /* x for +, y for - */
1162  curve25519_mul(r->x, b, qb[signbit^1]); /* y for +, x for - */
1163  curve25519_add(r->y, r->x, a);
1164  curve25519_sub(r->x, r->x, a);
1165  curve25519_mul(c, p->t, q->t2d);
1166  curve25519_add_reduce(r->t, p->z, p->z);
1167  curve25519_copy(r->z, r->t);
1168  curve25519_add(rb[2+signbit], rb[2+signbit], c); /* z for +, t for - */
1169  curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c); /* t for +, z for - */
1170 }
1171 
1172 void
1173 ge25519_pnielsadd_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519_pniels *q, byte signbit) {
1174  const bignum25519 *qb = (const bignum25519 *)q;
1175  bignum25519 *rb = (bignum25519 *)r;
1176  bignum25519 a,b,c;
1177 
1178  curve25519_sub(a, p->y, p->x);
1179  curve25519_add(b, p->y, p->x);
1180  curve25519_mul(a, a, qb[signbit]); /* ysubx for +, xaddy for - */
1181  curve25519_mul(r->x, b, qb[signbit^1]); /* xaddy for +, ysubx for - */
1182  curve25519_add(r->y, r->x, a);
1183  curve25519_sub(r->x, r->x, a);
1184  curve25519_mul(c, p->t, q->t2d);
1185  curve25519_mul(r->t, p->z, q->z);
1186  curve25519_add_reduce(r->t, r->t, r->t);
1187  curve25519_copy(r->z, r->t);
1188  curve25519_add(rb[2+signbit], rb[2+signbit], c); /* z for +, t for - */
1189  curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c); /* t for +, z for - */
1190 }
1191 
1192 void
1193 ge25519_double_partial(ge25519 *r, const ge25519 *p) {
1194  ge25519_p1p1 t;
1195  ge25519_double_p1p1(&t, p);
1196  ge25519_p1p1_to_partial(r, &t);
1197 }
1198 
1199 void
1200 ge25519_double(ge25519 *r, const ge25519 *p) {
1201  ge25519_p1p1 t;
1202  ge25519_double_p1p1(&t, p);
1203  ge25519_p1p1_to_full(r, &t);
1204 }
1205 
1206 void
1207 ge25519_add(ge25519 *r, const ge25519 *p, const ge25519 *q) {
1208  ge25519_p1p1 t;
1209  ge25519_add_p1p1(&t, p, q);
1210  ge25519_p1p1_to_full(r, &t);
1211 }
1212 
1213 void
1214 ge25519_nielsadd2(ge25519 *r, const ge25519_niels *q) {
1215  bignum25519 a,b,c,e,f,g,h;
1216 
1217  curve25519_sub(a, r->y, r->x);
1218  curve25519_add(b, r->y, r->x);
1219  curve25519_mul(a, a, q->ysubx);
1220  curve25519_mul(e, b, q->xaddy);
1221  curve25519_add(h, e, a);
1222  curve25519_sub(e, e, a);
1223  curve25519_mul(c, r->t, q->t2d);
1224  curve25519_add(f, r->z, r->z);
1225  curve25519_add_after_basic(g, f, c);
1226  curve25519_sub_after_basic(f, f, c);
1227  curve25519_mul(r->x, e, f);
1228  curve25519_mul(r->y, h, g);
1229  curve25519_mul(r->z, g, f);
1230  curve25519_mul(r->t, e, h);
1231 }
1232 
1233 void
1234 ge25519_pnielsadd(ge25519_pniels *r, const ge25519 *p, const ge25519_pniels *q) {
1235  bignum25519 a,b,c,x,y,z,t;
1236 
1237  curve25519_sub(a, p->y, p->x);
1238  curve25519_add(b, p->y, p->x);
1239  curve25519_mul(a, a, q->ysubx);
1240  curve25519_mul(x, b, q->xaddy);
1241  curve25519_add(y, x, a);
1242  curve25519_sub(x, x, a);
1243  curve25519_mul(c, p->t, q->t2d);
1244  curve25519_mul(t, p->z, q->z);
1245  curve25519_add(t, t, t);
1246  curve25519_add_after_basic(z, t, c);
1247  curve25519_sub_after_basic(t, t, c);
1248  curve25519_mul(r->xaddy, x, t);
1249  curve25519_mul(r->ysubx, y, z);
1250  curve25519_mul(r->z, z, t);
1251  curve25519_mul(r->t2d, x, y);
1252  curve25519_copy(y, r->ysubx);
1253  curve25519_sub(r->ysubx, r->ysubx, r->xaddy);
1254  curve25519_add(r->xaddy, r->xaddy, y);
1255  curve25519_mul(r->t2d, r->t2d, ge25519_ec2d);
1256 }
1257 
1258 void
1259 ge25519_pack(byte r[32], const ge25519 *p) {
1260  bignum25519 tx, ty, zi;
1261  byte parity[32];
1262  curve25519_recip(zi, p->z);
1263  curve25519_mul(tx, p->x, zi);
1264  curve25519_mul(ty, p->y, zi);
1265  curve25519_contract(r, ty);
1266  curve25519_contract(parity, tx);
1267  r[31] ^= ((parity[0] & 1) << 7);
1268 }
1269 
1270 int
1271 ed25519_verify(const unsigned char *x, const unsigned char *y, size_t len) {
1272  size_t differentbits = 0;
1273  while (len--)
1274  differentbits |= (*x++ ^ *y++);
1275  return (int) (1 & ((differentbits - 1) >> 8));
1276 }
1277 
1278 int
1279 ge25519_unpack_negative_vartime(ge25519 *r, const byte p[32]) {
1280  const byte zero[32] = {0};
1281  const bignum25519 one = {1};
1282  byte parity = p[31] >> 7;
1283  byte check[32];
1284  bignum25519 t, root, num, den, d3;
1285 
1286  curve25519_expand(r->y, p);
1287  curve25519_copy(r->z, one);
1288  curve25519_square(num, r->y); /* x = y^2 */
1289  curve25519_mul(den, num, ge25519_ecd); /* den = dy^2 */
1290  curve25519_sub_reduce(num, num, r->z); /* x = y^1 - 1 */
1291  curve25519_add(den, den, r->z); /* den = dy^2 + 1 */
1292 
1293  /* Computation of sqrt(num/den) */
1294  /* 1.: computation of num^((p-5)/8)*den^((7p-35)/8) = (num*den^7)^((p-5)/8) */
1295  curve25519_square(t, den);
1296  curve25519_mul(d3, t, den);
1297  curve25519_square(r->x, d3);
1298  curve25519_mul(r->x, r->x, den);
1299  curve25519_mul(r->x, r->x, num);
1300  curve25519_pow_two252m3(r->x, r->x);
1301 
1302  /* 2. computation of r->x = num * den^3 * (num*den^7)^((p-5)/8) */
1303  curve25519_mul(r->x, r->x, d3);
1304  curve25519_mul(r->x, r->x, num);
1305 
1306  /* 3. Check if either of the roots works: */
1307  curve25519_square(t, r->x);
1308  curve25519_mul(t, t, den);
1309  curve25519_sub_reduce(root, t, num);
1310  curve25519_contract(check, root);
1311  if (!ed25519_verify(check, zero, 32)) {
1312  curve25519_add_reduce(t, t, num);
1313  curve25519_contract(check, t);
1314  if (!ed25519_verify(check, zero, 32))
1315  return 0;
1316  curve25519_mul(r->x, r->x, ge25519_sqrtneg1);
1317  }
1318 
1319  curve25519_contract(check, r->x);
1320  if ((check[0] & 1) == parity) {
1321  curve25519_copy(t, r->x);
1322  curve25519_neg(r->x, t);
1323  }
1324  curve25519_mul(r->t, r->x, r->y);
1325  return 1;
1326 }
1327 
1328 /* computes [s1]p1 + [s2]basepoint */
1329 void
1330 ge25519_double_scalarmult_vartime(ge25519 *r, const ge25519 *p1, const bignum256modm s1, const bignum256modm s2) {
1331  signed char slide1[256], slide2[256];
1332  ge25519_pniels pre1[S1_TABLE_SIZE];
1333  ge25519 d1;
1334  ge25519_p1p1 t;
1335  sword32 i;
1336 
1337  contract256_slidingwindow_modm(slide1, s1, S1_SWINDOWSIZE);
1338  contract256_slidingwindow_modm(slide2, s2, S2_SWINDOWSIZE);
1339 
1340  ge25519_double(&d1, p1);
1341  ge25519_full_to_pniels(pre1, p1);
1342  for (i = 0; i < S1_TABLE_SIZE - 1; i++)
1343  ge25519_pnielsadd(&pre1[i+1], &d1, &pre1[i]);
1344 
1345  /* set neutral */
1346  memset(r, 0, sizeof(ge25519));
1347  r->y[0] = 1;
1348  r->z[0] = 1;
1349 
1350  i = 255;
1351  while ((i >= 0) && !(slide1[i] | slide2[i]))
1352  i--;
1353 
1354  for (; i >= 0; i--) {
1355  ge25519_double_p1p1(&t, r);
1356 
1357  if (slide1[i]) {
1358  ge25519_p1p1_to_full(r, &t);
1359  ge25519_pnielsadd_p1p1(&t, r, &pre1[abs(slide1[i]) / 2], (byte)slide1[i] >> 7);
1360  }
1361 
1362  if (slide2[i]) {
1363  ge25519_p1p1_to_full(r, &t);
1364  ge25519_nielsadd2_p1p1(&t, r, &ge25519_niels_sliding_multiples[abs(slide2[i]) / 2], (byte)slide2[i] >> 7);
1365  }
1366 
1367  ge25519_p1p1_to_partial(r, &t);
1368  }
1369 }
1370 
1371 #if !defined(HAVE_GE25519_SCALARMULT_BASE_CHOOSE_NIELS)
1372 
1373 word32
1374 ge25519_windowb_equal(word32 b, word32 c) {
1375  return ((b ^ c) - 1) >> 31;
1376 }
1377 
1378 void
1379 ge25519_scalarmult_base_choose_niels(ge25519_niels *t, const uint8_t table[256][96], word32 pos, signed char b) {
1380  bignum25519 neg;
1381  word32 sign = (word32)((byte)b >> 7);
1382  word32 mask = ~(sign - 1);
1383  word32 u = (b + mask) ^ mask;
1384  word32 i;
1385 
1386  /* ysubx, xaddy, t2d in packed form. initialize to ysubx = 1, xaddy = 1, t2d = 0 */
1387  uint8_t packed[96] = {0};
1388  packed[0] = 1;
1389  packed[32] = 1;
1390 
1391  for (i = 0; i < 8; i++)
1392  curve25519_move_conditional_bytes(packed, table[(pos * 8) + i], ge25519_windowb_equal(u, i + 1));
1393 
1394  /* expand in to t */
1395  curve25519_expand(t->ysubx, packed + 0);
1396  curve25519_expand(t->xaddy, packed + 32);
1397  curve25519_expand(t->t2d , packed + 64);
1398 
1399  /* adjust for sign */
1400  curve25519_swap_conditional(t->ysubx, t->xaddy, sign);
1401  curve25519_neg(neg, t->t2d);
1402  curve25519_swap_conditional(t->t2d, neg, sign);
1403 }
1404 
1405 #endif /* HAVE_GE25519_SCALARMULT_BASE_CHOOSE_NIELS */
1406 
1407 /* computes [s]basepoint */
1408 void
1409 ge25519_scalarmult_base_niels(ge25519 *r, const uint8_t basepoint_table[256][96], const bignum256modm s) {
1410  signed char b[64];
1411  word32 i;
1412  ge25519_niels t;
1413 
1414  contract256_window4_modm(b, s);
1415 
1416  ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[1]);
1417  curve25519_sub_reduce(r->x, t.xaddy, t.ysubx);
1418  curve25519_add_reduce(r->y, t.xaddy, t.ysubx);
1419  memset(r->z, 0, sizeof(bignum25519));
1420  curve25519_copy(r->t, t.t2d);
1421  r->z[0] = 2;
1422  for (i = 3; i < 64; i += 2) {
1423  ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
1424  ge25519_nielsadd2(r, &t);
1425  }
1426  ge25519_double_partial(r, r);
1427  ge25519_double_partial(r, r);
1428  ge25519_double_partial(r, r);
1429  ge25519_double(r, r);
1430  ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[0]);
1431  curve25519_mul(t.t2d, t.t2d, ge25519_ecd);
1432  ge25519_nielsadd2(r, &t);
1433  for(i = 2; i < 64; i += 2) {
1434  ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
1435  ge25519_nielsadd2(r, &t);
1436  }
1437 }
1438 
1439 ANONYMOUS_NAMESPACE_END
1440 NAMESPACE_END // Ed25519
1441 NAMESPACE_END // Donna
1442 NAMESPACE_END // CryptoPP
1443 
1444 //***************************** curve25519 *****************************//
1445 
1446 NAMESPACE_BEGIN(CryptoPP)
1447 NAMESPACE_BEGIN(Donna)
1448 
1449 int curve25519_mult_CXX(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32])
1450 {
1451  using namespace CryptoPP::Donna::X25519;
1452 
1454  for (size_t i = 0;i < 32;++i)
1455  e[i] = secretKey[i];
1456  e[0] &= 0xf8; e[31] &= 0x7f; e[31] |= 0x40;
1457 
1458  bignum25519 nqpqx = {1}, nqpqz = {0}, nqz = {1}, nqx;
1459  bignum25519 q, qx, qpqx, qqx, zzz, zmone;
1460  size_t bit, lastbit;
1461 
1462  curve25519_expand(q, othersKey);
1463  curve25519_copy(nqx, q);
1464 
1465  /* bit 255 is always 0, and bit 254 is always 1, so skip bit 255 and
1466  start pre-swapped on bit 254 */
1467  lastbit = 1;
1468 
1469  /* we are doing bits 254..3 in the loop, but are swapping in bits 253..2 */
1470  for (int i = 253; i >= 2; i--) {
1471  curve25519_add(qx, nqx, nqz);
1472  curve25519_sub(nqz, nqx, nqz);
1473  curve25519_add(qpqx, nqpqx, nqpqz);
1474  curve25519_sub(nqpqz, nqpqx, nqpqz);
1475  curve25519_mul(nqpqx, qpqx, nqz);
1476  curve25519_mul(nqpqz, qx, nqpqz);
1477  curve25519_add(qqx, nqpqx, nqpqz);
1478  curve25519_sub(nqpqz, nqpqx, nqpqz);
1479  curve25519_square(nqpqz, nqpqz);
1480  curve25519_square(nqpqx, qqx);
1481  curve25519_mul(nqpqz, nqpqz, q);
1482  curve25519_square(qx, qx);
1483  curve25519_square(nqz, nqz);
1484  curve25519_mul(nqx, qx, nqz);
1485  curve25519_sub(nqz, qx, nqz);
1486  curve25519_scalar_product(zzz, nqz, 121665);
1487  curve25519_add(zzz, zzz, qx);
1488  curve25519_mul(nqz, nqz, zzz);
1489 
1490  bit = (e[i/8] >> (i & 7)) & 1;
1491  curve25519_swap_conditional(nqx, nqpqx, bit ^ lastbit);
1492  curve25519_swap_conditional(nqz, nqpqz, bit ^ lastbit);
1493  lastbit = bit;
1494  }
1495 
1496  /* the final 3 bits are always zero, so we only need to double */
1497  for (int i = 0; i < 3; i++) {
1498  curve25519_add(qx, nqx, nqz);
1499  curve25519_sub(nqz, nqx, nqz);
1500  curve25519_square(qx, qx);
1501  curve25519_square(nqz, nqz);
1502  curve25519_mul(nqx, qx, nqz);
1503  curve25519_sub(nqz, qx, nqz);
1504  curve25519_scalar_product(zzz, nqz, 121665);
1505  curve25519_add(zzz, zzz, qx);
1506  curve25519_mul(nqz, nqz, zzz);
1507  }
1508 
1509  curve25519_recip(zmone, nqz);
1510  curve25519_mul(nqz, nqx, zmone);
1511  curve25519_contract(sharedKey, nqz);
1512 
1513  return 0;
1514 }
1515 
1516 int curve25519_mult(byte publicKey[32], const byte secretKey[32])
1517 {
1518  using namespace CryptoPP::Donna::X25519;
1519 
1520 #if (CRYPTOPP_CURVE25519_SSE2)
1521  if (HasSSE2())
1522  return curve25519_mult_SSE2(publicKey, secretKey, basePoint);
1523  else
1524 #endif
1525 
1526  return curve25519_mult_CXX(publicKey, secretKey, basePoint);
1527 }
1528 
1529 int curve25519_mult(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32])
1530 {
1531 #if (CRYPTOPP_CURVE25519_SSE2)
1532  if (HasSSE2())
1533  return curve25519_mult_SSE2(sharedKey, secretKey, othersKey);
1534  else
1535 #endif
1536 
1537  return curve25519_mult_CXX(sharedKey, secretKey, othersKey);
1538 }
1539 
1540 NAMESPACE_END // Donna
1541 NAMESPACE_END // CryptoPP
1542 
1543 //******************************* ed25519 *******************************//
1544 
1545 NAMESPACE_BEGIN(CryptoPP)
1546 NAMESPACE_BEGIN(Donna)
1547 
1548 int
1549 ed25519_publickey_CXX(byte publicKey[32], const byte secretKey[32])
1550 {
1551  using namespace CryptoPP::Donna::Ed25519;
1552 
1553  bignum256modm a;
1554  ALIGN(16) ge25519 A;
1555  hash_512bits extsk;
1556 
1557  /* A = aB */
1558  ed25519_extsk(extsk, secretKey);
1559  expand256_modm(a, extsk, 32);
1560  ge25519_scalarmult_base_niels(&A, ge25519_niels_base_multiples, a);
1561  ge25519_pack(publicKey, &A);
1562 
1563  return 0;
1564 }
1565 
1566 int
1567 ed25519_publickey(byte publicKey[32], const byte secretKey[32])
1568 {
1569  return ed25519_publickey_CXX(publicKey, secretKey);
1570 }
1571 
1572 int
1573 ed25519_sign_CXX(const byte *m, size_t mlen, const byte sk[32], const byte pk[32], byte RS[64])
1574 {
1575  using namespace CryptoPP::Donna::Ed25519;
1576 
1577  bignum256modm r, S, a;
1578  ALIGN(16) ge25519 R;
1579  hash_512bits extsk, hashr, hram;
1580 
1581  ed25519_extsk(extsk, sk);
1582 
1583  /* r = H(aExt[32..64], m) */
1584  SHA512 hash;
1585  hash.Update(extsk + 32, 32);
1586  hash.Update(m, mlen);
1587  hash.Final(hashr);
1588  expand256_modm(r, hashr, 64);
1589 
1590  /* R = rB */
1591  ge25519_scalarmult_base_niels(&R, ge25519_niels_base_multiples, r);
1592  ge25519_pack(RS, &R);
1593 
1594  /* S = H(R,A,m).. */
1595  ed25519_hram(hram, RS, pk, m, mlen);
1596  expand256_modm(S, hram, 64);
1597 
1598  /* S = H(R,A,m)a */
1599  expand256_modm(a, extsk, 32);
1600  mul256_modm(S, S, a);
1601 
1602  /* S = (r + H(R,A,m)a) */
1603  add256_modm(S, S, r);
1604 
1605  /* S = (r + H(R,A,m)a) mod L */
1606  contract256_modm(RS + 32, S);
1607  return 0;
1608 }
1609 
1610 int
1611 ed25519_sign(const byte* message, size_t messageLength, const byte secretKey[32],
1612  const byte publicKey[32], byte signature[64])
1613 {
1614  return ed25519_sign_CXX(message, messageLength, secretKey, publicKey, signature);
1615 }
1616 
1617 int
1618 ed25519_sign_open_CXX(const byte *m, size_t mlen, const byte pk[32], const byte RS[64]) {
1619 
1620  using namespace CryptoPP::Donna::Ed25519;
1621 
1622  ALIGN(16) ge25519 R, A;
1623  hash_512bits hash;
1624  bignum256modm hram, S;
1625  unsigned char checkR[32];
1626 
1627  if ((RS[63] & 224) || !ge25519_unpack_negative_vartime(&A, pk))
1628  return -1;
1629 
1630  /* hram = H(R,A,m) */
1631  ed25519_hram(hash, RS, pk, m, mlen);
1632  expand256_modm(hram, hash, 64);
1633 
1634  /* S */
1635  expand256_modm(S, RS + 32, 32);
1636 
1637  /* SB - H(R,A,m)A */
1638  ge25519_double_scalarmult_vartime(&R, &A, hram, S);
1639  ge25519_pack(checkR, &R);
1640 
1641  /* check that R = SB - H(R,A,m)A */
1642  return ed25519_verify(RS, checkR, 32) ? 0 : -1;
1643 }
1644 
1645 int
1646 ed25519_sign_open(const byte *message, size_t messageLength, const byte publicKey[32], const byte signature[64])
1647 {
1648  return ed25519_sign_open_CXX(message, messageLength, publicKey, signature);
1649 }
1650 
1651 NAMESPACE_END // Donna
1652 NAMESPACE_END // CryptoPP
1653 
1654 #endif // CRYPTOPP_CURVE25519_64BIT
Utility functions for the Crypto++ library.
void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock=NULL)
Access a block of memory.
Definition: misc.h:2362
Converts an enumeration to a type suitable for use as a template parameter.
Definition: cryptlib.h:135
EnumToType< ByteOrder, LITTLE_ENDIAN_ORDER > LittleEndian
Provides a constant for LittleEndian.
Definition: cryptlib.h:150
Library configuration file.
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: iterhash.cpp:13
byte order is little-endian
Definition: cryptlib.h:145
Classes and functions for secure memory allocations.
T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
Access a block of memory.
Definition: misc.h:2320
int ed25519_sign(const byte *message, size_t messageLength, const byte secretKey[32], const byte publicKey[32], byte signature[64])
Creates a signature on a message.
int curve25519_mult(byte publicKey[32], const byte secretKey[32])
Generate a public key.
SHA-512 message digest.
Definition: sha.h:141
Precompiled header file.
int ed25519_sign_open(const byte *message, size_t messageLength, const byte publicKey[32], const byte signature[64])
Verifies a signature on a message.
Fixed size stack-based SecBlock.
Definition: secblock.h:1071
Functions for CPU features and intrinsics.
Classes for SHA-1 and SHA-2 family of message digests.
virtual void CalculateDigest(byte *digest, const byte *input, size_t length)
Updates the hash with additional input and computes the hash of the current message.
Definition: cryptlib.h:1158
bool HasSSE2()
Determines SSE2 availability.
Definition: cpu.h:116
Access a block of memory.
Definition: misc.h:2388
Crypto++ library namespace.
int ed25519_publickey(byte publicKey[32], const byte secretKey[32])
Creates a public key from a secret key.
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:1114