]> Git Repo - secp256k1.git/blame - src/bench_internal.c
Make `secp256k1_scalar_add_bit` conditional; make `secp256k1_scalar_split_lambda_var...
[secp256k1.git] / src / bench_internal.c
CommitLineData
039723d5
PW
1/**********************************************************************
2 * Copyright (c) 2014-2015 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
6#include <stdio.h>
7
8#include "include/secp256k1.h"
9
10#include "util.h"
11#include "hash_impl.h"
12#include "num_impl.h"
13#include "field_impl.h"
14#include "group_impl.h"
15#include "scalar_impl.h"
91c0ce95 16#include "ecmult_const_impl.h"
039723d5
PW
17#include "ecmult_impl.h"
18#include "bench.h"
19
20typedef struct {
21 secp256k1_scalar_t scalar_x, scalar_y;
22 secp256k1_fe_t fe_x, fe_y;
23 secp256k1_ge_t ge_x, ge_y;
24 secp256k1_gej_t gej_x, gej_y;
3e6f1e20 25 unsigned char data[64];
039723d5
PW
26 int wnaf[256];
27} bench_inv_t;
28
29void bench_setup(void* arg) {
30 bench_inv_t *data = (bench_inv_t*)arg;
31
32 static const unsigned char init_x[32] = {
33 0x02, 0x03, 0x05, 0x07, 0x0b, 0x0d, 0x11, 0x13,
34 0x17, 0x1d, 0x1f, 0x25, 0x29, 0x2b, 0x2f, 0x35,
35 0x3b, 0x3d, 0x43, 0x47, 0x49, 0x4f, 0x53, 0x59,
36 0x61, 0x65, 0x67, 0x6b, 0x6d, 0x71, 0x7f, 0x83
37 };
38
39 static const unsigned char init_y[32] = {
40 0x82, 0x83, 0x85, 0x87, 0x8b, 0x8d, 0x81, 0x83,
41 0x97, 0xad, 0xaf, 0xb5, 0xb9, 0xbb, 0xbf, 0xc5,
42 0xdb, 0xdd, 0xe3, 0xe7, 0xe9, 0xef, 0xf3, 0xf9,
43 0x11, 0x15, 0x17, 0x1b, 0x1d, 0xb1, 0xbf, 0xd3
44 };
45
46 secp256k1_scalar_set_b32(&data->scalar_x, init_x, NULL);
47 secp256k1_scalar_set_b32(&data->scalar_y, init_y, NULL);
48 secp256k1_fe_set_b32(&data->fe_x, init_x);
49 secp256k1_fe_set_b32(&data->fe_y, init_y);
50 CHECK(secp256k1_ge_set_xo_var(&data->ge_x, &data->fe_x, 0));
51 CHECK(secp256k1_ge_set_xo_var(&data->ge_y, &data->fe_y, 1));
52 secp256k1_gej_set_ge(&data->gej_x, &data->ge_x);
53 secp256k1_gej_set_ge(&data->gej_y, &data->ge_y);
54 memcpy(data->data, init_x, 32);
3e6f1e20 55 memcpy(data->data + 32, init_y, 32);
039723d5
PW
56}
57
58void bench_scalar_add(void* arg) {
59 int i;
60 bench_inv_t *data = (bench_inv_t*)arg;
61
62 for (i = 0; i < 2000000; i++) {
63 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
64 }
65}
66
67void bench_scalar_negate(void* arg) {
68 int i;
69 bench_inv_t *data = (bench_inv_t*)arg;
70
71 for (i = 0; i < 2000000; i++) {
72 secp256k1_scalar_negate(&data->scalar_x, &data->scalar_x);
73 }
74}
75
76void bench_scalar_sqr(void* arg) {
77 int i;
78 bench_inv_t *data = (bench_inv_t*)arg;
79
80 for (i = 0; i < 200000; i++) {
81 secp256k1_scalar_sqr(&data->scalar_x, &data->scalar_x);
82 }
83}
84
85void bench_scalar_mul(void* arg) {
86 int i;
87 bench_inv_t *data = (bench_inv_t*)arg;
88
89 for (i = 0; i < 200000; i++) {
90 secp256k1_scalar_mul(&data->scalar_x, &data->scalar_x, &data->scalar_y);
91 }
92}
93
94#ifdef USE_ENDOMORPHISM
95void bench_scalar_split(void* arg) {
96 int i;
97 bench_inv_t *data = (bench_inv_t*)arg;
98
99 for (i = 0; i < 20000; i++) {
100 secp256k1_scalar_t l, r;
ed35d43a 101 secp256k1_scalar_split_lambda(&l, &r, &data->scalar_x);
039723d5
PW
102 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
103 }
104}
105#endif
106
107void bench_scalar_inverse(void* arg) {
108 int i;
109 bench_inv_t *data = (bench_inv_t*)arg;
110
111 for (i = 0; i < 2000; i++) {
112 secp256k1_scalar_inverse(&data->scalar_x, &data->scalar_x);
113 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
114 }
115}
116
117void bench_scalar_inverse_var(void* arg) {
118 int i;
119 bench_inv_t *data = (bench_inv_t*)arg;
120
121 for (i = 0; i < 2000; i++) {
122 secp256k1_scalar_inverse_var(&data->scalar_x, &data->scalar_x);
123 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
124 }
125}
126
127void bench_field_normalize(void* arg) {
128 int i;
129 bench_inv_t *data = (bench_inv_t*)arg;
130
131 for (i = 0; i < 2000000; i++) {
132 secp256k1_fe_normalize(&data->fe_x);
133 }
134}
135
136void bench_field_normalize_weak(void* arg) {
137 int i;
138 bench_inv_t *data = (bench_inv_t*)arg;
139
140 for (i = 0; i < 2000000; i++) {
141 secp256k1_fe_normalize_weak(&data->fe_x);
142 }
143}
144
145void bench_field_mul(void* arg) {
146 int i;
147 bench_inv_t *data = (bench_inv_t*)arg;
148
149 for (i = 0; i < 200000; i++) {
150 secp256k1_fe_mul(&data->fe_x, &data->fe_x, &data->fe_y);
151 }
152}
153
154void bench_field_sqr(void* arg) {
155 int i;
156 bench_inv_t *data = (bench_inv_t*)arg;
157
158 for (i = 0; i < 200000; i++) {
159 secp256k1_fe_sqr(&data->fe_x, &data->fe_x);
160 }
161}
162
163void bench_field_inverse(void* arg) {
164 int i;
165 bench_inv_t *data = (bench_inv_t*)arg;
166
167 for (i = 0; i < 20000; i++) {
168 secp256k1_fe_inv(&data->fe_x, &data->fe_x);
169 secp256k1_fe_add(&data->fe_x, &data->fe_y);
170 }
171}
172
173void bench_field_inverse_var(void* arg) {
174 int i;
175 bench_inv_t *data = (bench_inv_t*)arg;
176
177 for (i = 0; i < 20000; i++) {
178 secp256k1_fe_inv_var(&data->fe_x, &data->fe_x);
179 secp256k1_fe_add(&data->fe_x, &data->fe_y);
180 }
181}
182
183void bench_field_sqrt_var(void* arg) {
184 int i;
185 bench_inv_t *data = (bench_inv_t*)arg;
186
187 for (i = 0; i < 20000; i++) {
188 secp256k1_fe_sqrt_var(&data->fe_x, &data->fe_x);
189 secp256k1_fe_add(&data->fe_x, &data->fe_y);
190 }
191}
192
193void bench_group_double_var(void* arg) {
194 int i;
195 bench_inv_t *data = (bench_inv_t*)arg;
196
197 for (i = 0; i < 200000; i++) {
4f9791ab 198 secp256k1_gej_double_var(&data->gej_x, &data->gej_x, NULL);
039723d5
PW
199 }
200}
201
202void bench_group_add_var(void* arg) {
203 int i;
204 bench_inv_t *data = (bench_inv_t*)arg;
205
206 for (i = 0; i < 200000; i++) {
4f9791ab 207 secp256k1_gej_add_var(&data->gej_x, &data->gej_x, &data->gej_y, NULL);
039723d5
PW
208 }
209}
210
211void bench_group_add_affine(void* arg) {
212 int i;
213 bench_inv_t *data = (bench_inv_t*)arg;
214
215 for (i = 0; i < 200000; i++) {
216 secp256k1_gej_add_ge(&data->gej_x, &data->gej_x, &data->ge_y);
217 }
218}
219
220void bench_group_add_affine_var(void* arg) {
221 int i;
222 bench_inv_t *data = (bench_inv_t*)arg;
223
224 for (i = 0; i < 200000; i++) {
2d5a186c 225 secp256k1_gej_add_ge_var(&data->gej_x, &data->gej_x, &data->ge_y, NULL);
039723d5
PW
226 }
227}
228
229void bench_ecmult_wnaf(void* arg) {
230 int i;
231 bench_inv_t *data = (bench_inv_t*)arg;
232
233 for (i = 0; i < 20000; i++) {
55399c23 234 secp256k1_ecmult_wnaf(data->wnaf, 256, &data->scalar_x, WINDOW_A);
039723d5
PW
235 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
236 }
237}
238
91c0ce95
AP
239void bench_wnaf_const(void* arg) {
240 int i;
241 bench_inv_t *data = (bench_inv_t*)arg;
242
243 for (i = 0; i < 20000; i++) {
244 secp256k1_wnaf_const(data->wnaf, &data->scalar_x, WINDOW_A);
245 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
246 }
247}
248
039723d5
PW
249
250void bench_sha256(void* arg) {
251 int i;
252 bench_inv_t *data = (bench_inv_t*)arg;
253 secp256k1_sha256_t sha;
254
255 for (i = 0; i < 20000; i++) {
256 secp256k1_sha256_initialize(&sha);
257 secp256k1_sha256_write(&sha, data->data, 32);
258 secp256k1_sha256_finalize(&sha, data->data);
259 }
260}
261
262void bench_hmac_sha256(void* arg) {
263 int i;
264 bench_inv_t *data = (bench_inv_t*)arg;
265 secp256k1_hmac_sha256_t hmac;
266
267 for (i = 0; i < 20000; i++) {
268 secp256k1_hmac_sha256_initialize(&hmac, data->data, 32);
269 secp256k1_hmac_sha256_write(&hmac, data->data, 32);
270 secp256k1_hmac_sha256_finalize(&hmac, data->data);
271 }
272}
273
274void bench_rfc6979_hmac_sha256(void* arg) {
275 int i;
276 bench_inv_t *data = (bench_inv_t*)arg;
277 secp256k1_rfc6979_hmac_sha256_t rng;
278
279 for (i = 0; i < 20000; i++) {
3e6f1e20 280 secp256k1_rfc6979_hmac_sha256_initialize(&rng, data->data, 64);
039723d5
PW
281 secp256k1_rfc6979_hmac_sha256_generate(&rng, data->data, 32);
282 }
283}
284
285
286int have_flag(int argc, char** argv, char *flag) {
287 char** argm = argv + argc;
288 argv++;
289 if (argv == argm) {
290 return 1;
291 }
292 while (argv != NULL && argv != argm) {
293 if (strcmp(*argv, flag) == 0) return 1;
294 argv++;
295 }
296 return 0;
297}
298
299int main(int argc, char **argv) {
300 bench_inv_t data;
301 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "add")) run_benchmark("scalar_add", bench_scalar_add, bench_setup, NULL, &data, 10, 2000000);
302 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "negate")) run_benchmark("scalar_negate", bench_scalar_negate, bench_setup, NULL, &data, 10, 2000000);
303 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "sqr")) run_benchmark("scalar_sqr", bench_scalar_sqr, bench_setup, NULL, &data, 10, 200000);
304 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "mul")) run_benchmark("scalar_mul", bench_scalar_mul, bench_setup, NULL, &data, 10, 200000);
305#ifdef USE_ENDOMORPHISM
306 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "split")) run_benchmark("scalar_split", bench_scalar_split, bench_setup, NULL, &data, 10, 20000);
307#endif
308 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "inverse")) run_benchmark("scalar_inverse", bench_scalar_inverse, bench_setup, NULL, &data, 10, 2000);
309 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "inverse")) run_benchmark("scalar_inverse_var", bench_scalar_inverse_var, bench_setup, NULL, &data, 10, 2000);
310
311 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "normalize")) run_benchmark("field_normalize", bench_field_normalize, bench_setup, NULL, &data, 10, 2000000);
312 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "normalize")) run_benchmark("field_normalize_weak", bench_field_normalize_weak, bench_setup, NULL, &data, 10, 2000000);
313 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "sqr")) run_benchmark("field_sqr", bench_field_sqr, bench_setup, NULL, &data, 10, 200000);
314 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "mul")) run_benchmark("field_mul", bench_field_mul, bench_setup, NULL, &data, 10, 200000);
315 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "inverse")) run_benchmark("field_inverse", bench_field_inverse, bench_setup, NULL, &data, 10, 20000);
316 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "inverse")) run_benchmark("field_inverse_var", bench_field_inverse_var, bench_setup, NULL, &data, 10, 20000);
317 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "sqrt")) run_benchmark("field_sqrt_var", bench_field_sqrt_var, bench_setup, NULL, &data, 10, 20000);
318
319 if (have_flag(argc, argv, "group") || have_flag(argc, argv, "double")) run_benchmark("group_double_var", bench_group_double_var, bench_setup, NULL, &data, 10, 200000);
320 if (have_flag(argc, argv, "group") || have_flag(argc, argv, "add")) run_benchmark("group_add_var", bench_group_add_var, bench_setup, NULL, &data, 10, 200000);
321 if (have_flag(argc, argv, "group") || have_flag(argc, argv, "add")) run_benchmark("group_add_affine", bench_group_add_affine, bench_setup, NULL, &data, 10, 200000);
322 if (have_flag(argc, argv, "group") || have_flag(argc, argv, "add")) run_benchmark("group_add_affine_var", bench_group_add_affine_var, bench_setup, NULL, &data, 10, 200000);
323
91c0ce95 324 if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("wnaf_const", bench_wnaf_const, bench_setup, NULL, &data, 10, 20000);
039723d5
PW
325 if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("ecmult_wnaf", bench_ecmult_wnaf, bench_setup, NULL, &data, 10, 20000);
326
327 if (have_flag(argc, argv, "hash") || have_flag(argc, argv, "sha256")) run_benchmark("hash_sha256", bench_sha256, bench_setup, NULL, &data, 10, 20000);
328 if (have_flag(argc, argv, "hash") || have_flag(argc, argv, "hmac")) run_benchmark("hash_hmac_sha256", bench_hmac_sha256, bench_setup, NULL, &data, 10, 20000);
329 if (have_flag(argc, argv, "hash") || have_flag(argc, argv, "rng6979")) run_benchmark("hash_rfc6979_hmac_sha256", bench_rfc6979_hmac_sha256, bench_setup, NULL, &data, 10, 20000);
330 return 0;
331}
This page took 0.055921 seconds and 4 git commands to generate.