1 /**********************************************************************
2 * Copyright (c) 2017 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"
18 #include "secp256k1.c"
23 /* Setup once in advance */
24 secp256k1_context* ctx;
25 secp256k1_scratch_space* scratch;
26 secp256k1_scalar* scalars;
27 secp256k1_ge* pubkeys;
28 secp256k1_scalar* seckeys;
29 secp256k1_gej* expected_output;
30 secp256k1_ecmult_multi_func ecmult_multi;
32 /* Changes per test */
36 /* Changes per test iteration */
41 secp256k1_gej* output;
44 static int bench_callback(secp256k1_scalar* sc, secp256k1_ge* ge, size_t idx, void* arg) {
45 bench_data* data = (bench_data*)arg;
46 if (data->includes_g) ++idx;
48 *sc = data->scalars[data->offset1];
49 *ge = secp256k1_ge_const_g;
51 *sc = data->scalars[(data->offset1 + idx) % POINTS];
52 *ge = data->pubkeys[(data->offset2 + idx - 1) % POINTS];
57 static void bench_ecmult(void* arg, int iters) {
58 bench_data* data = (bench_data*)arg;
60 int includes_g = data->includes_g;
62 int count = data->count;
63 iters = iters / data->count;
65 for (iter = 0; iter < iters; ++iter) {
66 data->ecmult_multi(&data->ctx->error_callback, &data->ctx->ecmult_ctx, data->scratch, &data->output[iter], data->includes_g ? &data->scalars[data->offset1] : NULL, bench_callback, arg, count - includes_g);
67 data->offset1 = (data->offset1 + count) % POINTS;
68 data->offset2 = (data->offset2 + count - 1) % POINTS;
72 static void bench_ecmult_setup(void* arg) {
73 bench_data* data = (bench_data*)arg;
74 data->offset1 = (data->count * 0x537b7f6f + 0x8f66a481) % POINTS;
75 data->offset2 = (data->count * 0x7f6f537b + 0x6a1a8f49) % POINTS;
78 static void bench_ecmult_teardown(void* arg, int iters) {
79 bench_data* data = (bench_data*)arg;
81 iters = iters / data->count;
82 /* Verify the results in teardown, to avoid doing comparisons while benchmarking. */
83 for (iter = 0; iter < iters; ++iter) {
85 secp256k1_gej_add_var(&tmp, &data->output[iter], &data->expected_output[iter], NULL);
86 CHECK(secp256k1_gej_is_infinity(&tmp));
90 static void generate_scalar(uint32_t num, secp256k1_scalar* scalar) {
91 secp256k1_sha256 sha256;
92 unsigned char c[11] = {'e', 'c', 'm', 'u', 'l', 't', 0, 0, 0, 0};
93 unsigned char buf[32];
99 secp256k1_sha256_initialize(&sha256);
100 secp256k1_sha256_write(&sha256, c, sizeof(c));
101 secp256k1_sha256_finalize(&sha256, buf);
102 secp256k1_scalar_set_b32(scalar, buf, &overflow);
106 static void run_test(bench_data* data, size_t count, int includes_g, int num_iters) {
108 static const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
109 size_t iters = 1 + num_iters / count;
113 data->includes_g = includes_g;
115 /* Compute (the negation of) the expected results directly. */
116 data->offset1 = (data->count * 0x537b7f6f + 0x8f66a481) % POINTS;
117 data->offset2 = (data->count * 0x7f6f537b + 0x6a1a8f49) % POINTS;
118 for (iter = 0; iter < iters; ++iter) {
119 secp256k1_scalar tmp;
120 secp256k1_scalar total = data->scalars[(data->offset1++) % POINTS];
122 for (i = 0; i + 1 < count; ++i) {
123 secp256k1_scalar_mul(&tmp, &data->seckeys[(data->offset2++) % POINTS], &data->scalars[(data->offset1++) % POINTS]);
124 secp256k1_scalar_add(&total, &total, &tmp);
126 secp256k1_scalar_negate(&total, &total);
127 secp256k1_ecmult(&data->ctx->ecmult_ctx, &data->expected_output[iter], NULL, &zero, &total);
130 /* Run the benchmark. */
131 sprintf(str, includes_g ? "ecmult_%ig" : "ecmult_%i", (int)count);
132 run_benchmark(str, bench_ecmult, bench_ecmult_setup, bench_ecmult_teardown, data, 10, count * iters);
135 int main(int argc, char **argv) {
138 secp256k1_gej* pubkeys_gej;
141 int iters = get_iters(10000);
143 data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
144 scratch_size = secp256k1_strauss_scratch_size(POINTS) + STRAUSS_SCRATCH_OBJECTS*16;
145 data.scratch = secp256k1_scratch_space_create(data.ctx, scratch_size);
146 data.ecmult_multi = secp256k1_ecmult_multi_var;
149 if(have_flag(argc, argv, "pippenger_wnaf")) {
150 printf("Using pippenger_wnaf:\n");
151 data.ecmult_multi = secp256k1_ecmult_pippenger_batch_single;
152 } else if(have_flag(argc, argv, "strauss_wnaf")) {
153 printf("Using strauss_wnaf:\n");
154 data.ecmult_multi = secp256k1_ecmult_strauss_batch_single;
155 } else if(have_flag(argc, argv, "simple")) {
156 printf("Using simple algorithm:\n");
157 data.ecmult_multi = secp256k1_ecmult_multi_var;
158 secp256k1_scratch_space_destroy(data.ctx, data.scratch);
161 fprintf(stderr, "%s: unrecognized argument '%s'.\n", argv[0], argv[1]);
162 fprintf(stderr, "Use 'pippenger_wnaf', 'strauss_wnaf', 'simple' or no argument to benchmark a combined algorithm.\n");
168 data.scalars = malloc(sizeof(secp256k1_scalar) * POINTS);
169 data.seckeys = malloc(sizeof(secp256k1_scalar) * POINTS);
170 data.pubkeys = malloc(sizeof(secp256k1_ge) * POINTS);
171 data.expected_output = malloc(sizeof(secp256k1_gej) * (iters + 1));
172 data.output = malloc(sizeof(secp256k1_gej) * (iters + 1));
174 /* Generate a set of scalars, and private/public keypairs. */
175 pubkeys_gej = malloc(sizeof(secp256k1_gej) * POINTS);
176 secp256k1_gej_set_ge(&pubkeys_gej[0], &secp256k1_ge_const_g);
177 secp256k1_scalar_set_int(&data.seckeys[0], 1);
178 for (i = 0; i < POINTS; ++i) {
179 generate_scalar(i, &data.scalars[i]);
181 secp256k1_gej_double_var(&pubkeys_gej[i], &pubkeys_gej[i - 1], NULL);
182 secp256k1_scalar_add(&data.seckeys[i], &data.seckeys[i - 1], &data.seckeys[i - 1]);
185 secp256k1_ge_set_all_gej_var(data.pubkeys, pubkeys_gej, POINTS);
188 for (i = 1; i <= 8; ++i) {
189 run_test(&data, i, 1, iters);
192 /* This is disabled with low count of iterations because the loop runs 77 times even with iters=1
193 * and the higher it goes the longer the computation takes(more points)
194 * So we don't run this benchmark with low iterations to prevent slow down */
196 for (p = 0; p <= 11; ++p) {
197 for (i = 9; i <= 16; ++i) {
198 run_test(&data, i << p, 1, iters);
203 if (data.scratch != NULL) {
204 secp256k1_scratch_space_destroy(data.ctx, data.scratch);
206 secp256k1_context_destroy(data.ctx);
211 free(data.expected_output);