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"
12 #include "field_impl.h"
13 #include "scalar_impl.h"
14 #include "group_impl.h"
15 #include "ecmult_impl.h"
16 #include "ecmult_const_impl.h"
17 #include "ecmult_gen_impl.h"
18 #include "ecdsa_impl.h"
19 #include "eckey_impl.h"
20 #include "hash_impl.h"
21 #include "scratch_impl.h"
23 #define ARG_CHECK(cond) do { \
24 if (EXPECT(!(cond), 0)) { \
25 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
30 #define ARG_CHECK_NO_RETURN(cond) do { \
31 if (EXPECT(!(cond), 0)) { \
32 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
36 #ifndef USE_EXTERNAL_DEFAULT_CALLBACKS
39 static void secp256k1_default_illegal_callback_fn(const char* str, void* data) {
41 fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
44 static void secp256k1_default_error_callback_fn(const char* str, void* data) {
46 fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
50 void secp256k1_default_illegal_callback_fn(const char* str, void* data);
51 void secp256k1_default_error_callback_fn(const char* str, void* data);
54 static const secp256k1_callback default_illegal_callback = {
55 secp256k1_default_illegal_callback_fn,
59 static const secp256k1_callback default_error_callback = {
60 secp256k1_default_error_callback_fn,
64 struct secp256k1_context_struct {
65 secp256k1_ecmult_context ecmult_ctx;
66 secp256k1_ecmult_gen_context ecmult_gen_ctx;
67 secp256k1_callback illegal_callback;
68 secp256k1_callback error_callback;
71 static const secp256k1_context secp256k1_context_no_precomp_ = {
74 { secp256k1_default_illegal_callback_fn, 0 },
75 { secp256k1_default_error_callback_fn, 0 }
77 const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_;
79 size_t secp256k1_context_preallocated_size(unsigned int flags) {
80 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
82 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
83 secp256k1_callback_call(&default_illegal_callback,
88 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
89 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
91 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
92 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
97 size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) {
98 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
99 VERIFY_CHECK(ctx != NULL);
100 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
101 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
103 if (secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)) {
104 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
109 secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) {
110 void* const base = prealloc;
111 size_t prealloc_size;
112 secp256k1_context* ret;
114 VERIFY_CHECK(prealloc != NULL);
115 prealloc_size = secp256k1_context_preallocated_size(flags);
116 ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);
117 ret->illegal_callback = default_illegal_callback;
118 ret->error_callback = default_error_callback;
120 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
121 secp256k1_callback_call(&ret->illegal_callback,
126 secp256k1_ecmult_context_init(&ret->ecmult_ctx);
127 secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
129 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
130 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &prealloc);
132 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
133 secp256k1_ecmult_context_build(&ret->ecmult_ctx, &prealloc);
136 return (secp256k1_context*) ret;
139 secp256k1_context* secp256k1_context_create(unsigned int flags) {
140 size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
141 secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
142 if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
150 secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
151 size_t prealloc_size;
152 secp256k1_context* ret;
153 VERIFY_CHECK(ctx != NULL);
154 ARG_CHECK(prealloc != NULL);
156 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
157 ret = (secp256k1_context*)prealloc;
158 memcpy(ret, ctx, prealloc_size);
159 secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx);
160 secp256k1_ecmult_context_finalize_memcpy(&ret->ecmult_ctx, &ctx->ecmult_ctx);
164 secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
165 secp256k1_context* ret;
166 size_t prealloc_size;
168 VERIFY_CHECK(ctx != NULL);
169 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
170 ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
171 ret = secp256k1_context_preallocated_clone(ctx, ret);
175 void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
176 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
178 secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
179 secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
183 void secp256k1_context_destroy(secp256k1_context* ctx) {
185 secp256k1_context_preallocated_destroy(ctx);
190 void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
191 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
193 fun = secp256k1_default_illegal_callback_fn;
195 ctx->illegal_callback.fn = fun;
196 ctx->illegal_callback.data = data;
199 void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
200 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
202 fun = secp256k1_default_error_callback_fn;
204 ctx->error_callback.fn = fun;
205 ctx->error_callback.data = data;
208 secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
209 VERIFY_CHECK(ctx != NULL);
210 return secp256k1_scratch_create(&ctx->error_callback, max_size);
213 void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
214 VERIFY_CHECK(ctx != NULL);
215 secp256k1_scratch_destroy(&ctx->error_callback, scratch);
218 static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
219 if (sizeof(secp256k1_ge_storage) == 64) {
220 /* When the secp256k1_ge_storage type is exactly 64 byte, use its
221 * representation inside secp256k1_pubkey, as conversion is very fast.
222 * Note that secp256k1_pubkey_save must use the same representation. */
223 secp256k1_ge_storage s;
224 memcpy(&s, &pubkey->data[0], sizeof(s));
225 secp256k1_ge_from_storage(ge, &s);
227 /* Otherwise, fall back to 32-byte big endian for X and Y. */
229 secp256k1_fe_set_b32(&x, pubkey->data);
230 secp256k1_fe_set_b32(&y, pubkey->data + 32);
231 secp256k1_ge_set_xy(ge, &x, &y);
233 ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
237 static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
238 if (sizeof(secp256k1_ge_storage) == 64) {
239 secp256k1_ge_storage s;
240 secp256k1_ge_to_storage(&s, ge);
241 memcpy(&pubkey->data[0], &s, sizeof(s));
243 VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
244 secp256k1_fe_normalize_var(&ge->x);
245 secp256k1_fe_normalize_var(&ge->y);
246 secp256k1_fe_get_b32(pubkey->data, &ge->x);
247 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
251 int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
254 VERIFY_CHECK(ctx != NULL);
255 ARG_CHECK(pubkey != NULL);
256 memset(pubkey, 0, sizeof(*pubkey));
257 ARG_CHECK(input != NULL);
258 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
261 secp256k1_pubkey_save(pubkey, &Q);
262 secp256k1_ge_clear(&Q);
266 int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
271 VERIFY_CHECK(ctx != NULL);
272 ARG_CHECK(outputlen != NULL);
273 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33 : 65));
276 ARG_CHECK(output != NULL);
277 memset(output, 0, len);
278 ARG_CHECK(pubkey != NULL);
279 ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
280 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
281 ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
289 static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) {
291 if (sizeof(secp256k1_scalar) == 32) {
292 /* When the secp256k1_scalar type is exactly 32 byte, use its
293 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
294 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
295 memcpy(r, &sig->data[0], 32);
296 memcpy(s, &sig->data[32], 32);
298 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
299 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
303 static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) {
304 if (sizeof(secp256k1_scalar) == 32) {
305 memcpy(&sig->data[0], r, 32);
306 memcpy(&sig->data[32], s, 32);
308 secp256k1_scalar_get_b32(&sig->data[0], r);
309 secp256k1_scalar_get_b32(&sig->data[32], s);
313 int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
314 secp256k1_scalar r, s;
316 VERIFY_CHECK(ctx != NULL);
317 ARG_CHECK(sig != NULL);
318 ARG_CHECK(input != NULL);
320 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
321 secp256k1_ecdsa_signature_save(sig, &r, &s);
324 memset(sig, 0, sizeof(*sig));
329 int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input64) {
330 secp256k1_scalar r, s;
334 VERIFY_CHECK(ctx != NULL);
335 ARG_CHECK(sig != NULL);
336 ARG_CHECK(input64 != NULL);
338 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
340 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
343 secp256k1_ecdsa_signature_save(sig, &r, &s);
345 memset(sig, 0, sizeof(*sig));
350 int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
351 secp256k1_scalar r, s;
353 VERIFY_CHECK(ctx != NULL);
354 ARG_CHECK(output != NULL);
355 ARG_CHECK(outputlen != NULL);
356 ARG_CHECK(sig != NULL);
358 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
359 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
362 int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context* ctx, unsigned char *output64, const secp256k1_ecdsa_signature* sig) {
363 secp256k1_scalar r, s;
365 VERIFY_CHECK(ctx != NULL);
366 ARG_CHECK(output64 != NULL);
367 ARG_CHECK(sig != NULL);
369 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
370 secp256k1_scalar_get_b32(&output64[0], &r);
371 secp256k1_scalar_get_b32(&output64[32], &s);
375 int secp256k1_ecdsa_signature_normalize(const secp256k1_context* ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) {
376 secp256k1_scalar r, s;
379 VERIFY_CHECK(ctx != NULL);
380 ARG_CHECK(sigin != NULL);
382 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
383 ret = secp256k1_scalar_is_high(&s);
384 if (sigout != NULL) {
386 secp256k1_scalar_negate(&s, &s);
388 secp256k1_ecdsa_signature_save(sigout, &r, &s);
394 int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const secp256k1_pubkey *pubkey) {
396 secp256k1_scalar r, s;
398 VERIFY_CHECK(ctx != NULL);
399 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
400 ARG_CHECK(msg32 != NULL);
401 ARG_CHECK(sig != NULL);
402 ARG_CHECK(pubkey != NULL);
404 secp256k1_scalar_set_b32(&m, msg32, NULL);
405 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
406 return (!secp256k1_scalar_is_high(&s) &&
407 secp256k1_pubkey_load(ctx, &q, pubkey) &&
408 secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
411 static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
412 memcpy(buf + *offset, data, len);
416 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) {
417 unsigned char keydata[112];
418 unsigned int offset = 0;
419 secp256k1_rfc6979_hmac_sha256 rng;
421 /* We feed a byte array to the PRNG as input, consisting of:
422 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
423 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
424 * - optionally 16 extra bytes with the algorithm name.
425 * Because the arguments have distinct fixed lengths it is not possible for
426 * different argument mixtures to emulate each other and result in the same
429 buffer_append(keydata, &offset, key32, 32);
430 buffer_append(keydata, &offset, msg32, 32);
432 buffer_append(keydata, &offset, data, 32);
434 if (algo16 != NULL) {
435 buffer_append(keydata, &offset, algo16, 16);
437 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
438 memset(keydata, 0, sizeof(keydata));
439 for (i = 0; i <= counter; i++) {
440 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
442 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
446 const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
447 const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979;
449 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) {
450 secp256k1_scalar r, s;
451 secp256k1_scalar sec, non, msg;
454 VERIFY_CHECK(ctx != NULL);
455 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
456 ARG_CHECK(msg32 != NULL);
457 ARG_CHECK(signature != NULL);
458 ARG_CHECK(seckey != NULL);
459 if (noncefp == NULL) {
460 noncefp = secp256k1_nonce_function_default;
463 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
464 /* Fail if the secret key is invalid. */
465 if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
466 unsigned char nonce32[32];
467 unsigned int count = 0;
468 secp256k1_scalar_set_b32(&msg, msg32, NULL);
470 ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
474 secp256k1_scalar_set_b32(&non, nonce32, &overflow);
475 if (!overflow && !secp256k1_scalar_is_zero(&non)) {
476 if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) {
482 memset(nonce32, 0, 32);
483 secp256k1_scalar_clear(&msg);
484 secp256k1_scalar_clear(&non);
485 secp256k1_scalar_clear(&sec);
488 secp256k1_ecdsa_signature_save(signature, &r, &s);
490 memset(signature, 0, sizeof(*signature));
495 int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
496 secp256k1_scalar sec;
499 VERIFY_CHECK(ctx != NULL);
500 ARG_CHECK(seckey != NULL);
502 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
503 ret = !overflow && !secp256k1_scalar_is_zero(&sec);
504 secp256k1_scalar_clear(&sec);
508 int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
511 secp256k1_scalar sec;
514 VERIFY_CHECK(ctx != NULL);
515 ARG_CHECK(pubkey != NULL);
516 memset(pubkey, 0, sizeof(*pubkey));
517 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
518 ARG_CHECK(seckey != NULL);
520 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
521 ret = !overflow && !secp256k1_scalar_is_zero(&sec);
523 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pj, &sec);
524 secp256k1_ge_set_gej(&p, &pj);
525 secp256k1_pubkey_save(pubkey, &p);
527 secp256k1_scalar_clear(&sec);
531 int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
532 secp256k1_scalar sec;
533 VERIFY_CHECK(ctx != NULL);
534 ARG_CHECK(seckey != NULL);
536 secp256k1_scalar_set_b32(&sec, seckey, NULL);
537 secp256k1_scalar_negate(&sec, &sec);
538 secp256k1_scalar_get_b32(seckey, &sec);
540 secp256k1_scalar_clear(&sec);
544 int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *pubkey) {
547 VERIFY_CHECK(ctx != NULL);
548 ARG_CHECK(pubkey != NULL);
550 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
551 memset(pubkey, 0, sizeof(*pubkey));
553 secp256k1_ge_neg(&p, &p);
554 secp256k1_pubkey_save(pubkey, &p);
559 int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
560 secp256k1_scalar term;
561 secp256k1_scalar sec;
564 VERIFY_CHECK(ctx != NULL);
565 ARG_CHECK(seckey != NULL);
566 ARG_CHECK(tweak != NULL);
568 secp256k1_scalar_set_b32(&term, tweak, &overflow);
569 secp256k1_scalar_set_b32(&sec, seckey, NULL);
571 ret = !overflow && secp256k1_eckey_privkey_tweak_add(&sec, &term);
572 memset(seckey, 0, 32);
574 secp256k1_scalar_get_b32(seckey, &sec);
577 secp256k1_scalar_clear(&sec);
578 secp256k1_scalar_clear(&term);
582 int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
584 secp256k1_scalar term;
587 VERIFY_CHECK(ctx != NULL);
588 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
589 ARG_CHECK(pubkey != NULL);
590 ARG_CHECK(tweak != NULL);
592 secp256k1_scalar_set_b32(&term, tweak, &overflow);
593 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
594 memset(pubkey, 0, sizeof(*pubkey));
596 if (secp256k1_eckey_pubkey_tweak_add(&ctx->ecmult_ctx, &p, &term)) {
597 secp256k1_pubkey_save(pubkey, &p);
606 int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
607 secp256k1_scalar factor;
608 secp256k1_scalar sec;
611 VERIFY_CHECK(ctx != NULL);
612 ARG_CHECK(seckey != NULL);
613 ARG_CHECK(tweak != NULL);
615 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
616 secp256k1_scalar_set_b32(&sec, seckey, NULL);
617 ret = !overflow && secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
618 memset(seckey, 0, 32);
620 secp256k1_scalar_get_b32(seckey, &sec);
623 secp256k1_scalar_clear(&sec);
624 secp256k1_scalar_clear(&factor);
628 int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
630 secp256k1_scalar factor;
633 VERIFY_CHECK(ctx != NULL);
634 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
635 ARG_CHECK(pubkey != NULL);
636 ARG_CHECK(tweak != NULL);
638 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
639 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
640 memset(pubkey, 0, sizeof(*pubkey));
642 if (secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor)) {
643 secp256k1_pubkey_save(pubkey, &p);
652 int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
653 VERIFY_CHECK(ctx != NULL);
654 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
655 secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
660 int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
665 ARG_CHECK(pubnonce != NULL);
666 memset(pubnonce, 0, sizeof(*pubnonce));
668 ARG_CHECK(pubnonces != NULL);
670 secp256k1_gej_set_infinity(&Qj);
672 for (i = 0; i < n; i++) {
673 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
674 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
676 if (secp256k1_gej_is_infinity(&Qj)) {
679 secp256k1_ge_set_gej(&Q, &Qj);
680 secp256k1_pubkey_save(pubnonce, &Q);
684 #ifdef ENABLE_MODULE_ECDH
685 # include "modules/ecdh/main_impl.h"
688 #ifdef ENABLE_MODULE_RECOVERY
689 # include "modules/recovery/main_impl.h"