Bitcoin ABC 0.30.5
P2P Digital Currency
tests_impl.h
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2017 Amaury SÉCHET *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
6
7#ifndef SECP256K1_MODULE_SCHNORR_TESTS_H
8#define SECP256K1_MODULE_SCHNORR_TESTS_H
9
11
13 unsigned char privkey[32];
14 unsigned char message[32];
15 unsigned char schnorr_signature[64];
16 secp256k1_pubkey pubkey;
17
18 /* Generate a random key and message. */
19 {
22 secp256k1_scalar_get_b32(privkey, &key);
24 }
25
26 /* Construct and verify corresponding public key. */
27 CHECK(secp256k1_ec_seckey_verify(ctx, privkey) == 1);
28 CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, privkey) == 1);
29
30 /* Schnorr sign. */
31 CHECK(secp256k1_schnorr_sign(ctx, schnorr_signature, message, privkey, NULL, NULL) == 1);
32 CHECK(secp256k1_schnorr_verify(ctx, schnorr_signature, message, &pubkey) == 1);
33 /* Destroy signature and verify again. */
34 schnorr_signature[secp256k1_testrand_bits(6)] += 1 + secp256k1_testrand_int(255);
35 CHECK(secp256k1_schnorr_verify(ctx, schnorr_signature, message, &pubkey) == 0);
36}
37
38#define SIG_COUNT 32
39
41 unsigned char msg32[32];
42 unsigned char sig64[SIG_COUNT][64];
43 unsigned char ndata[SIG_COUNT][32];
44 secp256k1_gej pubkeyj[SIG_COUNT];
45 secp256k1_ge pubkey[SIG_COUNT];
47 int i, j;
48
50
51 for (i = 0; i < SIG_COUNT; i++) {
53 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pubkeyj[i], &key[i]);
54 secp256k1_ge_set_gej_var(&pubkey[i], &pubkeyj[i]);
55 secp256k1_fe_normalize(&pubkey[i].x);
56 secp256k1_fe_normalize(&pubkey[i].y);
57
58 do {
60 if (secp256k1_schnorr_sig_sign(ctx, sig64[i], msg32, &key[i], &pubkey[i], NULL, &ndata[i])) {
61 break;
62 }
63 } while(1);
64
65 CHECK(secp256k1_schnorr_sig_verify(&ctx->ecmult_ctx, sig64[i], &pubkey[i], msg32));
66
67 /* Apply several random modifications to the sig and check that it
68 * doesn't verify anymore. */
69 for (j = 0; j < count; j++) {
70 int pos = secp256k1_testrand_bits(6);
71 int mod = 1 + secp256k1_testrand_int(255);
72 sig64[i][pos] ^= mod;
73 CHECK(secp256k1_schnorr_sig_verify(&ctx->ecmult_ctx, sig64[i], &pubkey[i], msg32) == 0);
74 sig64[i][pos] ^= mod;
75 }
76 }
77}
78
79#undef SIG_COUNT
80
82 {
83 /* Test vector 1 */
84 static const unsigned char pkbuf[33] = {
85 0x02,
86 0x79, 0xBE, 0x66, 0x7E, 0xF9, 0xDC, 0xBB, 0xAC,
87 0x55, 0xA0, 0x62, 0x95, 0xCE, 0x87, 0x0B, 0x07,
88 0x02, 0x9B, 0xFC, 0xDB, 0x2D, 0xCE, 0x28, 0xD9,
89 0x59, 0xF2, 0x81, 0x5B, 0x16, 0xF8, 0x17, 0x98,
90 };
91
92 static const unsigned char msg[32] = {
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 };
98
99 static const unsigned char sig[64] = {
100 0x78, 0x7A, 0x84, 0x8E, 0x71, 0x04, 0x3D, 0x28,
101 0x0C, 0x50, 0x47, 0x0E, 0x8E, 0x15, 0x32, 0xB2,
102 0xDD, 0x5D, 0x20, 0xEE, 0x91, 0x2A, 0x45, 0xDB,
103 0xDD, 0x2B, 0xD1, 0xDF, 0xBF, 0x18, 0x7E, 0xF6,
104 0x70, 0x31, 0xA9, 0x88, 0x31, 0x85, 0x9D, 0xC3,
105 0x4D, 0xFF, 0xEE, 0xDD, 0xA8, 0x68, 0x31, 0x84,
106 0x2C, 0xCD, 0x00, 0x79, 0xE1, 0xF9, 0x2A, 0xF1,
107 0x77, 0xF7, 0xF2, 0x2C, 0xC1, 0xDC, 0xED, 0x05,
108 };
109
110 secp256k1_pubkey pubkey;
111 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
112 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey));
113 }
114
115 {
116 /* Test vector 2 */
117 static const unsigned char pkbuf[33] = {
118 0x02,
119 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F,
120 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE,
121 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8,
122 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59,
123 };
124
125 static const unsigned char msg[32] = {
126 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3,
127 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44,
128 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0,
129 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89,
130 };
131
132 static const unsigned char sig[64] = {
133 0x2A, 0x29, 0x8D, 0xAC, 0xAE, 0x57, 0x39, 0x5A,
134 0x15, 0xD0, 0x79, 0x5D, 0xDB, 0xFD, 0x1D, 0xCB,
135 0x56, 0x4D, 0xA8, 0x2B, 0x0F, 0x26, 0x9B, 0xC7,
136 0x0A, 0x74, 0xF8, 0x22, 0x04, 0x29, 0xBA, 0x1D,
137 0x1E, 0x51, 0xA2, 0x2C, 0xCE, 0xC3, 0x55, 0x99,
138 0xB8, 0xF2, 0x66, 0x91, 0x22, 0x81, 0xF8, 0x36,
139 0x5F, 0xFC, 0x2D, 0x03, 0x5A, 0x23, 0x04, 0x34,
140 0xA1, 0xA6, 0x4D, 0xC5, 0x9F, 0x70, 0x13, 0xFD,
141 };
142
143 secp256k1_pubkey pubkey;
144 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
145 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey));
146 }
147
148 {
149 /* Test vector 3 */
150 static const unsigned char pkbuf[33] = {
151 0x03,
152 0xFA, 0xC2, 0x11, 0x4C, 0x2F, 0xBB, 0x09, 0x15,
153 0x27, 0xEB, 0x7C, 0x64, 0xEC, 0xB1, 0x1F, 0x80,
154 0x21, 0xCB, 0x45, 0xE8, 0xE7, 0x80, 0x9D, 0x3C,
155 0x09, 0x38, 0xE4, 0xB8, 0xC0, 0xE5, 0xF8, 0x4B,
156 };
157
158 static const unsigned char msg[32] = {
159 0x5E, 0x2D, 0x58, 0xD8, 0xB3, 0xBC, 0xDF, 0x1A,
160 0xBA, 0xDE, 0xC7, 0x82, 0x90, 0x54, 0xF9, 0x0D,
161 0xDA, 0x98, 0x05, 0xAA, 0xB5, 0x6C, 0x77, 0x33,
162 0x30, 0x24, 0xB9, 0xD0, 0xA5, 0x08, 0xB7, 0x5C,
163 };
164
165 static const unsigned char sig[64] = {
166 0x00, 0xDA, 0x9B, 0x08, 0x17, 0x2A, 0x9B, 0x6F,
167 0x04, 0x66, 0xA2, 0xDE, 0xFD, 0x81, 0x7F, 0x2D,
168 0x7A, 0xB4, 0x37, 0xE0, 0xD2, 0x53, 0xCB, 0x53,
169 0x95, 0xA9, 0x63, 0x86, 0x6B, 0x35, 0x74, 0xBE,
170 0x00, 0x88, 0x03, 0x71, 0xD0, 0x17, 0x66, 0x93,
171 0x5B, 0x92, 0xD2, 0xAB, 0x4C, 0xD5, 0xC8, 0xA2,
172 0xA5, 0x83, 0x7E, 0xC5, 0x7F, 0xED, 0x76, 0x60,
173 0x77, 0x3A, 0x05, 0xF0, 0xDE, 0x14, 0x23, 0x80,
174 };
175
176 secp256k1_pubkey pubkey;
177 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
178 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey));
179 }
180
181 {
182 /* Test vector 4 */
183 static const unsigned char pkbuf[33] = {
184 0x03,
185 0xDE, 0xFD, 0xEA, 0x4C, 0xDB, 0x67, 0x77, 0x50,
186 0xA4, 0x20, 0xFE, 0xE8, 0x07, 0xEA, 0xCF, 0x21,
187 0xEB, 0x98, 0x98, 0xAE, 0x79, 0xB9, 0x76, 0x87,
188 0x66, 0xE4, 0xFA, 0xA0, 0x4A, 0x2D, 0x4A, 0x34,
189 };
190
191 static const unsigned char msg[32] = {
192 0x4D, 0xF3, 0xC3, 0xF6, 0x8F, 0xCC, 0x83, 0xB2,
193 0x7E, 0x9D, 0x42, 0xC9, 0x04, 0x31, 0xA7, 0x24,
194 0x99, 0xF1, 0x78, 0x75, 0xC8, 0x1A, 0x59, 0x9B,
195 0x56, 0x6C, 0x98, 0x89, 0xB9, 0x69, 0x67, 0x03,
196 };
197
198 static const unsigned char sig[64] = {
199 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 0x00, 0x00, 0x00, 0x3B, 0x78, 0xCE, 0x56, 0x3F,
201 0x89, 0xA0, 0xED, 0x94, 0x14, 0xF5, 0xAA, 0x28,
202 0xAD, 0x0D, 0x96, 0xD6, 0x79, 0x5F, 0x9C, 0x63,
203 0x02, 0xA8, 0xDC, 0x32, 0xE6, 0x4E, 0x86, 0xA3,
204 0x33, 0xF2, 0x0E, 0xF5, 0x6E, 0xAC, 0x9B, 0xA3,
205 0x0B, 0x72, 0x46, 0xD6, 0xD2, 0x5E, 0x22, 0xAD,
206 0xB8, 0xC6, 0xBE, 0x1A, 0xEB, 0x08, 0xD4, 0x9D,
207 };
208
209 secp256k1_pubkey pubkey;
210 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
211 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey));
212 }
213
214 {
215 /* Test vector 4b */
216 static const unsigned char pkbuf[33] = {
217 0x03,
218 0x1B, 0x84, 0xC5, 0x56, 0x7B, 0x12, 0x64, 0x40,
219 0x99, 0x5D, 0x3E, 0xD5, 0xAA, 0xBA, 0x05, 0x65,
220 0xD7, 0x1E, 0x18, 0x34, 0x60, 0x48, 0x19, 0xFF,
221 0x9C, 0x17, 0xF5, 0xE9, 0xD5, 0xDD, 0x07, 0x8F,
222 };
223
224 static const unsigned char msg[32] = {
225 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229 };
230
231 static const unsigned char sig[64] = {
232 0x52, 0x81, 0x85, 0x79, 0xAC, 0xA5, 0x97, 0x67,
233 0xE3, 0x29, 0x1D, 0x91, 0xB7, 0x6B, 0x63, 0x7B,
234 0xEF, 0x06, 0x20, 0x83, 0x28, 0x49, 0x92, 0xF2,
235 0xD9, 0x5F, 0x56, 0x4C, 0xA6, 0xCB, 0x4E, 0x35,
236 0x30, 0xB1, 0xDA, 0x84, 0x9C, 0x8E, 0x83, 0x04,
237 0xAD, 0xC0, 0xCF, 0xE8, 0x70, 0x66, 0x03, 0x34,
238 0xB3, 0xCF, 0xC1, 0x8E, 0x82, 0x5E, 0xF1, 0xDB,
239 0x34, 0xCF, 0xAE, 0x3D, 0xFC, 0x5D, 0x81, 0x87,
240 };
241
242 secp256k1_pubkey pubkey;
243 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
244 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey));
245 }
246
247 {
248 /* Test vector 6: R.y is not a quadratic residue */
249 static const unsigned char pkbuf[33] = {
250 0x02,
251 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F,
252 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE,
253 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8,
254 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59,
255 };
256
257 static const unsigned char msg[32] = {
258 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3,
259 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44,
260 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0,
261 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89,
262 };
263
264 static const unsigned char sig[64] = {
265 0x2A, 0x29, 0x8D, 0xAC, 0xAE, 0x57, 0x39, 0x5A,
266 0x15, 0xD0, 0x79, 0x5D, 0xDB, 0xFD, 0x1D, 0xCB,
267 0x56, 0x4D, 0xA8, 0x2B, 0x0F, 0x26, 0x9B, 0xC7,
268 0x0A, 0x74, 0xF8, 0x22, 0x04, 0x29, 0xBA, 0x1D,
269 0xFA, 0x16, 0xAE, 0xE0, 0x66, 0x09, 0x28, 0x0A,
270 0x19, 0xB6, 0x7A, 0x24, 0xE1, 0x97, 0x7E, 0x46,
271 0x97, 0x71, 0x2B, 0x5F, 0xD2, 0x94, 0x39, 0x14,
272 0xEC, 0xD5, 0xF7, 0x30, 0x90, 0x1B, 0x4A, 0xB7,
273 };
274
275 secp256k1_pubkey pubkey;
276 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
277 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey) == 0);
278 }
279
280 {
281 /* Test vector 7: Negated message hash, R.x mismatch */
282 static const unsigned char pkbuf[33] = {
283 0x03,
284 0xFA, 0xC2, 0x11, 0x4C, 0x2F, 0xBB, 0x09, 0x15,
285 0x27, 0xEB, 0x7C, 0x64, 0xEC, 0xB1, 0x1F, 0x80,
286 0x21, 0xCB, 0x45, 0xE8, 0xE7, 0x80, 0x9D, 0x3C,
287 0x09, 0x38, 0xE4, 0xB8, 0xC0, 0xE5, 0xF8, 0x4B,
288 };
289
290 static const unsigned char msg[32] = {
291 0x5E, 0x2D, 0x58, 0xD8, 0xB3, 0xBC, 0xDF, 0x1A,
292 0xBA, 0xDE, 0xC7, 0x82, 0x90, 0x54, 0xF9, 0x0D,
293 0xDA, 0x98, 0x05, 0xAA, 0xB5, 0x6C, 0x77, 0x33,
294 0x30, 0x24, 0xB9, 0xD0, 0xA5, 0x08, 0xB7, 0x5C,
295 };
296
297 static const unsigned char sig[64] = {
298 0x00, 0xDA, 0x9B, 0x08, 0x17, 0x2A, 0x9B, 0x6F,
299 0x04, 0x66, 0xA2, 0xDE, 0xFD, 0x81, 0x7F, 0x2D,
300 0x7A, 0xB4, 0x37, 0xE0, 0xD2, 0x53, 0xCB, 0x53,
301 0x95, 0xA9, 0x63, 0x86, 0x6B, 0x35, 0x74, 0xBE,
302 0xD0, 0x92, 0xF9, 0xD8, 0x60, 0xF1, 0x77, 0x6A,
303 0x1F, 0x74, 0x12, 0xAD, 0x8A, 0x1E, 0xB5, 0x0D,
304 0xAC, 0xCC, 0x22, 0x2B, 0xC8, 0xC0, 0xE2, 0x6B,
305 0x20, 0x56, 0xDF, 0x2F, 0x27, 0x3E, 0xFD, 0xEC,
306 };
307
308 secp256k1_pubkey pubkey;
309 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
310 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey) == 0);
311 }
312
313 {
314 /* Test vector 8: Negated s, R.x mismatch */
315 static const unsigned char pkbuf[33] = {
316 0x02,
317 0x79, 0xBE, 0x66, 0x7E, 0xF9, 0xDC, 0xBB, 0xAC,
318 0x55, 0xA0, 0x62, 0x95, 0xCE, 0x87, 0x0B, 0x07,
319 0x02, 0x9B, 0xFC, 0xDB, 0x2D, 0xCE, 0x28, 0xD9,
320 0x59, 0xF2, 0x81, 0x5B, 0x16, 0xF8, 0x17, 0x98,
321 };
322
323 static const unsigned char msg[32] = {
324 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
325 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
327 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328 };
329
330 static const unsigned char sig[64] = {
331 0x78, 0x7A, 0x84, 0x8E, 0x71, 0x04, 0x3D, 0x28,
332 0x0C, 0x50, 0x47, 0x0E, 0x8E, 0x15, 0x32, 0xB2,
333 0xDD, 0x5D, 0x20, 0xEE, 0x91, 0x2A, 0x45, 0xDB,
334 0xDD, 0x2B, 0xD1, 0xDF, 0xBF, 0x18, 0x7E, 0xF6,
335 0x8F, 0xCE, 0x56, 0x77, 0xCE, 0x7A, 0x62, 0x3C,
336 0xB2, 0x00, 0x11, 0x22, 0x57, 0x97, 0xCE, 0x7A,
337 0x8D, 0xE1, 0xDC, 0x6C, 0xCD, 0x4F, 0x75, 0x4A,
338 0x47, 0xDA, 0x6C, 0x60, 0x0E, 0x59, 0x54, 0x3C,
339 };
340
341 secp256k1_pubkey pubkey;
342 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
343 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey) == 0);
344 }
345
346 {
347 /* Test vector 9: Negated P, R.x mismatch */
348 static const unsigned char pkbuf[33] = {
349 0x03,
350 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F,
351 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE,
352 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8,
353 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59,
354 };
355
356 static const unsigned char msg[32] = {
357 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3,
358 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44,
359 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0,
360 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89,
361 };
362
363 static const unsigned char sig[64] = {
364 0x2A, 0x29, 0x8D, 0xAC, 0xAE, 0x57, 0x39, 0x5A,
365 0x15, 0xD0, 0x79, 0x5D, 0xDB, 0xFD, 0x1D, 0xCB,
366 0x56, 0x4D, 0xA8, 0x2B, 0x0F, 0x26, 0x9B, 0xC7,
367 0x0A, 0x74, 0xF8, 0x22, 0x04, 0x29, 0xBA, 0x1D,
368 0x1E, 0x51, 0xA2, 0x2C, 0xCE, 0xC3, 0x55, 0x99,
369 0xB8, 0xF2, 0x66, 0x91, 0x22, 0x81, 0xF8, 0x36,
370 0x5F, 0xFC, 0x2D, 0x03, 0x5A, 0x23, 0x04, 0x34,
371 0xA1, 0xA6, 0x4D, 0xC5, 0x9F, 0x70, 0x13, 0xFD,
372 };
373
374 secp256k1_pubkey pubkey;
375 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
376 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey) == 0);
377 }
378
379 {
380 /* Test vector 10: s * G = e * P, R = 0 */
381 static const unsigned char pkbuf[33] = {
382 0x02,
383 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F,
384 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE,
385 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8,
386 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59,
387 };
388
389 static const unsigned char msg[32] = {
390 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3,
391 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44,
392 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0,
393 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89,
394 };
395
396 static const unsigned char sig[64] = {
397 0x2A, 0x29, 0x8D, 0xAC, 0xAE, 0x57, 0x39, 0x5A,
398 0x15, 0xD0, 0x79, 0x5D, 0xDB, 0xFD, 0x1D, 0xCB,
399 0x56, 0x4D, 0xA8, 0x2B, 0x0F, 0x26, 0x9B, 0xC7,
400 0x0A, 0x74, 0xF8, 0x22, 0x04, 0x29, 0xBA, 0x1D,
401 0x8C, 0x34, 0x28, 0x86, 0x9A, 0x66, 0x3E, 0xD1,
402 0xE9, 0x54, 0x70, 0x5B, 0x02, 0x0C, 0xBB, 0x3E,
403 0x7B, 0xB6, 0xAC, 0x31, 0x96, 0x5B, 0x9E, 0xA4,
404 0xC7, 0x3E, 0x22, 0x7B, 0x17, 0xC5, 0xAF, 0x5A,
405 };
406
407 secp256k1_pubkey pubkey;
408 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
409 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey) == 0);
410 }
411
412 {
413 /* Test vector 11: R.x not on the curve, R.x mismatch */
414 static const unsigned char pkbuf[33] = {
415 0x02,
416 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F,
417 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE,
418 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8,
419 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59,
420 };
421
422 static const unsigned char msg[32] = {
423 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3,
424 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44,
425 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0,
426 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89,
427 };
428
429 static const unsigned char sig[64] = {
430 0x4A, 0x29, 0x8D, 0xAC, 0xAE, 0x57, 0x39, 0x5A,
431 0x15, 0xD0, 0x79, 0x5D, 0xDB, 0xFD, 0x1D, 0xCB,
432 0x56, 0x4D, 0xA8, 0x2B, 0x0F, 0x26, 0x9B, 0xC7,
433 0x0A, 0x74, 0xF8, 0x22, 0x04, 0x29, 0xBA, 0x1D,
434 0x1E, 0x51, 0xA2, 0x2C, 0xCE, 0xC3, 0x55, 0x99,
435 0xB8, 0xF2, 0x66, 0x91, 0x22, 0x81, 0xF8, 0x36,
436 0x5F, 0xFC, 0x2D, 0x03, 0x5A, 0x23, 0x04, 0x34,
437 0xA1, 0xA6, 0x4D, 0xC5, 0x9F, 0x70, 0x13, 0xFD,
438 };
439
440 secp256k1_pubkey pubkey;
441 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
442 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey) == 0);
443 }
444
445 {
446 /* Test vector 12: r = p */
447 static const unsigned char pkbuf[33] = {
448 0x02,
449 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F,
450 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE,
451 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8,
452 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59,
453 };
454
455 static const unsigned char msg[32] = {
456 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3,
457 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44,
458 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0,
459 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89,
460 };
461
462 static const unsigned char sig[64] = {
463 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
464 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
465 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
466 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x2F,
467 0x1E, 0x51, 0xA2, 0x2C, 0xCE, 0xC3, 0x55, 0x99,
468 0xB8, 0xF2, 0x66, 0x91, 0x22, 0x81, 0xF8, 0x36,
469 0x5F, 0xFC, 0x2D, 0x03, 0x5A, 0x23, 0x04, 0x34,
470 0xA1, 0xA6, 0x4D, 0xC5, 0x9F, 0x70, 0x13, 0xFD,
471 };
472
473 secp256k1_pubkey pubkey;
474 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
475 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey) == 0);
476 }
477
478 {
479 /* Test vector 13: s = n */
480 static const unsigned char pkbuf[33] = {
481 0x02,
482 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F,
483 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE,
484 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8,
485 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59,
486 };
487
488 static const unsigned char msg[32] = {
489 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3,
490 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44,
491 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0,
492 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89,
493 };
494
495 static const unsigned char sig[64] = {
496 0x2A, 0x29, 0x8D, 0xAC, 0xAE, 0x57, 0x39, 0x5A,
497 0x15, 0xD0, 0x79, 0x5D, 0xDB, 0xFD, 0x1D, 0xCB,
498 0x56, 0x4D, 0xA8, 0x2B, 0x0F, 0x26, 0x9B, 0xC7,
499 0x0A, 0x74, 0xF8, 0x22, 0x04, 0x29, 0xBA, 0x1D,
500 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
501 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
502 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B,
503 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41,
504 };
505
506 secp256k1_pubkey pubkey;
507 CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pkbuf, 33));
508 CHECK(secp256k1_schnorr_verify(ctx, sig, msg, &pubkey) == 0);
509 }
510}
511
513 int i;
514 for (i = 0; i < 32 * count; i++) {
516 }
517
520}
521
522#endif
secp256k1_context * ctx
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *a)
Multiply with the generator: R = a*G.
static void secp256k1_fe_normalize(secp256k1_fe *r)
Field element module.
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
SchnorrSig sig
Definition: processor.cpp:498
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
void test_schnorr_end_to_end(void)
Definition: tests_impl.h:12
void run_schnorr_tests(void)
Definition: tests_impl.h:512
#define SIG_COUNT
Definition: tests_impl.h:38
void test_schnorr_sign_verify(void)
Definition: tests_impl.h:40
void run_schnorr_compact_test(void)
Definition: tests_impl.h:81
static int secp256k1_schnorr_sig_verify(const secp256k1_ecmult_context *ctx, const unsigned char *sig64, secp256k1_ge *pubkey, const unsigned char *msg32)
static int secp256k1_schnorr_sig_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_scalar *privkey, secp256k1_ge *pubkey, secp256k1_nonce_function noncefp, const void *ndata)
#define CHECK(cond)
Definition: util.h:53
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an ECDSA secret key.
Definition: secp256k1.c:576
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a variable-length public key into the pubkey object.
Definition: secp256k1.c:278
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
Definition: secp256k1.c:599
SECP256K1_API int secp256k1_schnorr_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a signature using a custom EC-Schnorr-SHA256 construction.
Definition: main_impl.h:33
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorr_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Verify a signature created by secp256k1_schnorr_sign.
Definition: main_impl.h:13
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:71
secp256k1_ecmult_context ecmult_ctx
Definition: secp256k1.c:70
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:13
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:23
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:70
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
static uint32_t secp256k1_testrand_int(uint32_t range)
Generate a pseudorandom number in the range [0..range-1].
static void secp256k1_testrand256_test(unsigned char *b32)
Generate a pseudorandom 32-byte array with long sequences of zero and one bits.
static uint32_t secp256k1_testrand_bits(int bits)
Generate a pseudorandom number in the range [0..2**bits-1].
void random_scalar_order_test(secp256k1_scalar *num)
Definition: tests.c:103
static int count
Definition: tests.c:31