github.com/cloudflare/circl@v1.5.0/math/wnaf_test.go (about) 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)) // odd-numbers 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 b.Run(fmt.Sprintf("%v", w), func(b *testing.B) { 67 x, _ := rand.Int(rand.Reader, Two128) 68 b.ResetTimer() 69 for i := 0; i < b.N; i++ { 70 _ = OmegaNAF(x, w) 71 } 72 }) 73 } 74 } 75 76 func BenchmarkOmegaNAFRegular(b *testing.B) { 77 Two128 := big.NewInt(1) 78 Two128.Lsh(Two128, 128) 79 80 for w := uint(2); w < 6; w++ { 81 b.Run(fmt.Sprintf("%v", w), func(b *testing.B) { 82 x, _ := rand.Int(rand.Reader, Two128) 83 x.SetBit(x, 0, uint(1)) // odd-numbers 84 b.ResetTimer() 85 for i := 0; i < b.N; i++ { 86 _ = SignedDigit(x, w, 128) 87 } 88 }) 89 } 90 }