9#include "../include/secp256k1.h"
22 printf(
"Benchmark EC multiplication algorithms\n");
24 printf(
"Usage: %s <help|pippenger_wnaf|strauss_wnaf|simple>\n", argv[0]);
25 printf(
"The output shows the number of multiplied and summed points right after the\n");
26 printf(
"function name. The letter 'g' indicates that one of the points is the generator.\n");
27 printf(
"The benchmarks are divided by the number of points.\n");
29 printf(
"default (ecmult_multi): picks pippenger_wnaf or strauss_wnaf depending on the\n");
31 printf(
"pippenger_wnaf: for all batch sizes\n");
32 printf(
"strauss_wnaf: for all batch sizes\n");
33 printf(
"simple: multiply and sum each point individually\n");
75 for (i = 0; i < iters; ++i) {
77 if (scalar_gen_offset != NULL) {
80 if (seckey_offset != NULL) {
103 for (i = 0; i < iters; ++i) {
117 for (i = 0; i < iters; ++i) {
131 for (i = 0; i < iters; ++i) {
147 for (i = 0; i < iters; ++i) {
161 for (i = 0; i < iters/2; ++i) {
173 sprintf(str,
"ecmult_gen");
175 sprintf(str,
"ecmult_const");
178 sprintf(str,
"ecmult 1");
181 sprintf(str,
"ecmult 1g");
184 sprintf(str,
"ecmult 2g");
207 iters = iters / data->
count;
209 for (iter = 0; iter < iters; ++iter) {
224 iters = iters / data->
count;
226 for (iter = 0; iter < iters; ++iter) {
235 unsigned char c[10] = {
'e',
'c',
'm',
'u',
'l',
't', 0, 0, 0, 0};
236 unsigned char buf[32];
252 size_t iters = 1 + num_iters /
count;
260 for (iter = 0; iter < iters; ++iter) {
264 for (i = 0; i + 1 <
count; ++i) {
273 sprintf(str, includes_g ?
"ecmult_multi %ig" :
"ecmult_multi %i", (
int)
count);
277int main(
int argc,
char **argv) {
292 }
else if(
have_flag(argc, argv,
"pippenger_wnaf")) {
293 printf(
"Using pippenger_wnaf:\n");
295 }
else if(
have_flag(argc, argv,
"strauss_wnaf")) {
296 printf(
"Using strauss_wnaf:\n");
298 }
else if(
have_flag(argc, argv,
"simple")) {
299 printf(
"Using simple algorithm:\n");
301 fprintf(stderr,
"%s: unrecognized argument '%s'.\n\n", argv[0], argv[1]);
326 for (i = 0; i <
POINTS; ++i) {
340 for (i = 1; i <= 8; ++i) {
348 for (p = 0; p <= 11; ++p) {
349 for (i = 9; i <= 16; ++i) {
static void bench_ecmult_const(void *arg, int iters)
static void bench_ecmult_gen_teardown(void *arg, int iters)
static void bench_ecmult_2g(void *arg, int iters)
static int bench_ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *ge, size_t idx, void *arg)
static void bench_ecmult_teardown_helper(bench_data *data, size_t *seckey_offset, size_t *scalar_offset, size_t *scalar_gen_offset, int iters)
int main(int argc, char **argv)
static void bench_ecmult_setup(void *arg)
static void bench_ecmult_1_teardown(void *arg, int iters)
static void bench_ecmult_1(void *arg, int iters)
static void bench_ecmult_gen(void *arg, int iters)
static void generate_scalar(uint32_t num, secp256k1_scalar *scalar)
static void bench_ecmult_const_teardown(void *arg, int iters)
static void bench_ecmult_multi_setup(void *arg)
static void bench_ecmult_1g(void *arg, int iters)
static void bench_ecmult_multi(void *arg, int iters)
static void hash_into_offset(bench_data *data, size_t x)
static void run_ecmult_bench(bench_data *data, int iters)
static void bench_ecmult_multi_teardown(void *arg, int iters)
static void bench_ecmult_1g_teardown(void *arg, int iters)
static void run_ecmult_multi_bench(bench_data *data, size_t count, int includes_g, int num_iters)
static void bench_ecmult_2g_teardown(void *arg, int iters)
static void secp256k1_ecmult(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
Double multiply: R = na*A + ng*G.
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)
Multi-multiply: R = inp_g_sc * G + sum_i ni * Ai.
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *q, int bits)
Multiply: R = q*A (in constant-time) Here bits should be set to the maximum bitlength of the absolute...
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *a)
Multiply with the generator: R = a*G.
#define STRAUSS_SCRATCH_OBJECTS
static int secp256k1_ecmult_pippenger_batch_single(const secp256k1_callback *error_callback, const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n)
static size_t secp256k1_strauss_scratch_size(size_t n_points)
int(* secp256k1_ecmult_multi_func)(const secp256k1_callback *error_callback, const secp256k1_ecmult_context *, secp256k1_scratch *, secp256k1_gej *, const secp256k1_scalar *, secp256k1_ecmult_multi_callback cb, void *, size_t)
static int secp256k1_ecmult_strauss_batch_single(const secp256k1_callback *error_callback, const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n)
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Set r equal to the double of a.
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Set a group element (jacobian) equal to the point at infinity.
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Check whether a group element is the point at infinity.
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b.
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Set a batch of group elements equal to the inputs given in jacobian coordinates.
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
static const secp256k1_ge secp256k1_ge_const_g
Generator for secp256k1, value 'g' defined in "Standards for Efficient Cryptography" (SEC2) 2....
Internal SHA-256 implementation.
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
Set a scalar to an unsigned integer.
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Add two scalars together (modulo the group order).
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Multiply two scalars (modulo the group order).
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static void secp256k1_scalar_clear(secp256k1_scalar *r)
Clear a scalar to prevent the leak of sensitive data.
#define SECP256K1_SCALAR_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
int have_flag(int argc, char **argv, char *flag)
int get_iters(int default_iters)
void run_benchmark(char *name, void(*benchmark)(void *, int), void(*setup)(void *), void(*teardown)(void *, int), void *data, int count, int iter)
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash)
static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t size)
#define SECP256K1_CONTEXT_SIGN
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space * secp256k1_scratch_space_create(const secp256k1_context *ctx, size_t size) SECP256K1_ARG_NONNULL(1)
Create a secp256k1 scratch space object.
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
SECP256K1_API void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space *scratch) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 scratch space.
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
secp256k1_scalar * seckeys
secp256k1_gej * pubkeys_gej
secp256k1_ecmult_multi_func ecmult_multi
secp256k1_scratch_space * scratch
secp256k1_scalar * scalars
secp256k1_gej * expected_output
secp256k1_callback error_callback
secp256k1_ecmult_gen_context ecmult_gen_ctx
secp256k1_ecmult_context ecmult_ctx
A group element of the secp256k1 curve, in affine coordinates.
A group element of the secp256k1 curve, in jacobian coordinates.
A scalar modulo the group order of the secp256k1 curve.