github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/crypto/secp256k1/libsecp256k1/src/bench_internal.c (about) 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" 16 #include "ecmult_const_impl.h" 17 #include "ecmult_impl.h" 18 #include "bench.h" 19 #include "secp256k1.c" 20 21 typedef struct { 22 secp256k1_scalar scalar_x, scalar_y; 23 secp256k1_fe fe_x, fe_y; 24 secp256k1_ge ge_x, ge_y; 25 secp256k1_gej gej_x, gej_y; 26 unsigned char data[64]; 27 int wnaf[256]; 28 } bench_inv_t; 29 30 void bench_setup(void* arg) { 31 bench_inv_t *data = (bench_inv_t*)arg; 32 33 static const unsigned char init_x[32] = { 34 0x02, 0x03, 0x05, 0x07, 0x0b, 0x0d, 0x11, 0x13, 35 0x17, 0x1d, 0x1f, 0x25, 0x29, 0x2b, 0x2f, 0x35, 36 0x3b, 0x3d, 0x43, 0x47, 0x49, 0x4f, 0x53, 0x59, 37 0x61, 0x65, 0x67, 0x6b, 0x6d, 0x71, 0x7f, 0x83 38 }; 39 40 static const unsigned char init_y[32] = { 41 0x82, 0x83, 0x85, 0x87, 0x8b, 0x8d, 0x81, 0x83, 42 0x97, 0xad, 0xaf, 0xb5, 0xb9, 0xbb, 0xbf, 0xc5, 43 0xdb, 0xdd, 0xe3, 0xe7, 0xe9, 0xef, 0xf3, 0xf9, 44 0x11, 0x15, 0x17, 0x1b, 0x1d, 0xb1, 0xbf, 0xd3 45 }; 46 47 secp256k1_scalar_set_b32(&data->scalar_x, init_x, NULL); 48 secp256k1_scalar_set_b32(&data->scalar_y, init_y, NULL); 49 secp256k1_fe_set_b32(&data->fe_x, init_x); 50 secp256k1_fe_set_b32(&data->fe_y, init_y); 51 CHECK(secp256k1_ge_set_xo_var(&data->ge_x, &data->fe_x, 0)); 52 CHECK(secp256k1_ge_set_xo_var(&data->ge_y, &data->fe_y, 1)); 53 secp256k1_gej_set_ge(&data->gej_x, &data->ge_x); 54 secp256k1_gej_set_ge(&data->gej_y, &data->ge_y); 55 memcpy(data->data, init_x, 32); 56 memcpy(data->data + 32, init_y, 32); 57 } 58 59 void bench_scalar_add(void* arg) { 60 int i; 61 bench_inv_t *data = (bench_inv_t*)arg; 62 63 for (i = 0; i < 2000000; i++) { 64 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y); 65 } 66 } 67 68 void bench_scalar_negate(void* arg) { 69 int i; 70 bench_inv_t *data = (bench_inv_t*)arg; 71 72 for (i = 0; i < 2000000; i++) { 73 secp256k1_scalar_negate(&data->scalar_x, &data->scalar_x); 74 } 75 } 76 77 void bench_scalar_sqr(void* arg) { 78 int i; 79 bench_inv_t *data = (bench_inv_t*)arg; 80 81 for (i = 0; i < 200000; i++) { 82 secp256k1_scalar_sqr(&data->scalar_x, &data->scalar_x); 83 } 84 } 85 86 void bench_scalar_mul(void* arg) { 87 int i; 88 bench_inv_t *data = (bench_inv_t*)arg; 89 90 for (i = 0; i < 200000; i++) { 91 secp256k1_scalar_mul(&data->scalar_x, &data->scalar_x, &data->scalar_y); 92 } 93 } 94 95 #ifdef USE_ENDOMORPHISM 96 void bench_scalar_split(void* arg) { 97 int i; 98 bench_inv_t *data = (bench_inv_t*)arg; 99 100 for (i = 0; i < 20000; i++) { 101 secp256k1_scalar l, r; 102 secp256k1_scalar_split_lambda(&l, &r, &data->scalar_x); 103 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y); 104 } 105 } 106 #endif 107 108 void bench_scalar_inverse(void* arg) { 109 int i; 110 bench_inv_t *data = (bench_inv_t*)arg; 111 112 for (i = 0; i < 2000; i++) { 113 secp256k1_scalar_inverse(&data->scalar_x, &data->scalar_x); 114 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y); 115 } 116 } 117 118 void bench_scalar_inverse_var(void* arg) { 119 int i; 120 bench_inv_t *data = (bench_inv_t*)arg; 121 122 for (i = 0; i < 2000; i++) { 123 secp256k1_scalar_inverse_var(&data->scalar_x, &data->scalar_x); 124 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y); 125 } 126 } 127 128 void bench_field_normalize(void* arg) { 129 int i; 130 bench_inv_t *data = (bench_inv_t*)arg; 131 132 for (i = 0; i < 2000000; i++) { 133 secp256k1_fe_normalize(&data->fe_x); 134 } 135 } 136 137 void bench_field_normalize_weak(void* arg) { 138 int i; 139 bench_inv_t *data = (bench_inv_t*)arg; 140 141 for (i = 0; i < 2000000; i++) { 142 secp256k1_fe_normalize_weak(&data->fe_x); 143 } 144 } 145 146 void bench_field_mul(void* arg) { 147 int i; 148 bench_inv_t *data = (bench_inv_t*)arg; 149 150 for (i = 0; i < 200000; i++) { 151 secp256k1_fe_mul(&data->fe_x, &data->fe_x, &data->fe_y); 152 } 153 } 154 155 void bench_field_sqr(void* arg) { 156 int i; 157 bench_inv_t *data = (bench_inv_t*)arg; 158 159 for (i = 0; i < 200000; i++) { 160 secp256k1_fe_sqr(&data->fe_x, &data->fe_x); 161 } 162 } 163 164 void bench_field_inverse(void* arg) { 165 int i; 166 bench_inv_t *data = (bench_inv_t*)arg; 167 168 for (i = 0; i < 20000; i++) { 169 secp256k1_fe_inv(&data->fe_x, &data->fe_x); 170 secp256k1_fe_add(&data->fe_x, &data->fe_y); 171 } 172 } 173 174 void bench_field_inverse_var(void* arg) { 175 int i; 176 bench_inv_t *data = (bench_inv_t*)arg; 177 178 for (i = 0; i < 20000; i++) { 179 secp256k1_fe_inv_var(&data->fe_x, &data->fe_x); 180 secp256k1_fe_add(&data->fe_x, &data->fe_y); 181 } 182 } 183 184 void bench_field_sqrt_var(void* arg) { 185 int i; 186 bench_inv_t *data = (bench_inv_t*)arg; 187 188 for (i = 0; i < 20000; i++) { 189 secp256k1_fe_sqrt_var(&data->fe_x, &data->fe_x); 190 secp256k1_fe_add(&data->fe_x, &data->fe_y); 191 } 192 } 193 194 void bench_group_double_var(void* arg) { 195 int i; 196 bench_inv_t *data = (bench_inv_t*)arg; 197 198 for (i = 0; i < 200000; i++) { 199 secp256k1_gej_double_var(&data->gej_x, &data->gej_x, NULL); 200 } 201 } 202 203 void bench_group_add_var(void* arg) { 204 int i; 205 bench_inv_t *data = (bench_inv_t*)arg; 206 207 for (i = 0; i < 200000; i++) { 208 secp256k1_gej_add_var(&data->gej_x, &data->gej_x, &data->gej_y, NULL); 209 } 210 } 211 212 void bench_group_add_affine(void* arg) { 213 int i; 214 bench_inv_t *data = (bench_inv_t*)arg; 215 216 for (i = 0; i < 200000; i++) { 217 secp256k1_gej_add_ge(&data->gej_x, &data->gej_x, &data->ge_y); 218 } 219 } 220 221 void bench_group_add_affine_var(void* arg) { 222 int i; 223 bench_inv_t *data = (bench_inv_t*)arg; 224 225 for (i = 0; i < 200000; i++) { 226 secp256k1_gej_add_ge_var(&data->gej_x, &data->gej_x, &data->ge_y, NULL); 227 } 228 } 229 230 void bench_ecmult_wnaf(void* arg) { 231 int i; 232 bench_inv_t *data = (bench_inv_t*)arg; 233 234 for (i = 0; i < 20000; i++) { 235 secp256k1_ecmult_wnaf(data->wnaf, 256, &data->scalar_x, WINDOW_A); 236 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y); 237 } 238 } 239 240 void bench_wnaf_const(void* arg) { 241 int i; 242 bench_inv_t *data = (bench_inv_t*)arg; 243 244 for (i = 0; i < 20000; i++) { 245 secp256k1_wnaf_const(data->wnaf, data->scalar_x, WINDOW_A); 246 secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y); 247 } 248 } 249 250 251 void bench_sha256(void* arg) { 252 int i; 253 bench_inv_t *data = (bench_inv_t*)arg; 254 secp256k1_sha256_t sha; 255 256 for (i = 0; i < 20000; i++) { 257 secp256k1_sha256_initialize(&sha); 258 secp256k1_sha256_write(&sha, data->data, 32); 259 secp256k1_sha256_finalize(&sha, data->data); 260 } 261 } 262 263 void bench_hmac_sha256(void* arg) { 264 int i; 265 bench_inv_t *data = (bench_inv_t*)arg; 266 secp256k1_hmac_sha256_t hmac; 267 268 for (i = 0; i < 20000; i++) { 269 secp256k1_hmac_sha256_initialize(&hmac, data->data, 32); 270 secp256k1_hmac_sha256_write(&hmac, data->data, 32); 271 secp256k1_hmac_sha256_finalize(&hmac, data->data); 272 } 273 } 274 275 void bench_rfc6979_hmac_sha256(void* arg) { 276 int i; 277 bench_inv_t *data = (bench_inv_t*)arg; 278 secp256k1_rfc6979_hmac_sha256_t rng; 279 280 for (i = 0; i < 20000; i++) { 281 secp256k1_rfc6979_hmac_sha256_initialize(&rng, data->data, 64); 282 secp256k1_rfc6979_hmac_sha256_generate(&rng, data->data, 32); 283 } 284 } 285 286 void bench_context_verify(void* arg) { 287 int i; 288 (void)arg; 289 for (i = 0; i < 20; i++) { 290 secp256k1_context_destroy(secp256k1_context_create(SECP256K1_CONTEXT_VERIFY)); 291 } 292 } 293 294 void bench_context_sign(void* arg) { 295 int i; 296 (void)arg; 297 for (i = 0; i < 200; i++) { 298 secp256k1_context_destroy(secp256k1_context_create(SECP256K1_CONTEXT_SIGN)); 299 } 300 } 301 302 303 int have_flag(int argc, char** argv, char *flag) { 304 char** argm = argv + argc; 305 argv++; 306 if (argv == argm) { 307 return 1; 308 } 309 while (argv != NULL && argv != argm) { 310 if (strcmp(*argv, flag) == 0) { 311 return 1; 312 } 313 argv++; 314 } 315 return 0; 316 } 317 318 int main(int argc, char **argv) { 319 bench_inv_t data; 320 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "add")) run_benchmark("scalar_add", bench_scalar_add, bench_setup, NULL, &data, 10, 2000000); 321 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "negate")) run_benchmark("scalar_negate", bench_scalar_negate, bench_setup, NULL, &data, 10, 2000000); 322 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "sqr")) run_benchmark("scalar_sqr", bench_scalar_sqr, bench_setup, NULL, &data, 10, 200000); 323 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "mul")) run_benchmark("scalar_mul", bench_scalar_mul, bench_setup, NULL, &data, 10, 200000); 324 #ifdef USE_ENDOMORPHISM 325 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "split")) run_benchmark("scalar_split", bench_scalar_split, bench_setup, NULL, &data, 10, 20000); 326 #endif 327 if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "inverse")) run_benchmark("scalar_inverse", bench_scalar_inverse, bench_setup, NULL, &data, 10, 2000); 328 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); 329 330 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "normalize")) run_benchmark("field_normalize", bench_field_normalize, bench_setup, NULL, &data, 10, 2000000); 331 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); 332 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "sqr")) run_benchmark("field_sqr", bench_field_sqr, bench_setup, NULL, &data, 10, 200000); 333 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "mul")) run_benchmark("field_mul", bench_field_mul, bench_setup, NULL, &data, 10, 200000); 334 if (have_flag(argc, argv, "field") || have_flag(argc, argv, "inverse")) run_benchmark("field_inverse", bench_field_inverse, bench_setup, NULL, &data, 10, 20000); 335 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); 336 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); 337 338 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); 339 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); 340 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); 341 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); 342 343 if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("wnaf_const", bench_wnaf_const, bench_setup, NULL, &data, 10, 20000); 344 if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("ecmult_wnaf", bench_ecmult_wnaf, bench_setup, NULL, &data, 10, 20000); 345 346 if (have_flag(argc, argv, "hash") || have_flag(argc, argv, "sha256")) run_benchmark("hash_sha256", bench_sha256, bench_setup, NULL, &data, 10, 20000); 347 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); 348 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); 349 350 if (have_flag(argc, argv, "context") || have_flag(argc, argv, "verify")) run_benchmark("context_verify", bench_context_verify, bench_setup, NULL, &data, 10, 20); 351 if (have_flag(argc, argv, "context") || have_flag(argc, argv, "sign")) run_benchmark("context_sign", bench_context_sign, bench_setup, NULL, &data, 10, 200); 352 353 return 0; 354 }