|
7 | 7 | #include <stdint.h>
|
8 | 8 |
|
9 | 9 | #include "../include/secp256k1_bppp.h"
|
| 10 | +#include "../include/secp256k1.h" |
10 | 11 | #include "util.h"
|
11 | 12 | #include "bench.h"
|
12 | 13 |
|
| 14 | +#define MAX_PROOF_SIZE 500 |
| 15 | + |
13 | 16 | typedef struct {
|
14 | 17 | secp256k1_context* ctx;
|
| 18 | + secp256k1_bppp_generators* gens; |
| 19 | + secp256k1_scratch_space *scratch; |
| 20 | + secp256k1_pedersen_commitment commit; |
| 21 | + unsigned char *proofs; |
| 22 | + unsigned char blind[32]; |
| 23 | + unsigned char nonce[32]; |
| 24 | + size_t proof_len; |
| 25 | + size_t n_bits; |
| 26 | + size_t base; |
| 27 | + uint64_t min_value; |
| 28 | + uint64_t value; |
15 | 29 | } bench_bppp_data;
|
16 | 30 |
|
17 | 31 | static void bench_bppp_setup(void* arg) {
|
18 |
| - (void) arg; |
| 32 | + bench_bppp_data *data = (bench_bppp_data*)arg; |
| 33 | + |
| 34 | + data->min_value = 0; |
| 35 | + data->value = 100; |
| 36 | + data->proof_len = MAX_PROOF_SIZE; |
| 37 | + memset(data->blind, 0x77, 32); |
| 38 | + memset(data->nonce, 0x0, 32); |
| 39 | + CHECK(secp256k1_pedersen_commit(data->ctx, &data->commit, data->blind, data->value, secp256k1_generator_h)); |
| 40 | + |
| 41 | + CHECK(secp256k1_bppp_rangeproof_prove(data->ctx, data->scratch, data->gens, secp256k1_generator_h, data->proofs, &data->proof_len, data->n_bits, data->base, data->value, 0, &data->commit, data->blind, data->nonce, NULL, 0)); |
| 42 | + CHECK(secp256k1_bppp_rangeproof_verify(data->ctx, data->scratch, data->gens, secp256k1_generator_h, data->proofs, data->proof_len, data->n_bits, data->base, data->min_value, &data->commit, NULL, 0)); |
| 43 | +} |
| 44 | + |
| 45 | +static void bench_bppp_prove(void* arg, int iters) { |
| 46 | + bench_bppp_data *data = (bench_bppp_data*)arg; |
| 47 | + int i; |
| 48 | + |
| 49 | + for (i = 0; i < iters; i++) { |
| 50 | + data->nonce[1] = i; |
| 51 | + data->nonce[2] = i >> 8; |
| 52 | + data->nonce[3] = i >> 16; |
| 53 | + data->proof_len = MAX_PROOF_SIZE; |
| 54 | + CHECK(secp256k1_bppp_rangeproof_prove(data->ctx, data->scratch, data->gens, secp256k1_generator_h, &data->proofs[i*MAX_PROOF_SIZE], &data->proof_len, data->n_bits, data->base, data->value, 0, &data->commit, data->blind, data->nonce, NULL, 0)); |
| 55 | + } |
19 | 56 | }
|
20 | 57 |
|
21 |
| -static void bench_bppp(void* arg, int iters) { |
| 58 | +static void bench_bppp_verify(void* arg, int iters) { |
22 | 59 | bench_bppp_data *data = (bench_bppp_data*)arg;
|
| 60 | + int i; |
23 | 61 |
|
24 |
| - (void) data; |
25 |
| - (void) iters; |
| 62 | + for (i = 0; i < iters; i++) { |
| 63 | + CHECK(secp256k1_bppp_rangeproof_verify(data->ctx, data->scratch, data->gens, secp256k1_generator_h, &data->proofs[i*MAX_PROOF_SIZE], data->proof_len, data->n_bits, data->base, data->min_value, &data->commit, NULL, 0)); |
| 64 | + } |
26 | 65 | }
|
27 | 66 |
|
28 | 67 | int main(void) {
|
29 | 68 | bench_bppp_data data;
|
30 |
| - int iters = get_iters(32); |
| 69 | + int iters = get_iters(64); |
| 70 | + char test_name[64]; |
31 | 71 |
|
32 | 72 | data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
|
| 73 | + data.gens = secp256k1_bppp_generators_create(data.ctx, 24); |
| 74 | + data.scratch = secp256k1_scratch_space_create(data.ctx, 80 * 1024); |
| 75 | + data.proofs = (unsigned char *)malloc(iters * MAX_PROOF_SIZE); |
33 | 76 |
|
34 |
| - run_benchmark("bppp_verify_bit", bench_bppp, bench_bppp_setup, NULL, &data, 10, iters); |
| 77 | + data.n_bits = 1ul << 6; |
| 78 | + data.base = 16; |
| 79 | + sprintf(test_name, "bppp_prove_64bits_16base"); |
| 80 | + run_benchmark(test_name, bench_bppp_prove, bench_bppp_setup, NULL, &data, 4, iters); |
35 | 81 |
|
| 82 | + sprintf(test_name, "bppp_verify_64bits_16base"); |
| 83 | + run_benchmark(test_name, bench_bppp_verify, bench_bppp_setup, NULL, &data, 20, iters); |
| 84 | + |
| 85 | + secp256k1_scratch_space_destroy(data.ctx, data.scratch); |
| 86 | + free(data.proofs); |
| 87 | + secp256k1_bppp_generators_destroy(data.ctx, data.gens); |
36 | 88 | secp256k1_context_destroy(data.ctx);
|
| 89 | + |
37 | 90 | return 0;
|
38 | 91 | }
|
0 commit comments