Bitcoin ABC 0.32.8
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/* This is a C project. It should not be compiled with a C++ compiler,
8 * and we error out if we detect one.
9 *
10 * We still want to be able to test the project with a C++ compiler
11 * because it is still good to know if this will lead to real trouble, so
12 * there is a possibility to override the check. But be warned that
13 * compiling with a C++ compiler is not supported. */
14#if defined(__cplusplus) && !defined(SECP256K1_CPLUSPLUS_TEST_OVERRIDE)
15#error Trying to compile a C project with a C++ compiler.
16#endif
17
18#define SECP256K1_BUILD
19
20#include "../include/secp256k1.h"
21#include "../include/secp256k1_preallocated.h"
22
23#include "assumptions.h"
24#include "util.h"
25#include "field_impl.h"
26#include "scalar_impl.h"
27#include "group_impl.h"
28#include "ecmult_impl.h"
29#include "ecmult_const_impl.h"
30#include "ecmult_gen_impl.h"
31#include "ecdsa_impl.h"
32#include "eckey_impl.h"
33#include "hash_impl.h"
34#include "scratch_impl.h"
35#include "selftest.h"
36
37#ifdef SECP256K1_NO_BUILD
38# error "secp256k1.h processed without SECP256K1_BUILD defined while building secp256k1.c"
39#endif
40
41#if defined(VALGRIND)
42# include <valgrind/memcheck.h>
43#endif
44
45#define ARG_CHECK(cond) do { \
46 if (EXPECT(!(cond), 0)) { \
47 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
48 return 0; \
49 } \
50} while(0)
51
52#define ARG_CHECK_NO_RETURN(cond) do { \
53 if (EXPECT(!(cond), 0)) { \
54 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
55 } \
56} while(0)
57
63};
64
66 { 0 },
69 0
70};
72
74 size_t ret = sizeof(secp256k1_context);
75 /* A return value of 0 is reserved as an indicator for errors when we call this function internally. */
76 VERIFY_CHECK(ret != 0);
77
80 "Invalid flags");
81 return 0;
82 }
83
84 return ret;
85}
86
88 size_t ret = sizeof(secp256k1_context);
89 VERIFY_CHECK(ctx != NULL);
90 return ret;
91}
92
94 size_t prealloc_size;
96
97 if (!secp256k1_selftest()) {
99 }
100
102 if (prealloc_size == 0) {
103 return NULL;
104 }
105 VERIFY_CHECK(prealloc != NULL);
106 ret = (secp256k1_context*)prealloc;
109
110 /* Flags have been checked by secp256k1_context_preallocated_size. */
114
115 return ret;
116}
117
119 size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
122 free(ctx);
123 return NULL;
124 }
125
126 return ctx;
127}
128
131 VERIFY_CHECK(ctx != NULL);
132 ARG_CHECK(prealloc != NULL);
133
134 ret = (secp256k1_context*)prealloc;
135 *ret = *ctx;
136 return ret;
137}
138
141 size_t prealloc_size;
142
143 VERIFY_CHECK(ctx != NULL);
145 ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
147 return ret;
148}
149
152 if (ctx != NULL) {
154 }
155}
156
158 if (ctx != NULL) {
160 free(ctx);
161 }
162}
163
164void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
166 if (fun == NULL) {
168 }
169 ctx->illegal_callback.fn = fun;
170 ctx->illegal_callback.data = data;
171}
172
173void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
175 if (fun == NULL) {
177 }
178 ctx->error_callback.fn = fun;
179 ctx->error_callback.data = data;
180}
181
183 VERIFY_CHECK(ctx != NULL);
184 return secp256k1_scratch_create(&ctx->error_callback, max_size);
185}
186
188 VERIFY_CHECK(ctx != NULL);
190}
191
192/* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
193 * of the software. This is setup for use with valgrind but could be substituted with
194 * the appropriate instrumentation for other analysis tools.
195 */
196static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context* ctx, const void *p, size_t len) {
197#if defined(VALGRIND)
198 if (EXPECT(ctx->declassify,0)) VALGRIND_MAKE_MEM_DEFINED(p, len);
199#else
200 (void)ctx;
201 (void)p;
202 (void)len;
203#endif
204}
205
207 if (sizeof(secp256k1_ge_storage) == 64) {
208 /* When the secp256k1_ge_storage type is exactly 64 byte, use its
209 * representation inside secp256k1_pubkey, as conversion is very fast.
210 * Note that secp256k1_pubkey_save must use the same representation. */
212 memcpy(&s, &pubkey->data[0], sizeof(s));
214 } else {
215 /* Otherwise, fall back to 32-byte big endian for X and Y. */
216 secp256k1_fe x, y;
217 secp256k1_fe_set_b32(&x, pubkey->data);
218 secp256k1_fe_set_b32(&y, pubkey->data + 32);
219 secp256k1_ge_set_xy(ge, &x, &y);
220 }
222 return 1;
223}
224
226 if (sizeof(secp256k1_ge_storage) == 64) {
229 memcpy(&pubkey->data[0], &s, sizeof(s));
230 } else {
234 secp256k1_fe_get_b32(pubkey->data, &ge->x);
235 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
236 }
237}
238
239int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
240 secp256k1_ge Q;
241
242 VERIFY_CHECK(ctx != NULL);
243 ARG_CHECK(pubkey != NULL);
244 memset(pubkey, 0, sizeof(*pubkey));
245 ARG_CHECK(input != NULL);
246 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
247 return 0;
248 }
250 return 0;
251 }
252 secp256k1_pubkey_save(pubkey, &Q);
254 return 1;
255}
256
257int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
258 secp256k1_ge Q;
259 size_t len;
260 int ret = 0;
261
262 VERIFY_CHECK(ctx != NULL);
263 ARG_CHECK(outputlen != NULL);
264 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33u : 65u));
265 len = *outputlen;
266 *outputlen = 0;
267 ARG_CHECK(output != NULL);
268 memset(output, 0, len);
269 ARG_CHECK(pubkey != NULL);
271 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
273 if (ret) {
274 *outputlen = len;
275 }
276 }
277 return ret;
278}
279
281 unsigned char out[2][33];
282 const secp256k1_pubkey* pk[2];
283 int i;
284
285 VERIFY_CHECK(ctx != NULL);
286 pk[0] = pubkey0; pk[1] = pubkey1;
287 for (i = 0; i < 2; i++) {
288 size_t out_size = sizeof(out[i]);
289 /* If the public key is NULL or invalid, ec_pubkey_serialize will call
290 * the illegal_callback and return 0. In that case we will serialize the
291 * key as all zeros which is less than any valid public key. This
292 * results in consistent comparisons even if NULL or invalid pubkeys are
293 * involved and prevents edge cases such as sorting algorithms that use
294 * this function and do not terminate as a result. */
295 if (!secp256k1_ec_pubkey_serialize(ctx, out[i], &out_size, pk[i], SECP256K1_EC_COMPRESSED)) {
296 /* Note that ec_pubkey_serialize should already set the output to
297 * zero in that case, but it's not guaranteed by the API, we can't
298 * test it and writing a VERIFY_CHECK is more complex than
299 * explicitly memsetting (again). */
300 memset(out[i], 0, sizeof(out[i]));
301 }
302 }
303 return secp256k1_memcmp_var(out[0], out[1], sizeof(out[0]));
304}
305
307 (void)ctx;
308 if (sizeof(secp256k1_scalar) == 32) {
309 /* When the secp256k1_scalar type is exactly 32 byte, use its
310 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
311 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
312 memcpy(r, &sig->data[0], 32);
313 memcpy(s, &sig->data[32], 32);
314 } else {
315 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
316 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
317 }
318}
319
321 if (sizeof(secp256k1_scalar) == 32) {
322 memcpy(&sig->data[0], r, 32);
323 memcpy(&sig->data[32], s, 32);
324 } else {
325 secp256k1_scalar_get_b32(&sig->data[0], r);
326 secp256k1_scalar_get_b32(&sig->data[32], s);
327 }
328}
329
330int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
331 secp256k1_scalar r, s;
332
333 VERIFY_CHECK(ctx != NULL);
334 ARG_CHECK(sig != NULL);
335 ARG_CHECK(input != NULL);
336
337 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
339 return 1;
340 } else {
341 memset(sig, 0, sizeof(*sig));
342 return 0;
343 }
344}
345
347 secp256k1_scalar r, s;
348 int ret = 1;
349 int overflow = 0;
350
351 VERIFY_CHECK(ctx != NULL);
352 ARG_CHECK(sig != NULL);
353 ARG_CHECK(input64 != NULL);
354
355 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
356 ret &= !overflow;
357 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
358 ret &= !overflow;
359 if (ret) {
361 } else {
362 memset(sig, 0, sizeof(*sig));
363 }
364 return ret;
365}
366
367int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
368 secp256k1_scalar r, s;
369
370 VERIFY_CHECK(ctx != NULL);
371 ARG_CHECK(output != NULL);
372 ARG_CHECK(outputlen != NULL);
373 ARG_CHECK(sig != NULL);
374
376 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
377}
378
380 secp256k1_scalar r, s;
381
382 VERIFY_CHECK(ctx != NULL);
383 ARG_CHECK(output64 != NULL);
384 ARG_CHECK(sig != NULL);
385
387 secp256k1_scalar_get_b32(&output64[0], &r);
388 secp256k1_scalar_get_b32(&output64[32], &s);
389 return 1;
390}
391
393 secp256k1_scalar r, s;
394 int ret = 0;
395
396 VERIFY_CHECK(ctx != NULL);
397 ARG_CHECK(sigin != NULL);
398
399 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
400 ret = secp256k1_scalar_is_high(&s);
401 if (sigout != NULL) {
402 if (ret) {
404 }
405 secp256k1_ecdsa_signature_save(sigout, &r, &s);
406 }
407
408 return ret;
409}
410
411int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) {
412 secp256k1_ge q;
413 secp256k1_scalar r, s;
415 VERIFY_CHECK(ctx != NULL);
416 ARG_CHECK(msghash32 != NULL);
417 ARG_CHECK(sig != NULL);
418 ARG_CHECK(pubkey != NULL);
419
420 secp256k1_scalar_set_b32(&m, msghash32, NULL);
422 return (!secp256k1_scalar_is_high(&s) &&
423 secp256k1_pubkey_load(ctx, &q, pubkey) &&
424 secp256k1_ecdsa_sig_verify(&r, &s, &q, &m));
425}
426
427static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
428 memcpy(buf + *offset, data, len);
429 *offset += len;
430}
431
432static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
433 unsigned char keydata[112];
434 unsigned int offset = 0;
436 unsigned int i;
438 unsigned char msgmod32[32];
439 secp256k1_scalar_set_b32(&msg, msg32, NULL);
440 secp256k1_scalar_get_b32(msgmod32, &msg);
441 /* We feed a byte array to the PRNG as input, consisting of:
442 * - the private key (32 bytes) and reduced message (32 bytes), see RFC 6979 3.2d.
443 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
444 * - optionally 16 extra bytes with the algorithm name.
445 * Because the arguments have distinct fixed lengths it is not possible for
446 * different argument mixtures to emulate each other and result in the same
447 * nonces.
448 */
449 buffer_append(keydata, &offset, key32, 32);
450 buffer_append(keydata, &offset, msgmod32, 32);
451 if (data != NULL) {
452 buffer_append(keydata, &offset, data, 32);
453 }
454 if (algo16 != NULL) {
455 buffer_append(keydata, &offset, algo16, 16);
456 }
457 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
458 memset(keydata, 0, sizeof(keydata));
459 for (i = 0; i <= counter; i++) {
461 }
463 return 1;
464}
465
468
469static 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) {
470 secp256k1_scalar sec, non, msg;
471 int ret = 0;
472 int is_sec_valid;
473 unsigned char nonce32[32];
474 unsigned int count = 0;
475 /* Default initialization here is important so we won't pass uninit values to the cmov in the end */
478 if (recid) {
479 *recid = 0;
480 }
481 if (noncefp == NULL) {
483 }
484
485 /* Fail if the secret key is invalid. */
486 is_sec_valid = secp256k1_scalar_set_b32_seckey(&sec, seckey);
487 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_one, !is_sec_valid);
488 secp256k1_scalar_set_b32(&msg, msg32, NULL);
489 while (1) {
490 int is_nonce_valid;
491 ret = !!noncefp(nonce32, msg32, seckey, algo16, (void*)noncedata, count);
492 if (!ret) {
493 break;
494 }
495 is_nonce_valid = secp256k1_scalar_set_b32_seckey(&non, nonce32);
496 /* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */
497 secp256k1_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid));
498 if (is_nonce_valid) {
499 ret = secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid);
500 /* The final signature is no longer a secret, nor is the fact that we were successful or not. */
501 secp256k1_declassify(ctx, &ret, sizeof(ret));
502 if (ret) {
503 break;
504 }
505 }
506 count++;
507 }
508 /* We don't want to declassify is_sec_valid and therefore the range of
509 * seckey. As a result is_sec_valid is included in ret only after ret was
510 * used as a branching variable. */
511 ret &= is_sec_valid;
512 memset(nonce32, 0, 32);
518 if (recid) {
519 const int zero = 0;
520 secp256k1_int_cmov(recid, &zero, !ret);
521 }
522 return ret;
523}
524
525int 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) {
526 secp256k1_scalar r, s;
527 int ret;
528 const unsigned char secp256k1_ecdsa_der_algo16[17] = "ECDSA+DER ";
529 VERIFY_CHECK(ctx != NULL);
531 ARG_CHECK(msghash32 != NULL);
532 ARG_CHECK(signature != NULL);
533 ARG_CHECK(seckey != NULL);
534
535 ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msghash32, seckey, noncefp, secp256k1_ecdsa_der_algo16, noncedata);
536 secp256k1_ecdsa_signature_save(signature, &r, &s);
537 return ret;
538}
539
540int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
542 int ret;
543 VERIFY_CHECK(ctx != NULL);
544 ARG_CHECK(seckey != NULL);
545
546 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
548 return ret;
549}
550
551static 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) {
552 secp256k1_gej pj;
553 int ret;
554
555 ret = secp256k1_scalar_set_b32_seckey(seckey_scalar, seckey);
556 secp256k1_scalar_cmov(seckey_scalar, &secp256k1_scalar_one, !ret);
557
558 secp256k1_ecmult_gen(ecmult_gen_ctx, &pj, seckey_scalar);
559 secp256k1_ge_set_gej(p, &pj);
560 return ret;
561}
562
563int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
564 secp256k1_ge p;
565 secp256k1_scalar seckey_scalar;
566 int ret = 0;
567 VERIFY_CHECK(ctx != NULL);
568 ARG_CHECK(pubkey != NULL);
569 memset(pubkey, 0, sizeof(*pubkey));
571 ARG_CHECK(seckey != NULL);
572
573 ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey);
574 secp256k1_pubkey_save(pubkey, &p);
575 secp256k1_memczero(pubkey, sizeof(*pubkey), !ret);
576
577 secp256k1_scalar_clear(&seckey_scalar);
578 return ret;
579}
580
581int secp256k1_ec_seckey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
583 int ret = 0;
584 VERIFY_CHECK(ctx != NULL);
585 ARG_CHECK(seckey != NULL);
586
587 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
589 secp256k1_scalar_negate(&sec, &sec);
590 secp256k1_scalar_get_b32(seckey, &sec);
591
593 return ret;
594}
595
596int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
597 return secp256k1_ec_seckey_negate(ctx, seckey);
598}
599
601 int ret = 0;
602 secp256k1_ge p;
603 VERIFY_CHECK(ctx != NULL);
604 ARG_CHECK(pubkey != NULL);
605
606 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
607 memset(pubkey, 0, sizeof(*pubkey));
608 if (ret) {
609 secp256k1_ge_neg(&p, &p);
610 secp256k1_pubkey_save(pubkey, &p);
611 }
612 return ret;
613}
614
615
616static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak32) {
617 secp256k1_scalar term;
618 int overflow = 0;
619 int ret = 0;
620
621 secp256k1_scalar_set_b32(&term, tweak32, &overflow);
622 ret = (!overflow) & secp256k1_eckey_privkey_tweak_add(sec, &term);
624 return ret;
625}
626
627int secp256k1_ec_seckey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
629 int ret = 0;
630 VERIFY_CHECK(ctx != NULL);
631 ARG_CHECK(seckey != NULL);
632 ARG_CHECK(tweak32 != NULL);
633
634 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
635 ret &= secp256k1_ec_seckey_tweak_add_helper(&sec, tweak32);
637 secp256k1_scalar_get_b32(seckey, &sec);
638
640 return ret;
641}
642
643int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
644 return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak32);
645}
646
647static int secp256k1_ec_pubkey_tweak_add_helper(secp256k1_ge *p, const unsigned char *tweak32) {
648 secp256k1_scalar term;
649 int overflow = 0;
650 secp256k1_scalar_set_b32(&term, tweak32, &overflow);
651 return !overflow && secp256k1_eckey_pubkey_tweak_add(p, &term);
652}
653
654int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) {
655 secp256k1_ge p;
656 int ret = 0;
657 VERIFY_CHECK(ctx != NULL);
658 ARG_CHECK(pubkey != NULL);
659 ARG_CHECK(tweak32 != NULL);
660
661 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
662 memset(pubkey, 0, sizeof(*pubkey));
663 ret = ret && secp256k1_ec_pubkey_tweak_add_helper(&p, tweak32);
664 if (ret) {
665 secp256k1_pubkey_save(pubkey, &p);
666 }
667
668 return ret;
669}
670
671int secp256k1_ec_seckey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
672 secp256k1_scalar factor;
674 int ret = 0;
675 int overflow = 0;
676 VERIFY_CHECK(ctx != NULL);
677 ARG_CHECK(seckey != NULL);
678 ARG_CHECK(tweak32 != NULL);
679
680 secp256k1_scalar_set_b32(&factor, tweak32, &overflow);
681 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
682 ret &= (!overflow) & secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
684 secp256k1_scalar_get_b32(seckey, &sec);
685
687 secp256k1_scalar_clear(&factor);
688 return ret;
689}
690
691int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
692 return secp256k1_ec_seckey_tweak_mul(ctx, seckey, tweak32);
693}
694
695int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) {
696 secp256k1_ge p;
697 secp256k1_scalar factor;
698 int ret = 0;
699 int overflow = 0;
700 VERIFY_CHECK(ctx != NULL);
701 ARG_CHECK(pubkey != NULL);
702 ARG_CHECK(tweak32 != NULL);
703
704 secp256k1_scalar_set_b32(&factor, tweak32, &overflow);
705 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
706 memset(pubkey, 0, sizeof(*pubkey));
707 if (ret) {
708 if (secp256k1_eckey_pubkey_tweak_mul(&p, &factor)) {
709 secp256k1_pubkey_save(pubkey, &p);
710 } else {
711 ret = 0;
712 }
713 }
714
715 return ret;
716}
717
718int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
719 VERIFY_CHECK(ctx != NULL);
722 }
723 return 1;
724}
725
726int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
727 size_t i;
728 secp256k1_gej Qj;
729 secp256k1_ge Q;
730
731 VERIFY_CHECK(ctx != NULL);
732 ARG_CHECK(pubnonce != NULL);
733 memset(pubnonce, 0, sizeof(*pubnonce));
734 ARG_CHECK(n >= 1);
735 ARG_CHECK(pubnonces != NULL);
736
738
739 for (i = 0; i < n; i++) {
740 ARG_CHECK(pubnonces[i] != NULL);
741 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
742 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
743 }
744 if (secp256k1_gej_is_infinity(&Qj)) {
745 return 0;
746 }
747 secp256k1_ge_set_gej(&Q, &Qj);
748 secp256k1_pubkey_save(pubnonce, &Q);
749 return 1;
750}
751
752int secp256k1_tagged_sha256(const secp256k1_context* ctx, unsigned char *hash32, const unsigned char *tag, size_t taglen, const unsigned char *msg, size_t msglen) {
754 VERIFY_CHECK(ctx != NULL);
755 ARG_CHECK(hash32 != NULL);
756 ARG_CHECK(tag != NULL);
757 ARG_CHECK(msg != NULL);
758
759 secp256k1_sha256_initialize_tagged(&sha, tag, taglen);
760 secp256k1_sha256_write(&sha, msg, msglen);
761 secp256k1_sha256_finalize(&sha, hash32);
762 return 1;
763}
764
765#ifdef ENABLE_MODULE_ECDH
766# include "modules/ecdh/main_impl.h"
767#endif
768
769#ifdef ENABLE_MODULE_MULTISET
771#endif
772
773#ifdef ENABLE_MODULE_RECOVERY
775#endif
776
777#ifdef ENABLE_MODULE_SCHNORR
779#endif
780
781#ifdef ENABLE_MODULE_EXTRAKEYS
783#endif
784
785#ifdef ENABLE_MODULE_SCHNORRSIG
787#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:163
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:201
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:215
static void secp256k1_default_error_callback_fn(const char *str, void *data)
Definition: util.h:39
#define EXPECT(x, c)
Definition: util.h:75
static const secp256k1_callback default_error_callback
Definition: util.h:54
static void secp256k1_default_illegal_callback_fn(const char *str, void *data)
Definition: util.h:34
#define VERIFY_CHECK(cond)
Definition: util.h:100
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition: util.h:123
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
Definition: util.h:182
static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback *const cb, const char *const text)
Definition: util.h:29
static const secp256k1_callback default_illegal_callback
Definition: util.h:49
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:643
int secp256k1_ec_privkey_negate(const secp256k1_context *ctx, unsigned char *seckey)
Same as secp256k1_ec_seckey_negate, but DEPRECATED.
Definition: secp256k1.c:596
const secp256k1_nonce_function secp256k1_nonce_function_default
A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979).
Definition: secp256k1.c:467
secp256k1_context * secp256k1_context_preallocated_clone(const secp256k1_context *ctx, void *prealloc)
Copy a secp256k1 context object into caller-provided memory.
Definition: secp256k1.c:129
const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
Definition: secp256k1.c:466
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:752
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:654
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:257
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:367
static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak32)
Definition: secp256k1.c:616
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:671
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:239
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:87
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:469
int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey)
Verify an ECDSA secret key.
Definition: secp256k1.c:540
const secp256k1_context * secp256k1_context_no_precomp
A simple secp256k1 context object with no precomputed tables.
Definition: secp256k1.c:71
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:627
void secp256k1_context_preallocated_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object that has been created in caller-provided memory.
Definition: secp256k1.c:150
#define ARG_CHECK(cond)
Definition: secp256k1.c:45
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:551
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:392
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:173
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:330
static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context *ctx, const void *p, size_t len)
Definition: secp256k1.c:196
secp256k1_context * secp256k1_context_create(unsigned int flags)
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:118
int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey)
Negates a secret key in place.
Definition: secp256k1.c:581
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:280
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:726
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:346
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:164
static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature *sig, const secp256k1_scalar *r, const secp256k1_scalar *s)
Definition: secp256k1.c:320
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition: secp256k1.c:206
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:73
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:225
static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len)
Definition: secp256k1.c:427
static int secp256k1_ec_pubkey_tweak_add_helper(secp256k1_ge *p, const unsigned char *tweak32)
Definition: secp256k1.c:647
#define ARG_CHECK_NO_RETURN(cond)
Definition: secp256k1.c:52
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:432
static const secp256k1_context secp256k1_context_no_precomp_
Definition: secp256k1.c:65
int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32)
Updates the context randomization to protect against side-channel leakage.
Definition: secp256k1.c:718
secp256k1_scratch_space * secp256k1_scratch_space_create(const secp256k1_context *ctx, size_t max_size)
Create a secp256k1 scratch space object.
Definition: secp256k1.c:182
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:411
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:379
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:563
void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:157
secp256k1_context * secp256k1_context_clone(const secp256k1_context *ctx)
Copy a secp256k1 context object (into dynamically allocated memory).
Definition: secp256k1.c:139
void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space *scratch)
Destroy a secp256k1 scratch space.
Definition: secp256k1.c:187
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:695
static void secp256k1_ecdsa_signature_load(const secp256k1_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, const secp256k1_ecdsa_signature *sig)
Definition: secp256k1.c:306
secp256k1_context * secp256k1_context_preallocated_create(void *prealloc, unsigned int flags)
Create a secp256k1 context object in caller-provided memory.
Definition: secp256k1.c:93
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:525
int secp256k1_ec_pubkey_negate(const secp256k1_context *ctx, secp256k1_pubkey *pubkey)
Negates a public key in place.
Definition: secp256k1.c:600
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:691
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:194
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:205
#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:188
#define SECP256K1_FLAGS_BIT_COMPRESSION
Definition: secp256k1.h:195
#define SECP256K1_FLAGS_TYPE_CONTEXT
Definition: secp256k1.h:189
#define SECP256K1_FLAGS_TYPE_COMPRESSION
Definition: secp256k1.h:190
static int secp256k1_selftest(void)
Definition: selftest.h:28
void(* fn)(const char *text, void *data)
Definition: util.h:25
const void * data
Definition: util.h:26
secp256k1_callback illegal_callback
Definition: secp256k1.c:60
secp256k1_callback error_callback
Definition: secp256k1.c:61
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:59
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:83
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
secp256k1_fe x
Definition: group.h:17
secp256k1_fe y
Definition: group.h:18
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28
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