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 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <pthread.h> #include <stdatomic.h> #include <time.h> #include <unistd.h> #include <inttypes.h> #include <math.h>
typedef unsigned __int128 u128; typedef struct { uint64_t a0, a1; } u80;
static inline uint64_t splitmix64(uint64_t *s) { uint64_t z = (*s += 0x9E3779B97F4A7C15ULL); z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; return z ^ (z >> 31); }
static inline int u80_ge(u80 a, u80 b) { return a.a1 > b.a1 || (a.a1 == b.a1 && a.a0 >= b.a0); } static inline int u80_eq(u80 a, u80 b) { return a.a0 == b.a0 && a.a1 == b.a1; }
typedef struct { u80 m; uint64_t mp0; u80 R2; } Fld;
static inline u80 f_subraw(u80 a, u80 b) { uint64_t br = (a.a0 < b.a0); u80 r = { a.a0 - b.a0, a.a1 - b.a1 - br }; return r; } static inline u80 f_add(const Fld *F, u80 a, u80 b) { uint64_t lo = a.a0 + b.a0; uint64_t c = (lo < a.a0); uint64_t hi = a.a1 + b.a1 + c; u80 s = { lo, hi }; return u80_ge(s, F->m) ? f_subraw(s, F->m) : s; } static inline u80 f_sub(const Fld *F, u80 a, u80 b) { if (u80_ge(a, b)) return f_subraw(a, b); uint64_t br = (F->m.a0 < b.a0); u80 nb = { F->m.a0 - b.a0, F->m.a1 - b.a1 - br }; return f_add(F, a, nb); } static inline u80 f_mul(const Fld *F, u80 a, u80 b) { u128 t0 = (u128)a.a0 * b.a0; u128 t1 = (u128)a.a0 * b.a1; u128 t2 = (u128)a.a1 * b.a0; u128 t3 = (u128)a.a1 * b.a1; uint64_t r0 = (uint64_t)t0; u128 xs = (t0 >> 64) + (uint64_t)t1 + (uint64_t)t2; uint64_t r1 = (uint64_t)xs; uint64_t r2 = (uint64_t)((xs >> 64) + (t1 >> 64) + (t2 >> 64) + t3); for (int it = 0; it < 2; it++) { uint64_t k = r0 * F->mp0; u128 pl = (u128)F->m.a0 * k; u128 ph = (u128)F->m.a1 * k; uint64_t pl0 = (uint64_t)pl, pl1 = (uint64_t)(pl >> 64); uint64_t ph0 = (uint64_t)ph, ph1 = (uint64_t)(ph >> 64); uint64_t c0 = (r0 + pl0 < r0); u128 mid = (u128)r1 + pl1 + ph0 + c0; uint64_t nr1 = (uint64_t)mid; uint64_t nr2 = r2 + ph1 + (uint64_t)(mid >> 64); r0 = nr1; r1 = nr2; r2 = 0; } u80 res = { r0, r1 }; return u80_ge(res, F->m) ? f_subraw(res, F->m) : res; } static void fld_init(Fld *F, u80 m) { F->m = m; uint64_t inv = m.a0; for (int i = 0; i < 6; i++) inv *= 2 - m.a0 * inv; F->mp0 = 0ULL - inv; u80 r = { 1, 0 }; for (int i = 0; i < 256; i++) r = f_add(F, r, r); F->R2 = r; } static inline u80 f_tomont(const Fld *F, u80 a) { return f_mul(F, a, F->R2); } static inline u80 f_frommont(const Fld *F, u80 a) { return f_mul(F, a, (u80){1, 0}); } static u80 f_pow(const Fld *F, u80 base, u80 e) { u80 r = f_tomont(F, (u80){1, 0}); for (int i = 79; i >= 0; i--) { r = f_mul(F, r, r); int bit = (i < 64) ? (int)((e.a0 >> i) & 1) : (int)((e.a1 >> (i - 64)) & 1); if (bit) r = f_mul(F, r, base); } return r; } static inline u80 f_inv(const Fld *F, u80 a) { return f_pow(F, a, f_sub(F, F->m, (u80){2, 0})); }
typedef struct { u80 x, y; int inf; } Pt;
static Fld FP, FN; static u80 A_m, B_m, THREE_M; static Pt Gp, Qp; static Pt PT_INF;
static Pt pt_add(const Fld *F, Pt P, Pt T) { if (P.inf) return T; if (T.inf) return P; if (u80_eq(P.x, T.x)) { if (u80_eq(P.y, T.y)) { if ((P.y.a0 | P.y.a1) == 0) return PT_INF; u80 num = f_add(F, f_mul(F, f_mul(F, P.x, P.x), THREE_M), A_m); u80 den = f_add(F, P.y, P.y); u80 lam = f_mul(F, num, f_inv(F, den)); u80 x3 = f_sub(F, f_mul(F, lam, lam), f_add(F, P.x, P.x)); u80 y3 = f_sub(F, f_mul(F, lam, f_sub(F, P.x, x3)), P.y); Pt r = { x3, y3, 0 }; return r; } return PT_INF; } u80 lam = f_mul(F, f_sub(F, T.y, P.y), f_inv(F, f_sub(F, T.x, P.x))); u80 x3 = f_sub(F, f_sub(F, f_mul(F, lam, lam), P.x), T.x); u80 y3 = f_sub(F, f_mul(F, lam, f_sub(F, P.x, x3)), P.y); Pt r = { x3, y3, 0 }; return r; }
static Pt pt_mul(const Fld *F, Pt P, u80 k) { Pt R = PT_INF; for (int i = 79; i >= 0; i--) { R = pt_add(F, R, R); int bit = (i < 64) ? (int)((k.a0 >> i) & 1) : (int)((k.a1 >> (i - 64)) & 1); if (bit) R = pt_add(F, R, P); } return R; }
#define NT 32 static u80 Tx[NT], Ty[NT]; static u80 Td[NT];
static u80 rand_mod_n(uint64_t *rng) { u128 v = (u128)splitmix64(rng) << 64 | splitmix64(rng); u128 m = ((u128)FN.m.a1 << 64) | FN.m.a0; u128 r = v % m; u80 out = { (uint64_t)r, (uint64_t)(r >> 64) }; if (!out.a0 && !out.a1) out.a0 = 1; return out; }
static void tbl_init(void) { uint64_t rng = 0xDEADBEEFCAFEBABEULL; for (int j = 0; j < NT; j++) { u80 d = rand_mod_n(&rng); Pt T = pt_mul(&FP, Gp, d); if (j >= 16) T = pt_add(&FP, T, Qp); Tx[j] = T.x; Ty[j] = T.y; Td[j] = f_tomont(&FN, d); } }
#define TBITS 18 #define TSIZE (1u << TBITS) typedef struct { uint64_t xlo, xhi; uint64_t clo, chi; uint64_t al; uint8_t ypar; uint8_t used; } DEnt;
static DEnt *dtab; static pthread_mutex_t tabmux = PTHREAD_MUTEX_INITIALIZER;
static atomic_int won; static atomic_ullong total_hops; static uint64_t dp_mask; static long double expected_total; static u80 a_solved;
static void print_u80_hex(const char *tag, u80 v) { if (v.a1) printf("%s%llx%016llx\n", tag, (unsigned long long)v.a1, (unsigned long long)v.a0); else printf("%s%llx\n", tag, (unsigned long long)v.a0); }
static void try_dp(u80 x, u80 y, u80 c, uint64_t al) { uint8_t ypar = (uint8_t)(y.a0 & 1); uint64_t h = x.a0 * 0x9E3779B97F4A7C15ULL ^ x.a1 * 0xC2B2AE3D27D4EB4FULL; uint64_t idx = (h >> 44) & (TSIZE - 1); pthread_mutex_lock(&tabmux); for (int probes = 0; probes < 4096; probes++, idx = (idx + 1) & (TSIZE - 1)) { DEnt *e = &dtab[idx]; if (!e->used) { e->xlo = x.a0; e->xhi = x.a1; e->clo = c.a0; e->chi = c.a1; e->al = al; e->ypar = ypar; e->used = 1; break; } if (e->xlo == x.a0 && e->xhi == x.a1) { if (e->al != al || e->clo != c.a0 || e->chi != c.a1) { u80 c1 = { e->clo, e->chi }; uint64_t al1 = e->al; int ok = 0; u80 a; if (e->ypar == ypar) { int64_t d = (int64_t)al1 - (int64_t)al; if (d != 0) { u80 den = (d > 0) ? (u80){ (uint64_t)d, 0 } : f_sub(&FN, FN.m, (u80){ (uint64_t)(-d), 0 }); u80 num = f_sub(&FN, c, c1); a = f_frommont(&FN, f_mul(&FN, num, f_inv(&FN, f_tomont(&FN, den)))); ok = 1; } } else { uint64_t s = al1 + al; if (s != 0) { u80 num = f_sub(&FN, FN.m, f_add(&FN, c, c1)); a = f_frommont(&FN, f_mul(&FN, num, f_inv(&FN, f_tomont(&FN, (u80){ s, 0 })))); ok = 1; } } if (ok) { Pt R = pt_mul(&FP, Gp, a); if (!R.inf && u80_eq(R.x, Qp.x) && u80_eq(R.y, Qp.y)) { a_solved = a; atomic_store(&won, 1); printf("\n*** SOLVED ***\n"); print_u80_hex("a_hex = ", a); fflush(stdout); } } } break; } } pthread_mutex_unlock(&tabmux); }
typedef struct { int tid; int W; } TArg;
static void *worker(void *varg) { TArg *arg = (TArg *)varg; const int W = arg->W; const Fld F = FP; const Fld Fn = FN; uint64_t rng = 0x243F6A8885A308D3ULL ^ ((uint64_t)arg->tid * 0x13198A2E03707344ULL) ^ ((uint64_t)time(NULL) * 0xA4093822299F31D0ULL);
u80 *X = malloc(W * sizeof(u80)); u80 *Y = malloc(W * sizeof(u80)); u80 *C = malloc(W * sizeof(u80)); u80 *dx = malloc(W * sizeof(u80)); u80 *dy = malloc(W * sizeof(u80)); u80 *iv = malloc(W * sizeof(u80)); u80 *pre = malloc(W * sizeof(u80)); uint64_t *AL = malloc(W * sizeof(uint64_t)); int *idx = malloc(W * sizeof(int));
for (int w = 0; w < W; w++) { u80 cw = rand_mod_n(&rng); Pt S = pt_mul(&F, Gp, cw); if (S.inf) { w--; continue; } X[w] = S.x; Y[w] = S.y; C[w] = f_tomont(&Fn, cw); AL[w] = 0; }
uint64_t since = 0; while (!atomic_load_explicit(&won, memory_order_relaxed)) { for (int w = 0; w < W; w++) {
int j = (int)((X[w].a0 >> 32) & 31u); idx[w] = j; dx[w] = f_sub(&F, Tx[j], X[w]); dy[w] = f_sub(&F, Ty[j], Y[w]); } pre[0] = dx[0]; for (int w = 1; w < W; w++) pre[w] = f_mul(&F, pre[w - 1], dx[w]); u80 acc = f_inv(&F, pre[W - 1]); for (int w = W - 1; w >= 1; w--) { iv[w] = f_mul(&F, acc, pre[w - 1]); acc = f_mul(&F, acc, dx[w]); } iv[0] = acc; for (int w = 0; w < W; w++) { int j = idx[w]; u80 lam = f_mul(&F, dy[w], iv[w]); u80 x3 = f_sub(&F, f_sub(&F, f_mul(&F, lam, lam), X[w]), Tx[j]); u80 y3 = f_sub(&F, f_mul(&F, lam, f_sub(&F, X[w], x3)), Y[w]); X[w] = x3; Y[w] = y3; C[w] = f_add(&Fn, C[w], Td[j]); AL[w] += (uint64_t)(j >> 4); if ((X[w].a0 & dp_mask) == 0) try_dp(X[w], Y[w], C[w], AL[w]); } since += (uint64_t)W; if (since >= (1ULL << 24)) { atomic_fetch_add_explicit(&total_hops, since, memory_order_relaxed); since = 0; } } atomic_fetch_add_explicit(&total_hops, since, memory_order_relaxed);
free(X); free(Y); free(C); free(dx); free(dy); free(iv); free(pre); free(AL); free(idx); return NULL; }
static u80 parse80(const char *s) { u128 v = 0; for (const char *c = s; *c; c++) { int d; if (*c >= '0' && *c <= '9') d = *c - '0'; else if (*c >= 'a' && *c <= 'f') d = *c - 'a' + 10; else if (*c >= 'A' && *c <= 'F') d = *c - 'A' + 10; else break; v = (v << 4) | (u128)d; } u80 r = { (uint64_t)v, (uint64_t)(v >> 64) }; return r; }
typedef struct { char name[64]; u80 p, n, A, B, gx, gy, qx, qy, secret; } Inst;
static int load_inst(const char *path, Inst *I) { FILE *f = fopen(path, "r"); if (!f) { perror("open"); return 0; } char line[4096]; if (!fgets(line, sizeof line, f)) { fclose(f); return 0; } fclose(f); char *tok[12]; int nt = 0; for (char *t = strtok(line, " \t\r\n"); t && nt < 12; t = strtok(NULL, " \t\r\n")) tok[nt++] = t; if (nt < 9) { fprintf(stderr, "bad instance (need >=9 tokens)\n"); return 0; } snprintf(I->name, sizeof I->name, "%s", tok[0]); I->p = parse80(tok[1]); I->n = parse80(tok[2]); I->A = parse80(tok[3]); I->B = parse80(tok[4]); I->gx = parse80(tok[5]); I->gy = parse80(tok[6]); I->qx = parse80(tok[7]); I->qy = parse80(tok[8]); I->secret = nt >= 10 ? parse80(tok[9]) : (u80){0, 0}; return 1; }
static int on_curve(const Fld *F, u80 x, u80 y) { u80 lhs = f_mul(F, y, y); u80 rhs = f_add(F, f_add(F, f_mul(F, f_mul(F, x, x), x), f_mul(F, A_m, x)), B_m); return u80_eq(lhs, rhs); }
static int selftest(const Inst *I) { fld_init(&FP, I->p); fld_init(&FN, I->n); A_m = f_tomont(&FP, I->A); B_m = f_tomont(&FP, I->B); THREE_M = f_tomont(&FP, (u80){3, 0}); PT_INF.inf = 1; int fail = 0; uint64_t rng = 1; for (int i = 0; i < 500; i++) { u80 a = { splitmix64(&rng) >> 24, 0 }; u80 b = { splitmix64(&rng) >> 24, 0 }; if (u80_ge(a, FP.m) || u80_ge(b, FP.m)) { i--; continue; } u128 prod = (u128)a.a0 * b.a0; u128 mod = ((u128)FP.m.a1 << 64) | FP.m.a0; u128 ref = prod % mod; u80 got = f_frommont(&FP, f_mul(&FP, f_tomont(&FP, a), f_tomont(&FP, b))); u80 refu = { (uint64_t)ref, (uint64_t)(ref >> 64) }; if (!u80_eq(got, refu)) { printf("MUL FAIL %d\n", i); fail = 1; break; } u128 sum = (u128)a.a0 + b.a0; u128 refa = sum % mod; u80 gota = f_add(&FP, a, b); u80 refua = { (uint64_t)refa, (uint64_t)(refa >> 64) }; if (!u80_eq(gota, refua)) { printf("ADD FAIL %d\n", i); fail = 1; break; } u80 gotb = f_add(&FP, f_sub(&FP, a, b), b); if (!u80_eq(gotb, a)) { printf("SUB FAIL %d\n", i); fail = 1; break; } } printf("field selftest: %s\n", fail ? "FAIL" : "PASS"); Gp.x = f_tomont(&FP, I->gx); Gp.y = f_tomont(&FP, I->gy); Gp.inf = 0; Qp.x = f_tomont(&FP, I->qx); Qp.y = f_tomont(&FP, I->qy); Qp.inf = 0; printf("G on curve: %d\n", on_curve(&FP, Gp.x, Gp.y)); printf("Q on curve: %d\n", on_curve(&FP, Qp.x, Qp.y)); Pt nG = pt_mul(&FP, Gp, I->n); printf("n*G == O: %d\n", nG.inf); return !fail && on_curve(&FP, Gp.x, Gp.y) && on_curve(&FP, Qp.x, Qp.y) && nG.inf; }
int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "usage: %s <instance> <threads> [dpbits] [W] | %s selftest <instance>\n", argv[0], argv[0]); return 1; } Inst I; if (!load_inst(argv[argc - 1 < 2 ? 1 : (strcmp(argv[1], "selftest") == 0 ? 2 : 1)], &I)) return 1; if (strcmp(argv[1], "selftest") == 0) { return selftest(&I) ? 0 : 1; } int threads = atoi(argv[2]); if (threads < 1) threads = 1; int W = (argc >= 5) ? atoi(argv[4]) : 256; if (W < 8) W = 8;
fld_init(&FP, I.p); fld_init(&FN, I.n); A_m = f_tomont(&FP, I.A); B_m = f_tomont(&FP, I.B); THREE_M = f_tomont(&FP, (u80){3, 0}); PT_INF.inf = 1; Gp.x = f_tomont(&FP, I.gx); Gp.y = f_tomont(&FP, I.gy); Gp.inf = 0; Qp.x = f_tomont(&FP, I.qx); Qp.y = f_tomont(&FP, I.qy); Qp.inf = 0;
if (!on_curve(&FP, Gp.x, Gp.y) || !on_curve(&FP, Qp.x, Qp.y)) { fprintf(stderr, "instance failed on-curve check\n"); return 1; }
long double nld = (long double)I.n.a1 * 18446744073709551616.0L + (long double)I.n.a0; expected_total = sqrtl(3.14159265358979323846L * nld / 2.0L);
int dpbits = (argc >= 4) ? atoi(argv[3]) : 0; if (dpbits <= 0) { dpbits = (int)(log2l(expected_total)) - 14; if (dpbits < 8) dpbits = 8; if (dpbits > 26) dpbits = 26; } dp_mask = (1ULL << dpbits) - 1;
tbl_init(); dtab = calloc(TSIZE, sizeof(DEnt));
printf("instance=%s threads=%d W=%d dpbits=%d\n", I.name, threads, W, dpbits); print_u80_hex("p = ", I.p); print_u80_hex("n = ", I.n); printf("expected hops ~ %.4Le\n", (long double)expected_total); fflush(stdout);
pthread_t *th = malloc(threads * sizeof(pthread_t)); TArg *args = malloc(threads * sizeof(TArg)); time_t t0 = time(NULL); for (int i = 0; i < threads; i++) { args[i].tid = i; args[i].W = W; pthread_create(&th[i], NULL, worker, &args[i]); } unsigned long long last = 0; while (!atomic_load(&won)) { sleep(30); if (atomic_load(&won)) break; unsigned long long h = atomic_load(&total_hops); double el = (double)(time(NULL) - t0); double rate = el > 0 ? h / el : 0; double eta = rate > 0 ? (expected_total - (double)h) / rate : -1; printf("[t=%5.0fs] hops=%.4Le rate=%.3Le/s progress=%.2f%% eta=%.0fs\n", el, (long double)h, (long double)rate, 100.0 * (double)h / (double)expected_total, eta); fflush(stdout); (void)last; } for (int i = 0; i < threads; i++) pthread_join(th[i], NULL); unsigned long long h = atomic_load(&total_hops); printf("done in %.0fs, total hops %llu\n", (double)(time(NULL) - t0), h); print_u80_hex("a_hex = ", a_solved); if (I.secret.a0 || I.secret.a1) printf("check vs secret: %s\n", u80_eq(a_solved, I.secret) ? "MATCH" : "MISMATCH!!"); return 0; }
|