-
Notifications
You must be signed in to change notification settings - Fork 8
/
suite_test.go
133 lines (124 loc) · 2.95 KB
/
suite_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 h2c_test
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
h2c "github.com/armfazh/h2c-go-ref"
)
type vectorSuite struct {
SuiteID string `json:"ciphersuite"`
CurveName string `json:"curve"`
DST string `json:"dst"`
L string `json:"L"`
Z string `json:"Z"`
Expand string `json:"expand"`
Field struct {
M string `json:"m"`
P string `json:"p"`
} `json:"field"`
Hash string `json:"hash"`
K string `json:"k"`
Map struct {
Name string `json:"name"`
} `json:"map"`
RandomOracle bool `json:"randomOracle"`
Vectors []struct {
P struct {
X string `json:"x"`
Y string `json:"y"`
} `json:"P"`
Q0 struct {
X string `json:"x"`
Y string `json:"y"`
} `json:"Q0"`
Q1 struct {
X string `json:"x"`
Y string `json:"y"`
} `json:"Q1"`
Q struct {
X string `json:"x"`
Y string `json:"y"`
} `json:"Q"`
Msg string `json:"msg"`
U []string `json:"u"`
} `json:"vectors"`
}
func (v vectorSuite) test(t *testing.T) {
hashToCurve, err := h2c.SuiteID(v.SuiteID).Get([]byte(v.DST))
if err != nil {
t.Skipf(err.Error())
}
hashToScalar := hashToCurve.GetHashToScalar()
E := hashToCurve.GetCurve()
F := E.Field()
maxScalar := hashToScalar.GetScalarField().Order()
for i := range v.Vectors {
var x, y []interface{}
for _, xi := range strings.Split(v.Vectors[i].P.X, ",") {
x = append(x, xi)
}
for _, yi := range strings.Split(v.Vectors[i].P.Y, ",") {
y = append(y, yi)
}
got := hashToCurve.Hash([]byte(v.Vectors[i].Msg))
want := E.NewPoint(F.Elt(x), F.Elt(y))
if !got.IsEqual(want) {
t.Fatalf("suite: %v\ngot: %v\nwant: %v", v.SuiteID, got, want)
}
kElt := hashToScalar.Hash([]byte(v.Vectors[i].Msg))
kInt := kElt.Polynomial()[0]
if kInt.Sign() < 0 || kInt.Cmp(maxScalar) >= 0 {
t.Fatalf("suite: %v\ngot: %v", v.SuiteID, kInt)
}
}
}
func TestVectors(t *testing.T) {
if errFolder := filepath.Walk("testdata/suites",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
jsonFile, errFile := os.Open(path)
if errFile != nil {
return errFile
}
defer jsonFile.Close()
byteValue, errRead := ioutil.ReadAll(jsonFile)
if errRead != nil {
return errRead
}
var v vectorSuite
errJSON := json.Unmarshal(byteValue, &v)
if errJSON != nil {
return errJSON
}
t.Run(v.SuiteID, v.test)
return nil
}); errFolder != nil {
t.Fatalf("error on reading testdata folder: %v", errFolder)
}
}
func BenchmarkSuites(b *testing.B) {
msg := make([]byte, 256)
dst := make([]byte, 10)
for _, suite := range []h2c.SuiteID{
h2c.P256_XMDSHA256_SSWU_RO_,
h2c.Curve25519_XMDSHA512_ELL2_RO_,
h2c.Curve448_XOFSHAKE256_ELL2_RO_,
} {
b.Run(string(suite), func(b *testing.B) {
b.SetBytes(int64(len(msg)))
hashToCurve, _ := suite.Get(dst)
b.ResetTimer()
for i := 0; i < b.N; i++ {
hashToCurve.Hash(msg)
}
})
}
}