Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/ecmult_gen_impl.h
/********************************************************************** | /********************************************************************** | ||||
* Copyright (c) 2013, 2014, 2015 Pieter Wuille, Gregory Maxwell * | * Copyright (c) 2013, 2014, 2015 Pieter Wuille, Gregory Maxwell * | ||||
* 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.* | ||||
**********************************************************************/ | **********************************************************************/ | ||||
#ifndef SECP256K1_ECMULT_GEN_IMPL_H | #ifndef SECP256K1_ECMULT_GEN_IMPL_H | ||||
#define SECP256K1_ECMULT_GEN_IMPL_H | #define SECP256K1_ECMULT_GEN_IMPL_H | ||||
#include "util.h" | |||||
#include "scalar.h" | #include "scalar.h" | ||||
#include "group.h" | #include "group.h" | ||||
#include "ecmult_gen.h" | #include "ecmult_gen.h" | ||||
#include "hash_impl.h" | #include "hash_impl.h" | ||||
#ifdef USE_ECMULT_STATIC_PRECOMPUTATION | #ifdef USE_ECMULT_STATIC_PRECOMPUTATION | ||||
#include "ecmult_static_context.h" | #include "ecmult_static_context.h" | ||||
#endif | #endif | ||||
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION | |||||
static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE = ROUND_TO_ALIGN(sizeof(*((secp256k1_ecmult_gen_context*) NULL)->prec)); | |||||
#else | |||||
static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE = 0; | |||||
#endif | |||||
static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context *ctx) { | static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context *ctx) { | ||||
ctx->prec = NULL; | ctx->prec = NULL; | ||||
} | } | ||||
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, const secp256k1_callback* cb) { | static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, void **prealloc) { | ||||
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION | #ifndef USE_ECMULT_STATIC_PRECOMPUTATION | ||||
secp256k1_ge prec[1024]; | secp256k1_ge prec[1024]; | ||||
secp256k1_gej gj; | secp256k1_gej gj; | ||||
secp256k1_gej nums_gej; | secp256k1_gej nums_gej; | ||||
int i, j; | int i, j; | ||||
size_t const prealloc_size = SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE; | |||||
void* const base = *prealloc; | |||||
#endif | #endif | ||||
if (ctx->prec != NULL) { | if (ctx->prec != NULL) { | ||||
return; | return; | ||||
} | } | ||||
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION | #ifndef USE_ECMULT_STATIC_PRECOMPUTATION | ||||
ctx->prec = (secp256k1_ge_storage (*)[64][16])checked_malloc(cb, sizeof(*ctx->prec)); | ctx->prec = (secp256k1_ge_storage (*)[64][16])manual_alloc(prealloc, prealloc_size, base, prealloc_size); | ||||
/* get the generator */ | /* get the generator */ | ||||
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g); | secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g); | ||||
/* Construct a group element with no known corresponding scalar (nothing up my sleeve). */ | /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */ | ||||
{ | { | ||||
static const unsigned char nums_b32[33] = "The scalar for this x is unknown"; | static const unsigned char nums_b32[33] = "The scalar for this x is unknown"; | ||||
secp256k1_fe nums_x; | secp256k1_fe nums_x; | ||||
Show All 38 Lines | /* compute prec. */ | ||||
secp256k1_ge_set_all_gej_var(prec, precj, 1024); | secp256k1_ge_set_all_gej_var(prec, precj, 1024); | ||||
} | } | ||||
for (j = 0; j < 64; j++) { | for (j = 0; j < 64; j++) { | ||||
for (i = 0; i < 16; i++) { | for (i = 0; i < 16; i++) { | ||||
secp256k1_ge_to_storage(&(*ctx->prec)[j][i], &prec[j*16 + i]); | secp256k1_ge_to_storage(&(*ctx->prec)[j][i], &prec[j*16 + i]); | ||||
} | } | ||||
} | } | ||||
#else | #else | ||||
(void)cb; | (void)prealloc; | ||||
ctx->prec = (secp256k1_ge_storage (*)[64][16])secp256k1_ecmult_static_context; | ctx->prec = (secp256k1_ge_storage (*)[64][16])secp256k1_ecmult_static_context; | ||||
#endif | #endif | ||||
secp256k1_ecmult_gen_blind(ctx, NULL); | secp256k1_ecmult_gen_blind(ctx, NULL); | ||||
} | } | ||||
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context* ctx) { | static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context* ctx) { | ||||
return ctx->prec != NULL; | return ctx->prec != NULL; | ||||
} | } | ||||
static void secp256k1_ecmult_gen_context_clone(secp256k1_ecmult_gen_context *dst, | static void secp256k1_ecmult_gen_context_finalize_memcpy(secp256k1_ecmult_gen_context *dst, const secp256k1_ecmult_gen_context *src) { | ||||
const secp256k1_ecmult_gen_context *src, const secp256k1_callback* cb) { | |||||
if (src->prec == NULL) { | |||||
dst->prec = NULL; | |||||
} else { | |||||
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION | #ifndef USE_ECMULT_STATIC_PRECOMPUTATION | ||||
dst->prec = (secp256k1_ge_storage (*)[64][16])checked_malloc(cb, sizeof(*dst->prec)); | if (src->prec != NULL) { | ||||
memcpy(dst->prec, src->prec, sizeof(*dst->prec)); | /* We cast to void* first to suppress a -Wcast-align warning. */ | ||||
dst->prec = (secp256k1_ge_storage (*)[64][16])(void*)((unsigned char*)dst + ((unsigned char*)src->prec - (unsigned char*)src)); | |||||
} | |||||
#else | #else | ||||
(void)cb; | (void)dst, (void)src; | ||||
dst->prec = src->prec; | |||||
#endif | #endif | ||||
dst->initial = src->initial; | |||||
dst->blind = src->blind; | |||||
} | |||||
} | } | ||||
static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx) { | static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx) { | ||||
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION | |||||
free(ctx->prec); | |||||
#endif | |||||
secp256k1_scalar_clear(&ctx->blind); | secp256k1_scalar_clear(&ctx->blind); | ||||
secp256k1_gej_clear(&ctx->initial); | secp256k1_gej_clear(&ctx->initial); | ||||
ctx->prec = NULL; | ctx->prec = NULL; | ||||
} | } | ||||
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *gn) { | static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *gn) { | ||||
secp256k1_ge add; | secp256k1_ge add; | ||||
secp256k1_ge_storage adds; | secp256k1_ge_storage adds; | ||||
▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines |