1 /**********************************************************************
2 * Copyright (c) 2013-2015 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
7 #include "include/secp256k1.h"
8 #include "include/secp256k1_preallocated.h"
10 #include "assumptions.h"
13 #include "field_impl.h"
14 #include "scalar_impl.h"
15 #include "group_impl.h"
16 #include "ecmult_impl.h"
17 #include "ecmult_const_impl.h"
18 #include "ecmult_gen_impl.h"
19 #include "ecdsa_impl.h"
20 #include "eckey_impl.h"
21 #include "hash_impl.h"
22 #include "scratch_impl.h"
26 # include <valgrind/memcheck.h>
29 #define ARG_CHECK(cond) do { \
30 if (EXPECT(!(cond), 0)) { \
31 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
36 #define ARG_CHECK_NO_RETURN(cond) do { \
37 if (EXPECT(!(cond), 0)) { \
38 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
42 #ifndef USE_EXTERNAL_DEFAULT_CALLBACKS
45 static void secp256k1_default_illegal_callback_fn(const char* str, void* data) {
47 fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
50 static void secp256k1_default_error_callback_fn(const char* str, void* data) {
52 fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
56 void secp256k1_default_illegal_callback_fn(const char* str, void* data);
57 void secp256k1_default_error_callback_fn(const char* str, void* data);
60 static const secp256k1_callback default_illegal_callback = {
61 secp256k1_default_illegal_callback_fn,
65 static const secp256k1_callback default_error_callback = {
66 secp256k1_default_error_callback_fn,
70 struct secp256k1_context_struct {
71 secp256k1_ecmult_context ecmult_ctx;
72 secp256k1_ecmult_gen_context ecmult_gen_ctx;
73 secp256k1_callback illegal_callback;
74 secp256k1_callback error_callback;
78 static const secp256k1_context secp256k1_context_no_precomp_ = {
81 { secp256k1_default_illegal_callback_fn, 0 },
82 { secp256k1_default_error_callback_fn, 0 },
85 const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_;
87 size_t secp256k1_context_preallocated_size(unsigned int flags) {
88 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
90 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
91 secp256k1_callback_call(&default_illegal_callback,
96 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
97 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
99 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
100 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
105 size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) {
106 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
107 VERIFY_CHECK(ctx != NULL);
108 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
109 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
111 if (secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)) {
112 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
117 secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) {
118 void* const base = prealloc;
119 size_t prealloc_size;
120 secp256k1_context* ret;
122 if (!secp256k1_selftest()) {
123 secp256k1_callback_call(&default_error_callback, "self test failed");
125 VERIFY_CHECK(prealloc != NULL);
126 prealloc_size = secp256k1_context_preallocated_size(flags);
127 ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);
128 ret->illegal_callback = default_illegal_callback;
129 ret->error_callback = default_error_callback;
131 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
132 secp256k1_callback_call(&ret->illegal_callback,
137 secp256k1_ecmult_context_init(&ret->ecmult_ctx);
138 secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
140 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
141 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &prealloc);
143 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
144 secp256k1_ecmult_context_build(&ret->ecmult_ctx, &prealloc);
146 ret->declassify = !!(flags & SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY);
148 return (secp256k1_context*) ret;
151 secp256k1_context* secp256k1_context_create(unsigned int flags) {
152 size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
153 secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
154 if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
162 secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
163 size_t prealloc_size;
164 secp256k1_context* ret;
165 VERIFY_CHECK(ctx != NULL);
166 ARG_CHECK(prealloc != NULL);
168 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
169 ret = (secp256k1_context*)prealloc;
170 memcpy(ret, ctx, prealloc_size);
171 secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx);
172 secp256k1_ecmult_context_finalize_memcpy(&ret->ecmult_ctx, &ctx->ecmult_ctx);
176 secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
177 secp256k1_context* ret;
178 size_t prealloc_size;
180 VERIFY_CHECK(ctx != NULL);
181 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
182 ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
183 ret = secp256k1_context_preallocated_clone(ctx, ret);
187 void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
188 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
190 secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
191 secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
195 void secp256k1_context_destroy(secp256k1_context* ctx) {
197 secp256k1_context_preallocated_destroy(ctx);
202 void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
203 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
205 fun = secp256k1_default_illegal_callback_fn;
207 ctx->illegal_callback.fn = fun;
208 ctx->illegal_callback.data = data;
211 void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
212 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
214 fun = secp256k1_default_error_callback_fn;
216 ctx->error_callback.fn = fun;
217 ctx->error_callback.data = data;
220 secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
221 VERIFY_CHECK(ctx != NULL);
222 return secp256k1_scratch_create(&ctx->error_callback, max_size);
225 void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
226 VERIFY_CHECK(ctx != NULL);
227 secp256k1_scratch_destroy(&ctx->error_callback, scratch);
230 /* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
231 * of the software. This is setup for use with valgrind but could be substituted with
232 * the appropriate instrumentation for other analysis tools.
234 static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context* ctx, void *p, size_t len) {
235 #if defined(VALGRIND)
236 if (EXPECT(ctx->declassify,0)) VALGRIND_MAKE_MEM_DEFINED(p, len);
244 static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
245 if (sizeof(secp256k1_ge_storage) == 64) {
246 /* When the secp256k1_ge_storage type is exactly 64 byte, use its
247 * representation inside secp256k1_pubkey, as conversion is very fast.
248 * Note that secp256k1_pubkey_save must use the same representation. */
249 secp256k1_ge_storage s;
250 memcpy(&s, &pubkey->data[0], sizeof(s));
251 secp256k1_ge_from_storage(ge, &s);
253 /* Otherwise, fall back to 32-byte big endian for X and Y. */
255 secp256k1_fe_set_b32(&x, pubkey->data);
256 secp256k1_fe_set_b32(&y, pubkey->data + 32);
257 secp256k1_ge_set_xy(ge, &x, &y);
259 ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
263 static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
264 if (sizeof(secp256k1_ge_storage) == 64) {
265 secp256k1_ge_storage s;
266 secp256k1_ge_to_storage(&s, ge);
267 memcpy(&pubkey->data[0], &s, sizeof(s));
269 VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
270 secp256k1_fe_normalize_var(&ge->x);
271 secp256k1_fe_normalize_var(&ge->y);
272 secp256k1_fe_get_b32(pubkey->data, &ge->x);
273 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
277 int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
280 VERIFY_CHECK(ctx != NULL);
281 ARG_CHECK(pubkey != NULL);
282 memset(pubkey, 0, sizeof(*pubkey));
283 ARG_CHECK(input != NULL);
284 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
287 secp256k1_pubkey_save(pubkey, &Q);
288 secp256k1_ge_clear(&Q);
292 int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
297 VERIFY_CHECK(ctx != NULL);
298 ARG_CHECK(outputlen != NULL);
299 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33u : 65u));
302 ARG_CHECK(output != NULL);
303 memset(output, 0, len);
304 ARG_CHECK(pubkey != NULL);
305 ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
306 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
307 ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
315 static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) {
317 if (sizeof(secp256k1_scalar) == 32) {
318 /* When the secp256k1_scalar type is exactly 32 byte, use its
319 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
320 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
321 memcpy(r, &sig->data[0], 32);
322 memcpy(s, &sig->data[32], 32);
324 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
325 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
329 static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) {
330 if (sizeof(secp256k1_scalar) == 32) {
331 memcpy(&sig->data[0], r, 32);
332 memcpy(&sig->data[32], s, 32);
334 secp256k1_scalar_get_b32(&sig->data[0], r);
335 secp256k1_scalar_get_b32(&sig->data[32], s);
339 int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
340 secp256k1_scalar r, s;
342 VERIFY_CHECK(ctx != NULL);
343 ARG_CHECK(sig != NULL);
344 ARG_CHECK(input != NULL);
346 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
347 secp256k1_ecdsa_signature_save(sig, &r, &s);
350 memset(sig, 0, sizeof(*sig));
355 int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input64) {
356 secp256k1_scalar r, s;
360 VERIFY_CHECK(ctx != NULL);
361 ARG_CHECK(sig != NULL);
362 ARG_CHECK(input64 != NULL);
364 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
366 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
369 secp256k1_ecdsa_signature_save(sig, &r, &s);
371 memset(sig, 0, sizeof(*sig));
376 int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
377 secp256k1_scalar r, s;
379 VERIFY_CHECK(ctx != NULL);
380 ARG_CHECK(output != NULL);
381 ARG_CHECK(outputlen != NULL);
382 ARG_CHECK(sig != NULL);
384 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
385 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
388 int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context* ctx, unsigned char *output64, const secp256k1_ecdsa_signature* sig) {
389 secp256k1_scalar r, s;
391 VERIFY_CHECK(ctx != NULL);
392 ARG_CHECK(output64 != NULL);
393 ARG_CHECK(sig != NULL);
395 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
396 secp256k1_scalar_get_b32(&output64[0], &r);
397 secp256k1_scalar_get_b32(&output64[32], &s);
401 int secp256k1_ecdsa_signature_normalize(const secp256k1_context* ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) {
402 secp256k1_scalar r, s;
405 VERIFY_CHECK(ctx != NULL);
406 ARG_CHECK(sigin != NULL);
408 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
409 ret = secp256k1_scalar_is_high(&s);
410 if (sigout != NULL) {
412 secp256k1_scalar_negate(&s, &s);
414 secp256k1_ecdsa_signature_save(sigout, &r, &s);
420 int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const secp256k1_pubkey *pubkey) {
422 secp256k1_scalar r, s;
424 VERIFY_CHECK(ctx != NULL);
425 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
426 ARG_CHECK(msg32 != NULL);
427 ARG_CHECK(sig != NULL);
428 ARG_CHECK(pubkey != NULL);
430 secp256k1_scalar_set_b32(&m, msg32, NULL);
431 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
432 return (!secp256k1_scalar_is_high(&s) &&
433 secp256k1_pubkey_load(ctx, &q, pubkey) &&
434 secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
437 static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
438 memcpy(buf + *offset, data, len);
442 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) {
443 unsigned char keydata[112];
444 unsigned int offset = 0;
445 secp256k1_rfc6979_hmac_sha256 rng;
447 /* We feed a byte array to the PRNG as input, consisting of:
448 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
449 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
450 * - optionally 16 extra bytes with the algorithm name.
451 * Because the arguments have distinct fixed lengths it is not possible for
452 * different argument mixtures to emulate each other and result in the same
455 buffer_append(keydata, &offset, key32, 32);
456 buffer_append(keydata, &offset, msg32, 32);
458 buffer_append(keydata, &offset, data, 32);
460 if (algo16 != NULL) {
461 buffer_append(keydata, &offset, algo16, 16);
463 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
464 memset(keydata, 0, sizeof(keydata));
465 for (i = 0; i <= counter; i++) {
466 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
468 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
472 const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
473 const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979;
475 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) {
476 secp256k1_scalar sec, non, msg;
479 unsigned char nonce32[32];
480 unsigned int count = 0;
481 /* Default initialization here is important so we won't pass uninit values to the cmov in the end */
482 *r = secp256k1_scalar_zero;
483 *s = secp256k1_scalar_zero;
487 if (noncefp == NULL) {
488 noncefp = secp256k1_nonce_function_default;
491 /* Fail if the secret key is invalid. */
492 is_sec_valid = secp256k1_scalar_set_b32_seckey(&sec, seckey);
493 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_one, !is_sec_valid);
494 secp256k1_scalar_set_b32(&msg, msg32, NULL);
497 ret = !!noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
501 is_nonce_valid = secp256k1_scalar_set_b32_seckey(&non, nonce32);
502 /* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */
503 secp256k1_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid));
504 if (is_nonce_valid) {
505 ret = secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid);
506 /* The final signature is no longer a secret, nor is the fact that we were successful or not. */
507 secp256k1_declassify(ctx, &ret, sizeof(ret));
514 /* We don't want to declassify is_sec_valid and therefore the range of
515 * seckey. As a result is_sec_valid is included in ret only after ret was
516 * used as a branching variable. */
518 memset(nonce32, 0, 32);
519 secp256k1_scalar_clear(&msg);
520 secp256k1_scalar_clear(&non);
521 secp256k1_scalar_clear(&sec);
522 secp256k1_scalar_cmov(r, &secp256k1_scalar_zero, !ret);
523 secp256k1_scalar_cmov(s, &secp256k1_scalar_zero, !ret);
526 secp256k1_int_cmov(recid, &zero, !ret);
531 int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
532 secp256k1_scalar r, s;
534 VERIFY_CHECK(ctx != NULL);
535 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
536 ARG_CHECK(msg32 != NULL);
537 ARG_CHECK(signature != NULL);
538 ARG_CHECK(seckey != NULL);
540 ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msg32, seckey, noncefp, noncedata);
541 secp256k1_ecdsa_signature_save(signature, &r, &s);
545 int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
546 secp256k1_scalar sec;
548 VERIFY_CHECK(ctx != NULL);
549 ARG_CHECK(seckey != NULL);
551 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
552 secp256k1_scalar_clear(&sec);
556 int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
559 secp256k1_scalar sec;
561 VERIFY_CHECK(ctx != NULL);
562 ARG_CHECK(pubkey != NULL);
563 memset(pubkey, 0, sizeof(*pubkey));
564 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
565 ARG_CHECK(seckey != NULL);
567 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
568 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_one, !ret);
570 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pj, &sec);
571 secp256k1_ge_set_gej(&p, &pj);
572 secp256k1_pubkey_save(pubkey, &p);
573 memczero(pubkey, sizeof(*pubkey), !ret);
575 secp256k1_scalar_clear(&sec);
579 int secp256k1_ec_seckey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
580 secp256k1_scalar sec;
582 VERIFY_CHECK(ctx != NULL);
583 ARG_CHECK(seckey != NULL);
585 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
586 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret);
587 secp256k1_scalar_negate(&sec, &sec);
588 secp256k1_scalar_get_b32(seckey, &sec);
590 secp256k1_scalar_clear(&sec);
594 int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
595 return secp256k1_ec_seckey_negate(ctx, seckey);
598 int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *pubkey) {
601 VERIFY_CHECK(ctx != NULL);
602 ARG_CHECK(pubkey != NULL);
604 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
605 memset(pubkey, 0, sizeof(*pubkey));
607 secp256k1_ge_neg(&p, &p);
608 secp256k1_pubkey_save(pubkey, &p);
613 int secp256k1_ec_seckey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
614 secp256k1_scalar term;
615 secp256k1_scalar sec;
618 VERIFY_CHECK(ctx != NULL);
619 ARG_CHECK(seckey != NULL);
620 ARG_CHECK(tweak != NULL);
622 secp256k1_scalar_set_b32(&term, tweak, &overflow);
623 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
625 ret &= (!overflow) & secp256k1_eckey_privkey_tweak_add(&sec, &term);
626 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret);
627 secp256k1_scalar_get_b32(seckey, &sec);
629 secp256k1_scalar_clear(&sec);
630 secp256k1_scalar_clear(&term);
634 int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
635 return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak);
638 int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
640 secp256k1_scalar term;
643 VERIFY_CHECK(ctx != NULL);
644 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
645 ARG_CHECK(pubkey != NULL);
646 ARG_CHECK(tweak != NULL);
648 secp256k1_scalar_set_b32(&term, tweak, &overflow);
649 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
650 memset(pubkey, 0, sizeof(*pubkey));
652 if (secp256k1_eckey_pubkey_tweak_add(&ctx->ecmult_ctx, &p, &term)) {
653 secp256k1_pubkey_save(pubkey, &p);
662 int secp256k1_ec_seckey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
663 secp256k1_scalar factor;
664 secp256k1_scalar sec;
667 VERIFY_CHECK(ctx != NULL);
668 ARG_CHECK(seckey != NULL);
669 ARG_CHECK(tweak != NULL);
671 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
672 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
673 ret &= (!overflow) & secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
674 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret);
675 secp256k1_scalar_get_b32(seckey, &sec);
677 secp256k1_scalar_clear(&sec);
678 secp256k1_scalar_clear(&factor);
682 int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
683 return secp256k1_ec_seckey_tweak_mul(ctx, seckey, tweak);
686 int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
688 secp256k1_scalar factor;
691 VERIFY_CHECK(ctx != NULL);
692 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
693 ARG_CHECK(pubkey != NULL);
694 ARG_CHECK(tweak != NULL);
696 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
697 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
698 memset(pubkey, 0, sizeof(*pubkey));
700 if (secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor)) {
701 secp256k1_pubkey_save(pubkey, &p);
710 int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
711 VERIFY_CHECK(ctx != NULL);
712 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
713 secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
718 int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
723 ARG_CHECK(pubnonce != NULL);
724 memset(pubnonce, 0, sizeof(*pubnonce));
726 ARG_CHECK(pubnonces != NULL);
728 secp256k1_gej_set_infinity(&Qj);
730 for (i = 0; i < n; i++) {
731 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
732 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
734 if (secp256k1_gej_is_infinity(&Qj)) {
737 secp256k1_ge_set_gej(&Q, &Qj);
738 secp256k1_pubkey_save(pubnonce, &Q);
742 #ifdef ENABLE_MODULE_ECDH
743 # include "modules/ecdh/main_impl.h"
746 #ifdef ENABLE_MODULE_RECOVERY
747 # include "modules/recovery/main_impl.h"