2020-12-29 17:15:51 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2013-2015 Pieter Wuille *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2021-06-14 14:55:38 +00:00
|
|
|
#define SECP256K1_BUILD
|
|
|
|
|
|
|
|
#include "../include/secp256k1.h"
|
|
|
|
#include "../include/secp256k1_preallocated.h"
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
#include "assumptions.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "field_impl.h"
|
|
|
|
#include "scalar_impl.h"
|
|
|
|
#include "group_impl.h"
|
|
|
|
#include "ecmult_impl.h"
|
|
|
|
#include "ecmult_const_impl.h"
|
|
|
|
#include "ecmult_gen_impl.h"
|
|
|
|
#include "ecdsa_impl.h"
|
|
|
|
#include "eckey_impl.h"
|
|
|
|
#include "hash_impl.h"
|
|
|
|
#include "scratch_impl.h"
|
2020-09-15 01:39:26 +00:00
|
|
|
#include "selftest.h"
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2021-06-14 14:55:38 +00:00
|
|
|
#ifdef SECP256K1_NO_BUILD
|
|
|
|
# error "secp256k1.h processed without SECP256K1_BUILD defined while building secp256k1.c"
|
|
|
|
#endif
|
|
|
|
|
2020-08-26 17:35:27 +00:00
|
|
|
#if defined(VALGRIND)
|
|
|
|
# include <valgrind/memcheck.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ARG_CHECK(cond) do { \
|
|
|
|
if (EXPECT(!(cond), 0)) { \
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_callback_call(&ctx->illegal_callback, #cond); \
|
2020-08-26 17:35:27 +00:00
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define ARG_CHECK_NO_RETURN(cond) do { \
|
|
|
|
if (EXPECT(!(cond), 0)) { \
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_callback_call(&ctx->illegal_callback, #cond); \
|
2020-08-26 17:35:27 +00:00
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
struct rustsecp256k1_v0_5_0_context_struct {
|
|
|
|
rustsecp256k1_v0_5_0_ecmult_gen_context ecmult_gen_ctx;
|
|
|
|
rustsecp256k1_v0_5_0_callback illegal_callback;
|
|
|
|
rustsecp256k1_v0_5_0_callback error_callback;
|
2020-08-26 17:35:27 +00:00
|
|
|
int declassify;
|
|
|
|
};
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static const rustsecp256k1_v0_5_0_context rustsecp256k1_v0_5_0_context_no_precomp_ = {
|
2020-08-26 17:35:27 +00:00
|
|
|
{ 0 },
|
2022-03-08 19:45:41 +00:00
|
|
|
{ rustsecp256k1_v0_5_0_default_illegal_callback_fn, 0 },
|
|
|
|
{ rustsecp256k1_v0_5_0_default_error_callback_fn, 0 },
|
2020-08-26 17:35:27 +00:00
|
|
|
0
|
|
|
|
};
|
2022-03-08 19:45:41 +00:00
|
|
|
const rustsecp256k1_v0_5_0_context *rustsecp256k1_v0_5_0_context_no_precomp = &rustsecp256k1_v0_5_0_context_no_precomp_;
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
size_t rustsecp256k1_v0_5_0_context_preallocated_size(unsigned int flags) {
|
|
|
|
size_t ret = sizeof(rustsecp256k1_v0_5_0_context);
|
2020-12-29 17:15:51 +00:00
|
|
|
/* A return value of 0 is reserved as an indicator for errors when we call this function internally. */
|
|
|
|
VERIFY_CHECK(ret != 0);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_callback_call(&default_illegal_callback,
|
2020-08-26 17:35:27 +00:00
|
|
|
"Invalid flags");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
size_t rustsecp256k1_v0_5_0_context_preallocated_clone_size(const rustsecp256k1_v0_5_0_context* ctx) {
|
|
|
|
size_t ret = sizeof(rustsecp256k1_v0_5_0_context);
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_context* rustsecp256k1_v0_5_0_context_preallocated_create(void* prealloc, unsigned int flags) {
|
2020-08-26 17:35:27 +00:00
|
|
|
size_t prealloc_size;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_context* ret;
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
if (!rustsecp256k1_v0_5_0_selftest()) {
|
|
|
|
rustsecp256k1_v0_5_0_callback_call(&default_error_callback, "self test failed");
|
2020-12-29 17:15:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
prealloc_size = rustsecp256k1_v0_5_0_context_preallocated_size(flags);
|
2020-12-29 17:15:51 +00:00
|
|
|
if (prealloc_size == 0) {
|
|
|
|
return NULL;
|
2020-09-15 01:39:26 +00:00
|
|
|
}
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(prealloc != NULL);
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = (rustsecp256k1_v0_5_0_context*)prealloc;
|
2020-08-26 17:35:27 +00:00
|
|
|
ret->illegal_callback = default_illegal_callback;
|
|
|
|
ret->error_callback = default_error_callback;
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
/* Flags have been checked by rustsecp256k1_v0_5_0_context_preallocated_size. */
|
2020-12-29 17:15:51 +00:00
|
|
|
VERIFY_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_CONTEXT);
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ecmult_gen_context_build(&ret->ecmult_gen_ctx);
|
2020-08-26 17:35:27 +00:00
|
|
|
ret->declassify = !!(flags & SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
return ret;
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_context* rustsecp256k1_v0_5_0_context_create(unsigned int flags) {
|
|
|
|
size_t const prealloc_size = rustsecp256k1_v0_5_0_context_preallocated_size(flags);
|
|
|
|
rustsecp256k1_v0_5_0_context* ctx = (rustsecp256k1_v0_5_0_context*)checked_malloc(&default_error_callback, prealloc_size);
|
|
|
|
if (EXPECT(rustsecp256k1_v0_5_0_context_preallocated_create(ctx, flags) == NULL, 0)) {
|
2020-08-26 17:35:27 +00:00
|
|
|
free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_context* rustsecp256k1_v0_5_0_context_preallocated_clone(const rustsecp256k1_v0_5_0_context* ctx, void* prealloc) {
|
|
|
|
rustsecp256k1_v0_5_0_context* ret;
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(prealloc != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = (rustsecp256k1_v0_5_0_context*)prealloc;
|
|
|
|
*ret = *ctx;
|
2020-08-26 17:35:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_context* rustsecp256k1_v0_5_0_context_clone(const rustsecp256k1_v0_5_0_context* ctx) {
|
|
|
|
rustsecp256k1_v0_5_0_context* ret;
|
2020-08-26 17:35:27 +00:00
|
|
|
size_t prealloc_size;
|
|
|
|
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
2022-03-08 19:45:41 +00:00
|
|
|
prealloc_size = rustsecp256k1_v0_5_0_context_preallocated_clone_size(ctx);
|
|
|
|
ret = (rustsecp256k1_v0_5_0_context*)checked_malloc(&ctx->error_callback, prealloc_size);
|
|
|
|
ret = rustsecp256k1_v0_5_0_context_preallocated_clone(ctx, ret);
|
2020-08-26 17:35:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
void rustsecp256k1_v0_5_0_context_preallocated_destroy(rustsecp256k1_v0_5_0_context* ctx) {
|
|
|
|
ARG_CHECK_NO_RETURN(ctx != rustsecp256k1_v0_5_0_context_no_precomp);
|
2020-08-26 17:35:27 +00:00
|
|
|
if (ctx != NULL) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
void rustsecp256k1_v0_5_0_context_destroy(rustsecp256k1_v0_5_0_context* ctx) {
|
2020-08-26 17:35:27 +00:00
|
|
|
if (ctx != NULL) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_context_preallocated_destroy(ctx);
|
2020-08-26 17:35:27 +00:00
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
void rustsecp256k1_v0_5_0_context_set_illegal_callback(rustsecp256k1_v0_5_0_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
|
|
|
|
ARG_CHECK_NO_RETURN(ctx != rustsecp256k1_v0_5_0_context_no_precomp);
|
2020-08-26 17:35:27 +00:00
|
|
|
if (fun == NULL) {
|
2022-03-08 19:45:41 +00:00
|
|
|
fun = rustsecp256k1_v0_5_0_default_illegal_callback_fn;
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
ctx->illegal_callback.fn = fun;
|
|
|
|
ctx->illegal_callback.data = data;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
void rustsecp256k1_v0_5_0_context_set_error_callback(rustsecp256k1_v0_5_0_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
|
|
|
|
ARG_CHECK_NO_RETURN(ctx != rustsecp256k1_v0_5_0_context_no_precomp);
|
2020-08-26 17:35:27 +00:00
|
|
|
if (fun == NULL) {
|
2022-03-08 19:45:41 +00:00
|
|
|
fun = rustsecp256k1_v0_5_0_default_error_callback_fn;
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
ctx->error_callback.fn = fun;
|
|
|
|
ctx->error_callback.data = data;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scratch_space* rustsecp256k1_v0_5_0_scratch_space_create(const rustsecp256k1_v0_5_0_context* ctx, size_t max_size) {
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
2022-03-08 19:45:41 +00:00
|
|
|
return rustsecp256k1_v0_5_0_scratch_create(&ctx->error_callback, max_size);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
void rustsecp256k1_v0_5_0_scratch_space_destroy(const rustsecp256k1_v0_5_0_context *ctx, rustsecp256k1_v0_5_0_scratch_space* scratch) {
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scratch_destroy(&ctx->error_callback, scratch);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
|
|
|
|
* of the software. This is setup for use with valgrind but could be substituted with
|
|
|
|
* the appropriate instrumentation for other analysis tools.
|
|
|
|
*/
|
2022-03-08 19:45:41 +00:00
|
|
|
static SECP256K1_INLINE void rustsecp256k1_v0_5_0_declassify(const rustsecp256k1_v0_5_0_context* ctx, const void *p, size_t len) {
|
2020-08-26 17:35:27 +00:00
|
|
|
#if defined(VALGRIND)
|
|
|
|
if (EXPECT(ctx->declassify,0)) VALGRIND_MAKE_MEM_DEFINED(p, len);
|
|
|
|
#else
|
|
|
|
(void)ctx;
|
|
|
|
(void)p;
|
|
|
|
(void)len;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static int rustsecp256k1_v0_5_0_pubkey_load(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_ge* ge, const rustsecp256k1_v0_5_0_pubkey* pubkey) {
|
|
|
|
if (sizeof(rustsecp256k1_v0_5_0_ge_storage) == 64) {
|
|
|
|
/* When the rustsecp256k1_v0_5_0_ge_storage type is exactly 64 byte, use its
|
|
|
|
* representation inside rustsecp256k1_v0_5_0_pubkey, as conversion is very fast.
|
|
|
|
* Note that rustsecp256k1_v0_5_0_pubkey_save must use the same representation. */
|
|
|
|
rustsecp256k1_v0_5_0_ge_storage s;
|
2020-08-26 17:35:27 +00:00
|
|
|
memcpy(&s, &pubkey->data[0], sizeof(s));
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ge_from_storage(ge, &s);
|
2020-08-26 17:35:27 +00:00
|
|
|
} else {
|
|
|
|
/* Otherwise, fall back to 32-byte big endian for X and Y. */
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_fe x, y;
|
|
|
|
rustsecp256k1_v0_5_0_fe_set_b32(&x, pubkey->data);
|
|
|
|
rustsecp256k1_v0_5_0_fe_set_b32(&y, pubkey->data + 32);
|
|
|
|
rustsecp256k1_v0_5_0_ge_set_xy(ge, &x, &y);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
ARG_CHECK(!rustsecp256k1_v0_5_0_fe_is_zero(&ge->x));
|
2020-08-26 17:35:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static void rustsecp256k1_v0_5_0_pubkey_save(rustsecp256k1_v0_5_0_pubkey* pubkey, rustsecp256k1_v0_5_0_ge* ge) {
|
|
|
|
if (sizeof(rustsecp256k1_v0_5_0_ge_storage) == 64) {
|
|
|
|
rustsecp256k1_v0_5_0_ge_storage s;
|
|
|
|
rustsecp256k1_v0_5_0_ge_to_storage(&s, ge);
|
2020-08-26 17:35:27 +00:00
|
|
|
memcpy(&pubkey->data[0], &s, sizeof(s));
|
|
|
|
} else {
|
2022-03-08 19:45:41 +00:00
|
|
|
VERIFY_CHECK(!rustsecp256k1_v0_5_0_ge_is_infinity(ge));
|
|
|
|
rustsecp256k1_v0_5_0_fe_normalize_var(&ge->x);
|
|
|
|
rustsecp256k1_v0_5_0_fe_normalize_var(&ge->y);
|
|
|
|
rustsecp256k1_v0_5_0_fe_get_b32(pubkey->data, &ge->x);
|
|
|
|
rustsecp256k1_v0_5_0_fe_get_b32(pubkey->data + 32, &ge->y);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_pubkey_parse(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_pubkey* pubkey, const unsigned char *input, size_t inputlen) {
|
|
|
|
rustsecp256k1_v0_5_0_ge Q;
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(pubkey != NULL);
|
|
|
|
memset(pubkey, 0, sizeof(*pubkey));
|
|
|
|
ARG_CHECK(input != NULL);
|
2022-03-08 19:45:41 +00:00
|
|
|
if (!rustsecp256k1_v0_5_0_eckey_pubkey_parse(&Q, input, inputlen)) {
|
2020-09-15 01:39:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
if (!rustsecp256k1_v0_5_0_ge_is_in_correct_subgroup(&Q)) {
|
2020-08-26 17:35:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_pubkey_save(pubkey, &Q);
|
|
|
|
rustsecp256k1_v0_5_0_ge_clear(&Q);
|
2020-08-26 17:35:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_pubkey_serialize(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *output, size_t *outputlen, const rustsecp256k1_v0_5_0_pubkey* pubkey, unsigned int flags) {
|
|
|
|
rustsecp256k1_v0_5_0_ge Q;
|
2020-08-26 17:35:27 +00:00
|
|
|
size_t len;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(outputlen != NULL);
|
|
|
|
ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33u : 65u));
|
|
|
|
len = *outputlen;
|
|
|
|
*outputlen = 0;
|
|
|
|
ARG_CHECK(output != NULL);
|
|
|
|
memset(output, 0, len);
|
|
|
|
ARG_CHECK(pubkey != NULL);
|
|
|
|
ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
|
2022-03-08 19:45:41 +00:00
|
|
|
if (rustsecp256k1_v0_5_0_pubkey_load(ctx, &Q, pubkey)) {
|
|
|
|
ret = rustsecp256k1_v0_5_0_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
|
2020-08-26 17:35:27 +00:00
|
|
|
if (ret) {
|
|
|
|
*outputlen = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_pubkey_cmp(const rustsecp256k1_v0_5_0_context* ctx, const rustsecp256k1_v0_5_0_pubkey* pubkey0, const rustsecp256k1_v0_5_0_pubkey* pubkey1) {
|
2021-06-14 14:55:38 +00:00
|
|
|
unsigned char out[2][33];
|
2022-03-08 19:45:41 +00:00
|
|
|
const rustsecp256k1_v0_5_0_pubkey* pk[2];
|
2021-06-14 14:55:38 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
pk[0] = pubkey0; pk[1] = pubkey1;
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
size_t out_size = sizeof(out[i]);
|
|
|
|
/* If the public key is NULL or invalid, ec_pubkey_serialize will call
|
|
|
|
* the illegal_callback and return 0. In that case we will serialize the
|
|
|
|
* key as all zeros which is less than any valid public key. This
|
|
|
|
* results in consistent comparisons even if NULL or invalid pubkeys are
|
|
|
|
* involved and prevents edge cases such as sorting algorithms that use
|
|
|
|
* this function and do not terminate as a result. */
|
2022-03-08 19:45:41 +00:00
|
|
|
if (!rustsecp256k1_v0_5_0_ec_pubkey_serialize(ctx, out[i], &out_size, pk[i], SECP256K1_EC_COMPRESSED)) {
|
2021-06-14 14:55:38 +00:00
|
|
|
/* Note that ec_pubkey_serialize should already set the output to
|
|
|
|
* zero in that case, but it's not guaranteed by the API, we can't
|
|
|
|
* test it and writing a VERIFY_CHECK is more complex than
|
|
|
|
* explicitly memsetting (again). */
|
|
|
|
memset(out[i], 0, sizeof(out[i]));
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
return rustsecp256k1_v0_5_0_memcmp_var(out[0], out[1], sizeof(out[0]));
|
2021-06-14 14:55:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static void rustsecp256k1_v0_5_0_ecdsa_signature_load(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_scalar* r, rustsecp256k1_v0_5_0_scalar* s, const rustsecp256k1_v0_5_0_ecdsa_signature* sig) {
|
2020-08-26 17:35:27 +00:00
|
|
|
(void)ctx;
|
2022-03-08 19:45:41 +00:00
|
|
|
if (sizeof(rustsecp256k1_v0_5_0_scalar) == 32) {
|
|
|
|
/* When the rustsecp256k1_v0_5_0_scalar type is exactly 32 byte, use its
|
|
|
|
* representation inside rustsecp256k1_v0_5_0_ecdsa_signature, as conversion is very fast.
|
|
|
|
* Note that rustsecp256k1_v0_5_0_ecdsa_signature_save must use the same representation. */
|
2020-08-26 17:35:27 +00:00
|
|
|
memcpy(r, &sig->data[0], 32);
|
|
|
|
memcpy(s, &sig->data[32], 32);
|
|
|
|
} else {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(r, &sig->data[0], NULL);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(s, &sig->data[32], NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static void rustsecp256k1_v0_5_0_ecdsa_signature_save(rustsecp256k1_v0_5_0_ecdsa_signature* sig, const rustsecp256k1_v0_5_0_scalar* r, const rustsecp256k1_v0_5_0_scalar* s) {
|
|
|
|
if (sizeof(rustsecp256k1_v0_5_0_scalar) == 32) {
|
2020-08-26 17:35:27 +00:00
|
|
|
memcpy(&sig->data[0], r, 32);
|
|
|
|
memcpy(&sig->data[32], s, 32);
|
|
|
|
} else {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_get_b32(&sig->data[0], r);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_get_b32(&sig->data[32], s);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ecdsa_signature_parse_der(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar r, s;
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(sig != NULL);
|
|
|
|
ARG_CHECK(input != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
if (rustsecp256k1_v0_5_0_ecdsa_sig_parse(&r, &s, input, inputlen)) {
|
|
|
|
rustsecp256k1_v0_5_0_ecdsa_signature_save(sig, &r, &s);
|
2020-08-26 17:35:27 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
memset(sig, 0, sizeof(*sig));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ecdsa_signature_parse_compact(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_ecdsa_signature* sig, const unsigned char *input64) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar r, s;
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret = 1;
|
|
|
|
int overflow = 0;
|
|
|
|
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(sig != NULL);
|
|
|
|
ARG_CHECK(input64 != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(&r, &input64[0], &overflow);
|
2020-08-26 17:35:27 +00:00
|
|
|
ret &= !overflow;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(&s, &input64[32], &overflow);
|
2020-08-26 17:35:27 +00:00
|
|
|
ret &= !overflow;
|
|
|
|
if (ret) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ecdsa_signature_save(sig, &r, &s);
|
2020-08-26 17:35:27 +00:00
|
|
|
} else {
|
|
|
|
memset(sig, 0, sizeof(*sig));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ecdsa_signature_serialize_der(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *output, size_t *outputlen, const rustsecp256k1_v0_5_0_ecdsa_signature* sig) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar r, s;
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(output != NULL);
|
|
|
|
ARG_CHECK(outputlen != NULL);
|
|
|
|
ARG_CHECK(sig != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ecdsa_signature_load(ctx, &r, &s, sig);
|
|
|
|
return rustsecp256k1_v0_5_0_ecdsa_sig_serialize(output, outputlen, &r, &s);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ecdsa_signature_serialize_compact(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *output64, const rustsecp256k1_v0_5_0_ecdsa_signature* sig) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar r, s;
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(output64 != NULL);
|
|
|
|
ARG_CHECK(sig != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ecdsa_signature_load(ctx, &r, &s, sig);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_get_b32(&output64[0], &r);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_get_b32(&output64[32], &s);
|
2020-08-26 17:35:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ecdsa_signature_normalize(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_ecdsa_signature *sigout, const rustsecp256k1_v0_5_0_ecdsa_signature *sigin) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar r, s;
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(sigin != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ecdsa_signature_load(ctx, &r, &s, sigin);
|
|
|
|
ret = rustsecp256k1_v0_5_0_scalar_is_high(&s);
|
2020-08-26 17:35:27 +00:00
|
|
|
if (sigout != NULL) {
|
|
|
|
if (ret) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_negate(&s, &s);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ecdsa_signature_save(sigout, &r, &s);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ecdsa_verify(const rustsecp256k1_v0_5_0_context* ctx, const rustsecp256k1_v0_5_0_ecdsa_signature *sig, const unsigned char *msghash32, const rustsecp256k1_v0_5_0_pubkey *pubkey) {
|
|
|
|
rustsecp256k1_v0_5_0_ge q;
|
|
|
|
rustsecp256k1_v0_5_0_scalar r, s;
|
|
|
|
rustsecp256k1_v0_5_0_scalar m;
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
2020-12-29 17:15:51 +00:00
|
|
|
ARG_CHECK(msghash32 != NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
ARG_CHECK(sig != NULL);
|
|
|
|
ARG_CHECK(pubkey != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(&m, msghash32, NULL);
|
|
|
|
rustsecp256k1_v0_5_0_ecdsa_signature_load(ctx, &r, &s, sig);
|
|
|
|
return (!rustsecp256k1_v0_5_0_scalar_is_high(&s) &&
|
|
|
|
rustsecp256k1_v0_5_0_pubkey_load(ctx, &q, pubkey) &&
|
|
|
|
rustsecp256k1_v0_5_0_ecdsa_sig_verify(&r, &s, &q, &m));
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) {
|
|
|
|
memcpy(buf + *offset, data, len);
|
|
|
|
*offset += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
unsigned char keydata[112];
|
|
|
|
unsigned int offset = 0;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_rfc6979_hmac_sha256 rng;
|
2020-08-26 17:35:27 +00:00
|
|
|
unsigned int i;
|
|
|
|
/* We feed a byte array to the PRNG as input, consisting of:
|
|
|
|
* - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
|
|
|
|
* - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
|
|
|
|
* - optionally 16 extra bytes with the algorithm name.
|
|
|
|
* Because the arguments have distinct fixed lengths it is not possible for
|
|
|
|
* different argument mixtures to emulate each other and result in the same
|
|
|
|
* nonces.
|
|
|
|
*/
|
|
|
|
buffer_append(keydata, &offset, key32, 32);
|
|
|
|
buffer_append(keydata, &offset, msg32, 32);
|
|
|
|
if (data != NULL) {
|
|
|
|
buffer_append(keydata, &offset, data, 32);
|
|
|
|
}
|
|
|
|
if (algo16 != NULL) {
|
|
|
|
buffer_append(keydata, &offset, algo16, 16);
|
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
|
2020-08-26 17:35:27 +00:00
|
|
|
memset(keydata, 0, sizeof(keydata));
|
|
|
|
for (i = 0; i <= counter; i++) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_rfc6979_hmac_sha256_finalize(&rng);
|
2020-08-26 17:35:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
const rustsecp256k1_v0_5_0_nonce_function rustsecp256k1_v0_5_0_nonce_function_rfc6979 = nonce_function_rfc6979;
|
|
|
|
const rustsecp256k1_v0_5_0_nonce_function rustsecp256k1_v0_5_0_nonce_function_default = nonce_function_rfc6979;
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static int rustsecp256k1_v0_5_0_ecdsa_sign_inner(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_scalar* r, rustsecp256k1_v0_5_0_scalar* s, int* recid, const unsigned char *msg32, const unsigned char *seckey, rustsecp256k1_v0_5_0_nonce_function noncefp, const void* noncedata) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar sec, non, msg;
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret = 0;
|
|
|
|
int is_sec_valid;
|
|
|
|
unsigned char nonce32[32];
|
|
|
|
unsigned int count = 0;
|
|
|
|
/* Default initialization here is important so we won't pass uninit values to the cmov in the end */
|
2022-03-08 19:45:41 +00:00
|
|
|
*r = rustsecp256k1_v0_5_0_scalar_zero;
|
|
|
|
*s = rustsecp256k1_v0_5_0_scalar_zero;
|
2020-08-26 17:35:27 +00:00
|
|
|
if (recid) {
|
|
|
|
*recid = 0;
|
|
|
|
}
|
|
|
|
if (noncefp == NULL) {
|
2022-03-08 19:45:41 +00:00
|
|
|
noncefp = rustsecp256k1_v0_5_0_nonce_function_default;
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fail if the secret key is invalid. */
|
2022-03-08 19:45:41 +00:00
|
|
|
is_sec_valid = rustsecp256k1_v0_5_0_scalar_set_b32_seckey(&sec, seckey);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_cmov(&sec, &rustsecp256k1_v0_5_0_scalar_one, !is_sec_valid);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(&msg, msg32, NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
while (1) {
|
|
|
|
int is_nonce_valid;
|
|
|
|
ret = !!noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
|
|
|
|
if (!ret) {
|
|
|
|
break;
|
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
is_nonce_valid = rustsecp256k1_v0_5_0_scalar_set_b32_seckey(&non, nonce32);
|
2020-08-26 17:35:27 +00:00
|
|
|
/* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid));
|
2020-08-26 17:35:27 +00:00
|
|
|
if (is_nonce_valid) {
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = rustsecp256k1_v0_5_0_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid);
|
2020-08-26 17:35:27 +00:00
|
|
|
/* The final signature is no longer a secret, nor is the fact that we were successful or not. */
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_declassify(ctx, &ret, sizeof(ret));
|
2020-08-26 17:35:27 +00:00
|
|
|
if (ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
/* We don't want to declassify is_sec_valid and therefore the range of
|
|
|
|
* seckey. As a result is_sec_valid is included in ret only after ret was
|
|
|
|
* used as a branching variable. */
|
|
|
|
ret &= is_sec_valid;
|
|
|
|
memset(nonce32, 0, 32);
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&msg);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&non);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&sec);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_cmov(r, &rustsecp256k1_v0_5_0_scalar_zero, !ret);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_cmov(s, &rustsecp256k1_v0_5_0_scalar_zero, !ret);
|
2020-08-26 17:35:27 +00:00
|
|
|
if (recid) {
|
|
|
|
const int zero = 0;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_int_cmov(recid, &zero, !ret);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ecdsa_sign(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_ecdsa_signature *signature, const unsigned char *msghash32, const unsigned char *seckey, rustsecp256k1_v0_5_0_nonce_function noncefp, const void* noncedata) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar r, s;
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret;
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
2022-03-08 19:45:41 +00:00
|
|
|
ARG_CHECK(rustsecp256k1_v0_5_0_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
|
2020-12-29 17:15:51 +00:00
|
|
|
ARG_CHECK(msghash32 != NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
ARG_CHECK(signature != NULL);
|
|
|
|
ARG_CHECK(seckey != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = rustsecp256k1_v0_5_0_ecdsa_sign_inner(ctx, &r, &s, NULL, msghash32, seckey, noncefp, noncedata);
|
|
|
|
rustsecp256k1_v0_5_0_ecdsa_signature_save(signature, &r, &s);
|
2020-08-26 17:35:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_seckey_verify(const rustsecp256k1_v0_5_0_context* ctx, const unsigned char *seckey) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar sec;
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret;
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(seckey != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = rustsecp256k1_v0_5_0_scalar_set_b32_seckey(&sec, seckey);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&sec);
|
2020-08-26 17:35:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static int rustsecp256k1_v0_5_0_ec_pubkey_create_helper(const rustsecp256k1_v0_5_0_ecmult_gen_context *ecmult_gen_ctx, rustsecp256k1_v0_5_0_scalar *seckey_scalar, rustsecp256k1_v0_5_0_ge *p, const unsigned char *seckey) {
|
|
|
|
rustsecp256k1_v0_5_0_gej pj;
|
2020-09-15 01:39:26 +00:00
|
|
|
int ret;
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = rustsecp256k1_v0_5_0_scalar_set_b32_seckey(seckey_scalar, seckey);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_cmov(seckey_scalar, &rustsecp256k1_v0_5_0_scalar_one, !ret);
|
2020-09-15 01:39:26 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ecmult_gen(ecmult_gen_ctx, &pj, seckey_scalar);
|
|
|
|
rustsecp256k1_v0_5_0_ge_set_gej(p, &pj);
|
2020-09-15 01:39:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_pubkey_create(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_pubkey *pubkey, const unsigned char *seckey) {
|
|
|
|
rustsecp256k1_v0_5_0_ge p;
|
|
|
|
rustsecp256k1_v0_5_0_scalar seckey_scalar;
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret = 0;
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(pubkey != NULL);
|
|
|
|
memset(pubkey, 0, sizeof(*pubkey));
|
2022-03-08 19:45:41 +00:00
|
|
|
ARG_CHECK(rustsecp256k1_v0_5_0_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
|
2020-08-26 17:35:27 +00:00
|
|
|
ARG_CHECK(seckey != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = rustsecp256k1_v0_5_0_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey);
|
|
|
|
rustsecp256k1_v0_5_0_pubkey_save(pubkey, &p);
|
|
|
|
rustsecp256k1_v0_5_0_memczero(pubkey, sizeof(*pubkey), !ret);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&seckey_scalar);
|
2020-08-26 17:35:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_seckey_negate(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *seckey) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar sec;
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret = 0;
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(seckey != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = rustsecp256k1_v0_5_0_scalar_set_b32_seckey(&sec, seckey);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_cmov(&sec, &rustsecp256k1_v0_5_0_scalar_zero, !ret);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_negate(&sec, &sec);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_get_b32(seckey, &sec);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&sec);
|
2020-08-26 17:35:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_privkey_negate(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *seckey) {
|
|
|
|
return rustsecp256k1_v0_5_0_ec_seckey_negate(ctx, seckey);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_pubkey_negate(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_pubkey *pubkey) {
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret = 0;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ge p;
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(pubkey != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = rustsecp256k1_v0_5_0_pubkey_load(ctx, &p, pubkey);
|
2020-08-26 17:35:27 +00:00
|
|
|
memset(pubkey, 0, sizeof(*pubkey));
|
|
|
|
if (ret) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ge_neg(&p, &p);
|
|
|
|
rustsecp256k1_v0_5_0_pubkey_save(pubkey, &p);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-15 01:39:26 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static int rustsecp256k1_v0_5_0_ec_seckey_tweak_add_helper(rustsecp256k1_v0_5_0_scalar *sec, const unsigned char *tweak32) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar term;
|
2020-08-26 17:35:27 +00:00
|
|
|
int overflow = 0;
|
2020-09-15 01:39:26 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(&term, tweak32, &overflow);
|
|
|
|
ret = (!overflow) & rustsecp256k1_v0_5_0_eckey_privkey_tweak_add(sec, &term);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&term);
|
2020-09-15 01:39:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_seckey_tweak_add(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar sec;
|
2020-09-15 01:39:26 +00:00
|
|
|
int ret = 0;
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(seckey != NULL);
|
2020-12-29 17:15:51 +00:00
|
|
|
ARG_CHECK(tweak32 != NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = rustsecp256k1_v0_5_0_scalar_set_b32_seckey(&sec, seckey);
|
|
|
|
ret &= rustsecp256k1_v0_5_0_ec_seckey_tweak_add_helper(&sec, tweak32);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_cmov(&sec, &rustsecp256k1_v0_5_0_scalar_zero, !ret);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_get_b32(seckey, &sec);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&sec);
|
2020-08-26 17:35:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_privkey_tweak_add(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
|
|
|
|
return rustsecp256k1_v0_5_0_ec_seckey_tweak_add(ctx, seckey, tweak32);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
static int rustsecp256k1_v0_5_0_ec_pubkey_tweak_add_helper(rustsecp256k1_v0_5_0_ge *p, const unsigned char *tweak32) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar term;
|
2020-08-26 17:35:27 +00:00
|
|
|
int overflow = 0;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(&term, tweak32, &overflow);
|
|
|
|
return !overflow && rustsecp256k1_v0_5_0_eckey_pubkey_tweak_add(p, &term);
|
2020-09-15 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_pubkey_tweak_add(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_pubkey *pubkey, const unsigned char *tweak32) {
|
|
|
|
rustsecp256k1_v0_5_0_ge p;
|
2020-09-15 01:39:26 +00:00
|
|
|
int ret = 0;
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(pubkey != NULL);
|
2020-12-29 17:15:51 +00:00
|
|
|
ARG_CHECK(tweak32 != NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = rustsecp256k1_v0_5_0_pubkey_load(ctx, &p, pubkey);
|
2020-08-26 17:35:27 +00:00
|
|
|
memset(pubkey, 0, sizeof(*pubkey));
|
2022-03-08 19:45:41 +00:00
|
|
|
ret = ret && rustsecp256k1_v0_5_0_ec_pubkey_tweak_add_helper(&p, tweak32);
|
2020-08-26 17:35:27 +00:00
|
|
|
if (ret) {
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_pubkey_save(pubkey, &p);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_seckey_tweak_mul(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
|
|
|
|
rustsecp256k1_v0_5_0_scalar factor;
|
|
|
|
rustsecp256k1_v0_5_0_scalar sec;
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret = 0;
|
|
|
|
int overflow = 0;
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(seckey != NULL);
|
2020-12-29 17:15:51 +00:00
|
|
|
ARG_CHECK(tweak32 != NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(&factor, tweak32, &overflow);
|
|
|
|
ret = rustsecp256k1_v0_5_0_scalar_set_b32_seckey(&sec, seckey);
|
|
|
|
ret &= (!overflow) & rustsecp256k1_v0_5_0_eckey_privkey_tweak_mul(&sec, &factor);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_cmov(&sec, &rustsecp256k1_v0_5_0_scalar_zero, !ret);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_get_b32(seckey, &sec);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&sec);
|
|
|
|
rustsecp256k1_v0_5_0_scalar_clear(&factor);
|
2020-08-26 17:35:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_privkey_tweak_mul(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *seckey, const unsigned char *tweak32) {
|
|
|
|
return rustsecp256k1_v0_5_0_ec_seckey_tweak_mul(ctx, seckey, tweak32);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_pubkey_tweak_mul(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_pubkey *pubkey, const unsigned char *tweak32) {
|
|
|
|
rustsecp256k1_v0_5_0_ge p;
|
|
|
|
rustsecp256k1_v0_5_0_scalar factor;
|
2020-08-26 17:35:27 +00:00
|
|
|
int ret = 0;
|
|
|
|
int overflow = 0;
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(pubkey != NULL);
|
2020-12-29 17:15:51 +00:00
|
|
|
ARG_CHECK(tweak32 != NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_scalar_set_b32(&factor, tweak32, &overflow);
|
|
|
|
ret = !overflow && rustsecp256k1_v0_5_0_pubkey_load(ctx, &p, pubkey);
|
2020-08-26 17:35:27 +00:00
|
|
|
memset(pubkey, 0, sizeof(*pubkey));
|
|
|
|
if (ret) {
|
2022-03-08 19:45:41 +00:00
|
|
|
if (rustsecp256k1_v0_5_0_eckey_pubkey_tweak_mul(&p, &factor)) {
|
|
|
|
rustsecp256k1_v0_5_0_pubkey_save(pubkey, &p);
|
2020-08-26 17:35:27 +00:00
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_context_randomize(rustsecp256k1_v0_5_0_context* ctx, const unsigned char *seed32) {
|
2020-08-26 17:35:27 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
2022-03-08 19:45:41 +00:00
|
|
|
if (rustsecp256k1_v0_5_0_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
|
|
|
|
rustsecp256k1_v0_5_0_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
int rustsecp256k1_v0_5_0_ec_pubkey_combine(const rustsecp256k1_v0_5_0_context* ctx, rustsecp256k1_v0_5_0_pubkey *pubnonce, const rustsecp256k1_v0_5_0_pubkey * const *pubnonces, size_t n) {
|
2020-08-26 17:35:27 +00:00
|
|
|
size_t i;
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_gej Qj;
|
|
|
|
rustsecp256k1_v0_5_0_ge Q;
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
VERIFY_CHECK(ctx != NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
ARG_CHECK(pubnonce != NULL);
|
|
|
|
memset(pubnonce, 0, sizeof(*pubnonce));
|
|
|
|
ARG_CHECK(n >= 1);
|
|
|
|
ARG_CHECK(pubnonces != NULL);
|
|
|
|
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_gej_set_infinity(&Qj);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2022-03-08 19:45:41 +00:00
|
|
|
ARG_CHECK(pubnonces[i] != NULL);
|
|
|
|
rustsecp256k1_v0_5_0_pubkey_load(ctx, &Q, pubnonces[i]);
|
|
|
|
rustsecp256k1_v0_5_0_gej_add_ge(&Qj, &Qj, &Q);
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
if (rustsecp256k1_v0_5_0_gej_is_infinity(&Qj)) {
|
2020-08-26 17:35:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-03-08 19:45:41 +00:00
|
|
|
rustsecp256k1_v0_5_0_ge_set_gej(&Q, &Qj);
|
|
|
|
rustsecp256k1_v0_5_0_pubkey_save(pubnonce, &Q);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rustsecp256k1_v0_5_0_tagged_sha256(const rustsecp256k1_v0_5_0_context* ctx, unsigned char *hash32, const unsigned char *tag, size_t taglen, const unsigned char *msg, size_t msglen) {
|
|
|
|
rustsecp256k1_v0_5_0_sha256 sha;
|
|
|
|
VERIFY_CHECK(ctx != NULL);
|
|
|
|
ARG_CHECK(hash32 != NULL);
|
|
|
|
ARG_CHECK(tag != NULL);
|
|
|
|
ARG_CHECK(msg != NULL);
|
|
|
|
|
|
|
|
rustsecp256k1_v0_5_0_sha256_initialize_tagged(&sha, tag, taglen);
|
|
|
|
rustsecp256k1_v0_5_0_sha256_write(&sha, msg, msglen);
|
|
|
|
rustsecp256k1_v0_5_0_sha256_finalize(&sha, hash32);
|
2020-08-26 17:35:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_MODULE_ECDH
|
|
|
|
# include "modules/ecdh/main_impl.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENABLE_MODULE_RECOVERY
|
|
|
|
# include "modules/recovery/main_impl.h"
|
|
|
|
#endif
|
2020-09-15 01:39:26 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_MODULE_EXTRAKEYS
|
|
|
|
# include "modules/extrakeys/main_impl.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENABLE_MODULE_SCHNORRSIG
|
|
|
|
# include "modules/schnorrsig/main_impl.h"
|
|
|
|
#endif
|