Repository navigation
Expand file tree
/
Copy pathpow.cpp
More file actions
900 lines (759 loc) · 32.9 KB
/
Copy pathpow.cpp
File metadata and controls
900 lines (759 loc) · 32.9 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
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <arith_uint256.h>
#include <chain.h>
#include <consensus/params.h>
#include <pow.h>
#include <asert_table.h>
#include <primitives/block.h>
#include <uint256.h>
#include <algorithm> //min and max.
#include <atomic>
#include <cassert>
#include <cmath>
#include <iostream>
#include <logging.h>
//Blake2b, Scrypt and SHA3-512
#include <cryptopp/blake2.h>
#include <cryptopp/cryptlib.h>
#include <cryptopp/files.h>
#include <cryptopp/hex.h>
#include <cryptopp/scrypt.h>
#include <cryptopp/secblock.h>
#include <cryptopp/sha3.h>
#include <cryptopp/whrlpool.h>
//Fancy popcount implementation
#include <libpopcnt.h>
// DeploymentActiveAfter
#include <deploymentstatus.h>
static std::atomic<const CBlockIndex *> cachedAnchor{nullptr};
void ResetASERTAnchorBlockCache() noexcept {
cachedAnchor = nullptr;
}
bool IsASERTEnabled(const Consensus::Params ¶ms,
const CBlockIndex *pindexPrev) {
if (pindexPrev == nullptr) {
return false;
}
return DeploymentActiveAfter(pindexPrev, params, Consensus::DEPLOYMENT_ASERT);
}
/**
* Returns a pointer to the anchor block used for ASERT.
*
* The anchor is the last block whose difficulty was set by the old DAA,
* serving as the reference point from which ASERT computes all subsequent
* difficulties.
*
* For ALWAYS_ACTIVE deployments (testnet/signet), the anchor is block 1.
* For real BIP9 activation, the anchor is the last block of the LOCKED_IN
* period (one before the first block of the ACTIVE period).
*
* This function is meant to be removed some time after the upgrade, once
* the anchor block is deeply buried, and behind a hard-coded checkpoint.
*
* Precondition: pindex must not be nullptr.
* Postcondition: Returns a pointer to the anchor block, which is an
* ancestor of (or equal to) pindex.
*/
static const CBlockIndex *GetASERTAnchorBlock(const CBlockIndex *const pindex,
const Consensus::Params ¶ms) {
assert(pindex);
// Fast path: if we have a cached anchor that is an ancestor of pindex,
// return it immediately (no versionbits query needed).
// Note that if pindex == cachedAnchor, GetAncestor() returns cachedAnchor.
const CBlockIndex *lastCached = cachedAnchor.load();
if (lastCached && pindex->GetAncestor(lastCached->nHeight) == lastCached)
return lastCached;
// StateSinceHeight returns:
// 0 for ALWAYS_ACTIVE (no chain walk)
// H for real BIP9 (first block of the ACTIVE period)
//
// The anchor is the last block computed by the old DAA:
// block 1 for ALWAYS_ACTIVE (genesis has no ASERT history)
// block H-1 for real BIP9 (last block of LOCKED_IN period)
int activeSince = g_versionbitscache.StateSinceHeight(
pindex, params, Consensus::DEPLOYMENT_ASERT);
int anchorHeight = (activeSince <= 1) ? 1 : activeSince - 1;
// GetAncestor(h) returns the block at height h (the block itself when
// h == pindex->nHeight), so when computing difficulty for the first
// ACTIVE block — where pindex is the anchor — this returns pindex.
const CBlockIndex *anchor = pindex->GetAncestor(anchorHeight);
assert(anchor != nullptr);
// Overwrite the cache with the anchor we found. More likely than not, the next
// time we are asked to validate a header it will be part of same / similar chain, not
// some other unrelated chain with a totally different anchor.
cachedAnchor = anchor;
return anchor;
}
// ============================================================================
// FACTOR ASERT — operates in log2(compute) space via precomputed table.
//
// Sign convention (OPPOSITE of BCH's target-based formulation):
// Blocks too FAST → time_error > 0 → positive exponent → nBits UP (harder)
// Blocks too SLOW → time_error < 0 → negative exponent → nBits DOWN (easier)
// ============================================================================
ASERTResult CalculateFACTORASERT(const FACTORASERTParams& params,
int32_t anchorNBits,
int64_t timeDiff,
int64_t heightDiff) {
// --- Input validation ---
assert(anchorNBits % 2 == 0);
assert(anchorNBits >= params.nBitsMin && anchorNBits <= params.nBitsMax);
assert(heightDiff >= 0);
assert(params.halfLife > 0);
assert(params.targetSpacing > 0);
assert(params.nBitsMin >= 2 && params.nBitsMin % 2 == 0);
assert(params.nBitsMax <= 1022 && params.nBitsMax % 2 == 0);
assert(params.nBitsMin <= params.nBitsMax);
// --- Step 1: Look up anchor's log2(compute) ---
const int anchorIdx = anchorNBits / 2 - 1;
const int64_t anchor_log2_compute = LOG2_COMPUTE_TABLE[anchorIdx];
// --- Step 2: Compute exponent in Q32.32 ---
// time_error > 0 when blocks arrive faster than expected (need harder)
const int64_t expected_elapsed = heightDiff * params.targetSpacing;
const int64_t time_error = expected_elapsed - timeDiff;
// Overflow-safe Q32.32 conversion via split division.
// Direct (time_error << 32) overflows int64_t at |time_error| > ~2.15e9.
// This variant overflows only at |time_error| > 2^31 * halfLife (~11.7M years
// on mainnet), effectively eliminating the concern.
//
// Shifts go through uint64_t: in C++17, left-shifting a negative signed
// value is undefined behavior ([expr.shift]/2), which would fire for every
// slow-block header. The unsigned round-trip produces the same bit pattern
// on two's-complement platforms and is well-defined.
const int64_t integer_part = time_error / params.halfLife;
const int64_t remainder = time_error % params.halfLife;
const int64_t integer_shifted =
static_cast<int64_t>(static_cast<uint64_t>(integer_part) << 32);
const int64_t remainder_shifted =
static_cast<int64_t>(static_cast<uint64_t>(remainder) << 32);
const int64_t exponent_q32 = integer_shifted
+ (remainder_shifted / params.halfLife);
// --- Step 3: Compute target log2_compute ---
const int64_t target_log2_compute = anchor_log2_compute + exponent_q32;
// --- Step 4: Binary search for closest table entry ---
const int idxMin = params.nBitsMin / 2 - 1;
const int idxMax = params.nBitsMax / 2 - 1;
// Check clampedHigh BEFORE clamping the search result.
// This detects when the ASERT exponent pushes past what nBitsMax represents.
const bool clampedHigh = (target_log2_compute > LOG2_COMPUTE_TABLE[idxMax]);
int resultIdx;
if (target_log2_compute <= LOG2_COMPUTE_TABLE[idxMin]) {
resultIdx = idxMin;
} else if (target_log2_compute >= LOG2_COMPUTE_TABLE[idxMax]) {
resultIdx = idxMax;
} else {
// Binary search: find lo such that TABLE[lo] <= target < TABLE[lo+1]
int lo = idxMin, hi = idxMax;
while (lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
if (LOG2_COMPUTE_TABLE[mid] <= target_log2_compute) {
lo = mid;
} else {
hi = mid;
}
}
// Pick the closer of the two adjacent entries.
// Tiebreak: lower nBits (lower index = easier difficulty).
const int64_t dist_lo = target_log2_compute - LOG2_COMPUTE_TABLE[lo];
const int64_t dist_hi = LOG2_COMPUTE_TABLE[hi] - target_log2_compute;
resultIdx = (dist_lo <= dist_hi) ? lo : hi;
}
const int32_t newNBits = (resultIdx + 1) * 2;
return ASERTResult{newNBits, clampedHigh};
}
// ============================================================================
// FACTOR ASERT caller wrapper — bridges chain state to CalculateFACTORASERT().
//
// fPowAllowMinDifficultyBlocks is intentionally NOT checked here.
// ASERT computes difficulty absolutely from the anchor, not relative to the
// previous block. A min-difficulty block under ASERT doesn't reset the
// schedule: it adds +1 to height_diff without meaningfully advancing
// time_diff, making the exponent *more* positive (harder). The escape
// hatch is counterproductive under ASERT and must not be invoked.
// ============================================================================
static uint16_t GetNextFACTORASERTWorkRequired(
const CBlockIndex *pindexPrev,
const Consensus::Params ¶ms)
{
assert(pindexPrev != nullptr);
// Height guard: block 1 is the anchor itself (not ASERT-computed).
// Return genesis nBits so block 1 inherits the genesis difficulty.
if (pindexPrev->nHeight == 0) {
return pindexPrev->nBits;
}
// Anchor selection
const CBlockIndex *pindexAnchor = GetASERTAnchorBlock(pindexPrev, params);
assert(pindexAnchor != nullptr);
// Normalize anchor nBits to even (floor) and clamp into [nBitsMin, nBitsMax].
// Handles odd-nBits anchors (e.g. signet genesis nBits=33) and guards
// against an anchor whose old-DAA difficulty fell outside the ASERT range.
int32_t anchorNBits = pindexAnchor->nBits & ~1;
if (anchorNBits < params.nBitsMin) {
LogPrintf("ASERT: anchor nBits %d below minimum %d, clamping\n",
anchorNBits, params.nBitsMin);
anchorNBits = params.nBitsMin;
} else if (anchorNBits > params.nBitsMax) {
LogPrintf("ASERT: anchor nBits %d above maximum %d, clamping\n",
anchorNBits, params.nBitsMax);
anchorNBits = params.nBitsMax;
}
// Time and height measured from anchor itself (no +1, no parent).
const int64_t timeDiff = pindexPrev->GetBlockTime() - pindexAnchor->GetBlockTime();
const int64_t heightDiff = pindexPrev->nHeight - pindexAnchor->nHeight;
const FACTORASERTParams asertParams{
params.nPowTargetSpacing,
params.nASERTHalfLife,
params.nBitsMin,
params.nBitsMax
};
const ASERTResult result = CalculateFACTORASERT(asertParams, anchorNBits, timeDiff, heightDiff);
// Circuit breaker: if ASERT wants difficulty above nBitsMax, the
// factoring problem has become trivial at max difficulty — an
// existential threat. Return nBits=1024 which CheckProofOfWork
// rejects (>1023), halting the chain until a hardfork intervenes.
if (result.clampedHigh) {
LogPrintf("ASERT: clampedHigh — returning nBits=1024 (chain halt)\n");
return 1024;
}
return static_cast<uint16_t>(result.nBits);
}
uint16_t GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader* pblock, const Consensus::Params& params)
{
assert(pindexLast != nullptr);
if (params.fPowNoRetargeting) {
return pindexLast->nBits;
}
if (IsASERTEnabled(params, pindexLast)) {
return GetNextFACTORASERTWorkRequired(pindexLast, params);
}
// Check if interim DAA is active
const bool isInterimDAAActive = TimeLimitedDeploymentActive(pindexLast, params, Consensus::DEPLOYMENT_INTERIM_DAA);
// Use 42-block interval when interim DAA active, otherwise standard
const int64_t adjustmentInterval = isInterimDAAActive
? Consensus::INTERIM_DAA_PERIOD
: params.DifficultyAdjustmentInterval();
// Only change once per difficulty adjustment interval
if ((pindexLast->nHeight + 1) % adjustmentInterval != 0) {
if (params.fPowAllowMinDifficultyBlocks) {
// Special difficulty rule for testnet:
// If the new block's timestamp is more than 2* 10 minutes
// then allow mining of a min-difficulty block.
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing * 2) {
return params.powLimit;
} else {
// Return the last non-special-min-difficulty-rules-block
const CBlockIndex* pindex = pindexLast;
while (pindex->pprev && pindex->nHeight % adjustmentInterval != 0 &&
pindex->nBits == params.powLimit) {
pindex = pindex->pprev;
}
return pindex->nBits;
}
}
return pindexLast->nBits;
}
// Get first block of period
int nHeightFirst = pindexLast->nHeight - (adjustmentInterval - 1);
assert(nHeightFirst >= 0);
const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
assert(pindexFirst);
// Calculate target timespan based on active DAA
// Use (INTERIM_DAA_PERIOD - 1) because right now we don't know the time it took to mine the last block.
const int64_t nTargetTimespan = isInterimDAAActive
? ((Consensus::INTERIM_DAA_PERIOD - 1) * params.nPowTargetSpacing)
: params.nPowTargetTimespan;
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params, nTargetTimespan, isInterimDAAActive);
}
int32_t CalculateDifficultyDelta(const int32_t nBits, const double nPeriodTimeProportionConsumed, const bool isHardDiffRemoved) {
if (!isHardDiffRemoved) {
// Original difficulty adjustment algorithm
//Note for mainnet:
//If it takes more than 1 minute over the target blocktime, reduce difficulty.
if (nPeriodTimeProportionConsumed > 1.0333f)
return -1;
//Note for mainnet:
//To increase difficulty the network must be able to move the blocktime
//3 minutes under target blocktime. This is to avoid the difficulty becoming
//too much work for the network to handle. Based on heuristics.
if (nPeriodTimeProportionConsumed < 0.90f)
return 1;
return 0;
} else {
// Difficulty adjustment algorithm that skips over odd aka. hard diffs (2025)
// If block time is too long, decrease to the previous even diff
if (nPeriodTimeProportionConsumed > 1.0333f) {
int32_t nRetarget = 0;
if (nBits % 2 == 0) {
// Even diff to even diff
nRetarget = -2;
} else {
// Odd diff to even diff
nRetarget = -1;
}
// If block time is way too long (>60 min on mainnet), decrease by 4 or 3 instead of by 2 or 1
if (nPeriodTimeProportionConsumed > 2.0f) {
nRetarget -= 2;
}
return nRetarget;
}
// If block time is too short, increase to the next even diff
if (nPeriodTimeProportionConsumed < 0.90f) {
int32_t nRetarget = 0;
if (nBits % 2 == 0) {
// Even diff to even diff
nRetarget = 2;
} else {
// Odd diff to even diff
nRetarget = 1;
}
// If block time is way too short (<15 min on mainnet), increase by 4 or 3 instead of by 2 or 1
if (nPeriodTimeProportionConsumed < 0.5f) {
nRetarget += 2;
}
return nRetarget;
}
// The block time is just right. See if we should move away from an odd diff
if (nBits % 2 == 0) {
// Already even diff. Don't change difficulty
return 0;
} else {
// Currently odd diff. Decrease diff by 1 to reach an even difficulty
return -1;
}
}
}
/**
* Interim DAA: proportion-based difficulty adjustment with aggressive thresholds.
* Uses nPeriodTimeProportionConsumed like existing DAA but with different thresholds.
* Also ensures result skips odd difficulties (like HARD_DIFF_REMOVAL mode).
*
* @param nBits Current difficulty bits
* @param nPeriodTimeProportionConsumed Ratio of actual time to target time
*/
int32_t CalculateInterimDifficultyDelta(const int32_t nBits, const double nPeriodTimeProportionConsumed)
{
int32_t nRetarget = 0;
// Make sure we're going to an even aka. easy difficulty
if (nBits % 2 != 0) {
nRetarget -= 1;
}
// Too fast, increase difficulty
// 0.5 = 15min avg, 0.6667 ~= 20min avg, 0.9 = 27min avg for 30min target
if (nPeriodTimeProportionConsumed < 0.5f) {
nRetarget += 6;
} else if (nPeriodTimeProportionConsumed < 0.6667f) {
nRetarget += 4;
} else if (nPeriodTimeProportionConsumed < 0.9f) {
nRetarget += 2;
}
// Too slow, decrease difficulty
// 2.0 = 60min avg, 1.5 = 45min avg, 1.0333 ~= 31min avg for 30min target
else if (nPeriodTimeProportionConsumed > 2.0f) {
nRetarget -= 6;
} else if (nPeriodTimeProportionConsumed > 1.5f) {
nRetarget -= 4;
} else if (nPeriodTimeProportionConsumed > 1.0333f) {
nRetarget -= 2;
}
return nRetarget;
}
uint16_t CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params,
int64_t nTargetTimespan, bool isInterimDAA)
{
if (params.fPowNoRetargeting)
return pindexLast->nBits;
// Use provided target timespan or default
if (nTargetTimespan == 0) {
nTargetTimespan = params.nPowTargetTimespan;
}
// Compute constants
const int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
const double nPeriodTimeProportionConsumed = (double)nActualTimespan / (double)nTargetTimespan;
int32_t nRetarget;
if (isInterimDAA) {
// Use interim DAA algorithm
nRetarget = CalculateInterimDifficultyDelta(pindexLast->nBits, nPeriodTimeProportionConsumed);
} else {
// Use standard algorithm (with HARD_DIFF_REMOVAL check)
const bool isHardDiffRemoved = DeploymentActiveAfter(pindexLast, params, Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL);
nRetarget = CalculateDifficultyDelta(pindexLast->nBits, nPeriodTimeProportionConsumed, isHardDiffRemoved);
}
return (int32_t)pindexLast->nBits + nRetarget;
}
bool CheckProofOfWork(const CBlockHeader& block, const Consensus::Params& params)
{
if (block.nBits == 0 || block.nBits > 1023) {
LogPrintf("PoW error: nBits %d out of valid range [1, 1023]\n", block.nBits);
return false;
}
//First, generate the random seed submited for this block
uint1024 w = gHash(block, params);
//Check that |block->offset| <= \tilde{n} = 16 * |n|_2.
uint64_t abs_offset = (block.wOffset > 0) ? block.wOffset : -block.wOffset;
if (abs_offset > 16 * block.nBits) {
LogPrintf("PoW error: invalid wOffset\n");
return false;
}
//Get the semiprime n
mpz_t n, W;
mpz_init(n);
mpz_init(W);
mpz_import(W, 16, -1, 8, 0, 0, w.u64_begin());
//Add the offset to w to find the semiprime submitted: n = w + offset
if (block.wOffset >= 0) {
mpz_add_ui(n, W, abs_offset);
} else {
mpz_sub_ui(n, W, abs_offset);
}
{
char *w_str = mpz_get_str(NULL, 10, W);
char *n_str = mpz_get_str(NULL, 10, n);
LogPrint(BCLog::POW, " W: %s\n", w_str);
LogPrint(BCLog::POW, " N: %s\n", n_str);
free(w_str);
free(n_str);
}
//Clear memory for W.
mpz_clear(W);
//Check the number n has nBits
if (mpz_sizeinbase(n, 2) != block.nBits) {
LogPrintf("pow error: invalid nBits: expected %d, actual %d\n", block.nBits, mpz_sizeinbase(n, 2));
mpz_clear(n);
return false;
}
//Divide the factor submitted for this block by N
mpz_t nP1, nP2;
mpz_init(nP1);
mpz_init(nP2);
mpz_import(nP1, 16, -1, 8, 0, 0, block.nP1.u64_begin());
if (mpz_sgn(nP1) == 0) {
LogPrintf("PoW error: nP1 is zero\n");
mpz_clear(n);
mpz_clear(nP1);
mpz_clear(nP2);
return false;
}
mpz_tdiv_q(nP2, n, nP1);
{
char *p1_str = mpz_get_str(NULL, 10, nP1);
char *p2_str = mpz_get_str(NULL, 10, nP2);
LogPrint(BCLog::POW, "nP1: %s\n", p1_str);
LogPrint(BCLog::POW, "nP2: %s\n", p2_str);
free(p1_str);
free(p2_str);
}
//Check the bitsizes are as expected
const uint16_t nP1_bitsize = mpz_sizeinbase(nP1, 2);
const uint16_t expected_bitsize = (block.nBits >> 1) + (block.nBits & 1);
if (nP1_bitsize != expected_bitsize) {
LogPrintf("pow error: nP1 expected bitsize=%s, actual size=%s\n", expected_bitsize, nP1_bitsize);
mpz_clear(n);
mpz_clear(nP1);
mpz_clear(nP2);
return false;
}
//Check nP1 is a factor
mpz_t n_check;
mpz_init(n_check);
mpz_mul(n_check, nP1, nP2);
//Check that nP1*nP2 == n.
if (mpz_cmp(n_check, n) != 0) {
{
char *n_str = mpz_get_str(NULL, 10, n);
char *p1_str = mpz_get_str(NULL, 10, nP1);
LogPrintf("pow error: nP1 does not divide N. N=%s nP1=%s\n", n_str, p1_str);
free(n_str);
free(p1_str);
}
mpz_clear(n);
mpz_clear(nP1);
mpz_clear(nP2);
mpz_clear(n_check);
return false;
}
//Check that nP1 <= nP2.
if (mpz_cmp(nP1, nP2) > 0) {
{
char *n_str = mpz_get_str(NULL, 10, n);
char *p1_str = mpz_get_str(NULL, 10, nP1);
LogPrintf("pow error: nP1 must be the smallest factor. N=%s nP1=%s\n", n_str, p1_str);
free(n_str);
free(p1_str);
}
mpz_clear(n);
mpz_clear(nP1);
mpz_clear(nP2);
mpz_clear(n_check);
return false;
}
//Clear memory
mpz_clear(n);
mpz_clear(n_check);
//Test nP1 and nP2 for primality.
int is_nP1_prime = mpz_probab_prime_p(nP1, params.MillerRabinRounds);
int is_nP2_prime = mpz_probab_prime_p(nP2, params.MillerRabinRounds);
//Clear memory
mpz_clear(nP1);
mpz_clear(nP2);
//Check they are both prime
if (is_nP1_prime == 0 || is_nP2_prime == 0) {
LogPrintf("pow error: At least 1 composite factor found, rejected.\n");
return false;
}
return true;
}
uint1024 gHash(const CBlockHeader& block, const Consensus::Params& params)
{
//Get the required data for this block
uint256 hashPrevBlock = block.hashPrevBlock;
uint256 hashMerkleRoot = block.hashMerkleRoot;
uint64_t nNonce = block.nNonce;
uint32_t nTime = block.nTime;
int32_t nVersion = block.nVersion;
uint16_t nBits = block.nBits;
using namespace CryptoPP;
//Place data as raw bytes into the password and salt for Scrypt:
/////////////////////////////////////////////////
// pass = hashPrevBlock + hashMerkle + nNonce //
// salt = version + nBits + nTime //
/////////////////////////////////////////////////
byte pass[256 / 8 + 256 / 8 + 64 / 8] = {(byte)0};
byte salt[32 / 8 + 16 / 8 + 32 / 8] = {(byte)0};
//SALT: Copy version into the first 4 bytes of the salt.
memcpy(salt, &nVersion, sizeof(nVersion));
//SALT: Copy nBits into the next 2 bytes
int runningLen = sizeof(nVersion);
memcpy(&salt[runningLen], &nBits, sizeof(nBits));
//SALT: Copy nTime into the next 4 bytes
runningLen += sizeof(nBits);
memcpy(&salt[runningLen], &nTime, sizeof(nTime));
//PASS: Copy Previous Block Hash into the first 32 bytes
memcpy(pass, hashPrevBlock.begin(), hashPrevBlock.size());
//PASS: Copy Merkle Root hash into next 32 bytes
runningLen = hashPrevBlock.size();
memcpy(&pass[runningLen], hashMerkleRoot.begin(), hashMerkleRoot.size());
//PASS: Copy nNonce
runningLen += hashMerkleRoot.size();
memcpy(&pass[runningLen], &nNonce, sizeof(nNonce));
////////////////////////////////////////////////////////////////////////////////
// Scrypt parameters //
////////////////////////////////////////////////////////////////////////////////
// //
// N = Iterations count (Affects memory and CPU Usage). //
// r = block size ( affects memory and CPU usage). //
// p = Parallelism factor. (Number of threads). //
// pass = Input password. //
// salt = securely-generated random bytes. //
// derived-key-length = how many bytes to generate as output. Defaults to 32.//
// //
// For reference, Litecoin has N=1024, r=1, p=1. //
////////////////////////////////////////////////////////////////////////////////
Scrypt scrypt;
word64 N = 1ULL << 12;
word64 r = 1ULL << 1;
word64 p = 1ULL;
SecByteBlock derived(256);
//Scrypt Hash to 2048-bits hash.
scrypt.DeriveKey(derived, derived.size(), pass, sizeof(pass), salt, sizeof(salt), N, r, p);
//Consensus parameters
int roundsTotal = params.hashRounds;
//Prepare GMP objects
mpz_t prime_mpz, starting_number_mpz, a_mpz, a_inverse_mpz;
mpz_init(prime_mpz);
mpz_init(starting_number_mpz);
mpz_init(a_mpz);
mpz_init(a_inverse_mpz);
for (int round = 0; round < roundsTotal; round++) {
///////////////////////////////////////////////////////////////
// Memory Expensive Scrypt: 1MB required. //
///////////////////////////////////////////////////////////////
scrypt.DeriveKey(derived, //Final hash
derived.size(), //Final hash number of bytes
(const byte*)derived.data(), //Input hash
derived.size(), //Input hash number of bytes
salt, //Salt
sizeof(salt), //Salt bytes
N, //Number of rounds
r, //Sequential Read Sisze
p //Parallelizable iterations
);
///////////////////////////////////////////////////////////////
// Add different types of hashes to the core. //
///////////////////////////////////////////////////////////////
//Count the bits in previous hash.
uint64_t pcnt_half1 = popcnt(derived.data(), 128);
uint64_t pcnt_half2 = popcnt(&derived.data()[128], 128);
//Hash the first 1024-bits of the 2048-bits hash.
if (pcnt_half1 % 2 == 0) {
BLAKE2b bHash;
bHash.Update((const byte*)derived.data(), 128);
bHash.Final((byte*)derived.data());
} else {
SHA3_512 bHash;
bHash.Update((const byte*)derived.data(), 128);
bHash.Final((byte*)derived.data());
}
//Hash the second 1024-bits of the 2048-bits hash.
if (pcnt_half2 % 2 == 0) {
BLAKE2b bHash;
bHash.Update((const byte*)(&derived.data()[128]), 128);
bHash.Final((byte*)(&derived.data()[128]));
} else {
SHA3_512 bHash;
bHash.Update((const byte*)(&derived.data()[128]), 128);
bHash.Final((byte*)(&derived.data()[128]));
}
//////////////////////////////////////////////////////////////
// Perform expensive math opertions plus simple hashing //
//////////////////////////////////////////////////////////////
//Use the current hash to compute grunt work.
mpz_import(starting_number_mpz, 32, -1, 8, 0, 0, derived.data()); // -> M = 2048-hash
mpz_sqrt(starting_number_mpz, starting_number_mpz); // - \ a = floor( M^(1/2) )
mpz_set(a_mpz, starting_number_mpz); // - /
mpz_sqrt(starting_number_mpz, starting_number_mpz); // - \ p = floor( a^(1/2) )
mpz_nextprime(prime_mpz, starting_number_mpz); // - /
//Compute a^(-1) Mod p
mpz_invert(a_inverse_mpz, a_mpz, prime_mpz);
//Xor into current hash digest.
size_t words = 0;
uint64_t data[32] = {0};
uint64_t* hDigest = (uint64_t*)derived.data();
mpz_export(data, &words, -1, 8, 0, 0, a_inverse_mpz);
for (int jj = 0; jj < 32; jj++)
hDigest[jj] ^= data[jj];
//Check that at most 2048-bits were written
//Assume 64-bit limbs.
assert(words <= 32);
//Compute the population count of a_inverse
const int32_t irounds = popcnt(data, sizeof(data)) & 0x7f;
//Branch away
for (int jj = 0; jj < irounds; jj++) {
// sizeof(derived.data()) was sizeof(pointer), not the buffer size.
// On 64-bit this evaluated to 8, which is now the consensus value.
// Hardcode 8 so a 32-bit build doesn't fork from the network.
const int32_t br = popcnt(derived.data(), 8);
//Power mod
mpz_powm_ui(a_inverse_mpz, a_inverse_mpz, irounds, prime_mpz);
//Get the data out of gmp
mpz_export(data, &words, -1, 8, 0, 0, a_inverse_mpz);
assert(words <= 32);
for (int jj = 0; jj < 32; jj++)
hDigest[jj] ^= data[jj];
if (br % 3 == 0) {
SHA3_512 bHash;
bHash.Update((const byte*)derived.data(), 128);
bHash.Final((byte*)derived.data());
} else if (br % 3 == 2) {
BLAKE2b sHash;
sHash.Update((const byte*)(&derived.data()[128]), 128);
sHash.Final((byte*)(&derived.data()[192]));
} else {
Whirlpool wHash;
wHash.Update((const byte*)(derived.data()), 256);
wHash.Final((byte*)(&derived.data()[112]));
}
}
}
//Compute how many bytes to copy
int32_t allBytes = nBits / 8;
int32_t remBytes = nBits % 8;
//Make sure to stay within 2048-bits.
// NOTE: In the distant future this will have to be updated
// when nBITS starts to get close to 1024-bits, the limit of type uint1024.
assert(allBytes + 1 <= 128);
//TODO: Note that we use here a type to that holds 1024-bits instead of the
// 2048 bits of the hash above. For now this is fine, eventually, we will
// need to implement a 2048-bit when the system is factoring 900+ digit numbers.
// As this is unlikely to happen any time soon I think we are fine.
//Copy exactly the number of bytes that contains exactly the low nBits bits.
uint1024 w;
//Make sure the values in w are set to 0.
memset(w.u8_begin_write(), 0, 128);
memcpy(w.u8_begin_write(), derived.begin(), std::min(128, allBytes + 1));
//Trim off any bits from the Most Significant byte.
w.u8_begin_write()[allBytes] = w.u8_begin()[allBytes] & ((1 << remBytes) - 1);
//Set the nBits-bit to one.
if (remBytes == 0) {
w.u8_begin_write()[allBytes - 1] = w.u8_begin()[allBytes - 1] | 128;
} else {
w.u8_begin_write()[allBytes] = w.u8_begin()[allBytes] | (1 << (remBytes - 1));
}
mpz_clear(prime_mpz);
mpz_clear(starting_number_mpz);
mpz_clear(a_mpz);
mpz_clear(a_inverse_mpz);
return w;
}
// f(z) = z^2 + 1 Mod n
void f(mpz_t z, mpz_t n, mpz_t two)
{
mpz_powm(z, z, two, n);
mpz_add_ui(z, z, 1ULL);
mpz_mod(z, z, n);
}
// Pollard rho factoring algorithm
// Input: g First found factor goes here
// n Number to be factored.
// Output: Return 0 if n is prime.
// Returns 1 if both factors g and n/g terminate being prime.
// Return 0 otherwise.
int rho(mpz_t g, mpz_t n)
{
//Parameter of 25 gives 1 in 2^100 chance of false positive. That's good enough.
if (mpz_probab_prime_p(n, 25) != 0)
return 0; //test if n is prime with miller rabin test
mpz_t x;
mpz_t y;
mpz_t two;
mpz_t temp;
mpz_init_set_ui(x, 2);
mpz_init_set_ui(y, 2); //initialize x and y as 2
mpz_init_set_ui(two, 2);
mpz_set_ui(g, 1);
mpz_init(temp);
while (mpz_cmp_ui(g, 1) == 0) {
f(x, n, two); //x is changed
f(y, n, two);
f(y, n, two); //y is going through the sequence twice as fast
mpz_sub(temp, x, y);
mpz_gcd(g, temp, n);
}
//Set temp = n/g
mpz_divexact(temp, n, g);
//Test if g, temp are prime.
int u_p = mpz_probab_prime_p(temp, 30);
int g_p = mpz_probab_prime_p(g, 30);
//Clear gmp memory allocated inside this function
mpz_clear(x);
mpz_clear(y);
mpz_clear(temp);
mpz_clear(two);
//Enforce primality checks
//Check g is a proper factor. Pollard Rho may have missed a factorization.
if ((u_p != 0) && (g_p != 0) && (mpz_cmp(g, n) != 0))
return 1;
return 0;
}
int rho(uint64_t& g, uint64_t n)
{
//Declare needed types
mpz_t g1;
mpz_t n1;
//Initialize gmp types
mpz_init(g1);
mpz_init(n1);
//Set gmp types
mpz_set_ui(g1, g);
mpz_set_ui(n1, n);
//Factor and capture return value
int result = rho(g1, n1);
g = mpz_get_ui(g1);
//Clear memory allocated inside this function
mpz_clear(g1);
mpz_clear(n1);
//return
return result;
}