Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/secp256k1.c
/********************************************************************** | /********************************************************************** | ||||
* Copyright (c) 2013-2015 Pieter Wuille * | * Copyright (c) 2013-2015 Pieter Wuille * | ||||
* Distributed under the MIT software license, see the accompanying * | * Distributed under the MIT software license, see the accompanying * | ||||
* file COPYING or http://www.opensource.org/licenses/mit-license.php.* | * file COPYING or http://www.opensource.org/licenses/mit-license.php.* | ||||
**********************************************************************/ | **********************************************************************/ | ||||
#include "include/secp256k1.h" | #include "include/secp256k1.h" | ||||
#include "include/secp256k1_preallocated.h" | |||||
#include "util.h" | #include "util.h" | ||||
#include "num_impl.h" | #include "num_impl.h" | ||||
#include "field_impl.h" | #include "field_impl.h" | ||||
#include "scalar_impl.h" | #include "scalar_impl.h" | ||||
#include "group_impl.h" | #include "group_impl.h" | ||||
#include "ecmult_impl.h" | #include "ecmult_impl.h" | ||||
#include "ecmult_const_impl.h" | #include "ecmult_const_impl.h" | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | |||||
static const secp256k1_context secp256k1_context_no_precomp_ = { | static const secp256k1_context secp256k1_context_no_precomp_ = { | ||||
{ 0 }, | { 0 }, | ||||
{ 0 }, | { 0 }, | ||||
{ default_illegal_callback_fn, 0 }, | { default_illegal_callback_fn, 0 }, | ||||
{ default_error_callback_fn, 0 } | { default_error_callback_fn, 0 } | ||||
}; | }; | ||||
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_; | const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_; | ||||
secp256k1_context* secp256k1_context_create(unsigned int flags) { | size_t secp256k1_context_preallocated_size(unsigned int flags) { | ||||
secp256k1_context* ret = (secp256k1_context*)checked_malloc(&default_error_callback, sizeof(secp256k1_context)); | size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context)); | ||||
if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) { | |||||
secp256k1_callback_call(&default_illegal_callback, | |||||
"Invalid flags"); | |||||
return 0; | |||||
} | |||||
if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) { | |||||
ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE; | |||||
} | |||||
if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) { | |||||
ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE; | |||||
} | |||||
return ret; | |||||
} | |||||
size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) { | |||||
size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context)); | |||||
VERIFY_CHECK(ctx != NULL); | |||||
if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) { | |||||
ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE; | |||||
} | |||||
if (secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)) { | |||||
ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE; | |||||
} | |||||
return ret; | |||||
} | |||||
secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) { | |||||
void* const base = prealloc; | |||||
size_t prealloc_size; | |||||
secp256k1_context* ret; | |||||
VERIFY_CHECK(prealloc != NULL); | |||||
prealloc_size = secp256k1_context_preallocated_size(flags); | |||||
ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size); | |||||
ret->illegal_callback = default_illegal_callback; | ret->illegal_callback = default_illegal_callback; | ||||
ret->error_callback = default_error_callback; | ret->error_callback = default_error_callback; | ||||
if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) { | if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) { | ||||
secp256k1_callback_call(&ret->illegal_callback, | secp256k1_callback_call(&ret->illegal_callback, | ||||
"Invalid flags"); | "Invalid flags"); | ||||
free(ret); | |||||
return NULL; | return NULL; | ||||
} | } | ||||
secp256k1_ecmult_context_init(&ret->ecmult_ctx); | secp256k1_ecmult_context_init(&ret->ecmult_ctx); | ||||
secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx); | secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx); | ||||
if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) { | if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) { | ||||
secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &ret->error_callback); | secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &prealloc); | ||||
} | } | ||||
if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) { | if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) { | ||||
secp256k1_ecmult_context_build(&ret->ecmult_ctx, &ret->error_callback); | secp256k1_ecmult_context_build(&ret->ecmult_ctx, &prealloc); | ||||
} | |||||
return (secp256k1_context*) ret; | |||||
} | |||||
secp256k1_context* secp256k1_context_create(unsigned int flags) { | |||||
size_t const prealloc_size = secp256k1_context_preallocated_size(flags); | |||||
secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size); | |||||
if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) { | |||||
free(ctx); | |||||
return NULL; | |||||
} | |||||
return ctx; | |||||
} | } | ||||
secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) { | |||||
size_t prealloc_size; | |||||
secp256k1_context* ret; | |||||
VERIFY_CHECK(ctx != NULL); | |||||
ARG_CHECK(prealloc != NULL); | |||||
prealloc_size = secp256k1_context_preallocated_clone_size(ctx); | |||||
ret = (secp256k1_context*)prealloc; | |||||
memcpy(ret, ctx, prealloc_size); | |||||
secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx); | |||||
secp256k1_ecmult_context_finalize_memcpy(&ret->ecmult_ctx, &ctx->ecmult_ctx); | |||||
return ret; | return ret; | ||||
} | } | ||||
secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) { | secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) { | ||||
secp256k1_context* ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, sizeof(secp256k1_context)); | secp256k1_context* ret; | ||||
ret->illegal_callback = ctx->illegal_callback; | size_t prealloc_size; | ||||
ret->error_callback = ctx->error_callback; | |||||
secp256k1_ecmult_context_clone(&ret->ecmult_ctx, &ctx->ecmult_ctx, &ctx->error_callback); | VERIFY_CHECK(ctx != NULL); | ||||
secp256k1_ecmult_gen_context_clone(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx, &ctx->error_callback); | prealloc_size = secp256k1_context_preallocated_clone_size(ctx); | ||||
ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size); | |||||
ret = secp256k1_context_preallocated_clone(ctx, ret); | |||||
return ret; | return ret; | ||||
} | } | ||||
void secp256k1_context_destroy(secp256k1_context* ctx) { | void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) { | ||||
CHECK(ctx != secp256k1_context_no_precomp); | CHECK(ctx != secp256k1_context_no_precomp); | ||||
if (ctx != NULL) { | if (ctx != NULL) { | ||||
secp256k1_ecmult_context_clear(&ctx->ecmult_ctx); | secp256k1_ecmult_context_clear(&ctx->ecmult_ctx); | ||||
secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx); | secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx); | ||||
} | |||||
} | |||||
void secp256k1_context_destroy(secp256k1_context* ctx) { | |||||
if (ctx != NULL) { | |||||
secp256k1_context_preallocated_destroy(ctx); | |||||
free(ctx); | free(ctx); | ||||
} | } | ||||
} | } | ||||
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) { | void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) { | ||||
CHECK(ctx != secp256k1_context_no_precomp); | CHECK(ctx != secp256k1_context_no_precomp); | ||||
if (fun == NULL) { | if (fun == NULL) { | ||||
fun = default_illegal_callback_fn; | fun = default_illegal_callback_fn; | ||||
▲ Show 20 Lines • Show All 505 Lines • Show Last 20 Lines |