Bitcoin ABC 0.32.7
P2P Digital Currency
secp256k1.c
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2013-2015 Pieter Wuille *
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#define SECP256K1_BUILD
8
9#include "../include/secp256k1.h"
10#include "../include/secp256k1_preallocated.h"
11
12#include "assumptions.h"
13#include "util.h"
14#include "field_impl.h"
15#include "scalar_impl.h"
16#include "group_impl.h"
17#include "ecmult_impl.h"
18#include "ecmult_const_impl.h"
19#include "ecmult_gen_impl.h"
20#include "ecdsa_impl.h"
21#include "eckey_impl.h"
22#include "hash_impl.h"
23#include "scratch_impl.h"
24#include "selftest.h"
25
26#ifdef SECP256K1_NO_BUILD
27# error "secp256k1.h processed without SECP256K1_BUILD defined while building secp256k1.c"
28#endif
29
30#if defined(VALGRIND)
31# include <valgrind/memcheck.h>
32#endif
33
34#define ARG_CHECK(cond) do { \
35 if (EXPECT(!(cond), 0)) { \
36 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
37 return 0; \
38 } \
39} while(0)
40
41#define ARG_CHECK_NO_RETURN(cond) do { \
42 if (EXPECT(!(cond), 0)) { \
43 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
44 } \
45} while(0)
46
52};
53
55 { 0 },
58 0
59};
61
63 size_t ret = sizeof(secp256k1_context);
64 /* A return value of 0 is reserved as an indicator for errors when we call this function internally. */
65 VERIFY_CHECK(ret != 0);
66
69 "Invalid flags");
70 return 0;
71 }
72
73 return ret;
74}
75
77 size_t ret = sizeof(secp256k1_context);
78 VERIFY_CHECK(ctx != NULL);
79 return ret;
80}
81
83 size_t prealloc_size;
85
86 if (!secp256k1_selftest()) {
88 }
89
91 if (prealloc_size == 0) {
92 return NULL;
93 }
94 VERIFY_CHECK(prealloc != NULL);
95 ret = (secp256k1_context*)prealloc;
98
99 /* Flags have been checked by secp256k1_context_preallocated_size. */
103
104 return ret;
105}
106
108 size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
111 free(ctx);
112 return NULL;
113 }
114
115 return ctx;
116}
117
120 VERIFY_CHECK(ctx != NULL);
121 ARG_CHECK(prealloc != NULL);
122
123 ret = (secp256k1_context*)prealloc;
124 *ret = *ctx;
125 return ret;
126}
127
130 size_t prealloc_size;
131
132 VERIFY_CHECK(ctx != NULL);
134 ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
136 return ret;
137}
138
141 if (ctx != NULL) {
143 }
144}
145
147 if (ctx != NULL) {
149 free(ctx);
150 }
151}
152
153void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
155 if (fun == NULL) {
157 }
158 ctx->illegal_callback.fn = fun;
159 ctx->illegal_callback.data = data;
160}
161
162void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
164 if (fun == NULL) {
166 }
167 ctx->error_callback.fn = fun;
168 ctx->error_callback.data = data;
169}
170
172 VERIFY_CHECK(ctx != NULL);
173 return secp256k1_scratch_create(&ctx->error_callback, max_size);
174}
175
177 VERIFY_CHECK(ctx != NULL);
179}
180
181/* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
182 * of the software. This is setup for use with valgrind but could be substituted with
183 * the appropriate instrumentation for other analysis tools.
184 */
185static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context* ctx, const void *p, size_t len) {
186#if defined(VALGRIND)
187 if (EXPECT(ctx->declassify,0)) VALGRIND_MAKE_MEM_DEFINED(p, len);
188#else
189 (void)ctx;
190 (void)p;
191 (void)len;
192#endif
193}
194
196 if (sizeof(secp256k1_ge_storage) == 64) {
197 /* When the secp256k1_ge_storage type is exactly 64 byte, use its
198 * representation inside secp256k1_pubkey, as conversion is very fast.
199 * Note that secp256k1_pubkey_save must use the same representation. */
201 memcpy(&s, &pubkey->data[0], sizeof(s));
203 } else {
204 /* Otherwise, fall back to 32-byte big endian for X and Y. */
205 secp256k1_fe x, y;
206 secp256k1_fe_set_b32(&x, pubkey->data);
207 secp256k1_fe_set_b32(&y, pubkey->data + 32);
208 secp256k1_ge_set_xy(ge, &x, &y);
209 }
211 return 1;
212}
213
215 if (sizeof(secp256k1_ge_storage) == 64) {
218 memcpy(&pubkey->data[0], &s, sizeof(s));
219 } else {
223 secp256k1_fe_get_b32(pubkey->data, &ge->x);
224 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
225 }
226}
227
228int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
229 secp256k1_ge Q;
230
231 VERIFY_CHECK(ctx != NULL);
232 ARG_CHECK(pubkey != NULL);
233 memset(pubkey, 0, sizeof(*pubkey));
234 ARG_CHECK(input != NULL);
235 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
236 return 0;
237 }
239 return 0;
240 }
241 secp256k1_pubkey_save(pubkey, &Q);
243 return 1;
244}
245
246int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
247 secp256k1_ge Q;
248 size_t len;
249 int ret = 0;
250
251 VERIFY_CHECK(ctx != NULL);
252 ARG_CHECK(outputlen != NULL);
253 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33u : 65u));
254 len = *outputlen;
255 *outputlen = 0;
256 ARG_CHECK(output != NULL);
257 memset(output, 0, len);
258 ARG_CHECK(pubkey != NULL);
260 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
262 if (ret) {
263 *outputlen = len;
264 }
265 }
266 return ret;
267}
268
270 unsigned char out[2][33];
271 const secp256k1_pubkey* pk[2];
272 int i;
273
274 VERIFY_CHECK(ctx != NULL);
275 pk[0] = pubkey0; pk[1] = pubkey1;
276 for (i = 0; i < 2; i++) {
277 size_t out_size = sizeof(out[i]);
278 /* If the public key is NULL or invalid, ec_pubkey_serialize will call
279 * the illegal_callback and return 0. In that case we will serialize the
280 * key as all zeros which is less than any valid public key. This
281 * results in consistent comparisons even if NULL or invalid pubkeys are
282 * involved and prevents edge cases such as sorting algorithms that use
283 * this function and do not terminate as a result. */
284 if (!secp256k1_ec_pubkey_serialize(ctx, out[i], &out_size, pk[i], SECP256K1_EC_COMPRESSED)) {
285 /* Note that ec_pubkey_serialize should already set the output to
286 * zero in that case, but it's not guaranteed by the API, we can't
287 * test it and writing a VERIFY_CHECK is more complex than
288 * explicitly memsetting (again). */
289 memset(out[i], 0, sizeof(out[i]));
290 }
291 }
292 return secp256k1_memcmp_var(out[0], out[1], sizeof(out[0]));
293}
294
296 (void)ctx;
297 if (sizeof(secp256k1_scalar) == 32) {
298 /* When the secp256k1_scalar type is exactly 32 byte, use its
299 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
300 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
301 memcpy(r, &sig->data[0], 32);
302 memcpy(s, &sig->data[32], 32);
303 } else {
304 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
305 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
306 }
307}
308
310 if (sizeof(secp256k1_scalar) == 32) {
311 memcpy(&sig->data[0], r, 32);
312 memcpy(&sig->data[32], s, 32);
313 } else {
314 secp256k1_scalar_get_b32(&sig->data[0], r);
315 secp256k1_scalar_get_b32(&sig->data[32], s);
316 }
317}
318
319int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
320 secp256k1_scalar r, s;
321
322 VERIFY_CHECK(ctx != NULL);
323 ARG_CHECK(sig != NULL);
324 ARG_CHECK(input != NULL);
325
326 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
328 return 1;
329 } else {
330 memset(sig, 0, sizeof(*sig));
331 return 0;
332 }
333}
334
336 secp256k1_scalar r, s;
337 int ret = 1;
338 int overflow = 0;
339
340 VERIFY_CHECK(ctx != NULL);
341 ARG_CHECK(sig != NULL);
342 ARG_CHECK(input64 != NULL);
343
344 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
345 ret &= !overflow;
346 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
347 ret &= !overflow;
348 if (ret) {
350 } else {
351 memset(sig, 0, sizeof(*sig));
352 }
353 return ret;
354}
355
356int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
357 secp256k1_scalar r, s;
358
359 VERIFY_CHECK(ctx != NULL);
360 ARG_CHECK(output != NULL);
361 ARG_CHECK(outputlen != NULL);
362 ARG_CHECK(sig != NULL);
363
365 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
366}
367
369 secp256k1_scalar r, s;
370
371 VERIFY_CHECK(ctx != NULL);
372 ARG_CHECK(output64 != NULL);
373 ARG_CHECK(sig != NULL);
374
376 secp256k1_scalar_get_b32(&output64[0], &r);
377 secp256k1_scalar_get_b32(&output64[32], &s);
378 return 1;
379}
380
382 secp256k1_scalar r, s;
383 int ret = 0;
384
385 VERIFY_CHECK(ctx != NULL);
386 ARG_CHECK(sigin != NULL);
387
388 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
389 ret = secp256k1_scalar_is_high(&s);
390 if (sigout != NULL) {
391 if (ret) {
393 }
394 secp256k1_ecdsa_signature_save(sigout, &r, &s);
395 }
396
397 return ret;
398}
399
400int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) {
401 secp256k1_ge q;
402 secp256k1_scalar r, s;
404 VERIFY_CHECK(ctx != NULL);
405 ARG_CHECK(msghash32 != NULL);
406 ARG_CHECK(sig != NULL);
407 ARG_CHECK(pubkey != NULL);
408
409 secp256k1_scalar_set_b32(&m, msghash32, NULL);
411 return (!secp256k1_scalar_is_high(&s) &&
412 secp256k1_pubkey_load(ctx, &q, pubkey) &&
413 secp256k1_ecdsa_sig_verify(&r, &s, &q, &m));
414}
415
416static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
417 memcpy(buf + *offset, data, len);
418 *offset += len;
419}
420
421static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
422 unsigned char keydata[112];
423 unsigned int offset = 0;
425 unsigned int i;
426 /* We feed a byte array to the PRNG as input, consisting of:
427 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
428 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
429 * - optionally 16 extra bytes with the algorithm name.
430 * Because the arguments have distinct fixed lengths it is not possible for
431 * different argument mixtures to emulate each other and result in the same
432 * nonces.
433 */
434 buffer_append(keydata, &offset, key32, 32);
435 buffer_append(keydata, &offset, msg32, 32);
436 if (data != NULL) {
437 buffer_append(keydata, &offset, data, 32);
438 }
439 if (algo16 != NULL) {
440 buffer_append(keydata, &offset, algo16, 16);
441 }
442 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
443 memset(keydata, 0, sizeof(keydata));
444 for (i = 0; i <= counter; i++) {
446 }
448 return 1;
449}
450
453
454static int secp256k1_ecdsa_sign_inner(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, int* recid, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const unsigned char algo16[17], const void* noncedata) {
455 secp256k1_scalar sec, non, msg;
456 int ret = 0;
457 int is_sec_valid;
458 unsigned char nonce32[32];
459 unsigned int count = 0;
460 /* Default initialization here is important so we won't pass uninit values to the cmov in the end */
463 if (recid) {
464 *recid = 0;
465 }
466 if (noncefp == NULL) {
468 }
469
470 /* Fail if the secret key is invalid. */
471 is_sec_valid = secp256k1_scalar_set_b32_seckey(&sec, seckey);
472 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_one, !is_sec_valid);
473 secp256k1_scalar_set_b32(&msg, msg32, NULL);
474 while (1) {
475 int is_nonce_valid;
476 ret = !!noncefp(nonce32, msg32, seckey, algo16, (void*)noncedata, count);
477 if (!ret) {
478 break;
479 }
480 is_nonce_valid = secp256k1_scalar_set_b32_seckey(&non, nonce32);
481 /* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */
482 secp256k1_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid));
483 if (is_nonce_valid) {
484 ret = secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid);
485 /* The final signature is no longer a secret, nor is the fact that we were successful or not. */
486 secp256k1_declassify(ctx, &ret, sizeof(ret));
487 if (ret) {
488 break;
489 }
490 }
491 count++;
492 }
493 /* We don't want to declassify is_sec_valid and therefore the range of
494 * seckey. As a result is_sec_valid is included in ret only after ret was
495 * used as a branching variable. */
496 ret &= is_sec_valid;
497 memset(nonce32, 0, 32);
503 if (recid) {
504 const int zero = 0;
505 secp256k1_int_cmov(recid, &zero, !ret);
506 }
507 return ret;
508}
509
510int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
511 secp256k1_scalar r, s;
512 int ret;
513 const unsigned char secp256k1_ecdsa_der_algo16[17] = "ECDSA+DER ";
514 VERIFY_CHECK(ctx != NULL);
516 ARG_CHECK(msghash32 != NULL);
517 ARG_CHECK(signature != NULL);
518 ARG_CHECK(seckey != NULL);
519
520 ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msghash32, seckey, noncefp, secp256k1_ecdsa_der_algo16, noncedata);
521 secp256k1_ecdsa_signature_save(signature, &r, &s);
522 return ret;
523}
524
525int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
527 int ret;
528 VERIFY_CHECK(ctx != NULL);
529 ARG_CHECK(seckey != NULL);
530
531 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
533 return ret;
534}
535
536static int secp256k1_ec_pubkey_create_helper(const secp256k1_ecmult_gen_context *ecmult_gen_ctx, secp256k1_scalar *seckey_scalar, secp256k1_ge *p, const unsigned char *seckey) {
537 secp256k1_gej pj;
538 int ret;
539
540 ret = secp256k1_scalar_set_b32_seckey(seckey_scalar, seckey);
541 secp256k1_scalar_cmov(seckey_scalar, &secp256k1_scalar_one, !ret);
542
543 secp256k1_ecmult_gen(ecmult_gen_ctx, &pj, seckey_scalar);
544 secp256k1_ge_set_gej(p, &pj);
545 return ret;
546}
547
548int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
549 secp256k1_ge p;
550 secp256k1_scalar seckey_scalar;
551 int ret = 0;
552 VERIFY_CHECK(ctx != NULL);
553 ARG_CHECK(pubkey != NULL);
554 memset(pubkey, 0, sizeof(*pubkey));
556 ARG_CHECK(seckey != NULL);
557
558 ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey);
559 secp256k1_pubkey_save(pubkey, &p);
560 secp256k1_memczero(pubkey, sizeof(*pubkey), !ret);
561
562 secp256k1_scalar_clear(&seckey_scalar);
563 return ret;
564}
565
566int secp256k1_ec_seckey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
568 int ret = 0;
569 VERIFY_CHECK(ctx != NULL);
570 ARG_CHECK(seckey != NULL);
571
572 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
574 secp256k1_scalar_negate(&sec, &sec);
575 secp256k1_scalar_get_b32(seckey, &sec);
576
578 return ret;
579}
580
581int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
582 return secp256k1_ec_seckey_negate(ctx, seckey);
583}
584
586 int ret = 0;
587 secp256k1_ge p;
588 VERIFY_CHECK(ctx != NULL);
589 ARG_CHECK(pubkey != NULL);
590
591 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
592 memset(pubkey, 0, sizeof(*pubkey));
593 if (ret) {
594 secp256k1_ge_neg(&p, &p);
595 secp256k1_pubkey_save(pubkey, &p);
596 }
597 return ret;
598}
599
600
601static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak32) {
602 secp256k1_scalar term;
603 int overflow = 0;
604 int ret = 0;
605
606 secp256k1_scalar_set_b32(&term, tweak32, &overflow);
607 ret = (!overflow) & secp256k1_eckey_privkey_tweak_add(sec, &term);
609 return ret;
610}
611
612int secp256k1_ec_seckey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
614 int ret = 0;
615 VERIFY_CHECK(ctx != NULL);
616 ARG_CHECK(seckey != NULL);
617 ARG_CHECK(tweak32 != NULL);
618
619 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
620 ret &= secp256k1_ec_seckey_tweak_add_helper(&sec, tweak32);
622 secp256k1_scalar_get_b32(seckey, &sec);
623
625 return ret;
626}
627
628int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
629 return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak32);
630}
631
632static int secp256k1_ec_pubkey_tweak_add_helper(secp256k1_ge *p, const unsigned char *tweak32) {
633 secp256k1_scalar term;
634 int overflow = 0;
635 secp256k1_scalar_set_b32(&term, tweak32, &overflow);
636 return !overflow && secp256k1_eckey_pubkey_tweak_add(p, &term);
637}
638
639int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) {
640 secp256k1_ge p;
641 int ret = 0;
642 VERIFY_CHECK(ctx != NULL);
643 ARG_CHECK(pubkey != NULL);
644 ARG_CHECK(tweak32 != NULL);
645
646 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
647 memset(pubkey, 0, sizeof(*pubkey));
648 ret = ret && secp256k1_ec_pubkey_tweak_add_helper(&p, tweak32);
649 if (ret) {
650 secp256k1_pubkey_save(pubkey, &p);
651 }
652
653 return ret;
654}
655
656int secp256k1_ec_seckey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
657 secp256k1_scalar factor;
659 int ret = 0;
660 int overflow = 0;
661 VERIFY_CHECK(ctx != NULL);
662 ARG_CHECK(seckey != NULL);
663 ARG_CHECK(tweak32 != NULL);
664
665 secp256k1_scalar_set_b32(&factor, tweak32, &overflow);
666 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
667 ret &= (!overflow) & secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
669 secp256k1_scalar_get_b32(seckey, &sec);
670
672 secp256k1_scalar_clear(&factor);
673 return ret;
674}
675
676int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
677 return secp256k1_ec_seckey_tweak_mul(ctx, seckey, tweak32);
678}
679
680int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) {
681 secp256k1_ge p;
682 secp256k1_scalar factor;
683 int ret = 0;
684 int overflow = 0;
685 VERIFY_CHECK(ctx != NULL);
686 ARG_CHECK(pubkey != NULL);
687 ARG_CHECK(tweak32 != NULL);
688
689 secp256k1_scalar_set_b32(&factor, tweak32, &overflow);
690 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
691 memset(pubkey, 0, sizeof(*pubkey));
692 if (ret) {
693 if (secp256k1_eckey_pubkey_tweak_mul(&p, &factor)) {
694 secp256k1_pubkey_save(pubkey, &p);
695 } else {
696 ret = 0;
697 }
698 }
699
700 return ret;
701}
702
703int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
704 VERIFY_CHECK(ctx != NULL);
707 }
708 return 1;
709}
710
711int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
712 size_t i;
713 secp256k1_gej Qj;
714 secp256k1_ge Q;
715
716 VERIFY_CHECK(ctx != NULL);
717 ARG_CHECK(pubnonce != NULL);
718 memset(pubnonce, 0, sizeof(*pubnonce));
719 ARG_CHECK(n >= 1);
720 ARG_CHECK(pubnonces != NULL);
721
723
724 for (i = 0; i < n; i++) {
725 ARG_CHECK(pubnonces[i] != NULL);
726 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
727 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
728 }
729 if (secp256k1_gej_is_infinity(&Qj)) {
730 return 0;
731 }
732 secp256k1_ge_set_gej(&Q, &Qj);
733 secp256k1_pubkey_save(pubnonce, &Q);
734 return 1;
735}
736
737int secp256k1_tagged_sha256(const secp256k1_context* ctx, unsigned char *hash32, const unsigned char *tag, size_t taglen, const unsigned char *msg, size_t msglen) {
739 VERIFY_CHECK(ctx != NULL);
740 ARG_CHECK(hash32 != NULL);
741 ARG_CHECK(tag != NULL);
742 ARG_CHECK(msg != NULL);
743
744 secp256k1_sha256_initialize_tagged(&sha, tag, taglen);
745 secp256k1_sha256_write(&sha, msg, msglen);
746 secp256k1_sha256_finalize(&sha, hash32);
747 return 1;
748}
749
750#ifdef ENABLE_MODULE_ECDH
751# include "modules/ecdh/main_impl.h"
752#endif
753
754#ifdef ENABLE_MODULE_MULTISET
756#endif
757
758#ifdef ENABLE_MODULE_RECOVERY
760#endif
761
762#ifdef ENABLE_MODULE_SCHNORR
764#endif
765
766#ifdef ENABLE_MODULE_EXTRAKEYS
768#endif
769
770#ifdef ENABLE_MODULE_SCHNORRSIG
772#endif
int flags
Definition: bitcoin-tx.cpp:542
static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, size_t *size, const secp256k1_scalar *r, const secp256k1_scalar *s)
static int secp256k1_ecdsa_sig_sign(const secp256k1_ecmult_gen_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, const secp256k1_scalar *seckey, const secp256k1_scalar *message, const secp256k1_scalar *nonce, int *recid)
static int secp256k1_ecdsa_sig_parse(secp256k1_scalar *r, secp256k1_scalar *s, const unsigned char *sig, size_t size)
static int secp256k1_ecdsa_sig_verify(const secp256k1_scalar *r, const secp256k1_scalar *s, const secp256k1_ge *pubkey, const secp256k1_scalar *message)
static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp256k1_scalar *tweak)
static int secp256k1_eckey_pubkey_tweak_mul(secp256k1_ge *key, const secp256k1_scalar *tweak)
static int secp256k1_eckey_pubkey_tweak_add(secp256k1_ge *key, const secp256k1_scalar *tweak)
static int secp256k1_eckey_privkey_tweak_mul(secp256k1_scalar *key, const secp256k1_scalar *tweak)
static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size)
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed)
static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx)
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_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_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const unsigned char *seed32)
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *ctx)
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
Normalize a field element, without constant-time guarantee.
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
Set a field element equal to 32-byte big endian value.
static int secp256k1_fe_is_zero(const secp256k1_fe *a)
Verify whether a field element is zero.
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
Convert a field element to a 32-byte big endian value.
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Set a group element (jacobian) equal to the point at infinity.
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Check whether a group element is the point at infinity.
static void secp256k1_ge_clear(secp256k1_ge *r)
Clear a secp256k1_ge to prevent leaking sensitive information.
static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y)
Set a group element equal to the point with given X and Y coordinates.
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity).
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
Convert a group element back from the storage type.
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge *ge)
Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Check whether a group element is the point at infinity.
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Convert a group element to the storage type.
static void secp256k1_sha256_initialize_tagged(secp256k1_sha256 *hash, const unsigned char *tag, size_t taglen)
Definition: hash_impl.h:169
secp256k1_context * ctx
Definition: bench_impl.h:13
SchnorrSig sig
Definition: processor.cpp:523
static void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
static int secp256k1_scalar_set_b32_seckey(secp256k1_scalar *r, const unsigned char *bin)
Set a scalar from a big endian byte array and returns 1 if it is a valid seckey and 0 otherwise.
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
Check whether a scalar is higher than the group order divided by 2.
static void secp256k1_scalar_clear(secp256k1_scalar *r)
Clear a scalar to prevent the leak of sensitive data.
static const secp256k1_scalar secp256k1_scalar_zero
Definition: scalar_impl.h:32
static const secp256k1_scalar secp256k1_scalar_one
Definition: scalar_impl.h:31
static void secp256k1_scratch_destroy(const secp256k1_callback *error_callback, secp256k1_scratch *scratch)
static secp256k1_scratch * secp256k1_scratch_create(const secp256k1_callback *error_callback, size_t max_size)
static void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256 *rng, unsigned char *out, size_t outlen)
static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256 *rng, const unsigned char *key, size_t keylen)
static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256 *rng)
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t size)
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition: util.h:221
static SECP256K1_INLINE void secp256k1_int_cmov(int *r, const int *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
Definition: util.h:235
static void secp256k1_default_error_callback_fn(const char *str, void *data)
Definition: util.h:34
#define EXPECT(x, c)
Definition: util.h:70
static const secp256k1_callback default_error_callback
Definition: util.h:49
static void secp256k1_default_illegal_callback_fn(const char *str, void *data)
Definition: util.h:29
#define VERIFY_CHECK(cond)
Definition: util.h:95
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition: util.h:118
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
Definition: util.h:202
static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback *const cb, const char *const text)
Definition: util.h:24
static const secp256k1_callback default_illegal_callback
Definition: util.h:44
int secp256k1_ec_privkey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32)
Same as secp256k1_ec_seckey_tweak_add, but DEPRECATED.
Definition: secp256k1.c:628
int secp256k1_ec_privkey_negate(const secp256k1_context *ctx, unsigned char *seckey)
Same as secp256k1_ec_seckey_negate, but DEPRECATED.
Definition: secp256k1.c:581
const secp256k1_nonce_function secp256k1_nonce_function_default
A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979).
Definition: secp256k1.c:452
secp256k1_context * secp256k1_context_preallocated_clone(const secp256k1_context *ctx, void *prealloc)
Copy a secp256k1 context object into caller-provided memory.
Definition: secp256k1.c:118
const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
Definition: secp256k1.c:451
int secp256k1_tagged_sha256(const secp256k1_context *ctx, unsigned char *hash32, const unsigned char *tag, size_t taglen, const unsigned char *msg, size_t msglen)
Compute a tagged hash as defined in BIP-340.
Definition: secp256k1.c:737
int secp256k1_ec_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32)
Tweak a public key by adding tweak times the generator to it.
Definition: secp256k1.c:639
int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags)
Serialize a pubkey object into a serialized byte sequence.
Definition: secp256k1.c:246
int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig)
Serialize an ECDSA signature in DER format.
Definition: secp256k1.c:356
static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak32)
Definition: secp256k1.c:601
int secp256k1_ec_seckey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32)
Tweak a secret key by multiplying it by a tweak.
Definition: secp256k1.c:656
int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen)
Parse a variable-length public key into the pubkey object.
Definition: secp256k1.c:228
size_t secp256k1_context_preallocated_clone_size(const secp256k1_context *ctx)
Determine the memory size of a secp256k1 context object to be copied into caller-provided memory.
Definition: secp256k1.c:76
static int secp256k1_ecdsa_sign_inner(const secp256k1_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, int *recid, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const unsigned char algo16[17], const void *noncedata)
Definition: secp256k1.c:454
int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey)
Verify an ECDSA secret key.
Definition: secp256k1.c:525
const secp256k1_context * secp256k1_context_no_precomp
A simple secp256k1 context object with no precomputed tables.
Definition: secp256k1.c:60
int secp256k1_ec_seckey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32)
Tweak a secret key by adding tweak to it.
Definition: secp256k1.c:612
void secp256k1_context_preallocated_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object that has been created in caller-provided memory.
Definition: secp256k1.c:139
#define ARG_CHECK(cond)
Definition: secp256k1.c:34
static int secp256k1_ec_pubkey_create_helper(const secp256k1_ecmult_gen_context *ecmult_gen_ctx, secp256k1_scalar *seckey_scalar, secp256k1_ge *p, const unsigned char *seckey)
Definition: secp256k1.c:536
int secp256k1_ecdsa_signature_normalize(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin)
Convert a signature to a normalized lower-S form.
Definition: secp256k1.c:381
void secp256k1_context_set_error_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data)
Set a callback function to be called when an internal consistency check fails.
Definition: secp256k1.c:162
int secp256k1_ecdsa_signature_parse_der(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input, size_t inputlen)
Parse a DER ECDSA signature.
Definition: secp256k1.c:319
static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context *ctx, const void *p, size_t len)
Definition: secp256k1.c:185
secp256k1_context * secp256k1_context_create(unsigned int flags)
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:107
int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey)
Negates a secret key in place.
Definition: secp256k1.c:566
int secp256k1_ec_pubkey_cmp(const secp256k1_context *ctx, const secp256k1_pubkey *pubkey0, const secp256k1_pubkey *pubkey1)
Compare two public keys using lexicographic (of compressed serialization) order.
Definition: secp256k1.c:269
int secp256k1_ec_pubkey_combine(const secp256k1_context *ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey *const *pubnonces, size_t n)
Add a number of public keys together.
Definition: secp256k1.c:711
int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input64)
Parse an ECDSA signature in compact (64 bytes) format.
Definition: secp256k1.c:335
void secp256k1_context_set_illegal_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data)
Set a callback function to be called when an illegal argument is passed to an API call.
Definition: secp256k1.c:153
static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature *sig, const secp256k1_scalar *r, const secp256k1_scalar *s)
Definition: secp256k1.c:309
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition: secp256k1.c:195
size_t secp256k1_context_preallocated_size(unsigned int flags)
Determine the memory size of a secp256k1 context object to be created in caller-provided memory.
Definition: secp256k1.c:62
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:214
static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len)
Definition: secp256k1.c:416
static int secp256k1_ec_pubkey_tweak_add_helper(secp256k1_ge *p, const unsigned char *tweak32)
Definition: secp256k1.c:632
#define ARG_CHECK_NO_RETURN(cond)
Definition: secp256k1.c:41
static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter)
Definition: secp256k1.c:421
static const secp256k1_context secp256k1_context_no_precomp_
Definition: secp256k1.c:54
int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32)
Updates the context randomization to protect against side-channel leakage.
Definition: secp256k1.c:703
secp256k1_scratch_space * secp256k1_scratch_space_create(const secp256k1_context *ctx, size_t max_size)
Create a secp256k1 scratch space object.
Definition: secp256k1.c:171
int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey)
Verify an ECDSA signature.
Definition: secp256k1.c:400
int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, const secp256k1_ecdsa_signature *sig)
Serialize an ECDSA signature in compact (64 byte) format.
Definition: secp256k1.c:368
int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey)
Compute the public key for a secret key.
Definition: secp256k1.c:548
void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:146
secp256k1_context * secp256k1_context_clone(const secp256k1_context *ctx)
Copy a secp256k1 context object (into dynamically allocated memory).
Definition: secp256k1.c:128
void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space *scratch)
Destroy a secp256k1 scratch space.
Definition: secp256k1.c:176
int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32)
Tweak a public key by multiplying it by a tweak value.
Definition: secp256k1.c:680
static void secp256k1_ecdsa_signature_load(const secp256k1_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, const secp256k1_ecdsa_signature *sig)
Definition: secp256k1.c:295
secp256k1_context * secp256k1_context_preallocated_create(void *prealloc, unsigned int flags)
Create a secp256k1 context object in caller-provided memory.
Definition: secp256k1.c:82
int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *noncedata)
Create an ECDSA signature.
Definition: secp256k1.c:510
int secp256k1_ec_pubkey_negate(const secp256k1_context *ctx, secp256k1_pubkey *pubkey)
Negates a public key in place.
Definition: secp256k1.c:585
int secp256k1_ec_privkey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32)
Same as secp256k1_ec_seckey_tweak_mul, but DEPRECATED.
Definition: secp256k1.c:676
struct secp256k1_context_struct secp256k1_context
Opaque data structure that holds context information (precomputed tables etc.).
Definition: secp256k1.h:46
#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY
Definition: secp256k1.h:179
int(* secp256k1_nonce_function)(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int attempt)
A pointer to a function to deterministically generate a nonce.
Definition: secp256k1.h:103
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:190
#define SECP256K1_INLINE
Definition: secp256k1.h:127
#define SECP256K1_FLAGS_TYPE_MASK
All flags' lower 8 bits indicate what they're for.
Definition: secp256k1.h:173
#define SECP256K1_FLAGS_BIT_COMPRESSION
Definition: secp256k1.h:180
#define SECP256K1_FLAGS_TYPE_CONTEXT
Definition: secp256k1.h:174
#define SECP256K1_FLAGS_TYPE_COMPRESSION
Definition: secp256k1.h:175
static int secp256k1_selftest(void)
Definition: selftest.h:28
void(* fn)(const char *text, void *data)
Definition: util.h:20
const void * data
Definition: util.h:21
secp256k1_callback illegal_callback
Definition: secp256k1.c:49
secp256k1_callback error_callback
Definition: secp256k1.c:50
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:48
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:83
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:13
secp256k1_fe x
Definition: group.h:14
secp256k1_fe y
Definition: group.h:15
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
unsigned char data[64]
Definition: secp256k1.h:71
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
static int count
Definition: tests.c:33