|
6 | 6 |
|
7 | 7 | #include <stdint.h>
|
8 | 8 |
|
| 9 | +#include "include/secp256k1.h" |
9 | 10 | #include "include/secp256k1_bulletproofs.h"
|
10 | 11 | #include "util.h"
|
11 | 12 | #include "bench.h"
|
12 | 13 |
|
13 | 14 | typedef struct {
|
14 | 15 | 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; |
15 | 26 | } bench_bulletproofs_data;
|
16 | 27 |
|
17 | 28 | 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 | + } |
19 | 53 | }
|
20 | 54 |
|
21 |
| -static void bench_bulletproofs(void* arg, int iters) { |
| 55 | +static void bench_bulletproofs_verify(void* arg, int iters) { |
22 | 56 | bench_bulletproofs_data *data = (bench_bulletproofs_data*)arg;
|
| 57 | + int i; |
23 | 58 |
|
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 | + } |
26 | 62 | }
|
27 | 63 |
|
28 | 64 | int main(void) {
|
29 | 65 | bench_bulletproofs_data data;
|
30 | 66 | int iters = get_iters(32);
|
| 67 | + int i; |
31 | 68 |
|
32 | 69 | 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 | + } |
33 | 79 |
|
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 | + } |
35 | 86 |
|
| 87 | + secp256k1_scratch_space_destroy(data.ctx, data.scratch); |
| 88 | + secp256k1_bulletproofs_generators_destroy(data.ctx, data.gens); |
36 | 89 | secp256k1_context_destroy(data.ctx);
|
37 | 90 | return 0;
|
38 | 91 | }
|
0 commit comments