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