@@ -6,19 +6,68 @@ use std::fs;
66
77use crate :: codec:: { ByteSchnorrCodec , KeccakDuplexSponge } ;
88use crate :: fiat_shamir:: NISigmaProtocol ;
9+ use crate :: tests:: spec:: {
10+ custom_schnorr_protocol:: SchnorrProtocolCustom , random:: SRandom , rng:: TestDRNG ,
11+ } ;
912use crate :: tests:: test_utils:: {
1013 bbs_blind_commitment_computation, discrete_logarithm, dleq, pedersen_commitment,
1114 pedersen_commitment_dleq,
1215} ;
1316
14- use crate :: tests:: spec:: {
15- custom_schnorr_protocol:: SchnorrProtocolCustom , random:: SRandom , rng:: TestDRNG ,
16- } ;
17-
1817type Codec = ByteSchnorrCodec < G , KeccakDuplexSponge > ;
1918type SigmaP = SchnorrProtocolCustom < G > ;
2019type NISigmaP = NISigmaProtocol < SigmaP , Codec > ;
2120
21+ /// Macro to generate non-interactive sigma protocols test functions
22+ macro_rules! generate_ni_function {
23+ ( $name: ident, $test_fn: ident, $( $param: tt) ,* ) => {
24+ #[ allow( non_snake_case) ]
25+ fn $name( seed: & [ u8 ] , context: & [ u8 ] ) -> ( Vec <Scalar >, Vec <u8 >) {
26+ let mut rng = TestDRNG :: new( seed) ;
27+ let ( morphismp, witness) = $test_fn( $( generate_ni_function!( @arg rng, $param) ) ,* ) ;
28+
29+ let protocol = SchnorrProtocolCustom ( morphismp) ;
30+ let domain_sep: Vec <u8 > = context. to_vec( ) ;
31+ let nizk = NISigmaP :: new( & domain_sep, protocol) ;
32+
33+ let proof_bytes = nizk. prove_batchable( & witness, & mut rng) . unwrap( ) ;
34+ let verified = nizk. verify_batchable( & proof_bytes) . is_ok( ) ;
35+ assert!( verified, "Fiat-Shamir Schnorr proof verification failed" ) ;
36+ ( witness, proof_bytes)
37+ }
38+ } ;
39+
40+ ( @arg $rng: ident, $type: ident) => {
41+ G :: $type( & mut $rng)
42+ } ;
43+ ( @arg $rng: ident, [ $type: ident; $count: expr] ) => {
44+ ( 0 ..$count) . map( |_| G :: $type( & mut $rng) ) . collect:: <Vec <_>>( ) . try_into( ) . unwrap( )
45+ } ;
46+ }
47+
48+ generate_ni_function ! ( NI_discrete_logarithm , discrete_logarithm, srandom) ;
49+ generate_ni_function ! ( NI_dleq , dleq, srandom, prandom) ;
50+ generate_ni_function ! (
51+ NI_pedersen_commitment ,
52+ pedersen_commitment,
53+ prandom,
54+ srandom,
55+ srandom
56+ ) ;
57+ generate_ni_function ! (
58+ NI_pedersen_commitment_dleq ,
59+ pedersen_commitment_dleq,
60+ [ prandom; 4 ] ,
61+ [ srandom; 2 ]
62+ ) ;
63+ generate_ni_function ! (
64+ NI_bbs_blind_commitment_computation ,
65+ bbs_blind_commitment_computation,
66+ [ prandom; 4 ] ,
67+ [ srandom; 3 ] ,
68+ srandom
69+ ) ;
70+
2271#[ allow( clippy:: type_complexity) ]
2372#[ allow( non_snake_case) ]
2473#[ test]
@@ -28,7 +77,7 @@ fn sage_test_vectors() {
2877
2978 let vectors = extract_vectors ( "src/tests/spec/allVectors.json" ) . unwrap ( ) ;
3079
31- let functions: [ fn ( seed : & [ u8 ] , context : & [ u8 ] ) -> ( Vec < Scalar > , Vec < u8 > ) ; 5 ] = [
80+ let functions: [ fn ( & [ u8 ] , & [ u8 ] ) -> ( Vec < Scalar > , Vec < u8 > ) ; 5 ] = [
3281 NI_discrete_logarithm ,
3382 NI_dleq ,
3483 NI_pedersen_commitment ,
@@ -52,127 +101,20 @@ fn sage_test_vectors() {
52101
53102fn extract_vectors ( path : & str ) -> json:: Result < Vec < ( Vec < u8 > , Vec < u8 > ) > > {
54103 let content = fs:: read_to_string ( path) . expect ( "Unable to read JSON file" ) ;
55-
56104 let root: JsonValue = json:: parse ( & content) . expect ( "JSON parsing error" ) ;
57105
58- let mut vectors: Vec < ( Vec < u8 > , Vec < u8 > ) > = Vec :: new ( ) ;
59-
60- for ( _, obj) in root. entries ( ) {
61- let context_hex = obj[ "Context" ]
62- . as_str ( )
63- . expect ( "Context field not found or not a string" ) ;
64- let proof_hex = obj[ "Proof" ]
65- . as_str ( )
66- . expect ( "Context field not found or not a string" ) ;
67-
68- vectors. push ( (
69- Vec :: from_hex ( context_hex) . unwrap ( ) ,
70- Vec :: from_hex ( proof_hex) . unwrap ( ) ,
71- ) ) ;
72- }
73- Ok ( vectors)
74- }
75-
76- /// This part tests the implementation of the SigmaProtocol trait for the
77- /// SchnorrProtocol structure as well as the Fiat-Shamir NISigmaProtocol transform
78- #[ allow( non_snake_case) ]
79- fn NI_discrete_logarithm ( seed : & [ u8 ] , context : & [ u8 ] ) -> ( Vec < Scalar > , Vec < u8 > ) {
80- let mut rng = TestDRNG :: new ( seed) ;
81- let ( morphismp, witness) = discrete_logarithm ( G :: srandom ( & mut rng) ) ;
82-
83- let protocol = SchnorrProtocolCustom ( morphismp) ;
84- let domain_sep: Vec < u8 > = context. to_vec ( ) ;
85- let nizk = NISigmaP :: new ( & domain_sep, protocol) ;
86-
87- let proof_bytes = nizk. prove_batchable ( & witness, & mut rng) . unwrap ( ) ;
88- let verified = nizk. verify_batchable ( & proof_bytes) . is_ok ( ) ;
89- assert ! ( verified, "Fiat-Shamir Schnorr proof verification failed" ) ;
90- ( witness, proof_bytes)
91- }
92-
93- #[ allow( non_snake_case) ]
94- fn NI_dleq ( seed : & [ u8 ] , context : & [ u8 ] ) -> ( Vec < Scalar > , Vec < u8 > ) {
95- let mut rng = TestDRNG :: new ( seed) ;
96- let ( morphismp, witness) = dleq ( G :: srandom ( & mut rng) , G :: prandom ( & mut rng) ) ;
97-
98- let protocol = SchnorrProtocolCustom ( morphismp) ;
99- let domain_sep: Vec < u8 > = context. to_vec ( ) ;
100- let nizk = NISigmaP :: new ( & domain_sep, protocol) ;
101-
102- let proof_bytes = nizk. prove_batchable ( & witness, & mut rng) . unwrap ( ) ;
103- let verified = nizk. verify_batchable ( & proof_bytes) . is_ok ( ) ;
104- assert ! ( verified, "Fiat-Shamir Schnorr proof verification failed" ) ;
105- ( witness, proof_bytes)
106- }
107-
108- #[ allow( non_snake_case) ]
109- fn NI_pedersen_commitment ( seed : & [ u8 ] , context : & [ u8 ] ) -> ( Vec < Scalar > , Vec < u8 > ) {
110- let mut rng = TestDRNG :: new ( seed) ;
111- let ( morphismp, witness) = pedersen_commitment (
112- G :: prandom ( & mut rng) ,
113- G :: srandom ( & mut rng) ,
114- G :: srandom ( & mut rng) ,
115- ) ;
116-
117- let protocol = SchnorrProtocolCustom ( morphismp) ;
118- let domain_sep: Vec < u8 > = context. to_vec ( ) ;
119- let nizk = NISigmaP :: new ( & domain_sep, protocol) ;
120-
121- let proof_bytes = nizk. prove_batchable ( & witness, & mut rng) . unwrap ( ) ;
122- let verified = nizk. verify_batchable ( & proof_bytes) . is_ok ( ) ;
123- assert ! ( verified, "Fiat-Shamir Schnorr proof verification failed" ) ;
124- ( witness, proof_bytes)
125- }
126-
127- #[ allow( non_snake_case) ]
128- fn NI_pedersen_commitment_dleq ( seed : & [ u8 ] , context : & [ u8 ] ) -> ( Vec < Scalar > , Vec < u8 > ) {
129- let mut rng = TestDRNG :: new ( seed) ;
130- let ( morphismp, witness) = pedersen_commitment_dleq (
131- ( 0 ..4 )
132- . map ( |_| G :: prandom ( & mut rng) )
133- . collect :: < Vec < _ > > ( )
134- . try_into ( )
135- . unwrap ( ) ,
136- ( 0 ..2 )
137- . map ( |_| G :: srandom ( & mut rng) )
138- . collect :: < Vec < _ > > ( )
139- . try_into ( )
140- . unwrap ( ) ,
141- ) ;
142-
143- let protocol = SchnorrProtocolCustom ( morphismp) ;
144- let domain_sep: Vec < u8 > = context. to_vec ( ) ;
145- let nizk = NISigmaP :: new ( & domain_sep, protocol) ;
146-
147- let proof_bytes = nizk. prove_batchable ( & witness, & mut rng) . unwrap ( ) ;
148- let verified = nizk. verify_batchable ( & proof_bytes) . is_ok ( ) ;
149- assert ! ( verified, "Fiat-Shamir Schnorr proof verification failed" ) ;
150- ( witness, proof_bytes)
151- }
152-
153- #[ allow( non_snake_case) ]
154- fn NI_bbs_blind_commitment_computation ( seed : & [ u8 ] , context : & [ u8 ] ) -> ( Vec < Scalar > , Vec < u8 > ) {
155- let mut rng = TestDRNG :: new ( seed) ;
156- let ( morphismp, witness) = bbs_blind_commitment_computation (
157- ( 0 ..4 )
158- . map ( |_| G :: prandom ( & mut rng) )
159- . collect :: < Vec < _ > > ( )
160- . try_into ( )
161- . unwrap ( ) ,
162- ( 0 ..3 )
163- . map ( |_| G :: srandom ( & mut rng) )
164- . collect :: < Vec < _ > > ( )
165- . try_into ( )
166- . unwrap ( ) ,
167- G :: srandom ( & mut rng) ,
168- ) ;
169-
170- let protocol = SchnorrProtocolCustom ( morphismp) ;
171- let domain_sep: Vec < u8 > = context. to_vec ( ) ;
172- let nizk = NISigmaP :: new ( & domain_sep, protocol) ;
173-
174- let proof_bytes = nizk. prove_batchable ( & witness, & mut rng) . unwrap ( ) ;
175- let verified = nizk. verify_batchable ( & proof_bytes) . is_ok ( ) ;
176- assert ! ( verified, "Fiat-Shamir Schnorr proof verification failed" ) ;
177- ( witness, proof_bytes)
106+ root. entries ( )
107+ . map ( |( _, obj) | {
108+ let context_hex = obj[ "Context" ]
109+ . as_str ( )
110+ . expect ( "Context field not found or not a string" ) ;
111+ let proof_hex = obj[ "Proof" ]
112+ . as_str ( )
113+ . expect ( "Proof field not found or not a string" ) ;
114+ Ok ( (
115+ Vec :: from_hex ( context_hex) . unwrap ( ) ,
116+ Vec :: from_hex ( proof_hex) . unwrap ( ) ,
117+ ) )
118+ } )
119+ . collect ( )
178120}
0 commit comments