Bitcoin Core  0.19.99
P2P Digital Currency
bench_internal.c
Go to the documentation of this file.
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 {
26  unsigned char data[64];
27  int wnaf[256];
28 } bench_inv;
29 
30 void bench_setup(void* arg) {
31  bench_inv *data = (bench_inv*)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 *data = (bench_inv*)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 *data = (bench_inv*)arg;
71 
72  for (i = 0; i < 2000000; i++) {
74  }
75 }
76 
77 void bench_scalar_sqr(void* arg) {
78  int i;
79  bench_inv *data = (bench_inv*)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 *data = (bench_inv*)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 *data = (bench_inv*)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 *data = (bench_inv*)arg;
111 
112  for (i = 0; i < 2000; i++) {
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 *data = (bench_inv*)arg;
121 
122  for (i = 0; i < 2000; i++) {
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 *data = (bench_inv*)arg;
131 
132  for (i = 0; i < 2000000; i++) {
134  }
135 }
136 
137 void bench_field_normalize_weak(void* arg) {
138  int i;
139  bench_inv *data = (bench_inv*)arg;
140 
141  for (i = 0; i < 2000000; i++) {
143  }
144 }
145 
146 void bench_field_mul(void* arg) {
147  int i;
148  bench_inv *data = (bench_inv*)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 *data = (bench_inv*)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 *data = (bench_inv*)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 *data = (bench_inv*)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(void* arg) {
185  int i;
186  bench_inv *data = (bench_inv*)arg;
187  secp256k1_fe t;
188 
189  for (i = 0; i < 20000; i++) {
190  t = data->fe_x;
191  secp256k1_fe_sqrt(&data->fe_x, &t);
192  secp256k1_fe_add(&data->fe_x, &data->fe_y);
193  }
194 }
195 
196 void bench_group_double_var(void* arg) {
197  int i;
198  bench_inv *data = (bench_inv*)arg;
199 
200  for (i = 0; i < 200000; i++) {
201  secp256k1_gej_double_var(&data->gej_x, &data->gej_x, NULL);
202  }
203 }
204 
205 void bench_group_add_var(void* arg) {
206  int i;
207  bench_inv *data = (bench_inv*)arg;
208 
209  for (i = 0; i < 200000; i++) {
210  secp256k1_gej_add_var(&data->gej_x, &data->gej_x, &data->gej_y, NULL);
211  }
212 }
213 
214 void bench_group_add_affine(void* arg) {
215  int i;
216  bench_inv *data = (bench_inv*)arg;
217 
218  for (i = 0; i < 200000; i++) {
219  secp256k1_gej_add_ge(&data->gej_x, &data->gej_x, &data->ge_y);
220  }
221 }
222 
223 void bench_group_add_affine_var(void* arg) {
224  int i;
225  bench_inv *data = (bench_inv*)arg;
226 
227  for (i = 0; i < 200000; i++) {
228  secp256k1_gej_add_ge_var(&data->gej_x, &data->gej_x, &data->ge_y, NULL);
229  }
230 }
231 
232 void bench_group_jacobi_var(void* arg) {
233  int i;
234  bench_inv *data = (bench_inv*)arg;
235 
236  for (i = 0; i < 20000; i++) {
238  }
239 }
240 
241 void bench_ecmult_wnaf(void* arg) {
242  int i;
243  bench_inv *data = (bench_inv*)arg;
244 
245  for (i = 0; i < 20000; i++) {
246  secp256k1_ecmult_wnaf(data->wnaf, 256, &data->scalar_x, WINDOW_A);
247  secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
248  }
249 }
250 
251 void bench_wnaf_const(void* arg) {
252  int i;
253  bench_inv *data = (bench_inv*)arg;
254 
255  for (i = 0; i < 20000; i++) {
256  secp256k1_wnaf_const(data->wnaf, data->scalar_x, WINDOW_A, 256);
257  secp256k1_scalar_add(&data->scalar_x, &data->scalar_x, &data->scalar_y);
258  }
259 }
260 
261 
262 void bench_sha256(void* arg) {
263  int i;
264  bench_inv *data = (bench_inv*)arg;
265  secp256k1_sha256 sha;
266 
267  for (i = 0; i < 20000; i++) {
269  secp256k1_sha256_write(&sha, data->data, 32);
270  secp256k1_sha256_finalize(&sha, data->data);
271  }
272 }
273 
274 void bench_hmac_sha256(void* arg) {
275  int i;
276  bench_inv *data = (bench_inv*)arg;
278 
279  for (i = 0; i < 20000; i++) {
280  secp256k1_hmac_sha256_initialize(&hmac, data->data, 32);
281  secp256k1_hmac_sha256_write(&hmac, data->data, 32);
282  secp256k1_hmac_sha256_finalize(&hmac, data->data);
283  }
284 }
285 
286 void bench_rfc6979_hmac_sha256(void* arg) {
287  int i;
288  bench_inv *data = (bench_inv*)arg;
290 
291  for (i = 0; i < 20000; i++) {
294  }
295 }
296 
297 void bench_context_verify(void* arg) {
298  int i;
299  (void)arg;
300  for (i = 0; i < 20; i++) {
302  }
303 }
304 
305 void bench_context_sign(void* arg) {
306  int i;
307  (void)arg;
308  for (i = 0; i < 200; i++) {
310  }
311 }
312 
313 #ifndef USE_NUM_NONE
314 void bench_num_jacobi(void* arg) {
315  int i;
316  bench_inv *data = (bench_inv*)arg;
317  secp256k1_num nx, norder;
318 
319  secp256k1_scalar_get_num(&nx, &data->scalar_x);
321  secp256k1_scalar_get_num(&norder, &data->scalar_y);
322 
323  for (i = 0; i < 200000; i++) {
324  secp256k1_num_jacobi(&nx, &norder);
325  }
326 }
327 #endif
328 
329 int main(int argc, char **argv) {
330  bench_inv data;
331  if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "add")) run_benchmark("scalar_add", bench_scalar_add, bench_setup, NULL, &data, 10, 2000000);
332  if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "negate")) run_benchmark("scalar_negate", bench_scalar_negate, bench_setup, NULL, &data, 10, 2000000);
333  if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "sqr")) run_benchmark("scalar_sqr", bench_scalar_sqr, bench_setup, NULL, &data, 10, 200000);
334  if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "mul")) run_benchmark("scalar_mul", bench_scalar_mul, bench_setup, NULL, &data, 10, 200000);
335 #ifdef USE_ENDOMORPHISM
336  if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "split")) run_benchmark("scalar_split", bench_scalar_split, bench_setup, NULL, &data, 10, 20000);
337 #endif
338  if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "inverse")) run_benchmark("scalar_inverse", bench_scalar_inverse, bench_setup, NULL, &data, 10, 2000);
339  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);
340 
341  if (have_flag(argc, argv, "field") || have_flag(argc, argv, "normalize")) run_benchmark("field_normalize", bench_field_normalize, bench_setup, NULL, &data, 10, 2000000);
342  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);
343  if (have_flag(argc, argv, "field") || have_flag(argc, argv, "sqr")) run_benchmark("field_sqr", bench_field_sqr, bench_setup, NULL, &data, 10, 200000);
344  if (have_flag(argc, argv, "field") || have_flag(argc, argv, "mul")) run_benchmark("field_mul", bench_field_mul, bench_setup, NULL, &data, 10, 200000);
345  if (have_flag(argc, argv, "field") || have_flag(argc, argv, "inverse")) run_benchmark("field_inverse", bench_field_inverse, bench_setup, NULL, &data, 10, 20000);
346  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);
347  if (have_flag(argc, argv, "field") || have_flag(argc, argv, "sqrt")) run_benchmark("field_sqrt", bench_field_sqrt, bench_setup, NULL, &data, 10, 20000);
348 
349  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);
350  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);
351  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);
352  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);
353  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, 20000);
354 
355  if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("wnaf_const", bench_wnaf_const, bench_setup, NULL, &data, 10, 20000);
356  if (have_flag(argc, argv, "ecmult") || have_flag(argc, argv, "wnaf")) run_benchmark("ecmult_wnaf", bench_ecmult_wnaf, bench_setup, NULL, &data, 10, 20000);
357 
358  if (have_flag(argc, argv, "hash") || have_flag(argc, argv, "sha256")) run_benchmark("hash_sha256", bench_sha256, bench_setup, NULL, &data, 10, 20000);
359  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);
360  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);
361 
362  if (have_flag(argc, argv, "context") || have_flag(argc, argv, "verify")) run_benchmark("context_verify", bench_context_verify, bench_setup, NULL, &data, 10, 20);
363  if (have_flag(argc, argv, "context") || have_flag(argc, argv, "sign")) run_benchmark("context_sign", bench_context_sign, bench_setup, NULL, &data, 10, 200);
364 
365 #ifndef USE_NUM_NONE
366  if (have_flag(argc, argv, "num") || have_flag(argc, argv, "jacobi")) run_benchmark("num_jacobi", bench_num_jacobi, bench_setup, NULL, &data, 10, 200000);
367 #endif
368  return 0;
369 }
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Multiply two scalars (modulo the group order).
secp256k1_fe fe_y
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b.
void bench_context_sign(void *arg)
static int secp256k1_gej_has_quad_y_var(const secp256k1_gej *a)
Check whether a group element&#39;s y coordinate is a quadratic residue.
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
Sets a field element to be the product of two others.
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256 *rng, const unsigned char *key, size_t keylen)
static void secp256k1_scalar_get_num(secp256k1_num *r, const secp256k1_scalar *a)
Convert a scalar to a number.
secp256k1_scalar scalar_x
void bench_num_jacobi(void *arg)
static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256 *hash, const unsigned char *key, size_t size)
secp256k1_ge ge_y
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
void bench_field_normalize(void *arg)
void bench_group_add_var(void *arg)
void bench_scalar_mul(void *arg)
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:24
void bench_group_jacobi_var(void *arg)
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:168
void bench_field_sqr(void *arg)
static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a)
Adds a field element to another.
static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b (with b given in affine coordinates).
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Set r equal to the double of a.
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object.
Definition: secp256k1.c:101
secp256k1_scalar scalar_y
void run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
Definition: bench.h:34
static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the inverse of a scalar (modulo the group order).
secp256k1_ge ge_x
void bench_scalar_inverse(void *arg)
static int secp256k1_wnaf_const(int *wnaf, secp256k1_scalar s, int w, int size)
Convert a number to WNAF notation.
void bench_field_inverse_var(void *arg)
secp256k1_gej gej_y
void bench_scalar_negate(void *arg)
static int secp256k1_num_jacobi(const secp256k1_num *a, const secp256k1_num *b)
Compute the jacobi symbol (a|b).
static void secp256k1_hmac_sha256_write(secp256k1_hmac_sha256 *hash, const unsigned char *data, size_t size)
void bench_wnaf_const(void *arg)
static void secp256k1_scalar_sqr(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the square of a scalar (modulo the group order).
secp256k1_fe fe_x
void bench_group_add_affine(void *arg)
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:14
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
Weakly normalize a field element: reduce it magnitude to 1, but don&#39;t fully normalize.
void bench_scalar_inverse_var(void *arg)
static void secp256k1_hmac_sha256_finalize(secp256k1_hmac_sha256 *hash, unsigned char *out32)
void bench_rfc6979_hmac_sha256(void *arg)
#define CHECK(cond)
Definition: util.h:52
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
#define WINDOW_A
Definition: ecmult_impl.h:33
void bench_ecmult_wnaf(void *arg)
void bench_field_normalize_weak(void *arg)
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t size)
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Set a group element (affine) equal to the point with the given X coordinate, and given oddness for Y...
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a)
Sets a field element to be the square of another.
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
Set a field element equal to 32-byte big endian value.
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create.
Definition: secp256k1.h:167
void bench_group_double_var(void *arg)
void bench_field_sqrt(void *arg)
void bench_hmac_sha256(void *arg)
unsigned char data[64]
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Add two scalars together (modulo the group order).
static void secp256k1_scalar_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the inverse of a scalar (modulo the group order), without constant-time guarantee.
void * memcpy(void *a, const void *b, size_t c)
static void secp256k1_fe_normalize(secp256k1_fe *r)
Field element module.
void bench_sha256(void *arg)
void bench_setup(void *arg)
int wnaf[256]
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity).
int main(int argc, char **argv)
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash)
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
void bench_scalar_add(void *arg)
static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a, int w)
Convert a number to WNAF notation.
Definition: ecmult_impl.h:375
static void secp256k1_scalar_order_get_num(secp256k1_num *r)
Get the order of the group as a number.
static int secp256k1_fe_sqrt(secp256k1_fe *r, const secp256k1_fe *a)
If a has a square root, it is computed in r and 1 is returned.
void bench_group_add_affine_var(void *arg)
void bench_field_mul(void *arg)
static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a)
Potentially faster version of secp256k1_fe_inv, without constant-time guarantee.
void bench_context_verify(void *arg)
int have_flag(int argc, char **argv, char *flag)
Definition: bench.h:67
static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a)
Sets a field element to be the (modular) inverse of another.
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object.
Definition: secp256k1.c:67
static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out32)
void bench_field_inverse(void *arg)
secp256k1_gej gej_x
void bench_scalar_sqr(void *arg)
static void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256 *rng, unsigned char *out, size_t outlen)