Debian Codegraph @

src/x25519.c

raw symbols SCIP
1 #include "common.h"
2 #include "endianess.h"
3
4 FAKE_INIT(x25519)
5
6 /*
7 * Fast variable-base scalar multiplication for the Montgomery curve Curve25519
8 *
9 * y² = x³ + 486662x² + x
10 *
11 * over the prime field 2²⁵⁵ - 19.
12 */
13
14 #include "mod25519.c"
15
16 /*
17 * Execute the step in the Montgomery ladder.
18 *
19 * x2/z2 is updated with the doubling of P₂
20 * x3/z3 is updated with the sum P₂+P₃
21 *
22 * @param[in,out] x2 The projective X-coordinate of P₂ (< 2²⁶)
23 * @param[in,out] z2 The projective Z-coordinate of P₂ (< 2²⁶)
24 * @param[in,out] x3 The projective X-coordinate of P₃ (< 2²⁶)
25 * @param[in,out] z3 The projective Z-coordinate of P₃ (< 2²⁶)
26 * @param[in] xp The affine X-coordinate of P₃-P₂ (< 2²⁶)
27 */
28 STATIC void ladder_step(uint32_t x2[10], uint32_t z2[10], uint32_t x3[10], uint32_t z3[10], const uint32_t xp[10])
29 {
30 uint32_t t0[10], t1[10];
31 static const uint32_t nr_121666[10] = { 121666 };
32
33 /* https://www.hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#ladder-mladd-1987-m */
34
35 sub_25519(t0, x3, z3); /* t0 = D < 2^26 */
36 sub_25519(t1, x2, z2); /* t1 = B < 2^26 */
37 add32(x2, x2, z2); /* x2 = A < 2^27 */
38 add32(z2, x3, z3); /* z2 = C < 2^27 */
39 mul_25519(z3, t0, x2); /* z3 = DA < 2^26 */
40 mul_25519(z2, z2, t1); /* z2 = CB < 2^26 */
41 add32(x3, z3, z2); /* x3 = DA+CB < 2^27 */
42 sub_25519(z2, z3, z2); /* z2 = DA-CB < 2^26 */
43 mul_25519(x3, x3, x3); /* x3 = X5 < 2^26 */
44 mul_25519(z2, z2, z2); /* z2 = (DA-CB)² < 2^26 */
45 mul_25519(t0, t1, t1); /* t0 = BB < 2^26 */
46 mul_25519(t1, x2, x2); /* t1 = AA < 2^26 */
47 sub_25519(x2, t1, t0); /* x2 = E < 2^26 */
48 mul_25519(z3, xp, z2); /* z3 = Z5 < 2^26 */
49 mul_25519(z2, x2, nr_121666); /* z2 = a24*E < 2^26 */
50 add32(z2, t0, z2); /* z2 = BB+a24*E < 2^27 */
51 mul_25519(z2, x2, z2); /* z2 = Z4 < 2^26 */
52 mul_25519(x2, t1, t0); /* x2 = X4 < 2^26 */
53 }
54
55 /*
56 * Variable-base scalar multiplication on Curve25519.
57 *
58 * @param[out] ssecret The X-coordinate of the resulting point.
59 * @param[in] k The scalar encoded in little-endian mode.
60 * It must have been already clamped.
61 * @param[in] len Length of the scalar in bytes.
62 * @param[in] pubkey The X-coordinate of the point to multiply, encoded in
63 * little-endian mode.
64 */
65 void ladder(uint8_t ssecret[32], const uint8_t *k, size_t len, const uint8_t pubkey[32])
66 {
67 uint32_t R0x[10] = { 1 };
68 uint32_t R0z[10] = { 0 };
69 uint32_t R1x[10];
70 uint32_t R1z[10] = { 1 };
71 uint32_t xp[10];
72 uint32_t invz[10];
73 uint32_t affx[10];
74 uint64_t tmp_64[4];
75 unsigned bit_idx, swap;
76 unsigned i;
77
78 for (i=0; i<4; i++) {
79 tmp_64[i] = LOAD_U64_LITTLE(&pubkey[i*8]);
80 }
81 convert_le64_to_le25p5(xp, tmp_64);
82
83 memcpy(R1x, xp, sizeof R1x);
84 bit_idx = 7;
85 swap = 0;
86
87 /* https://eprint.iacr.org/2020/956.pdf */
88
89 while (len>0) {
90 unsigned bit;
91
92 bit = (k[len-1] >> bit_idx) & 1;
93 swap ^= bit;
94
95 cswap(R0x, R0z, R1x, R1z, swap);
96 ladder_step(R0x, R0z, R1x, R1z, xp);
97 swap = bit;
98
99 if (bit_idx-- == 0) {
100 bit_idx = 7;
101 len--;
102 }
103 }
104 cswap(R0x, R0z, R1x, R1z, swap);
105
106 invert_25519(invz, R0z);
107 mul_25519(affx, R0x, invz);
108 convert_le25p5_to_le64(tmp_64, affx);
109 reduce_25519_le64(tmp_64);
110
111 for (i=0; i<4; i++) {
112 STORE_U64_LITTLE(&ssecret[i*8], tmp_64[i]);
113 }
114 }
115
116 #ifdef PROFILE
117 int main(void)
118 {
119 uint8_t pubkey[32];
120 uint8_t secret[32];
121 uint8_t out[32];
122 unsigned i;
123
124 secret[0] = pubkey[0] = 0xAA;
125 for (i=1; i<32; i++) {
126 secret[i] = pubkey[i] = (uint8_t)((secret[i-1] << 1) | (secret[i-1] >> 7));
127 }
128
129 for (i=0; i<10000; i++) {
130 ladder(out, secret, sizeof secret, pubkey);
131 }
132 }
133 #endif