Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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"
10
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 secp256k1_gej_neg(&tmp, &tmp);
88 secp256k1_gej_add_var(&tmp, &tmp, &sum_output, NULL);
89 CHECK(secp256k1_gej_is_infinity(&tmp));
90}
91
92static void bench_ecmult_setup(void* arg) {
93 bench_data* data = (bench_data*)arg;
94 /* Re-randomize offset to ensure that we're using different scalars and
95 * group elements in each run. */
96 hash_into_offset(data, data->offset1);
97}
98
99static void bench_ecmult_gen(void* arg, int iters) {
100 bench_data* data = (bench_data*)arg;
101 int i;
102
103 for (i = 0; i < iters; ++i) {
104 secp256k1_ecmult_gen(&data->ctx->ecmult_gen_ctx, &data->output[i], &data->scalars[(data->offset1+i) % POINTS]);
105 }
106}
107
108static void bench_ecmult_gen_teardown(void* arg, int iters) {
109 bench_data* data = (bench_data*)arg;
110 bench_ecmult_teardown_helper(data, NULL, NULL, &data->offset1, iters);
111}
112
113static void bench_ecmult_const(void* arg, int iters) {
114 bench_data* data = (bench_data*)arg;
115 int i;
116
117 for (i = 0; i < iters; ++i) {
118 secp256k1_ecmult_const(&data->output[i], &data->pubkeys[(data->offset1+i) % POINTS], &data->scalars[(data->offset2+i) % POINTS], 256);
119 }
120}
121
122static void bench_ecmult_const_teardown(void* arg, int iters) {
123 bench_data* data = (bench_data*)arg;
124 bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, NULL, iters);
125}
126
127static void bench_ecmult_1p(void* arg, int iters) {
128 bench_data* data = (bench_data*)arg;
129 int i;
130
131 for (i = 0; i < iters; ++i) {
132 secp256k1_ecmult(&data->output[i], &data->pubkeys_gej[(data->offset1+i) % POINTS], &data->scalars[(data->offset2+i) % POINTS], NULL);
133 }
134}
135
136static void bench_ecmult_1p_teardown(void* arg, int iters) {
137 bench_data* data = (bench_data*)arg;
138 bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, NULL, iters);
139}
140
141static void bench_ecmult_0p_g(void* arg, int iters) {
142 bench_data* data = (bench_data*)arg;
143 secp256k1_scalar zero;
144 int i;
145
146 secp256k1_scalar_set_int(&zero, 0);
147 for (i = 0; i < iters; ++i) {
148 secp256k1_ecmult(&data->output[i], NULL, &zero, &data->scalars[(data->offset1+i) % POINTS]);
149 }
150}
151
152static void bench_ecmult_0p_g_teardown(void* arg, int iters) {
153 bench_data* data = (bench_data*)arg;
154 bench_ecmult_teardown_helper(data, NULL, NULL, &data->offset1, iters);
155}
156
157static void bench_ecmult_1p_g(void* arg, int iters) {
158 bench_data* data = (bench_data*)arg;
159 int i;
160
161 for (i = 0; i < iters/2; ++i) {
162 secp256k1_ecmult(&data->output[i], &data->pubkeys_gej[(data->offset1+i) % POINTS], &data->scalars[(data->offset2+i) % POINTS], &data->scalars[(data->offset1+i) % POINTS]);
163 }
164}
165
166static void bench_ecmult_1p_g_teardown(void* arg, int iters) {
167 bench_data* data = (bench_data*)arg;
168 bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, &data->offset1, iters/2);
169}
170
171static void run_ecmult_bench(bench_data* data, int iters) {
172 char str[32];
173 sprintf(str, "ecmult_gen");
174 run_benchmark(str, bench_ecmult_gen, bench_ecmult_setup, bench_ecmult_gen_teardown, data, 10, iters);
175 sprintf(str, "ecmult_const");
176 run_benchmark(str, bench_ecmult_const, bench_ecmult_setup, bench_ecmult_const_teardown, data, 10, iters);
177 /* ecmult with non generator point */
178 sprintf(str, "ecmult_1p");
179 run_benchmark(str, bench_ecmult_1p, bench_ecmult_setup, bench_ecmult_1p_teardown, data, 10, iters);
180 /* ecmult with generator point */
181 sprintf(str, "ecmult_0p_g");
182 run_benchmark(str, bench_ecmult_0p_g, bench_ecmult_setup, bench_ecmult_0p_g_teardown, data, 10, iters);
183 /* ecmult with generator and non-generator point. The reported time is per point. */
184 sprintf(str, "ecmult_1p_g");
185 run_benchmark(str, bench_ecmult_1p_g, bench_ecmult_setup, bench_ecmult_1p_g_teardown, data, 10, 2*iters);
186}
187
188static int bench_ecmult_multi_callback(secp256k1_scalar* sc, secp256k1_ge* ge, size_t idx, void* arg) {
189 bench_data* data = (bench_data*)arg;
190 if (data->includes_g) ++idx;
191 if (idx == 0) {
192 *sc = data->scalars[data->offset1];
193 *ge = secp256k1_ge_const_g;
194 } else {
195 *sc = data->scalars[(data->offset1 + idx) % POINTS];
196 *ge = data->pubkeys[(data->offset2 + idx - 1) % POINTS];
197 }
198 return 1;
199}
200
201static void bench_ecmult_multi(void* arg, int iters) {
202 bench_data* data = (bench_data*)arg;
203
204 int includes_g = data->includes_g;
205 int iter;
206 int count = data->count;
207 iters = iters / data->count;
208
209 for (iter = 0; iter < iters; ++iter) {
210 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);
211 data->offset1 = (data->offset1 + count) % POINTS;
212 data->offset2 = (data->offset2 + count - 1) % POINTS;
213 }
214}
215
216static void bench_ecmult_multi_setup(void* arg) {
217 bench_data* data = (bench_data*)arg;
218 hash_into_offset(data, data->count);
219}
220
221static void bench_ecmult_multi_teardown(void* arg, int iters) {
222 bench_data* data = (bench_data*)arg;
223 int iter;
224 iters = iters / data->count;
225 /* Verify the results in teardown, to avoid doing comparisons while benchmarking. */
226 for (iter = 0; iter < iters; ++iter) {
227 secp256k1_gej tmp;
228 secp256k1_gej_add_var(&tmp, &data->output[iter], &data->expected_output[iter], NULL);
229 CHECK(secp256k1_gej_is_infinity(&tmp));
230 }
231}
232
233static void generate_scalar(uint32_t num, secp256k1_scalar* scalar) {
235 unsigned char c[10] = {'e', 'c', 'm', 'u', 'l', 't', 0, 0, 0, 0};
236 unsigned char buf[32];
237 int overflow = 0;
238 c[6] = num;
239 c[7] = num >> 8;
240 c[8] = num >> 16;
241 c[9] = num >> 24;
242 secp256k1_sha256_initialize(&sha256);
243 secp256k1_sha256_write(&sha256, c, sizeof(c));
244 secp256k1_sha256_finalize(&sha256, buf);
245 secp256k1_scalar_set_b32(scalar, buf, &overflow);
246 CHECK(!overflow);
247}
248
249static void run_ecmult_multi_bench(bench_data* data, size_t count, int includes_g, int num_iters) {
250 char str[32];
251 static const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
252 size_t iters = 1 + num_iters / count;
253 size_t iter;
254
255 data->count = count;
256 data->includes_g = includes_g;
257
258 /* Compute (the negation of) the expected results directly. */
259 hash_into_offset(data, data->count);
260 for (iter = 0; iter < iters; ++iter) {
262 secp256k1_scalar total = data->scalars[(data->offset1++) % POINTS];
263 size_t i = 0;
264 for (i = 0; i + 1 < count; ++i) {
265 secp256k1_scalar_mul(&tmp, &data->seckeys[(data->offset2++) % POINTS], &data->scalars[(data->offset1++) % POINTS]);
266 secp256k1_scalar_add(&total, &total, &tmp);
267 }
268 secp256k1_scalar_negate(&total, &total);
269 secp256k1_ecmult(&data->expected_output[iter], NULL, &zero, &total);
270 }
271
272 /* Run the benchmark. */
273 if (includes_g) {
274 sprintf(str, "ecmult_multi_%ip_g", (int)count - 1);
275 } else {
276 sprintf(str, "ecmult_multi_%ip", (int)count);
277 }
278 run_benchmark(str, bench_ecmult_multi, bench_ecmult_multi_setup, bench_ecmult_multi_teardown, data, 10, count * iters);
279}
280
281int main(int argc, char **argv) {
282 bench_data data;
283 int i, p;
284 size_t scratch_size;
285
286 int iters = get_iters(10000);
287
288 data.ecmult_multi = secp256k1_ecmult_multi_var;
289
290 if (argc > 1) {
291 if(have_flag(argc, argv, "-h")
292 || have_flag(argc, argv, "--help")
293 || have_flag(argc, argv, "help")) {
294 help(argv);
295 return 0;
296 } else if(have_flag(argc, argv, "pippenger_wnaf")) {
297 printf("Using pippenger_wnaf:\n");
298 data.ecmult_multi = secp256k1_ecmult_pippenger_batch_single;
299 } else if(have_flag(argc, argv, "strauss_wnaf")) {
300 printf("Using strauss_wnaf:\n");
301 data.ecmult_multi = secp256k1_ecmult_strauss_batch_single;
302 } else if(have_flag(argc, argv, "simple")) {
303 printf("Using simple algorithm:\n");
304 } else {
305 fprintf(stderr, "%s: unrecognized argument '%s'.\n\n", argv[0], argv[1]);
306 help(argv);
307 return 1;
308 }
309 }
310
312 scratch_size = secp256k1_strauss_scratch_size(POINTS) + STRAUSS_SCRATCH_OBJECTS*16;
313 if (!have_flag(argc, argv, "simple")) {
314 data.scratch = secp256k1_scratch_space_create(data.ctx, scratch_size);
315 } else {
316 data.scratch = NULL;
317 }
318
319 /* Allocate stuff */
320 data.scalars = malloc(sizeof(secp256k1_scalar) * POINTS);
321 data.seckeys = malloc(sizeof(secp256k1_scalar) * POINTS);
322 data.pubkeys = malloc(sizeof(secp256k1_ge) * POINTS);
323 data.pubkeys_gej = malloc(sizeof(secp256k1_gej) * POINTS);
324 data.expected_output = malloc(sizeof(secp256k1_gej) * (iters + 1));
325 data.output = malloc(sizeof(secp256k1_gej) * (iters + 1));
326
327 /* Generate a set of scalars, and private/public keypairs. */
328 secp256k1_gej_set_ge(&data.pubkeys_gej[0], &secp256k1_ge_const_g);
329 secp256k1_scalar_set_int(&data.seckeys[0], 1);
330 for (i = 0; i < POINTS; ++i) {
331 generate_scalar(i, &data.scalars[i]);
332 if (i) {
333 secp256k1_gej_double_var(&data.pubkeys_gej[i], &data.pubkeys_gej[i - 1], NULL);
334 secp256k1_scalar_add(&data.seckeys[i], &data.seckeys[i - 1], &data.seckeys[i - 1]);
335 }
336 }
337 secp256k1_ge_set_all_gej_var(data.pubkeys, data.pubkeys_gej, POINTS);
338
339
341 /* Initialize offset1 and offset2 */
342 hash_into_offset(&data, 0);
343 run_ecmult_bench(&data, iters);
344
345 for (i = 1; i <= 8; ++i) {
346 run_ecmult_multi_bench(&data, i, 1, iters);
347 }
348
349 /* This is disabled with low count of iterations because the loop runs 77 times even with iters=1
350 * and the higher it goes the longer the computation takes(more points)
351 * So we don't run this benchmark with low iterations to prevent slow down */
352 if (iters > 2) {
353 for (p = 0; p <= 11; ++p) {
354 for (i = 9; i <= 16; ++i) {
355 run_ecmult_multi_bench(&data, i << p, 1, iters);
356 }
357 }
358 }
359
360 if (data.scratch != NULL) {
361 secp256k1_scratch_space_destroy(data.ctx, data.scratch);
362 }
364 free(data.scalars);
365 free(data.pubkeys);
366 free(data.pubkeys_gej);
367 free(data.seckeys);
368 free(data.output);
369 free(data.expected_output);
370
371 return(0);
372}
void print_output_table_header_row(void)
Definition bench.h:163
int have_flag(int argc, char **argv, char *flag)
Definition bench.h:116
int get_iters(int default_iters)
Definition bench.h:154
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:82
void help(char **argv)
#define POINTS
const mie::Vuint & p
Definition bn.cpp:27
#define STRAUSS_SCRATCH_OBJECTS
Definition ecmult_impl.h:50
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)
#define CHECK(cond)
Definition util.h:80
int * count
char ** argv
return str
Definition CLI11.hpp:1359
constexpr const char sha256[]
#define SECP256K1_SCALAR_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
Definition scalar_4x64.h:17
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Definition secp256k1.c:146
#define SECP256K1_CONTEXT_SIGN
Definition secp256k1.h:196
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space * secp256k1_scratch_space_create(const secp256k1_context *ctx, size_t size) SECP256K1_ARG_NONNULL(1)
Definition secp256k1.c:171
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Definition secp256k1.c:107
SECP256K1_API void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space *scratch) SECP256K1_ARG_NONNULL(1)
Definition secp256k1.c:176
#define SECP256K1_CONTEXT_VERIFY
Definition secp256k1.h:195
unsigned int uint32_t
Definition stdint.h:126
secp256k1_scalar * seckeys
secp256k1_gej * output
secp256k1_gej * pubkeys_gej
size_t offset2
secp256k1_ecmult_multi_func ecmult_multi
size_t offset1
secp256k1_scratch_space * scratch
secp256k1_context * ctx
secp256k1_ge * pubkeys
size_t count
secp256k1_scalar * scalars
secp256k1_gej * expected_output
bool overflow
char * s
uint8_t buf[2048]