bip340_vectors_test.mx raw
1 package secp256k1
2
3 import (
4 "bytes"
5 "crypto/sha256"
6 "testing"
7 )
8
9 // BIP-340 test vectors, vendored verbatim from
10 // https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv
11 // Mirrored at testdata/bip0340.csv. The CSV is inlined here because the moxie
12 // test runner runs with cwd in a temp dir (not the package dir), and the test
13 // build does not include embed.
14 const bip340CSVData = `index,secret key,public key,aux_rand,message,signature,verification result,comment
15 0,0000000000000000000000000000000000000000000000000000000000000003,F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9,0000000000000000000000000000000000000000000000000000000000000000,0000000000000000000000000000000000000000000000000000000000000000,E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0,TRUE,
16 1,B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF,DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,0000000000000000000000000000000000000000000000000000000000000001,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A,TRUE,
17 2,C90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B14E5C9,DD308AFEC5777E13121FA72B9CC1B7CC0139715309B086C960E18FD969774EB8,C87AA53824B4D7AE2EB035A2B5BBBCCC080E76CDC6D1692C4B0B62D798E6D906,7E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C,5831AAEED7B44BB74E5EAB94BA9D4294C49BCF2A60728D8B4C200F50DD313C1BAB745879A5AD954A72C45A91C3A51D3C7ADEA98D82F8481E0E1E03674A6F3FB7,TRUE,
18 3,0B432B2677937381AEF05BB02A66ECD012773062CF3FA2549E44F58ED2401710,25D1DFF95105F5253C4022F628A996AD3A0D95FBF21D468A1B33F8C160D8F517,FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,7EB0509757E246F19449885651611CB965ECC1A187DD51B64FDA1EDC9637D5EC97582B9CB13DB3933705B32BA982AF5AF25FD78881EBB32771FC5922EFC66EA3,TRUE,test fails if msg is reduced modulo p or n
19 4,,D69C3509BB99E412E68B0FE8544E72837DFA30746D8BE2AA65975F29D22DC7B9,,4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703,00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6376AFB1548AF603B3EB45C9F8207DEE1060CB71C04E80F593060B07D28308D7F4,TRUE,
20 5,,EEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B,FALSE,public key not on the curve
21 6,,DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,FFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A14602975563CC27944640AC607CD107AE10923D9EF7A73C643E166BE5EBEAFA34B1AC553E2,FALSE,has_even_y(R) is false
22 7,,DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,1FA62E331EDBC21C394792D2AB1100A7B432B013DF3F6FF4F99FCB33E0E1515F28890B3EDB6E7189B630448B515CE4F8622A954CFE545735AAEA5134FCCDB2BD,FALSE,negated message
23 8,,DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769961764B3AA9B2FFCB6EF947B6887A226E8D7C93E00C5ED0C1834FF0D0C2E6DA6,FALSE,negated s value
24 9,,DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,0000000000000000000000000000000000000000000000000000000000000000123DDA8328AF9C23A94C1FEECFD123BA4FB73476F0D594DCB65C6425BD186051,FALSE,sG - eP is infinite. Test fails in single verification if has_even_y(inf) is defined as true and x(inf) as 0
25 10,,DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,00000000000000000000000000000000000000000000000000000000000000017615FBAF5AE28864013C099742DEADB4DBA87F11AC6754F93780D5A1837CF197,FALSE,sG - eP is infinite. Test fails in single verification if has_even_y(inf) is defined as true and x(inf) as 1
26 11,,DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B,FALSE,sig[0:32] is not an X coordinate on the curve
27 12,,DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F69E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B,FALSE,sig[0:32] is equal to field size
28 13,,DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E177769FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141,FALSE,sig[32:64] is equal to curve order
29 14,,FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC30,,243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89,6CFF5C3BA86C69EA4B7376F31A9BCB4F74C1976089B2D9963DA2E5543E17776969E89B4C5564D00349106B8497785DD7D1D713A8AE82B32FA79D5F7FC407D39B,FALSE,public key is not a valid X coordinate because it exceeds the field size
30 15,0340034003400340034003400340034003400340034003400340034003400340,778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117,0000000000000000000000000000000000000000000000000000000000000000,,71535DB165ECD9FBBC046E5FFAEA61186BB6AD436732FCCC25291A55895464CF6069CE26BF03466228F19A3A62DB8A649F2D560FAC652827D1AF0574E427AB63,TRUE,message of size 0 (added 2022-12)
31 16,0340034003400340034003400340034003400340034003400340034003400340,778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117,0000000000000000000000000000000000000000000000000000000000000000,11,08A20A0AFEF64124649232E0693C583AB1B9934AE63B4C3511F3AE1134C6A303EA3173BFEA6683BD101FA5AA5DBC1996FE7CACFC5A577D33EC14564CEC2BACBF,TRUE,message of size 1 (added 2022-12)
32 17,0340034003400340034003400340034003400340034003400340034003400340,778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117,0000000000000000000000000000000000000000000000000000000000000000,0102030405060708090A0B0C0D0E0F1011,5130F39A4059B43BC7CAC09A19ECE52B5D8699D1A71E3C52DA9AFDB6B50AC370C4A482B77BF960F8681540E25B6771ECE1E5A37FD80E5A51897C5566A97EA5A5,TRUE,message of size 17 (added 2022-12)
33 18,0340034003400340034003400340034003400340034003400340034003400340,778CAA53B4393AC467774D09497A87224BF9FAB6F6E68B23086497324D6FD117,0000000000000000000000000000000000000000000000000000000000000000,99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999,403B12B0D8555A344175EA7EC746566303321E5DBFA8BE6F091635163ECA79A8585ED3E3170807E7C03B720FC54C7B23897FCBA0E9D0B4A06894CFD249F22367,TRUE,message of size 100 (added 2022-12)
34 `
35
36 // hexNibble returns the value of a single hex digit, or -1 if invalid.
37 func hexNibble(c byte) int {
38 if c >= '0' && c <= '9' {
39 return int(c - '0')
40 }
41 if c >= 'a' && c <= 'f' {
42 return int(c-'a') + 10
43 }
44 if c >= 'A' && c <= 'F' {
45 return int(c-'A') + 10
46 }
47 return -1
48 }
49
50 // hexDecode parses a hex string. Returns (bytes, true) on success.
51 func hexDecode(s string) ([]byte, bool) {
52 if len(s)%2 != 0 {
53 return nil, false
54 }
55 out := []byte{:len(s)/2}
56 for i := 0; i < len(s); i += 2 {
57 hi := hexNibble(s[i])
58 lo := hexNibble(s[i+1])
59 if hi < 0 || lo < 0 {
60 return nil, false
61 }
62 out[i/2] = byte(hi<<4 | lo)
63 }
64 return out, true
65 }
66
67 // splitLines and splitFields replace strings package (removed in moxie).
68 func splitLines(b []byte) [][]byte {
69 out := [][]byte{}
70 start := 0
71 for i := 0; i < len(b); i++ {
72 if b[i] == '\n' {
73 out = append(out, b[start:i])
74 start = i + 1
75 }
76 }
77 if start < len(b) {
78 out = append(out, b[start:])
79 }
80 return out
81 }
82
83 func splitCSVFields(line []byte) [][]byte {
84 out := [][]byte{}
85 start := 0
86 for i := 0; i < len(line); i++ {
87 if line[i] == ',' {
88 out = append(out, line[start:i])
89 start = i + 1
90 }
91 }
92 out = append(out, line[start:])
93 return out
94 }
95
96 func trimCR(b []byte) []byte {
97 if len(b) > 0 && b[len(b)-1] == '\r' {
98 return b[:len(b)-1]
99 }
100 return b
101 }
102
103 // TestBIP340Vectors runs the official BIP-340 test vectors against
104 // SignSchnorr / VerifySchnorr. Vectors are vendored from
105 // https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv
106 // at testdata/bip0340.csv.
107 func TestBIP340Vectors(t *testing.T) {
108 lines := splitLines([]byte(bip340CSVData))
109 if len(lines) < 2 {
110 t.Fatalf("CSV too short: %d lines", len(lines))
111 }
112 signCount := 0
113 verifyCount := 0
114 // Skip header.
115 for i := 1; i < len(lines); i++ {
116 line := trimCR(lines[i])
117 if len(line) == 0 {
118 continue
119 }
120 fields := splitCSVFields(line)
121 if len(fields) < 7 {
122 t.Errorf("line %d: only %d fields, expected >= 7", i, len(fields))
123 continue
124 }
125 idx := string(fields[0])
126 skHex := string(fields[1])
127 pkHex := string(fields[2])
128 auxHex := string(fields[3])
129 msgHex := string(fields[4])
130 sigHex := string(fields[5])
131 want := bytes.Equal(fields[6], []byte("TRUE"))
132
133 pk, hexOK := hexDecode(pkHex)
134 if !hexOK || len(pk) != 32 {
135 t.Errorf("vec %s: bad pubkey hex", idx)
136 continue
137 }
138 var pkArr [32]byte
139 copy(pkArr[:], pk)
140
141 msg, hexOK2 := hexDecode(msgHex)
142 if !hexOK2 {
143 t.Errorf("vec %s: bad msg hex", idx)
144 continue
145 }
146
147 sig, hexOK3 := hexDecode(sigHex)
148 if !hexOK3 || len(sig) != 64 {
149 t.Errorf("vec %s: bad sig hex (%d bytes)", idx, len(sig))
150 continue
151 }
152 var sigArr [64]byte
153 copy(sigArr[:], sig)
154
155 // Sign vectors (those with a secret key) - check determinism.
156 if skHex != "" {
157 sk, hexOK4 := hexDecode(skHex)
158 if !hexOK4 || len(sk) != 32 {
159 t.Errorf("vec %s: bad sk hex", idx)
160 continue
161 }
162 var skArr [32]byte
163 copy(skArr[:], sk)
164
165 aux, hexOK5 := hexDecode(auxHex)
166 if !hexOK5 || len(aux) != 32 {
167 t.Errorf("vec %s: bad aux hex", idx)
168 continue
169 }
170 var auxArr [32]byte
171 copy(auxArr[:], aux)
172
173 gotSig, ok := SignSchnorrBytes(skArr, msg, auxArr)
174 if !ok {
175 t.Errorf("vec %s: SignSchnorrBytes returned not ok", idx)
176 continue
177 }
178 if !bytes.Equal(gotSig[:], sig) {
179 t.Errorf("vec %s: sig mismatch\n want %x\n got %x", idx, sig, gotSig[:])
180 continue
181 }
182
183 // Sanity: derived pubkey matches expected.
184 gotPK, ok := PubKeyFromSecKey(skArr)
185 if !ok {
186 t.Errorf("vec %s: PubKeyFromSecKey not ok", idx)
187 continue
188 }
189 if !bytes.Equal(gotPK[:], pk) {
190 t.Errorf("vec %s: derived pubkey mismatch\n want %x\n got %x", idx, pk, gotPK[:])
191 continue
192 }
193 signCount++
194 }
195 verifyCount++
196
197 // All vectors: verify and compare to expected TRUE/FALSE.
198 got := VerifySchnorrBytes(pkArr, msg, sigArr)
199 if got != want {
200 t.Errorf("vec %s: VerifySchnorrBytes = %v, want %v", idx, got, want)
201 }
202 }
203 // 8 sign vectors (0-3, 15-18), 19 verify vectors (0-18). Catches any silent
204 // loop break or skipped lines.
205 if signCount != 8 {
206 t.Errorf("expected 8 sign vectors covered, got %d", signCount)
207 }
208 if verifyCount != 19 {
209 t.Errorf("expected 19 verify vectors covered, got %d", verifyCount)
210 }
211 }
212
213 // TestRadix16Carry exercises the constant-time ScalarMult digits[64]==1 carry
214 // edge case. Folded from the runtime helper TestSignedRadix16 (which remains
215 // callable from non-test contexts like web/mlstest).
216 func TestRadix16Carry(t *testing.T) {
217 if err := TestSignedRadix16(); err != nil {
218 t.Fatal(err)
219 }
220 }
221
222 // TestNostrEventRoundtrip exercises the SignSchnorr/VerifySchnorr 32-byte
223 // API that Nostr uses (sha256 of canonical event JSON, then sign).
224 func TestNostrEventRoundtrip(t *testing.T) {
225 var sk [32]byte
226 sk[31] = 5
227 pk, ok := PubKeyFromSecKey(sk)
228 if !ok {
229 t.Fatal("PubKeyFromSecKey failed")
230 }
231 msg := sha256.Sum([]byte(`[0,"deadbeef",1700000000,1,[],"hello"]`))
232 var aux [32]byte
233 sig, ok := SignSchnorr(sk, msg, aux)
234 if !ok {
235 t.Fatal("SignSchnorr failed")
236 }
237 if !VerifySchnorr(pk, msg, sig) {
238 t.Fatal("VerifySchnorr returned false on own signature")
239 }
240 // Wrong msg should not verify.
241 bad := sha256.Sum([]byte("different"))
242 if VerifySchnorr(pk, bad, sig) {
243 t.Fatal("VerifySchnorr accepted wrong message")
244 }
245 }
246