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 #define SECP256K1_BUILD (1)
9 #include "include/secp256k1.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"
23 #define ARG_CHECK(cond) do { \
24 if (EXPECT(!(cond), 0)) { \
25 ctx->illegal_callback.fn(#cond, ctx->illegal_callback.data); \
30 static void default_illegal_callback_fn(const char* str, void* data) {
32 fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
36 static const callback_t default_illegal_callback = {
37 default_illegal_callback_fn,
41 static void default_error_callback_fn(const char* str, void* data) {
43 fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
47 static const callback_t default_error_callback = {
48 default_error_callback_fn,
53 struct secp256k1_context_struct {
54 secp256k1_ecmult_context_t ecmult_ctx;
55 secp256k1_ecmult_gen_context_t ecmult_gen_ctx;
56 callback_t illegal_callback;
57 callback_t error_callback;
60 secp256k1_context_t* secp256k1_context_create(int flags) {
61 secp256k1_context_t* ret = (secp256k1_context_t*)checked_malloc(&default_error_callback, sizeof(secp256k1_context_t));
62 ret->illegal_callback = default_illegal_callback;
63 ret->error_callback = default_error_callback;
65 secp256k1_ecmult_context_init(&ret->ecmult_ctx);
66 secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
68 if (flags & SECP256K1_CONTEXT_SIGN) {
69 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &ret->error_callback);
71 if (flags & SECP256K1_CONTEXT_VERIFY) {
72 secp256k1_ecmult_context_build(&ret->ecmult_ctx, &ret->error_callback);
78 secp256k1_context_t* secp256k1_context_clone(const secp256k1_context_t* ctx) {
79 secp256k1_context_t* ret = (secp256k1_context_t*)checked_malloc(&ctx->error_callback, sizeof(secp256k1_context_t));
80 ret->illegal_callback = ctx->illegal_callback;
81 ret->error_callback = ctx->error_callback;
82 secp256k1_ecmult_context_clone(&ret->ecmult_ctx, &ctx->ecmult_ctx, &ctx->error_callback);
83 secp256k1_ecmult_gen_context_clone(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx, &ctx->error_callback);
87 void secp256k1_context_destroy(secp256k1_context_t* ctx) {
88 secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
89 secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
94 void secp256k1_context_set_illegal_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), void* data) {
95 ctx->illegal_callback.fn = fun;
96 ctx->illegal_callback.data = data;
99 void secp256k1_context_set_error_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), void* data) {
100 ctx->error_callback.fn = fun;
101 ctx->error_callback.data = data;
104 static int secp256k1_pubkey_load(const secp256k1_context_t* ctx, secp256k1_ge_t* ge, const secp256k1_pubkey_t* pubkey) {
105 if (sizeof(secp256k1_ge_storage_t) == 64) {
106 /* When the secp256k1_ge_storage_t type is exactly 64 byte, use its
107 * representation inside secp256k1_pubkey_t, as conversion is very fast.
108 * Note that secp256k1_pubkey_save must use the same representation. */
109 secp256k1_ge_storage_t s;
110 memcpy(&s, &pubkey->data[0], 64);
111 secp256k1_ge_from_storage(ge, &s);
113 /* Otherwise, fall back to 32-byte big endian for X and Y. */
115 secp256k1_fe_set_b32(&x, pubkey->data);
116 secp256k1_fe_set_b32(&y, pubkey->data + 32);
117 secp256k1_ge_set_xy(ge, &x, &y);
119 ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
123 static void secp256k1_pubkey_save(secp256k1_pubkey_t* pubkey, secp256k1_ge_t* ge) {
124 if (sizeof(secp256k1_ge_storage_t) == 64) {
125 secp256k1_ge_storage_t s;
126 secp256k1_ge_to_storage(&s, ge);
127 memcpy(&pubkey->data[0], &s, 64);
129 VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
130 secp256k1_fe_normalize_var(&ge->x);
131 secp256k1_fe_normalize_var(&ge->y);
132 secp256k1_fe_get_b32(pubkey->data, &ge->x);
133 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
137 int secp256k1_ec_pubkey_parse(const secp256k1_context_t* ctx, secp256k1_pubkey_t* pubkey, const unsigned char *input, int inputlen) {
141 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
142 memset(pubkey, 0, sizeof(*pubkey));
145 secp256k1_pubkey_save(pubkey, &Q);
146 secp256k1_ge_clear(&Q);
150 int secp256k1_ec_pubkey_serialize(const secp256k1_context_t* ctx, unsigned char *output, int *outputlen, const secp256k1_pubkey_t* pubkey, int compressed) {
154 return (secp256k1_pubkey_load(ctx, &Q, pubkey) &&
155 secp256k1_eckey_pubkey_serialize(&Q, output, outputlen, compressed));
158 static void secp256k1_ecdsa_signature_load(const secp256k1_context_t* ctx, secp256k1_scalar_t* r, secp256k1_scalar_t* s, int* recid, const secp256k1_ecdsa_signature_t* sig) {
160 if (sizeof(secp256k1_scalar_t) == 32) {
161 /* When the secp256k1_scalar_t type is exactly 32 byte, use its
162 * representation inside secp256k1_ecdsa_signature_t, as conversion is very fast.
163 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
164 memcpy(r, &sig->data[0], 32);
165 memcpy(s, &sig->data[32], 32);
167 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
168 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
171 *recid = sig->data[64];
175 static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature_t* sig, const secp256k1_scalar_t* r, const secp256k1_scalar_t* s, int recid) {
176 if (sizeof(secp256k1_scalar_t) == 32) {
177 memcpy(&sig->data[0], r, 32);
178 memcpy(&sig->data[32], s, 32);
180 secp256k1_scalar_get_b32(&sig->data[0], r);
181 secp256k1_scalar_get_b32(&sig->data[32], s);
183 sig->data[64] = recid;
186 int secp256k1_ecdsa_signature_parse_der(const secp256k1_context_t* ctx, secp256k1_ecdsa_signature_t* sig, const unsigned char *input, int inputlen) {
187 secp256k1_scalar_t r, s;
190 ARG_CHECK(sig != NULL);
191 ARG_CHECK(input != NULL);
193 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
194 secp256k1_ecdsa_signature_save(sig, &r, &s, -1);
197 memset(sig, 0, sizeof(*sig));
202 int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context_t* ctx, secp256k1_ecdsa_signature_t* sig, const unsigned char *input64, int recid) {
203 secp256k1_scalar_t r, s;
208 ARG_CHECK(sig != NULL);
209 ARG_CHECK(input64 != NULL);
211 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
213 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
215 ret &= (recid == -1 || (recid >= 0 && recid < 4));
217 secp256k1_ecdsa_signature_save(sig, &r, &s, recid);
219 memset(sig, 0, sizeof(*sig));
224 int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context_t* ctx, unsigned char *output, int *outputlen, const secp256k1_ecdsa_signature_t* sig) {
225 secp256k1_scalar_t r, s;
228 ARG_CHECK(output != NULL);
229 ARG_CHECK(outputlen != NULL);
230 ARG_CHECK(sig != NULL);
232 secp256k1_ecdsa_signature_load(ctx, &r, &s, NULL, sig);
233 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
236 int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context_t* ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_signature_t* sig) {
237 secp256k1_scalar_t r, s;
241 ARG_CHECK(output64 != NULL);
242 ARG_CHECK(sig != NULL);
244 secp256k1_ecdsa_signature_load(ctx, &r, &s, &rec, sig);
245 secp256k1_scalar_get_b32(&output64[0], &r);
246 secp256k1_scalar_get_b32(&output64[32], &s);
248 ARG_CHECK(rec >= 0 && rec < 4);
254 int secp256k1_ecdsa_verify(const secp256k1_context_t* ctx, const unsigned char *msg32, const secp256k1_ecdsa_signature_t *sig, const secp256k1_pubkey_t *pubkey) {
256 secp256k1_scalar_t r, s;
257 secp256k1_scalar_t m;
258 ARG_CHECK(ctx != NULL);
259 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
260 ARG_CHECK(msg32 != NULL);
261 ARG_CHECK(sig != NULL);
262 ARG_CHECK(pubkey != NULL);
264 secp256k1_scalar_set_b32(&m, msg32, NULL);
265 secp256k1_ecdsa_signature_load(ctx, &r, &s, NULL, sig);
266 return (secp256k1_pubkey_load(ctx, &q, pubkey) &&
267 secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
270 static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, unsigned int counter, const void *data) {
271 unsigned char keydata[96];
272 secp256k1_rfc6979_hmac_sha256_t rng;
274 /* We feed a byte array to the PRNG as input, consisting of:
275 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
276 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
278 memcpy(keydata, key32, 32);
279 memcpy(keydata + 32, msg32, 32);
281 memcpy(keydata + 64, data, 32);
283 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, data != NULL ? 96 : 64);
284 memset(keydata, 0, sizeof(keydata));
285 for (i = 0; i <= counter; i++) {
286 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
288 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
292 const secp256k1_nonce_function_t secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
293 const secp256k1_nonce_function_t secp256k1_nonce_function_default = nonce_function_rfc6979;
295 int secp256k1_ecdsa_sign(const secp256k1_context_t* ctx, const unsigned char *msg32, secp256k1_ecdsa_signature_t *signature, const unsigned char *seckey, secp256k1_nonce_function_t noncefp, const void* noncedata) {
296 secp256k1_scalar_t r, s;
297 secp256k1_scalar_t sec, non, msg;
301 unsigned int count = 0;
302 ARG_CHECK(ctx != NULL);
303 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
304 ARG_CHECK(msg32 != NULL);
305 ARG_CHECK(signature != NULL);
306 ARG_CHECK(seckey != NULL);
307 if (noncefp == NULL) {
308 noncefp = secp256k1_nonce_function_default;
311 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
312 /* Fail if the secret key is invalid. */
313 if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
314 secp256k1_scalar_set_b32(&msg, msg32, NULL);
316 unsigned char nonce32[32];
317 ret = noncefp(nonce32, msg32, seckey, count, noncedata);
321 secp256k1_scalar_set_b32(&non, nonce32, &overflow);
322 memset(nonce32, 0, 32);
323 if (!secp256k1_scalar_is_zero(&non) && !overflow) {
324 if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, &recid)) {
330 secp256k1_scalar_clear(&msg);
331 secp256k1_scalar_clear(&non);
332 secp256k1_scalar_clear(&sec);
335 secp256k1_ecdsa_signature_save(signature, &r, &s, recid);
337 memset(signature, 0, sizeof(*signature));
342 int secp256k1_ecdsa_recover(const secp256k1_context_t* ctx, const unsigned char *msg32, const secp256k1_ecdsa_signature_t *signature, secp256k1_pubkey_t *pubkey) {
344 secp256k1_scalar_t r, s;
345 secp256k1_scalar_t m;
347 ARG_CHECK(ctx != NULL);
348 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
349 ARG_CHECK(msg32 != NULL);
350 ARG_CHECK(signature != NULL);
351 ARG_CHECK(pubkey != NULL);
353 secp256k1_ecdsa_signature_load(ctx, &r, &s, &recid, signature);
354 ARG_CHECK(recid >= 0 && recid < 4);
355 secp256k1_scalar_set_b32(&m, msg32, NULL);
356 if (secp256k1_ecdsa_sig_recover(&ctx->ecmult_ctx, &r, &s, &q, &m, recid)) {
357 secp256k1_pubkey_save(pubkey, &q);
360 memset(pubkey, 0, sizeof(*pubkey));
365 int secp256k1_ec_seckey_verify(const secp256k1_context_t* ctx, const unsigned char *seckey) {
366 secp256k1_scalar_t sec;
369 ARG_CHECK(ctx != NULL);
370 ARG_CHECK(seckey != NULL);
373 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
374 ret = !secp256k1_scalar_is_zero(&sec) && !overflow;
375 secp256k1_scalar_clear(&sec);
379 int secp256k1_ec_pubkey_create(const secp256k1_context_t* ctx, secp256k1_pubkey_t *pubkey, const unsigned char *seckey) {
382 secp256k1_scalar_t sec;
385 ARG_CHECK(ctx != NULL);
386 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
387 ARG_CHECK(pubkey != NULL);
388 ARG_CHECK(seckey != NULL);
390 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
391 ret = !overflow & !secp256k1_scalar_is_zero(&sec);
392 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pj, &sec);
393 secp256k1_ge_set_gej(&p, &pj);
394 secp256k1_pubkey_save(pubkey, &p);
395 secp256k1_scalar_clear(&sec);
397 memset(pubkey, 0, sizeof(*pubkey));
402 int secp256k1_ec_privkey_tweak_add(const secp256k1_context_t* ctx, unsigned char *seckey, const unsigned char *tweak) {
403 secp256k1_scalar_t term;
404 secp256k1_scalar_t sec;
407 ARG_CHECK(ctx != NULL);
408 ARG_CHECK(seckey != NULL);
409 ARG_CHECK(tweak != NULL);
412 secp256k1_scalar_set_b32(&term, tweak, &overflow);
413 secp256k1_scalar_set_b32(&sec, seckey, NULL);
415 ret = secp256k1_eckey_privkey_tweak_add(&sec, &term) && !overflow;
417 secp256k1_scalar_get_b32(seckey, &sec);
420 secp256k1_scalar_clear(&sec);
421 secp256k1_scalar_clear(&term);
425 int secp256k1_ec_pubkey_tweak_add(const secp256k1_context_t* ctx, secp256k1_pubkey_t *pubkey, const unsigned char *tweak) {
427 secp256k1_scalar_t term;
430 ARG_CHECK(ctx != NULL);
431 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
432 ARG_CHECK(pubkey != NULL);
433 ARG_CHECK(tweak != NULL);
435 secp256k1_scalar_set_b32(&term, tweak, &overflow);
436 if (!overflow && secp256k1_pubkey_load(ctx, &p, pubkey)) {
437 ret = secp256k1_eckey_pubkey_tweak_add(&ctx->ecmult_ctx, &p, &term);
439 secp256k1_pubkey_save(pubkey, &p);
441 memset(pubkey, 0, sizeof(*pubkey));
448 int secp256k1_ec_privkey_tweak_mul(const secp256k1_context_t* ctx, unsigned char *seckey, const unsigned char *tweak) {
449 secp256k1_scalar_t factor;
450 secp256k1_scalar_t sec;
453 ARG_CHECK(ctx != NULL);
454 ARG_CHECK(seckey != NULL);
455 ARG_CHECK(tweak != NULL);
458 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
459 secp256k1_scalar_set_b32(&sec, seckey, NULL);
460 ret = secp256k1_eckey_privkey_tweak_mul(&sec, &factor) && !overflow;
462 secp256k1_scalar_get_b32(seckey, &sec);
465 secp256k1_scalar_clear(&sec);
466 secp256k1_scalar_clear(&factor);
470 int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context_t* ctx, secp256k1_pubkey_t *pubkey, const unsigned char *tweak) {
472 secp256k1_scalar_t factor;
475 ARG_CHECK(ctx != NULL);
476 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
477 ARG_CHECK(pubkey != NULL);
478 ARG_CHECK(tweak != NULL);
480 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
481 if (!overflow && secp256k1_pubkey_load(ctx, &p, pubkey)) {
482 ret = secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor);
484 secp256k1_pubkey_save(pubkey, &p);
486 memset(pubkey, 0, sizeof(*pubkey));
493 int secp256k1_ec_privkey_export(const secp256k1_context_t* ctx, const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed) {
494 secp256k1_scalar_t key;
496 ARG_CHECK(seckey != NULL);
497 ARG_CHECK(privkey != NULL);
498 ARG_CHECK(privkeylen != NULL);
499 ARG_CHECK(ctx != NULL);
500 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
502 secp256k1_scalar_set_b32(&key, seckey, NULL);
503 ret = secp256k1_eckey_privkey_serialize(&ctx->ecmult_gen_ctx, privkey, privkeylen, &key, compressed);
504 secp256k1_scalar_clear(&key);
508 int secp256k1_ec_privkey_import(const secp256k1_context_t* ctx, unsigned char *seckey, const unsigned char *privkey, int privkeylen) {
509 secp256k1_scalar_t key;
511 ARG_CHECK(seckey != NULL);
512 ARG_CHECK(privkey != NULL);
515 ret = secp256k1_eckey_privkey_parse(&key, privkey, privkeylen);
517 secp256k1_scalar_get_b32(seckey, &key);
519 secp256k1_scalar_clear(&key);
523 int secp256k1_context_randomize(secp256k1_context_t* ctx, const unsigned char *seed32) {
524 ARG_CHECK(ctx != NULL);
525 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
526 secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
530 #ifdef ENABLE_MODULE_ECDH
531 # include "modules/ecdh/main_impl.h"