-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathed25519_test.go
143 lines (117 loc) · 4 KB
/
ed25519_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
package crypto
import (
"reflect"
"testing"
)
const Ed25519PubHex = "69ec35fafe61e514f4d2e54279671ab7e1e7fee9c4356da912ecd9f49db06773"
const Ed25519Seed = "3a547668e859fb7b112a1e2dd7efcb739176ab8cfd1d9f224847fce362ebd99c"
const Ed25519PrivHex = "3a547668e859fb7b112a1e2dd7efcb739176ab8cfd1d9f224847fce362ebd99c69ec35fafe61e514f4d2e54279671ab7e1e7fee9c4356da912ecd9f49db06773"
const EdMessageHex = "91b5963ab438f4ddf9dbdda98d50eab56fb8dbab24242fd86997ed99d89f0869549ea309d697e6d072c479ff1464b4831c902e40b45e181df506b6cda5be36f95cd9023270e902e2ad7ce1c1e09545fc25733cd9d155f6c65bac93006ac9f9c6a2fcf912e5fbf49277d553576b9853900c906adde560"
const EdSignatureHex = "b5aa14ad195fa99bcc1a0e77eb92903d6eb5fb387c3b71a3d19ac80ec86c1615251cc2ee0e06ba91319e5f56b893b03fb1160aa8dab4a72f69aa77be92d98d0c"
func TestEd25519SuiteType(t *testing.T) {
signer := ed25519Signer{
verifier: &ed25519Verifier{suiteType: "Test"},
}
expected := "Test"
result := signer.SuiteType()
if expected != result {
t.Errorf("Got %s, expected %s", result, expected)
}
}
func TestEd25519KeyPairFromSeed(t *testing.T) {
seed, _ := FromHex(Ed25519Seed)
pub, priv, err := Ed25519KeyPairFromSeed(seed)
if err != nil {
t.Fatalf(err.Error())
}
pubKey := ToHex(pub)
privKey := ToHex(priv)
if pubKey != Ed25519PubHex || privKey != Ed25519PrivHex {
t.Fatalf("key do not match pub key : %s, expected pub key : %s, priv key : %s, expected priv key : %s", pubKey, Ed25519PubHex, privKey, Ed25519PrivHex)
}
}
func TestEd25519Sign(t *testing.T) {
toSign, _ := FromHex(EdMessageHex)
privKey, _ := FromHex(Ed25519PrivHex)
signer := ed25519Signer{
privKey: privKey,
}
signature, sigErr := signer.Sign(toSign)
if sigErr != nil {
t.Errorf("Got a signature error %s", sigErr.Error())
}
expected, _ := FromHex(EdSignatureHex)
if !reflect.DeepEqual(signature, expected) {
t.Errorf("Expected %x, signature was %x.", expected, signature)
}
}
func TestEd25519Verify(t *testing.T) {
signature, _ := FromHex(EdSignatureHex)
message, _ := FromHex(EdMessageHex)
pubKey, _ := FromHex(Ed25519PubHex)
verifier := ed25519Verifier{
publicKey: pubKey,
}
result := verifier.Verify(message, signature)
if !result {
t.Errorf("Expected to verify signature.")
}
}
func TestNewEd25519Verifier(t *testing.T) {
signature, _ := FromHex(EdSignatureHex)
pubKey, _ := FromHex(Ed25519PubHex)
message, _ := FromHex(EdMessageHex)
verifier, verErr := NewEd25519Verifier(pubKey)
if verErr != nil {
t.Errorf("Got a constructor error %s", verErr.Error())
}
result := verifier.Verify(message, signature)
if !result {
t.Errorf("Expected to verify signature.")
}
if verifier.SuiteType() != "ed25519" {
t.Errorf("Expected suite type ed25519 got %s", verifier.SuiteType())
}
}
func TestNewEd25519Signer(t *testing.T) {
signature, _ := FromHex(EdSignatureHex)
message, _ := FromHex(EdMessageHex)
privKey, _ := FromHex(Ed25519PrivHex)
signer, signerErr := NewEd25519Signer(privKey)
if signerErr != nil {
t.Errorf("Got a constructor error %s", signerErr.Error())
}
verifyResult := signer.Verify(message, signature)
if !verifyResult {
t.Errorf("Expected to verify signature.")
}
if signer.SuiteType() != "ed25519" {
t.Errorf("Expected suite type ed25519 got %s", signer.SuiteType())
}
signResult, sigErr := signer.Sign(message)
if sigErr != nil {
t.Errorf("Got a sign error %s", sigErr.Error())
}
if !reflect.DeepEqual(signResult, signature) {
t.Errorf("Expected %x, signature was %x.", signature, signResult)
}
}
func TestNewEd25519KeySize(t *testing.T) {
key := []byte{1, 2, 3}
_, sigErr := NewEd25519Signer(key)
_, verErr := NewEd25519Verifier(key)
if sigErr == nil {
t.Errorf("Did not get signature error.")
}
if verErr == nil {
t.Errorf("Did not get verifier error.")
}
}
func TestEd25519PublicFromPrivate(t *testing.T) {
privKey, _ := FromHex(Ed25519PrivHex)
pubKey, _ := Ed25519PublicKeyFromPrivate(privKey)
expected, _ := FromHex(Ed25519PubHex)
if !reflect.DeepEqual(pubKey, expected) {
t.Errorf("Expected %x, pubkey was %x.", expected, pubKey)
}
}