...
1 package math
2
3 import (
4 "crypto/rand"
5 "fmt"
6 "math/big"
7 "testing"
8
9 "github.com/cloudflare/circl/internal/test"
10 )
11
12 func TestOmegaNAF(t *testing.T) {
13 testTimes := 1 << 7
14 var max big.Int
15 max.SetInt64(1)
16 max.Lsh(&max, 128)
17
18 for w := uint(2); w < 10; w++ {
19 for j := 0; j < testTimes; j++ {
20 x, _ := rand.Int(rand.Reader, &max)
21 L := OmegaNAF(x, w)
22
23 var y big.Int
24 for i := len(L) - 1; i >= 0; i-- {
25 y.Add(&y, &y).Add(&y, big.NewInt(int64(L[i])))
26 }
27 want := x
28 got := &y
29 if got.Cmp(want) != 0 {
30 test.ReportError(t, got, want, x, w)
31 }
32 }
33 }
34 }
35
36 func TestOmegaNAFRegular(t *testing.T) {
37 testTimes := 1 << 7
38 Two128 := big.NewInt(1)
39 Two128.Lsh(Two128, 128)
40
41 for w := uint(2); w < 10; w++ {
42 for j := 0; j < testTimes; j++ {
43 x, _ := rand.Int(rand.Reader, Two128)
44 x.SetBit(x, 0, uint(1))
45 L := SignedDigit(x, w, 128)
46
47 var y big.Int
48 for i := len(L) - 1; i >= 0; i-- {
49 y.Lsh(&y, w-1)
50 y.Add(&y, big.NewInt(int64(L[i])))
51 }
52 want := x
53 got := &y
54 if got.Cmp(want) != 0 {
55 test.ReportError(t, got, want, x, w)
56 }
57 }
58 }
59 }
60
61 func BenchmarkOmegaNAF(b *testing.B) {
62 Two128 := big.NewInt(1)
63 Two128.Lsh(Two128, 128)
64
65 for w := uint(2); w < 6; w++ {
66 w := w
67 b.Run(fmt.Sprintf("%v", w), func(b *testing.B) {
68 x, _ := rand.Int(rand.Reader, Two128)
69 b.ResetTimer()
70 for i := 0; i < b.N; i++ {
71 _ = OmegaNAF(x, w)
72 }
73 })
74 }
75 }
76
77 func BenchmarkOmegaNAFRegular(b *testing.B) {
78 Two128 := big.NewInt(1)
79 Two128.Lsh(Two128, 128)
80
81 for w := uint(2); w < 6; w++ {
82 w := w
83 b.Run(fmt.Sprintf("%v", w), func(b *testing.B) {
84 x, _ := rand.Int(rand.Reader, Two128)
85 x.SetBit(x, 0, uint(1))
86 b.ResetTimer()
87 for i := 0; i < b.N; i++ {
88 _ = SignedDigit(x, w, 128)
89 }
90 })
91 }
92 }
93
View as plain text