Skip to content

Commit da6288c

Browse files
committed
Add rangeproof unit tests
1 parent 60a448c commit da6288c

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/modules/bppp/tests_impl.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,41 @@ void norm_arg_verify_vectors(void) {
552552
}
553553
#undef IDX_TO_TEST
554554

555+
void rangeproof_test(size_t digit_base, size_t num_bits, uint64_t value, uint64_t min_value) {
556+
secp256k1_generator asset_genp;
557+
size_t plen;
558+
size_t num_digits = num_bits/secp256k1_bppp_log2(digit_base);
559+
size_t n = num_digits > digit_base ? num_digits : digit_base;
560+
size_t res;
561+
secp256k1_pedersen_commitment commit;
562+
secp256k1_context* secp_ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
563+
const unsigned char blind[32] = "help me! i'm bliiiiiiiiiiiiiiind";
564+
const unsigned char nonce[32] = "nonce? non ce n'est vrai amirite";
565+
/* Extra commit is a Joan Shelley lyric */
566+
const unsigned char extra_commit[] = "Shock of teal blue beneath clouds gathering, and the light of empty black on the waves at the horizon";
567+
const size_t extra_commit_len = sizeof(extra_commit);
568+
secp256k1_sha256 transcript;
569+
const secp256k1_bppp_generators *gs = secp256k1_bppp_generators_create(secp_ctx, n + 8);
570+
secp256k1_scratch *scratch = secp256k1_scratch_space_create(secp_ctx, 1000*1000); /* shouldn't need much */
571+
unsigned char proof[1000];
572+
plen = 1000;
573+
asset_genp = *secp256k1_generator_h;
574+
CHECK(secp256k1_pedersen_commit(secp_ctx, &commit, blind, value, &asset_genp));
575+
secp256k1_bppp_generators_serialize(secp_ctx, gs, proof, &plen);
576+
plen = 1000;
577+
secp256k1_sha256_initialize(&transcript);
578+
579+
580+
res = secp256k1_bppp_rangeproof_prove(secp_ctx, scratch, gs, &asset_genp, proof, &plen, num_bits, digit_base, value, min_value, &commit, blind, nonce, extra_commit, extra_commit_len);
581+
CHECK(res == 1);
582+
583+
res = secp256k1_bppp_rangeproof_verify(secp_ctx, scratch, gs, &asset_genp, proof, plen, num_bits, digit_base, min_value, &commit, extra_commit, extra_commit_len);
584+
CHECK(res == 1);
585+
}
586+
555587
void run_bppp_tests(void) {
556588
/* Update the global context for all bppp tests*/
589+
size_t i;
557590
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
558591
test_log_exp();
559592
test_norm_util_helpers();
@@ -570,6 +603,19 @@ void run_bppp_tests(void) {
570603
norm_arg_test(64, 32);
571604
norm_arg_test(64, 64);
572605
norm_arg_verify_vectors();
606+
607+
for (i = 0; i < 16; i++) {
608+
rangeproof_test(2, 4, i, i/2);
609+
}
610+
611+
rangeproof_test(16, 4, 7, 3);
612+
rangeproof_test(16, 8, 243, 129);
613+
rangeproof_test(16, 16, 12431, 6332);
614+
rangeproof_test(16, 32, 134132, 57251);
615+
for (i = 0; i < 100; i++) {
616+
uint64_t v = secp256k1_testrand64();
617+
rangeproof_test(16, 64, v, 0);
618+
}
573619
}
574620

575621
#endif

0 commit comments

Comments
 (0)