Skip to content

Commit 422dd69

Browse files
committed
bulletproofs: add benchmark
1 parent 682a8c5 commit 422dd69

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

src/bench_bulletproofs.c

+58-5
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,86 @@
66

77
#include <stdint.h>
88

9+
#include "include/secp256k1.h"
910
#include "include/secp256k1_bulletproofs.h"
1011
#include "util.h"
1112
#include "bench.h"
1213

1314
typedef struct {
1415
secp256k1_context* ctx;
16+
secp256k1_bulletproofs_generators* gens;
17+
secp256k1_scratch_space *scratch;
18+
secp256k1_pedersen_commitment commit;
19+
unsigned char proof[SECP256K1_BULLETPROOFS_RANGEPROOF_UNCOMPRESSED_MAX_LENGTH_];
20+
unsigned char blind[32];
21+
unsigned char nonce[32];
22+
size_t proof_len;
23+
size_t n_bits;
24+
uint64_t min_value;
25+
uint64_t value;
1526
} bench_bulletproofs_data;
1627

1728
static void bench_bulletproofs_setup(void* arg) {
18-
(void) arg;
29+
bench_bulletproofs_data *data = (bench_bulletproofs_data*)arg;
30+
31+
data->min_value = 99;
32+
data->value = 100;
33+
data->proof_len = sizeof(data->proof);
34+
memset(data->blind, 0x77, 32);
35+
memset(data->nonce, 0x0, 32);
36+
CHECK(secp256k1_pedersen_commit(data->ctx, &data->commit, data->blind, data->value, secp256k1_generator_h));
37+
38+
CHECK(secp256k1_bulletproofs_rangeproof_uncompressed_prove(data->ctx, data->gens, secp256k1_generator_h, data->proof, &data->proof_len, data->n_bits, data->value, data->min_value, &data->commit, data->blind, data->nonce, NULL, NULL, 0));
39+
CHECK(secp256k1_bulletproofs_rangeproof_uncompressed_verify(data->ctx, data->scratch, data->gens, secp256k1_generator_h, data->proof, data->proof_len, data->min_value, &data->commit, NULL, 0));
40+
}
41+
42+
static void bench_bulletproofs_prove(void* arg, int iters) {
43+
bench_bulletproofs_data *data = (bench_bulletproofs_data*)arg;
44+
int i;
45+
46+
for (i = 0; i < iters; i++) {
47+
data->nonce[1] = i;
48+
data->nonce[2] = i >> 8;
49+
data->nonce[3] = i >> 16;
50+
data->proof_len = sizeof(data->proof);
51+
CHECK(secp256k1_bulletproofs_rangeproof_uncompressed_prove(data->ctx, data->gens, secp256k1_generator_h, data->proof, &data->proof_len, data->n_bits, data->value, data->min_value, &data->commit, data->blind, data->nonce, NULL, NULL, 0));
52+
}
1953
}
2054

21-
static void bench_bulletproofs(void* arg, int iters) {
55+
static void bench_bulletproofs_verify(void* arg, int iters) {
2256
bench_bulletproofs_data *data = (bench_bulletproofs_data*)arg;
57+
int i;
2358

24-
(void) data;
25-
(void) iters;
59+
for (i = 0; i < iters; i++) {
60+
CHECK(secp256k1_bulletproofs_rangeproof_uncompressed_verify(data->ctx, data->scratch, data->gens, secp256k1_generator_h, data->proof, data->proof_len, data->min_value, &data->commit, NULL, 0));
61+
}
2662
}
2763

2864
int main(void) {
2965
bench_bulletproofs_data data;
3066
int iters = get_iters(32);
67+
int i;
3168

3269
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
70+
data.gens = secp256k1_bulletproofs_generators_create(data.ctx, 128);
71+
data.scratch = secp256k1_scratch_space_create(data.ctx, 100 * 1024);
72+
73+
for (i = 0; i <= 6; i++) {
74+
char test_name[64];
75+
data.n_bits = 1ul << i;
76+
sprintf(test_name, "bulletproofs_uncompressed_prove_%i", 1 << i);
77+
run_benchmark(test_name, bench_bulletproofs_prove, bench_bulletproofs_setup, NULL, &data, 20, iters);
78+
}
3379

34-
run_benchmark("bulletproofs_verify_bit", bench_bulletproofs, bench_bulletproofs_setup, NULL, &data, 10, iters);
80+
for (i = 0; i <= 6; i++) {
81+
char test_name[64];
82+
data.n_bits = 1ul << i;
83+
sprintf(test_name, "bulletproofs_uncompressed_verif_%i", 1 << i);
84+
run_benchmark(test_name, bench_bulletproofs_verify, bench_bulletproofs_setup, NULL, &data, 20, iters);
85+
}
3586

87+
secp256k1_scratch_space_destroy(data.ctx, data.scratch);
88+
secp256k1_bulletproofs_generators_destroy(data.ctx, data.gens);
3689
secp256k1_context_destroy(data.ctx);
3790
return 0;
3891
}

0 commit comments

Comments
 (0)