-
Notifications
You must be signed in to change notification settings - Fork 145
/
hash_test.go
133 lines (111 loc) · 3.05 KB
/
hash_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
package bls12381
import (
"encoding/hex"
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/cloudflare/circl/ecc/bls12381/ff"
"github.com/cloudflare/circl/internal/test"
)
type vectorHash struct {
SuiteID string `json:"ciphersuite"`
CurveName string `json:"curve"`
DST string `json:"dst"`
IsRandomOracle bool `json:"randomOracle"`
Vectors []struct {
P point `json:"P"`
Msg string `json:"msg"`
} `json:"vectors"`
Field struct {
M string `json:"m"`
P string `json:"p"`
} `json:"field"`
}
type elm string
func (e elm) toBytes(t *testing.T) (out []byte) {
var buf [ff.FpSize]byte
for _, s := range strings.Split(string(e), ",") {
x, err := hex.DecodeString(s[2:])
if err != nil {
t.Fatal(err)
}
copy(buf[ff.FpSize-len(x):ff.FpSize], x)
out = append(append([]byte{}, buf[:]...), out...)
}
return
}
type point struct {
X elm `json:"x"`
Y elm `json:"y"`
}
func (p point) toBytes(t *testing.T) []byte { return append(p.X.toBytes(t), p.Y.toBytes(t)...) }
type hasher interface {
Encode(_, _ []byte)
Hash(_, _ []byte)
SetBytes([]byte) error
IsEqualTo(_ hasher) bool
IsRTorsion() bool
}
type g1Hasher struct{ *G1 }
func (g g1Hasher) IsEqualTo(x hasher) bool { return g.IsEqual(x.(g1Hasher).G1) }
func (g g1Hasher) IsRTorsion() bool { return g.IsOnG1() }
type g2Hasher struct{ *G2 }
func (g g2Hasher) IsEqualTo(x hasher) bool { return g.IsEqual(x.(g2Hasher).G2) }
func (g g2Hasher) IsRTorsion() bool { return g.IsOnG2() }
func (v *vectorHash) test(t *testing.T) {
var got, want hasher
if v.Field.M == "0x1" {
got, want = g1Hasher{new(G1)}, g1Hasher{new(G1)}
} else if v.Field.M == "0x2" {
got, want = g2Hasher{new(G2)}, g2Hasher{new(G2)}
}
dst := []byte(v.DST)
doHash := got.Encode
if v.IsRandomOracle {
doHash = got.Hash
}
for i, vi := range v.Vectors {
input := []byte(vi.Msg)
doHash(input, dst)
err := want.SetBytes(vi.P.toBytes(t))
test.CheckNoErr(t, err, "bad deserialization")
if !got.IsEqualTo(want) || !got.IsRTorsion() {
test.ReportError(t, got, want, v.SuiteID, i)
}
}
}
func readFile(t *testing.T, fileName string) *vectorHash {
jsonFile, err := os.Open(fileName)
if err != nil {
t.Fatalf("File %v can not be opened. Error: %v", fileName, err)
}
defer jsonFile.Close()
input, err := io.ReadAll(jsonFile)
if err != nil {
t.Fatalf("File %v can not be loaded. Error: %v", fileName, err)
}
v := new(vectorHash)
err = json.Unmarshal(input, v)
if err != nil {
t.Fatalf("File %v can not be parsed. Error: %v", fileName, err)
}
return v
}
func TestHashVectors(t *testing.T) {
// Test vectors from draft-irtf-cfrg-hash-to-curve:
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11
//
// JSON files can be found at:
// https://github.com/cfrg/draft-irtf-cfrg-hash-to-curve/tree/draft-irtf-cfrg-hash-to-curve-10/poc/vectors
fileNames, err := filepath.Glob("./testdata/BLS12381*.json")
if err != nil {
t.Fatal(err)
}
for _, fileName := range fileNames {
v := readFile(t, fileName)
t.Run(v.SuiteID, v.test)
}
}