Debian Codegraph @

ff/mul64_test.go

raw symbols SCIP
1 package ff
2
3 import (
4 "math/big"
5 "math/rand"
6 "reflect"
7 "testing"
8 )
9
10 func Test_mul64(t *testing.T) {
11 type args struct {
12 x uint64
13 y uint64
14 }
15 tests := []struct {
16 name string
17 args args
18 wantZ [2]uint64
19 }{
20 {"2*1", args{2, 1}, [2]uint64{2, 0}},
21 {"(2^64 - 1)(2^64 - 1)", args{0xffffffffffffffff, 0xffffffffffffffff}, [2]uint64{1, 0xfffffffffffffffe}},
22 }
23 for _, tt := range tests {
24 t.Run(tt.name, func(t *testing.T) {
25 if gotZ := mul64(tt.args.x, tt.args.y); !reflect.DeepEqual(gotZ, tt.wantZ) {
26 t.Errorf("mul64() = %v, want %v", gotZ, tt.wantZ)
27 }
28 })
29 }
30 }
31
32 func Test_mul64_random(t *testing.T) {
33 r := rand.NewSource(1)
34 for i := 0; i < 100; i++ {
35 a := uint64(r.Int63())
36 b := uint64(r.Int63())
37 A := new(big.Int).SetUint64(a)
38 B := new(big.Int).SetUint64(b)
39 got := mul64(a, b)
40 C1 := new(big.Int).Mul(A, B)
41 want := [2]uint64{C1.Uint64(), C1.Rsh(C1, 64).Uint64()}
42 if got != want {
43 t.Errorf("/%d mul64(%v,%v) = %v, want %v", i, a, b, got, want)
44 t.FailNow()
45 }
46 }
47 }
48
49 func Benchmark_mul64(b *testing.B) {
50 r := rand.NewSource(1)
51 x := uint64(r.Int63())
52 y := uint64(r.Int63())
53 b.ResetTimer()
54 for i := 0; i < b.N; i++ {
55 mul64(x, y)
56 }
57 }
58
59 func Benchmark_mul64_BI(b *testing.B) {
60 r := rand.NewSource(1)
61 x := uint64(r.Int63())
62 y := uint64(r.Int63())
63 A := new(big.Int).SetUint64(x)
64 B := new(big.Int).SetUint64(y)
65 C := new(big.Int)
66 b.ResetTimer()
67 for i := 0; i < b.N; i++ {
68 C.Mul(A, B)
69 }
70 }