11 static const uint32_t reduce_mask_25 = (1 << 25) - 1;
12 static const uint32_t reduce_mask_26 = (1 << 26) - 1;
48 out[0] =
a[0] + b[0] ; c = (
out[0] >> 26);
out[0] &= reduce_mask_26;
49 out[1] =
a[1] + b[1] + c; c = (
out[1] >> 25);
out[1] &= reduce_mask_25;
50 out[2] =
a[2] + b[2] + c; c = (
out[2] >> 26);
out[2] &= reduce_mask_26;
51 out[3] =
a[3] + b[3] + c; c = (
out[3] >> 25);
out[3] &= reduce_mask_25;
52 out[4] =
a[4] + b[4] + c; c = (
out[4] >> 26);
out[4] &= reduce_mask_26;
53 out[5] =
a[5] + b[5] + c; c = (
out[5] >> 25);
out[5] &= reduce_mask_25;
54 out[6] =
a[6] + b[6] + c; c = (
out[6] >> 26);
out[6] &= reduce_mask_26;
55 out[7] =
a[7] + b[7] + c; c = (
out[7] >> 25);
out[7] &= reduce_mask_25;
56 out[8] =
a[8] + b[8] + c; c = (
out[8] >> 26);
out[8] &= reduce_mask_26;
57 out[9] =
a[9] + b[9] + c; c = (
out[9] >> 25);
out[9] &= reduce_mask_25;
64 out[0] =
a[0] + b[0] ; c = (
out[0] >> 26);
out[0] &= reduce_mask_26;
65 out[1] =
a[1] + b[1] + c; c = (
out[1] >> 25);
out[1] &= reduce_mask_25;
66 out[2] =
a[2] + b[2] + c; c = (
out[2] >> 26);
out[2] &= reduce_mask_26;
67 out[3] =
a[3] + b[3] + c; c = (
out[3] >> 25);
out[3] &= reduce_mask_25;
68 out[4] =
a[4] + b[4] + c; c = (
out[4] >> 26);
out[4] &= reduce_mask_26;
69 out[5] =
a[5] + b[5] + c; c = (
out[5] >> 25);
out[5] &= reduce_mask_25;
70 out[6] =
a[6] + b[6] + c; c = (
out[6] >> 26);
out[6] &= reduce_mask_26;
71 out[7] =
a[7] + b[7] + c; c = (
out[7] >> 25);
out[7] &= reduce_mask_25;
72 out[8] =
a[8] + b[8] + c; c = (
out[8] >> 26);
out[8] &= reduce_mask_26;
73 out[9] =
a[9] + b[9] + c; c = (
out[9] >> 25);
out[9] &= reduce_mask_25;
78 static const uint32_t twoP0 = 0x07ffffda;
79 static const uint32_t twoP13579 = 0x03fffffe;
80 static const uint32_t twoP2468 = 0x07fffffe;
81 static const uint32_t fourP0 = 0x0fffffb4;
82 static const uint32_t fourP13579 = 0x07fffffc;
83 static const uint32_t fourP2468 = 0x0ffffffc;
89 out[0] = twoP0 +
a[0] - b[0] ; c = (
out[0] >> 26);
out[0] &= reduce_mask_26;
90 out[1] = twoP13579 +
a[1] - b[1] + c; c = (
out[1] >> 25);
out[1] &= reduce_mask_25;
91 out[2] = twoP2468 +
a[2] - b[2] + c; c = (
out[2] >> 26);
out[2] &= reduce_mask_26;
92 out[3] = twoP13579 +
a[3] - b[3] + c; c = (
out[3] >> 25);
out[3] &= reduce_mask_25;
93 out[4] = twoP2468 +
a[4] - b[4] + c;
94 out[5] = twoP13579 +
a[5] - b[5] ;
95 out[6] = twoP2468 +
a[6] - b[6] ;
96 out[7] = twoP13579 +
a[7] - b[7] ;
97 out[8] = twoP2468 +
a[8] - b[8] ;
98 out[9] = twoP13579 +
a[9] - b[9] ;
105 out[0] = fourP0 +
a[0] - b[0] ; c = (
out[0] >> 26);
out[0] &= reduce_mask_26;
106 out[1] = fourP13579 +
a[1] - b[1] + c; c = (
out[1] >> 25);
out[1] &= reduce_mask_25;
107 out[2] = fourP2468 +
a[2] - b[2] + c; c = (
out[2] >> 26);
out[2] &= reduce_mask_26;
108 out[3] = fourP13579 +
a[3] - b[3] + c; c = (
out[3] >> 25);
out[3] &= reduce_mask_25;
109 out[4] = fourP2468 +
a[4] - b[4] + c; c = (
out[4] >> 26);
out[4] &= reduce_mask_26;
110 out[5] = fourP13579 +
a[5] - b[5] + c; c = (
out[5] >> 25);
out[5] &= reduce_mask_25;
111 out[6] = fourP2468 +
a[6] - b[6] + c; c = (
out[6] >> 26);
out[6] &= reduce_mask_26;
112 out[7] = fourP13579 +
a[7] - b[7] + c; c = (
out[7] >> 25);
out[7] &= reduce_mask_25;
113 out[8] = fourP2468 +
a[8] - b[8] + c; c = (
out[8] >> 26);
out[8] &= reduce_mask_26;
114 out[9] = fourP13579 +
a[9] - b[9] + c; c = (
out[9] >> 25);
out[9] &= reduce_mask_25;
121 out[0] = fourP0 +
a[0] - b[0] ; c = (
out[0] >> 26);
out[0] &= reduce_mask_26;
122 out[1] = fourP13579 +
a[1] - b[1] + c; c = (
out[1] >> 25);
out[1] &= reduce_mask_25;
123 out[2] = fourP2468 +
a[2] - b[2] + c; c = (
out[2] >> 26);
out[2] &= reduce_mask_26;
124 out[3] = fourP13579 +
a[3] - b[3] + c; c = (
out[3] >> 25);
out[3] &= reduce_mask_25;
125 out[4] = fourP2468 +
a[4] - b[4] + c; c = (
out[4] >> 26);
out[4] &= reduce_mask_26;
126 out[5] = fourP13579 +
a[5] - b[5] + c; c = (
out[5] >> 25);
out[5] &= reduce_mask_25;
127 out[6] = fourP2468 +
a[6] - b[6] + c; c = (
out[6] >> 26);
out[6] &= reduce_mask_26;
128 out[7] = fourP13579 +
a[7] - b[7] + c; c = (
out[7] >> 25);
out[7] &= reduce_mask_25;
129 out[8] = fourP2468 +
a[8] - b[8] + c; c = (
out[8] >> 26);
out[8] &= reduce_mask_26;
130 out[9] = fourP13579 +
a[9] - b[9] + c; c = (
out[9] >> 25);
out[9] &= reduce_mask_25;
138 out[0] = twoP0 -
a[0] ; c = (
out[0] >> 26);
out[0] &= reduce_mask_26;
139 out[1] = twoP13579 -
a[1] + c; c = (
out[1] >> 25);
out[1] &= reduce_mask_25;
140 out[2] = twoP2468 -
a[2] + c; c = (
out[2] >> 26);
out[2] &= reduce_mask_26;
141 out[3] = twoP13579 -
a[3] + c; c = (
out[3] >> 25);
out[3] &= reduce_mask_25;
142 out[4] = twoP2468 -
a[4] + c; c = (
out[4] >> 26);
out[4] &= reduce_mask_26;
143 out[5] = twoP13579 -
a[5] + c; c = (
out[5] >> 25);
out[5] &= reduce_mask_25;
144 out[6] = twoP2468 -
a[6] + c; c = (
out[6] >> 26);
out[6] &= reduce_mask_26;
145 out[7] = twoP13579 -
a[7] + c; c = (
out[7] >> 25);
out[7] &= reduce_mask_25;
146 out[8] = twoP2468 -
a[8] + c; c = (
out[8] >> 26);
out[8] &= reduce_mask_26;
147 out[9] = twoP13579 -
a[9] + c; c = (
out[9] >> 25);
out[9] &= reduce_mask_25;
152 #define curve25519_mul_noinline curve25519_mul 155 uint32_t r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
156 uint32_t s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
157 uint64_t m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
186 m9 =
mul32x32_64(r0, s9) +
mul32x32_64(r1, s8) +
mul32x32_64(r2, s7) +
mul32x32_64(r3, s6) +
mul32x32_64(r4, s5) +
mul32x32_64(r5, s4) +
mul32x32_64(r6, s3) +
mul32x32_64(r7, s2) +
mul32x32_64(r8, s1) +
mul32x32_64(r9, s0);
225 r0 = (
uint32_t)m0 & reduce_mask_26; c = (m0 >> 26);
226 m1 += c; r1 = (
uint32_t)m1 & reduce_mask_25; c = (m1 >> 25);
227 m2 += c; r2 = (
uint32_t)m2 & reduce_mask_26; c = (m2 >> 26);
228 m3 += c; r3 = (
uint32_t)m3 & reduce_mask_25; c = (m3 >> 25);
229 m4 += c; r4 = (
uint32_t)m4 & reduce_mask_26; c = (m4 >> 26);
230 m5 += c; r5 = (
uint32_t)m5 & reduce_mask_25; c = (m5 >> 25);
231 m6 += c; r6 = (
uint32_t)m6 & reduce_mask_26; c = (m6 >> 26);
232 m7 += c; r7 = (
uint32_t)m7 & reduce_mask_25; c = (m7 >> 25);
233 m8 += c; r8 = (
uint32_t)m8 & reduce_mask_26; c = (m8 >> 26);
253 uint32_t r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
255 uint64_t m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
299 r0 = (
uint32_t)m0 & reduce_mask_26; c = (m0 >> 26);
300 m1 += c; r1 = (
uint32_t)m1 & reduce_mask_25; c = (m1 >> 25);
301 m2 += c; r2 = (
uint32_t)m2 & reduce_mask_26; c = (m2 >> 26);
302 m3 += c; r3 = (
uint32_t)m3 & reduce_mask_25; c = (m3 >> 25);
303 m4 += c; r4 = (
uint32_t)m4 & reduce_mask_26; c = (m4 >> 26);
304 m5 += c; r5 = (
uint32_t)m5 & reduce_mask_25; c = (m5 >> 25);
305 m6 += c; r6 = (
uint32_t)m6 & reduce_mask_26; c = (m6 >> 26);
306 m7 += c; r7 = (
uint32_t)m7 & reduce_mask_25; c = (m7 >> 25);
307 m8 += c; r8 = (
uint32_t)m8 & reduce_mask_26; c = (m8 >> 26);
328 uint32_t r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
330 uint64_t m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
375 r0 = (
uint32_t)m0 & reduce_mask_26; c = (m0 >> 26);
376 m1 += c; r1 = (
uint32_t)m1 & reduce_mask_25; c = (m1 >> 25);
377 m2 += c; r2 = (
uint32_t)m2 & reduce_mask_26; c = (m2 >> 26);
378 m3 += c; r3 = (
uint32_t)m3 & reduce_mask_25; c = (m3 >> 25);
379 m4 += c; r4 = (
uint32_t)m4 & reduce_mask_26; c = (m4 >> 26);
380 m5 += c; r5 = (
uint32_t)m5 & reduce_mask_25; c = (m5 >> 25);
381 m6 += c; r6 = (
uint32_t)m6 & reduce_mask_26; c = (m6 >> 26);
382 m7 += c; r7 = (
uint32_t)m7 & reduce_mask_25; c = (m7 >> 25);
383 m8 += c; r8 = (
uint32_t)m8 & reduce_mask_26; c = (m8 >> 26);
403 curve25519_expand(
bignum25519 out,
const unsigned char in[32]) {
404 static const union {
uint8_t b[2];
uint16_t s; } endian_check = {{1,0}};
407 if (endian_check.s == 1) {
418 ((((uint32_t)in[s + 0]) ) | \ 419 (((uint32_t)in[s + 1]) << 8) | \ 420 (((uint32_t)in[s + 2]) << 16) | \ 421 (((uint32_t)in[s + 3]) << 24)) 433 out[0] = ( x0 ) & 0x3ffffff;
434 out[1] = ((((
uint64_t)x1 << 32) | x0) >> 26) & 0x1ffffff;
435 out[2] = ((((
uint64_t)x2 << 32) | x1) >> 19) & 0x3ffffff;
436 out[3] = ((((
uint64_t)x3 << 32) | x2) >> 13) & 0x1ffffff;
437 out[4] = (( x3) >> 6) & 0x3ffffff;
438 out[5] = ( x4 ) & 0x1ffffff;
439 out[6] = ((((
uint64_t)x5 << 32) | x4) >> 25) & 0x3ffffff;
440 out[7] = ((((
uint64_t)x6 << 32) | x5) >> 19) & 0x1ffffff;
441 out[8] = ((((
uint64_t)x7 << 32) | x6) >> 12) & 0x3ffffff;
442 out[9] = (( x7) >> 6) & 0x1ffffff;
449 curve25519_contract(
unsigned char out[32],
const bignum25519 in) {
451 curve25519_copy(f, in);
453 #define carry_pass() \ 454 f[1] += f[0] >> 26; f[0] &= reduce_mask_26; \ 455 f[2] += f[1] >> 25; f[1] &= reduce_mask_25; \ 456 f[3] += f[2] >> 26; f[2] &= reduce_mask_26; \ 457 f[4] += f[3] >> 25; f[3] &= reduce_mask_25; \ 458 f[5] += f[4] >> 26; f[4] &= reduce_mask_26; \ 459 f[6] += f[5] >> 25; f[5] &= reduce_mask_25; \ 460 f[7] += f[6] >> 26; f[6] &= reduce_mask_26; \ 461 f[8] += f[7] >> 25; f[7] &= reduce_mask_25; \ 462 f[9] += f[8] >> 26; f[8] &= reduce_mask_26; 464 #define carry_pass_full() \ 466 f[0] += 19 * (f[9] >> 25); f[9] &= reduce_mask_25; 468 #define carry_pass_final() \ 470 f[9] &= reduce_mask_25; 481 f[0] += (reduce_mask_26 + 1) - 19;
482 f[1] += (reduce_mask_25 + 1) - 1;
483 f[2] += (reduce_mask_26 + 1) - 1;
484 f[3] += (reduce_mask_25 + 1) - 1;
485 f[4] += (reduce_mask_26 + 1) - 1;
486 f[5] += (reduce_mask_25 + 1) - 1;
487 f[6] += (reduce_mask_26 + 1) - 1;
488 f[7] += (reduce_mask_25 + 1) - 1;
489 f[8] += (reduce_mask_26 + 1) - 1;
490 f[9] += (reduce_mask_25 + 1) - 1;
509 out[s+0] |= (unsigned char )(f[i] & 0xff); \ 510 out[s+1] = (unsigned char )((f[i] >> 8) & 0xff); \ 511 out[s+2] = (unsigned char )((f[i] >> 16) & 0xff); \ 512 out[s+3] = (unsigned char )((f[i] >> 24) & 0xff); 533 const uint32_t nb = flag - 1, b = ~nb;
536 outl[0] = (outl[0] & nb) | (inl[0] & b);
537 outl[1] = (outl[1] & nb) | (inl[1] & b);
538 outl[2] = (outl[2] & nb) | (inl[2] & b);
539 outl[3] = (outl[3] & nb) | (inl[3] & b);
540 outl[4] = (outl[4] & nb) | (inl[4] & b);
541 outl[5] = (outl[5] & nb) | (inl[5] & b);
542 outl[6] = (outl[6] & nb) | (inl[6] & b);
543 outl[7] = (outl[7] & nb) | (inl[7] & b);
544 outl[8] = (outl[8] & nb) | (inl[8] & b);
545 outl[9] = (outl[9] & nb) | (inl[9] & b);
546 outl[10] = (outl[10] & nb) | (inl[10] & b);
547 outl[11] = (outl[11] & nb) | (inl[11] & b);
548 outl[12] = (outl[12] & nb) | (inl[12] & b);
549 outl[13] = (outl[13] & nb) | (inl[13] & b);
550 outl[14] = (outl[14] & nb) | (inl[14] & b);
551 outl[15] = (outl[15] & nb) | (inl[15] & b);
552 outl[16] = (outl[16] & nb) | (inl[16] & b);
553 outl[17] = (outl[17] & nb) | (inl[17] & b);
554 outl[18] = (outl[18] & nb) | (inl[18] & b);
555 outl[19] = (outl[19] & nb) | (inl[19] & b);
556 outl[20] = (outl[20] & nb) | (inl[20] & b);
557 outl[21] = (outl[21] & nb) | (inl[21] & b);
558 outl[22] = (outl[22] & nb) | (inl[22] & b);
559 outl[23] = (outl[23] & nb) | (inl[23] & b);
567 uint32_t x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
569 x0 = swap & (
a[0] ^ b[0]);
a[0] ^= x0; b[0] ^= x0;
570 x1 = swap & (
a[1] ^ b[1]);
a[1] ^= x1; b[1] ^= x1;
571 x2 = swap & (
a[2] ^ b[2]);
a[2] ^= x2; b[2] ^= x2;
572 x3 = swap & (
a[3] ^ b[3]);
a[3] ^= x3; b[3] ^= x3;
573 x4 = swap & (
a[4] ^ b[4]);
a[4] ^= x4; b[4] ^= x4;
574 x5 = swap & (
a[5] ^ b[5]);
a[5] ^= x5; b[5] ^= x5;
575 x6 = swap & (
a[6] ^ b[6]);
a[6] ^= x6; b[6] ^= x6;
576 x7 = swap & (
a[7] ^ b[7]);
a[7] ^= x7; b[7] ^= x7;
577 x8 = swap & (
a[8] ^ b[8]);
a[8] ^= x8; b[8] ^= x8;
578 x9 = swap & (
a[9] ^ b[9]);
a[9] ^= x9; b[9] ^= x9;
#define curve25519_square(r, n)
#define curve25519_add_after_basic
#define mul32x32_64(a, b)
mdb_size_t count(MDB_cursor *cur)
unsigned __int64 uint64_t
#define carry_pass_final()
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
uint32_t bignum25519align16[12]
#define carry_pass_full()