Bitcoin ABC 0.32.12
P2P Digital Currency
bench_ecmult.c
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2017 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
6#include <stdio.h>
7
8#include "secp256k1.c"
9
10#include "include/secp256k1.h"
11#include "util.h"
12#include "hash_impl.h"
13#include "field_impl.h"
14#include "group_impl.h"
15#include "scalar_impl.h"
16#include "ecmult_impl.h"
17#include "bench.h"
18
19#define POINTS 32768
20
21void help(char **argv) {
22 printf("Benchmark EC multiplication algorithms\n");
23 printf("\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");
28 printf("\n");
29 printf("default (ecmult_multi): picks pippenger_wnaf or strauss_wnaf depending on the\n");
30 printf(" batch size\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");
34}
35
36typedef struct {
37 /* Setup once in advance */
46
47 /* Changes per benchmark */
48 size_t count;
50
51 /* Changes per benchmark iteration, used to pick different scalars and pubkeys
52 * in each run. */
53 size_t offset1;
54 size_t offset2;
55
56 /* Benchmark output. */
59
60/* Hashes x into [0, POINTS) twice and store the result in offset1 and offset2. */
61static void hash_into_offset(bench_data* data, size_t x) {
62 data->offset1 = (x * 0x537b7f6f + 0x8f66a481) % POINTS;
63 data->offset2 = (x * 0x7f6f537b + 0x6a1a8f49) % POINTS;
64}
65
66/* Check correctness of the benchmark by computing
67 * sum(outputs) ?= (sum(scalars_gen) + sum(seckeys)*sum(scalars))*G */
68static void bench_ecmult_teardown_helper(bench_data* data, size_t* seckey_offset, size_t* scalar_offset, size_t* scalar_gen_offset, int iters) {
69 int i;
70 secp256k1_gej sum_output, tmp;
71 secp256k1_scalar sum_scalars;
72
73 secp256k1_gej_set_infinity(&sum_output);
74 secp256k1_scalar_clear(&sum_scalars);
75 for (i = 0; i < iters; ++i) {
76 secp256k1_gej_add_var(&sum_output, &sum_output, &data->output[i], NULL);
77 if (scalar_gen_offset != NULL) {
78 secp256k1_scalar_add(&sum_scalars, &sum_scalars, &data->scalars[(*scalar_gen_offset+i) % POINTS]);
79 }
80 if (seckey_offset != NULL) {
81 secp256k1_scalar s = data->seckeys[(*seckey_offset+i) % POINTS];
82 secp256k1_scalar_mul(&s, &s, &data->scalars[(*scalar_offset+i) % POINTS]);
83 secp256k1_scalar_add(&sum_scalars, &sum_scalars, &s);
84 }
85 }
86 secp256k1_ecmult_gen(&data->ctx->ecmult_gen_ctx, &tmp, &sum_scalars);
87 CHECK(secp256k1_gej_eq_var(&tmp, &sum_output));
88}
89
90static void bench_ecmult_setup(void* arg) {
91 bench_data* data = (bench_data*)arg;
92 /* Re-randomize offset to ensure that we're using different scalars and
93 * group elements in each run. */
94 hash_into_offset(data, data->offset1);
95}
96
97static void bench_ecmult_gen(void* arg, int iters) {
98 bench_data* data = (bench_data*)arg;
99 int i;
100
101 for (i = 0; i < iters; ++i) {
102 secp256k1_ecmult_gen(&data->ctx->ecmult_gen_ctx, &data->output[i], &data->scalars[(data->offset1+i) % POINTS]);
103 }
104}
105
106static void bench_ecmult_gen_teardown(void* arg, int iters) {
107 bench_data* data = (bench_data*)arg;
108 bench_ecmult_teardown_helper(data, NULL, NULL, &data->offset1, iters);
109}
110
111static void bench_ecmult_const(void* arg, int iters) {
112 bench_data* data = (bench_data*)arg;
113 int i;
114
115 for (i = 0; i < iters; ++i) {
116 secp256k1_ecmult_const(&data->output[i], &data->pubkeys[(data->offset1+i) % POINTS], &data->scalars[(data->offset2+i) % POINTS], 256);
117 }
118}
119
120static void bench_ecmult_const_teardown(void* arg, int iters) {
121 bench_data* data = (bench_data*)arg;
122 bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, NULL, iters);
123}
124
125static void bench_ecmult_1p(void* arg, int iters) {
126 bench_data* data = (bench_data*)arg;
127 int i;
128
129 for (i = 0; i < iters; ++i) {
130 secp256k1_ecmult(&data->output[i], &data->pubkeys_gej[(data->offset1+i) % POINTS], &data->scalars[(data->offset2+i) % POINTS], NULL);
131 }
132}
133
134static void bench_ecmult_1p_teardown(void* arg, int iters) {
135 bench_data* data = (bench_data*)arg;
136 bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, NULL, iters);
137}
138
139static void bench_ecmult_0p_g(void* arg, int iters) {
140 bench_data* data = (bench_data*)arg;
141 secp256k1_scalar zero;
142 int i;
143
144 secp256k1_scalar_set_int(&zero, 0);
145 for (i = 0; i < iters; ++i) {
146 secp256k1_ecmult(&data->output[i], NULL, &zero, &data->scalars[(data->offset1+i) % POINTS]);
147 }
148}
149
150static void bench_ecmult_0p_g_teardown(void* arg, int iters) {
151 bench_data* data = (bench_data*)arg;
152 bench_ecmult_teardown_helper(data, NULL, NULL, &data->offset1, iters);
153}
154
155static void bench_ecmult_1p_g(void* arg, int iters) {
156 bench_data* data = (bench_data*)arg;
157 int i;
158
159 for (i = 0; i < iters/2; ++i) {
160 secp256k1_ecmult(&data->output[i], &data->pubkeys_gej[(data->offset1+i) % POINTS], &data->scalars[(data->offset2+i) % POINTS], &data->scalars[(data->offset1+i) % POINTS]);
161 }
162}
163
164static void bench_ecmult_1p_g_teardown(void* arg, int iters) {
165 bench_data* data = (bench_data*)arg;
166 bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, &data->offset1, iters/2);
167}
168
169static void run_ecmult_bench(bench_data* data, int iters) {
170 char str[32];
171 sprintf(str, "ecmult_gen");
173 sprintf(str, "ecmult_const");
175 /* ecmult with non generator point */
176 sprintf(str, "ecmult_1p");
178 /* ecmult with generator point */
179 sprintf(str, "ecmult_0p_g");
181 /* ecmult with generator and non-generator point. The reported time is per point. */
182 sprintf(str, "ecmult_1p_g");
184}
185
186static int bench_ecmult_multi_callback(secp256k1_scalar* sc, secp256k1_ge* ge, size_t idx, void* arg) {
187 bench_data* data = (bench_data*)arg;
188 if (data->includes_g) ++idx;
189 if (idx == 0) {
190 *sc = data->scalars[data->offset1];
192 } else {
193 *sc = data->scalars[(data->offset1 + idx) % POINTS];
194 *ge = data->pubkeys[(data->offset2 + idx - 1) % POINTS];
195 }
196 return 1;
197}
198
199static void bench_ecmult_multi(void* arg, int iters) {
200 bench_data* data = (bench_data*)arg;
201
202 int includes_g = data->includes_g;
203 int iter;
204 int count = data->count;
205 iters = iters / data->count;
206
207 for (iter = 0; iter < iters; ++iter) {
208 data->ecmult_multi(&data->ctx->error_callback, data->scratch, &data->output[iter], data->includes_g ? &data->scalars[data->offset1] : NULL, bench_ecmult_multi_callback, arg, count - includes_g);
209 data->offset1 = (data->offset1 + count) % POINTS;
210 data->offset2 = (data->offset2 + count - 1) % POINTS;
211 }
212}
213
214static void bench_ecmult_multi_setup(void* arg) {
215 bench_data* data = (bench_data*)arg;
216 hash_into_offset(data, data->count);
217}
218
219static void bench_ecmult_multi_teardown(void* arg, int iters) {
220 bench_data* data = (bench_data*)arg;
221 int iter;
222 iters = iters / data->count;
223 /* Verify the results in teardown, to avoid doing comparisons while benchmarking. */
224 for (iter = 0; iter < iters; ++iter) {
225 secp256k1_gej tmp;
226 secp256k1_gej_add_var(&tmp, &data->output[iter], &data->expected_output[iter], NULL);
228 }
229}
230
231static void generate_scalar(uint32_t num, secp256k1_scalar* scalar) {
233 unsigned char c[10] = {'e', 'c', 'm', 'u', 'l', 't', 0, 0, 0, 0};
234 unsigned char buf[32];
235 int overflow = 0;
236 c[6] = num;
237 c[7] = num >> 8;
238 c[8] = num >> 16;
239 c[9] = num >> 24;
241 secp256k1_sha256_write(&sha256, c, sizeof(c));
243 secp256k1_scalar_set_b32(scalar, buf, &overflow);
244 CHECK(!overflow);
245}
246
247static void run_ecmult_multi_bench(bench_data* data, size_t count, int includes_g, int num_iters) {
248 char str[32];
249 static const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
250 size_t iters = 1 + num_iters / count;
251 size_t iter;
252
253 data->count = count;
254 data->includes_g = includes_g;
255
256 /* Compute (the negation of) the expected results directly. */
257 hash_into_offset(data, data->count);
258 for (iter = 0; iter < iters; ++iter) {
260 secp256k1_scalar total = data->scalars[(data->offset1++) % POINTS];
261 size_t i = 0;
262 for (i = 0; i + 1 < count; ++i) {
263 secp256k1_scalar_mul(&tmp, &data->seckeys[(data->offset2++) % POINTS], &data->scalars[(data->offset1++) % POINTS]);
264 secp256k1_scalar_add(&total, &total, &tmp);
265 }
266 secp256k1_scalar_negate(&total, &total);
267 secp256k1_ecmult(&data->expected_output[iter], NULL, &zero, &total);
268 }
269
270 /* Run the benchmark. */
271 if (includes_g) {
272 sprintf(str, "ecmult_multi_%ip_g", (int)count - 1);
273 } else {
274 sprintf(str, "ecmult_multi_%ip", (int)count);
275 }
277}
278
279int main(int argc, char **argv) {
280 bench_data data;
281 int i, p;
282 size_t scratch_size;
283
284 int iters = get_iters(10000);
285
287
288 if (argc > 1) {
289 if(have_flag(argc, argv, "-h")
290 || have_flag(argc, argv, "--help")
291 || have_flag(argc, argv, "help")) {
292 help(argv);
293 return 0;
294 } else if(have_flag(argc, argv, "pippenger_wnaf")) {
295 printf("Using pippenger_wnaf:\n");
297 } else if(have_flag(argc, argv, "strauss_wnaf")) {
298 printf("Using strauss_wnaf:\n");
300 } else if(have_flag(argc, argv, "simple")) {
301 printf("Using simple algorithm:\n");
302 } else {
303 fprintf(stderr, "%s: unrecognized argument '%s'.\n\n", argv[0], argv[1]);
304 help(argv);
305 return 1;
306 }
307 }
308
311 if (!have_flag(argc, argv, "simple")) {
312 data.scratch = secp256k1_scratch_space_create(data.ctx, scratch_size);
313 } else {
314 data.scratch = NULL;
315 }
316
317 /* Allocate stuff */
318 data.scalars = malloc(sizeof(secp256k1_scalar) * POINTS);
319 data.seckeys = malloc(sizeof(secp256k1_scalar) * POINTS);
320 data.pubkeys = malloc(sizeof(secp256k1_ge) * POINTS);
321 data.pubkeys_gej = malloc(sizeof(secp256k1_gej) * POINTS);
322 data.expected_output = malloc(sizeof(secp256k1_gej) * (iters + 1));
323 data.output = malloc(sizeof(secp256k1_gej) * (iters + 1));
324
325 /* Generate a set of scalars, and private/public keypairs. */
328 for (i = 0; i < POINTS; ++i) {
329 generate_scalar(i, &data.scalars[i]);
330 if (i) {
331 secp256k1_gej_double_var(&data.pubkeys_gej[i], &data.pubkeys_gej[i - 1], NULL);
332 secp256k1_scalar_add(&data.seckeys[i], &data.seckeys[i - 1], &data.seckeys[i - 1]);
333 }
334 }
336
337
339 /* Initialize offset1 and offset2 */
340 hash_into_offset(&data, 0);
341 run_ecmult_bench(&data, iters);
342
343 for (i = 1; i <= 8; ++i) {
344 run_ecmult_multi_bench(&data, i, 1, iters);
345 }
346
347 /* This is disabled with low count of iterations because the loop runs 77 times even with iters=1
348 * and the higher it goes the longer the computation takes(more points)
349 * So we don't run this benchmark with low iterations to prevent slow down */
350 if (iters > 2) {
351 for (p = 0; p <= 11; ++p) {
352 for (i = 9; i <= 16; ++i) {
353 run_ecmult_multi_bench(&data, i << p, 1, iters);
354 }
355 }
356 }
357
358 if (data.scratch != NULL) {
360 }
362 free(data.scalars);
363 free(data.pubkeys);
364 free(data.pubkeys_gej);
365 free(data.seckeys);
366 free(data.output);
367 free(data.expected_output);
368
369 return(0);
370}
static void bench_ecmult_const(void *arg, int iters)
Definition: bench_ecmult.c:111
static void bench_ecmult_gen_teardown(void *arg, int iters)
Definition: bench_ecmult.c:106
static int bench_ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *ge, size_t idx, void *arg)
Definition: bench_ecmult.c:186
static void bench_ecmult_teardown_helper(bench_data *data, size_t *seckey_offset, size_t *scalar_offset, size_t *scalar_gen_offset, int iters)
Definition: bench_ecmult.c:68
int main(int argc, char **argv)
Definition: bench_ecmult.c:279
static void bench_ecmult_setup(void *arg)
Definition: bench_ecmult.c:90
static void bench_ecmult_gen(void *arg, int iters)
Definition: bench_ecmult.c:97
static void generate_scalar(uint32_t num, secp256k1_scalar *scalar)
Definition: bench_ecmult.c:231
void help(char **argv)
Definition: bench_ecmult.c:21
static void bench_ecmult_const_teardown(void *arg, int iters)
Definition: bench_ecmult.c:120
static void bench_ecmult_multi_setup(void *arg)
Definition: bench_ecmult.c:214
static void bench_ecmult_1p_g_teardown(void *arg, int iters)
Definition: bench_ecmult.c:164
static void bench_ecmult_1p(void *arg, int iters)
Definition: bench_ecmult.c:125
static void bench_ecmult_1p_teardown(void *arg, int iters)
Definition: bench_ecmult.c:134
static void bench_ecmult_multi(void *arg, int iters)
Definition: bench_ecmult.c:199
static void hash_into_offset(bench_data *data, size_t x)
Definition: bench_ecmult.c:61
static void bench_ecmult_0p_g_teardown(void *arg, int iters)
Definition: bench_ecmult.c:150
static void run_ecmult_bench(bench_data *data, int iters)
Definition: bench_ecmult.c:169
static void bench_ecmult_1p_g(void *arg, int iters)
Definition: bench_ecmult.c:155
static void bench_ecmult_0p_g(void *arg, int iters)
Definition: bench_ecmult.c:139
static void bench_ecmult_multi_teardown(void *arg, int iters)
Definition: bench_ecmult.c:219
#define POINTS
Definition: bench_ecmult.c:19
static void run_ecmult_multi_bench(bench_data *data, size_t count, int includes_g, int num_iters)
Definition: bench_ecmult.c:247
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, 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
Definition: ecmult_impl.h:50
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)
Definition: ecmult_impl.h:401
static size_t secp256k1_strauss_scratch_size(size_t n_points)
Definition: ecmult_impl.h:356
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)
Definition: ecmult_impl.h:726
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)
Definition: ecmult_impl.h:816
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
Definition: group_impl.h:62
Internal SHA-256 implementation.
Definition: sha256.cpp:40
void printf(const char *fmt, const Args &...args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1126
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)
Definition: scalar_4x64.h:17
void print_output_table_header_row(void)
Definition: bench.h:179
int have_flag(int argc, char **argv, char *flag)
Definition: bench.h:132
int get_iters(int default_iters)
Definition: bench.h:170
void run_benchmark(char *name, void(*benchmark)(void *, int), void(*setup)(void *), void(*teardown)(void *, int), void *data, int count, int iter)
Definition: bench.h:98
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 CHECK(cond)
Definition: util.h:81
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:176
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.
Definition: secp256k1.c:207
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:137
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
Definition: secp256k1.h:203
SECP256K1_API void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space *scratch) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 scratch space.
Definition: secp256k1.c:212
secp256k1_scalar * seckeys
Definition: bench_ecmult.c:43
secp256k1_gej * output
Definition: bench_ecmult.c:57
secp256k1_gej * pubkeys_gej
Definition: bench_ecmult.c:42
size_t offset2
Definition: bench_ecmult.c:54
secp256k1_ecmult_multi_func ecmult_multi
Definition: bench_ecmult.c:45
int includes_g
Definition: bench_ecmult.c:49
size_t offset1
Definition: bench_ecmult.c:53
secp256k1_scratch_space * scratch
Definition: bench_ecmult.c:39
secp256k1_context * ctx
Definition: bench_ecmult.c:38
secp256k1_ge * pubkeys
Definition: bench_ecmult.c:41
size_t count
Definition: bench_ecmult.c:48
secp256k1_scalar * scalars
Definition: bench_ecmult.c:40
secp256k1_gej * expected_output
Definition: bench_ecmult.c:44
secp256k1_callback error_callback
Definition: secp256k1.c:66
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:64
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
static int count