-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathuint256.go
1829 lines (1686 loc) · 64.6 KB
/
uint256.go
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
// Copyright (c) 2021-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
// Package uint256 implements highly optimized fixed precision unsigned 256-bit
// integer arithmetic.
package uint256
import (
"bytes"
"fmt"
"math/big"
"math/bits"
)
// References:
// [TAOCP2]: The Art of Computer Programming, Volume 2.
// Seminumerical Algorithms (Knuth, Donald E.)
var (
// zero32 is an array of 32 bytes used for the purposes of zeroing and is
// defined here to avoid extra allocations.
zero32 = [32]byte{}
// bigUint256Mask is the value 2^256 - 1 (aka max uint256) as a stdlib big
// integer. It is defined here to save allocations in the conversion code.
bigTwo256 = new(big.Int).Lsh(big.NewInt(1), 256)
bigUint256Mask = new(big.Int).Sub(bigTwo256, big.NewInt(1))
)
// Uint256 implements high-performance, zero-allocation, unsigned 256-bit
// fixed-precision arithmetic. All operations are performed modulo 2^256, so
// callers may rely on "wrap around" semantics.
//
// It implements the primary arithmetic operations (addition, subtraction,
// multiplication, squaring, division, negation), bitwise operations (lsh, rsh,
// not, or, and, xor), comparison operations (equals, less, greater, cmp),
// interpreting and producing big and little endian bytes, and other convenience
// methods such as determining the minimum number of bits required to represent
// the current value, whether or not the value can be represented as a uint64
// without loss of precision, and text formatting with base conversion.
//
// Should it be absolutely necessary, conversion to the standard library
// math/big.Int can be accomplished by using the ToBig or PutBig methods.
// However, that should typically be avoided when possible as conversion to
// big.Ints requires allocations and is slower for every operation, often to a
// significant degree.
type Uint256 struct {
// The uint256 is represented as 4 unsigned 64-bit integers in base 2^64.
//
// The following depicts the internal representation:
//
// --------------------------------------------------------------------
// | n[3] | n[2] | n[1] | n[0] |
// | 64 bits | 64 bits | 64 bits | 64 bits |
// | Mult: 2^(64*3) | Mult: 2^(64*2) | Mult: 2^(64*1) | Mult: 2^(64*0) |
// -------------------------------------------------------------------
//
// For example, consider the number:
// 0x0000000000000000080000000000000000000000000001000000000000000001 =
// 2^187 + 2^72 + 1
//
// It would be represented as:
// n[0] = 1
// n[1] = 2^8
// n[2] = 2^59
// n[3] = 0
//
// The full 256-bit value is then calculated by looping i from 3..0 and
// doing sum(n[i] * 2^(64i)) as follows:
// n[3] * 2^(64*3) = 0 * 2^192 = 0
// n[2] * 2^(64*2) = 2^59 * 2^128 = 2^187
// n[1] * 2^(64*1) = 2^8 * 2^64 = 2^72
// n[0] * 2^(64*0) = 1 * 2^0 = 1
// Sum: 0 + 2^187 + 2^72 + 1 = 2^187 + 2^72 + 1
n [4]uint64
}
// Set sets the uint256 equal to the same value as the passed one.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n := new(Uint256).Set(n2).AddUint64(1) so that n = n2 + 1 where n2 is not
// modified.
func (n *Uint256) Set(n2 *Uint256) *Uint256 {
*n = *n2
return n
}
// SetUint64 sets the uint256 to the passed unsigned 64-bit integer. This is a
// convenience function since it is fairly common to perform arithmetic with
// small native integers.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n := new(Uint256).SetUint64(2).Mul(n2) so that n = 2 * n2.
func (n *Uint256) SetUint64(n2 uint64) *Uint256 {
n.n[0] = n2
n.n[1] = 0
n.n[2] = 0
n.n[3] = 0
return n
}
// SetBytes interprets the provided array as a 256-bit big-endian unsigned
// integer and sets the uint256 to the result.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n := new(Uint256).SetBytes(n2Bytes).AddUint64(1) so that n = n2 + 1.
func (n *Uint256) SetBytes(b *[32]byte) *Uint256 {
// Pack the 256 total bits across the 4 uint64 words. This could be done
// with a for loop, but benchmarks show this unrolled version is about 2
// times faster than the variant that uses a loop.
n.n[0] = uint64(b[31]) | uint64(b[30])<<8 | uint64(b[29])<<16 |
uint64(b[28])<<24 | uint64(b[27])<<32 | uint64(b[26])<<40 |
uint64(b[25])<<48 | uint64(b[24])<<56
n.n[1] = uint64(b[23]) | uint64(b[22])<<8 | uint64(b[21])<<16 |
uint64(b[20])<<24 | uint64(b[19])<<32 | uint64(b[18])<<40 |
uint64(b[17])<<48 | uint64(b[16])<<56
n.n[2] = uint64(b[15]) | uint64(b[14])<<8 | uint64(b[13])<<16 |
uint64(b[12])<<24 | uint64(b[11])<<32 | uint64(b[10])<<40 |
uint64(b[9])<<48 | uint64(b[8])<<56
n.n[3] = uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 |
uint64(b[4])<<24 | uint64(b[3])<<32 | uint64(b[2])<<40 |
uint64(b[1])<<48 | uint64(b[0])<<56
return n
}
// SetBytesLE interprets the provided array as a 256-bit little-endian unsigned
// integer and sets the uint256 to the result.
func (n *Uint256) SetBytesLE(b *[32]byte) *Uint256 {
// Pack the 256 total bits across the 4 uint64 words. This could be done
// with a for loop, but benchmarks show this unrolled version is about 2
// times faster than the variant that uses a loop.
n.n[0] = uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 |
uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 |
uint64(b[6])<<48 | uint64(b[7])<<56
n.n[1] = uint64(b[8]) | uint64(b[9])<<8 | uint64(b[10])<<16 |
uint64(b[11])<<24 | uint64(b[12])<<32 | uint64(b[13])<<40 |
uint64(b[14])<<48 | uint64(b[15])<<56
n.n[2] = uint64(b[16]) | uint64(b[17])<<8 | uint64(b[18])<<16 |
uint64(b[19])<<24 | uint64(b[20])<<32 | uint64(b[21])<<40 |
uint64(b[22])<<48 | uint64(b[23])<<56
n.n[3] = uint64(b[24]) | uint64(b[25])<<8 | uint64(b[26])<<16 |
uint64(b[27])<<24 | uint64(b[28])<<32 | uint64(b[29])<<40 |
uint64(b[30])<<48 | uint64(b[31])<<56
return n
}
// zeroArray32 zeroes the provided 32-byte buffer.
func zeroArray32(b *[32]byte) {
copy(b[:], zero32[:])
}
// minInt is a helper function to return the minimum of two ints.
// This avoids a math import and the need to cast to floats.
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
// SetByteSlice interprets the provided slice as a 256-bit big-endian unsigned
// integer (meaning it is truncated to the final 32 bytes so that it is modulo
// 2^256), and sets the uint256 to the result.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n := new(Uint256).SetByteSlice(n2Slice).AddUint64(1) so that n = n2 + 1.
func (n *Uint256) SetByteSlice(b []byte) *Uint256 {
var b32 [32]byte
b = b[len(b)-minInt(len(b), 32):]
copy(b32[32-len(b):], b)
n.SetBytes(&b32)
zeroArray32(&b32)
return n
}
// SetByteSliceLE interprets the provided slice as a 256-bit little-endian
// unsigned integer (meaning it is truncated to the first 32 bytes so that it is
// modulo 2^256), and sets the uint256 to the result.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n := new(Uint256).SetByteSliceLE(n2Slice).AddUint64(1) so that n = n2 + 1.
func (n *Uint256) SetByteSliceLE(b []byte) *Uint256 {
var b32 [32]byte
b = b[:minInt(len(b), 32)]
copy(b32[:], b)
n.SetBytesLE(&b32)
zeroArray32(&b32)
return n
}
// PutBytesUnchecked unpacks the uint256 to a 32-byte big-endian value directly
// into the passed byte slice. The target slice must have at least 32 bytes
// available or it will panic.
//
// There is a similar function, PutBytes, which unpacks the uint256 into a
// 32-byte array directly. This version is provided since it can be useful to
// write directly into part of a larger buffer without needing a separate
// allocation.
func (n *Uint256) PutBytesUnchecked(b []byte) {
// Unpack the 256 total bits from the 4 uint64 words. This could be done
// with a for loop, but benchmarks show this unrolled version is about 2
// times faster than the variant which uses a loop.
b[31] = byte(n.n[0])
b[30] = byte(n.n[0] >> 8)
b[29] = byte(n.n[0] >> 16)
b[28] = byte(n.n[0] >> 24)
b[27] = byte(n.n[0] >> 32)
b[26] = byte(n.n[0] >> 40)
b[25] = byte(n.n[0] >> 48)
b[24] = byte(n.n[0] >> 56)
b[23] = byte(n.n[1])
b[22] = byte(n.n[1] >> 8)
b[21] = byte(n.n[1] >> 16)
b[20] = byte(n.n[1] >> 24)
b[19] = byte(n.n[1] >> 32)
b[18] = byte(n.n[1] >> 40)
b[17] = byte(n.n[1] >> 48)
b[16] = byte(n.n[1] >> 56)
b[15] = byte(n.n[2])
b[14] = byte(n.n[2] >> 8)
b[13] = byte(n.n[2] >> 16)
b[12] = byte(n.n[2] >> 24)
b[11] = byte(n.n[2] >> 32)
b[10] = byte(n.n[2] >> 40)
b[9] = byte(n.n[2] >> 48)
b[8] = byte(n.n[2] >> 56)
b[7] = byte(n.n[3])
b[6] = byte(n.n[3] >> 8)
b[5] = byte(n.n[3] >> 16)
b[4] = byte(n.n[3] >> 24)
b[3] = byte(n.n[3] >> 32)
b[2] = byte(n.n[3] >> 40)
b[1] = byte(n.n[3] >> 48)
b[0] = byte(n.n[3] >> 56)
}
// PutBytesUncheckedLE unpacks the uint256 to a 32-byte little-endian value
// directly into the passed byte slice. The target slice must have at least 32
// bytes available or it will panic.
//
// There is a similar function, PutBytesLE, which unpacks the uint256 into a
// 32-byte array directly. This version is provided since it can be useful to
// write directly into part of a larger buffer without needing a separate
// allocation.
func (n *Uint256) PutBytesUncheckedLE(b []byte) {
// Unpack the 256 total bits from the 4 uint64 words. This could be done
// with a for loop, but benchmarks show this unrolled version is about 2
// times faster than the variant which uses a loop.
b[31] = byte(n.n[3] >> 56)
b[30] = byte(n.n[3] >> 48)
b[29] = byte(n.n[3] >> 40)
b[28] = byte(n.n[3] >> 32)
b[27] = byte(n.n[3] >> 24)
b[26] = byte(n.n[3] >> 16)
b[25] = byte(n.n[3] >> 8)
b[24] = byte(n.n[3])
b[23] = byte(n.n[2] >> 56)
b[22] = byte(n.n[2] >> 48)
b[21] = byte(n.n[2] >> 40)
b[20] = byte(n.n[2] >> 32)
b[19] = byte(n.n[2] >> 24)
b[18] = byte(n.n[2] >> 16)
b[17] = byte(n.n[2] >> 8)
b[16] = byte(n.n[2])
b[15] = byte(n.n[1] >> 56)
b[14] = byte(n.n[1] >> 48)
b[13] = byte(n.n[1] >> 40)
b[12] = byte(n.n[1] >> 32)
b[11] = byte(n.n[1] >> 24)
b[10] = byte(n.n[1] >> 16)
b[9] = byte(n.n[1] >> 8)
b[8] = byte(n.n[1])
b[7] = byte(n.n[0] >> 56)
b[6] = byte(n.n[0] >> 48)
b[5] = byte(n.n[0] >> 40)
b[4] = byte(n.n[0] >> 32)
b[3] = byte(n.n[0] >> 24)
b[2] = byte(n.n[0] >> 16)
b[1] = byte(n.n[0] >> 8)
b[0] = byte(n.n[0])
}
// PutBytes unpacks the uint256 to a 32-byte big-endian value using the passed
// byte array.
//
// There is a similar function, PutBytesUnchecked, which unpacks the uint256
// into a slice that must have at least 32 bytes available. This version is
// provided since it can be useful to write directly into an array that is type
// checked.
//
// Alternatively, there is also Bytes, which unpacks the uint256 into a new
// array and returns that which can sometimes be more ergonomic in applications
// that aren't concerned about an additional copy.
func (n *Uint256) PutBytes(b *[32]byte) {
n.PutBytesUnchecked(b[:])
}
// PutBytesLE unpacks the uint256 to a 32-byte little-endian value using the
// passed byte array.
//
// There is a similar function, PutBytesUncheckedLE, which unpacks the uint256
// into a slice that must have at least 32 bytes available. This version is
// provided since it can be useful to write directly into an array that is type
// checked.
//
// Alternatively, there is also BytesLE, which unpacks the uint256 into a new
// array and returns that which can sometimes be more ergonomic in applications
// that aren't concerned about an additional copy.
func (n *Uint256) PutBytesLE(b *[32]byte) {
n.PutBytesUncheckedLE(b[:])
}
// Bytes unpacks the uint256 to a 32-byte big-endian array.
//
// See PutBytes and PutBytesUnchecked for variants that allow an array or slice
// to be passed which can be useful to cut down on the number of allocations
// by allowing the caller to reuse a buffer or write directly into part of a
// larger buffer.
func (n *Uint256) Bytes() [32]byte {
var b [32]byte
n.PutBytesUnchecked(b[:])
return b
}
// BytesLE unpacks the uint256 to a 32-byte little-endian array.
//
// See PutBytesLE and PutBytesUncheckedLE for variants that allow an array or
// slice to be passed which can be useful to cut down on the number of
// allocations by allowing the caller to reuse a buffer or write directly into
// part of a larger buffer.
func (n *Uint256) BytesLE() [32]byte {
var b [32]byte
n.PutBytesUncheckedLE(b[:])
return b
}
// Zero sets the uint256 to zero. A newly created uint256 is already set to
// zero. This function can be useful to clear an existing uint256 for reuse.
func (n *Uint256) Zero() {
n.n[0], n.n[1], n.n[2], n.n[3] = 0, 0, 0, 0
}
// IsZero returns whether or not the uint256 is equal to zero.
func (n *Uint256) IsZero() bool {
return n.n[0] == 0 && n.n[1] == 0 && n.n[2] == 0 && n.n[3] == 0
}
// IsOdd returns whether or not the uint256 is odd.
func (n *Uint256) IsOdd() bool {
// Only odd numbers have the bottom bit set.
return n.n[0]&1 == 1
}
// IsUint32 returns whether or not the uint256 can be converted to a uint32
// without any loss of precision. In other words, 0 <= n < 2^32.
func (n *Uint256) IsUint32() bool {
return (n.n[0]>>32 | n.n[1] | n.n[2] | n.n[3]) == 0
}
// Uint32 returns the uint32 representation of the value. In other words, it
// returns the low-order 32 bits of the value as a uint32 which is equivalent to
// the value modulo 2^32. The caller can determine if this method can be used
// without truncation with the IsUint32 method.
func (n *Uint256) Uint32() uint32 {
return uint32(n.n[0])
}
// IsUint64 returns whether or not the uint256 can be converted to a uint64
// without any loss of precision. In other words, 0 <= n < 2^64.
func (n *Uint256) IsUint64() bool {
return (n.n[1] | n.n[2] | n.n[3]) == 0
}
// Uint64 returns the uint64 representation of the value. In other words, it
// returns the low-order 64 bits of the value as a uint64 which is equivalent to
// the value modulo 2^64. The caller can determine if this method can be used
// without truncation with the IsUint64 method.
func (n *Uint256) Uint64() uint64 {
return n.n[0]
}
// Eq returns whether or not the two uint256s represent the same value.
func (n *Uint256) Eq(n2 *Uint256) bool {
return n.n[0] == n2.n[0] && n.n[1] == n2.n[1] && n.n[2] == n2.n[2] &&
n.n[3] == n2.n[3]
}
// EqUint64 returns whether or not the uint256 represents the same value as the
// given uint64.
func (n *Uint256) EqUint64(n2 uint64) bool {
return n.n[0] == n2 && (n.n[1]|n.n[2]|n.n[3]) == 0
}
// Lt returns whether or not the uint256 is less than the given one. That is,
// it returns true when n < n2.
func (n *Uint256) Lt(n2 *Uint256) bool {
var borrow uint64
_, borrow = bits.Sub64(n.n[0], n2.n[0], borrow)
_, borrow = bits.Sub64(n.n[1], n2.n[1], borrow)
_, borrow = bits.Sub64(n.n[2], n2.n[2], borrow)
_, borrow = bits.Sub64(n.n[3], n2.n[3], borrow)
return borrow != 0
}
// LtUint64 returns whether or not the uint256 is less than the given uint64.
// That is, it returns true when n < n2.
func (n *Uint256) LtUint64(n2 uint64) bool {
return n.n[0] < n2 && (n.n[1]|n.n[2]|n.n[3]) == 0
}
// LtEq returns whether or not the uint256 is less than or equal to the given
// one. That is, it returns true when n <= n2.
func (n *Uint256) LtEq(n2 *Uint256) bool {
return !n2.Lt(n)
}
// LtEqUint64 returns whether or not the uint256 is less than or equal to the
// given uint64. That is, it returns true when n <= n2.
func (n *Uint256) LtEqUint64(n2 uint64) bool {
return n.n[0] <= n2 && (n.n[1]|n.n[2]|n.n[3]) == 0
}
// Gt returns whether or not the uint256 is greater than the given one. That
// is, it returns true when n > n2.
func (n *Uint256) Gt(n2 *Uint256) bool {
return n2.Lt(n)
}
// GtUint64 returns whether or not the uint256 is greater than the given uint64.
// That is, it returns true when n > n2.
func (n *Uint256) GtUint64(n2 uint64) bool {
return n.n[0] > n2 || (n.n[1]|n.n[2]|n.n[3]) != 0
}
// GtEq returns whether or not the uint256 is greater than or equal to the given
// one. That is, it returns true when n >= n2.
func (n *Uint256) GtEq(n2 *Uint256) bool {
return !n.Lt(n2)
}
// GtEqUint64 returns whether or not the uint256 is greater than or equal to the
// given uint64. That is, it returns true when n >= n2.
func (n *Uint256) GtEqUint64(n2 uint64) bool {
return !n.LtUint64(n2)
}
// Cmp compares the two uint256s and returns -1, 0, or 1 depending on whether
// the uint256 is less than, equal to, or greater than the given one.
//
// That is, it returns:
//
// -1 when n < n2
// 0 when n == n2
// +1 when n > n2
func (n *Uint256) Cmp(n2 *Uint256) int {
var borrow uint64
r0, borrow := bits.Sub64(n.n[0], n2.n[0], borrow)
r1, borrow := bits.Sub64(n.n[1], n2.n[1], borrow)
r2, borrow := bits.Sub64(n.n[2], n2.n[2], borrow)
r3, borrow := bits.Sub64(n.n[3], n2.n[3], borrow)
if borrow != 0 {
return -1
}
if r0|r1|r2|r3 == 0 {
return 0
}
return 1
}
// CmpUint64 compares the uint256 with the given uint64 and returns -1, 0, or 1
// depending on whether the uint256 is less than, equal to, or greater than the
// uint64.
//
// That is, it returns:
//
// -1 when n < n2
// 0 when n == n2
// +1 when n > n2
func (n *Uint256) CmpUint64(n2 uint64) int {
if n.LtUint64(n2) {
return -1
}
if n.GtUint64(n2) {
return 1
}
return 0
}
// Add2 adds the passed two uint256s together modulo 2^256 and stores the result
// in n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.Add2(n1, n2).AddUint64(1) so that n = n1 + n2 + 1.
func (n *Uint256) Add2(n1, n2 *Uint256) *Uint256 {
var c uint64
n.n[0], c = bits.Add64(n1.n[0], n2.n[0], c)
n.n[1], c = bits.Add64(n1.n[1], n2.n[1], c)
n.n[2], c = bits.Add64(n1.n[2], n2.n[2], c)
n.n[3], _ = bits.Add64(n1.n[3], n2.n[3], c)
return n
}
// Add adds the passed uint256 to the existing one modulo 2^256 and stores the
// result in n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.Add(n2).AddUin64(1) so that n = n + n2 + 1.
func (n *Uint256) Add(n2 *Uint256) *Uint256 {
return n.Add2(n, n2)
}
// AddUint64 adds the passed uint64 to the existing uint256 modulo 2^256 and
// stores the result in n.
//
// The scalar is returned to support chaining. This enables syntax like:
// n.AddUint64(2).MulUint64(2) so that n = (n + 2) * 2.
func (n *Uint256) AddUint64(n2 uint64) *Uint256 {
var c uint64
n.n[0], c = bits.Add64(n.n[0], n2, c)
n.n[1], c = bits.Add64(n.n[1], 0, c)
n.n[2], c = bits.Add64(n.n[2], 0, c)
n.n[3], _ = bits.Add64(n.n[3], 0, c)
return n
}
// Sub2 subtracts the second given uint256 from the first modulo 2^256 and
// stores the result in n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.Sub2(n1, n2).AddUint64(1) so that n = n1 - n2 + 1.
func (n *Uint256) Sub2(n1, n2 *Uint256) *Uint256 {
var borrow uint64
n.n[0], borrow = bits.Sub64(n1.n[0], n2.n[0], borrow)
n.n[1], borrow = bits.Sub64(n1.n[1], n2.n[1], borrow)
n.n[2], borrow = bits.Sub64(n1.n[2], n2.n[2], borrow)
n.n[3], _ = bits.Sub64(n1.n[3], n2.n[3], borrow)
return n
}
// Sub subtracts the given uint256 from the existing one modulo 2^256 and stores
// the result in n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.Sub(n2).AddUint64(1) so that n = n - n2 + 1.
func (n *Uint256) Sub(n2 *Uint256) *Uint256 {
var borrow uint64
n.n[0], borrow = bits.Sub64(n.n[0], n2.n[0], borrow)
n.n[1], borrow = bits.Sub64(n.n[1], n2.n[1], borrow)
n.n[2], borrow = bits.Sub64(n.n[2], n2.n[2], borrow)
n.n[3], _ = bits.Sub64(n.n[3], n2.n[3], borrow)
return n
}
// SubUint64 subtracts the given uint64 from the uint256 modulo 2^256 and stores
// the result in n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.SubUint64(1).MulUint64(3) so that n = (n - 1) * 3.
func (n *Uint256) SubUint64(n2 uint64) *Uint256 {
var borrow uint64
n.n[0], borrow = bits.Sub64(n.n[0], n2, borrow)
n.n[1], borrow = bits.Sub64(n.n[1], 0, borrow)
n.n[2], borrow = bits.Sub64(n.n[2], 0, borrow)
n.n[3], _ = bits.Sub64(n.n[3], 0, borrow)
return n
}
// mulAdd64 multiplies the two passed base 2^64 digits together, adds the given
// value to the result, and returns the 128-bit result via a (hi, lo) tuple
// where the upper half of the bits are returned in hi and the lower half in lo.
func mulAdd64(digit1, digit2, m uint64) (hi, lo uint64) {
// Note the carry on the final add is safe to discard because the maximum
// possible value is:
// (2^64 - 1)(2^64 - 1) + (2^64 - 1) = 2^128 - 2^64
// and:
// 2^128 - 2^64 < 2^128.
var c uint64
hi, lo = bits.Mul64(digit1, digit2)
lo, c = bits.Add64(lo, m, 0)
hi, _ = bits.Add64(hi, 0, c)
return hi, lo
}
// mulAdd64Carry multiplies the two passed base 2^64 digits together, adds both
// the given value and carry to the result, and returns the 128-bit result via a
// (hi, lo) tuple where the upper half of the bits are returned in hi and the
// lower half in lo.
func mulAdd64Carry(digit1, digit2, m, c uint64) (hi, lo uint64) {
// Note the carries on the high order adds are safe to discard because the
// maximum possible value is:
// (2^64 - 1)(2^64 - 1) + 2*(2^64 - 1) = 2^128 - 1
// and:
// 2^128 - 1 < 2^128.
var c2 uint64
hi, lo = bits.Mul64(digit1, digit2)
lo, c2 = bits.Add64(lo, m, 0)
hi, _ = bits.Add64(hi, 0, c2)
lo, c2 = bits.Add64(lo, c, 0)
hi, _ = bits.Add64(hi, 0, c2)
return hi, lo
}
// Mul2 multiplies the passed uint256s together modulo 2^256 and stores the
// result in n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.Mul2(n1, n2).AddUint64(1) so that n = (n1 * n2) + 1.
func (n *Uint256) Mul2(n1, n2 *Uint256) *Uint256 {
// The general strategy employed here is:
// 1) Calculate the 512-bit product of the two uint256s using standard
// schoolbook multiplication.
// 2) Reduce the result modulo 2^256.
//
// However, some optimizations are used versus naively calculating all
// intermediate terms:
// 1) Reuse the high 64 bits from the intermediate 128-bit products directly
// since that is equivalent to shifting the result right by 64 bits.
// 2) Ignore all of the products between individual digits that would
// ordinarily be needed for the full 512-bit product when they are
// guaranteed to result in values that fall in the half open interval
// [2^256, 2^512) since they are all ≡ 0 (mod 2^256) given they are
// necessarily multiples of it.
// 3) Use native uint64s for the calculations involving the final digit of
// the result (r3) because all overflow carries to bits >= 256 which, as
// above, are all ≡ 0 (mod 2^256) and thus safe to discard.
var r0, r1, r2, r3, c uint64
// Terms resulting from the product of the first digit of the second number
// by all digits of the first number.
c, r0 = bits.Mul64(n2.n[0], n1.n[0])
c, r1 = mulAdd64(n2.n[0], n1.n[1], c)
c, r2 = mulAdd64(n2.n[0], n1.n[2], c)
r3 = n2.n[0]*n1.n[3] + c
// Terms resulting from the product of the second digit of the second number
// by all digits of the first number except those that are guaranteed to be
// ≡ 0 (mod 2^256).
c, r1 = mulAdd64(n2.n[1], n1.n[0], r1)
c, r2 = mulAdd64Carry(n2.n[1], n1.n[1], r2, c)
r3 += n2.n[1]*n1.n[2] + c
// Terms resulting from the product of the third digit of the second number
// by all digits of the first number except those that are guaranteed to be
// ≡ 0 (mod 2^256).
c, r2 = mulAdd64(n2.n[2], n1.n[0], r2)
r3 += n2.n[2]*n1.n[1] + c
// Terms resulting from the product of the fourth digit of the second number
// by all digits of the first number except those that are guaranteed to be
// ≡ 0 (mod 2^256).
r3 += n2.n[3] * n1.n[0]
n.n[0], n.n[1], n.n[2], n.n[3] = r0, r1, r2, r3
return n
}
// Mul multiplies the passed uint256 by the existing value in n modulo 2^256
// and stores the result in n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.Mul(n2).AddUint64(1) so that n = (n * n2) + 1.
func (n *Uint256) Mul(n2 *Uint256) *Uint256 {
return n.Mul2(n, n2)
}
// MulUint64 multiplies the uint256 by the passed uint64 and stores the result
// in n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.MulUint64(2).Add(n2) so that n = 2 * n + n2.
func (n *Uint256) MulUint64(n2 uint64) *Uint256 {
// The general strategy employed here is:
// 1) Calculate the 320-bit product of the uint256 and uint64 using standard
// schoolbook multiplication.
// 2) Reduce the result modulo 2^256.
//
// However, some optimizations are used versus naively calculating all
// intermediate terms:
// 1) Reuse the high 64 bits from the intermediate 128-bit products directly
// since that is equivalent to shifting the result right by 64 bits.
// 2) Use native uint64s for the calculations involving the final digit of
// the result because all overflow carries to bits >= 256 which is ≡ 0
// (mod 2^256) given they are necessarily multiples of it.
var c uint64
c, n.n[0] = bits.Mul64(n.n[0], n2)
c, n.n[1] = mulAdd64(n.n[1], n2, c)
c, n.n[2] = mulAdd64(n.n[2], n2, c)
n.n[3] = n.n[3]*n2 + c
return n
}
// SquareVal squares the passed uint256 modulo 2^256 and stores the result in
// n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.SquareVal(n2).Mul(n2) so that n = n2^2 * n2 = n2^3.
func (n *Uint256) SquareVal(n2 *Uint256) *Uint256 {
// Similar to multiplication, the general strategy employed here is:
// 1) Calculate the 512-bit product of the two uint256s using standard
// schoolbook multiplication.
// 2) Reduce the result modulo 2^256.
//
// However, some optimizations are used versus naively calculating all
// intermediate terms:
// 1) Reuse the high 64 bits from the intermediate 128-bit products directly
// since that is equivalent to shifting the result right by 64 bits.
// 2) Ignore all of the products between individual digits that would
// ordinarily be needed for the full 512-bit product when they are
// guaranteed to result in values that fall in the half open interval
// [2^256, 2^512) since they are all ≡ 0 (mod 2^256) given they are
// necessarily multiples of it.
// 3) Use native uint64s for the calculations involving the final digit of
// the result (r3) because all overflow carries to bits >= 256 which, as
// above, are all ≡ 0 (mod 2^256) and thus safe to discard.
// 4) Use the fact the number is being multiplied by itself to take
// advantage of symmetry to double the result of some individual products
// versus calculating them again.
var r0, r1, r2, r3, c uint64
// Terms resulting from the product of the first digit of the second number
// by all digits of the first number except those that will be accounted for
// later via symmetry.
c, r0 = bits.Mul64(n2.n[0], n2.n[0])
c, r1 = mulAdd64(n2.n[0], n2.n[1], c)
c, r2 = mulAdd64(n2.n[0], n2.n[2], c)
r3 = c
// Terms resulting from the product of the second digit of the second number
// by all digits of the first number except those that will be accounted for
// later via symmetry and those that are guaranteed to be ≡ 0 (mod 2^256).
c, r1 = mulAdd64(n2.n[1], n2.n[0], r1)
c, r2 = mulAdd64Carry(n2.n[1], n2.n[1], r2, c)
r3 += c
// Terms resulting from the product of the third digit of the second number
// by all digits of the first number except those that will be accounted for
// later via symmetry and those that are guaranteed to be ≡ 0 (mod 2^256).
c, r2 = mulAdd64(n2.n[2], n2.n[0], r2)
r3 += c
// Terms resulting from the product of the fourth digit of the second number
// by all digits of the first number except those that are guaranteed to be
// ≡ 0 (mod 2^256) and doubling those that were skipped earlier.
r3 += 2 * (n2.n[0]*n2.n[3] + n2.n[1]*n2.n[2])
n.n[0], n.n[1], n.n[2], n.n[3] = r0, r1, r2, r3
return n
}
// Square squares the uint256 modulo 2^256 and stores the result in n.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.Square().Mul(n2) so that n = n^2 * n2.
func (n *Uint256) Square() *Uint256 {
return n.SquareVal(n)
}
// numDigits returns the number of base 2^64 digits required to represent the
// uint256. The result is 0 when the value is 0.
func (n *Uint256) numDigits() int {
for i := 3; i >= 0; i-- {
if n.n[i] != 0 {
return i + 1
}
}
return 0
}
// prefixLt returns whether or not the first argument is less than the prefix of
// the same length from the second when treating both as little-endian encoded
// integers. That is, it returns true when a < b[0:len(a)].
func prefixLt(a, b []uint64) bool {
var borrow uint64
for i := 0; i < len(a); i++ {
_, borrow = bits.Sub64(a[i], b[i], borrow)
}
return borrow != 0
}
// Div2 divides the passed uint256 dividend by the passed uint256 divisor modulo
// 2^256 and stores the result in n. It will panic if the divisor is 0.
//
// This implements truncated division like native Go integers and it is safe to
// alias the arguments.
//
// The uint256 is returned to support chaining. This enables syntax like:
// n.Div2(n1, n2).AddUint64(1) so that n = (n1 / n2) + 1.
func (n *Uint256) Div2(dividend, divisor *Uint256) *Uint256 {
if divisor.IsZero() {
panic("division by zero")
}
// Fast path for a couple of obvious cases. The result is zero when the
// divisor is larger than the dividend and one when they are equal. The
// remaining code also has preconditions on these cases already being
// handled.
if divisor.Gt(dividend) {
n.Zero()
return n
}
if dividend.Eq(divisor) {
return n.SetUint64(1)
}
// When the dividend can be fully represented by a uint64, perform the
// division using native uint64s since the divisor is also guaranteed to be
// fully representable as a uint64 given it was already confirmed to be
// smaller than the dividend above.
if dividend.IsUint64() {
return n.SetUint64(dividend.n[0] / divisor.n[0])
}
// When the divisor can be fully represented by a uint64, the divisor only
// consists of a single base 2^64 digit, so use that fact to avoid extra
// work. It is important to note that the algorithm below also requires the
// divisor to be at least two digits, so this is not solely a performance
// optimization.
if divisor.IsUint64() {
// The range here satisfies the following inequality:
// 1 ≤ divisor < 2^64 ≤ dividend < 2^256
var quotient Uint256
var r uint64
for d := dividend.numDigits() - 1; d >= 0; d-- {
quotient.n[d], r = bits.Div64(r, dividend.n[d], divisor.n[0])
}
return n.Set("ient)
}
// The range at this point satisfies the following inequality:
// 2^64 ≤ divisor < dividend < 2^256
// -------------------------------------------------------------------------
// The following is loosely based on Algorithm 4.3.1D of [TAOCP2] and is
// therefore based on long (aka schoolbook) division. It has been modified
// as compared to said algorithm in the following ways for performance
// reasons:
//
// 1. It uses full words instead of splitting each one into half words
// 2. It does not perform the test which effectively expands the estimate to
// be based on 3 digits of the dividend/remainder divided by 2 digits
// of the divisor instead of 2 digits by 1 (Step D3)
// 3. It conditionally subtracts the partial product from the partial
// remainder in the case the latter is less than the former instead of
// always subtracting it and then conditionally adding back (Steps D4,
// D5, and D6)
// 4. It only computes the active part of the remainder and throws the rest
// away since it is not needed here (parts of Steps D3, D4, D5, and D6)
// 5. It skips undoing the normalization for the remainder since it is not
// needed here (Step D8)
//
// The primary rationale for these changes follows:
//
// In regards to (1), almost all modern hardware provides native 128-bit by
// 64-bit division operations as well as 64-bit by 64-bit multiplication
// operations which the Go compiler uses in place of the respective
// `bits.Div64/Mul64` and, as a further benefit, using full words cuts the
// number of iterations in half.
//
// The test referred to by (2) serves to pin the potential error in the
// estimated quotient to be a max of 1 too high instead of a max of 2 as
// well as reduce the probability that single correction is needed at all.
// However, in order to achieve that, given full 64-bit words are used here,
// the Knuth method would involve an unconditional test which relies on
// comparing the result of a software-implemented 128-bit product against
// another 128-bit product as well as a 128-bit addition with carry. That
// is typically a sensible tradeoff when the number of digits involved is
// large and when working with half words since an additional potential
// correction would typically cost more than the test under those
// circumstances.
//
// However, in this implementation, it ends being faster to perform the
// extra correction than it is to perform the aforementioned test. The
// primary reasons for this are:
//
// - The number of digits is ≤ 4 which means very few operations are needed
// for the correction
// - Both the comparison of the product against the remainder and the
// conditional subtraction of the product from the remainder are able to
// make use of hardware acceleration that almost all modern hardware
// provides
//
// Moreover, the probability of the initial estimate being correct
// significantly increases as the size of the base grows. Given this
// implementation uses a base of 2^64, the probability of needing a
// correction is extremely low.
//
// Concretely, for quotient digits that are uniformly distributed, the
// probability of guessing the initial estimate correctly with normalization
// for a base of 2^64 is: 1 - 2/2^64 ~= 99.99999999999999999%. In other
// words, on average, roughly only 1 out of every 10 quintillion (10^19)
// will require an adjustment.
// -------------------------------------------------------------------------
// Start by normalizing the arguments.
//
// For reference, normalization is the process of scaling the operands such
// that the leading digit of the divisor is greater than or equal to half of
// the base (also often called the radix). Since this implementation uses a
// base of 2^64, half of that equates to 2^63. Mathematically:
// 2^63 ≤ leading digit of divisor < 2^64
//
// Note that the number of digits in each operand at this point satisfies
// the following inequality:
// 2 ≤ num divisor digits ≤ num dividend digits ≤ 4
//
// Further, notice that being ≥ 2^63 is equivalent to a digit having its
// most significant bit set. This means the scaling factor can very quickly
// be determined by counting the number of leading zeros and applied to the
// operands via bit shifts.
//
// This process significantly reduces the probability of requiring an
// adjustment to the initial estimate for quotient digits later as per the
// previous comments as well as bounds the possible number of error
// adjustments.
numDivisorDigits := divisor.numDigits()
numDividendDigits := dividend.numDigits()
sf := uint8(bits.LeadingZeros64(divisor.n[numDivisorDigits-1]))
var divisorN [4]uint64
var dividendN [5]uint64
if sf > 0 {
// Normalize the divisor by scaling it per the scaling factor.
for i := numDivisorDigits - 1; i > 0; i-- {
divisorN[i] = divisor.n[i]<<sf | divisor.n[i-1]>>(64-sf)
}
divisorN[0] = divisor.n[0] << sf
// Scale the dividend by the same scaling factor too.
dividendN[numDividendDigits] = dividend.n[numDividendDigits-1] >> (64 - sf)
for i := numDividendDigits - 1; i > 0; i-- {
dividendN[i] = dividend.n[i]<<sf | dividend.n[i-1]>>(64-sf)
}
dividendN[0] = dividend.n[0] << sf
} else {
copy(divisorN[:], divisor.n[:])
copy(dividendN[:], dividend.n[:])
}
// NOTE: The number of digits in the non-normalized dividend and divisor
// satisfies the following inequality at this point:
// 2 ≤ numDivisorDigits ≤ numDividendDigits ≤ 4
//
// Therefore the maximum value of d in the loop is 2.
var p [5]uint64
var qhat, c, borrow uint64
n.Zero()
for d := numDividendDigits - numDivisorDigits; d >= 0; d-- {
// Estimate the quotient digit by dividing the 2 leading digits of the
// active part of the remainder by the leading digit of the normalized
// divisor.
//
// Per Theorems A and B in section 4.3.1 of [TAOCP2], when the leading
// digit of the divisor is normalized as described above, the estimated
// quotient digit (qhat) produced by this is guaranteed to be ≥ the
// correct one (q) and at most 2 more. Mathematically:
// qhat - 2 ≤ q ≤ qhat
//
// Further, the algorithm implemented here guarantees that the leading
// digit of the active part of the remainder will be ≤ the leading digit
// of the normalized divisor. When it is less, the estimated quotient
// digit will fit into a single word and everything is normal. However,
// in the equals case, a second word would be required meaning the
// calculation would overflow.
//
// Combining that with the theorem above, the estimate in the case of
// overflow must either be one or two more than the maximum possible
// value for a single digit. In either case, assuming a type capable of
// representing values larger than a single digit without overflow were
// used instead, the quotient digit would have to be adjusted downwards
// until it was correct, and given the correct quotient digit must fit
// into a single word, it will necessarily either be the maximum
// possible value allowed for a single digit or one less. Therefore,
// the equality check here bypasses the overflow by setting the estimate
// to the max possible value and allowing the loop below to handle the
// potential remaining (extremely rare) overestimate.
//
// Also note that this behavior is not merely an optimization to skip
// the 128-bit by 64-bit division as both the hardware instruction and
// the bits.Div64 implementation would otherwise lead to a panic due to
// overflow as well.
//
// For some intuition, consider the base 10 case with a divisor of 5
// (recall the divisor is normalized, so it must be at least half the
// base). Then the maximum 2-digit dividend with the leading digit less
// than 5 is 49 and the minimum with the leading digit equal to 5 is 50.
// Observe that floor(49/5) = 9 is a single digit result, but
// floor(50/5) = 10 is not. The worst overestimate case for these
// conditions would occur at floor(59/5) = floor(11.8) = 11 which is
// indeed 2 more than the maximum possible single digit 9.
if dividendN[d+numDivisorDigits] == divisorN[numDivisorDigits-1] {
qhat = ^uint64(0)
} else {
qhat, _ = bits.Div64(dividendN[d+numDivisorDigits],
dividendN[d+numDivisorDigits-1], divisorN[numDivisorDigits-1])
}
// Calculate the product of the estimated quotient digit and divisor.
//
// This is semantically equivalent to the following if uint320 were
// supported: