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