@@ -15,32 +15,32 @@ use proof_transcript::ProofTranscript;
1515use range_proof:: RangeProof ;
1616use util;
1717
18- /// Represents a deferred computation to verify a single rangeproof.
18+ /// Represents a deferred computation to verify a single rangeproof.
1919/// Multiple instances can be verified more efficient as a batch using
2020/// `RangeProof::verify_batch` function.
2121pub struct Verification {
22- /// Number of commitments in the aggregated proof
23- pub m : usize ,
22+ /// Number of commitments in the aggregated proof
23+ pub m : usize ,
2424
25- /// Size of the range in bits
26- pub n : usize ,
25+ /// Size of the range in bits
26+ pub n : usize ,
2727
28- /// Pair of scalars multiplying pedersen bases `B`, `B_blinding`.
29- pedersen_base_scalars : ( Scalar , Scalar ) ,
28+ /// Pair of scalars multiplying pedersen bases `B`, `B_blinding`.
29+ pedersen_base_scalars : ( Scalar , Scalar ) ,
3030
31- /// List of scalars for `n*m` `G` bases. These are separated from `h_scalars`
32- /// so we can easily pad them when verifying proofs with different `m`s.
33- g_scalars : Vec < Scalar > ,
31+ /// List of scalars for `n*m` `G` bases. These are separated from `h_scalars`
32+ /// so we can easily pad them when verifying proofs with different `m`s.
33+ g_scalars : Vec < Scalar > ,
3434
35- /// List of scalars for `n*m` `H` bases. These are separated from `g_scalars`
36- /// so we can easily pad them when verifying proofs with different `m`s.
37- h_scalars : Vec < Scalar > ,
35+ /// List of scalars for `n*m` `H` bases. These are separated from `g_scalars`
36+ /// so we can easily pad them when verifying proofs with different `m`s.
37+ h_scalars : Vec < Scalar > ,
3838
39- /// List of scalars for any number of dynamic bases.
40- dynamic_base_scalars : Vec < Scalar > ,
39+ /// List of scalars for any number of dynamic bases.
40+ dynamic_base_scalars : Vec < Scalar > ,
4141
42- /// List of dynamic bases for the corresponding scalars.
43- dynamic_bases : Vec < RistrettoPoint > ,
42+ /// List of dynamic bases for the corresponding scalars.
43+ dynamic_bases : Vec < RistrettoPoint > ,
4444
4545 /// Internal flag that prevents accidentally dropping
4646 /// this struct without properly verifying it first.
@@ -50,14 +50,15 @@ pub struct Verification {
5050impl Drop for Verification {
5151 fn drop ( & mut self ) {
5252 if !self . verified {
53- panic ! ( "Deferred range proof Verification was not explicitly \
54- verified using `RangeProof::verify_batch`!") ;
53+ panic ! (
54+ "Deferred range proof Verification was not explicitly \
55+ verified using `RangeProof::verify_batch`!"
56+ ) ;
5557 }
5658 }
5759}
5860
5961impl RangeProof {
60-
6162 /// Prepares a `Verification` struct
6263 /// that can be combined with others in a batch.
6364 pub fn prepare_verification < R : Rng > (
@@ -117,7 +118,9 @@ impl RangeProof {
117118 let h = s_inv
118119 . zip ( util:: exp_iter ( y. invert ( ) ) )
119120 . zip ( concat_z_and_2)
120- . map ( |( ( s_i_inv, exp_y_inv) , z_and_2) | z + exp_y_inv * ( zz * z_and_2 - b * s_i_inv) ) ;
121+ . map ( |( ( s_i_inv, exp_y_inv) , z_and_2) | {
122+ z + exp_y_inv * ( zz * z_and_2 - b * s_i_inv)
123+ } ) ;
121124
122125 let value_commitment_scalars = util:: exp_iter ( z) . take ( m) . map ( |z_exp| c * zz * z_exp) ;
123126 let basepoint_scalar = w * ( self . t_x - a * b) + c * ( delta ( n, m, & y, & z) - self . t_x ) ;
@@ -128,17 +131,15 @@ impl RangeProof {
128131 pedersen_base_scalars : ( basepoint_scalar, -self . e_blinding - c * self . t_x_blinding ) ,
129132 g_scalars : g. collect ( ) ,
130133 h_scalars : h. collect ( ) ,
131- dynamic_base_scalars :
132- iter:: once ( Scalar :: one ( ) )
134+ dynamic_base_scalars : iter:: once ( Scalar :: one ( ) )
133135 . chain ( iter:: once ( x) )
134136 . chain ( value_commitment_scalars)
135137 . chain ( iter:: once ( c * x) )
136138 . chain ( iter:: once ( c * x * x) )
137139 . chain ( x_sq. iter ( ) . cloned ( ) )
138140 . chain ( x_inv_sq. iter ( ) . cloned ( ) )
139141 . collect ( ) ,
140- dynamic_bases :
141- iter:: once ( & self . A )
142+ dynamic_bases : iter:: once ( & self . A )
142143 . chain ( iter:: once ( & self . S ) )
143144 . chain ( value_commitments. iter ( ) )
144145 . chain ( iter:: once ( & self . T_1 ) )
@@ -147,7 +148,7 @@ impl RangeProof {
147148 . chain ( self . ipp_proof . R_vec . iter ( ) )
148149 . cloned ( )
149150 . collect ( ) ,
150- verified : false
151+ verified : false ,
151152 }
152153 }
153154
@@ -159,81 +160,87 @@ impl RangeProof {
159160 pub fn verify_batch < R : Rng , B : AsMut < [ Verification ] > > (
160161 mut batch : B ,
161162 gens : GeneratorsView ,
162- rng : & mut R
163+ rng : & mut R ,
163164 ) -> Result < ( ) , & ' static str > {
164165 let batch = batch. as_mut ( ) ;
165166
166- // we will special-case the first item to avoid unnecessary multiplication,
167- // so lets check that we have at least one item.
168- if batch. len ( ) == 0 {
169- return Ok ( ( ) )
170- }
167+ // we will special-case the first item to avoid unnecessary multiplication,
168+ // so lets check that we have at least one item.
169+ if batch. len ( ) == 0 {
170+ return Ok ( ( ) ) ;
171+ }
171172
172173 // Make sure we have enough static generators
173174 let n = batch. iter ( ) . map ( |v| v. n ) . max ( ) . unwrap_or ( 0 ) ;
174175 let m = batch. iter ( ) . map ( |v| v. m ) . max ( ) . unwrap_or ( 0 ) ;
175176 if gens. G . len ( ) < ( n * m) {
176- return Err ( "The generators view does not have enough generators for the largest proof" )
177+ return Err (
178+ "The generators view does not have enough generators for the largest proof" ,
179+ ) ;
177180 }
178181
179182 // First statement is used without a random factor
180183 batch[ 0 ] . verified = true ;
181184 let mut pedersen_base_scalars: ( Scalar , Scalar ) = batch[ 0 ] . pedersen_base_scalars ;
182185 let mut g_scalars: Vec < Scalar > = batch[ 0 ] . g_scalars . clone ( ) ;
183186 let mut h_scalars: Vec < Scalar > = batch[ 0 ] . h_scalars . clone ( ) ;
184-
187+
185188 // pad static scalars to the largest proof
186- g_scalars. resize ( n* m, Scalar :: zero ( ) ) ;
187- h_scalars. resize ( n* m, Scalar :: zero ( ) ) ;
189+ g_scalars. resize ( n * m, Scalar :: zero ( ) ) ;
190+ h_scalars. resize ( n * m, Scalar :: zero ( ) ) ;
188191
189192 let mut dynamic_base_scalars: Vec < Scalar > = batch[ 0 ] . dynamic_base_scalars . clone ( ) ;
190193 let mut dynamic_bases: Vec < RistrettoPoint > = batch[ 0 ] . dynamic_bases . clone ( ) ;
191-
194+
192195 // Other statements are added with a random factor per statement
193196 for verification in & mut batch[ 1 ..] {
194197 verification. verified = true ;
195198 let batch_challenge = Scalar :: random ( rng) ;
196199
197- pedersen_base_scalars. 0 += batch_challenge* verification. pedersen_base_scalars . 0 ;
198- pedersen_base_scalars. 1 += batch_challenge* verification. pedersen_base_scalars . 1 ;
200+ pedersen_base_scalars. 0 += batch_challenge * verification. pedersen_base_scalars . 0 ;
201+ pedersen_base_scalars. 1 += batch_challenge * verification. pedersen_base_scalars . 1 ;
199202
200203 // Note: this loop may be shorter than the total amount of scalars if `m < max({m})`
201204 for ( i, s) in verification. g_scalars . iter ( ) . enumerate ( ) {
202- g_scalars[ i] += batch_challenge* s;
205+ g_scalars[ i] += batch_challenge * s;
203206 }
204207 for ( i, s) in verification. h_scalars . iter ( ) . enumerate ( ) {
205- h_scalars[ i] += batch_challenge* s;
208+ h_scalars[ i] += batch_challenge * s;
206209 }
207210
208- dynamic_base_scalars = dynamic_base_scalars. iter ( )
211+ dynamic_base_scalars = dynamic_base_scalars
212+ . iter ( )
209213 . cloned ( )
210- . chain ( verification. dynamic_base_scalars . iter ( ) . map ( |s| batch_challenge* s ) )
214+ . chain ( verification. dynamic_base_scalars . iter ( ) . map ( |s| {
215+ batch_challenge * s
216+ } ) )
211217 . collect ( ) ;
212218
213- dynamic_bases = dynamic_bases. iter ( )
219+ dynamic_bases = dynamic_bases
220+ . iter ( )
214221 . chain ( verification. dynamic_bases . iter ( ) )
215222 . cloned ( )
216223 . collect ( ) ;
217224 }
218225
219- let mega_check = ristretto:: vartime:: multiscalar_mul (
220- iter:: once ( & pedersen_base_scalars. 0 )
221- . chain ( iter:: once ( & pedersen_base_scalars. 1 ) )
222- . chain ( g_scalars. iter ( ) )
223- . chain ( h_scalars. iter ( ) )
224- . chain ( dynamic_base_scalars. iter ( ) ) ,
225- iter:: once ( & gens. pedersen_generators . B )
226- . chain ( iter:: once ( & gens. pedersen_generators . B_blinding ) )
227- . chain ( gens. G . iter ( ) )
228- . chain ( gens. H . iter ( ) )
229- . chain ( dynamic_bases. iter ( ) )
230- ) ;
231-
232- if mega_check. is_identity ( ) {
233- Ok ( ( ) )
234- } else {
235- Err ( "Batch verification failed" )
236- }
226+ let mega_check = ristretto:: vartime:: multiscalar_mul (
227+ iter:: once ( & pedersen_base_scalars. 0 )
228+ . chain ( iter:: once ( & pedersen_base_scalars. 1 ) )
229+ . chain ( g_scalars. iter ( ) )
230+ . chain ( h_scalars. iter ( ) )
231+ . chain ( dynamic_base_scalars. iter ( ) ) ,
232+ iter:: once ( & gens. pedersen_generators . B )
233+ . chain ( iter:: once ( & gens. pedersen_generators . B_blinding ) )
234+ . chain ( gens. G . iter ( ) )
235+ . chain ( gens. H . iter ( ) )
236+ . chain ( dynamic_bases. iter ( ) ) ,
237+ ) ;
238+
239+ if mega_check. is_identity ( ) {
240+ Ok ( ( ) )
241+ } else {
242+ Err ( "Batch verification failed" )
243+ }
237244 }
238245}
239246
@@ -279,4 +286,4 @@ mod tests {
279286
280287 assert_eq ! ( power_g, delta( n, 1 , & y, & z) , ) ;
281288 }
282- }
289+ }
0 commit comments