Skip to content

Commit 9f5cef2

Browse files
committed
Add rangeproof unit tests
1 parent a06826e commit 9f5cef2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/modules/bppp/tests_impl.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,42 @@ static void norm_arg_prove_vectors(void) {
591591

592592
#undef IDX_TO_TEST
593593

594+
static void rangeproof_test(size_t digit_base, size_t num_bits, uint64_t value, uint64_t min_value) {
595+
secp256k1_generator asset_genp;
596+
size_t plen;
597+
size_t num_digits = num_bits/secp256k1_bppp_log2(digit_base);
598+
size_t n = num_digits > digit_base ? num_digits : digit_base;
599+
size_t res;
600+
secp256k1_pedersen_commitment commit;
601+
const unsigned char blind[32] = "help me! i'm bliiiiiiiiiiiiiiind";
602+
const unsigned char nonce[32] = "nonce? non ce n'est vrai amirite";
603+
/* Extra commit is a Joan Shelley lyric */
604+
const unsigned char extra_commit[] = "Shock of teal blue beneath clouds gathering, and the light of empty black on the waves at the horizon";
605+
const size_t extra_commit_len = sizeof(extra_commit);
606+
secp256k1_sha256 transcript;
607+
const secp256k1_bppp_generators *gs = secp256k1_bppp_generators_create(CTX, n + 8);
608+
secp256k1_scratch *scratch = secp256k1_scratch_space_create(CTX, 1000*1000); /* shouldn't need much */
609+
unsigned char proof[1000];
610+
plen = 1000;
611+
asset_genp = *secp256k1_generator_h;
612+
CHECK(secp256k1_pedersen_commit(CTX, &commit, blind, value, &asset_genp));
613+
secp256k1_sha256_initialize(&transcript);
614+
615+
616+
res = secp256k1_bppp_rangeproof_prove(CTX, scratch, gs, &asset_genp, proof, &plen, num_bits, digit_base, value, min_value, &commit, blind, nonce, extra_commit, extra_commit_len);
617+
CHECK(res == 1);
618+
619+
res = secp256k1_bppp_rangeproof_verify(CTX, scratch, gs, &asset_genp, proof, plen, num_bits, digit_base, min_value, &commit, extra_commit, extra_commit_len);
620+
CHECK(res == 1);
621+
622+
proof[plen - 1] ^= 1;
623+
res = secp256k1_bppp_rangeproof_verify(CTX, scratch, gs, &asset_genp, proof, plen, num_bits, digit_base, min_value, &commit, extra_commit, extra_commit_len);
624+
CHECK(res == 0);
625+
}
626+
594627
static void run_bppp_tests(void) {
628+
/* Update the global context for all bppp tests*/
629+
size_t i;
595630
test_log_exp();
596631
test_norm_util_helpers();
597632
test_serialize_two_points();
@@ -609,6 +644,19 @@ static void run_bppp_tests(void) {
609644

610645
norm_arg_verify_vectors();
611646
norm_arg_prove_vectors();
647+
648+
for (i = 0; i < 16; i++) {
649+
rangeproof_test(2, 4, i, i/2);
650+
}
651+
652+
rangeproof_test(16, 4, 7, 3);
653+
rangeproof_test(16, 8, 243, 129);
654+
rangeproof_test(16, 16, 12431, 6332);
655+
rangeproof_test(16, 32, 134132, 57251);
656+
for (i = 0; i < 100; i++) {
657+
uint64_t v = secp256k1_testrand64();
658+
rangeproof_test(16, 64, v, 0);
659+
}
612660
}
613661

614662
#endif

0 commit comments

Comments
 (0)