11#include <stdio.h>
22#include <string.h>
3- #include <assert .h>
3+ #include <stdlib .h>
44#include <time.h>
5- #include <openssl/rand.h>
65#include <secp256k1.h>
7-
86#include "secp256k1_mpt.h"
7+ #include "test_utils.h"
98
109/* ---- Aggregation parameters ---- */
1110#define M 2
1514/* ---- Benchmark parameters ---- */
1615#define VERIFY_RUNS 5
1716
18- /* ---- Prototypes ---- */
19- int secp256k1_bulletproof_prove_agg (
20- const secp256k1_context * ctx ,
21- unsigned char * proof_out ,
22- size_t * proof_len ,
23- const uint64_t * values ,
24- const unsigned char * blindings_flat ,
25- size_t m ,
26- const secp256k1_pubkey * pk_base ,
27- const unsigned char * context_id
28- );
29-
30- int secp256k1_bulletproof_verify_agg (
31- const secp256k1_context * ctx ,
32- const secp256k1_pubkey * G_vec ,
33- const secp256k1_pubkey * H_vec ,
34- const unsigned char * proof ,
35- size_t proof_len ,
36- const secp256k1_pubkey * commitment_C_vec ,
37- size_t m ,
38- const secp256k1_pubkey * pk_base ,
39- const unsigned char * context_id
40- );
41-
42- int secp256k1_bulletproof_create_commitment (
43- const secp256k1_context * ctx ,
44- secp256k1_pubkey * commitment_C ,
45- uint64_t value ,
46- const unsigned char * blinding_factor ,
47- const secp256k1_pubkey * pk_base
48- );
49-
50- extern int secp256k1_mpt_get_generator_vector (
51- const secp256k1_context * ctx ,
52- secp256k1_pubkey * vec ,
53- size_t n ,
54- const unsigned char * label ,
55- size_t label_len
56- );
5717
5818/* ---- Helpers ---- */
5919
@@ -62,49 +22,32 @@ static inline double elapsed_ms(struct timespec a, struct timespec b) {
6222 (b .tv_nsec - a .tv_nsec ) / 1e6 ;
6323}
6424
65- static void random_scalar (
66- const secp256k1_context * ctx ,
67- unsigned char out [32 ]
68- ) {
69- do {
70- RAND_bytes (out , 32 );
71- } while (!secp256k1_ec_seckey_verify (ctx , out ));
72- }
73-
7425/* ---- Main ---- */
7526int main (void ) {
7627 printf ("[TEST] Aggregated Bulletproof test (m = %d)\n" , M );
7728
7829 /* ---- Context ---- */
7930 secp256k1_context * ctx =
8031 secp256k1_context_create (SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY );
32+ EXPECT (ctx != NULL );
8133
8234 /* ---- Values ---- */
8335 uint64_t values [M ] = { 5000 , 123456 };
8436 unsigned char blindings [M ][32 ];
8537 secp256k1_pubkey commitments [M ];
8638
87- /**
88- * CONTEXT BINDING:
89- * In the production system, this ID is derived deterministically:
90- * TransactionContextID := H(TxType || Account || MPTokenIssuanceID || ...)
91- * * See [Spec Section 3.3.1] for the derivation rules.
92- * * For this library unit test, random bytes suffice to verify that the
93- * proof binds correctly to *whatever* context ID is provided.
94- */
39+ /* ---- Context Binding ---- */
9540 unsigned char context_id [32 ];
96- RAND_bytes (context_id , 32 );
97-
41+ EXPECT (RAND_bytes (context_id , 32 ) == 1 );
9842
9943 secp256k1_pubkey pk_base ;
100- /* Use the standard H generator from the library */
101- assert (secp256k1_mpt_get_h_generator (ctx , & pk_base ));
102-
44+ /* Use the standard H generator from the library */
45+ EXPECT (secp256k1_mpt_get_h_generator (ctx , & pk_base ));
10346
10447 /* ---- Commitments ---- */
10548 for (size_t i = 0 ; i < M ; i ++ ) {
10649 random_scalar (ctx , blindings [i ]);
107- assert (secp256k1_bulletproof_create_commitment (
50+ EXPECT (secp256k1_bulletproof_create_commitment (
10851 ctx ,
10952 & commitments [i ],
11053 values [i ],
@@ -116,11 +59,11 @@ int main(void) {
11659 const size_t n = BP_TOTAL_BITS (M );
11760 secp256k1_pubkey * G_vec = malloc (n * sizeof (secp256k1_pubkey ));
11861 secp256k1_pubkey * H_vec = malloc (n * sizeof (secp256k1_pubkey ));
119- assert (G_vec && H_vec );
62+ EXPECT (G_vec && H_vec );
12063
121- assert (secp256k1_mpt_get_generator_vector (
64+ EXPECT (secp256k1_mpt_get_generator_vector (
12265 ctx , G_vec , n , (const unsigned char * )"G" , 1 ));
123- assert (secp256k1_mpt_get_generator_vector (
66+ EXPECT (secp256k1_mpt_get_generator_vector (
12467 ctx , H_vec , n , (const unsigned char * )"H" , 1 ));
12568
12669 /* ---- Prove (timed) ---- */
@@ -132,7 +75,8 @@ int main(void) {
13275 struct timespec t_p_start , t_p_end ;
13376 clock_gettime (CLOCK_MONOTONIC , & t_p_start );
13477
135- assert (secp256k1_bulletproof_prove_agg (
78+ /* Note: We cast the 2D array 'blindings' to flat pointer */
79+ EXPECT (secp256k1_bulletproof_prove_agg (
13680 ctx ,
13781 proof ,
13882 & proof_len ,
@@ -167,10 +111,7 @@ int main(void) {
167111
168112 clock_gettime (CLOCK_MONOTONIC , & t_v_end );
169113
170- if (!ok ) {
171- printf ("FAILED\n" );
172- return 1 ;
173- }
114+ EXPECT (ok );
174115
175116 printf ("PASSED\n" );
176117 printf ("[BENCH] Verification time (single): %.3f ms\n" ,
@@ -196,7 +137,7 @@ int main(void) {
196137
197138 clock_gettime (CLOCK_MONOTONIC , & te );
198139
199- assert (ok );
140+ EXPECT (ok );
200141 total_ms += elapsed_ms (ts , te );
201142 }
202143
@@ -212,10 +153,11 @@ int main(void) {
212153 unsigned char bad_blinding [32 ];
213154 random_scalar (ctx , bad_blinding );
214155
215- assert (secp256k1_bulletproof_create_commitment (
156+ /* Create a fake commitment to (value + 1) to break the sum */
157+ EXPECT (secp256k1_bulletproof_create_commitment (
216158 ctx ,
217159 & bad_commitments [1 ],
218- values [1 ] + 1 ,
160+ values [M - 1 ] + 1 ,
219161 bad_blinding ,
220162 & pk_base ));
221163
@@ -231,8 +173,8 @@ int main(void) {
231173 context_id );
232174
233175 if (ok ) {
234- printf ( "FAILED (accepted invalid proof) \n" );
235- return 1 ;
176+ fprintf ( stderr , "FAILED: Accepted invalid proof! \n" );
177+ exit ( EXIT_FAILURE ) ;
236178 }
237179
238180 printf ("PASSED (rejected invalid proof)\n" );
0 commit comments