...
1 package ff
2
3 import (
4 "testing"
5
6 "github.com/cloudflare/circl/internal/test"
7 )
8
9 func randomFp6(t testing.TB) *Fp6 { return &Fp6{*randomFp2(t), *randomFp2(t), *randomFp2(t)} }
10
11
12 func expVarTime(z, x *Fp6, n []byte) {
13 zz := new(Fp6)
14 zz.SetOne()
15 N := 8 * len(n)
16 for i := 0; i < N; i++ {
17 zz.Sqr(zz)
18 bit := 0x1 & (n[i/8] >> uint(7-i%8))
19 if bit != 0 {
20 zz.Mul(zz, x)
21 }
22 }
23 *z = *zz
24 }
25
26 func TestFp6(t *testing.T) {
27 const testTimes = 1 << 10
28 t.Run("no_alias", func(t *testing.T) {
29 var want, got Fp6
30 x := randomFp6(t)
31 got = *x
32 got.Sqr(&got)
33 want = *x
34 want.Mul(&want, &want)
35 if got.IsEqual(&want) == 0 {
36 test.ReportError(t, got, want, x)
37 }
38 })
39 t.Run("mul_inv", func(t *testing.T) {
40 var z Fp6
41 for i := 0; i < testTimes; i++ {
42 x := randomFp6(t)
43 y := randomFp6(t)
44
45
46 z.Inv(x)
47 z.Mul(&z, y)
48 z.Mul(&z, x)
49 z.Sub(&z, y)
50 got := z.IsZero()
51 want := 1
52 if got != want {
53 test.ReportError(t, got, want, x, y)
54 }
55 }
56 })
57 t.Run("mul_sqr", func(t *testing.T) {
58 var l0, l1, r0, r1 Fp6
59 for i := 0; i < testTimes; i++ {
60 x := randomFp6(t)
61 y := randomFp6(t)
62
63
64 l0.Add(x, y)
65 l1.Sub(x, y)
66 l0.Mul(&l0, &l1)
67 r0.Sqr(x)
68 r1.Sqr(y)
69 r0.Sub(&r0, &r1)
70 got := &l0
71 want := &r0
72 if got.IsEqual(want) == 0 {
73 test.ReportError(t, got, want, x, y)
74 }
75 }
76 })
77 t.Run("frobenius", func(t *testing.T) {
78 var got, want Fp6
79 p := FpOrder()
80 for i := 0; i < testTimes; i++ {
81 x := randomFp6(t)
82
83
84 got.Frob(x)
85 expVarTime(&want, x, p)
86
87 if got.IsEqual(&want) == 0 {
88 test.ReportError(t, got, want, x)
89 }
90 }
91 })
92 }
93
94 func BenchmarkFp6(b *testing.B) {
95 x := randomFp6(b)
96 y := randomFp6(b)
97 z := randomFp6(b)
98 b.Run("Add", func(b *testing.B) {
99 for i := 0; i < b.N; i++ {
100 z.Add(x, y)
101 }
102 })
103 b.Run("Mul", func(b *testing.B) {
104 for i := 0; i < b.N; i++ {
105 z.Mul(x, y)
106 }
107 })
108 b.Run("Sqr", func(b *testing.B) {
109 for i := 0; i < b.N; i++ {
110 z.Sqr(x)
111 }
112 })
113 b.Run("Inv", func(b *testing.B) {
114 for i := 0; i < b.N; i++ {
115 z.Inv(x)
116 }
117 })
118 }
119
View as plain text