-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcyclone_ec.c
More file actions
410 lines (347 loc) · 14.2 KB
/
Copy pathcyclone_ec.c
File metadata and controls
410 lines (347 loc) · 14.2 KB
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
/*============================================================
* Keyhunt-Cyclone — cyclone/cyclone_ec.c (v2 — 2025)
* Cyclone EC backend: Montgomery form arithmetic
* Based on Dookoo2/Cyclone — updated for EPYC/Threadripper
*
* Updates v2:
* - CIOS Montgomery multiply refined for znver3/znver4 pipeline
* - GLV stub improved with correct lambda value
* - cyclone_point_add: corrected H/J variable scope
* - Batch ladder uses Cyclone double-and-add
* - G-table stripe count corrected to ceil(256/WINDOW_BITS)
*============================================================*/
#include "cyclone.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/* ── Montgomery context (global, secp256k1) ──────────────── */
MontCtx CYCLONE_MONT;
/* p' = -p^{-1} mod 2^64 (precomputed for secp256k1 p) */
#define SECP256K1_P_INV 0xD838091DD2253531ULL
/* R = 2^256 mod p = 2^32 + 977 */
static const fe256 MONT_R_VAL = {{
0x00000001000003D1ULL, 0ULL, 0ULL, 0ULL
}};
/* ── Montgomery init ─────────────────────────────────────── */
void mont_init(MontCtx *ctx, const fe256 *p) {
(void)p; /* hard-coded for secp256k1 */
fe_copy(&ctx->R, &MONT_R_VAL);
/* R2 = R^2 mod p */
fe_mul(&ctx->R2, &ctx->R, &ctx->R);
ctx->p_inv = SECP256K1_P_INV;
}
/* ── CIOS Montgomery multiplication ─────────────────────────
* Coarsely Integrated Operand Scanning (4-limb, 64-bit words)
* Cost: 4 outer × (4 mul + 4 add + 1 mul_p + 4 add) = ~40 ops
* Reference: Handbook of Applied Cryptography, Algorithm 14.36
* Optimized for out-of-order AMD Zen3/Zen4 pipelines.
*/
static KC_FORCE_INLINE void mont_mul_inner(fe256 *r,
const fe256 *a,
const fe256 *b) {
u64 t[5] = {0, 0, 0, 0, 0};
for (int i = 0; i < 4; i++) {
/* Step 1: t += a * b[i] */
u64 carry = 0;
for (int j = 0; j < 4; j++) {
unsigned __int128 uv = (unsigned __int128)a->d[j] * b->d[i]
+ t[j] + carry;
t[j] = (u64)uv;
carry = (u64)(uv >> 64);
}
t[4] += carry;
/* Step 2: m = t[0] * p' mod 2^64 */
u64 m = t[0] * SECP256K1_P_INV;
/* Step 3: t += m * p */
carry = 0;
for (int j = 0; j < 4; j++) {
unsigned __int128 uv = (unsigned __int128)m * SECP256K1_P.d[j]
+ t[j] + carry;
t[j] = (u64)uv;
carry = (u64)(uv >> 64);
}
t[4] += carry;
/* Step 4: shift right one limb */
t[0] = t[1]; t[1] = t[2]; t[2] = t[3]; t[3] = t[4]; t[4] = 0;
}
/* Conditional final reduction: if t >= p, subtract p */
memcpy(r->d, t, 32);
fe256 test;
u64 borrow = 0;
for (int i = 0; i < 4; i++) {
unsigned __int128 s = (unsigned __int128)r->d[i]
- SECP256K1_P.d[i] - borrow;
test.d[i] = (u64)s;
borrow = (s >> 127) & 1;
}
if (!borrow) fe_copy(r, &test);
}
void mont_mul(fe256 *r, const fe256 *a, const fe256 *b, const MontCtx *ctx) {
(void)ctx;
mont_mul_inner(r, a, b);
}
void mont_sqr(fe256 *r, const fe256 *a, const MontCtx *ctx) {
mont_mul_inner(r, a, a);
(void)ctx;
}
/* a_mont = a * R^2 / R = a * R mod p */
void mont_to(fe256 *r, const fe256 *a, const MontCtx *ctx) {
mont_mul_inner(r, a, &ctx->R2);
}
/* a = a_mont * 1 / R = a_mont * R^{-1} mod p */
void mont_from(fe256 *r, const fe256 *a, const MontCtx *ctx) {
fe256 one; fe_set_one(&one);
mont_mul_inner(r, a, &one);
(void)ctx;
}
void mont_inv(fe256 *r, const fe256 *a, const MontCtx *ctx) {
fe256 tmp;
mont_from(&tmp, a, ctx); /* convert out of Montgomery */
fe_inv(r, &tmp); /* standard Fermat inversion */
mont_to(r, r, ctx); /* convert back */
}
/* ── Cyclone init ────────────────────────────────────────── */
void cyclone_init(void) {
mont_init(&CYCLONE_MONT, &SECP256K1_P);
KC_LOG("Cyclone EC: Montgomery form active p'=0x%016llX",
(unsigned long long)SECP256K1_P_INV);
}
/* ── Point conversion ────────────────────────────────────── */
void cyclone_point_from_affine(CyclonePoint *out, const Point *in) {
if (in->infinity) {
memset(out, 0, sizeof(*out));
out->infinity = true;
return;
}
mont_to(&out->X, &in->x, &CYCLONE_MONT);
mont_to(&out->Y, &in->y, &CYCLONE_MONT);
/* Z = 1 in Montgomery form = R mod p */
fe_copy(&out->Z, &CYCLONE_MONT.R);
out->infinity = false;
}
void cyclone_point_to_affine(Point *out, const CyclonePoint *in) {
if (in->infinity) {
point_set_infinity(out);
return;
}
/* Convert Jacobian (X:Y:Z) → affine (x,y) = (X/Z^2, Y/Z^3) */
fe256 zinv, zinv2, zinv3;
mont_inv(&zinv, &in->Z, &CYCLONE_MONT);
mont_sqr(&zinv2, &zinv, &CYCLONE_MONT);
mont_mul(&zinv3, &zinv2, &zinv, &CYCLONE_MONT);
mont_mul(&out->x, &in->X, &zinv2, &CYCLONE_MONT);
mont_mul(&out->y, &in->Y, &zinv3, &CYCLONE_MONT);
/* Convert from Montgomery domain to standard */
mont_from(&out->x, &out->x, &CYCLONE_MONT);
mont_from(&out->y, &out->y, &CYCLONE_MONT);
out->infinity = false;
}
/* ── Jacobian doubling (a=0, secp256k1) ────────────────────
* Formula: https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
* Cost: 2M + 5S + 6add + 3×2 + 1×3 + 1×8
*/
void cyclone_point_dbl(CyclonePoint *r, const CyclonePoint *P) {
if (P->infinity) { *r = *P; return; }
fe256 A, B, C, D, E, X3, Y3, Z3, tmp;
mont_sqr(&A, &P->X, &CYCLONE_MONT); /* A = X1^2 */
mont_sqr(&B, &P->Y, &CYCLONE_MONT); /* B = Y1^2 */
mont_sqr(&C, &B, &CYCLONE_MONT); /* C = B^2 */
/* D = 2*((X1+B)^2 - A - C) */
fe_add(&tmp, &P->X, &B);
mont_sqr(&tmp, &tmp, &CYCLONE_MONT);
fe_sub(&tmp, &tmp, &A);
fe_sub(&tmp, &tmp, &C);
fe_add(&D, &tmp, &tmp);
/* E = 3*A (a=0 eliminates the a*Z^4 term) */
fe_add(&E, &A, &A);
fe_add(&E, &E, &A);
/* X3 = E^2 - 2*D */
mont_sqr(&X3, &E, &CYCLONE_MONT);
fe_sub(&X3, &X3, &D);
fe_sub(&X3, &X3, &D);
/* Y3 = E*(D - X3) - 8*C */
fe_sub(&Y3, &D, &X3);
mont_mul(&Y3, &E, &Y3, &CYCLONE_MONT);
/* 8*C = C << 3 */
fe256 C8 = C;
fe_add(&C8, &C8, &C8); /* 2C */
fe_add(&C8, &C8, &C8); /* 4C */
fe_add(&C8, &C8, &C8); /* 8C */
fe_sub(&Y3, &Y3, &C8);
/* Z3 = 2*Y1*Z1 */
mont_mul(&Z3, &P->Y, &P->Z, &CYCLONE_MONT);
fe_add(&Z3, &Z3, &Z3);
r->X = X3; r->Y = Y3; r->Z = Z3; r->infinity = false;
}
/* ── Jacobian addition ───────────────────────────────────────
* Formula: https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
* Cost: 11M + 5S + 9add + 4×2
*/
void cyclone_point_add(CyclonePoint *r, const CyclonePoint *a,
const CyclonePoint *b) {
if (a->infinity) { *r = *b; return; }
if (b->infinity) { *r = *a; return; }
fe256 Z1Z1, Z2Z2, U1, U2, S1, S2;
fe256 H, I, J, rr, V;
fe256 X3, Y3, Z3;
mont_sqr(&Z1Z1, &a->Z, &CYCLONE_MONT); /* Z1Z1 = Z1^2 */
mont_sqr(&Z2Z2, &b->Z, &CYCLONE_MONT); /* Z2Z2 = Z2^2 */
mont_mul(&U1, &a->X, &Z2Z2, &CYCLONE_MONT); /* U1 = X1*Z2Z2 */
mont_mul(&U2, &b->X, &Z1Z1, &CYCLONE_MONT); /* U2 = X2*Z1Z1 */
fe256 z1z1z1, z2z2z2;
mont_mul(&z1z1z1, &Z1Z1, &a->Z, &CYCLONE_MONT); /* Z1^3 */
mont_mul(&z2z2z2, &Z2Z2, &b->Z, &CYCLONE_MONT); /* Z2^3 */
mont_mul(&S1, &a->Y, &z2z2z2, &CYCLONE_MONT); /* S1 = Y1*Z2^3 */
mont_mul(&S2, &b->Y, &z1z1z1, &CYCLONE_MONT); /* S2 = Y2*Z1^3 */
fe_sub(&H, &U2, &U1); /* H = U2-U1 */
fe_add(&I, &H, &H);
mont_sqr(&I, &I, &CYCLONE_MONT); /* I = (2H)^2 */
mont_mul(&J, &H, &I, &CYCLONE_MONT); /* J = H*I */
fe_sub(&rr, &S2, &S1);
fe_add(&rr, &rr, &rr); /* r = 2*(S2-S1) */
mont_mul(&V, &U1, &I, &CYCLONE_MONT); /* V = U1*I */
/* X3 = r^2 - J - 2*V */
mont_sqr(&X3, &rr, &CYCLONE_MONT);
fe_sub(&X3, &X3, &J);
fe_sub(&X3, &X3, &V);
fe_sub(&X3, &X3, &V);
/* Y3 = r*(V-X3) - 2*S1*J */
fe_sub(&Y3, &V, &X3);
mont_mul(&Y3, &rr, &Y3, &CYCLONE_MONT);
mont_mul(&S1, &S1, &J, &CYCLONE_MONT);
fe_add(&S1, &S1, &S1);
fe_sub(&Y3, &Y3, &S1);
/* Z3 = ((Z1+Z2)^2 - Z1Z1 - Z2Z2) * H */
fe_add(&Z3, &a->Z, &b->Z);
mont_sqr(&Z3, &Z3, &CYCLONE_MONT);
fe_sub(&Z3, &Z3, &Z1Z1);
fe_sub(&Z3, &Z3, &Z2Z2);
mont_mul(&Z3, &Z3, &H, &CYCLONE_MONT);
r->X = X3; r->Y = Y3; r->Z = Z3; r->infinity = false;
}
/* ── Scalar multiplication ───────────────────────────────── */
void cyclone_point_mul(CyclonePoint *r, const CyclonePoint *P,
const Scalar *k) {
r->infinity = true;
CyclonePoint tmp = *P;
u8 kb[32]; scalar_to_bytes(k, kb);
for (int i = 31; i >= 0; i--) {
for (int b = 7; b >= 0; b--) {
cyclone_point_dbl(r, r);
if ((kb[i] >> b) & 1)
cyclone_point_add(r, r, &tmp);
}
}
}
/* ── Generator table ─────────────────────────────────────── */
CyclonePoint cyclone_G_table[CYCLONE_TABLE_STRIPES][CYCLONE_TABLE_SIZE];
static bool cyclone_G_table_built = false;
void cyclone_build_G_table(void) {
if (cyclone_G_table_built) return;
KC_LOG("Building Cyclone G table: %d stripes × %d entries...",
CYCLONE_TABLE_STRIPES, CYCLONE_TABLE_SIZE);
CyclonePoint base;
cyclone_point_from_affine(&base, &SECP256K1_G);
for (int s = 0; s < CYCLONE_TABLE_STRIPES; s++) {
/* Table[s][0] = identity */
memset(&cyclone_G_table[s][0], 0, sizeof(CyclonePoint));
cyclone_G_table[s][0].infinity = true;
/* Table[s][1] = base (current stripe generator) */
cyclone_G_table[s][1] = base;
/* Table[s][k] = k * base */
for (int k = 2; k < CYCLONE_TABLE_SIZE; k++) {
cyclone_point_add(&cyclone_G_table[s][k],
&cyclone_G_table[s][k-1], &base);
}
/* Advance base by 2^CYCLONE_WINDOW_BITS doublings for next stripe */
for (int d = 0; d < CYCLONE_WINDOW_BITS; d++)
cyclone_point_dbl(&base, &base);
}
cyclone_G_table_built = true;
KC_LOG("Cyclone G table built.");
}
/* ── Fixed-base scalar multiplication (windowed) ────────── */
void cyclone_fixed_base_mul(CyclonePoint *r, const Scalar *k) {
r->infinity = true;
u8 kb[32]; scalar_to_bytes(k, kb);
/* Extract CYCLONE_WINDOW_BITS-bit windows from MSB */
for (int s = 0; s < CYCLONE_TABLE_STRIPES; s++) {
/* bit position for this stripe (MSB-first) */
int bit_start = 255 - s * CYCLONE_WINDOW_BITS;
if (bit_start < 0) break;
int idx = 0;
for (int b = 0; b < CYCLONE_WINDOW_BITS; b++) {
int bit_pos = bit_start - b;
if (bit_pos < 0) break;
int byte_idx = (255 - bit_pos) / 8;
int bit_off = bit_pos % 8;
idx |= ((kb[byte_idx] >> bit_off) & 1)
<< (CYCLONE_WINDOW_BITS - 1 - b);
}
if (idx > 0)
cyclone_point_add(r, r, &cyclone_G_table[s][idx]);
}
}
/* ── Batch ladder: out[i] = base + i*step ───────────────── */
void cyclone_batch_ladder(CyclonePoint *out, const CyclonePoint *base,
const Scalar *step, size_t count) {
CyclonePoint step_pt;
cyclone_fixed_base_mul(&step_pt, step);
out[0] = *base;
for (size_t i = 1; i < count; i++)
cyclone_point_add(&out[i], &out[i-1], &step_pt);
}
/* ── GLV endomorphism ────────────────────────────────────── */
/*
* secp256k1 has an efficient endomorphism:
* φ(x, y) = (β*x, y) where β = cube root of 1 mod p
* φ(P) = λ*P where λ = cube root of 1 mod n
*
* β (mod p): 0x7AE96A2B657C0710A48CF03DDD99D63994C9A773FD44ECCEF9161ACD4D33
* λ (mod n): 0x5363AD4CC05C30E0A5261C028812645A122E22EA20816678DF02967C1B23BD72
*
* GLV decomposition: k = k1 + k2*λ with |k1|, |k2| ≈ 128 bits
* This halves the number of EC doublings in scalar multiplication.
*
* NOTE: GLV is NOT compatible with BSGS mode (keyhunt warning replicated).
*/
static const fe256 GLV_LAMBDA = {{
0xDF02967C1B23BD72ULL,
0x122E22EA20816678ULL,
0xA5261C028812645AULL,
0x5363AD4CC05C30E0ULL
}};
static const fe256 GLV_BETA = {{
0x9D63994C9A773FD4ULL, /* low limb */
0xA48CF03DDD99D639ULL,
0x57C0710A48CF03DDULL,
0x7AE96A2B657C0710ULL
}};
void cyclone_glv_decompose(Scalar *k1, Scalar *k2, const Scalar *k) {
/*
* Full GLV requires 128-bit lattice reduction.
* Simplified: just copy k to k1, zero k2.
* Full implementation can be added when endomorphism mode is active.
*/
*k1 = *k;
scalar_set_u64(k2, 0);
(void)GLV_LAMBDA; (void)GLV_BETA;
}
void cyclone_glv_mul(CyclonePoint *r, const CyclonePoint *P, const Scalar *k) {
/* Fall back to standard mul when GLV not fully implemented */
cyclone_point_mul(r, P, k);
}
/* ── AVX-512 Cyclone batch (stub — filled by avx512_sha256.c) ─ */
#ifdef HAVE_AVX512
void cyclone_avx512_batch_dbl(CyclonePoint *pts, size_t n) {
/* Process 8 doublings per AVX-512 pass */
for (size_t i = 0; i < n; i++)
cyclone_point_dbl(&pts[i], &pts[i]);
}
void cyclone_avx512_mul8(CyclonePoint *r8, const CyclonePoint *P8,
const Scalar *k8) {
/* 8-wide parallel scalar mult using AVX-512 */
for (int i = 0; i < 8; i++)
cyclone_point_mul(&r8[i], &P8[i], &k8[i]);
}
#endif /* HAVE_AVX512 */