9#include "../include/secp256k1.h"
21static void help(
char **argv) {
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");
62 data->offset1 = (x * 0x537b7f6f + 0x8f66a481) %
POINTS;
63 data->offset2 = (x * 0x7f6f537b + 0x6a1a8f49) %
POINTS;
75 for (i = 0; i < iters; ++i) {
77 if (scalar_gen_offset != NULL) {
80 if (seckey_offset != NULL) {
101 for (i = 0; i < iters; ++i) {
115 for (i = 0; i < iters; ++i) {
129 for (i = 0; i < iters; ++i) {
143 for (i = 0; i < iters; ++i) {
157 for (i = 0; i < iters/2; ++i) {
169 sprintf(str,
"ecmult_gen");
171 sprintf(str,
"ecmult_const");
174 sprintf(str,
"ecmult_1p");
177 sprintf(str,
"ecmult_0p_g");
180 sprintf(str,
"ecmult_1p_g");
186 if (
data->includes_g) ++idx;
200 int includes_g =
data->includes_g;
203 iters = iters /
data->count;
205 for (iter = 0; iter < iters; ++iter) {
220 iters = iters /
data->count;
222 for (iter = 0; iter < iters; ++iter) {
231 unsigned char c[10] = {
'e',
'c',
'm',
'u',
'l',
't', 0, 0, 0, 0};
232 unsigned char buf[32];
247 size_t iters = 1 + num_iters /
count;
251 data->includes_g = includes_g;
255 for (iter = 0; iter < iters; ++iter) {
259 for (i = 0; i + 1 <
count; ++i) {
269 sprintf(str,
"ecmult_multi_%ip_g", (
int)
count - 1);
271 sprintf(str,
"ecmult_multi_%ip", (
int)
count);
276int main(
int argc,
char **argv) {
291 }
else if(
have_flag(argc, argv,
"pippenger_wnaf")) {
292 printf(
"Using pippenger_wnaf:\n");
294 }
else if(
have_flag(argc, argv,
"strauss_wnaf")) {
295 printf(
"Using strauss_wnaf:\n");
297 }
else if(
have_flag(argc, argv,
"simple")) {
298 printf(
"Using simple algorithm:\n");
300 fprintf(stderr,
"%s: unrecognized argument '%s'.\n\n", argv[0], argv[1]);
325 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) {
355 if (
data.scratch != NULL) {
361 free(
data.pubkeys_gej);
364 free(
data.expected_output);
static void bench_ecmult_const(void *arg, int iters)
static void bench_ecmult_gen_teardown(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_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_1p_g_teardown(void *arg, int iters)
static void bench_ecmult_1p(void *arg, int iters)
static void bench_ecmult_1p_teardown(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 help(char **argv)
static void bench_ecmult_0p_g_teardown(void *arg, int iters)
static void run_ecmult_bench(bench_data *data, int iters)
static void bench_ecmult_1p_g(void *arg, int iters)
static void bench_ecmult_0p_g(void *arg, int iters)
static void bench_ecmult_multi_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 run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
static int secp256k1_ecmult_multi_var(const secp256k1_callback *error_callback, 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(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
Double multiply: R = na*A + ng*G.
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *q)
Multiply: R = q*A (in constant-time for q)
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_strauss_batch_single(const secp256k1_callback *error_callback, 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)
static int secp256k1_ecmult_pippenger_batch_single(const secp256k1_callback *error_callback, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n)
int(* secp256k1_ecmult_multi_func)(const secp256k1_callback *error_callback, secp256k1_scratch *, secp256k1_gej *, const secp256k1_scalar *, secp256k1_ecmult_multi_callback cb, void *, size_t)
static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b)
Check two group elements (jacobian) for equality in variable time.
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 const secp256k1_ge secp256k1_ge_const_g
#define CHECK(cond)
Unconditional failure on condition failure.
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 const secp256k1_scalar secp256k1_scalar_zero
static int get_iters(int default_iters)
static void print_output_table_header_row(void)
static int have_flag(int argc, char **argv, char *flag)
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)
static void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space *scratch)
static secp256k1_scratch_space * secp256k1_scratch_space_create(const secp256k1_context *ctx, size_t max_size)
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
secp256k1_scalar * seckeys
secp256k1_gej * pubkeys_gej
secp256k1_ecmult_multi_func ecmult_multi
secp256k1_scratch_space * scratch
secp256k1_scalar * scalars
secp256k1_gej * expected_output
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
A group element of the secp256k1 curve, in jacobian coordinates.
A scalar modulo the group order of the secp256k1 curve.