]> Git Repo - secp256k1.git/blame - src/bench_internal.c
Merge #699: Initialize field elements when resulting in infinity
[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
7c068998 10#include "assumptions.h"
039723d5
PW
11#include "util.h"
12#include "hash_impl.h"
13#include "num_impl.h"
14#include "field_impl.h"
15#include "group_impl.h"
16#include "scalar_impl.h"
91c0ce95 17#include "ecmult_const_impl.h"
039723d5
PW
18#include "ecmult_impl.h"
19#include "bench.h"
2f774870 20#include "secp256k1.c"
039723d5
PW
21
22typedef struct {
dd891e0e
PW
23 secp256k1_scalar scalar_x, scalar_y;
24 secp256k1_fe fe_x, fe_y;
25 secp256k1_ge ge_x, ge_y;
26 secp256k1_gej gej_x, gej_y;
3e6f1e20 27 unsigned char data[64];
039723d5 28 int wnaf[256];
d1dc9dfc 29} bench_inv;
039723d5
PW
30
31void bench_setup(void* arg) {
d1dc9dfc 32 bench_inv *data = (bench_inv*)arg;
039723d5
PW
33
34 static const unsigned char init_x[32] = {
35 0x02, 0x03, 0x05, 0x07, 0x0b, 0x0d, 0x11, 0x13,
36 0x17, 0x1d, 0x1f, 0x25, 0x29, 0x2b, 0x2f, 0x35,
37 0x3b, 0x3d, 0x43, 0x47, 0x49, 0x4f, 0x53, 0x59,
38 0x61, 0x65, 0x67, 0x6b, 0x6d, 0x71, 0x7f, 0x83
39 };
40
41 static const unsigned char init_y[32] = {
42 0x82, 0x83, 0x85, 0x87, 0x8b, 0x8d, 0x81, 0x83,
43 0x97, 0xad, 0xaf, 0xb5, 0xb9, 0xbb, 0xbf, 0xc5,
44 0xdb, 0xdd, 0xe3, 0xe7, 0xe9, 0xef, 0xf3, 0xf9,
45 0x11, 0x15, 0x17, 0x1b, 0x1d, 0xb1, 0xbf, 0xd3
46 };
47
48 secp256k1_scalar_set_b32(&data->scalar_x, init_x, NULL);
49 secp256k1_scalar_set_b32(&data->scalar_y, init_y, NULL);
50 secp256k1_fe_set_b32(&data->fe_x, init_x);
51 secp256k1_fe_set_b32(&data->fe_y, init_y);
52 CHECK(secp256k1_ge_set_xo_var(&data->ge_x, &data->fe_x, 0));
53 CHECK(secp256k1_ge_set_xo_var(&data->ge_y, &data->fe_y, 1));
54 secp256k1_gej_set_ge(&data->gej_x, &data->ge_x);
55 secp256k1_gej_set_ge(&data->gej_y, &data->ge_y);
56 memcpy(data->data, init_x, 32);
3e6f1e20 57 memcpy(data->data + 32, init_y, 32);
039723d5
PW
58}
59
ca4906b0 60void bench_scalar_add(void* arg, int iters) {
73a30c6b 61 int i, j = 0;
d1dc9dfc 62 bench_inv *data = (bench_inv*)arg;
039723d5 63
ca4906b0 64 for (i = 0; i < iters; i++) {
73a30c6b 65 j += secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
039723d5 66 }
ca4906b0 67 CHECK(j <= iters);
039723d5
PW
68}
69
ca4906b0 70void bench_scalar_negate(void* arg, int iters) {
039723d5 71 int i;
d1dc9dfc 72 bench_inv *data = (bench_inv*)arg;
039723d5 73
ca4906b0 74 for (i = 0; i < iters; i++) {
039723d5
PW
75 secp256k1_scalar_negate(&data->scalar_x, &data->scalar_x);
76 }
77}
78
ca4906b0 79void bench_scalar_sqr(void* arg, int iters) {
039723d5 80 int i;
d1dc9dfc 81 bench_inv *data = (bench_inv*)arg;
039723d5 82
ca4906b0 83 for (i = 0; i < iters; i++) {
039723d5
PW
84 secp256k1_scalar_sqr(&data->scalar_x, &data->scalar_x);
85 }
86}
87
ca4906b0 88void bench_scalar_mul(void* arg, int iters) {
039723d5 89 int i;
d1dc9dfc 90 bench_inv *data = (bench_inv*)arg;
039723d5 91
ca4906b0 92 for (i = 0; i < iters; i++) {
039723d5
PW
93 secp256k1_scalar_mul(&data->scalar_x, &data->scalar_x, &data->scalar_y);
94 }
95}
96
97#ifdef USE_ENDOMORPHISM
ca4906b0 98void bench_scalar_split(void* arg, int iters) {
362bb256 99 int i, j = 0;
d1dc9dfc 100 bench_inv *data = (bench_inv*)arg;
039723d5 101
ca4906b0 102 for (i = 0; i < iters; i++) {
362bb256
ET
103 secp256k1_scalar_split_lambda(&data->scalar_x, &data->scalar_y, &data->scalar_x);
104 j += secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
039723d5 105 }
ca4906b0 106 CHECK(j <= iters);
039723d5
PW
107}
108#endif
109
ca4906b0 110void bench_scalar_inverse(void* arg, int iters) {
73a30c6b 111 int i, j = 0;
d1dc9dfc 112 bench_inv *data = (bench_inv*)arg;
039723d5 113
ca4906b0 114 for (i = 0; i < iters; i++) {
039723d5 115 secp256k1_scalar_inverse(&data->scalar_x, &data->scalar_x);
73a30c6b 116 j += secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
039723d5 117 }
ca4906b0 118 CHECK(j <= iters);
039723d5
PW
119}
120
ca4906b0 121void bench_scalar_inverse_var(void* arg, int iters) {
73a30c6b 122 int i, j = 0;
d1dc9dfc 123 bench_inv *data = (bench_inv*)arg;
039723d5 124
ca4906b0 125 for (i = 0; i < iters; i++) {
039723d5 126 secp256k1_scalar_inverse_var(&data->scalar_x, &data->scalar_x);
73a30c6b 127 j += secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
039723d5 128 }
ca4906b0 129 CHECK(j <= iters);
039723d5
PW
130}
131
ca4906b0 132void bench_field_normalize(void* arg, int iters) {
039723d5 133 int i;
d1dc9dfc 134 bench_inv *data = (bench_inv*)arg;
039723d5 135
ca4906b0 136 for (i = 0; i < iters; i++) {
039723d5
PW
137 secp256k1_fe_normalize(&data->fe_x);
138 }
139}
140
ca4906b0 141void bench_field_normalize_weak(void* arg, int iters) {
039723d5 142 int i;
d1dc9dfc 143 bench_inv *data = (bench_inv*)arg;
039723d5 144
ca4906b0 145 for (i = 0; i < iters; i++) {
039723d5
PW
146 secp256k1_fe_normalize_weak(&data->fe_x);
147 }
148}
149
ca4906b0 150void bench_field_mul(void* arg, int iters) {
039723d5 151 int i;
d1dc9dfc 152 bench_inv *data = (bench_inv*)arg;
039723d5 153
ca4906b0 154 for (i = 0; i < iters; i++) {
039723d5
PW
155 secp256k1_fe_mul(&data->fe_x, &data->fe_x, &data->fe_y);
156 }
157}
158
ca4906b0 159void bench_field_sqr(void* arg, int iters) {
039723d5 160 int i;
d1dc9dfc 161 bench_inv *data = (bench_inv*)arg;
039723d5 162
ca4906b0 163 for (i = 0; i < iters; i++) {
039723d5
PW
164 secp256k1_fe_sqr(&data->fe_x, &data->fe_x);
165 }
166}
167
ca4906b0 168void bench_field_inverse(void* arg, int iters) {
039723d5 169 int i;
d1dc9dfc 170 bench_inv *data = (bench_inv*)arg;
039723d5 171
ca4906b0 172 for (i = 0; i < iters; i++) {
039723d5
PW
173 secp256k1_fe_inv(&data->fe_x, &data->fe_x);
174 secp256k1_fe_add(&data->fe_x, &data->fe_y);
175 }
176}
177
ca4906b0 178void bench_field_inverse_var(void* arg, int iters) {
039723d5 179 int i;
d1dc9dfc 180 bench_inv *data = (bench_inv*)arg;
039723d5 181
ca4906b0 182 for (i = 0; i < iters; i++) {
039723d5
PW
183 secp256k1_fe_inv_var(&data->fe_x, &data->fe_x);
184 secp256k1_fe_add(&data->fe_x, &data->fe_y);
185 }
186}
187
ca4906b0 188void bench_field_sqrt(void* arg, int iters) {
73a30c6b 189 int i, j = 0;
d1dc9dfc 190 bench_inv *data = (bench_inv*)arg;
b76e45d5 191 secp256k1_fe t;
039723d5 192
ca4906b0 193 for (i = 0; i < iters; i++) {
b76e45d5 194 t = data->fe_x;
73a30c6b 195 j += secp256k1_fe_sqrt(&data->fe_x, &t);
039723d5
PW
196 secp256k1_fe_add(&data->fe_x, &data->fe_y);
197 }
ca4906b0 198 CHECK(j <= iters);
039723d5
PW
199}
200
ca4906b0 201void bench_group_double_var(void* arg, int iters) {
039723d5 202 int i;
d1dc9dfc 203 bench_inv *data = (bench_inv*)arg;
039723d5 204
ca4906b0 205 for (i = 0; i < iters; i++) {
4f9791ab 206 secp256k1_gej_double_var(&data->gej_x, &data->gej_x, NULL);
039723d5
PW
207 }
208}
209
ca4906b0 210void bench_group_add_var(void* arg, int iters) {
039723d5 211 int i;
d1dc9dfc 212 bench_inv *data = (bench_inv*)arg;
039723d5 213
ca4906b0 214 for (i = 0; i < iters; i++) {
4f9791ab 215 secp256k1_gej_add_var(&data->gej_x, &data->gej_x, &data->gej_y, NULL);
039723d5
PW
216 }
217}
218
ca4906b0 219void bench_group_add_affine(void* arg, int iters) {
039723d5 220 int i;
d1dc9dfc 221 bench_inv *data = (bench_inv*)arg;
039723d5 222
ca4906b0 223 for (i = 0; i < iters; i++) {
039723d5
PW
224 secp256k1_gej_add_ge(&data->gej_x, &data->gej_x, &data->ge_y);
225 }
226}
227
ca4906b0 228void bench_group_add_affine_var(void* arg, int iters) {
039723d5 229 int i;
d1dc9dfc 230 bench_inv *data = (bench_inv*)arg;
039723d5 231
ca4906b0 232 for (i = 0; i < iters; i++) {
2d5a186c 233 secp256k1_gej_add_ge_var(&data->gej_x, &data->gej_x, &data->ge_y, NULL);
039723d5
PW
234 }
235}
236
ca4906b0 237void bench_group_jacobi_var(void* arg, int iters) {
73a30c6b 238 int i, j = 0;
d1dc9dfc 239 bench_inv *data = (bench_inv*)arg;
e6e9805f 240
ca4906b0 241 for (i = 0; i < iters; i++) {
73a30c6b 242 j += secp256k1_gej_has_quad_y_var(&data->gej_x);
e6e9805f 243 }
ca4906b0 244 CHECK(j == iters);
e6e9805f
PW
245}
246
ca4906b0 247void bench_ecmult_wnaf(void* arg, int iters) {
73a30c6b 248 int i, bits = 0, overflow = 0;
d1dc9dfc 249 bench_inv *data = (bench_inv*)arg;
039723d5 250
ca4906b0 251 for (i = 0; i < iters; i++) {
73a30c6b
ET
252 bits += secp256k1_ecmult_wnaf(data->wnaf, 256, &data->scalar_x, WINDOW_A);
253 overflow += secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
039723d5 254 }
73a30c6b 255 CHECK(overflow >= 0);
ca4906b0 256 CHECK(bits <= 256*iters);
039723d5
PW
257}
258
ca4906b0 259void bench_wnaf_const(void* arg, int iters) {
73a30c6b 260 int i, bits = 0, overflow = 0;
d1dc9dfc 261 bench_inv *data = (bench_inv*)arg;
91c0ce95 262
ca4906b0 263 for (i = 0; i < iters; i++) {
73a30c6b
ET
264 bits += secp256k1_wnaf_const(data->wnaf, &data->scalar_x, WINDOW_A, 256);
265 overflow += secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
91c0ce95 266 }
73a30c6b 267 CHECK(overflow >= 0);
ca4906b0 268 CHECK(bits <= 256*iters);
91c0ce95
AP
269}
270
039723d5 271
ca4906b0 272void bench_sha256(void* arg, int iters) {
039723d5 273 int i;
d1dc9dfc
PW
274 bench_inv *data = (bench_inv*)arg;
275 secp256k1_sha256 sha;
039723d5 276
ca4906b0 277 for (i = 0; i < iters; i++) {
039723d5
PW
278 secp256k1_sha256_initialize(&sha);
279 secp256k1_sha256_write(&sha, data->data, 32);
280 secp256k1_sha256_finalize(&sha, data->data);
281 }
282}
283
ca4906b0 284void bench_hmac_sha256(void* arg, int iters) {
039723d5 285 int i;
d1dc9dfc
PW
286 bench_inv *data = (bench_inv*)arg;
287 secp256k1_hmac_sha256 hmac;
039723d5 288
ca4906b0 289 for (i = 0; i < iters; i++) {
039723d5
PW
290 secp256k1_hmac_sha256_initialize(&hmac, data->data, 32);
291 secp256k1_hmac_sha256_write(&hmac, data->data, 32);
292 secp256k1_hmac_sha256_finalize(&hmac, data->data);
293 }
294}
295
ca4906b0 296void bench_rfc6979_hmac_sha256(void* arg, int iters) {
039723d5 297 int i;
d1dc9dfc
PW
298 bench_inv *data = (bench_inv*)arg;
299 secp256k1_rfc6979_hmac_sha256 rng;
039723d5 300
ca4906b0 301 for (i = 0; i < iters; i++) {
3e6f1e20 302 secp256k1_rfc6979_hmac_sha256_initialize(&rng, data->data, 64);
039723d5
PW
303 secp256k1_rfc6979_hmac_sha256_generate(&rng, data->data, 32);
304 }
305}
306
ca4906b0 307void bench_context_verify(void* arg, int iters) {
2f774870
PW
308 int i;
309 (void)arg;
ca4906b0 310 for (i = 0; i < iters; i++) {
2f774870
PW
311 secp256k1_context_destroy(secp256k1_context_create(SECP256K1_CONTEXT_VERIFY));
312 }
313}
314
ca4906b0 315void bench_context_sign(void* arg, int iters) {
2f774870
PW
316 int i;
317 (void)arg;
ca4906b0 318 for (i = 0; i < iters; i++) {
2f774870
PW
319 secp256k1_context_destroy(secp256k1_context_create(SECP256K1_CONTEXT_SIGN));
320 }
321}
322
efd953a7 323#ifndef USE_NUM_NONE
ca4906b0 324void bench_num_jacobi(void* arg, int iters) {
73a30c6b 325 int i, j = 0;
d1dc9dfc 326 bench_inv *data = (bench_inv*)arg;
efd953a7
PD
327 secp256k1_num nx, norder;
328
329 secp256k1_scalar_get_num(&nx, &data->scalar_x);
330 secp256k1_scalar_order_get_num(&norder);
331 secp256k1_scalar_get_num(&norder, &data->scalar_y);
332
ca4906b0 333 for (i = 0; i < iters; i++) {
73a30c6b 334 j += secp256k1_num_jacobi(&nx, &norder);
efd953a7 335 }
ca4906b0 336 CHECK(j <= iters);
efd953a7
PD
337}
338#endif
039723d5 339
039723d5 340int main(int argc, char **argv) {
d1dc9dfc 341 bench_inv data;
ca4906b0
ET
342 int iters = get_iters(20000);
343
344 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "add")) run_benchmark("scalar_add", bench_scalar_add, bench_setup, NULL, &data, 10, iters*100);
345 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "negate")) run_benchmark("scalar_negate", bench_scalar_negate, bench_setup, NULL, &data, 10, iters*100);
346 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "sqr")) run_benchmark("scalar_sqr", bench_scalar_sqr, bench_setup, NULL, &data, 10, iters*10);
347 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "mul")) run_benchmark("scalar_mul", bench_scalar_mul, bench_setup, NULL, &data, 10, iters*10);
039723d5 348#ifdef USE_ENDOMORPHISM
ca4906b0 349 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "split")) run_benchmark("scalar_split", bench_scalar_split, bench_setup, NULL, &data, 10, iters);
039723d5
PW
350#endif
351 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "inverse")) run_benchmark("scalar_inverse", bench_scalar_inverse, bench_setup, NULL, &data, 10, 2000);
352 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);
353
ca4906b0
ET
354 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "normalize")) run_benchmark("field_normalize", bench_field_normalize, bench_setup, NULL, &data, 10, iters*100);
355 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, iters*100);
356 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "sqr")) run_benchmark("field_sqr", bench_field_sqr, bench_setup, NULL, &data, 10, iters*10);
357 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "mul")) run_benchmark("field_mul", bench_field_mul, bench_setup, NULL, &data, 10, iters*10);
358 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "inverse")) run_benchmark("field_inverse", bench_field_inverse, bench_setup, NULL, &data, 10, iters);
359 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, iters);
360 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "sqrt")) run_benchmark("field_sqrt", bench_field_sqrt, bench_setup, NULL, &data, 10, iters);
039723d5 361
ca4906b0
ET
362 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, iters*10);
363 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, iters*10);
364 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, iters*10);
365 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, iters*10);
366 if (have_flag(argc, argv, "group") || have_flag(argc, argv, "jacobi")) run_benchmark("group_jacobi_var", bench_group_jacobi_var, bench_setup, NULL, &data, 10, iters);
039723d5 367
ca4906b0
ET
368 if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("wnaf_const", bench_wnaf_const, bench_setup, NULL, &data, 10, iters);
369 if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("ecmult_wnaf", bench_ecmult_wnaf, bench_setup, NULL, &data, 10, iters);
039723d5 370
ca4906b0
ET
371 if (have_flag(argc, argv, "hash") || have_flag(argc, argv, "sha256")) run_benchmark("hash_sha256", bench_sha256, bench_setup, NULL, &data, 10, iters);
372 if (have_flag(argc, argv, "hash") || have_flag(argc, argv, "hmac")) run_benchmark("hash_hmac_sha256", bench_hmac_sha256, bench_setup, NULL, &data, 10, iters);
373 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, iters);
2f774870 374
ca4906b0
ET
375 if (have_flag(argc, argv, "context") || have_flag(argc, argv, "verify")) run_benchmark("context_verify", bench_context_verify, bench_setup, NULL, &data, 10, 1 + iters/1000);
376 if (have_flag(argc, argv, "context") || have_flag(argc, argv, "sign")) run_benchmark("context_sign", bench_context_sign, bench_setup, NULL, &data, 10, 1 + iters/100);
2f774870 377
efd953a7 378#ifndef USE_NUM_NONE
ca4906b0 379 if (have_flag(argc, argv, "num") || have_flag(argc, argv, "jacobi")) run_benchmark("num_jacobi", bench_num_jacobi, bench_setup, NULL, &data, 10, iters*10);
efd953a7 380#endif
039723d5
PW
381 return 0;
382}
This page took 0.086743 seconds and 4 git commands to generate.