Skip to content

Commit 90582d8

Browse files
committed
Add benchmarks
1 parent d11862d commit 90582d8

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/bench_bulletproofs_pp.c

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**********************************************************************
2+
* Copyright (c) 2020 Andrew Poelstra *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5+
**********************************************************************/
6+
7+
#include <stdint.h>
8+
9+
#include "include/secp256k1.h"
10+
#include "include/secp256k1_bulletproofs.h"
11+
#include "util.h"
12+
#include "bench.h"
13+
14+
typedef struct {
15+
secp256k1_context* ctx;
16+
secp256k1_bulletproofs_generators* gens;
17+
secp256k1_scratch_space *scratch;
18+
secp256k1_pedersen_commitment commit;
19+
unsigned char proof[1000];
20+
unsigned char blind[32];
21+
unsigned char nonce[32];
22+
size_t proof_len;
23+
size_t n_bits;
24+
size_t base;
25+
uint64_t min_value;
26+
uint64_t value;
27+
} bench_bulletproofs_data;
28+
29+
static void bench_bulletproofs_setup(void* arg) {
30+
bench_bulletproofs_data *data = (bench_bulletproofs_data*)arg;
31+
32+
data->min_value = 0;
33+
data->value = 100;
34+
data->proof_len = sizeof(data->proof);
35+
memset(data->blind, 0x77, 32);
36+
memset(data->nonce, 0x0, 32);
37+
CHECK(secp256k1_pedersen_commit(data->ctx, &data->commit, data->blind, data->value, secp256k1_generator_h));
38+
39+
CHECK(secp256k1_bulletproofs_pp_rangeproof_prove(data->ctx, data->scratch, data->gens, secp256k1_generator_h, data->proof, &data->proof_len, data->n_bits, data->base, data->value, 0, &data->commit, data->blind, data->nonce, NULL, 0));
40+
CHECK(secp256k1_bulletproofs_pp_rangeproof_verify(data->ctx, data->scratch, data->gens, secp256k1_generator_h, data->proof, data->proof_len, data->n_bits, data->base, data->min_value, &data->commit, NULL, 0));
41+
}
42+
43+
static void bench_bulletproofs_prove(void* arg, int iters) {
44+
bench_bulletproofs_data *data = (bench_bulletproofs_data*)arg;
45+
int i;
46+
47+
for (i = 0; i < iters; i++) {
48+
data->nonce[1] = i;
49+
data->nonce[2] = i >> 8;
50+
data->nonce[3] = i >> 16;
51+
data->proof_len = sizeof(data->proof);
52+
CHECK(secp256k1_bulletproofs_pp_rangeproof_prove(data->ctx, data->scratch, data->gens, secp256k1_generator_h, data->proof, &data->proof_len, data->n_bits, data->base, data->value, 0, &data->commit, data->blind, data->nonce, NULL, 0));
53+
}
54+
}
55+
56+
static void bench_bulletproofs_verify(void* arg, int iters) {
57+
bench_bulletproofs_data *data = (bench_bulletproofs_data*)arg;
58+
int i;
59+
60+
for (i = 0; i < iters; i++) {
61+
CHECK(secp256k1_bulletproofs_pp_rangeproof_verify(data->ctx, data->scratch, data->gens, secp256k1_generator_h, data->proof, data->proof_len, data->n_bits, data->base, data->min_value, &data->commit, NULL, 0));
62+
}
63+
}
64+
65+
int main(void) {
66+
bench_bulletproofs_data data;
67+
int iters = get_iters(32);
68+
char test_name[64];
69+
70+
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
71+
data.gens = secp256k1_bulletproofs_generators_create(data.ctx, 24);
72+
data.scratch = secp256k1_scratch_space_create(data.ctx, 8000 * 1024);
73+
74+
data.n_bits = 1ul << 6;
75+
data.base = 16;
76+
sprintf(test_name, "bulletproofs_uncompressed_prove_64bits_16base");
77+
run_benchmark(test_name, bench_bulletproofs_prove, bench_bulletproofs_setup, NULL, &data, 20, iters);
78+
79+
sprintf(test_name, "bulletproofs_uncompressed_verify_64bits_16base");
80+
run_benchmark(test_name, bench_bulletproofs_verify, bench_bulletproofs_setup, NULL, &data, 20, iters);
81+
82+
secp256k1_scratch_space_destroy(data.ctx, data.scratch);
83+
secp256k1_bulletproofs_generators_destroy(data.ctx, data.gens);
84+
secp256k1_context_destroy(data.ctx);
85+
86+
return 0;
87+
}

src/modules/bulletproofs/Makefile.am.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ noinst_HEADERS += src/modules/bulletproofs/tests_impl.h
88

99
if USE_BENCHMARK
1010
noinst_PROGRAMS += bench_bulletproofs
11-
bench_bulletproofs_SOURCES = src/bench_bulletproofs.c
11+
bench_bulletproofs_SOURCES = src/bench_bulletproofs_pp.c
1212
bench_bulletproofs_LDADD = libsecp256k1.la $(SECP_LIBS)
1313
bench_bulletproofs_LDFLAGS = -static
1414
endif

0 commit comments

Comments
 (0)