| 1 |
package ff |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/big" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func Test_double(t *testing.T) { |
| 10 |
type args struct { |
| 11 |
a Element |
| 12 |
} |
| 13 |
tests := []struct { |
| 14 |
name string |
| 15 |
args args |
| 16 |
wantB Element |
| 17 |
}{ |
| 18 |
{"1", args{Element{1, 0, 0, 0}}, Element{2, 0, 0, 0}}, |
| 19 |
{"2^64", args{Element{0, 1, 0, 0}}, Element{0, 2, 0, 0}}, |
| 20 |
} |
| 21 |
for _, tt := range tests { |
| 22 |
t.Run(tt.name, func(t *testing.T) { |
| 23 |
if gotB := double(tt.args.a); !reflect.DeepEqual(gotB, tt.wantB) { |
| 24 |
t.Errorf("double() = %v, want %v", gotB, tt.wantB) |
| 25 |
} |
| 26 |
}) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func Test_double_random(t *testing.T) { |
| 31 |
for i := 0; i < 100; i++ { |
| 32 |
a := randomElement(i) |
| 33 |
a[3] = a[3] & 0x7fffffffffffffff |
| 34 |
got := double(a) |
| 35 |
want := BigIntToElement(new(big.Int).Lsh(ElementToBigInt(a), 1)) |
| 36 |
if got != want { |
| 37 |
t.Errorf("/%d double(%v) = %v, want %v", i, a, got, want) |
| 38 |
t.FailNow() |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func Benchmark_double(b *testing.B) { |
| 44 |
a := randomElement(1) |
| 45 |
a[3] = a[3] & 0x7fffffffffffffff |
| 46 |
b.ResetTimer() |
| 47 |
for i := 0; i < b.N; i++ { |
| 48 |
double(a) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func Benchmark_double_BI(b *testing.B) { |
| 53 |
a := ElementToBigInt(randomElement(1)) |
| 54 |
z := new(big.Int) |
| 55 |
b.ResetTimer() |
| 56 |
for i := 0; i < b.N; i++ { |
| 57 |
z.Lsh(a, 1) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func Benchmark_double_add(b *testing.B) { |
| 62 |
a := randomElement(1) |
| 63 |
b.ResetTimer() |
| 64 |
for i := 0; i < b.N; i++ { |
| 65 |
add(a, a) |
| 66 |
} |
| 67 |
} |