Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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
22
23#include "assumptions.h"
24#include "checkmem.h"
25#include "util.h"
26
27#include "field_impl.h"
28#include "scalar_impl.h"
29#include "group_impl.h"
30#include "ecmult_impl.h"
31#include "ecmult_const_impl.h"
32#include "ecmult_gen_impl.h"
33#include "ecdsa_impl.h"
34#include "eckey_impl.h"
35#include "hash_impl.h"
36#include "int128_impl.h"
37#include "scratch_impl.h"
38#include "selftest.h"
39#include "hsort_impl.h"
40
41#ifdef SECP256K1_NO_BUILD
42# error "secp256k1.h processed without SECP256K1_BUILD defined while building secp256k1.c"
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_VOID(cond) do { \
53 if (EXPECT(!(cond), 0)) { \
54 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
55 return; \
56 } \
57} while(0)
58
59/* Note that whenever you change the context struct, you must also change the
60 * context_eq function. */
67
76
77/* Helper function that determines if a context is proper, i.e., is not the static context or a copy thereof.
78 *
79 * This is intended for "context" functions such as secp256k1_context_clone. Functions that need specific
80 * features of a context should still check for these features directly. For example, a function that needs
81 * ecmult_gen should directly check for the existence of the ecmult_gen context. */
85
89 }
90}
91
93 size_t ret = sizeof(secp256k1_context);
94 /* A return value of 0 is reserved as an indicator for errors when we call this function internally. */
95 VERIFY_CHECK(ret != 0);
96
99 "Invalid flags");
100 return 0;
101 }
102
105 "Declassify flag requires running with memory checking");
106 return 0;
107 }
108
109 return ret;
110}
111
117
119 size_t prealloc_size;
121
123
125 if (prealloc_size == 0) {
126 return NULL;
127 }
128 VERIFY_CHECK(prealloc != NULL);
129 ret = (secp256k1_context*)prealloc;
130 ret->illegal_callback = default_illegal_callback;
131 ret->error_callback = default_error_callback;
132
133 /* Flags have been checked by secp256k1_context_preallocated_size. */
135 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx);
137
138 return ret;
139}
140
142 size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
144 if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
145 free(ctx);
146 return NULL;
147 }
148
149 return ctx;
150}
151
154 VERIFY_CHECK(ctx != NULL);
155 ARG_CHECK(prealloc != NULL);
157
158 ret = (secp256k1_context*)prealloc;
159 *ret = *ctx;
160 return ret;
161}
162
165 size_t prealloc_size;
166
167 VERIFY_CHECK(ctx != NULL);
169
171 ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
173 return ret;
174}
175
177 ARG_CHECK_VOID(ctx == NULL || secp256k1_context_is_proper(ctx));
178
179 /* Defined as noop */
180 if (ctx == NULL) {
181 return;
182 }
183
185}
186
188 ARG_CHECK_VOID(ctx == NULL || secp256k1_context_is_proper(ctx));
189
190 /* Defined as noop */
191 if (ctx == NULL) {
192 return;
193 }
194
196 free(ctx);
197}
198
199void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
200 /* We compare pointers instead of checking secp256k1_context_is_proper() here
201 because setting callbacks is allowed on *copies* of the static context:
202 it's harmless and makes testing easier. */
204 if (fun == NULL) {
206 }
207 ctx->illegal_callback.fn = fun;
208 ctx->illegal_callback.data = data;
209}
210
211void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
212 /* We compare pointers instead of checking secp256k1_context_is_proper() here
213 because setting callbacks is allowed on *copies* of the static context:
214 it's harmless and makes testing easier. */
216 if (fun == NULL) {
218 }
219 ctx->error_callback.fn = fun;
220 ctx->error_callback.data = data;
221}
222
224 VERIFY_CHECK(ctx != NULL);
225 return secp256k1_scratch_create(&ctx->error_callback, max_size);
226}
227
232
233/* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
234 * of the software.
235 */
236static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context* ctx, const void *p, size_t len) {
237 if (EXPECT(ctx->declassify, 0)) SECP256K1_CHECKMEM_DEFINE(p, len);
238}
239
240static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
242
243 /* We require that the secp256k1_ge_storage type is exactly 64 bytes.
244 * This is formally not guaranteed by the C standard, but should hold on any
245 * sane compiler in the real world. */
246 STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
247 memcpy(&s, &pubkey->data[0], 64);
250 return 1;
251}
252
255
256 STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
259 memcpy(&pubkey->data[0], &s, 64);
260}
261
262int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
263 secp256k1_ge Q;
264
265 VERIFY_CHECK(ctx != NULL);
266 ARG_CHECK(pubkey != NULL);
267 memset(pubkey, 0, sizeof(*pubkey));
268 ARG_CHECK(input != NULL);
269 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
270 return 0;
271 }
273 return 0;
274 }
275 secp256k1_pubkey_save(pubkey, &Q);
277 return 1;
278}
279
280int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
281 secp256k1_ge Q;
282 size_t len;
283 int ret = 0;
284
285 VERIFY_CHECK(ctx != NULL);
286 ARG_CHECK(outputlen != NULL);
287 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33u : 65u));
288 len = *outputlen;
289 *outputlen = 0;
290 ARG_CHECK(output != NULL);
291 memset(output, 0, len);
292 ARG_CHECK(pubkey != NULL);
294 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
296 if (ret) {
297 *outputlen = len;
298 }
299 }
300 return ret;
301}
302
303int secp256k1_ec_pubkey_cmp(const secp256k1_context* ctx, const secp256k1_pubkey* pubkey0, const secp256k1_pubkey* pubkey1) {
304 unsigned char out[2][33];
305 const secp256k1_pubkey* pk[2];
306 int i;
307
308 VERIFY_CHECK(ctx != NULL);
309 pk[0] = pubkey0; pk[1] = pubkey1;
310 for (i = 0; i < 2; i++) {
311 size_t out_size = sizeof(out[i]);
312 /* If the public key is NULL or invalid, ec_pubkey_serialize will call
313 * the illegal_callback and return 0. In that case we will serialize the
314 * key as all zeros which is less than any valid public key. This
315 * results in consistent comparisons even if NULL or invalid pubkeys are
316 * involved and prevents edge cases such as sorting algorithms that use
317 * this function and do not terminate as a result. */
318 if (!secp256k1_ec_pubkey_serialize(ctx, out[i], &out_size, pk[i], SECP256K1_EC_COMPRESSED)) {
319 /* Note that ec_pubkey_serialize should already set the output to
320 * zero in that case, but it's not guaranteed by the API, we can't
321 * test it and writing a VERIFY_CHECK is more complex than
322 * explicitly memsetting (again). */
323 memset(out[i], 0, sizeof(out[i]));
324 }
325 }
326 return secp256k1_memcmp_var(out[0], out[1], sizeof(out[0]));
327}
328
329static int secp256k1_ec_pubkey_sort_cmp(const void* pk1, const void* pk2, void *ctx) {
331 *(secp256k1_pubkey **)pk1,
332 *(secp256k1_pubkey **)pk2);
333}
334
335int secp256k1_ec_pubkey_sort(const secp256k1_context* ctx, const secp256k1_pubkey **pubkeys, size_t n_pubkeys) {
336 VERIFY_CHECK(ctx != NULL);
337 ARG_CHECK(pubkeys != NULL);
338
339 /* Suppress wrong warning (fixed in MSVC 19.33) */
340 #if defined(_MSC_VER) && (_MSC_VER < 1933)
341 #pragma warning(push)
342 #pragma warning(disable: 4090)
343 #endif
344
345 /* Casting away const is fine because neither secp256k1_hsort nor
346 * secp256k1_ec_pubkey_sort_cmp modify the data pointed to by the cmp_data
347 * argument. */
348 secp256k1_hsort(pubkeys, n_pubkeys, sizeof(*pubkeys), secp256k1_ec_pubkey_sort_cmp, (void *)ctx);
349
350 #if defined(_MSC_VER) && (_MSC_VER < 1933)
351 #pragma warning(pop)
352 #endif
353
354 return 1;
355}
356
358 (void)ctx;
359 if (sizeof(secp256k1_scalar) == 32) {
360 /* When the secp256k1_scalar type is exactly 32 byte, use its
361 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
362 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
363 memcpy(r, &sig->data[0], 32);
364 memcpy(s, &sig->data[32], 32);
365 } else {
366 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
367 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
368 }
369}
370
372 if (sizeof(secp256k1_scalar) == 32) {
373 memcpy(&sig->data[0], r, 32);
374 memcpy(&sig->data[32], s, 32);
375 } else {
376 secp256k1_scalar_get_b32(&sig->data[0], r);
377 secp256k1_scalar_get_b32(&sig->data[32], s);
378 }
379}
380
381int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
382 secp256k1_scalar r, s;
383
384 VERIFY_CHECK(ctx != NULL);
385 ARG_CHECK(sig != NULL);
386 ARG_CHECK(input != NULL);
387
388 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
390 return 1;
391 } else {
392 memset(sig, 0, sizeof(*sig));
393 return 0;
394 }
395}
396
398 secp256k1_scalar r, s;
399 int ret = 1;
400 int overflow = 0;
401
402 VERIFY_CHECK(ctx != NULL);
403 ARG_CHECK(sig != NULL);
404 ARG_CHECK(input64 != NULL);
405
406 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
407 ret &= !overflow;
408 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
409 ret &= !overflow;
410 if (ret) {
412 } else {
413 memset(sig, 0, sizeof(*sig));
414 }
415 return ret;
416}
417
418int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
419 secp256k1_scalar r, s;
420
421 VERIFY_CHECK(ctx != NULL);
422 ARG_CHECK(output != NULL);
423 ARG_CHECK(outputlen != NULL);
424 ARG_CHECK(sig != NULL);
425
426 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
427 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
428}
429
431 secp256k1_scalar r, s;
432
433 VERIFY_CHECK(ctx != NULL);
434 ARG_CHECK(output64 != NULL);
435 ARG_CHECK(sig != NULL);
436
437 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
438 secp256k1_scalar_get_b32(&output64[0], &r);
439 secp256k1_scalar_get_b32(&output64[32], &s);
440 return 1;
441}
442
444 secp256k1_scalar r, s;
445 int ret = 0;
446
447 VERIFY_CHECK(ctx != NULL);
448 ARG_CHECK(sigin != NULL);
449
450 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
452 if (sigout != NULL) {
453 if (ret) {
455 }
456 secp256k1_ecdsa_signature_save(sigout, &r, &s);
457 }
458
459 return ret;
460}
461
462int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) {
463 secp256k1_ge q;
464 secp256k1_scalar r, s;
466 VERIFY_CHECK(ctx != NULL);
467 ARG_CHECK(msghash32 != NULL);
468 ARG_CHECK(sig != NULL);
469 ARG_CHECK(pubkey != NULL);
470
471 secp256k1_scalar_set_b32(&m, msghash32, NULL);
472 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
473 return (!secp256k1_scalar_is_high(&s) &&
474 secp256k1_pubkey_load(ctx, &q, pubkey) &&
475 secp256k1_ecdsa_sig_verify(&r, &s, &q, &m));
476}
477
478static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
479 memcpy(buf + *offset, data, len);
480 *offset += len;
481}
482
483static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
484 unsigned char keydata[112];
485 unsigned int offset = 0;
487 unsigned int i;
489 unsigned char msgmod32[32];
490 secp256k1_scalar_set_b32(&msg, msg32, NULL);
491 secp256k1_scalar_get_b32(msgmod32, &msg);
492 /* We feed a byte array to the PRNG as input, consisting of:
493 * - the private key (32 bytes) and reduced message (32 bytes), see RFC 6979 3.2d.
494 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
495 * - optionally 16 extra bytes with the algorithm name.
496 * Because the arguments have distinct fixed lengths it is not possible for
497 * different argument mixtures to emulate each other and result in the same
498 * nonces.
499 */
500 buffer_append(keydata, &offset, key32, 32);
501 buffer_append(keydata, &offset, msgmod32, 32);
502 if (data != NULL) {
503 buffer_append(keydata, &offset, data, 32);
504 }
505 if (algo16 != NULL) {
506 buffer_append(keydata, &offset, algo16, 16);
507 }
508 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
509 memset(keydata, 0, sizeof(keydata));
510 for (i = 0; i <= counter; i++) {
512 }
514 return 1;
515}
516
519
520static 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 void* noncedata) {
521 secp256k1_scalar sec, non, msg;
522 int ret = 0;
523 int is_sec_valid;
524 unsigned char nonce32[32];
525 unsigned int count = 0;
526 /* Default initialization here is important so we won't pass uninit values to the cmov in the end */
529 if (recid) {
530 *recid = 0;
531 }
532 if (noncefp == NULL) {
534 }
535
536 /* Fail if the secret key is invalid. */
537 is_sec_valid = secp256k1_scalar_set_b32_seckey(&sec, seckey);
538 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_one, !is_sec_valid);
539 secp256k1_scalar_set_b32(&msg, msg32, NULL);
540 while (1) {
541 int is_nonce_valid;
542 ret = !!noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
543 if (!ret) {
544 break;
545 }
546 is_nonce_valid = secp256k1_scalar_set_b32_seckey(&non, nonce32);
547 /* The nonce is still secret here, but it being invalid is less likely than 1:2^255. */
548 secp256k1_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid));
549 if (is_nonce_valid) {
550 ret = secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid);
551 /* The final signature is no longer a secret, nor is the fact that we were successful or not. */
552 secp256k1_declassify(ctx, &ret, sizeof(ret));
553 if (ret) {
554 break;
555 }
556 }
557 count++;
558 }
559 /* We don't want to declassify is_sec_valid and therefore the range of
560 * seckey. As a result is_sec_valid is included in ret only after ret was
561 * used as a branching variable. */
562 ret &= is_sec_valid;
563 memset(nonce32, 0, 32);
569 if (recid) {
570 const int zero = 0;
571 secp256k1_int_cmov(recid, &zero, !ret);
572 }
573 return ret;
574}
575
576int 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) {
577 secp256k1_scalar r, s;
578 int ret;
579 VERIFY_CHECK(ctx != NULL);
581 ARG_CHECK(msghash32 != NULL);
582 ARG_CHECK(signature != NULL);
583 ARG_CHECK(seckey != NULL);
584
585 ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msghash32, seckey, noncefp, noncedata);
586 secp256k1_ecdsa_signature_save(signature, &r, &s);
587 return ret;
588}
589
590int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
592 int ret;
593 VERIFY_CHECK(ctx != NULL);
594 ARG_CHECK(seckey != NULL);
595
596 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
598 return ret;
599}
600
601static 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) {
602 secp256k1_gej pj;
603 int ret;
604
605 ret = secp256k1_scalar_set_b32_seckey(seckey_scalar, seckey);
607
608 secp256k1_ecmult_gen(ecmult_gen_ctx, &pj, seckey_scalar);
609 secp256k1_ge_set_gej(p, &pj);
610 return ret;
611}
612
613int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
614 secp256k1_ge p;
615 secp256k1_scalar seckey_scalar;
616 int ret = 0;
617 VERIFY_CHECK(ctx != NULL);
618 ARG_CHECK(pubkey != NULL);
619 memset(pubkey, 0, sizeof(*pubkey));
621 ARG_CHECK(seckey != NULL);
622
623 ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey);
624 secp256k1_pubkey_save(pubkey, &p);
625 secp256k1_memczero(pubkey, sizeof(*pubkey), !ret);
626
627 secp256k1_scalar_clear(&seckey_scalar);
628 return ret;
629}
630
631int secp256k1_ec_seckey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
633 int ret = 0;
634 VERIFY_CHECK(ctx != NULL);
635 ARG_CHECK(seckey != NULL);
636
637 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
639 secp256k1_scalar_negate(&sec, &sec);
640 secp256k1_scalar_get_b32(seckey, &sec);
641
643 return ret;
644}
645
646int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
647 return secp256k1_ec_seckey_negate(ctx, seckey);
648}
649
651 int ret = 0;
652 secp256k1_ge p;
653 VERIFY_CHECK(ctx != NULL);
654 ARG_CHECK(pubkey != NULL);
655
656 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
657 memset(pubkey, 0, sizeof(*pubkey));
658 if (ret) {
659 secp256k1_ge_neg(&p, &p);
660 secp256k1_pubkey_save(pubkey, &p);
661 }
662 return ret;
663}
664
665
666static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak32) {
667 secp256k1_scalar term;
668 int overflow = 0;
669 int ret = 0;
670
671 secp256k1_scalar_set_b32(&term, tweak32, &overflow);
672 ret = (!overflow) & secp256k1_eckey_privkey_tweak_add(sec, &term);
674 return ret;
675}
676
677int secp256k1_ec_seckey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
679 int ret = 0;
680 VERIFY_CHECK(ctx != NULL);
681 ARG_CHECK(seckey != NULL);
682 ARG_CHECK(tweak32 != NULL);
683
684 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
687 secp256k1_scalar_get_b32(seckey, &sec);
688
690 return ret;
691}
692
693int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
694 return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak32);
695}
696
697static int secp256k1_ec_pubkey_tweak_add_helper(secp256k1_ge *p, const unsigned char *tweak32) {
698 secp256k1_scalar term;
699 int overflow = 0;
700 secp256k1_scalar_set_b32(&term, tweak32, &overflow);
701 return !overflow && secp256k1_eckey_pubkey_tweak_add(p, &term);
702}
703
704int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) {
705 secp256k1_ge p;
706 int ret = 0;
707 VERIFY_CHECK(ctx != NULL);
708 ARG_CHECK(pubkey != NULL);
709 ARG_CHECK(tweak32 != NULL);
710
711 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
712 memset(pubkey, 0, sizeof(*pubkey));
714 if (ret) {
715 secp256k1_pubkey_save(pubkey, &p);
716 }
717
718 return ret;
719}
720
721int secp256k1_ec_seckey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
722 secp256k1_scalar factor;
724 int ret = 0;
725 int overflow = 0;
726 VERIFY_CHECK(ctx != NULL);
727 ARG_CHECK(seckey != NULL);
728 ARG_CHECK(tweak32 != NULL);
729
730 secp256k1_scalar_set_b32(&factor, tweak32, &overflow);
731 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
732 ret &= (!overflow) & secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
734 secp256k1_scalar_get_b32(seckey, &sec);
735
737 secp256k1_scalar_clear(&factor);
738 return ret;
739}
740
741int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
742 return secp256k1_ec_seckey_tweak_mul(ctx, seckey, tweak32);
743}
744
745int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) {
746 secp256k1_ge p;
747 secp256k1_scalar factor;
748 int ret = 0;
749 int overflow = 0;
750 VERIFY_CHECK(ctx != NULL);
751 ARG_CHECK(pubkey != NULL);
752 ARG_CHECK(tweak32 != NULL);
753
754 secp256k1_scalar_set_b32(&factor, tweak32, &overflow);
755 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
756 memset(pubkey, 0, sizeof(*pubkey));
757 if (ret) {
758 if (secp256k1_eckey_pubkey_tweak_mul(&p, &factor)) {
759 secp256k1_pubkey_save(pubkey, &p);
760 } else {
761 ret = 0;
762 }
763 }
764
765 return ret;
766}
767
768int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
769 VERIFY_CHECK(ctx != NULL);
771
774 }
775 return 1;
776}
777
778int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
779 size_t i;
780 secp256k1_gej Qj;
781 secp256k1_ge Q;
782
783 VERIFY_CHECK(ctx != NULL);
784 ARG_CHECK(pubnonce != NULL);
785 memset(pubnonce, 0, sizeof(*pubnonce));
786 ARG_CHECK(n >= 1);
787 ARG_CHECK(pubnonces != NULL);
788
790
791 for (i = 0; i < n; i++) {
792 ARG_CHECK(pubnonces[i] != NULL);
793 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
794 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
795 }
796 if (secp256k1_gej_is_infinity(&Qj)) {
797 return 0;
798 }
799 secp256k1_ge_set_gej(&Q, &Qj);
800 secp256k1_pubkey_save(pubnonce, &Q);
801 return 1;
802}
803
804int secp256k1_tagged_sha256(const secp256k1_context* ctx, unsigned char *hash32, const unsigned char *tag, size_t taglen, const unsigned char *msg, size_t msglen) {
806 VERIFY_CHECK(ctx != NULL);
807 ARG_CHECK(hash32 != NULL);
808 ARG_CHECK(tag != NULL);
809 ARG_CHECK(msg != NULL);
810
811 secp256k1_sha256_initialize_tagged(&sha, tag, taglen);
812 secp256k1_sha256_write(&sha, msg, msglen);
813 secp256k1_sha256_finalize(&sha, hash32);
814 return 1;
815}
816
817#ifdef ENABLE_MODULE_ECDH
818# include "modules/ecdh/main_impl.h"
819#endif
820
821#ifdef ENABLE_MODULE_RECOVERY
823#endif
824
825#ifdef ENABLE_MODULE_EXTRAKEYS
827#endif
828
829#ifdef ENABLE_MODULE_SCHNORRSIG
831#endif
832
833#ifdef ENABLE_MODULE_ELLSWIFT
835#endif
int ret
int flags
#define SECP256K1_CHECKMEM_DEFINE(p, len)
Definition checkmem.h:91
#define SECP256K1_CHECKMEM_RUNNING()
Definition checkmem.h:93
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)
#define secp256k1_fe_is_zero
Definition field.h:85
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_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
static void secp256k1_hsort(void *ptr, size_t count, size_t size, int(*cmp)(const void *, const void *, void *), void *cmp_data)
#define EXPECT(x, c)
Definition util.h:26
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:28
static const secp256k1_scalar secp256k1_scalar_one
Definition scalar_impl.h:27
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:229
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:243
static void secp256k1_default_error_callback_fn(const char *str, void *data)
Definition util.h:96
static const secp256k1_callback default_error_callback
Definition util.h:111
#define SECP256K1_INLINE
Definition util.h:48
#define STATIC_ASSERT(expr)
Assert statically that expr is true.
Definition util.h:58
static void secp256k1_default_illegal_callback_fn(const char *str, void *data)
Definition util.h:91
#define VERIFY_CHECK(cond)
Definition util.h:153
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition util.h:156
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
Definition util.h:210
static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback *const cb, const char *const text)
Definition util.h:86
static const secp256k1_callback default_illegal_callback
Definition util.h:106
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:693
int secp256k1_ec_privkey_negate(const secp256k1_context *ctx, unsigned char *seckey)
Same as secp256k1_ec_seckey_negate, but DEPRECATED.
Definition secp256k1.c:646
const secp256k1_nonce_function secp256k1_nonce_function_default
Definition secp256k1.c:518
secp256k1_context * secp256k1_context_preallocated_clone(const secp256k1_context *ctx, void *prealloc)
Copy a secp256k1 context object into caller-provided memory.
Definition secp256k1.c:152
const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
Definition secp256k1.c:517
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:804
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:704
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:280
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:418
static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak32)
Definition secp256k1.c:666
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:721
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:262
const secp256k1_context * secp256k1_context_static
Definition secp256k1.c:74
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:112
int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey)
Verify an ECDSA secret key.
Definition secp256k1.c:590
static int secp256k1_context_is_proper(const secp256k1_context *ctx)
Definition secp256k1.c:82
const secp256k1_context * secp256k1_context_no_precomp
Definition secp256k1.c:75
int secp256k1_ec_pubkey_sort(const secp256k1_context *ctx, const secp256k1_pubkey **pubkeys, size_t n_pubkeys)
Sort public keys using lexicographic (of compressed serialization) order.
Definition secp256k1.c:335
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:677
void secp256k1_context_preallocated_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object that has been created in caller-provided memory.
Definition secp256k1.c:176
#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:601
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:443
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:211
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:381
static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context *ctx, const void *p, size_t len)
Definition secp256k1.c:236
secp256k1_context * secp256k1_context_create(unsigned int flags)
Create a secp256k1 context object (in dynamically allocated memory).
Definition secp256k1.c:141
int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey)
Negates a secret key in place.
Definition secp256k1.c:631
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:303
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:778
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:397
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:199
static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature *sig, const secp256k1_scalar *r, const secp256k1_scalar *s)
Definition secp256k1.c:371
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition secp256k1.c:240
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:92
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition secp256k1.c:253
static int secp256k1_ec_pubkey_sort_cmp(const void *pk1, const void *pk2, void *ctx)
Definition secp256k1.c:329
static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len)
Definition secp256k1.c:478
static int secp256k1_ec_pubkey_tweak_add_helper(secp256k1_ge *p, const unsigned char *tweak32)
Definition secp256k1.c:697
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:483
int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32)
Randomizes the context to provide enhanced protection against side-channel leakage.
Definition secp256k1.c:768
secp256k1_scratch_space * secp256k1_scratch_space_create(const secp256k1_context *ctx, size_t max_size)
Create a secp256k1 scratch space object.
Definition secp256k1.c:223
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:462
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:430
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:613
void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition secp256k1.c:187
void secp256k1_selftest(void)
Perform basic self tests (to be used in conjunction with secp256k1_context_static)
Definition secp256k1.c:86
secp256k1_context * secp256k1_context_clone(const secp256k1_context *ctx)
Copy a secp256k1 context object (into dynamically allocated memory).
Definition secp256k1.c:163
void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space *scratch)
Destroy a secp256k1 scratch space.
Definition secp256k1.c:228
static const secp256k1_context secp256k1_context_static_
Definition secp256k1.c:68
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:745
static void secp256k1_ecdsa_signature_load(const secp256k1_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, const secp256k1_ecdsa_signature *sig)
Definition secp256k1.c:357
secp256k1_context * secp256k1_context_preallocated_create(void *prealloc, unsigned int flags)
Create a secp256k1 context object in caller-provided memory.
Definition secp256k1.c:118
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:576
int secp256k1_ec_pubkey_negate(const secp256k1_context *ctx, secp256k1_pubkey *pubkey)
Negates a public key in place.
Definition secp256k1.c:650
#define ARG_CHECK_VOID(cond)
Definition secp256k1.c:52
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:741
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 void *noncedata)
Definition secp256k1.c:520
struct secp256k1_context_struct secp256k1_context
Unless explicitly stated all pointer arguments must not be NULL.
Definition secp256k1.h:50
#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY
Definition secp256k1.h:200
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:107
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition secp256k1.h:215
#define SECP256K1_FLAGS_TYPE_MASK
Definition secp256k1.h:194
#define SECP256K1_FLAGS_BIT_COMPRESSION
Definition secp256k1.h:201
#define SECP256K1_FLAGS_TYPE_CONTEXT
Definition secp256k1.h:195
#define SECP256K1_FLAGS_TYPE_COMPRESSION
Definition secp256k1.h:196
static int secp256k1_selftest_passes(void)
Definition selftest.h:28
void(* fn)(const char *text, void *data)
Definition util.h:82
const void * data
Definition util.h:83
secp256k1_callback illegal_callback
Definition secp256k1.c:63
secp256k1_callback error_callback
Definition secp256k1.c:64
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition secp256k1.c:62
Opaque data structured that holds a parsed ECDSA signature.
Definition secp256k1.h:87
unsigned char data[64]
Definition secp256k1.h:88
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
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:74
unsigned char data[64]
Definition secp256k1.h:75
A scalar modulo the group order of the secp256k1 curve.
Definition scalar_4x64.h:13
static int count