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 **********************************************************************/
8 #include "include/secp256k1.h"
11 #include "hash_impl.h"
13 #include "field_impl.h"
14 #include "group_impl.h"
15 #include "scalar_impl.h"
16 #include "ecmult_impl.h"
20 secp256k1_scalar_t scalar_x, scalar_y;
21 secp256k1_fe_t fe_x, fe_y;
22 secp256k1_ge_t ge_x, ge_y;
23 secp256k1_gej_t gej_x, gej_y;
24 unsigned char data[64];
28 void bench_setup(void* arg) {
29 bench_inv_t *data = (bench_inv_t*)arg;
31 static const unsigned char init_x[32] = {
32 0x02, 0x03, 0x05, 0x07, 0x0b, 0x0d, 0x11, 0x13,
33 0x17, 0x1d, 0x1f, 0x25, 0x29, 0x2b, 0x2f, 0x35,
34 0x3b, 0x3d, 0x43, 0x47, 0x49, 0x4f, 0x53, 0x59,
35 0x61, 0x65, 0x67, 0x6b, 0x6d, 0x71, 0x7f, 0x83
38 static const unsigned char init_y[32] = {
39 0x82, 0x83, 0x85, 0x87, 0x8b, 0x8d, 0x81, 0x83,
40 0x97, 0xad, 0xaf, 0xb5, 0xb9, 0xbb, 0xbf, 0xc5,
41 0xdb, 0xdd, 0xe3, 0xe7, 0xe9, 0xef, 0xf3, 0xf9,
42 0x11, 0x15, 0x17, 0x1b, 0x1d, 0xb1, 0xbf, 0xd3
45 secp256k1_scalar_set_b32(&data->scalar_x, init_x, NULL);
46 secp256k1_scalar_set_b32(&data->scalar_y, init_y, NULL);
47 secp256k1_fe_set_b32(&data->fe_x, init_x);
48 secp256k1_fe_set_b32(&data->fe_y, init_y);
49 CHECK(secp256k1_ge_set_xo_var(&data->ge_x, &data->fe_x, 0));
50 CHECK(secp256k1_ge_set_xo_var(&data->ge_y, &data->fe_y, 1));
51 secp256k1_gej_set_ge(&data->gej_x, &data->ge_x);
52 secp256k1_gej_set_ge(&data->gej_y, &data->ge_y);
53 memcpy(data->data, init_x, 32);
54 memcpy(data->data + 32, init_y, 32);
57 void bench_scalar_add(void* arg) {
59 bench_inv_t *data = (bench_inv_t*)arg;
61 for (i = 0; i < 2000000; i++) {
62 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
66 void bench_scalar_negate(void* arg) {
68 bench_inv_t *data = (bench_inv_t*)arg;
70 for (i = 0; i < 2000000; i++) {
71 secp256k1_scalar_negate(&data->scalar_x, &data->scalar_x);
75 void bench_scalar_sqr(void* arg) {
77 bench_inv_t *data = (bench_inv_t*)arg;
79 for (i = 0; i < 200000; i++) {
80 secp256k1_scalar_sqr(&data->scalar_x, &data->scalar_x);
84 void bench_scalar_mul(void* arg) {
86 bench_inv_t *data = (bench_inv_t*)arg;
88 for (i = 0; i < 200000; i++) {
89 secp256k1_scalar_mul(&data->scalar_x, &data->scalar_x, &data->scalar_y);
93 #ifdef USE_ENDOMORPHISM
94 void bench_scalar_split(void* arg) {
96 bench_inv_t *data = (bench_inv_t*)arg;
98 for (i = 0; i < 20000; i++) {
99 secp256k1_scalar_t l, r;
100 secp256k1_scalar_split_lambda_var(&l, &r, &data->scalar_x);
101 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
106 void bench_scalar_inverse(void* arg) {
108 bench_inv_t *data = (bench_inv_t*)arg;
110 for (i = 0; i < 2000; i++) {
111 secp256k1_scalar_inverse(&data->scalar_x, &data->scalar_x);
112 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
116 void bench_scalar_inverse_var(void* arg) {
118 bench_inv_t *data = (bench_inv_t*)arg;
120 for (i = 0; i < 2000; i++) {
121 secp256k1_scalar_inverse_var(&data->scalar_x, &data->scalar_x);
122 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
126 void bench_field_normalize(void* arg) {
128 bench_inv_t *data = (bench_inv_t*)arg;
130 for (i = 0; i < 2000000; i++) {
131 secp256k1_fe_normalize(&data->fe_x);
135 void bench_field_normalize_weak(void* arg) {
137 bench_inv_t *data = (bench_inv_t*)arg;
139 for (i = 0; i < 2000000; i++) {
140 secp256k1_fe_normalize_weak(&data->fe_x);
144 void bench_field_mul(void* arg) {
146 bench_inv_t *data = (bench_inv_t*)arg;
148 for (i = 0; i < 200000; i++) {
149 secp256k1_fe_mul(&data->fe_x, &data->fe_x, &data->fe_y);
153 void bench_field_sqr(void* arg) {
155 bench_inv_t *data = (bench_inv_t*)arg;
157 for (i = 0; i < 200000; i++) {
158 secp256k1_fe_sqr(&data->fe_x, &data->fe_x);
162 void bench_field_inverse(void* arg) {
164 bench_inv_t *data = (bench_inv_t*)arg;
166 for (i = 0; i < 20000; i++) {
167 secp256k1_fe_inv(&data->fe_x, &data->fe_x);
168 secp256k1_fe_add(&data->fe_x, &data->fe_y);
172 void bench_field_inverse_var(void* arg) {
174 bench_inv_t *data = (bench_inv_t*)arg;
176 for (i = 0; i < 20000; i++) {
177 secp256k1_fe_inv_var(&data->fe_x, &data->fe_x);
178 secp256k1_fe_add(&data->fe_x, &data->fe_y);
182 void bench_field_sqrt_var(void* arg) {
184 bench_inv_t *data = (bench_inv_t*)arg;
186 for (i = 0; i < 20000; i++) {
187 secp256k1_fe_sqrt_var(&data->fe_x, &data->fe_x);
188 secp256k1_fe_add(&data->fe_x, &data->fe_y);
192 void bench_group_double_var(void* arg) {
194 bench_inv_t *data = (bench_inv_t*)arg;
196 for (i = 0; i < 200000; i++) {
197 secp256k1_gej_double_var(&data->gej_x, &data->gej_x, NULL);
201 void bench_group_add_var(void* arg) {
203 bench_inv_t *data = (bench_inv_t*)arg;
205 for (i = 0; i < 200000; i++) {
206 secp256k1_gej_add_var(&data->gej_x, &data->gej_x, &data->gej_y, NULL);
210 void bench_group_add_affine(void* arg) {
212 bench_inv_t *data = (bench_inv_t*)arg;
214 for (i = 0; i < 200000; i++) {
215 secp256k1_gej_add_ge(&data->gej_x, &data->gej_x, &data->ge_y);
219 void bench_group_add_affine_var(void* arg) {
221 bench_inv_t *data = (bench_inv_t*)arg;
223 for (i = 0; i < 200000; i++) {
224 secp256k1_gej_add_ge_var(&data->gej_x, &data->gej_x, &data->ge_y, NULL);
228 void bench_ecmult_wnaf(void* arg) {
230 bench_inv_t *data = (bench_inv_t*)arg;
232 for (i = 0; i < 20000; i++) {
233 secp256k1_ecmult_wnaf(data->wnaf, 256, &data->scalar_x, WINDOW_A);
234 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
239 void bench_sha256(void* arg) {
241 bench_inv_t *data = (bench_inv_t*)arg;
242 secp256k1_sha256_t sha;
244 for (i = 0; i < 20000; i++) {
245 secp256k1_sha256_initialize(&sha);
246 secp256k1_sha256_write(&sha, data->data, 32);
247 secp256k1_sha256_finalize(&sha, data->data);
251 void bench_hmac_sha256(void* arg) {
253 bench_inv_t *data = (bench_inv_t*)arg;
254 secp256k1_hmac_sha256_t hmac;
256 for (i = 0; i < 20000; i++) {
257 secp256k1_hmac_sha256_initialize(&hmac, data->data, 32);
258 secp256k1_hmac_sha256_write(&hmac, data->data, 32);
259 secp256k1_hmac_sha256_finalize(&hmac, data->data);
263 void bench_rfc6979_hmac_sha256(void* arg) {
265 bench_inv_t *data = (bench_inv_t*)arg;
266 secp256k1_rfc6979_hmac_sha256_t rng;
268 for (i = 0; i < 20000; i++) {
269 secp256k1_rfc6979_hmac_sha256_initialize(&rng, data->data, 64);
270 secp256k1_rfc6979_hmac_sha256_generate(&rng, data->data, 32);
275 int have_flag(int argc, char** argv, char *flag) {
276 char** argm = argv + argc;
281 while (argv != NULL && argv != argm) {
282 if (strcmp(*argv, flag) == 0) return 1;
288 int main(int argc, char **argv) {
290 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "add")) run_benchmark("scalar_add", bench_scalar_add, bench_setup, NULL, &data, 10, 2000000);
291 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "negate")) run_benchmark("scalar_negate", bench_scalar_negate, bench_setup, NULL, &data, 10, 2000000);
292 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "sqr")) run_benchmark("scalar_sqr", bench_scalar_sqr, bench_setup, NULL, &data, 10, 200000);
293 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "mul")) run_benchmark("scalar_mul", bench_scalar_mul, bench_setup, NULL, &data, 10, 200000);
294 #ifdef USE_ENDOMORPHISM
295 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "split")) run_benchmark("scalar_split", bench_scalar_split, bench_setup, NULL, &data, 10, 20000);
297 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "inverse")) run_benchmark("scalar_inverse", bench_scalar_inverse, bench_setup, NULL, &data, 10, 2000);
298 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);
300 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "normalize")) run_benchmark("field_normalize", bench_field_normalize, bench_setup, NULL, &data, 10, 2000000);
301 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);
302 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "sqr")) run_benchmark("field_sqr", bench_field_sqr, bench_setup, NULL, &data, 10, 200000);
303 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "mul")) run_benchmark("field_mul", bench_field_mul, bench_setup, NULL, &data, 10, 200000);
304 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "inverse")) run_benchmark("field_inverse", bench_field_inverse, bench_setup, NULL, &data, 10, 20000);
305 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);
306 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);
308 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);
309 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);
310 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);
311 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);
313 if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("ecmult_wnaf", bench_ecmult_wnaf, bench_setup, NULL, &data, 10, 20000);
315 if (have_flag(argc, argv, "hash") || have_flag(argc, argv, "sha256")) run_benchmark("hash_sha256", bench_sha256, bench_setup, NULL, &data, 10, 20000);
316 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);
317 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);