]> Git Repo - secp256k1.git/blame_incremental - src/secp256k1.c
Include stdio.h and stdlib.h explicitly in secp256k1.c
[secp256k1.git] / src / secp256k1.c
... / ...
CommitLineData
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 **********************************************************************/
6
7#include "include/secp256k1.h"
8#include "include/secp256k1_preallocated.h"
9
10#include "util.h"
11#include "num_impl.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"
22
23#define ARG_CHECK(cond) do { \
24 if (EXPECT(!(cond), 0)) { \
25 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
26 return 0; \
27 } \
28} while(0)
29
30#define ARG_CHECK_NO_RETURN(cond) do { \
31 if (EXPECT(!(cond), 0)) { \
32 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
33 } \
34} while(0)
35
36#ifndef USE_EXTERNAL_DEFAULT_CALLBACKS
37#include <stdlib.h>
38#include <stdio.h>
39static void default_illegal_callback_fn(const char* str, void* data) {
40 (void)data;
41 fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
42 abort();
43}
44static void default_error_callback_fn(const char* str, void* data) {
45 (void)data;
46 fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
47 abort();
48}
49#else
50void default_illegal_callback_fn(const char* str, void* data);
51void default_error_callback_fn(const char* str, void* data);
52#endif
53
54static const secp256k1_callback default_illegal_callback = {
55 default_illegal_callback_fn,
56 NULL
57};
58
59static const secp256k1_callback default_error_callback = {
60 default_error_callback_fn,
61 NULL
62};
63
64struct secp256k1_context_struct {
65 secp256k1_ecmult_context ecmult_ctx;
66 secp256k1_ecmult_gen_context ecmult_gen_ctx;
67 secp256k1_callback illegal_callback;
68 secp256k1_callback error_callback;
69};
70
71static const secp256k1_context secp256k1_context_no_precomp_ = {
72 { 0 },
73 { 0 },
74 { default_illegal_callback_fn, 0 },
75 { default_error_callback_fn, 0 }
76};
77const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_;
78
79size_t secp256k1_context_preallocated_size(unsigned int flags) {
80 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
81
82 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
83 secp256k1_callback_call(&default_illegal_callback,
84 "Invalid flags");
85 return 0;
86 }
87
88 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
89 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
90 }
91 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
92 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
93 }
94 return ret;
95}
96
97size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) {
98 size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
99 VERIFY_CHECK(ctx != NULL);
100 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
101 ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
102 }
103 if (secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)) {
104 ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
105 }
106 return ret;
107}
108
109secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) {
110 void* const base = prealloc;
111 size_t prealloc_size;
112 secp256k1_context* ret;
113
114 VERIFY_CHECK(prealloc != NULL);
115 prealloc_size = secp256k1_context_preallocated_size(flags);
116 ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);
117 ret->illegal_callback = default_illegal_callback;
118 ret->error_callback = default_error_callback;
119
120 if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
121 secp256k1_callback_call(&ret->illegal_callback,
122 "Invalid flags");
123 return NULL;
124 }
125
126 secp256k1_ecmult_context_init(&ret->ecmult_ctx);
127 secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
128
129 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) {
130 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &prealloc);
131 }
132 if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) {
133 secp256k1_ecmult_context_build(&ret->ecmult_ctx, &prealloc);
134 }
135
136 return (secp256k1_context*) ret;
137}
138
139secp256k1_context* secp256k1_context_create(unsigned int flags) {
140 size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
141 secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
142 if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
143 free(ctx);
144 return NULL;
145 }
146
147 return ctx;
148}
149
150secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
151 size_t prealloc_size;
152 secp256k1_context* ret;
153 VERIFY_CHECK(ctx != NULL);
154 ARG_CHECK(prealloc != NULL);
155
156 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
157 ret = (secp256k1_context*)prealloc;
158 memcpy(ret, ctx, prealloc_size);
159 secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx);
160 secp256k1_ecmult_context_finalize_memcpy(&ret->ecmult_ctx, &ctx->ecmult_ctx);
161 return ret;
162}
163
164secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
165 secp256k1_context* ret;
166 size_t prealloc_size;
167
168 VERIFY_CHECK(ctx != NULL);
169 prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
170 ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
171 ret = secp256k1_context_preallocated_clone(ctx, ret);
172 return ret;
173}
174
175void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
176 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
177 if (ctx != NULL) {
178 secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
179 secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
180 }
181}
182
183void secp256k1_context_destroy(secp256k1_context* ctx) {
184 if (ctx != NULL) {
185 secp256k1_context_preallocated_destroy(ctx);
186 free(ctx);
187 }
188}
189
190void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
191 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
192 if (fun == NULL) {
193 fun = default_illegal_callback_fn;
194 }
195 ctx->illegal_callback.fn = fun;
196 ctx->illegal_callback.data = data;
197}
198
199void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
200 ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp);
201 if (fun == NULL) {
202 fun = default_error_callback_fn;
203 }
204 ctx->error_callback.fn = fun;
205 ctx->error_callback.data = data;
206}
207
208secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
209 VERIFY_CHECK(ctx != NULL);
210 return secp256k1_scratch_create(&ctx->error_callback, max_size);
211}
212
213void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) {
214 VERIFY_CHECK(ctx != NULL);
215 secp256k1_scratch_destroy(&ctx->error_callback, scratch);
216}
217
218static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) {
219 if (sizeof(secp256k1_ge_storage) == 64) {
220 /* When the secp256k1_ge_storage type is exactly 64 byte, use its
221 * representation inside secp256k1_pubkey, as conversion is very fast.
222 * Note that secp256k1_pubkey_save must use the same representation. */
223 secp256k1_ge_storage s;
224 memcpy(&s, &pubkey->data[0], sizeof(s));
225 secp256k1_ge_from_storage(ge, &s);
226 } else {
227 /* Otherwise, fall back to 32-byte big endian for X and Y. */
228 secp256k1_fe x, y;
229 secp256k1_fe_set_b32(&x, pubkey->data);
230 secp256k1_fe_set_b32(&y, pubkey->data + 32);
231 secp256k1_ge_set_xy(ge, &x, &y);
232 }
233 ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
234 return 1;
235}
236
237static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) {
238 if (sizeof(secp256k1_ge_storage) == 64) {
239 secp256k1_ge_storage s;
240 secp256k1_ge_to_storage(&s, ge);
241 memcpy(&pubkey->data[0], &s, sizeof(s));
242 } else {
243 VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
244 secp256k1_fe_normalize_var(&ge->x);
245 secp256k1_fe_normalize_var(&ge->y);
246 secp256k1_fe_get_b32(pubkey->data, &ge->x);
247 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
248 }
249}
250
251int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
252 secp256k1_ge Q;
253
254 VERIFY_CHECK(ctx != NULL);
255 ARG_CHECK(pubkey != NULL);
256 memset(pubkey, 0, sizeof(*pubkey));
257 ARG_CHECK(input != NULL);
258 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
259 return 0;
260 }
261 secp256k1_pubkey_save(pubkey, &Q);
262 secp256k1_ge_clear(&Q);
263 return 1;
264}
265
266int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) {
267 secp256k1_ge Q;
268 size_t len;
269 int ret = 0;
270
271 VERIFY_CHECK(ctx != NULL);
272 ARG_CHECK(outputlen != NULL);
273 ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33 : 65));
274 len = *outputlen;
275 *outputlen = 0;
276 ARG_CHECK(output != NULL);
277 memset(output, 0, len);
278 ARG_CHECK(pubkey != NULL);
279 ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
280 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
281 ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
282 if (ret) {
283 *outputlen = len;
284 }
285 }
286 return ret;
287}
288
289static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) {
290 (void)ctx;
291 if (sizeof(secp256k1_scalar) == 32) {
292 /* When the secp256k1_scalar type is exactly 32 byte, use its
293 * representation inside secp256k1_ecdsa_signature, as conversion is very fast.
294 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
295 memcpy(r, &sig->data[0], 32);
296 memcpy(s, &sig->data[32], 32);
297 } else {
298 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
299 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
300 }
301}
302
303static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) {
304 if (sizeof(secp256k1_scalar) == 32) {
305 memcpy(&sig->data[0], r, 32);
306 memcpy(&sig->data[32], s, 32);
307 } else {
308 secp256k1_scalar_get_b32(&sig->data[0], r);
309 secp256k1_scalar_get_b32(&sig->data[32], s);
310 }
311}
312
313int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
314 secp256k1_scalar r, s;
315
316 VERIFY_CHECK(ctx != NULL);
317 ARG_CHECK(sig != NULL);
318 ARG_CHECK(input != NULL);
319
320 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
321 secp256k1_ecdsa_signature_save(sig, &r, &s);
322 return 1;
323 } else {
324 memset(sig, 0, sizeof(*sig));
325 return 0;
326 }
327}
328
329int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input64) {
330 secp256k1_scalar r, s;
331 int ret = 1;
332 int overflow = 0;
333
334 VERIFY_CHECK(ctx != NULL);
335 ARG_CHECK(sig != NULL);
336 ARG_CHECK(input64 != NULL);
337
338 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
339 ret &= !overflow;
340 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
341 ret &= !overflow;
342 if (ret) {
343 secp256k1_ecdsa_signature_save(sig, &r, &s);
344 } else {
345 memset(sig, 0, sizeof(*sig));
346 }
347 return ret;
348}
349
350int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) {
351 secp256k1_scalar r, s;
352
353 VERIFY_CHECK(ctx != NULL);
354 ARG_CHECK(output != NULL);
355 ARG_CHECK(outputlen != NULL);
356 ARG_CHECK(sig != NULL);
357
358 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
359 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
360}
361
362int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context* ctx, unsigned char *output64, const secp256k1_ecdsa_signature* sig) {
363 secp256k1_scalar r, s;
364
365 VERIFY_CHECK(ctx != NULL);
366 ARG_CHECK(output64 != NULL);
367 ARG_CHECK(sig != NULL);
368
369 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
370 secp256k1_scalar_get_b32(&output64[0], &r);
371 secp256k1_scalar_get_b32(&output64[32], &s);
372 return 1;
373}
374
375int secp256k1_ecdsa_signature_normalize(const secp256k1_context* ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) {
376 secp256k1_scalar r, s;
377 int ret = 0;
378
379 VERIFY_CHECK(ctx != NULL);
380 ARG_CHECK(sigin != NULL);
381
382 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
383 ret = secp256k1_scalar_is_high(&s);
384 if (sigout != NULL) {
385 if (ret) {
386 secp256k1_scalar_negate(&s, &s);
387 }
388 secp256k1_ecdsa_signature_save(sigout, &r, &s);
389 }
390
391 return ret;
392}
393
394int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const secp256k1_pubkey *pubkey) {
395 secp256k1_ge q;
396 secp256k1_scalar r, s;
397 secp256k1_scalar m;
398 VERIFY_CHECK(ctx != NULL);
399 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
400 ARG_CHECK(msg32 != NULL);
401 ARG_CHECK(sig != NULL);
402 ARG_CHECK(pubkey != NULL);
403
404 secp256k1_scalar_set_b32(&m, msg32, NULL);
405 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
406 return (!secp256k1_scalar_is_high(&s) &&
407 secp256k1_pubkey_load(ctx, &q, pubkey) &&
408 secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
409}
410
411static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
412 memcpy(buf + *offset, data, len);
413 *offset += len;
414}
415
416static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
417 unsigned char keydata[112];
418 unsigned int offset = 0;
419 secp256k1_rfc6979_hmac_sha256 rng;
420 unsigned int i;
421 /* We feed a byte array to the PRNG as input, consisting of:
422 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
423 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
424 * - optionally 16 extra bytes with the algorithm name.
425 * Because the arguments have distinct fixed lengths it is not possible for
426 * different argument mixtures to emulate each other and result in the same
427 * nonces.
428 */
429 buffer_append(keydata, &offset, key32, 32);
430 buffer_append(keydata, &offset, msg32, 32);
431 if (data != NULL) {
432 buffer_append(keydata, &offset, data, 32);
433 }
434 if (algo16 != NULL) {
435 buffer_append(keydata, &offset, algo16, 16);
436 }
437 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
438 memset(keydata, 0, sizeof(keydata));
439 for (i = 0; i <= counter; i++) {
440 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
441 }
442 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
443 return 1;
444}
445
446const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
447const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979;
448
449int 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) {
450 secp256k1_scalar r, s;
451 secp256k1_scalar sec, non, msg;
452 int ret = 0;
453 int overflow = 0;
454 VERIFY_CHECK(ctx != NULL);
455 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
456 ARG_CHECK(msg32 != NULL);
457 ARG_CHECK(signature != NULL);
458 ARG_CHECK(seckey != NULL);
459 if (noncefp == NULL) {
460 noncefp = secp256k1_nonce_function_default;
461 }
462
463 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
464 /* Fail if the secret key is invalid. */
465 if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
466 unsigned char nonce32[32];
467 unsigned int count = 0;
468 secp256k1_scalar_set_b32(&msg, msg32, NULL);
469 while (1) {
470 ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
471 if (!ret) {
472 break;
473 }
474 secp256k1_scalar_set_b32(&non, nonce32, &overflow);
475 if (!overflow && !secp256k1_scalar_is_zero(&non)) {
476 if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) {
477 break;
478 }
479 }
480 count++;
481 }
482 memset(nonce32, 0, 32);
483 secp256k1_scalar_clear(&msg);
484 secp256k1_scalar_clear(&non);
485 secp256k1_scalar_clear(&sec);
486 }
487 if (ret) {
488 secp256k1_ecdsa_signature_save(signature, &r, &s);
489 } else {
490 memset(signature, 0, sizeof(*signature));
491 }
492 return ret;
493}
494
495int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
496 secp256k1_scalar sec;
497 int ret;
498 int overflow;
499 VERIFY_CHECK(ctx != NULL);
500 ARG_CHECK(seckey != NULL);
501
502 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
503 ret = !overflow && !secp256k1_scalar_is_zero(&sec);
504 secp256k1_scalar_clear(&sec);
505 return ret;
506}
507
508int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) {
509 secp256k1_gej pj;
510 secp256k1_ge p;
511 secp256k1_scalar sec;
512 int overflow;
513 int ret = 0;
514 VERIFY_CHECK(ctx != NULL);
515 ARG_CHECK(pubkey != NULL);
516 memset(pubkey, 0, sizeof(*pubkey));
517 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
518 ARG_CHECK(seckey != NULL);
519
520 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
521 ret = (!overflow) & (!secp256k1_scalar_is_zero(&sec));
522 if (ret) {
523 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pj, &sec);
524 secp256k1_ge_set_gej(&p, &pj);
525 secp256k1_pubkey_save(pubkey, &p);
526 }
527 secp256k1_scalar_clear(&sec);
528 return ret;
529}
530
531int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
532 secp256k1_scalar sec;
533 VERIFY_CHECK(ctx != NULL);
534 ARG_CHECK(seckey != NULL);
535
536 secp256k1_scalar_set_b32(&sec, seckey, NULL);
537 secp256k1_scalar_negate(&sec, &sec);
538 secp256k1_scalar_get_b32(seckey, &sec);
539
540 secp256k1_scalar_clear(&sec);
541 return 1;
542}
543
544int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *pubkey) {
545 int ret = 0;
546 secp256k1_ge p;
547 VERIFY_CHECK(ctx != NULL);
548 ARG_CHECK(pubkey != NULL);
549
550 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
551 memset(pubkey, 0, sizeof(*pubkey));
552 if (ret) {
553 secp256k1_ge_neg(&p, &p);
554 secp256k1_pubkey_save(pubkey, &p);
555 }
556 return ret;
557}
558
559int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
560 secp256k1_scalar term;
561 secp256k1_scalar sec;
562 int ret = 0;
563 int overflow = 0;
564 VERIFY_CHECK(ctx != NULL);
565 ARG_CHECK(seckey != NULL);
566 ARG_CHECK(tweak != NULL);
567
568 secp256k1_scalar_set_b32(&term, tweak, &overflow);
569 secp256k1_scalar_set_b32(&sec, seckey, NULL);
570
571 ret = !overflow && secp256k1_eckey_privkey_tweak_add(&sec, &term);
572 memset(seckey, 0, 32);
573 if (ret) {
574 secp256k1_scalar_get_b32(seckey, &sec);
575 }
576
577 secp256k1_scalar_clear(&sec);
578 secp256k1_scalar_clear(&term);
579 return ret;
580}
581
582int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
583 secp256k1_ge p;
584 secp256k1_scalar term;
585 int ret = 0;
586 int overflow = 0;
587 VERIFY_CHECK(ctx != NULL);
588 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
589 ARG_CHECK(pubkey != NULL);
590 ARG_CHECK(tweak != NULL);
591
592 secp256k1_scalar_set_b32(&term, tweak, &overflow);
593 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
594 memset(pubkey, 0, sizeof(*pubkey));
595 if (ret) {
596 if (secp256k1_eckey_pubkey_tweak_add(&ctx->ecmult_ctx, &p, &term)) {
597 secp256k1_pubkey_save(pubkey, &p);
598 } else {
599 ret = 0;
600 }
601 }
602
603 return ret;
604}
605
606int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
607 secp256k1_scalar factor;
608 secp256k1_scalar sec;
609 int ret = 0;
610 int overflow = 0;
611 VERIFY_CHECK(ctx != NULL);
612 ARG_CHECK(seckey != NULL);
613 ARG_CHECK(tweak != NULL);
614
615 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
616 secp256k1_scalar_set_b32(&sec, seckey, NULL);
617 ret = !overflow && secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
618 memset(seckey, 0, 32);
619 if (ret) {
620 secp256k1_scalar_get_b32(seckey, &sec);
621 }
622
623 secp256k1_scalar_clear(&sec);
624 secp256k1_scalar_clear(&factor);
625 return ret;
626}
627
628int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
629 secp256k1_ge p;
630 secp256k1_scalar factor;
631 int ret = 0;
632 int overflow = 0;
633 VERIFY_CHECK(ctx != NULL);
634 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
635 ARG_CHECK(pubkey != NULL);
636 ARG_CHECK(tweak != NULL);
637
638 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
639 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
640 memset(pubkey, 0, sizeof(*pubkey));
641 if (ret) {
642 if (secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor)) {
643 secp256k1_pubkey_save(pubkey, &p);
644 } else {
645 ret = 0;
646 }
647 }
648
649 return ret;
650}
651
652int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) {
653 VERIFY_CHECK(ctx != NULL);
654 if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
655 secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
656 }
657 return 1;
658}
659
660int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) {
661 size_t i;
662 secp256k1_gej Qj;
663 secp256k1_ge Q;
664
665 ARG_CHECK(pubnonce != NULL);
666 memset(pubnonce, 0, sizeof(*pubnonce));
667 ARG_CHECK(n >= 1);
668 ARG_CHECK(pubnonces != NULL);
669
670 secp256k1_gej_set_infinity(&Qj);
671
672 for (i = 0; i < n; i++) {
673 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
674 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
675 }
676 if (secp256k1_gej_is_infinity(&Qj)) {
677 return 0;
678 }
679 secp256k1_ge_set_gej(&Q, &Qj);
680 secp256k1_pubkey_save(pubnonce, &Q);
681 return 1;
682}
683
684#ifdef ENABLE_MODULE_ECDH
685# include "modules/ecdh/main_impl.h"
686#endif
687
688#ifdef ENABLE_MODULE_RECOVERY
689# include "modules/recovery/main_impl.h"
690#endif
This page took 0.03085 seconds and 4 git commands to generate.