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));
89 /* A return value of 0 is reserved as an indicator for errors when we call this function internally. */
90 VERIFY_CHECK(ret != 0);
92 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
93 secp256k1_callback_call(&default_illegal_callback,
98 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
99 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
101 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
102 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
107 size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) {
108 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
109 VERIFY_CHECK(ctx != NULL);
110 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
111 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
113 if (secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)) {
114 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
119 secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) {
120 void* const base = prealloc;
121 size_t prealloc_size;
122 secp256k1_context* ret;
124 if (!secp256k1_selftest()) {
125 secp256k1_callback_call(&default_error_callback, "self test failed");
128 prealloc_size = secp256k1_context_preallocated_size(flags);
129 if (prealloc_size == 0) {
132 VERIFY_CHECK(prealloc != NULL);
133 ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);
134 ret->illegal_callback = default_illegal_callback;
135 ret->error_callback = default_error_callback;
137 secp256k1_ecmult_context_init(&ret->ecmult_ctx);
138 secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
140 /* Flags have been checked by secp256k1_context_preallocated_size. */
141 VERIFY_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_CONTEXT);
142 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
143 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &prealloc);
145 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
146 secp256k1_ecmult_context_build(&ret->ecmult_ctx, &prealloc);
148 ret->declassify = !!(flags & SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY);
150 return (secp256k1_context*) ret;
153 secp256k1_context* secp256k1_context_create(unsigned int flags) {
154 size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
155 secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
156 if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
164 secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
165 size_t prealloc_size;
166 secp256k1_context* ret;
167 VERIFY_CHECK(ctx != NULL);
168 ARG_CHECK(prealloc != NULL);
170 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
171 ret = (secp256k1_context*)prealloc;
172 memcpy(ret, ctx, prealloc_size);
173 secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx);
174 secp256k1_ecmult_context_finalize_memcpy(&ret->ecmult_ctx, &ctx->ecmult_ctx);
178 secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
179 secp256k1_context* ret;
180 size_t prealloc_size;
182 VERIFY_CHECK(ctx != NULL);
183 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
184 ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
185 ret = secp256k1_context_preallocated_clone(ctx, ret);
189 void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
190 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
192 secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
193 secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
197 void secp256k1_context_destroy(secp256k1_context* ctx) {
199 secp256k1_context_preallocated_destroy(ctx);
204 void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
205 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
207 fun = secp256k1_default_illegal_callback_fn;
209 ctx->illegal_callback.fn = fun;
210 ctx->illegal_callback.data = data;
213 void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
214 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
216 fun = secp256k1_default_error_callback_fn;
218 ctx->error_callback.fn = fun;
219 ctx->error_callback.data = data;
222 secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
223 VERIFY_CHECK(ctx != NULL);
224 return secp256k1_scratch_create(&ctx->error_callback, max_size);
227 void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
228 VERIFY_CHECK(ctx != NULL);
229 secp256k1_scratch_destroy(&ctx->error_callback, scratch);
232 /* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
233 * of the software. This is setup for use with valgrind but could be substituted with
234 * the appropriate instrumentation for other analysis tools.
236 static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context* ctx, const void *p, size_t len) {
237 #if defined(VALGRIND)
238 if (EXPECT(ctx->declassify,0)) VALGRIND_MAKE_MEM_DEFINED(p, len);
246 static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
247 if (sizeof(secp256k1_ge_storage) == 64) {
248 /* When the secp256k1_ge_storage type is exactly 64 byte, use its
249 * representation inside secp256k1_pubkey, as conversion is very fast.
250 * Note that secp256k1_pubkey_save must use the same representation. */
251 secp256k1_ge_storage s;
252 memcpy(&s, &pubkey->data[0], sizeof(s));
253 secp256k1_ge_from_storage(ge, &s);
255 /* Otherwise, fall back to 32-byte big endian for X and Y. */
257 secp256k1_fe_set_b32(&x, pubkey->data);
258 secp256k1_fe_set_b32(&y, pubkey->data + 32);
259 secp256k1_ge_set_xy(ge, &x, &y);
261 ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
265 static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
266 if (sizeof(secp256k1_ge_storage) == 64) {
267 secp256k1_ge_storage s;
268 secp256k1_ge_to_storage(&s, ge);
269 memcpy(&pubkey->data[0], &s, sizeof(s));
271 VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
272 secp256k1_fe_normalize_var(&ge->x);
273 secp256k1_fe_normalize_var(&ge->y);
274 secp256k1_fe_get_b32(pubkey->data, &ge->x);
275 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
279 int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
282 VERIFY_CHECK(ctx != NULL);
283 ARG_CHECK(pubkey != NULL);
284 memset(pubkey, 0, sizeof(*pubkey));
285 ARG_CHECK(input != NULL);
286 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
289 if (!secp256k1_ge_is_in_correct_subgroup(&Q)) {
292 secp256k1_pubkey_save(pubkey, &Q);
293 secp256k1_ge_clear(&Q);
297 int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
302 VERIFY_CHECK(ctx != NULL);
303 ARG_CHECK(outputlen != NULL);
304 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33u : 65u));
307 ARG_CHECK(output != NULL);
308 memset(output, 0, len);
309 ARG_CHECK(pubkey != NULL);
310 ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
311 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
312 ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
320 static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) {
322 if (sizeof(secp256k1_scalar) == 32) {
323 /* When the secp256k1_scalar type is exactly 32 byte, use its
324 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
325 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
326 memcpy(r, &sig->data[0], 32);
327 memcpy(s, &sig->data[32], 32);
329 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
330 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
334 static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) {
335 if (sizeof(secp256k1_scalar) == 32) {
336 memcpy(&sig->data[0], r, 32);
337 memcpy(&sig->data[32], s, 32);
339 secp256k1_scalar_get_b32(&sig->data[0], r);
340 secp256k1_scalar_get_b32(&sig->data[32], s);
344 int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
345 secp256k1_scalar r, s;
347 VERIFY_CHECK(ctx != NULL);
348 ARG_CHECK(sig != NULL);
349 ARG_CHECK(input != NULL);
351 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
352 secp256k1_ecdsa_signature_save(sig, &r, &s);
355 memset(sig, 0, sizeof(*sig));
360 int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input64) {
361 secp256k1_scalar r, s;
365 VERIFY_CHECK(ctx != NULL);
366 ARG_CHECK(sig != NULL);
367 ARG_CHECK(input64 != NULL);
369 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
371 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
374 secp256k1_ecdsa_signature_save(sig, &r, &s);
376 memset(sig, 0, sizeof(*sig));
381 int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
382 secp256k1_scalar r, s;
384 VERIFY_CHECK(ctx != NULL);
385 ARG_CHECK(output != NULL);
386 ARG_CHECK(outputlen != NULL);
387 ARG_CHECK(sig != NULL);
389 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
390 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
393 int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context* ctx, unsigned char *output64, const secp256k1_ecdsa_signature* sig) {
394 secp256k1_scalar r, s;
396 VERIFY_CHECK(ctx != NULL);
397 ARG_CHECK(output64 != NULL);
398 ARG_CHECK(sig != NULL);
400 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
401 secp256k1_scalar_get_b32(&output64[0], &r);
402 secp256k1_scalar_get_b32(&output64[32], &s);
406 int secp256k1_ecdsa_signature_normalize(const secp256k1_context* ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) {
407 secp256k1_scalar r, s;
410 VERIFY_CHECK(ctx != NULL);
411 ARG_CHECK(sigin != NULL);
413 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
414 ret = secp256k1_scalar_is_high(&s);
415 if (sigout != NULL) {
417 secp256k1_scalar_negate(&s, &s);
419 secp256k1_ecdsa_signature_save(sigout, &r, &s);
425 int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) {
427 secp256k1_scalar r, s;
429 VERIFY_CHECK(ctx != NULL);
430 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
431 ARG_CHECK(msghash32 != NULL);
432 ARG_CHECK(sig != NULL);
433 ARG_CHECK(pubkey != NULL);
435 secp256k1_scalar_set_b32(&m, msghash32, NULL);
436 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
437 return (!secp256k1_scalar_is_high(&s) &&
438 secp256k1_pubkey_load(ctx, &q, pubkey) &&
439 secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
442 static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
443 memcpy(buf + *offset, data, len);
447 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) {
448 unsigned char keydata[112];
449 unsigned int offset = 0;
450 secp256k1_rfc6979_hmac_sha256 rng;
452 /* We feed a byte array to the PRNG as input, consisting of:
453 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
454 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
455 * - optionally 16 extra bytes with the algorithm name.
456 * Because the arguments have distinct fixed lengths it is not possible for
457 * different argument mixtures to emulate each other and result in the same
460 buffer_append(keydata, &offset, key32, 32);
461 buffer_append(keydata, &offset, msg32, 32);
463 buffer_append(keydata, &offset, data, 32);
465 if (algo16 != NULL) {
466 buffer_append(keydata, &offset, algo16, 16);
468 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
469 memset(keydata, 0, sizeof(keydata));
470 for (i = 0; i <= counter; i++) {
471 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
473 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
477 const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
478 const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979;
480 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) {
481 secp256k1_scalar sec, non, msg;
484 unsigned char nonce32[32];
485 unsigned int count = 0;
486 /* Default initialization here is important so we won't pass uninit values to the cmov in the end */
487 *r = secp256k1_scalar_zero;
488 *s = secp256k1_scalar_zero;
492 if (noncefp == NULL) {
493 noncefp = secp256k1_nonce_function_default;
496 /* Fail if the secret key is invalid. */
497 is_sec_valid = secp256k1_scalar_set_b32_seckey(&sec, seckey);
498 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_one, !is_sec_valid);
499 secp256k1_scalar_set_b32(&msg, msg32, NULL);
502 ret = !!noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
506 is_nonce_valid = secp256k1_scalar_set_b32_seckey(&non, nonce32);
507 /* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */
508 secp256k1_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid));
509 if (is_nonce_valid) {
510 ret = secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid);
511 /* The final signature is no longer a secret, nor is the fact that we were successful or not. */
512 secp256k1_declassify(ctx, &ret, sizeof(ret));
519 /* We don't want to declassify is_sec_valid and therefore the range of
520 * seckey. As a result is_sec_valid is included in ret only after ret was
521 * used as a branching variable. */
523 memset(nonce32, 0, 32);
524 secp256k1_scalar_clear(&msg);
525 secp256k1_scalar_clear(&non);
526 secp256k1_scalar_clear(&sec);
527 secp256k1_scalar_cmov(r, &secp256k1_scalar_zero, !ret);
528 secp256k1_scalar_cmov(s, &secp256k1_scalar_zero, !ret);
531 secp256k1_int_cmov(recid, &zero, !ret);
536 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) {
537 secp256k1_scalar r, s;
539 VERIFY_CHECK(ctx != NULL);
540 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
541 ARG_CHECK(msghash32 != NULL);
542 ARG_CHECK(signature != NULL);
543 ARG_CHECK(seckey != NULL);
545 ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msghash32, seckey, noncefp, noncedata);
546 secp256k1_ecdsa_signature_save(signature, &r, &s);
550 int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
551 secp256k1_scalar sec;
553 VERIFY_CHECK(ctx != NULL);
554 ARG_CHECK(seckey != NULL);
556 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
557 secp256k1_scalar_clear(&sec);
561 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) {
565 ret = secp256k1_scalar_set_b32_seckey(seckey_scalar, seckey);
566 secp256k1_scalar_cmov(seckey_scalar, &secp256k1_scalar_one, !ret);
568 secp256k1_ecmult_gen(ecmult_gen_ctx, &pj, seckey_scalar);
569 secp256k1_ge_set_gej(p, &pj);
573 int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
575 secp256k1_scalar seckey_scalar;
577 VERIFY_CHECK(ctx != NULL);
578 ARG_CHECK(pubkey != NULL);
579 memset(pubkey, 0, sizeof(*pubkey));
580 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
581 ARG_CHECK(seckey != NULL);
583 ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey);
584 secp256k1_pubkey_save(pubkey, &p);
585 secp256k1_memczero(pubkey, sizeof(*pubkey), !ret);
587 secp256k1_scalar_clear(&seckey_scalar);
591 int secp256k1_ec_seckey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
592 secp256k1_scalar sec;
594 VERIFY_CHECK(ctx != NULL);
595 ARG_CHECK(seckey != NULL);
597 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
598 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret);
599 secp256k1_scalar_negate(&sec, &sec);
600 secp256k1_scalar_get_b32(seckey, &sec);
602 secp256k1_scalar_clear(&sec);
606 int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
607 return secp256k1_ec_seckey_negate(ctx, seckey);
610 int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *pubkey) {
613 VERIFY_CHECK(ctx != NULL);
614 ARG_CHECK(pubkey != NULL);
616 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
617 memset(pubkey, 0, sizeof(*pubkey));
619 secp256k1_ge_neg(&p, &p);
620 secp256k1_pubkey_save(pubkey, &p);
626 static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak) {
627 secp256k1_scalar term;
631 secp256k1_scalar_set_b32(&term, tweak, &overflow);
632 ret = (!overflow) & secp256k1_eckey_privkey_tweak_add(sec, &term);
633 secp256k1_scalar_clear(&term);
637 int secp256k1_ec_seckey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
638 secp256k1_scalar sec;
640 VERIFY_CHECK(ctx != NULL);
641 ARG_CHECK(seckey != NULL);
642 ARG_CHECK(tweak != NULL);
644 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
645 ret &= secp256k1_ec_seckey_tweak_add_helper(&sec, tweak);
646 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret);
647 secp256k1_scalar_get_b32(seckey, &sec);
649 secp256k1_scalar_clear(&sec);
653 int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
654 return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak);
657 static int secp256k1_ec_pubkey_tweak_add_helper(const secp256k1_ecmult_context* ecmult_ctx, secp256k1_ge *p, const unsigned char *tweak) {
658 secp256k1_scalar term;
660 secp256k1_scalar_set_b32(&term, tweak, &overflow);
661 return !overflow && secp256k1_eckey_pubkey_tweak_add(ecmult_ctx, p, &term);
664 int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
667 VERIFY_CHECK(ctx != NULL);
668 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
669 ARG_CHECK(pubkey != NULL);
670 ARG_CHECK(tweak != NULL);
672 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
673 memset(pubkey, 0, sizeof(*pubkey));
674 ret = ret && secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &p, tweak);
676 secp256k1_pubkey_save(pubkey, &p);
682 int secp256k1_ec_seckey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
683 secp256k1_scalar factor;
684 secp256k1_scalar sec;
687 VERIFY_CHECK(ctx != NULL);
688 ARG_CHECK(seckey != NULL);
689 ARG_CHECK(tweak != NULL);
691 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
692 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
693 ret &= (!overflow) & secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
694 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret);
695 secp256k1_scalar_get_b32(seckey, &sec);
697 secp256k1_scalar_clear(&sec);
698 secp256k1_scalar_clear(&factor);
702 int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
703 return secp256k1_ec_seckey_tweak_mul(ctx, seckey, tweak);
706 int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
708 secp256k1_scalar factor;
711 VERIFY_CHECK(ctx != NULL);
712 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
713 ARG_CHECK(pubkey != NULL);
714 ARG_CHECK(tweak != NULL);
716 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
717 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
718 memset(pubkey, 0, sizeof(*pubkey));
720 if (secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor)) {
721 secp256k1_pubkey_save(pubkey, &p);
730 int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
731 VERIFY_CHECK(ctx != NULL);
732 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
733 secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
738 int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
743 ARG_CHECK(pubnonce != NULL);
744 memset(pubnonce, 0, sizeof(*pubnonce));
746 ARG_CHECK(pubnonces != NULL);
748 secp256k1_gej_set_infinity(&Qj);
750 for (i = 0; i < n; i++) {
751 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
752 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
754 if (secp256k1_gej_is_infinity(&Qj)) {
757 secp256k1_ge_set_gej(&Q, &Qj);
758 secp256k1_pubkey_save(pubnonce, &Q);
762 #ifdef ENABLE_MODULE_ECDH
763 # include "modules/ecdh/main_impl.h"
766 #ifdef ENABLE_MODULE_RECOVERY
767 # include "modules/recovery/main_impl.h"
770 #ifdef ENABLE_MODULE_EXTRAKEYS
771 # include "modules/extrakeys/main_impl.h"
774 #ifdef ENABLE_MODULE_SCHNORRSIG
775 # include "modules/schnorrsig/main_impl.h"