99
1010#include <stdint.h>
1111
12+ #include "group_impl.h"
1213#include "include/secp256k1_bulletproofs.h"
1314#include "bulletproofs_pp_norm_product_impl.h"
15+ #include "bulletproofs_pp_rangeproof_impl.h"
1416#include "bulletproofs_util.h"
17+ #include "modules/generator/main_impl.h"
1518
1619static void test_bulletproofs_generators_api (void ) {
1720 /* The BP generator API requires no precomp */
@@ -207,7 +210,40 @@ void norm_arg_test(unsigned int n, unsigned int m) {
207210 secp256k1_bulletproofs_generators_destroy (ctx , gs );
208211}
209212
213+ void rangeproof_test (size_t digit_base , size_t num_bits , uint64_t value , uint64_t min_value ) {
214+ secp256k1_generator asset_genp ;
215+ size_t plen ;
216+ size_t num_digits = num_bits /secp256k1_bulletproofs_pp_log2 (digit_base );
217+ size_t n = num_digits > digit_base ? num_digits : digit_base ;
218+ size_t res ;
219+ secp256k1_pedersen_commitment commit ;
220+ secp256k1_context * secp_ctx = secp256k1_context_create (SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY );
221+ const unsigned char blind [32 ] = "help me! i'm bliiiiiiiiiiiiiiind" ;
222+ const unsigned char nonce [32 ] = "nonce? non ce n'est vrai amirite" ;
223+ /* Extra commit is a Joan Shelley lyric */
224+ const unsigned char extra_commit [] = "Shock of teal blue beneath clouds gathering, and the light of empty black on the waves at the horizon" ;
225+ const size_t extra_commit_len = sizeof (extra_commit );
226+ secp256k1_sha256 transcript ;
227+ const secp256k1_bulletproofs_generators * gs = secp256k1_bulletproofs_generators_create (secp_ctx , n + 8 );
228+ secp256k1_scratch * scratch = secp256k1_scratch_space_create (secp_ctx , 1000 * 1000 ); /* shouldn't need much */
229+ unsigned char proof [1000 ];
230+ plen = 1000 ;
231+ asset_genp = * secp256k1_generator_h ;
232+ CHECK (secp256k1_pedersen_commit (secp_ctx , & commit , blind , value , & asset_genp ));
233+ secp256k1_bulletproofs_generators_serialize (secp_ctx , gs , proof , & plen );
234+ plen = 1000 ;
235+ secp256k1_sha256_initialize (& transcript );
236+
237+
238+ res = secp256k1_bulletproofs_pp_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 );
239+ CHECK (res == 1 );
240+
241+ res = secp256k1_bulletproofs_pp_rangeproof_verify (secp_ctx , scratch , gs , & asset_genp , proof , plen , num_bits , digit_base , min_value , & commit , extra_commit , extra_commit_len );
242+ CHECK (res == 1 );
243+ }
244+
210245void run_bulletproofs_tests (void ) {
246+ size_t i ;
211247 test_log_exp ();
212248 test_norm_util_helpers ();
213249 test_bulletproofs_generators_api ();
@@ -220,6 +256,18 @@ void run_bulletproofs_tests(void) {
220256 norm_arg_test (32 , 64 );
221257 norm_arg_test (64 , 32 );
222258 norm_arg_test (64 , 64 );
259+ for (i = 0 ; i < 16 ; i ++ ) {
260+ rangeproof_test (2 , 4 , i , i /2 );
261+ }
262+
263+ rangeproof_test (16 , 4 , 7 , 3 );
264+ rangeproof_test (16 , 8 , 243 , 129 );
265+ rangeproof_test (16 , 16 , 12431 , 6332 );
266+ rangeproof_test (16 , 32 , 134132 , 57251 );
267+ for (i = 0 ; i < 100 ; i ++ ) {
268+ uint64_t v = secp256k1_testrand64 ();
269+ rangeproof_test (16 , 64 , v , 0 );
270+ }
223271}
224272
225273#endif
0 commit comments