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 static void default_illegal_callback_fn(const char* str, void* data) {
38 fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
42 static const secp256k1_callback default_illegal_callback = {
43 default_illegal_callback_fn,
47 static void default_error_callback_fn(const char* str, void* data) {
49 fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
53 static const secp256k1_callback default_error_callback = {
54 default_error_callback_fn,
59 struct secp256k1_context_struct {
60 secp256k1_ecmult_context ecmult_ctx;
61 secp256k1_ecmult_gen_context ecmult_gen_ctx;
62 secp256k1_callback illegal_callback;
63 secp256k1_callback error_callback;
66 static const secp256k1_context secp256k1_context_no_precomp_ = {
69 { default_illegal_callback_fn, 0 },
70 { default_error_callback_fn, 0 }
72 const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_;
74 size_t secp256k1_context_preallocated_size(unsigned int flags) {
75 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
77 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
78 secp256k1_callback_call(&default_illegal_callback,
83 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
84 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
86 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
87 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
92 size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) {
93 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
94 VERIFY_CHECK(ctx != NULL);
95 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
96 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
98 if (secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)) {
99 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
104 secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) {
105 void* const base = prealloc;
106 size_t prealloc_size;
107 secp256k1_context* ret;
109 VERIFY_CHECK(prealloc != NULL);
110 prealloc_size = secp256k1_context_preallocated_size(flags);
111 ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);
112 ret->illegal_callback = default_illegal_callback;
113 ret->error_callback = default_error_callback;
115 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
116 secp256k1_callback_call(&ret->illegal_callback,
121 secp256k1_ecmult_context_init(&ret->ecmult_ctx);
122 secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
124 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
125 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &prealloc);
127 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
128 secp256k1_ecmult_context_build(&ret->ecmult_ctx, &prealloc);
131 return (secp256k1_context*) ret;
134 secp256k1_context* secp256k1_context_create(unsigned int flags) {
135 size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
136 secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
137 if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
145 secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
146 size_t prealloc_size;
147 secp256k1_context* ret;
148 VERIFY_CHECK(ctx != NULL);
149 ARG_CHECK(prealloc != NULL);
151 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
152 ret = (secp256k1_context*)prealloc;
153 memcpy(ret, ctx, prealloc_size);
154 secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx);
155 secp256k1_ecmult_context_finalize_memcpy(&ret->ecmult_ctx, &ctx->ecmult_ctx);
159 secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
160 secp256k1_context* ret;
161 size_t prealloc_size;
163 VERIFY_CHECK(ctx != NULL);
164 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
165 ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
166 ret = secp256k1_context_preallocated_clone(ctx, ret);
170 void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
171 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
173 secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
174 secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
178 void secp256k1_context_destroy(secp256k1_context* ctx) {
180 secp256k1_context_preallocated_destroy(ctx);
185 void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
186 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
188 fun = default_illegal_callback_fn;
190 ctx->illegal_callback.fn = fun;
191 ctx->illegal_callback.data = data;
194 void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
195 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
197 fun = default_error_callback_fn;
199 ctx->error_callback.fn = fun;
200 ctx->error_callback.data = data;
203 secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
204 VERIFY_CHECK(ctx != NULL);
205 return secp256k1_scratch_create(&ctx->error_callback, max_size);
208 void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
209 VERIFY_CHECK(ctx != NULL);
210 secp256k1_scratch_destroy(&ctx->error_callback, scratch);
213 static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
214 if (sizeof(secp256k1_ge_storage) == 64) {
215 /* When the secp256k1_ge_storage type is exactly 64 byte, use its
216 * representation inside secp256k1_pubkey, as conversion is very fast.
217 * Note that secp256k1_pubkey_save must use the same representation. */
218 secp256k1_ge_storage s;
219 memcpy(&s, &pubkey->data[0], sizeof(s));
220 secp256k1_ge_from_storage(ge, &s);
222 /* Otherwise, fall back to 32-byte big endian for X and Y. */
224 secp256k1_fe_set_b32(&x, pubkey->data);
225 secp256k1_fe_set_b32(&y, pubkey->data + 32);
226 secp256k1_ge_set_xy(ge, &x, &y);
228 ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
232 static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
233 if (sizeof(secp256k1_ge_storage) == 64) {
234 secp256k1_ge_storage s;
235 secp256k1_ge_to_storage(&s, ge);
236 memcpy(&pubkey->data[0], &s, sizeof(s));
238 VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
239 secp256k1_fe_normalize_var(&ge->x);
240 secp256k1_fe_normalize_var(&ge->y);
241 secp256k1_fe_get_b32(pubkey->data, &ge->x);
242 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
246 int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
249 VERIFY_CHECK(ctx != NULL);
250 ARG_CHECK(pubkey != NULL);
251 memset(pubkey, 0, sizeof(*pubkey));
252 ARG_CHECK(input != NULL);
253 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
256 secp256k1_pubkey_save(pubkey, &Q);
257 secp256k1_ge_clear(&Q);
261 int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
266 VERIFY_CHECK(ctx != NULL);
267 ARG_CHECK(outputlen != NULL);
268 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33 : 65));
271 ARG_CHECK(output != NULL);
272 memset(output, 0, len);
273 ARG_CHECK(pubkey != NULL);
274 ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
275 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
276 ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
284 static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) {
286 if (sizeof(secp256k1_scalar) == 32) {
287 /* When the secp256k1_scalar type is exactly 32 byte, use its
288 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
289 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
290 memcpy(r, &sig->data[0], 32);
291 memcpy(s, &sig->data[32], 32);
293 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
294 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
298 static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) {
299 if (sizeof(secp256k1_scalar) == 32) {
300 memcpy(&sig->data[0], r, 32);
301 memcpy(&sig->data[32], s, 32);
303 secp256k1_scalar_get_b32(&sig->data[0], r);
304 secp256k1_scalar_get_b32(&sig->data[32], s);
308 int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
309 secp256k1_scalar r, s;
311 VERIFY_CHECK(ctx != NULL);
312 ARG_CHECK(sig != NULL);
313 ARG_CHECK(input != NULL);
315 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
316 secp256k1_ecdsa_signature_save(sig, &r, &s);
319 memset(sig, 0, sizeof(*sig));
324 int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input64) {
325 secp256k1_scalar r, s;
329 VERIFY_CHECK(ctx != NULL);
330 ARG_CHECK(sig != NULL);
331 ARG_CHECK(input64 != NULL);
333 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
335 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
338 secp256k1_ecdsa_signature_save(sig, &r, &s);
340 memset(sig, 0, sizeof(*sig));
345 int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
346 secp256k1_scalar r, s;
348 VERIFY_CHECK(ctx != NULL);
349 ARG_CHECK(output != NULL);
350 ARG_CHECK(outputlen != NULL);
351 ARG_CHECK(sig != NULL);
353 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
354 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
357 int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context* ctx, unsigned char *output64, const secp256k1_ecdsa_signature* sig) {
358 secp256k1_scalar r, s;
360 VERIFY_CHECK(ctx != NULL);
361 ARG_CHECK(output64 != NULL);
362 ARG_CHECK(sig != NULL);
364 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
365 secp256k1_scalar_get_b32(&output64[0], &r);
366 secp256k1_scalar_get_b32(&output64[32], &s);
370 int secp256k1_ecdsa_signature_normalize(const secp256k1_context* ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) {
371 secp256k1_scalar r, s;
374 VERIFY_CHECK(ctx != NULL);
375 ARG_CHECK(sigin != NULL);
377 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
378 ret = secp256k1_scalar_is_high(&s);
379 if (sigout != NULL) {
381 secp256k1_scalar_negate(&s, &s);
383 secp256k1_ecdsa_signature_save(sigout, &r, &s);
389 int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const secp256k1_pubkey *pubkey) {
391 secp256k1_scalar r, s;
393 VERIFY_CHECK(ctx != NULL);
394 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
395 ARG_CHECK(msg32 != NULL);
396 ARG_CHECK(sig != NULL);
397 ARG_CHECK(pubkey != NULL);
399 secp256k1_scalar_set_b32(&m, msg32, NULL);
400 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
401 return (!secp256k1_scalar_is_high(&s) &&
402 secp256k1_pubkey_load(ctx, &q, pubkey) &&
403 secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
406 static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
407 memcpy(buf + *offset, data, len);
411 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) {
412 unsigned char keydata[112];
413 unsigned int offset = 0;
414 secp256k1_rfc6979_hmac_sha256 rng;
416 /* We feed a byte array to the PRNG as input, consisting of:
417 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
418 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
419 * - optionally 16 extra bytes with the algorithm name.
420 * Because the arguments have distinct fixed lengths it is not possible for
421 * different argument mixtures to emulate each other and result in the same
424 buffer_append(keydata, &offset, key32, 32);
425 buffer_append(keydata, &offset, msg32, 32);
427 buffer_append(keydata, &offset, data, 32);
429 if (algo16 != NULL) {
430 buffer_append(keydata, &offset, algo16, 16);
432 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
433 memset(keydata, 0, sizeof(keydata));
434 for (i = 0; i <= counter; i++) {
435 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
437 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
441 const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
442 const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979;
444 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) {
445 secp256k1_scalar r, s;
446 secp256k1_scalar sec, non, msg;
449 VERIFY_CHECK(ctx != NULL);
450 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
451 ARG_CHECK(msg32 != NULL);
452 ARG_CHECK(signature != NULL);
453 ARG_CHECK(seckey != NULL);
454 if (noncefp == NULL) {
455 noncefp = secp256k1_nonce_function_default;
458 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
459 /* Fail if the secret key is invalid. */
460 if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
461 unsigned char nonce32[32];
462 unsigned int count = 0;
463 secp256k1_scalar_set_b32(&msg, msg32, NULL);
465 ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
469 secp256k1_scalar_set_b32(&non, nonce32, &overflow);
470 if (!overflow && !secp256k1_scalar_is_zero(&non)) {
471 if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) {
477 memset(nonce32, 0, 32);
478 secp256k1_scalar_clear(&msg);
479 secp256k1_scalar_clear(&non);
480 secp256k1_scalar_clear(&sec);
483 secp256k1_ecdsa_signature_save(signature, &r, &s);
485 memset(signature, 0, sizeof(*signature));
490 int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
491 secp256k1_scalar sec;
494 VERIFY_CHECK(ctx != NULL);
495 ARG_CHECK(seckey != NULL);
497 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
498 ret = !overflow && !secp256k1_scalar_is_zero(&sec);
499 secp256k1_scalar_clear(&sec);
503 int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
506 secp256k1_scalar sec;
509 VERIFY_CHECK(ctx != NULL);
510 ARG_CHECK(pubkey != NULL);
511 memset(pubkey, 0, sizeof(*pubkey));
512 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
513 ARG_CHECK(seckey != NULL);
515 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
516 ret = (!overflow) & (!secp256k1_scalar_is_zero(&sec));
518 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pj, &sec);
519 secp256k1_ge_set_gej(&p, &pj);
520 secp256k1_pubkey_save(pubkey, &p);
522 secp256k1_scalar_clear(&sec);
526 int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
527 secp256k1_scalar sec;
528 VERIFY_CHECK(ctx != NULL);
529 ARG_CHECK(seckey != NULL);
531 secp256k1_scalar_set_b32(&sec, seckey, NULL);
532 secp256k1_scalar_negate(&sec, &sec);
533 secp256k1_scalar_get_b32(seckey, &sec);
535 secp256k1_scalar_clear(&sec);
539 int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *pubkey) {
542 VERIFY_CHECK(ctx != NULL);
543 ARG_CHECK(pubkey != NULL);
545 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
546 memset(pubkey, 0, sizeof(*pubkey));
548 secp256k1_ge_neg(&p, &p);
549 secp256k1_pubkey_save(pubkey, &p);
554 int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
555 secp256k1_scalar term;
556 secp256k1_scalar sec;
559 VERIFY_CHECK(ctx != NULL);
560 ARG_CHECK(seckey != NULL);
561 ARG_CHECK(tweak != NULL);
563 secp256k1_scalar_set_b32(&term, tweak, &overflow);
564 secp256k1_scalar_set_b32(&sec, seckey, NULL);
566 ret = !overflow && secp256k1_eckey_privkey_tweak_add(&sec, &term);
567 memset(seckey, 0, 32);
569 secp256k1_scalar_get_b32(seckey, &sec);
572 secp256k1_scalar_clear(&sec);
573 secp256k1_scalar_clear(&term);
577 int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
579 secp256k1_scalar term;
582 VERIFY_CHECK(ctx != NULL);
583 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
584 ARG_CHECK(pubkey != NULL);
585 ARG_CHECK(tweak != NULL);
587 secp256k1_scalar_set_b32(&term, tweak, &overflow);
588 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
589 memset(pubkey, 0, sizeof(*pubkey));
591 if (secp256k1_eckey_pubkey_tweak_add(&ctx->ecmult_ctx, &p, &term)) {
592 secp256k1_pubkey_save(pubkey, &p);
601 int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
602 secp256k1_scalar factor;
603 secp256k1_scalar sec;
606 VERIFY_CHECK(ctx != NULL);
607 ARG_CHECK(seckey != NULL);
608 ARG_CHECK(tweak != NULL);
610 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
611 secp256k1_scalar_set_b32(&sec, seckey, NULL);
612 ret = !overflow && secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
613 memset(seckey, 0, 32);
615 secp256k1_scalar_get_b32(seckey, &sec);
618 secp256k1_scalar_clear(&sec);
619 secp256k1_scalar_clear(&factor);
623 int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
625 secp256k1_scalar factor;
628 VERIFY_CHECK(ctx != NULL);
629 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
630 ARG_CHECK(pubkey != NULL);
631 ARG_CHECK(tweak != NULL);
633 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
634 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
635 memset(pubkey, 0, sizeof(*pubkey));
637 if (secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor)) {
638 secp256k1_pubkey_save(pubkey, &p);
647 int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
648 VERIFY_CHECK(ctx != NULL);
649 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
650 secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
655 int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
660 ARG_CHECK(pubnonce != NULL);
661 memset(pubnonce, 0, sizeof(*pubnonce));
663 ARG_CHECK(pubnonces != NULL);
665 secp256k1_gej_set_infinity(&Qj);
667 for (i = 0; i < n; i++) {
668 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
669 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
671 if (secp256k1_gej_is_infinity(&Qj)) {
674 secp256k1_ge_set_gej(&Q, &Qj);
675 secp256k1_pubkey_save(pubnonce, &Q);
679 #ifdef ENABLE_MODULE_ECDH
680 # include "modules/ecdh/main_impl.h"
683 #ifdef ENABLE_MODULE_RECOVERY
684 # include "modules/recovery/main_impl.h"