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
|
package math
import (
"crypto/rand"
"fmt"
"math/big"
"testing"
"github.com/cloudflare/circl/internal/test"
)
func TestOmegaNAF(t *testing.T) {
testTimes := 1 << 7
var max big.Int
max.SetInt64(1)
max.Lsh(&max, 128)
for w := uint(2); w < 10; w++ {
for j := 0; j < testTimes; j++ {
x, _ := rand.Int(rand.Reader, &max)
L := OmegaNAF(x, w)
var y big.Int
for i := len(L) - 1; i >= 0; i-- {
y.Add(&y, &y).Add(&y, big.NewInt(int64(L[i])))
}
want := x
got := &y
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x, w)
}
}
}
}
func TestOmegaNAFRegular(t *testing.T) {
testTimes := 1 << 7
Two128 := big.NewInt(1)
Two128.Lsh(Two128, 128)
for w := uint(2); w < 10; w++ {
for j := 0; j < testTimes; j++ {
x, _ := rand.Int(rand.Reader, Two128)
x.SetBit(x, 0, uint(1)) // odd-numbers
L := SignedDigit(x, w, 128)
var y big.Int
for i := len(L) - 1; i >= 0; i-- {
y.Lsh(&y, w-1)
y.Add(&y, big.NewInt(int64(L[i])))
}
want := x
got := &y
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x, w)
}
}
}
}
func BenchmarkOmegaNAF(b *testing.B) {
Two128 := big.NewInt(1)
Two128.Lsh(Two128, 128)
for w := uint(2); w < 6; w++ {
w := w // pin variable
b.Run(fmt.Sprintf("%v", w), func(b *testing.B) {
x, _ := rand.Int(rand.Reader, Two128)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = OmegaNAF(x, w)
}
})
}
}
func BenchmarkOmegaNAFRegular(b *testing.B) {
Two128 := big.NewInt(1)
Two128.Lsh(Two128, 128)
for w := uint(2); w < 6; w++ {
w := w // pin variable
b.Run(fmt.Sprintf("%v", w), func(b *testing.B) {
x, _ := rand.Int(rand.Reader, Two128)
x.SetBit(x, 0, uint(1)) // odd-numbers
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = SignedDigit(x, w, 128)
}
})
}
}
|