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"
25 # include <valgrind/memcheck.h>
28 #define ARG_CHECK(cond) do { \
29 if (EXPECT(!(cond), 0)) { \
30 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
35 #define ARG_CHECK_NO_RETURN(cond) do { \
36 if (EXPECT(!(cond), 0)) { \
37 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
41 #ifndef USE_EXTERNAL_DEFAULT_CALLBACKS
44 static void secp256k1_default_illegal_callback_fn(const char* str, void* data) {
46 fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
49 static void secp256k1_default_error_callback_fn(const char* str, void* data) {
51 fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
55 void secp256k1_default_illegal_callback_fn(const char* str, void* data);
56 void secp256k1_default_error_callback_fn(const char* str, void* data);
59 static const secp256k1_callback default_illegal_callback = {
60 secp256k1_default_illegal_callback_fn,
64 static const secp256k1_callback default_error_callback = {
65 secp256k1_default_error_callback_fn,
69 struct secp256k1_context_struct {
70 secp256k1_ecmult_context ecmult_ctx;
71 secp256k1_ecmult_gen_context ecmult_gen_ctx;
72 secp256k1_callback illegal_callback;
73 secp256k1_callback error_callback;
77 static const secp256k1_context secp256k1_context_no_precomp_ = {
80 { secp256k1_default_illegal_callback_fn, 0 },
81 { secp256k1_default_error_callback_fn, 0 },
84 const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_;
86 size_t secp256k1_context_preallocated_size(unsigned int flags) {
87 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
89 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
90 secp256k1_callback_call(&default_illegal_callback,
95 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
96 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
98 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
99 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
104 size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) {
105 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
106 VERIFY_CHECK(ctx != NULL);
107 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
108 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
110 if (secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)) {
111 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
116 secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) {
117 void* const base = prealloc;
118 size_t prealloc_size;
119 secp256k1_context* ret;
121 VERIFY_CHECK(prealloc != NULL);
122 prealloc_size = secp256k1_context_preallocated_size(flags);
123 ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);
124 ret->illegal_callback = default_illegal_callback;
125 ret->error_callback = default_error_callback;
127 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
128 secp256k1_callback_call(&ret->illegal_callback,
133 secp256k1_ecmult_context_init(&ret->ecmult_ctx);
134 secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
136 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
137 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &prealloc);
139 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
140 secp256k1_ecmult_context_build(&ret->ecmult_ctx, &prealloc);
142 ret->declassify = !!(flags & SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY);
144 return (secp256k1_context*) ret;
147 secp256k1_context* secp256k1_context_create(unsigned int flags) {
148 size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
149 secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
150 if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
158 secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
159 size_t prealloc_size;
160 secp256k1_context* ret;
161 VERIFY_CHECK(ctx != NULL);
162 ARG_CHECK(prealloc != NULL);
164 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
165 ret = (secp256k1_context*)prealloc;
166 memcpy(ret, ctx, prealloc_size);
167 secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx);
168 secp256k1_ecmult_context_finalize_memcpy(&ret->ecmult_ctx, &ctx->ecmult_ctx);
172 secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
173 secp256k1_context* ret;
174 size_t prealloc_size;
176 VERIFY_CHECK(ctx != NULL);
177 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
178 ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
179 ret = secp256k1_context_preallocated_clone(ctx, ret);
183 void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
184 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
186 secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
187 secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
191 void secp256k1_context_destroy(secp256k1_context* ctx) {
193 secp256k1_context_preallocated_destroy(ctx);
198 void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
199 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
201 fun = secp256k1_default_illegal_callback_fn;
203 ctx->illegal_callback.fn = fun;
204 ctx->illegal_callback.data = data;
207 void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
208 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
210 fun = secp256k1_default_error_callback_fn;
212 ctx->error_callback.fn = fun;
213 ctx->error_callback.data = data;
216 secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
217 VERIFY_CHECK(ctx != NULL);
218 return secp256k1_scratch_create(&ctx->error_callback, max_size);
221 void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
222 VERIFY_CHECK(ctx != NULL);
223 secp256k1_scratch_destroy(&ctx->error_callback, scratch);
226 /* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
227 * of the software. This is setup for use with valgrind but could be substituted with
228 * the appropriate instrumentation for other analysis tools.
230 static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context* ctx, const void *p, size_t len) {
231 #if defined(VALGRIND)
232 if (EXPECT(ctx->declassify,0)) VALGRIND_MAKE_MEM_DEFINED(p, len);
240 static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
241 if (sizeof(secp256k1_ge_storage) == 64) {
242 /* When the secp256k1_ge_storage type is exactly 64 byte, use its
243 * representation inside secp256k1_pubkey, as conversion is very fast.
244 * Note that secp256k1_pubkey_save must use the same representation. */
245 secp256k1_ge_storage s;
246 memcpy(&s, &pubkey->data[0], sizeof(s));
247 secp256k1_ge_from_storage(ge, &s);
249 /* Otherwise, fall back to 32-byte big endian for X and Y. */
251 secp256k1_fe_set_b32(&x, pubkey->data);
252 secp256k1_fe_set_b32(&y, pubkey->data + 32);
253 secp256k1_ge_set_xy(ge, &x, &y);
255 ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
259 static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
260 if (sizeof(secp256k1_ge_storage) == 64) {
261 secp256k1_ge_storage s;
262 secp256k1_ge_to_storage(&s, ge);
263 memcpy(&pubkey->data[0], &s, sizeof(s));
265 VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
266 secp256k1_fe_normalize_var(&ge->x);
267 secp256k1_fe_normalize_var(&ge->y);
268 secp256k1_fe_get_b32(pubkey->data, &ge->x);
269 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
273 int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
276 VERIFY_CHECK(ctx != NULL);
277 ARG_CHECK(pubkey != NULL);
278 memset(pubkey, 0, sizeof(*pubkey));
279 ARG_CHECK(input != NULL);
280 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
283 secp256k1_pubkey_save(pubkey, &Q);
284 secp256k1_ge_clear(&Q);
288 int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
293 VERIFY_CHECK(ctx != NULL);
294 ARG_CHECK(outputlen != NULL);
295 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33u : 65u));
298 ARG_CHECK(output != NULL);
299 memset(output, 0, len);
300 ARG_CHECK(pubkey != NULL);
301 ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
302 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
303 ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
311 static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) {
313 if (sizeof(secp256k1_scalar) == 32) {
314 /* When the secp256k1_scalar type is exactly 32 byte, use its
315 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
316 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
317 memcpy(r, &sig->data[0], 32);
318 memcpy(s, &sig->data[32], 32);
320 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
321 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
325 static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) {
326 if (sizeof(secp256k1_scalar) == 32) {
327 memcpy(&sig->data[0], r, 32);
328 memcpy(&sig->data[32], s, 32);
330 secp256k1_scalar_get_b32(&sig->data[0], r);
331 secp256k1_scalar_get_b32(&sig->data[32], s);
335 int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
336 secp256k1_scalar r, s;
338 VERIFY_CHECK(ctx != NULL);
339 ARG_CHECK(sig != NULL);
340 ARG_CHECK(input != NULL);
342 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
343 secp256k1_ecdsa_signature_save(sig, &r, &s);
346 memset(sig, 0, sizeof(*sig));
351 int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input64) {
352 secp256k1_scalar r, s;
356 VERIFY_CHECK(ctx != NULL);
357 ARG_CHECK(sig != NULL);
358 ARG_CHECK(input64 != NULL);
360 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
362 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
365 secp256k1_ecdsa_signature_save(sig, &r, &s);
367 memset(sig, 0, sizeof(*sig));
372 int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
373 secp256k1_scalar r, s;
375 VERIFY_CHECK(ctx != NULL);
376 ARG_CHECK(output != NULL);
377 ARG_CHECK(outputlen != NULL);
378 ARG_CHECK(sig != NULL);
380 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
381 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
384 int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context* ctx, unsigned char *output64, const secp256k1_ecdsa_signature* sig) {
385 secp256k1_scalar r, s;
387 VERIFY_CHECK(ctx != NULL);
388 ARG_CHECK(output64 != NULL);
389 ARG_CHECK(sig != NULL);
391 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
392 secp256k1_scalar_get_b32(&output64[0], &r);
393 secp256k1_scalar_get_b32(&output64[32], &s);
397 int secp256k1_ecdsa_signature_normalize(const secp256k1_context* ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) {
398 secp256k1_scalar r, s;
401 VERIFY_CHECK(ctx != NULL);
402 ARG_CHECK(sigin != NULL);
404 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
405 ret = secp256k1_scalar_is_high(&s);
406 if (sigout != NULL) {
408 secp256k1_scalar_negate(&s, &s);
410 secp256k1_ecdsa_signature_save(sigout, &r, &s);
416 int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const secp256k1_pubkey *pubkey) {
418 secp256k1_scalar r, s;
420 VERIFY_CHECK(ctx != NULL);
421 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
422 ARG_CHECK(msg32 != NULL);
423 ARG_CHECK(sig != NULL);
424 ARG_CHECK(pubkey != NULL);
426 secp256k1_scalar_set_b32(&m, msg32, NULL);
427 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
428 return (!secp256k1_scalar_is_high(&s) &&
429 secp256k1_pubkey_load(ctx, &q, pubkey) &&
430 secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
433 static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
434 memcpy(buf + *offset, data, len);
438 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) {
439 unsigned char keydata[112];
440 unsigned int offset = 0;
441 secp256k1_rfc6979_hmac_sha256 rng;
443 /* We feed a byte array to the PRNG as input, consisting of:
444 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
445 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
446 * - optionally 16 extra bytes with the algorithm name.
447 * Because the arguments have distinct fixed lengths it is not possible for
448 * different argument mixtures to emulate each other and result in the same
451 buffer_append(keydata, &offset, key32, 32);
452 buffer_append(keydata, &offset, msg32, 32);
454 buffer_append(keydata, &offset, data, 32);
456 if (algo16 != NULL) {
457 buffer_append(keydata, &offset, algo16, 16);
459 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
460 memset(keydata, 0, sizeof(keydata));
461 for (i = 0; i <= counter; i++) {
462 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
464 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
468 const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
469 const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979;
471 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) {
472 secp256k1_scalar sec, non, msg;
475 unsigned char nonce32[32];
476 unsigned int count = 0;
477 /* Default initialization here is important so we won't pass uninit values to the cmov in the end */
478 *r = secp256k1_scalar_zero;
479 *s = secp256k1_scalar_zero;
483 if (noncefp == NULL) {
484 noncefp = secp256k1_nonce_function_default;
487 /* Fail if the secret key is invalid. */
488 is_sec_valid = secp256k1_scalar_set_b32_seckey(&sec, seckey);
489 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_one, !is_sec_valid);
490 secp256k1_scalar_set_b32(&msg, msg32, NULL);
493 ret = !!noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
497 is_nonce_valid = secp256k1_scalar_set_b32_seckey(&non, nonce32);
498 /* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */
499 secp256k1_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid));
500 if (is_nonce_valid) {
501 ret = secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid);
502 /* The final signature is no longer a secret, nor is the fact that we were successful or not. */
503 secp256k1_declassify(ctx, &ret, sizeof(ret));
510 /* We don't want to declassify is_sec_valid and therefore the range of
511 * seckey. As a result is_sec_valid is included in ret only after ret was
512 * used as a branching variable. */
514 memset(nonce32, 0, 32);
515 secp256k1_scalar_clear(&msg);
516 secp256k1_scalar_clear(&non);
517 secp256k1_scalar_clear(&sec);
518 secp256k1_scalar_cmov(r, &secp256k1_scalar_zero, !ret);
519 secp256k1_scalar_cmov(s, &secp256k1_scalar_zero, !ret);
522 secp256k1_int_cmov(recid, &zero, !ret);
527 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) {
528 secp256k1_scalar r, s;
530 VERIFY_CHECK(ctx != NULL);
531 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
532 ARG_CHECK(msg32 != NULL);
533 ARG_CHECK(signature != NULL);
534 ARG_CHECK(seckey != NULL);
536 ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msg32, seckey, noncefp, noncedata);
537 secp256k1_ecdsa_signature_save(signature, &r, &s);
541 int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
542 secp256k1_scalar sec;
544 VERIFY_CHECK(ctx != NULL);
545 ARG_CHECK(seckey != NULL);
547 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
548 secp256k1_scalar_clear(&sec);
552 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) {
556 ret = secp256k1_scalar_set_b32_seckey(seckey_scalar, seckey);
557 secp256k1_scalar_cmov(seckey_scalar, &secp256k1_scalar_one, !ret);
559 secp256k1_ecmult_gen(ecmult_gen_ctx, &pj, seckey_scalar);
560 secp256k1_ge_set_gej(p, &pj);
564 int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
566 secp256k1_scalar seckey_scalar;
568 VERIFY_CHECK(ctx != NULL);
569 ARG_CHECK(pubkey != NULL);
570 memset(pubkey, 0, sizeof(*pubkey));
571 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
572 ARG_CHECK(seckey != NULL);
574 ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey);
575 secp256k1_pubkey_save(pubkey, &p);
576 memczero(pubkey, sizeof(*pubkey), !ret);
578 secp256k1_scalar_clear(&seckey_scalar);
582 int secp256k1_ec_seckey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
583 secp256k1_scalar sec;
585 VERIFY_CHECK(ctx != NULL);
586 ARG_CHECK(seckey != NULL);
588 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
589 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret);
590 secp256k1_scalar_negate(&sec, &sec);
591 secp256k1_scalar_get_b32(seckey, &sec);
593 secp256k1_scalar_clear(&sec);
597 int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
598 return secp256k1_ec_seckey_negate(ctx, seckey);
601 int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *pubkey) {
604 VERIFY_CHECK(ctx != NULL);
605 ARG_CHECK(pubkey != NULL);
607 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
608 memset(pubkey, 0, sizeof(*pubkey));
610 secp256k1_ge_neg(&p, &p);
611 secp256k1_pubkey_save(pubkey, &p);
617 static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak) {
618 secp256k1_scalar term;
622 secp256k1_scalar_set_b32(&term, tweak, &overflow);
623 ret = (!overflow) & secp256k1_eckey_privkey_tweak_add(sec, &term);
624 secp256k1_scalar_clear(&term);
628 int secp256k1_ec_seckey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
629 secp256k1_scalar sec;
631 VERIFY_CHECK(ctx != NULL);
632 ARG_CHECK(seckey != NULL);
633 ARG_CHECK(tweak != NULL);
635 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
636 ret &= secp256k1_ec_seckey_tweak_add_helper(&sec, tweak);
637 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret);
638 secp256k1_scalar_get_b32(seckey, &sec);
640 secp256k1_scalar_clear(&sec);
644 int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
645 return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak);
648 static int secp256k1_ec_pubkey_tweak_add_helper(const secp256k1_ecmult_context* ecmult_ctx, secp256k1_ge *p, const unsigned char *tweak) {
649 secp256k1_scalar term;
651 secp256k1_scalar_set_b32(&term, tweak, &overflow);
652 return !overflow && secp256k1_eckey_pubkey_tweak_add(ecmult_ctx, p, &term);
655 int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
658 VERIFY_CHECK(ctx != NULL);
659 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
660 ARG_CHECK(pubkey != NULL);
661 ARG_CHECK(tweak != NULL);
663 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
664 memset(pubkey, 0, sizeof(*pubkey));
665 ret = ret && secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &p, tweak);
667 secp256k1_pubkey_save(pubkey, &p);
673 int secp256k1_ec_seckey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
674 secp256k1_scalar factor;
675 secp256k1_scalar sec;
678 VERIFY_CHECK(ctx != NULL);
679 ARG_CHECK(seckey != NULL);
680 ARG_CHECK(tweak != NULL);
682 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
683 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
684 ret &= (!overflow) & secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
685 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret);
686 secp256k1_scalar_get_b32(seckey, &sec);
688 secp256k1_scalar_clear(&sec);
689 secp256k1_scalar_clear(&factor);
693 int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
694 return secp256k1_ec_seckey_tweak_mul(ctx, seckey, tweak);
697 int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
699 secp256k1_scalar factor;
702 VERIFY_CHECK(ctx != NULL);
703 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
704 ARG_CHECK(pubkey != NULL);
705 ARG_CHECK(tweak != NULL);
707 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
708 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
709 memset(pubkey, 0, sizeof(*pubkey));
711 if (secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor)) {
712 secp256k1_pubkey_save(pubkey, &p);
721 int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
722 VERIFY_CHECK(ctx != NULL);
723 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
724 secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
729 int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
734 ARG_CHECK(pubnonce != NULL);
735 memset(pubnonce, 0, sizeof(*pubnonce));
737 ARG_CHECK(pubnonces != NULL);
739 secp256k1_gej_set_infinity(&Qj);
741 for (i = 0; i < n; i++) {
742 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
743 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
745 if (secp256k1_gej_is_infinity(&Qj)) {
748 secp256k1_ge_set_gej(&Q, &Qj);
749 secp256k1_pubkey_save(pubnonce, &Q);
753 #ifdef ENABLE_MODULE_ECDH
754 # include "modules/ecdh/main_impl.h"
757 #ifdef ENABLE_MODULE_RECOVERY
758 # include "modules/recovery/main_impl.h"
761 #ifdef ENABLE_MODULE_EXTRAKEYS
762 # include "modules/extrakeys/main_impl.h"
765 #ifdef ENABLE_MODULE_SCHNORRSIG
766 # include "modules/schnorrsig/main_impl.h"