-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRCKangaroo.cpp
More file actions
2593 lines (2458 loc) · 67.5 KB
/
Copy pathRCKangaroo.cpp
File metadata and controls
2593 lines (2458 loc) · 67.5 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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is a part of RCKangaroo software
// (c) 2024, RetiredCoder (RC)
// License: GPLv3, see "LICENSE.TXT" file
// https://github.com/RetiredC
//
// Puzzle-140 research fork: checkpoint/resume, ramlimit freeze, autotune, compact DP.
// + Entropy140 / LinkSolve / TagTrace / VarianceForge / LotteryHerd / DualLane
// + 3.3: StreamTrio/L2Pin/WarpDP/JumpDNA/herd knobs/sidechannels/hybrid/FoundShield/Advise
// + 3.4: JumpDNA full multi-gen + TagTrace GPU implant
// + 3.5: TrueCollider inventions pack (20)
// + 3.6: AI-imagination echo-modes pack (25)
#include <iostream>
#include <vector>
#include <math.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "cuda_runtime.h"
#include "cuda.h"
#include "defs.h"
#include "utils.h"
#include "GpuKang.h"
#include "autotune.h"
#include "netherd.h"
#include "entropy140.h"
#include "linksolve.h"
#include "tagtrace.h"
#include "p140ops.h"
#include "sidechannel.h"
#include "hybridmath.h"
#include "inventions.h"
#include "echomodes.h"
EcJMP EcJumps1[JMP_CNT];
EcJMP EcJumps2[JMP_CNT];
EcJMP EcJumps3[JMP_CNT];
RCGpuKang* GpuKangs[MAX_GPU_CNT];
int GpuCnt;
volatile long ThrCnt;
volatile bool gSolved;
EcInt Int_HalfRange;
EcPoint Pnt_HalfRange;
EcPoint Pnt_NegHalfRange;
EcInt Int_TameOffset;
Ec ec;
CriticalSection csAddPoints;
u8* pPntList;
u8* pPntList2;
volatile int PntIndex;
TFastBase db;
EcPoint gPntToSolve;
EcInt gPrivKey;
volatile u64 TotalOps;
u32 TotalSolved;
u32 gTotalErrors;
u64 PntTotalOps;
bool IsBench;
u32 gDP;
u32 gRange;
EcInt gStart;
bool gStartSet;
EcPoint gPubKey;
u8 gGPUs_Mask[MAX_GPU_CNT];
char gTamesFileName[1024];
double gMax;
bool gGenMode; //tames generation mode
bool gIsOpsLimit;
// Fork options
bool gAutoTune = true;
bool gAutoTuneSet = false;
char gProfile[64] = "";
double gRamLimitGB = 0;
char gSaveFile[1024] = "";
char gLoadFile[1024] = "";
double gCheckpointHours = 0;
volatile bool gStopRequested = false;
volatile bool gFreezeTames = false;
int gPhase = PHASE_FILL;
int gPuzzle = 0;
bool gDP_user_set = false;
bool gRange_user_set = false;
NetHerdConfig gNetHerd;
bool gLoadCheckpoint = false;
u64 gJumpSeed = 0; // jumps always built with seed 0 for compatibility
AutoTuneResult gTune;
// New P140 capability flags
int gMode = MODE_SOTA;
bool gTagTrace = false;
bool gTagTraceSet = false;
bool gVarianceForge = true;
bool gVarianceForgeSet = false;
bool gDualLane = true;
bool gDualLaneSet = false;
int gLotteryWaves = 0; // 0 = unset; -lottery N sets waves
u64 gLotteryOps = 0; // ops budget per wave (0 = use -max)
bool gLotteryKeepDp = true; // keep DP table between waves
bool gPreserveDb = false; // lottery: don't clear DP on wave end
EntropyConfig gEntropy;
bool gEntropyTSet = false;
LinkSolveResult gLinkResult;
bool gModUserSet = false;
u64 gModM = 0;
u64 gModR = 0;
// 3.3 knobs
bool gStreamTrio = true;
bool gStreamTrioSet = false;
bool gL2Pin = true;
bool gL2PinSet = false;
bool gWarpDP = true;
bool gWarpDPSet = false;
bool gGroupSweep = false;
bool gGroupSweepSet = false;
bool gJumpDNA = false;
bool gJumpDNASet = false;
bool gWaveRoulette = false;
bool gAntiLoop = false;
bool gHerdSkew = false;
bool gSobol = false;
bool gGravity = false;
bool gFoundShield = true;
bool gFoundShieldSet = false;
bool gOrbitResolve = false;
bool gGaudry = false;
bool gAdvise = false;
bool gPocketRadar = false;
int gModFan = 0;
u64 gSideMaxCand = 0;
PocketRadarConfig gPocketCfg;
GaudryBox gGaudryBox;
AntiLoopState gAntiLoopState;
GravityCluster gGravityHost;
// 3.5 invention knobs
bool gSlipstream = true;
bool gSlipstreamSet = false;
bool gChronos = false;
bool gPhantom = false;
bool gForecast = false;
bool gMirrorWorld = false;
bool gTimeCrystal = false;
bool gJumpPoison = false;
bool gDarkRoom = false;
bool gNearMiss = false;
bool gFalseDawn = true;
bool gFalseDawnSet = false;
bool gFleetHive = false;
bool gBoneyard = false;
bool gBudget = false;
u64 gTimeCrystalSeed = 1421345234ull;
ChronosState gChronosState;
ForecastState gForecastState;
PhantomStats gPhantomStats;
int gNetHerdRole = NETHERD_ROLE_ANY;
bool gNetHerdDemo = false;
// 3.6 echo-mode knobs
bool gEchoChamber = false;
bool gShadowLedger = false;
u64 gShadowMod = 65537ull;
bool gDriftCompass = false;
bool gKeyholeSaw = false;
bool gTwinFlame = false;
bool gAfterimage = false;
bool gClaimPredator = false;
bool gDreamReplay = false;
bool gSymmetryPoison = false;
bool gBreadcrumb = false;
bool gQuantumFanfic = false;
bool gDreamtime = false;
bool gMobius = false;
bool gNegentropy = false;
bool gCageMatch = false;
bool gStowaway = false;
bool gApocalypse = false;
EchoChamberState gEchoState;
DriftCompassState gDriftState;
AfterimageState gAfterState;
DreamReplayState gDreamReplayState;
NegentropyState gNegentropyState;
CageMatchState gCageState;
ApocalypseState gApocalypseState;
u64 gTwinFlameH = 0;
static u64 gDpAdded = 0;
static u64 gDpTameSkipped = 0;
static u64 gLastOpsSnapshot = 0;
static u64 gLastStatsMs = 0;
void InitGpus()
{
GpuCnt = 0;
int gcnt = 0;
cudaGetDeviceCount(&gcnt);
if (gcnt > MAX_GPU_CNT)
gcnt = MAX_GPU_CNT;
if (!gcnt)
return;
int drv, rt;
cudaRuntimeGetVersion(&rt);
cudaDriverGetVersion(&drv);
char drvver[100];
sprintf(drvver, "%d.%d/%d.%d", drv / 1000, (drv % 100) / 10, rt / 1000, (rt % 100) / 10);
printf("CUDA devices: %d, CUDA driver/runtime: %s\r\n", gcnt, drvver);
cudaError_t cudaStatus;
for (int i = 0; i < gcnt; i++)
{
cudaStatus = cudaSetDevice(i);
if (cudaStatus != cudaSuccess)
{
printf("cudaSetDevice for gpu %d failed!\r\n", i);
continue;
}
if (!gGPUs_Mask[i])
continue;
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, i);
printf("GPU %d: %s, %.2f GB, %d CUs, cap %d.%d, PCI %d, L2 size: %d KB\r\n", i, deviceProp.name, ((float)(deviceProp.totalGlobalMem / (1024 * 1024))) / 1024.0f, deviceProp.multiProcessorCount, deviceProp.major, deviceProp.minor, deviceProp.pciBusID, deviceProp.l2CacheSize / 1024);
if (deviceProp.major < 6)
{
printf("GPU %d - not supported, skip\r\n", i);
continue;
}
cudaSetDeviceFlags(cudaDeviceScheduleBlockingSync);
GpuKangs[GpuCnt] = new RCGpuKang();
GpuKangs[GpuCnt]->CudaIndex = i;
GpuKangs[GpuCnt]->persistingL2CacheMaxSize = deviceProp.persistingL2CacheMaxSize;
GpuKangs[GpuCnt]->mpCnt = deviceProp.multiProcessorCount;
GpuKangs[GpuCnt]->IsOldGpu = deviceProp.l2CacheSize < 16 * 1024 * 1024;
GpuKangs[GpuCnt]->ComputeMajor = deviceProp.major;
GpuKangs[GpuCnt]->ComputeMinor = deviceProp.minor;
GpuKangs[GpuCnt]->Is5xxx = (deviceProp.major == 12);
int cm = deviceProp.major * 10 + deviceProp.minor;
if (cm == 89 || cm == 120)
{
GpuKangs[GpuCnt]->sm_inv_cnt = GpuKangs[GpuCnt]->Is5xxx
? (GpuKangs[GpuCnt]->mpCnt / 24) : (GpuKangs[GpuCnt]->mpCnt / 32);
if (!GpuKangs[GpuCnt]->sm_inv_cnt)
GpuKangs[GpuCnt]->sm_inv_cnt = 1;
printf("GPU %d: v4 turbo ASM candidate (sm_%d) — will load cubin in Prepare\r\n", i, cm);
}
else if (cm == 86)
{
printf("GPU %d: RTX Ampere sm_86 — optimized CUDA fatbin path (ASM turbo blocked: Ada ULDC)\r\n", i);
}
GpuCnt++;
}
printf("Total GPUs for work: %d\r\n", GpuCnt);
}
#ifdef _WIN32
static BOOL WINAPI ConsoleCtrlHandler(DWORD type)
{
if (type == CTRL_C_EVENT || type == CTRL_BREAK_EVENT || type == CTRL_CLOSE_EVENT)
{
printf("\r\nCtrl+C — requesting checkpoint stop...\r\n");
gStopRequested = true;
return TRUE;
}
return FALSE;
}
u32 __stdcall kang_thr_proc(void* data)
{
RCGpuKang* Kang = (RCGpuKang*)data;
Kang->Execute();
InterlockedDecrement(&ThrCnt);
return 0;
}
#else
static void UnixSignalHandler(int)
{
gStopRequested = true;
}
void* kang_thr_proc(void* data)
{
RCGpuKang* Kang = (RCGpuKang*)data;
Kang->Execute();
__sync_fetch_and_sub(&ThrCnt, 1);
return 0;
}
#endif
void AddPointsToList(u32* data, int pnt_cnt, u64 ops_cnt)
{
csAddPoints.Enter();
if (PntIndex + pnt_cnt >= MAX_CNT_LIST)
{
csAddPoints.Leave();
printf("DPs buffer overflow, some points lost, increase DP value!\r\n");
return;
}
memcpy(pPntList + GPU_DP_SIZE * PntIndex, data, pnt_cnt * GPU_DP_SIZE);
PntIndex += pnt_cnt;
PntTotalOps += ops_cnt;
csAddPoints.Leave();
}
bool Collision_SOTA(EcPoint& pnt, EcInt t, int TameType, EcInt w, int WildType, bool IsNeg)
{
if (IsNeg)
t.Neg();
if (TameType == TAME)
{
gPrivKey = t;
gPrivKey.Sub(w);
EcInt sv = gPrivKey;
gPrivKey.Add(Int_HalfRange);
EcPoint P = ec.MultiplyG(gPrivKey);
if (P.IsEqual(pnt))
return true;
gPrivKey = sv;
gPrivKey.Neg();
gPrivKey.Add(Int_HalfRange);
P = ec.MultiplyG(gPrivKey);
return P.IsEqual(pnt);
}
else
{
gPrivKey = t;
gPrivKey.Sub(w);
if (gPrivKey.data[4] >> 63)
gPrivKey.Neg();
gPrivKey.ShiftRight(1);
EcInt sv = gPrivKey;
gPrivKey.Add(Int_HalfRange);
EcPoint P = ec.MultiplyG(gPrivKey);
if (P.IsEqual(pnt))
return true;
gPrivKey = sv;
gPrivKey.Neg();
gPrivKey.Add(Int_HalfRange);
P = ec.MultiplyG(gPrivKey);
return P.IsEqual(pnt);
}
}
static u64 MixSeed(u64 x)
{
x ^= x >> 30; x *= 0xbf58476d1ce4e5b9ULL;
x ^= x >> 27; x *= 0x94d049bb133111ebULL;
x ^= x >> 31;
return x ? x : 0x9e3779b97f4a7c15ULL;
}
static void MaybeUpdateFreezePhase()
{
if (gRamLimitGB <= 0 || gGenMode)
return;
double used = db.GetRamUsedGB();
double ratio = used / gRamLimitGB;
if (!gFreezeTames && ratio >= 0.93)
{
gFreezeTames = true;
gPhase = PHASE_FREEZE;
printf("RAM limit: %.3f / %.3f GB (%.1f%%) — FREEZE new TAMEs, continue WILDs (FreezeCascade)\r\n",
used, gRamLimitGB, ratio * 100.0);
gPhase = PHASE_WILD_STORM;
printf("Phase -> WILD storm (TAME table frozen)\r\n");
}
}
void CheckNewPoints()
{
csAddPoints.Enter();
if (!PntIndex)
{
csAddPoints.Leave();
return;
}
int cnt = PntIndex;
memcpy(pPntList2, pPntList, GPU_DP_SIZE * cnt);
PntIndex = 0;
csAddPoints.Leave();
MaybeUpdateFreezePhase();
for (int i = 0; i < cnt; i++)
{
DBRec nrec;
u8* p = pPntList2 + i * GPU_DP_SIZE;
memcpy(nrec.x, p, 12);
memcpy(nrec.d, p + 16, 22);
nrec.type = gGenMode ? TAME : p[40];
if (!gGenMode && gFreezeTames && nrec.type == TAME)
{
gDpTameSkipped++;
continue;
}
u8* pref_raw = db.FindOrAddDataBlock((u8*)&nrec);
if (!pref_raw)
{
gDpAdded++;
if (gPhantom)
Phantom_NotePack(&gPhantomStats);
if (gNetHerd.enabled && !gNetHerd.is_server)
NetHerd_SubmitDP(&nrec);
continue;
}
if (gGenMode)
continue;
DBRec pref_rec;
UnpackDBRec(pref_raw, nrec.x[0], nrec.x[1], nrec.x[2], &pref_rec);
// restore full x prefix match identity for collision (use nrec.x for first 3; packed has 5 more)
memcpy(pref_rec.x, nrec.x, 3);
if (pref_rec.type == nrec.type)
{
if (pref_rec.type == TAME)
continue;
if (*(u64*)pref_rec.d == *(u64*)nrec.d)
continue;
}
if (TagTrace_CheapRejectSameDist(&pref_rec, &nrec))
continue;
EcInt w, t;
int TameType, WildType;
if (pref_rec.type != TAME)
{
memcpy(w.data, pref_rec.d, sizeof(pref_rec.d));
if (pref_rec.d[21] == 0xFF) memset(((u8*)w.data) + 22, 0xFF, 18);
memcpy(t.data, nrec.d, sizeof(nrec.d));
if (nrec.d[21] == 0xFF) memset(((u8*)t.data) + 22, 0xFF, 18);
TameType = nrec.type;
WildType = pref_rec.type;
}
else
{
memcpy(w.data, nrec.d, sizeof(nrec.d));
if (nrec.d[21] == 0xFF) memset(((u8*)w.data) + 22, 0xFF, 18);
memcpy(t.data, pref_rec.d, sizeof(pref_rec.d));
if (pref_rec.d[21] == 0xFF) memset(((u8*)t.data) + 22, 0xFF, 18);
TameType = TAME;
WildType = nrec.type;
}
bool res = Collision_SOTA(gPntToSolve, t, TameType, w, WildType, false) || Collision_SOTA(gPntToSolve, t, TameType, w, WildType, true);
if (!res)
{
EcInt dummy;
if (CompactResolve_Try(&pref_rec, &nrec, &dummy))
{
gPrivKey = dummy;
gSolved = true;
break;
}
bool w12 = ((pref_rec.type == WILD1) && (nrec.type == WILD2)) || ((pref_rec.type == WILD2) && (nrec.type == WILD1));
if (w12)
;
else
{
printf("Collision Error\r\n");
gTotalErrors++;
}
// Near-miss gravity: same-type collisions / close tags
if (gGravity)
{
Gravity_ObserveNearMiss(&gGravityHost, nrec.x, 1);
for (int gi = 0; gi < GpuCnt; gi++)
GpuKangs[gi]->ObserveGravityNearMiss(nrec.x, 1);
}
if (gNearMiss)
NearMiss_Commit(nrec.x, 1, true);
if (gHerdSkew)
{
for (int gi = 0; gi < GpuCnt; gi++)
GpuKangs[gi]->ObserveHerdSkew(false);
}
continue;
}
if (gHerdSkew)
{
for (int gi = 0; gi < GpuCnt; gi++)
GpuKangs[gi]->ObserveHerdSkew(true);
}
if (gPhantom)
Phantom_NoteReconstruct(&gPhantomStats);
gSolved = true;
break;
}
}
static const char* PhaseName(int p)
{
switch (p)
{
case PHASE_FILL: return "FILL";
case PHASE_FREEZE: return "FREEZE";
case PHASE_WILD_STORM: return "WILD";
default: return "?";
}
}
void ShowStats(u64 tm_start, double exp_ops, double dp_val)
{
#ifdef DEBUG_MODE
for (int i = 0; i <= MD_LEN; i++)
{
u64 val = 0;
for (int j = 0; j < GpuCnt; j++)
{
val += GpuKangs[j]->dbg[i];
}
if (val)
printf("Loop size %d: %llu\r\n", i, val);
}
#endif
int speed = GpuKangs[0]->GetStatsSpeed();
for (int i = 1; i < GpuCnt; i++)
speed += GpuKangs[i]->GetStatsSpeed();
u64 est_dps_cnt = (u64)(exp_ops / dp_val);
u64 exp_sec = 0xFFFFFFFFFFFFFFFFull;
if (speed)
exp_sec = (u64)((exp_ops / 1000000) / speed);
u64 exp_days = exp_sec / (3600 * 24);
int exp_hours = (int)(exp_sec - exp_days * (3600 * 24)) / 3600;
int exp_min = (int)(exp_sec - exp_days * (3600 * 24) - exp_hours * 3600) / 60;
u64 sec = (GetTickCount64() - tm_start) / 1000;
u64 days = sec / (3600 * 24);
int hours = (int)(sec - days * (3600 * 24)) / 3600;
int min = (int)(sec - days * (3600 * 24) - hours * 3600) / 60;
double progress = (exp_ops > 0) ? ((double)PntTotalOps / exp_ops) : 0;
if (progress > 1.0) progress = 1.0;
double ram_gb = db.GetRamUsedGB();
double ops_bits = (PntTotalOps > 0) ? log2((double)PntTotalOps) : 0;
u64 now = GetTickCount64();
double dp_rate = 0;
if (gLastStatsMs && now > gLastStatsMs)
{
double dt = (now - gLastStatsMs) / 1000.0;
if (dt > 0)
dp_rate = (double)(PntTotalOps - gLastOpsSnapshot) / dt;
}
gLastStatsMs = now;
gLastOpsSnapshot = PntTotalOps;
if (gRamLimitGB > 0)
printf("%sSpeed: %d MKeys/s, Err: %d, DPs: %lluK/%lluK (%.1f%%), ops: 2^%.2f / 2^%.2f, RAM: %.2f/%.2f GB, DP/s: %.0f, Phase: %s, Time: %llud:%02dh:%02dm/%llud:%02dh:%02dm\r\n",
gGenMode ? "GEN: " : (IsBench ? "BENCH: " : "MAIN: "),
speed, gTotalErrors, db.GetBlockCnt() / 1000, est_dps_cnt / 1000, progress * 100.0,
ops_bits, log2(exp_ops), ram_gb, gRamLimitGB, dp_rate, PhaseName(gPhase),
days, hours, min, exp_days, exp_hours, exp_min);
else
printf("%sSpeed: %d MKeys/s, Err: %d, DPs: %lluK/%lluK (%.1f%%), ops: 2^%.2f / 2^%.2f, RAM: %.2f GB, DP/s: %.0f, Phase: %s, Time: %llud:%02dh:%02dm/%llud:%02dh:%02dm\r\n",
gGenMode ? "GEN: " : (IsBench ? "BENCH: " : "MAIN: "),
speed, gTotalErrors, db.GetBlockCnt() / 1000, est_dps_cnt / 1000, progress * 100.0,
ops_bits, log2(exp_ops), ram_gb, dp_rate, PhaseName(gPhase),
days, hours, min, exp_days, exp_hours, exp_min);
fflush(stdout);
}
static bool SaveJumpTables(FILE* fp)
{
for (int i = 0; i < JMP_CNT; i++)
{
if (fwrite(EcJumps1[i].p.x.data, 1, 40, fp) != 40) return false;
if (fwrite(EcJumps1[i].p.y.data, 1, 40, fp) != 40) return false;
if (fwrite(EcJumps1[i].dist.data, 1, 40, fp) != 40) return false;
}
for (int i = 0; i < JMP_CNT; i++)
{
if (fwrite(EcJumps2[i].p.x.data, 1, 40, fp) != 40) return false;
if (fwrite(EcJumps2[i].p.y.data, 1, 40, fp) != 40) return false;
if (fwrite(EcJumps2[i].dist.data, 1, 40, fp) != 40) return false;
}
for (int i = 0; i < JMP_CNT; i++)
{
if (fwrite(EcJumps3[i].p.x.data, 1, 40, fp) != 40) return false;
if (fwrite(EcJumps3[i].p.y.data, 1, 40, fp) != 40) return false;
if (fwrite(EcJumps3[i].dist.data, 1, 40, fp) != 40) return false;
}
return true;
}
static bool LoadJumpTables(FILE* fp)
{
for (int i = 0; i < JMP_CNT; i++)
{
if (fread(EcJumps1[i].p.x.data, 1, 40, fp) != 40) return false;
if (fread(EcJumps1[i].p.y.data, 1, 40, fp) != 40) return false;
if (fread(EcJumps1[i].dist.data, 1, 40, fp) != 40) return false;
}
for (int i = 0; i < JMP_CNT; i++)
{
if (fread(EcJumps2[i].p.x.data, 1, 40, fp) != 40) return false;
if (fread(EcJumps2[i].p.y.data, 1, 40, fp) != 40) return false;
if (fread(EcJumps2[i].dist.data, 1, 40, fp) != 40) return false;
}
for (int i = 0; i < JMP_CNT; i++)
{
if (fread(EcJumps3[i].p.x.data, 1, 40, fp) != 40) return false;
if (fread(EcJumps3[i].p.y.data, 1, 40, fp) != 40) return false;
if (fread(EcJumps3[i].dist.data, 1, 40, fp) != 40) return false;
}
return true;
}
bool SaveCheckpoint(const char* path)
{
char tmp[1100];
snprintf(tmp, sizeof(tmp), "%s.tmp", path);
// Pause GPU threads so kangaroo buffers are stable
for (int i = 0; i < GpuCnt; i++)
GpuKangs[i]->RequestPause();
Sleep(500);
FILE* fp = fopen(tmp, "wb");
if (!fp)
{
printf("checkpoint: cannot open %s\r\n", tmp);
for (int i = 0; i < GpuCnt; i++)
GpuKangs[i]->ClearPause();
return false;
}
bool ok = true;
ok = ok && fwrite(CHECKPOINT_MAGIC, 1, 8, fp) == 8;
u32 ver = CHECKPOINT_VER;
ok = ok && fwrite(&ver, 1, 4, fp) == 4;
ok = ok && fwrite(&gRange, 1, 4, fp) == 4;
ok = ok && fwrite(&gDP, 1, 4, fp) == 4;
ok = ok && fwrite(gStart.data, 1, 40, fp) == 40;
ok = ok && fwrite(gPubKey.x.data, 1, 40, fp) == 40;
ok = ok && fwrite(gPubKey.y.data, 1, 40, fp) == 40;
ok = ok && fwrite((const void*)&PntTotalOps, 1, 8, fp) == 8;
u8 freeze = gFreezeTames ? 1 : 0;
ok = ok && fwrite(&freeze, 1, 1, fp) == 1;
ok = ok && fwrite(&gPhase, 1, 4, fp) == 4;
ok = ok && SaveRndState(fp);
ok = ok && SaveJumpTables(fp);
// DP table to sidecar
char dbpath[1100];
snprintf(dbpath, sizeof(dbpath), "%s.dp", path);
db.Header[0] = (u8)gRange;
db.Header[1] = 'C';
ok = ok && db.AtomicSaveToFile(dbpath);
ok = ok && fwrite(&GpuCnt, 1, 4, fp) == 4;
for (int i = 0; i < GpuCnt && ok; i++)
ok = GpuKangs[i]->SaveKangState(fp);
fclose(fp);
for (int i = 0; i < GpuCnt; i++)
GpuKangs[i]->ClearPause();
if (!ok)
{
printf("checkpoint: write failed\r\n");
remove(tmp);
return false;
}
if (!AtomicRenameFile(tmp, path))
{
printf("checkpoint: rename failed\r\n");
return false;
}
printf("checkpoint saved: %s (+ %s)\r\n", path, dbpath);
return true;
}
bool LoadCheckpointMeta(const char* path)
{
FILE* fp = fopen(path, "rb");
if (!fp)
return false;
char magic[8];
if (fread(magic, 1, 8, fp) != 8 || memcmp(magic, CHECKPOINT_MAGIC, 8) != 0)
{
fclose(fp);
printf("checkpoint: bad magic\r\n");
return false;
}
u32 ver = 0;
fread(&ver, 1, 4, fp);
if (ver != CHECKPOINT_VER)
{
fclose(fp);
printf("checkpoint: unsupported version %u\r\n", ver);
return false;
}
fread(&gRange, 1, 4, fp);
fread(&gDP, 1, 4, fp);
fread(gStart.data, 1, 40, fp);
gStartSet = true;
fread(gPubKey.x.data, 1, 40, fp);
fread(gPubKey.y.data, 1, 40, fp);
fread((void*)&PntTotalOps, 1, 8, fp);
u8 freeze = 0;
fread(&freeze, 1, 1, fp);
gFreezeTames = freeze != 0;
fread(&gPhase, 1, 4, fp);
if (!LoadRndState(fp) || !LoadJumpTables(fp))
{
fclose(fp);
printf("checkpoint: rng/jumps load failed\r\n");
return false;
}
char dbpath[1100];
snprintf(dbpath, sizeof(dbpath), "%s.dp", path);
if (!db.LoadFromFile(dbpath))
{
fclose(fp);
printf("checkpoint: DP table load failed (%s)\r\n", dbpath);
return false;
}
int gpu_cnt_file = 0;
fread(&gpu_cnt_file, 1, 4, fp);
// Kangaroo states loaded after InitGpus in LoadCheckpointGpus
fclose(fp);
gRange_user_set = true;
gDP_user_set = true;
gLoadCheckpoint = true;
printf("checkpoint meta loaded: range=%u dp=%u ops=%llu freeze=%d phase=%s DPs=%llu\r\n",
gRange, gDP, (unsigned long long)PntTotalOps, (int)gFreezeTames, PhaseName(gPhase),
(unsigned long long)db.GetBlockCnt());
return true;
}
bool LoadCheckpointGpus(const char* path)
{
FILE* fp = fopen(path, "rb");
if (!fp)
return false;
// skip header through jumps+db already loaded — re-parse to GPU section
char magic[8];
u32 ver;
fread(magic, 1, 8, fp);
fread(&ver, 1, 4, fp);
u32 tmp32; EcInt tmpi; EcPoint tmpp; u64 tmp64; u8 tmp8;
fread(&tmp32, 1, 4, fp); // range
fread(&tmp32, 1, 4, fp); // dp
fread(tmpi.data, 1, 40, fp);
fread(tmpp.x.data, 1, 40, fp);
fread(tmpp.y.data, 1, 40, fp);
fread(&tmp64, 1, 8, fp);
fread(&tmp8, 1, 1, fp);
fread(&tmp32, 1, 4, fp);
if (!LoadRndState(fp)) { fclose(fp); return false; } // consume (already applied)
if (!LoadJumpTables(fp)) { fclose(fp); return false; }
int gpu_cnt_file = 0;
fread(&gpu_cnt_file, 1, 4, fp);
if (gpu_cnt_file != GpuCnt)
{
printf("checkpoint: GPU count mismatch file=%d now=%d\r\n", gpu_cnt_file, GpuCnt);
fclose(fp);
return false;
}
for (int i = 0; i < GpuCnt; i++)
{
if (!GpuKangs[i]->LoadKangState(fp))
{
fclose(fp);
return false;
}
}
fclose(fp);
printf("checkpoint: kangaroo states loaded for %d GPU(s)\r\n", GpuCnt);
return true;
}
bool SolvePoint(EcPoint PntToSolve, int Range, int DP, EcInt* pk_res)
{
if ((Range < 32) || (Range > 180))
{
printf("Unsupported Range value (%d)!\r\n", Range);
return false;
}
if ((DP < 14) || (DP > 60))
{
printf("Unsupported DP value (%d)!\r\n", DP);
return false;
}
printf("\r\nSolving point: Range %d bits, DP %d, start...\r\n", Range, DP);
double ops = 1.15 * pow(2.0, Range / 2.0);
double dp_val = (double)(1ull << DP);
double ram = (DB_REC_LEN + 4 + 4) * ops / dp_val;
ram += sizeof(TListRec) * 256 * 256 * 256;
ram /= (1024 * 1024 * 1024);
printf("SOTA method, estimated ops: 2^%.3f, RAM for DPs: %.3f GB (compact %dB/rec). DP and GPU overheads not included!\r\n",
log2(ops), ram, DB_REC_LEN);
printf("Honest note: for range %d this is ~2^%.1f group ops — a single 3060 Ti will NOT finish #140 quickly.\r\n",
Range, log2(ops));
gIsOpsLimit = false;
double MaxTotalOps = 0.0;
if (gMax > 0)
{
MaxTotalOps = gMax * ops;
double ram_max = (DB_REC_LEN + 4 + 4) * MaxTotalOps / dp_val;
ram_max += sizeof(TListRec) * 256 * 256 * 256;
ram_max /= (1024 * 1024 * 1024);
printf("Max allowed number of ops: 2^%.3f, max RAM for DPs: %.3f GB\r\n", log2(MaxTotalOps), ram_max);
}
if (gRamLimitGB > 0)
printf("RAM limit: %.3f GB (freeze TAMEs at 93%%)\r\n", gRamLimitGB);
u64 total_kangs = GpuKangs[0]->CalcKangCnt();
for (int i = 1; i < GpuCnt; i++)
total_kangs += GpuKangs[i]->CalcKangCnt();
double path_single_kang = ops / total_kangs;
double DPs_per_kang = path_single_kang / dp_val;
printf("Estimated DPs per kangaroo: %.3f.%s\r\n", DPs_per_kang, (DPs_per_kang < 5) ? " DP overhead is big, use less DP value if possible!" : "");
printf("Herd: concurrent TAME+WILD1+WILD2 from start (1/3 each).\r\n");
if (gTagTrace)
printf("TagTrace: ON (GPU implant + host safety filter)\r\n");
if (gDualLane)
printf("DualLane: requested (per-GPU stream path if CUDA streams OK)\r\n");
if (gStreamTrio)
printf("StreamTrio: requested (hop / DP-pack / host-copy)\r\n");
if (gL2Pin)
printf("L2Pin: requested (persisting L2 for jumps on Ampere+)\r\n");
if (gWarpDP)
printf("WarpDP: requested (warp ballot before DPs_out atomics)\r\n");
if (gAntiLoop)
printf("AntiLoop Sentinel: ON\r\n");
if (gChronos)
printf("ChronosKernel: ON (mid-run host retune)\r\n");
if (gPhantom)
printf("PhantomHerd: ON (denser DP hot path)\r\n");
if (gForecast)
printf("CollisionForecast: ON (sterility -> reseed)\r\n");
if (gTimeCrystal)
printf("TimeCrystal: ON\r\n");
if (gJumpPoison)
printf("JumpPoison: ON\r\n");
if (gMirrorWorld)
printf("MirrorWorld: ON (limited dual)\r\n");
if (gFalseDawn)
printf("FalseDawn Filter: ON (default)\r\n");
if (gEchoChamber)
printf("EchoChamber: ON (orthogonal dual-table sensor)\r\n");
if (gShadowLedger)
printf("ShadowLedger: ON (M=%llu)\r\n", (unsigned long long)gShadowMod);
if (gDriftCompass)
printf("DriftCompass: ON\r\n");
if (gDreamtime)
printf("DreamtimeWalk: ON (entropy jumps)\r\n");
if (gAfterimage)
printf("Afterimage: ON\r\n");
if (gDreamReplay)
printf("DreamReplay: ON\r\n");
if (gNegentropy)
printf("NegentropyBudget: ON\r\n");
if (gApocalypse)
printf("ApocalypseSave: ON (multi-path ckpt)\r\n");
if (gStowaway)
printf("Stowaway: ON (polite priority)\r\n");
if (gTwinFlame)
printf("TwinFlame: ON (host-only H)\r\n");
if (gHerdSkew)
printf("HerdSkew: ON (dynamic TAME:WILD)\r\n");
if (gSobol)
printf("SobolStarts: ON (quasirandom WILD)\r\n");
if (gGravity)
printf("GravityRespawn: ON (near-miss bias)\r\n");
if (gOrbitResolve)
{
OrbitResolveResult orb;
OrbitResolve_Run(PntToSolve, &orb);
}
if (gMirrorWorld)
{
EcPoint Q;
char note[320];
MirrorWorld_Prepare(PntToSolve, &Q, note, (int)sizeof(note));
printf(" %s\r\n", note);
}
if (!gGenMode && gTamesFileName[0] && !gLoadCheckpoint)
{
printf("load tames...\r\n");
if (db.LoadFromFile(gTamesFileName))
{
printf("tames loaded\r\n");
if (db.Header[0] != gRange)
{
printf("loaded tames have different range, they cannot be used, clear\r\n");
db.Clear();
}
}
else
printf("tames loading failed\r\n");
}
bool jumps_from_ckpt = gLoadCheckpoint;
if (!jumps_from_ckpt)
{
TagTrace_SetEnabled(gTagTrace);
TagTrace_ResetStats();
if (gJumpDNA)
{
JumpDNAResult jd;
JumpDNA_Run(Range, DP, EcJumps1, EcJumps2, EcJumps3, &jd);
PrintJumpDNA(&jd);
gJumpSeed = gJumpDNA_Seed ? gJumpDNA_Seed : jd.best_seed;
}
else if (gDreamtime)
{
u64 ent = gTimeCrystalSeed ? gTimeCrystalSeed : 0xD2EA4718ULL;
#ifdef _WIN32
FILETIME ft; GetSystemTimeAsFileTime(&ft);
ent ^= ((u64)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
#endif
Dreamtime_BuildJumps(Range, ent, EcJumps1, EcJumps2, EcJumps3);
gJumpSeed = ent;
}
else if (gTimeCrystal)
{
TimeCrystal_BuildJumps(Range, gTimeCrystalSeed, EcJumps1, EcJumps2, EcJumps3);
gJumpSeed = gTimeCrystalSeed;
}
else if (gJumpPoison)
{
JumpPoison_BuildJumps(Range, gVarianceProfileId, gJumpSeed ? gJumpSeed : 0x50150001ULL, EcJumps1, EcJumps2, EcJumps3);
}
else if (gVarianceForge && !gVarianceForgeDone)
{
VarianceForgeResult vf;
VarianceForge_Run(Range, DP, false, EcJumps1, EcJumps2, EcJumps3, &vf);
PrintVarianceForge(&vf);
if (gJumpSeed != 0)
BuildJumpTablesProfile(Range, gVarianceProfileId, gJumpSeed, EcJumps1, EcJumps2, EcJumps3);
}
else if (gVarianceForgeDone)
{
BuildJumpTablesProfile(Range, gVarianceProfileId, gJumpSeed, EcJumps1, EcJumps2, EcJumps3);
printf("VarianceForge: profile id=%d seed=%llu\r\n",
gVarianceProfileId, (unsigned long long)gJumpSeed);
}
else
{
BuildJumpTablesProfile(Range, 0, gJumpSeed, EcJumps1, EcJumps2, EcJumps3);
PrintVarianceForgeLite(Range, DP);
}
if (gTagTrace)
TagTrace_Precompute(EcJumps1, EcJumps2, EcJumps3);
}
if (!gLoadCheckpoint && !gPreserveDb)
PntTotalOps = 0;
u64 ops_at_start = PntTotalOps; // lottery keep-DP: limit is per-wave delta
PntIndex = 0;
Int_HalfRange.Set(1);
Int_HalfRange.ShiftLeft(Range - 1);
Pnt_HalfRange = ec.MultiplyG(Int_HalfRange);
Pnt_NegHalfRange = Pnt_HalfRange;
Pnt_NegHalfRange.y.NegModP();
Int_TameOffset.Set(1);
Int_TameOffset.ShiftLeft(Range - 1);
EcInt tt;
tt.Set(1);
tt.ShiftLeft(Range - 5);
Int_TameOffset.Sub(tt);
gPntToSolve = PntToSolve;
if (!gFreezeTames)
gPhase = PHASE_FILL;
for (int i = 0; i < GpuCnt; i++)