 |
PRCYCoin
2.0.0.7rc1
P2P Digital Currency
|
Go to the documentation of this file.
15 #define MAX_PROOF_SIZE 2000
16 #define CIRCUIT_DIR "src/modules/bulletproofs/bin_circuits/"
21 unsigned char nonce[32];
49 const unsigned char nonce[32] =
"my kingdom for some randomness!!";
50 const unsigned char genbd[32] =
"yet more blinding, for the asset";
55 for (i = 0; i < data->
n_proofs; i++) {
62 static void bench_bulletproof_rangeproof_setup(
void* arg) {
67 unsigned char blind[32] =
"and my kingdom too for a blinder";
69 bench_bulletproof_common_setup (data->
common);
80 data->
blind[i] = malloc(32);
84 data->
value[i] = i * 17;
91 CHECK(
secp256k1_bulletproof_rangeproof_prove(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, data->
common->
proof[0], &data->
common->
plen, data->
value, NULL, data->
blind, data->
n_commits, data->
common->
value_gen, data->
nbits, data->
common->
nonce, NULL, 0) == 1);
94 CHECK(
secp256k1_bulletproof_rangeproof_verify(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, data->
common->
proof[i], data->
common->
plen, NULL, data->
commit[i], data->
n_commits, data->
nbits, &data->
common->
value_gen[0], NULL, 0) == 1);
96 CHECK(
secp256k1_bulletproof_rangeproof_verify(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, data->
common->
proof[0], data->
common->
plen, NULL, data->
commit[0], data->
n_commits, data->
nbits, data->
common->
value_gen, NULL, 0) == 1);
97 CHECK(
secp256k1_bulletproof_rangeproof_verify_multi(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, (
const unsigned char **) data->
common->
proof, data->
common->
n_proofs, data->
common->
plen, NULL, (
const secp256k1_pedersen_commitment **) data->
commit, data->
n_commits, data->
nbits, data->
common->
value_gen, NULL, 0) == 1);
99 CHECK(
secp256k1_bulletproof_rangeproof_rewind(data->
common->
ctx, data->
common->
generators, &v, blind, data->
common->
proof[0], data->
common->
plen, 0, data->
commit[0], &data->
common->
value_gen[0], data->
common->
nonce, NULL, 0) == 1);
103 static void bench_bulletproof_circuit_setup(
void* arg) {
107 bench_bulletproof_common_setup (data->
common);
109 CHECK(
secp256k1_bulletproof_circuit_prove(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, data->
circ[0], data->
common->
proof[0], &data->
common->
plen, data->
assn, NULL, 0, data->
common->
nonce, &data->
common->
value_gen[0], NULL, 0) == 1);
114 CHECK(
secp256k1_bulletproof_circuit_verify(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, data->
circ[0], data->
common->
proof[0], data->
common->
plen, NULL, 0, data->
common->
value_gen, NULL, 0) == 1);
115 CHECK(
secp256k1_bulletproof_circuit_verify_multi(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, (
const secp256k1_bulletproof_circuit*
const*) data->
circ, (
const unsigned char **) data->
common->
proof, data->
common->
n_proofs, data->
common->
plen, NULL, NULL, data->
common->
value_gen, NULL, 0) == 1);
121 for (i = 0; i < data->
n_proofs; i++) {
122 free(data->
proof[i]);
128 static void bench_bulletproof_rangeproof_teardown(
void* arg) {
132 if (data->
blind != NULL) {
134 free((
unsigned char*) data->
blind[i]);
137 if (data->
commit != NULL) {
146 bench_bulletproof_common_teardown(data->
common);
149 static void bench_bulletproof_circuit_teardown(
void* arg) {
151 bench_bulletproof_common_teardown(data->
common);
154 static void bench_bulletproof_rangeproof_prove(
void* arg) {
157 for (i = 0; i < 25; i++) {
158 CHECK(
secp256k1_bulletproof_rangeproof_prove(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, data->
common->
proof[0], &data->
common->
plen, data->
value, NULL, data->
blind, data->
n_commits, data->
common->
value_gen, data->
nbits, data->
common->
nonce, NULL, 0) == 1);
163 unsigned char proof[2000];
168 unsigned char blind[32] =
"and my kingdom too for a blinder";
169 unsigned char nonce[32] =
"and my kingdom too for a blinder";
170 unsigned char blinds[nCommits][32];
173 const unsigned char *blind_ptr[nCommits];
174 for (i = 0; i < nCommits; i++) {
175 memcpy(blinds[i], blind, 32);
176 blind_ptr[i] = blinds[i];
179 CHECK(
secp256k1_bulletproof_rangeproof_prove(context, scratch, generators, proof, &len, values, NULL, blind_ptr, nCommits, &
secp256k1_generator_const_h, 64, nonce, NULL, 0) == 1);
181 CHECK(
secp256k1_bulletproof_rangeproof_verify(context, scratch, generators, proof, len, NULL, commitments, nCommits, 64, &
secp256k1_generator_const_h, NULL, 0) == 1);
184 static void bench_bulletproof_rangeproof_verify(
void* arg) {
189 CHECK(
secp256k1_bulletproof_rangeproof_verify_multi(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, (
const unsigned char **) data->
common->
proof, data->
common->
n_proofs, data->
common->
plen, NULL, (
const secp256k1_pedersen_commitment **) data->
commit, data->
n_commits, data->
nbits, data->
common->
value_gen, NULL, 0) == 1);
193 static void bench_bulletproof_rangeproof_rewind_succeed(
void* arg) {
196 unsigned char blind[32];
200 CHECK(
secp256k1_bulletproof_rangeproof_rewind(data->
common->
ctx, data->
common->
generators, &v, blind, data->
common->
proof[0], data->
common->
plen, 0, data->
commit[0], &data->
common->
value_gen[0], data->
common->
nonce, NULL, 0) == 1);
204 static void bench_bulletproof_rangeproof_rewind_fail(
void* arg) {
207 unsigned char blind[32];
212 CHECK(
secp256k1_bulletproof_rangeproof_rewind(data->
common->
ctx, data->
common->
generators, &v, blind, data->
common->
proof[0], data->
common->
plen, 0, data->
commit[0], &data->
common->
value_gen[0], data->
common->
nonce, NULL, 0) == 0);
217 static void bench_bulletproof_circuit_prove(
void* arg) {
219 CHECK(
secp256k1_bulletproof_circuit_prove(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, data->
circ[0], data->
common->
proof[0], &data->
common->
plen, data->
assn, NULL, 0, data->
common->
nonce, data->
common->
value_gen, NULL, 0) == 1);
222 static void bench_bulletproof_circuit_verify(
void* arg) {
225 for (i = 0; i < 10; i++) {
226 CHECK(
secp256k1_bulletproof_circuit_verify_multi(data->
common->
ctx, data->
common->
scratch, data->
common->
generators, (
const secp256k1_bulletproof_circuit*
const*) data->
circ, (
const unsigned char **) data->
common->
proof, data->
common->
n_proofs, data->
common->
plen, NULL, NULL, data->
common->
value_gen, NULL, 0) == 1);
238 sprintf(str,
"bulletproof_prove, %i, %i, 0, ", (
int)nbits, (
int) n_commits);
239 run_benchmark(str, bench_bulletproof_rangeproof_prove, bench_bulletproof_rangeproof_setup, bench_bulletproof_rangeproof_teardown, (
void *)data, 5, 25);
242 sprintf(str,
"bulletproof_verify, %i, %i, 1, ", (
int)nbits, (
int) n_commits);
243 run_benchmark(str, bench_bulletproof_rangeproof_verify, bench_bulletproof_rangeproof_setup, bench_bulletproof_rangeproof_teardown, (
void *)data, 5, data->common->iters);
245 if (n_commits == 1) {
246 sprintf(str,
"bulletproof_rewind_succeed, %i, ", (
int)nbits);
247 run_benchmark(str, bench_bulletproof_rangeproof_rewind_succeed, bench_bulletproof_rangeproof_setup, bench_bulletproof_rangeproof_teardown, (
void *)data, 5, data->common->iters);
248 sprintf(str,
"bulletproof_rewind_fail, %i, ", (
int)nbits);
249 run_benchmark(str, bench_bulletproof_rangeproof_rewind_fail, bench_bulletproof_rangeproof_setup, bench_bulletproof_rangeproof_teardown, (
void *)data, 5, data->common->iters);
253 sprintf(str,
"bulletproof_verify, %i, %i, 2, ", (
int)nbits, (
int) n_commits);
254 run_benchmark(str, bench_bulletproof_rangeproof_verify, bench_bulletproof_rangeproof_setup, bench_bulletproof_rangeproof_teardown, (
void *)data, 5, data->common->iters);
258 sprintf(str,
"bulletproof_verify, %i, %i, 50, ", (
int)nbits, (
int) n_commits);
259 run_benchmark(str, bench_bulletproof_rangeproof_verify, bench_bulletproof_rangeproof_setup, bench_bulletproof_rangeproof_teardown, (
void *)data, 5, data->common->iters);
263 sprintf(str,
"bulletproof_verify, %i, %i, 100, ", (
int)nbits, (
int) n_commits);
264 run_benchmark(str, bench_bulletproof_rangeproof_verify, bench_bulletproof_rangeproof_setup, bench_bulletproof_rangeproof_teardown, (
void *)data, 5, data->common->iters);
267 sprintf(str,
"bulletproof_verify, %i, %i, 500, ", (
int)nbits, (
int) n_commits);
268 run_benchmark(str, bench_bulletproof_rangeproof_verify, bench_bulletproof_rangeproof_setup, bench_bulletproof_rangeproof_teardown, (
void *)data, 5, data->common->iters);
271 sprintf(str,
"bulletproof_verify, %i, %i, 1000, ", (
int)nbits, (
int) n_commits);
272 run_benchmark(str, bench_bulletproof_rangeproof_verify, bench_bulletproof_rangeproof_setup, bench_bulletproof_rangeproof_teardown, (
void *)data, 5, data->common->iters);
285 for (i = 1; i < 500; i++) {
294 sprintf(fname,
"bulletproof_prove_%s, ",
name);
295 run_benchmark(fname, bench_bulletproof_circuit_prove, bench_bulletproof_circuit_setup, bench_bulletproof_circuit_teardown, (
void *)data, 1, 1);
297 data->common->n_proofs = 1;
298 sprintf(fname,
"bulletproof_verify_%s, %i, ",
name, 1);
299 run_benchmark(fname, bench_bulletproof_circuit_verify, bench_bulletproof_circuit_setup, bench_bulletproof_circuit_teardown, (
void *)data, 2, 10);
301 data->common->n_proofs = 2;
302 sprintf(fname,
"bulletproof_verify_%s, %i, ",
name, 2);
303 run_benchmark(fname, bench_bulletproof_circuit_verify, bench_bulletproof_circuit_setup, bench_bulletproof_circuit_teardown, (
void *)data, 2, 10);
305 data->common->n_proofs = 100;
306 sprintf(fname,
"bulletproof_verify_%s, %i, ",
name, 100);
307 run_benchmark(fname, bench_bulletproof_circuit_verify, bench_bulletproof_circuit_setup, bench_bulletproof_circuit_teardown, (
void *)data, 2, 10);
341 uint64_t values[5] = {300000000000000000, 300000000000000000, 300000000000000000, 400, 500};
343 run_rangeproof_test(&rp_data, 8, 1);
344 run_rangeproof_test(&rp_data, 16, 1);
345 run_rangeproof_test(&rp_data, 32, 1);
347 run_rangeproof_test(&rp_data, 64, 1);
348 run_rangeproof_test(&rp_data, 64, 2);
349 run_rangeproof_test(&rp_data, 64, 4);
350 run_rangeproof_test(&rp_data, 64, 8);
351 run_rangeproof_test(&rp_data, 64, 16);
352 run_rangeproof_test(&rp_data, 64, 32);
353 run_rangeproof_test(&rp_data, 64, 64);
354 run_rangeproof_test(&rp_data, 64, 128);
355 run_rangeproof_test(&rp_data, 64, 256);
356 run_rangeproof_test(&rp_data, 64, 512);
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_pedersen_commit(const secp256k1_context2 *ctx, secp256k1_pedersen_commitment *commit, const unsigned char *blind, uint64_t value, const secp256k1_generator *value_gen, const secp256k1_generator *blind_gen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6)
Generate a Pedersen commitment.
SECP256K1_API void secp256k1_scratch_space_destroy(secp256k1_scratch_space2 *scratch)
Destroy a secp256k1 scratch space.
SECP256K1_WARN_UNUSED_RESULT SECP256K1_API int secp256k1_bulletproof_rangeproof_rewind(const secp256k1_context2 *ctx, const secp256k1_bulletproof_generators *gens, uint64_t *value, unsigned char *blind, const unsigned char *proof, size_t plen, uint64_t min_value, const secp256k1_pedersen_commitment *commit, const secp256k1_generator *value_gen, const unsigned char *nonce, const unsigned char *extra_commit, size_t extra_commit_len) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(8) SECP256K1_ARG_NONNULL(9)
Extracts the value and blinding factor from a single-commit rangeproof given a secret nonce Returns: ...
secp256k1_bulletproof_generators * generators
SECP256K1_API secp256k1_bulletproof_generators * secp256k1_bulletproof_generators_create(const secp256k1_context2 *ctx, const secp256k1_generator *blinding_gen, size_t n) SECP256K1_ARG_NONNULL(1)
Allocates and initializes a list of NUMS generators, along with precomputation data Returns a list of...
secp256k1_pedersen_commitment ** commit
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space2 * secp256k1_scratch_space_create(const secp256k1_context2 *ctx, size_t max_size) SECP256K1_ARG_NONNULL(1)
Create a secp256k1 scratch space object.
void * memcpy(void *a, const void *b, size_t c)
secp256k1_generator blind_gen
secp256k1_bulletproof_circuit_assignment * assn
SECP256K1_API void secp256k1_context_destroy(secp256k1_context2 *ctx)
Destroy a secp256k1 context object.
void simple_test_bench_bulletproof_rangeproof_prove(size_t nCommits, uint64_t *values)
void run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
Opaque data structure that stores a base point.
SECP256K1_WARN_UNUSED_RESULT SECP256K1_API int secp256k1_bulletproof_rangeproof_verify_multi(const secp256k1_context2 *ctx, secp256k1_scratch_space2 *scratch, const secp256k1_bulletproof_generators *gens, const unsigned char *const *proof, size_t n_proofs, size_t plen, const uint64_t *const *min_value, const secp256k1_pedersen_commitment *const *commit, size_t n_commits, size_t nbits, const secp256k1_generator *value_gen, const unsigned char *const *extra_commit, size_t *extra_commit_len) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(8)
Batch-verifies multiple bulletproof (aggregate) rangeproofs of the same size using same generator Ret...
bench_bulletproof_t * common
SECP256K1_WARN_UNUSED_RESULT SECP256K1_API int secp256k1_bulletproof_rangeproof_prove(const secp256k1_context2 *ctx, secp256k1_scratch_space2 *scratch, const secp256k1_bulletproof_generators *gens, unsigned char *proof, size_t *plen, const uint64_t *value, const uint64_t *min_value, const unsigned char *const *blind, size_t n_commits, const secp256k1_generator *value_gen, size_t nbits, const unsigned char *nonce, const unsigned char *extra_commit, size_t extra_commit_len) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(8) SECP256K1_ARG_NONNULL(10) SECP256K1_ARG_NONNULL(12)
Produces an aggregate Bulletproof rangeproof for a set of Pedersen commitments Returns: 1: rangeproof...
void run_circuit_test(bench_bulletproof_circuit_t *data, const char *name)
SECP256K1_WARN_UNUSED_RESULT SECP256K1_API int secp256k1_bulletproof_rangeproof_verify(const secp256k1_context2 *ctx, secp256k1_scratch_space2 *scratch, const secp256k1_bulletproof_generators *gens, const unsigned char *proof, size_t plen, const uint64_t *min_value, const secp256k1_pedersen_commitment *commit, size_t n_commits, size_t nbits, const secp256k1_generator *value_gen, const unsigned char *extra_commit, size_t extra_commit_len) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(7) SECP256K1_ARG_NONNULL(10)
Verifies a single bulletproof (aggregate) rangeproof Returns: 1: rangeproof was valid 0: rangeproof w...
secp256k1_bulletproof_circuit ** circ
SECP256K1_API int secp256k1_bulletproof_circuit_verify(const secp256k1_context2 *ctx, secp256k1_scratch_space2 *scratch, const secp256k1_bulletproof_generators *gens, const secp256k1_bulletproof_circuit *circ, const unsigned char *proof, size_t plen, const secp256k1_pedersen_commitment *commit, size_t n_commits, const secp256k1_generator *value_gen, const unsigned char *extra_commit, size_t extra_commit_len) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(9)
Verifies a single bulletproof zero-knowledge proof (zkp) Returns: 1: zkp accepted 0: zkp did not acce...
SECP256K1_API secp256k1_bulletproof_circuit_assignment * secp256k1_bulletproof_circuit_assignment_decode(const secp256k1_context2 *ctx, const char *fname) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Decodes an accepting wire assignment which is serialized in an opaque binary format Returns a circuit...
SECP256K1_API void secp256k1_bulletproof_circuit_destroy(const secp256k1_context2 *ctx, secp256k1_bulletproof_circuit *circ) SECP256K1_ARG_NONNULL(1)
Destroys a circuit Args: ctx: pointer to a context object (cannot be NULL) gen: pointer to the circui...
SECP256K1_API void secp256k1_bulletproof_generators_destroy(const secp256k1_context2 *ctx, secp256k1_bulletproof_generators *gen) SECP256K1_ARG_NONNULL(1)
Destroys a list of NUMS generators, freeing allocated memory Args: ctx: pointer to a context object (...
Opaque data structure that stores a Pedersen commitment.
const unsigned char ** blind
SECP256K1_API void secp256k1_bulletproof_circuit_assignment_destroy(const secp256k1_context2 *ctx, secp256k1_bulletproof_circuit_assignment *assn) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Destroys a circuit assignment Args: ctx: pointer to a context object (cannot be NULL) gen: pointer to...
const SECP256K1_API secp256k1_generator secp256k1_generator_const_g
Standard secp256k1 generator G.
const SECP256K1_API secp256k1_generator secp256k1_generator_const_h
Alternate secp256k1 generator from Elements Alpha.
secp256k1_generator * value_gen
SECP256K1_API secp256k1_context2 * secp256k1_context_create2(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object.
bench_bulletproof_t * common
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create2.
SECP256K1_API int secp256k1_bulletproof_circuit_verify_multi(const secp256k1_context2 *ctx, secp256k1_scratch_space2 *scratch, const secp256k1_bulletproof_generators *gens, const secp256k1_bulletproof_circuit *const *circ, const unsigned char *const *proof, size_t n_proofs, size_t plen, const secp256k1_pedersen_commitment **commit, size_t *n_commits, const secp256k1_generator *value_gen, const unsigned char **extra_commit, size_t *extra_commit_len) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(10)
Batch-verifies multiple bulletproof zero-knowledge proofs of equally-sized circuits (zkp) Returns: 1:...
secp256k1_bulletproof_circuit * secp256k1_bulletproof_circuit_decode(const secp256k1_context2 *ctx, const char *fname)
Decodes a circuit which is serialized in an opaque binary format Returns a circuit,...
SECP256K1_API int secp256k1_bulletproof_circuit_prove(const secp256k1_context2 *ctx, secp256k1_scratch_space2 *scratch, const secp256k1_bulletproof_generators *gens, const secp256k1_bulletproof_circuit *circ, unsigned char *proof, size_t *plen, const secp256k1_bulletproof_circuit_assignment *assn, const unsigned char **blind, size_t n_commits, const unsigned char *nonce, const secp256k1_generator *value_gen, const unsigned char *extra_commit, size_t extra_commit_len) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7) SECP256K1_ARG_NONNULL(10) SECP256K1_ARG_NONNULL(11)
Produces a bulletproof zero-knowledge proof (zkp) Returns: 1: proof was successfully created 0: pro...
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_generator_generate(const secp256k1_context2 *ctx, secp256k1_generator *gen, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Generate a generator for the curve.
secp256k1_scratch_space2 * scratch
#define SECP256K1_CONTEXT_SIGN