@@ -39,6 +39,11 @@ impl<G: Group + GroupEncoding> SigmaProtocol for AndProtocol<G> {
3939 witness : & Self :: Witness ,
4040 rng : & mut ( impl rand:: Rng + rand:: CryptoRng ) ,
4141 ) -> Result < ( Self :: Commitment , Self :: ProverState ) , ProofError > {
42+ let expected_w_len: usize = self . 0 . iter ( ) . map ( |p| p. scalars_nb ( ) ) . sum ( ) ;
43+ if expected_w_len != witness. len ( ) || self . is_empty ( ) {
44+ return Err ( ProofError :: Other ) ;
45+ }
46+
4247 let mut cursor = 0 ;
4348 let mut commitment = Vec :: with_capacity ( self . 0 . iter ( ) . map ( |p| p. statements_nb ( ) ) . sum ( ) ) ;
4449 let mut state = Vec :: with_capacity ( self . len ( ) ) ;
@@ -59,6 +64,10 @@ impl<G: Group + GroupEncoding> SigmaProtocol for AndProtocol<G> {
5964 state : Self :: ProverState ,
6065 challenge : & Self :: Challenge ,
6166 ) -> Result < Self :: Response , ProofError > {
67+ if state. len ( ) != self . len ( ) {
68+ return Err ( ProofError :: Other ) ;
69+ }
70+
6271 let mut response = Vec :: with_capacity ( self . 0 . iter ( ) . map ( |p| p. scalars_nb ( ) ) . sum ( ) ) ;
6372 for ( proto, proto_state) in self . 0 . iter ( ) . zip ( state) {
6473 let proto_response = proto. prover_response ( proto_state, challenge) ?;
@@ -73,6 +82,12 @@ impl<G: Group + GroupEncoding> SigmaProtocol for AndProtocol<G> {
7382 challenge : & Self :: Challenge ,
7483 response : & Self :: Response ,
7584 ) -> Result < ( ) , ProofError > {
85+ let expected_c_len: usize = self . 0 . iter ( ) . map ( |p| p. statements_nb ( ) ) . sum ( ) ;
86+ let expected_r_len: usize = self . 0 . iter ( ) . map ( |p| p. scalars_nb ( ) ) . sum ( ) ;
87+ if commitment. len ( ) != expected_c_len || response. len ( ) != expected_r_len {
88+ return Err ( ProofError :: Other ) ;
89+ }
90+
7691 let mut c_cursor = 0 ;
7792 let mut r_cursor = 0 ;
7893 for proto in & self . 0 {
@@ -96,6 +111,12 @@ impl<G: Group + GroupEncoding> SigmaProtocol for AndProtocol<G> {
96111 challenge : & Self :: Challenge ,
97112 response : & Self :: Response ,
98113 ) -> Result < Vec < u8 > , ProofError > {
114+ let expected_c_len: usize = self . 0 . iter ( ) . map ( |p| p. statements_nb ( ) ) . sum ( ) ;
115+ let expected_r_len: usize = self . 0 . iter ( ) . map ( |p| p. scalars_nb ( ) ) . sum ( ) ;
116+ if commitment. len ( ) != expected_c_len || response. len ( ) != expected_r_len {
117+ return Err ( ProofError :: Other ) ;
118+ }
119+
99120 let mut bytes = Vec :: new ( ) ;
100121 let mut c_cursor = 0 ;
101122 let mut r_cursor = 0 ;
@@ -185,12 +206,13 @@ impl<G: Group + GroupEncoding> SigmaProtocol for OrProtocol<G> {
185206 rng : & mut ( impl rand:: Rng + rand:: CryptoRng ) ,
186207 ) -> Result < ( Self :: Commitment , Self :: ProverState ) , ProofError > {
187208 let real_index = witness. 0 ;
188- if real_index >= self . len ( ) {
209+ let expected_w_len = self . 0 [ real_index] . scalars_nb ( ) ;
210+ if real_index >= self . len ( ) || witness. 1 . len ( ) != expected_w_len {
189211 return Err ( ProofError :: Other ) ;
190212 }
191213
192- let mut fake_transcripts = Vec :: new ( ) ;
193- let mut commitment = Vec :: new ( ) ;
214+ let mut fake_transcripts = Vec :: with_capacity ( self . len ( ) - 1 ) ;
215+ let mut commitment = Vec :: with_capacity ( self . 0 . iter ( ) . map ( |p| p . statements_nb ( ) ) . sum ( ) ) ;
194216 let ( real_commit, real_state) = self . 0 [ real_index] . prover_commit ( & witness. 1 , rng) ?;
195217 for ( i, proto) in self . 0 . iter ( ) . enumerate ( ) {
196218 if i != real_index {
@@ -244,6 +266,16 @@ impl<G: Group + GroupEncoding> SigmaProtocol for OrProtocol<G> {
244266 challenge : & Self :: Challenge ,
245267 response : & Self :: Response ,
246268 ) -> Result < ( ) , ProofError > {
269+ let expected_c_len: usize = self . 0 . iter ( ) . map ( |p| p. statements_nb ( ) ) . sum ( ) ;
270+ let expected_ch_nb = self . len ( ) ;
271+ let expected_r_len: usize = self . 0 . iter ( ) . map ( |p| p. scalars_nb ( ) ) . sum ( ) ;
272+ if commitment. len ( ) != expected_c_len
273+ || response. 0 . len ( ) != expected_ch_nb
274+ || response. 1 . len ( ) != expected_r_len
275+ {
276+ return Err ( ProofError :: Other ) ;
277+ }
278+
247279 let mut expected_difference = * challenge;
248280 let mut c_cursor = 0 ;
249281 let mut r_cursor = 0 ;
@@ -270,6 +302,16 @@ impl<G: Group + GroupEncoding> SigmaProtocol for OrProtocol<G> {
270302 _challenge : & Self :: Challenge ,
271303 response : & Self :: Response ,
272304 ) -> Result < Vec < u8 > , ProofError > {
305+ let expected_c_len: usize = self . 0 . iter ( ) . map ( |p| p. statements_nb ( ) ) . sum ( ) ;
306+ let expected_ch_nb = self . len ( ) ;
307+ let expected_r_len: usize = self . 0 . iter ( ) . map ( |p| p. scalars_nb ( ) ) . sum ( ) ;
308+ if commitment. len ( ) != expected_c_len
309+ || response. 0 . len ( ) != expected_ch_nb
310+ || response. 1 . len ( ) != expected_r_len
311+ {
312+ return Err ( ProofError :: Other ) ;
313+ }
314+
273315 let mut bytes = Vec :: new ( ) ;
274316 let mut c_cursor = 0 ;
275317 let mut r_cursor = 0 ;
@@ -292,18 +334,27 @@ impl<G: Group + GroupEncoding> SigmaProtocol for OrProtocol<G> {
292334 & self ,
293335 data : & [ u8 ] ,
294336 ) -> Result < ( Self :: Commitment , Self :: Response ) , ProofError > {
337+ let point_size = G :: generator ( ) . to_bytes ( ) . as_ref ( ) . len ( ) ;
338+ let scalar_size = <<G as Group >:: Scalar as PrimeField >:: Repr :: default ( )
339+ . as_ref ( )
340+ . len ( ) ;
341+
342+ let expected_d_len: usize = self
343+ . 0
344+ . iter ( )
345+ . map ( |p| ( p. scalars_nb ( ) + 1 ) * scalar_size + p. statements_nb ( ) * point_size)
346+ . sum ( ) ;
347+ if data. len ( ) != expected_d_len {
348+ return Err ( ProofError :: ProofSizeMismatch ) ;
349+ }
350+
295351 let mut cursor = 0 ;
296352 let mut commitment = Vec :: with_capacity ( self . 0 . iter ( ) . map ( |p| p. statements_nb ( ) ) . sum ( ) ) ;
297353 let mut response = (
298354 Vec :: with_capacity ( self . len ( ) ) ,
299355 Vec :: with_capacity ( self . 0 . iter ( ) . map ( |p| p. scalars_nb ( ) ) . sum ( ) ) ,
300356 ) ;
301357
302- let point_size = G :: generator ( ) . to_bytes ( ) . as_ref ( ) . len ( ) ;
303- let scalar_size = <<G as Group >:: Scalar as PrimeField >:: Repr :: default ( )
304- . as_ref ( )
305- . len ( ) ;
306-
307358 for proto in & self . 0 {
308359 let c_nb = proto. statements_nb ( ) ;
309360 let r_nb = proto. scalars_nb ( ) ;
0 commit comments