-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathuint256_bench_test.go
1325 lines (1181 loc) · 35.2 KB
/
uint256_bench_test.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 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package uint256
import (
"fmt"
"math/big"
"math/rand"
"testing"
"time"
)
// These variables are used to help ensure the benchmarks do not elide code.
var (
noElideBool bool
noElideBytes []byte
noElideInt int
noElideUint16 uint16
noElideString string
)
// randBenchVal houses values used throughout the benchmarks that are randomly
// generated with each run to ensure they are not overfitted.
type randBenchVal struct {
buf1 [32]byte
buf2 [32]byte
n1 *Uint256
n2 *Uint256
n2Low64 *Uint256
bigN1 *big.Int
bigN2 *big.Int
bigN2Low64 *big.Int
}
// randBenchVals houses a slice of the aforementioned randomly-generated values
// to be used throughout the benchmarks to ensure they are not overfitted.
var randBenchVals = func() []randBenchVal {
// Use a unique random seed each benchmark instance.
seed := time.Now().Unix()
rng := rand.New(rand.NewSource(seed))
var zeroArr [32]byte
vals := make([]randBenchVal, 512)
for i := 0; i < len(vals); i++ {
val := &vals[i]
if _, err := rng.Read(val.buf1[:]); err != nil {
panic(fmt.Sprintf("failed to read random: %v", err))
}
for val.buf2 == zeroArr {
if _, err := rng.Read(val.buf2[:]); err != nil {
panic(fmt.Sprintf("failed to read random: %v", err))
}
}
val.n1 = new(Uint256).SetBytes(&val.buf1)
val.n2 = new(Uint256).SetBytes(&val.buf2)
val.n2Low64 = new(Uint256).SetUint64(val.n2.Uint64())
val.bigN1 = new(big.Int).SetBytes(val.buf1[:])
val.bigN2 = new(big.Int).SetBytes(val.buf2[:])
val.bigN2Low64 = new(big.Int).SetUint64(val.n2.Uint64())
}
return vals
}()
// maxUint256Bytes returns the raw bytes for a max value unsigned 256-bit
// big-endian integer used throughout the benchmarks.
func maxUint256Bytes() []byte {
return hexToBytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
}
// BenchmarkUint256SetBytes benchmarks initializing an unsigned 256-bit integer
// from bytes in big endian with the specialized type.
func BenchmarkUint256SetBytes(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
n.SetBytes(&vals[j].buf1)
}
}
}
// BenchmarkBigIntSetBytes benchmarks initializing an unsigned 256-bit integer
// from bytes in big endian with stdlib big integers.
func BenchmarkBigIntSetBytes(b *testing.B) {
n := new(big.Int)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
n.SetBytes(vals[j].buf1[:])
}
}
}
// BenchmarkUint256SetBytesLE benchmarks initializing an unsigned 256-bit
// integer from bytes in little endian with the specialized type.
func BenchmarkUint256SetBytesLE(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
n.SetBytesLE(&vals[j].buf1)
}
}
}
// BenchmarkBigIntSetBytesLE benchmarks initializing an unsigned 256-bit integer
// from bytes in little endian with stdlib big integers.
func BenchmarkBigIntSetBytesLE(b *testing.B) {
n := new(big.Int)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
// The big int API only takes the bytes in big endian, so they need
// to be reversed by the caller. Note that this implementation
// assumes the buffer is already 32 bytes and is not robust against
// other cases, but it's good enough for a reasonable benchmark.
buf := vals[j].buf1[:]
reversed := make([]byte, len(buf))
blen := len(buf)
for i := 0; i < blen/2; i++ {
reversed[i], reversed[blen-1-i] = buf[blen-1-i], buf[i]
}
n.SetBytes(reversed)
}
}
}
// BenchmarkUint256Bytes benchmarks unpacking an unsigned 256-bit integer to
// bytes in big endian with the specialized type.
func BenchmarkUint256Bytes(b *testing.B) {
vals := randBenchVals
var buf [32]byte
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
vals[j].n1.PutBytes(&buf)
}
}
}
// BenchmarkBigIntBytes benchmarks unpacking an unsigned 256-bit integer to
// bytes in big endian with the stdlib big integers.
func BenchmarkBigIntBytes(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
noElideBytes = vals[j].bigN1.Bytes()
}
}
}
// BenchmarkUint256BytesLE benchmarks unpacking an unsigned 256-bit integer to
// bytes in little endian with the specialized type.
func BenchmarkUint256BytesLE(b *testing.B) {
vals := randBenchVals
var buf [32]byte
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
vals[j].n1.PutBytesLE(&buf)
}
}
}
// BenchmarkBigIntBytesLE benchmarks unpacking an unsigned 256-bit integer to
// bytes in little endian with the stdlib big integers.
func BenchmarkBigIntBytesLE(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
// The big int API only provides the bytes in big endian, so they
// need to be reversed by the caller. Note that this implementation
// assumes the buffer is already 32 bytes and is not robust against
// other cases, but it's good enough for a reasonable benchmark.
buf := vals[j].bigN1.Bytes()
blen := len(buf)
for i := 0; i < blen/2; i++ {
buf[i], buf[blen-1-i] = buf[blen-1-i], buf[i]
}
noElideBytes = buf
}
}
}
// BenchmarkUint256Zero benchmarks zeroing an unsigned 256-bit integer with the
// specialized type.
func BenchmarkUint256Zero(b *testing.B) {
n := new(Uint256).SetByteSlice(maxUint256Bytes())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
n.Zero()
}
}
// BenchmarkBigIntZero benchmarks zeroing an unsigned 256-bit integer with
// stdlib big integers.
func BenchmarkBigIntZero(b *testing.B) {
n := new(big.Int).SetBytes(maxUint256Bytes())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
n.SetUint64(0)
}
}
// BenchmarkUint256IsZero benchmarks determining if an unsigned 256-bit integer
// is zero with the specialized type.
func BenchmarkUint256IsZero(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
noElideBool = vals[j].n1.IsZero()
}
}
}
// BenchmarkBigIntIsZero benchmarks determining if an unsigned 256-bit integer
// is zero with stdlib big integers.
func BenchmarkBigIntIsZero(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
noElideBool = vals[j].bigN1.Sign() == 0
}
}
}
// BenchmarkUint256IsOdd benchmarks determining if an unsigned 256-bit integer
// is odd with the specialized type.
func BenchmarkUint256IsOdd(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
noElideBool = vals[j].n1.IsOdd()
}
}
}
// BenchmarkBigIntIsOdd benchmarks determining if an unsigned 256-bit integer is
// odd with stdlib big integers.
func BenchmarkBigIntIsOdd(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
noElideBool = vals[j].bigN1.Bit(0) == 1
}
}
}
// BenchmarkUint256Eq benchmarks determining equality between two unsigned
// 256-bit integers with the specialized type.
func BenchmarkUint256Eq(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideBool = val.n1.Eq(val.n2)
}
}
}
// BenchmarkBigIntEq benchmarks determining equality between two unsigned
// 256-bit integers with stdlib big integers.
func BenchmarkBigIntEq(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideBool = val.bigN1.Cmp(val.bigN2) == 0
}
}
}
// BenchmarkUint256Lt benchmarks determining if one unsigned 256-bit integer is
// less than another with the specialized type.
func BenchmarkUint256Lt(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideBool = val.n1.Lt(val.n2)
}
}
}
// BenchmarkBigIntLt benchmarks determining if one unsigned 256-bit integer is
// less than another with stdlib big integers.
func BenchmarkBigIntLt(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideBool = val.bigN1.Cmp(val.bigN2) < 0
}
}
}
// BenchmarkUint256Gt benchmarks determining if one unsigned 256-bit integer is
// greater than another with the specialized type.
func BenchmarkUint256Gt(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideBool = val.n1.Gt(val.n2)
}
}
}
// BenchmarkBigIntGt benchmarks determining if one unsigned 256-bit integer is
// greater than another with stdlib big integers.
func BenchmarkBigIntGt(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideBool = val.bigN1.Cmp(val.bigN2) > 0
}
}
}
// BenchmarkUint256Cmp benchmarks comparing two unsigned 256-bit integers with
// the specialized type.
func BenchmarkUint256Cmp(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideInt = val.n1.Cmp(val.n2)
}
}
}
// BenchmarkBigIntCmp benchmarks comparing two unsigned 256-bit integers with
// stdlib big integers.
func BenchmarkBigIntCmp(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideInt = val.bigN1.Cmp(val.bigN2)
}
}
}
// BenchmarkUint256CmpUint64 benchmarks comparing an unsigned 256-bit integer
// with an unsigned 64-bit integer with the specialized type.
func BenchmarkUint256CmpUint64(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideInt = val.n1.CmpUint64(val.n2Low64.Uint64())
}
}
}
// BenchmarkBigIntCmp benchmarks comparing an unsigned 256-bit integer with an
// unsigned 64-bit integer with stdlib big integers.
func BenchmarkBigIntCmpUint64(b *testing.B) {
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
noElideInt = val.bigN1.Cmp(val.bigN2Low64)
}
}
}
// BenchmarkUint256Add benchmarks computing the sum of unsigned 256-bit integers
// with the specialized type.
func BenchmarkUint256Add(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Add2(val.n1, val.n2)
}
}
}
// BenchmarkBigIntAdd benchmarks computing the sum of unsigned 256-bit integers
// with stdlib big integers.
func BenchmarkBigIntAdd(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Add(val.bigN1, val.bigN2)
n.Mod(n, two256)
}
}
}
// BenchmarkUint256AddUint64 benchmarks computing the sum of an unsigned 256-bit
// integer and an unsigned 64-bit integer with the specialized type.
func BenchmarkUint256AddUint64(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Set(val.n1)
n.AddUint64(val.n2Low64.Uint64())
}
}
}
// BenchmarkBigIntAddUint64 benchmarks computing the sum of an unsigned 256-bit
// integer and an unsigned 64-bit integer with stdlib big integers.
func BenchmarkBigIntAddUint64(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Set(val.bigN1) // For fair comparison.
n.Add(n, val.bigN2Low64)
n.Mod(n, two256)
}
}
}
// BenchmarkUint256Sub benchmarks computing the difference of unsigned 256-bit
// integers with the specialized type.
func BenchmarkUint256Sub(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Sub2(val.n1, val.n2)
}
}
}
// BenchmarkBigIntSub benchmarks computing the difference of unsigned 256-bit
// integers with stdlib big integers.
func BenchmarkBigIntSub(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Sub(val.bigN1, val.bigN2)
n.Mod(n, two256)
}
}
}
// BenchmarkUint256SubUint64 benchmarks computing the difference of an unsigned
// 256-bit integer and unsigned 64-bit integer with the specialized type.
func BenchmarkUint256SubUint64(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Set(val.n1)
n.SubUint64(val.n2Low64.Uint64())
}
}
}
// BenchmarkBigIntSubUint64 benchmarks computing the difference of an unsigned
// 256-bit integer and unsigned 64-bit integer with stdlib big integers.
func BenchmarkBigIntSubUint64(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Set(val.bigN1) // For fair comparison.
n.Sub(n, val.bigN2Low64)
n.Mod(n, two256)
}
}
}
// BenchmarkUint256Mul benchmarks computing the product of unsigned 256-bit
// integers with the specialized type.
func BenchmarkUint256Mul(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Mul2(val.n1, val.n2)
}
}
}
// BenchmarkBigIntMul benchmarks computing the product of unsigned 256-bit
// integers with stdlib big integers.
func BenchmarkBigIntMul(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Mul(val.bigN1, val.bigN2)
n.Mod(n, two256)
}
}
}
// BenchmarkUint256MulUint64 benchmarks computing the product of an unsigned
// 256-bit integer and unsigned 64-bit integer with the specialized type.
func BenchmarkUint256MulUint64(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Set(val.n1)
n.MulUint64(val.n2Low64.Uint64())
}
}
}
// BenchmarkBigIntMulUint64 benchmarks computing the product of an unsigned
// 256-bit integer and unsigned 64-bit integer with stdlib big integers.
func BenchmarkBigIntMulUint64(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Set(val.bigN1) // For fair comparison.
n.Mul(n, val.bigN2Low64)
n.Mod(n, two256)
}
}
}
// BenchmarkUint256Square benchmarks computing the quotient of unsigned 256-bit
// integers with the specialized type.
func BenchmarkUint256Square(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.SquareVal(val.n1)
}
}
}
// BenchmarkBigIntSquare benchmarks computing the quotient of unsigned 256-bit
// integers with stdlib big integers.
func BenchmarkBigIntSquare(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Mul(val.bigN1, val.bigN1)
n.Mod(n, two256)
}
}
}
// divBenchTest describes tests that are used for the deterministic division
// benchmarks. It is defined separately so the same tests can easily be used in
// comparison benchmarks between the specialized type in this package and stdlib
// big integers.
type divBenchTest struct {
name string // benchmark description
n1 *Uint256 // dividend
n2 *Uint256 // divisor
}
// makeDivBenches returns a slice of tests that consist of deterministic
// unsigned 256-bit integers of various lengths for use in the division
// benchmarks.
func makeDivBenches() []divBenchTest {
return []divBenchTest{{
// (1<<256 - 2) / (1<<256 - 1)
name: "dividend lt divisor",
n1: hexToUint256("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"),
n2: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
}, {
// (1<<256 - 1) / (1<<256 - 1)
name: "dividend eq divisor",
n1: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
}, {
// (1<<64 - 1) / (1<<64 - 255)
name: "1 by 1 near",
n1: hexToUint256("000000000000000000000000000000000000000000000000ffffffffffffffff"),
n2: hexToUint256("000000000000000000000000000000000000000000000000ffffffffffffff01"),
}, {
// (1<<64 - 1) / (1<<2 - 1)
name: "1 by 1 far",
n1: hexToUint256("000000000000000000000000000000000000000000000000ffffffffffffffff"),
n2: hexToUint256("0000000000000000000000000000000000000000000000000000000000000003"),
}, {
// (1<<128 - 1) / (1<<64 - 1)
name: "2 by 1 near",
n1: hexToUint256("00000000000000000000000000000000ffffffffffffffffffffffffffffffff"),
n2: hexToUint256("000000000000000000000000000000000000000000000000ffffffffffffffff"),
}, {
// (1<<128 - 1) / (1<<2 - 1)
name: "2 by 1 far",
n1: hexToUint256("00000000000000000000000000000000ffffffffffffffffffffffffffffffff"),
n2: hexToUint256("0000000000000000000000000000000000000000000000000000000000000003"),
}, {
// (1<<192 - 1) / (1<<64 - 1)
name: "3 by 1 near",
n1: hexToUint256("0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("000000000000000000000000000000000000000000000000ffffffffffffffff"),
}, {
// (1<<192 - 1) / (1<<2 - 1)
name: "3 by 1 far",
n1: hexToUint256("0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("0000000000000000000000000000000000000000000000000000000000000003"),
}, {
// (1<<256 - 1) / (1<<64 - 1)
name: "4 by 1 near",
n1: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("000000000000000000000000000000000000000000000000ffffffffffffffff"),
}, {
// (1<<256 - 1) / (1<<2 - 1)
name: "4 by 1 far",
n1: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("0000000000000000000000000000000000000000000000000000000000000003"),
}, {
// (1<<128 - 1) / (1<<128 - 255)
name: "2 by 2 near",
n1: hexToUint256("00000000000000000000000000000000ffffffffffffffffffffffffffffffff"),
n2: hexToUint256("00000000000000000000000000000000ffffffffffffffffffffffffffffff01"),
}, {
// (1<<128 - 1) / (1<<65 - 1)
name: "2 by 2 far",
n1: hexToUint256("00000000000000000000000000000000ffffffffffffffffffffffffffffffff"),
n2: hexToUint256("000000000000000000000000000000000000000000000001ffffffffffffffff"),
}, {
// (1<<192 - 1) / (1<<128 - 1)
name: "3 by 2 near",
n1: hexToUint256("0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("00000000000000000000000000000000ffffffffffffffffffffffffffffffff"),
}, {
// (1<<192 - 1) / (1<<65 - 1)
name: "3 by 2 far",
n1: hexToUint256("0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("000000000000000000000000000000000000000000000001ffffffffffffffff"),
}, {
// (1<<256 - 1) / (1<<128 - 1)
name: "4 by 2 near",
n1: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("00000000000000000000000000000000ffffffffffffffffffffffffffffffff"),
}, {
// (1<<256 - 1) / (1<<65 - 1)
name: "4 by 2 far",
n1: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("000000000000000000000000000000000000000000000001ffffffffffffffff"),
}, {
// (1<<192 - 1) / (1<<192 - 255)
name: "3 by 3 near",
n1: hexToUint256("0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("0000000000000000ffffffffffffffffffffffffffffffffffffffffffffff01"),
}, {
// (1<<192 - 1) / (1<<129 - 1)
name: "3 by 3 far",
n1: hexToUint256("0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("00000000000000000000000000000001ffffffffffffffffffffffffffffffff"),
}, {
// (1<<256 - 1) / (1<<192 - 1)
name: "4 by 3 near",
n1: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff"),
}, {
// (1<<256 - 1) / (1<<129 - 1)
name: "4 by 3 far",
n1: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("00000000000000000000000000000001ffffffffffffffffffffffffffffffff"),
}, {
// (1<<256 - 1) / (1<<256 - 255)
name: "4 by 4 near",
n1: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01"),
}, {
// (1<<256 - 1) / (1<<193 - 1)
name: "4 by 4 far",
n1: hexToUint256("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
n2: hexToUint256("0000000000000001ffffffffffffffffffffffffffffffffffffffffffffffff"),
}}
}
// BenchmarkUint256Div benchmarks computing the quotient of deterministic
// unsigned 256-bit integers of various length with the specialized type.
func BenchmarkUint256Div(b *testing.B) {
benches := makeDivBenches()
for benchIdx := range benches {
bench := benches[benchIdx]
b.Run(bench.name, func(b *testing.B) {
n := new(Uint256)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
n.Div2(bench.n1, bench.n2)
}
})
}
}
// BenchmarkUint256DivRandom benchmarks computing the quotient of random large
// unsigned 256-bit integers with the specialized type.
func BenchmarkUint256DivRandom(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Div2(val.n1, val.n2)
}
}
}
// BenchmarkBigIntDiv benchmarks computing the quotient of deterministic
// unsigned 256-bit integers of various length with stdlib big integers.
func BenchmarkBigIntDiv(b *testing.B) {
benches := makeDivBenches()
for benchIdx := range benches {
bench := benches[benchIdx]
b.Run(bench.name, func(b *testing.B) {
n := new(big.Int)
n1Bytes, n2Bytes := bench.n1.Bytes(), bench.n2.Bytes()
n1 := new(big.Int).SetBytes(n1Bytes[:])
n2 := new(big.Int).SetBytes(n2Bytes[:])
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
n.Div(n1, n2)
}
})
}
}
// BenchmarkBigIntDivRandom benchmarks computing the quotient of random large
// unsigned 256-bit integers with stdlib big integers.
func BenchmarkBigIntDivRandom(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Div(val.bigN1, val.bigN2)
n.Mod(n, two256)
}
}
}
// BenchmarkUint256DivUint64 benchmarks computing the quotient of an unsigned
// 256-bit integer and unsigned 64-bit integer with the specialized type.
func BenchmarkUint256DivUint64(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Set(val.n1)
n.DivUint64(val.n2Low64.Uint64())
}
}
}
// BenchmarkBigIntDivUint64 benchmarks computing the quotient of an unsigned
// 256-bit integer and unsigned 64-bit integer with stdlib big integers.
func BenchmarkBigIntDivUint64(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
val := &vals[j]
n.Div(val.bigN1, val.bigN2Low64)
n.Mod(n, two256)
}
}
}
// BenchmarkUint256Negate benchmarks computing the negation modulo 2^256 of an
// unsigned 256-bit integer with the specialized type.
func BenchmarkUint256Negate(b *testing.B) {
n := new(Uint256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
n.NegateVal(vals[j].n1)
}
}
}
// BenchmarkBigIntNegate benchmarks computing the negation module 2^256 of an
// unsigned 256-bit integer with stdlib big integers.
func BenchmarkBigIntNegate(b *testing.B) {
n := new(big.Int)
two256 := new(big.Int).Lsh(big.NewInt(1), 256)
vals := randBenchVals
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals); j++ {
n.Mod(n.Neg(vals[j].bigN1), two256)
}
}
}
// BenchmarkUint256Lsh benchmarks left shifting an unsigned 256-bit integer with
// the specialized type.
func BenchmarkUint256Lsh(b *testing.B) {
for _, bits := range []uint32{0, 1, 64, 128, 192, 255, 256} {
benchName := fmt.Sprintf("bits %d", bits)
b.Run(benchName, func(b *testing.B) {
result := new(Uint256)
max256 := new(Uint256).SetByteSlice(maxUint256Bytes())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result.LshVal(max256, bits)
}
})
}
}
// BenchmarkBigIntLsh benchmarks left shifting an unsigned 256-bit integer with
// stdlib big integers.
func BenchmarkBigIntLsh(b *testing.B) {
for _, bits := range []uint{0, 1, 64, 128, 192, 255, 256} {
benchName := fmt.Sprintf("bits %d", bits)
b.Run(benchName, func(b *testing.B) {
result := new(big.Int)
max256 := new(big.Int).SetBytes(maxUint256Bytes())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result.Lsh(max256, bits)
}
})
}
}
// BenchmarkUint256Rsh benchmarks right shifting an unsigned 256-bit integer
// with the specialized type.
func BenchmarkUint256Rsh(b *testing.B) {
for _, bits := range []uint32{0, 1, 64, 128, 192, 255, 256} {
benchName := fmt.Sprintf("bits %d", bits)
b.Run(benchName, func(b *testing.B) {
result := new(Uint256)
max256 := new(Uint256).SetByteSlice(maxUint256Bytes())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result.RshVal(max256, bits)
}
})