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"
11 #include "field_impl.h"
12 #include "scalar_impl.h"
13 #include "group_impl.h"
14 #include "ecmult_impl.h"
15 #include "ecmult_const_impl.h"
16 #include "ecmult_gen_impl.h"
17 #include "ecdsa_impl.h"
18 #include "eckey_impl.h"
19 #include "hash_impl.h"
21 #define ARG_CHECK(cond) do { \
22 if (EXPECT(!(cond), 0)) { \
23 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
28 static void default_illegal_callback_fn(const char* str, void* data) {
30 fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
34 static const secp256k1_callback default_illegal_callback = {
35 default_illegal_callback_fn,
39 static void default_error_callback_fn(const char* str, void* data) {
41 fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
45 static const secp256k1_callback default_error_callback = {
46 default_error_callback_fn,
51 struct secp256k1_context_struct {
52 secp256k1_ecmult_context ecmult_ctx;
53 secp256k1_ecmult_gen_context ecmult_gen_ctx;
54 secp256k1_callback illegal_callback;
55 secp256k1_callback error_callback;
58 secp256k1_context* secp256k1_context_create(unsigned int flags) {
59 secp256k1_context* ret = (secp256k1_context*)checked_malloc(&default_error_callback, sizeof(secp256k1_context));
60 ret->illegal_callback = default_illegal_callback;
61 ret->error_callback = default_error_callback;
63 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
64 secp256k1_callback_call(&ret->illegal_callback,
70 secp256k1_ecmult_context_init(&ret->ecmult_ctx);
71 secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
73 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
74 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &ret->error_callback);
76 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
77 secp256k1_ecmult_context_build(&ret->ecmult_ctx, &ret->error_callback);
83 secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
84 secp256k1_context* ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, sizeof(secp256k1_context));
85 ret->illegal_callback = ctx->illegal_callback;
86 ret->error_callback = ctx->error_callback;
87 secp256k1_ecmult_context_clone(&ret->ecmult_ctx, &ctx->ecmult_ctx, &ctx->error_callback);
88 secp256k1_ecmult_gen_context_clone(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx, &ctx->error_callback);
92 void secp256k1_context_destroy(secp256k1_context* ctx) {
94 secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
95 secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
101 void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
103 fun = default_illegal_callback_fn;
105 ctx->illegal_callback.fn = fun;
106 ctx->illegal_callback.data = data;
109 void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
111 fun = default_error_callback_fn;
113 ctx->error_callback.fn = fun;
114 ctx->error_callback.data = data;
117 static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
118 if (sizeof(secp256k1_ge_storage) == 64) {
119 /* When the secp256k1_ge_storage type is exactly 64 byte, use its
120 * representation inside secp256k1_pubkey, as conversion is very fast.
121 * Note that secp256k1_pubkey_save must use the same representation. */
122 secp256k1_ge_storage s;
123 memcpy(&s, &pubkey->data[0], 64);
124 secp256k1_ge_from_storage(ge, &s);
126 /* Otherwise, fall back to 32-byte big endian for X and Y. */
128 secp256k1_fe_set_b32(&x, pubkey->data);
129 secp256k1_fe_set_b32(&y, pubkey->data + 32);
130 secp256k1_ge_set_xy(ge, &x, &y);
132 ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
136 static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
137 if (sizeof(secp256k1_ge_storage) == 64) {
138 secp256k1_ge_storage s;
139 secp256k1_ge_to_storage(&s, ge);
140 memcpy(&pubkey->data[0], &s, 64);
142 VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
143 secp256k1_fe_normalize_var(&ge->x);
144 secp256k1_fe_normalize_var(&ge->y);
145 secp256k1_fe_get_b32(pubkey->data, &ge->x);
146 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
150 int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
154 VERIFY_CHECK(ctx != NULL);
155 ARG_CHECK(pubkey != NULL);
156 memset(pubkey, 0, sizeof(*pubkey));
157 ARG_CHECK(input != NULL);
158 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
161 secp256k1_pubkey_save(pubkey, &Q);
162 secp256k1_ge_clear(&Q);
166 int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
172 VERIFY_CHECK(ctx != NULL);
173 ARG_CHECK(outputlen != NULL);
174 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33 : 65));
177 ARG_CHECK(output != NULL);
178 memset(output, 0, len);
179 ARG_CHECK(pubkey != NULL);
180 ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
181 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
182 ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
190 static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) {
192 if (sizeof(secp256k1_scalar) == 32) {
193 /* When the secp256k1_scalar type is exactly 32 byte, use its
194 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
195 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
196 memcpy(r, &sig->data[0], 32);
197 memcpy(s, &sig->data[32], 32);
199 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
200 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
204 static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) {
205 if (sizeof(secp256k1_scalar) == 32) {
206 memcpy(&sig->data[0], r, 32);
207 memcpy(&sig->data[32], s, 32);
209 secp256k1_scalar_get_b32(&sig->data[0], r);
210 secp256k1_scalar_get_b32(&sig->data[32], s);
214 int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
215 secp256k1_scalar r, s;
218 ARG_CHECK(sig != NULL);
219 ARG_CHECK(input != NULL);
221 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
222 secp256k1_ecdsa_signature_save(sig, &r, &s);
225 memset(sig, 0, sizeof(*sig));
230 int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input64) {
231 secp256k1_scalar r, s;
236 ARG_CHECK(sig != NULL);
237 ARG_CHECK(input64 != NULL);
239 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
241 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
244 secp256k1_ecdsa_signature_save(sig, &r, &s);
246 memset(sig, 0, sizeof(*sig));
251 int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
252 secp256k1_scalar r, s;
255 ARG_CHECK(output != NULL);
256 ARG_CHECK(outputlen != NULL);
257 ARG_CHECK(sig != NULL);
259 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
260 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
263 int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context* ctx, unsigned char *output64, const secp256k1_ecdsa_signature* sig) {
264 secp256k1_scalar r, s;
267 ARG_CHECK(output64 != NULL);
268 ARG_CHECK(sig != NULL);
270 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
271 secp256k1_scalar_get_b32(&output64[0], &r);
272 secp256k1_scalar_get_b32(&output64[32], &s);
276 int secp256k1_ecdsa_signature_normalize(const secp256k1_context* ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) {
277 secp256k1_scalar r, s;
280 VERIFY_CHECK(ctx != NULL);
281 ARG_CHECK(sigin != NULL);
283 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
284 ret = secp256k1_scalar_is_high(&s);
285 if (sigout != NULL) {
287 secp256k1_scalar_negate(&s, &s);
289 secp256k1_ecdsa_signature_save(sigout, &r, &s);
295 int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const secp256k1_pubkey *pubkey) {
297 secp256k1_scalar r, s;
299 VERIFY_CHECK(ctx != NULL);
300 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
301 ARG_CHECK(msg32 != NULL);
302 ARG_CHECK(sig != NULL);
303 ARG_CHECK(pubkey != NULL);
305 secp256k1_scalar_set_b32(&m, msg32, NULL);
306 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
307 return (!secp256k1_scalar_is_high(&s) &&
308 secp256k1_pubkey_load(ctx, &q, pubkey) &&
309 secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
312 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) {
313 unsigned char keydata[112];
315 secp256k1_rfc6979_hmac_sha256_t rng;
317 /* We feed a byte array to the PRNG as input, consisting of:
318 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
319 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
320 * - optionally 16 extra bytes with the algorithm name.
321 * Because the arguments have distinct fixed lengths it is not possible for
322 * different argument mixtures to emulate each other and result in the same
325 memcpy(keydata, key32, 32);
326 memcpy(keydata + 32, msg32, 32);
328 memcpy(keydata + 64, data, 32);
331 if (algo16 != NULL) {
332 memcpy(keydata + keylen, algo16, 16);
335 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, keylen);
336 memset(keydata, 0, sizeof(keydata));
337 for (i = 0; i <= counter; i++) {
338 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
340 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
344 const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
345 const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979;
347 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) {
348 secp256k1_scalar r, s;
349 secp256k1_scalar sec, non, msg;
352 VERIFY_CHECK(ctx != NULL);
353 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
354 ARG_CHECK(msg32 != NULL);
355 ARG_CHECK(signature != NULL);
356 ARG_CHECK(seckey != NULL);
357 if (noncefp == NULL) {
358 noncefp = secp256k1_nonce_function_default;
361 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
362 /* Fail if the secret key is invalid. */
363 if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
364 unsigned int count = 0;
365 secp256k1_scalar_set_b32(&msg, msg32, NULL);
367 unsigned char nonce32[32];
368 ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
372 secp256k1_scalar_set_b32(&non, nonce32, &overflow);
373 memset(nonce32, 0, 32);
374 if (!overflow && !secp256k1_scalar_is_zero(&non)) {
375 if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) {
381 secp256k1_scalar_clear(&msg);
382 secp256k1_scalar_clear(&non);
383 secp256k1_scalar_clear(&sec);
386 secp256k1_ecdsa_signature_save(signature, &r, &s);
388 memset(signature, 0, sizeof(*signature));
393 int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
394 secp256k1_scalar sec;
397 VERIFY_CHECK(ctx != NULL);
398 ARG_CHECK(seckey != NULL);
401 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
402 ret = !overflow && !secp256k1_scalar_is_zero(&sec);
403 secp256k1_scalar_clear(&sec);
407 int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
410 secp256k1_scalar sec;
413 VERIFY_CHECK(ctx != NULL);
414 ARG_CHECK(pubkey != NULL);
415 memset(pubkey, 0, sizeof(*pubkey));
416 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
417 ARG_CHECK(seckey != NULL);
419 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
420 ret = (!overflow) & (!secp256k1_scalar_is_zero(&sec));
422 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pj, &sec);
423 secp256k1_ge_set_gej(&p, &pj);
424 secp256k1_pubkey_save(pubkey, &p);
426 secp256k1_scalar_clear(&sec);
430 int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
431 secp256k1_scalar term;
432 secp256k1_scalar sec;
435 VERIFY_CHECK(ctx != NULL);
436 ARG_CHECK(seckey != NULL);
437 ARG_CHECK(tweak != NULL);
440 secp256k1_scalar_set_b32(&term, tweak, &overflow);
441 secp256k1_scalar_set_b32(&sec, seckey, NULL);
443 ret = !overflow && secp256k1_eckey_privkey_tweak_add(&sec, &term);
444 memset(seckey, 0, 32);
446 secp256k1_scalar_get_b32(seckey, &sec);
449 secp256k1_scalar_clear(&sec);
450 secp256k1_scalar_clear(&term);
454 int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
456 secp256k1_scalar term;
459 VERIFY_CHECK(ctx != NULL);
460 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
461 ARG_CHECK(pubkey != NULL);
462 ARG_CHECK(tweak != NULL);
464 secp256k1_scalar_set_b32(&term, tweak, &overflow);
465 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
466 memset(pubkey, 0, sizeof(*pubkey));
468 if (secp256k1_eckey_pubkey_tweak_add(&ctx->ecmult_ctx, &p, &term)) {
469 secp256k1_pubkey_save(pubkey, &p);
478 int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
479 secp256k1_scalar factor;
480 secp256k1_scalar sec;
483 VERIFY_CHECK(ctx != NULL);
484 ARG_CHECK(seckey != NULL);
485 ARG_CHECK(tweak != NULL);
488 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
489 secp256k1_scalar_set_b32(&sec, seckey, NULL);
490 ret = !overflow && secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
491 memset(seckey, 0, 32);
493 secp256k1_scalar_get_b32(seckey, &sec);
496 secp256k1_scalar_clear(&sec);
497 secp256k1_scalar_clear(&factor);
501 int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
503 secp256k1_scalar factor;
506 VERIFY_CHECK(ctx != NULL);
507 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
508 ARG_CHECK(pubkey != NULL);
509 ARG_CHECK(tweak != NULL);
511 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
512 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
513 memset(pubkey, 0, sizeof(*pubkey));
515 if (secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor)) {
516 secp256k1_pubkey_save(pubkey, &p);
525 int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
526 VERIFY_CHECK(ctx != NULL);
527 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
528 secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
532 int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
537 ARG_CHECK(pubnonce != NULL);
538 memset(pubnonce, 0, sizeof(*pubnonce));
540 ARG_CHECK(pubnonces != NULL);
542 secp256k1_gej_set_infinity(&Qj);
544 for (i = 0; i < n; i++) {
545 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
546 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
548 if (secp256k1_gej_is_infinity(&Qj)) {
551 secp256k1_ge_set_gej(&Q, &Qj);
552 secp256k1_pubkey_save(pubnonce, &Q);
556 #ifdef ENABLE_MODULE_ECDH
557 # include "modules/ecdh/main_impl.h"
560 #ifdef ENABLE_MODULE_SCHNORR
561 # include "modules/schnorr/main_impl.h"
564 #ifdef ENABLE_MODULE_RECOVERY
565 # include "modules/recovery/main_impl.h"