-
Notifications
You must be signed in to change notification settings - Fork 1
/
schnorr_test.go
126 lines (107 loc) · 2.94 KB
/
schnorr_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
package bip340
import (
"io"
"math/big"
"net/http"
"strings"
"testing"
"encoding/csv"
"encoding/hex"
)
const TEST_CASES_CSV = "https://raw.githubusercontent.com/bitcoin/bips/master/bip-0340/test-vectors.csv"
func TestSign(t *testing.T) {
resp, err := http.Get(TEST_CASES_CSV)
if err != nil {
t.Fatalf("Failed to get test cases from %s", TEST_CASES_CSV)
}
defer resp.Body.Close()
r := csv.NewReader(resp.Body)
for {
testCase, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Unexpected error reading CSV file: %s", err.Error())
}
if testCase[0] == "index" {
// head
continue
}
index := testCase[0]
privateKey := testCase[1]
publicKey := testCase[2]
aux := testCase[3]
message := testCase[4]
sig := testCase[5]
shouldVerify := testCase[6] == "TRUE"
errComment := testCase[7]
m := decodeMessage(message, t)
// check public key
pk := decodePublicKey(publicKey, t)
if privateKey != "" {
// use private key and aux to sign
d := decodePrivateKey(privateKey, t)
a := decodeMessage(aux, t)
signature, err := Sign(d, m, a[:])
if err != nil {
t.Fatalf("[%s] Unexpected error from Sign(%s, %s): %v",
index, privateKey, message, err)
}
observed := hex.EncodeToString(signature[:])
expected := strings.ToLower(sig)
// check if signature matches
if observed != expected {
t.Fatalf("[%s]: Sign(%s, %s, %s) = %s, want %s",
index, privateKey, message, aux, observed, expected)
}
// check if pubkey derivation is ok
if GetPublicKey(d) != pk {
t.Fatalf(
"[%s]: Derived public key is different from key on file: %x != %x",
index, pk, d)
}
}
// test if signature verification works
signature32 := decodeSignature(sig, t)
isValid, err := Verify(pk, m, signature32)
if isValid && shouldVerify {
// verifies as expected
} else if !isValid && !shouldVerify {
// fails to verify as expected
} else {
t.Fatalf("[%s] Verify test failed (verified? %v (%s); expected: %v): %s",
index, isValid, err.Error(), shouldVerify, errComment)
}
}
}
func decodeSignature(s string, t *testing.T) (sig [64]byte) {
signature, err := hex.DecodeString(s)
if err != nil && t != nil {
t.Fatalf("Unexpected error from hex.DecodeString(%s): %v", s, err)
}
copy(sig[:], signature)
return
}
func decodeMessage(m string, t *testing.T) (msg [32]byte) {
message, err := hex.DecodeString(m)
if err != nil && t != nil {
t.Fatalf("Unexpected error from hex.DecodeString(%s): %v", m, err)
}
copy(msg[:], message)
return
}
func decodePublicKey(pk string, t *testing.T) (pubKey [32]byte) {
pubKey, err := ParsePublicKey(pk)
if err != nil && t != nil {
t.Fatalf("Unexpected error from ParsePublicKey(%s): %v", pk, err)
}
return
}
func decodePrivateKey(d string, t *testing.T) *big.Int {
privKey, err := ParsePrivateKey(d)
if err != nil && t != nil {
t.Fatalf("Unexpected error from ParsePrivateKey(%s): %v", d, err)
}
return privKey
}