2020-12-17 08:33:49 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2013, 2014, 2017 Pieter Wuille, Andrew Poelstra *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2013-05-09 15:24:32 +02:00
|
|
|
|
2017-08-26 18:44:21 +03:00
|
|
|
#ifndef SECP256K1_ECMULT_H
|
|
|
|
#define SECP256K1_ECMULT_H
|
2013-03-08 02:52:50 +01:00
|
|
|
|
2013-03-31 17:02:52 +02:00
|
|
|
#include "group.h"
|
2017-08-16 14:45:27 -07:00
|
|
|
#include "scalar.h"
|
|
|
|
#include "scratch.h"
|
2013-03-08 02:52:50 +01:00
|
|
|
|
2021-06-28 16:33:03 -04:00
|
|
|
/* Noone will ever need more than a window size of 24. The code might
|
|
|
|
* be correct for larger values of ECMULT_WINDOW_SIZE but this is not
|
|
|
|
* not tested.
|
|
|
|
*
|
|
|
|
* The following limitations are known, and there are probably more:
|
|
|
|
* If WINDOW_G > 27 and size_t has 32 bits, then the code is incorrect
|
|
|
|
* because the size of the memory object that we allocate (in bytes)
|
|
|
|
* will not fit in a size_t.
|
|
|
|
* If WINDOW_G > 31 and int has 32 bits, then the code is incorrect
|
|
|
|
* because certain expressions will overflow.
|
|
|
|
*/
|
|
|
|
#if ECMULT_WINDOW_SIZE < 2 || ECMULT_WINDOW_SIZE > 24
|
|
|
|
# error Set ECMULT_WINDOW_SIZE to an integer in range [2..24].
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** The number of entries a table with precomputed multiples needs to have. */
|
|
|
|
#define ECMULT_TABLE_SIZE(w) (1L << ((w)-2))
|
|
|
|
|
2015-02-03 17:27:00 -08:00
|
|
|
typedef struct {
|
|
|
|
/* For accelerating the computation of a*P + b*G: */
|
2015-09-21 20:57:54 +02:00
|
|
|
secp256k1_ge_storage (*pre_g)[]; /* odd multiples of the generator */
|
|
|
|
secp256k1_ge_storage (*pre_g_128)[]; /* odd multiples of 2^128*generator */
|
|
|
|
} secp256k1_ecmult_context;
|
2015-02-03 17:27:00 -08:00
|
|
|
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx);
|
2018-10-22 16:25:26 +02:00
|
|
|
static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, void **prealloc);
|
|
|
|
static void secp256k1_ecmult_context_finalize_memcpy(secp256k1_ecmult_context *dst, const secp256k1_ecmult_context *src);
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context *ctx);
|
|
|
|
static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context *ctx);
|
2013-04-01 06:29:30 +02:00
|
|
|
|
|
|
|
/** Double multiply: R = na*A + ng*G */
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_ecmult(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng);
|
2013-03-09 22:47:40 +01:00
|
|
|
|
2017-08-16 14:45:27 -07:00
|
|
|
typedef int (secp256k1_ecmult_multi_callback)(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *data);
|
|
|
|
|
2017-09-14 17:55:13 +02:00
|
|
|
/**
|
|
|
|
* Multi-multiply: R = inp_g_sc * G + sum_i ni * Ai.
|
2017-11-05 19:40:18 +00:00
|
|
|
* Chooses the right algorithm for a given number of points and scratch space
|
|
|
|
* size. Resets and overwrites the given scratch space. If the points do not
|
|
|
|
* fit in the scratch space the algorithm is repeatedly run with batches of
|
2018-12-20 20:48:19 +00:00
|
|
|
* points. If no scratch space is given then a simple algorithm is used that
|
|
|
|
* simply multiplies the points with the corresponding scalars and adds them up.
|
2017-09-14 17:55:13 +02:00
|
|
|
* Returns: 1 on success (including when inp_g_sc is NULL and n is 0)
|
|
|
|
* 0 if there is not enough scratch space for a single point or
|
|
|
|
* callback returns 0
|
|
|
|
*/
|
2019-03-13 23:30:51 +00:00
|
|
|
static int secp256k1_ecmult_multi_var(const secp256k1_callback* error_callback, const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n);
|
2017-08-16 14:45:27 -07:00
|
|
|
|
2017-08-26 18:44:21 +03:00
|
|
|
#endif /* SECP256K1_ECMULT_H */
|