-
Notifications
You must be signed in to change notification settings - Fork 146
/
wycheproof_test.go
114 lines (103 loc) · 2.74 KB
/
wycheproof_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
package ed25519_test
import (
"bytes"
"encoding/hex"
"encoding/json"
"io"
"os"
"testing"
"github.com/cloudflare/circl/internal/test"
"github.com/cloudflare/circl/sign/ed25519"
)
type group struct {
Key struct {
Curve string `json:"curve"`
Size int `json:"keySize"`
Pk string `json:"pk"`
Sk string `json:"sk"`
Type string `json:"type"`
} `json:"key"`
Type string `json:"type"`
Tests []struct {
TcID int `json:"tcId"`
Comment string `json:"comment"`
Msg string `json:"msg"`
Sig string `json:"sig"`
Result string `json:"result"`
Flags []string `json:"flags"`
} `json:"tests"`
}
type Wycheproof struct {
Alg string `json:"algorithm"`
Version string `json:"generatorVersion"`
Num int `json:"numberOfTests"`
Groups []group `json:"testGroups"`
}
func (kat *Wycheproof) readFile(t *testing.T, fileName string) {
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 read. Error: %v", fileName, err)
}
err = json.Unmarshal(input, &kat)
if err != nil {
t.Fatalf("File %v can not be loaded. Error: %v", fileName, err)
}
}
func (kat *Wycheproof) keyPair(t *testing.T) {
for i, g := range kat.Groups {
if g.Key.Curve != "edwards25519" {
t.Errorf("Curve not expected %v", g.Key.Curve)
}
private, _ := hex.DecodeString(g.Key.Sk)
public, _ := hex.DecodeString(g.Key.Pk)
keys := ed25519.NewKeyFromSeed(private)
got := keys.Public().(ed25519.PublicKey)
want := public
if !bytes.Equal(got, want) {
test.ReportError(t, got, want, i, g.Key.Sk)
}
}
}
func (kat *Wycheproof) verify(t *testing.T) {
for i, g := range kat.Groups {
for _, gT := range g.Tests {
isValid := gT.Result == "valid"
private, _ := hex.DecodeString(g.Key.Sk)
public, _ := hex.DecodeString(g.Key.Pk)
sig, _ := hex.DecodeString(gT.Sig)
msg, _ := hex.DecodeString(gT.Msg)
priv := ed25519.NewKeyFromSeed(private)
got := priv.Public().(ed25519.PublicKey)
want := public
if !bytes.Equal(got, want) {
test.ReportError(t, got, want, i, gT.TcID)
}
if isValid {
got := ed25519.Sign(priv, msg)
want := sig
if !bytes.Equal(got, want) {
test.ReportError(t, got, want, i, gT.TcID)
}
}
{
got := ed25519.Verify(priv.Public().(ed25519.PublicKey), msg, sig)
want := isValid
if got != want {
test.ReportError(t, got, want, i, gT.TcID)
}
}
}
}
}
func TestWycheproof(t *testing.T) {
// Test vectors from Wycheproof v0.4.12
var kat Wycheproof
kat.readFile(t, "testdata/wycheproof_Ed25519.json")
t.Run("EDDSAKeyPair", kat.keyPair)
t.Run("EDDSAVerify", kat.verify)
}