This repository was archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathg1_test.go
180 lines (168 loc) · 3.59 KB
/
g1_test.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package bls12
import (
"bytes"
"fmt"
"crypto/rand"
"io/ioutil"
"testing"
)
func BenchmarkUncompressG1(b *testing.B) {
p1 := new(G1).HashToPointFast([]byte("test2"))
b.ResetTimer()
m := p1.Marshal()
for i := 0; i < b.N; i++ {
p1.Unmarshal(m)
}
}
func BenchmarkBaseMultG1(b *testing.B) {
x, _ := rand.Int(rand.Reader, Order)
s := new(Scalar).FromInt(x)
g1 := G1{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
g1.ScalarBaseMult(s)
}
}
func BenchmarkMultG1(b *testing.B) {
x, _ := rand.Int(rand.Reader, Order)
s := new(Scalar).FromInt(x)
g1 := new(G2).HashToPointFast([]byte("yxxx"))
b.ResetTimer()
for i := 0; i < b.N; i++ {
g1.ScalarMult(s)
}
}
func BenchmarkHashToPointG1(b *testing.B) {
var buf [512]byte
var g1 G1
for i := 0; i < b.N; i++ {
buf[0] = byte(i)
buf[1] = byte(i >> 8)
buf[2] = byte(i >> 16)
g1.HashToPoint(buf[:])
}
}
func BenchmarkHashToPointFastG1(b *testing.B) {
var buf [512]byte
var g1 G1
for i := 0; i < b.N; i++ {
buf[0] = byte(i)
buf[1] = byte(i >> 8)
buf[2] = byte(i >> 16)
g1.HashToPointFast(buf[:])
}
}
func BenchmarkHashToPointRelicG1(b *testing.B) {
var buf [512]byte
var g1 G1
for i := 0; i < b.N; i++ {
buf[0] = byte(i)
buf[1] = byte(i >> 8)
buf[2] = byte(i >> 16)
g1.HashToPointRelic(buf[:])
}
}
func TestHashToPointG1(t *testing.T) {
var p G1
var buf [512]byte
for i := 0; i < 1000; i++ {
buf[0] = byte(i)
buf[1] = byte(i >> 8)
buf[2] = byte(i >> 16)
p.HashToPoint(buf[:])
if !p.Check() {
t.Fatalf("point landed in wrong subgroup for %d\n", i)
}
}
}
func TestHashToPointFastG1(t *testing.T) {
var p G1
var buf [512]byte
for i := 0; i < 1000; i++ {
buf[0] = byte(i)
buf[1] = byte(i >> 8)
buf[2] = byte(i >> 16)
p.HashToPointFast(buf[:])
if !p.Check() {
t.Fatalf("point landed in wrong subgroup for %d\n", i)
}
}
}
func TestVectorG1HashToPoint(t *testing.T) {
data := readFile(t, "testdata/g1_hashtopoint.dat")
for i := 0; i < 1000; i++ {
var g1, g1t G1
data = g1.Unmarshal(data)
if data == nil {
t.Fatal("failed to unmarshal test data")
}
g1t.HashToPoint([]byte(fmt.Sprintf("%d", i)))
if !g1t.Equal(&g1) {
t.Fatalf("wrong hash at %d\n", i)
}
}
}
func TestVectorG1Compressed(t *testing.T) {
// t.Run("Compressed", func(t *testing.T) {
var (
data = readFile(t, "testdata/g1_compressed_valid_test_vectors.dat")
ep = G1Zero()
a = &G1{}
one = G1One()
d = data
)
for i := 0; i < 1000; i++ {
ok := a.Unmarshal(d[:G1Size])
if ok == nil {
t.Errorf("%d: failed decoding", i)
}
if !ep.Equal(a) {
t.Errorf("%d: different point", i)
}
buf := ep.Marshal()
if !bytes.Equal(buf, d[:G1Size]) {
t.Logf("%d <- %x", i, d[:G1Size])
t.Logf("%d -> %x", i, buf)
t.Errorf("%d: different encoding", i)
}
d = d[G1Size:]
ep.Add(one)
}
// })
}
func TestVectorG1Uncompressed(t *testing.T) {
// t.Run("Uncompressed", func(t *testing.T) {
var (
data = readFile(t, "testdata/g1_uncompressed_valid_test_vectors.dat")
ep = G1Zero()
a = &G1{}
one = G1One()
d = data
)
for i := 0; i < 1000; i++ {
ok := a.Unmarshal(d[:G1UncompressedSize])
if ok == nil {
t.Errorf("%d: failed decoding", i)
}
if !ep.Equal(a) {
t.Errorf("%d: different point", i)
}
buf := ep.MarshalUncompressed()
if !bytes.Equal(buf, d[:G1UncompressedSize]) {
t.Logf("%d <- %x", i, d[:G1UncompressedSize])
t.Logf("%d -> %x", i, buf)
t.Errorf("%d: different encoding", i)
}
d = d[G1UncompressedSize:]
ep.Add(one)
}
// })
}
func readFile(t *testing.T, name string) []byte {
t.Helper()
res, err := ioutil.ReadFile(name)
if err != nil {
t.Fatal(err)
}
return res
}