@@ -15,32 +15,32 @@ use proof_transcript::ProofTranscript;
15
15
use range_proof:: RangeProof ;
16
16
use util;
17
17
18
- /// Represents a deferred computation to verify a single rangeproof.
18
+ /// Represents a deferred computation to verify a single rangeproof.
19
19
/// Multiple instances can be verified more efficient as a batch using
20
20
/// `RangeProof::verify_batch` function.
21
21
pub 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 ,
24
24
25
- /// Size of the range in bits
26
- pub n : usize ,
25
+ /// Size of the range in bits
26
+ pub n : usize ,
27
27
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 ) ,
30
30
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 > ,
34
34
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 > ,
38
38
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 > ,
41
41
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 > ,
44
44
45
45
/// Internal flag that prevents accidentally dropping
46
46
/// this struct without properly verifying it first.
@@ -50,14 +50,15 @@ pub struct Verification {
50
50
impl Drop for Verification {
51
51
fn drop ( & mut self ) {
52
52
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
+ ) ;
55
57
}
56
58
}
57
59
}
58
60
59
61
impl RangeProof {
60
-
61
62
/// Prepares a `Verification` struct
62
63
/// that can be combined with others in a batch.
63
64
pub fn prepare_verification < R : Rng > (
@@ -117,7 +118,9 @@ impl RangeProof {
117
118
let h = s_inv
118
119
. zip ( util:: exp_iter ( y. invert ( ) ) )
119
120
. 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
+ } ) ;
121
124
122
125
let value_commitment_scalars = util:: exp_iter ( z) . take ( m) . map ( |z_exp| c * zz * z_exp) ;
123
126
let basepoint_scalar = w * ( self . t_x - a * b) + c * ( delta ( n, m, & y, & z) - self . t_x ) ;
@@ -128,17 +131,15 @@ impl RangeProof {
128
131
pedersen_base_scalars : ( basepoint_scalar, -self . e_blinding - c * self . t_x_blinding ) ,
129
132
g_scalars : g. collect ( ) ,
130
133
h_scalars : h. collect ( ) ,
131
- dynamic_base_scalars :
132
- iter:: once ( Scalar :: one ( ) )
134
+ dynamic_base_scalars : iter:: once ( Scalar :: one ( ) )
133
135
. chain ( iter:: once ( x) )
134
136
. chain ( value_commitment_scalars)
135
137
. chain ( iter:: once ( c * x) )
136
138
. chain ( iter:: once ( c * x * x) )
137
139
. chain ( x_sq. iter ( ) . cloned ( ) )
138
140
. chain ( x_inv_sq. iter ( ) . cloned ( ) )
139
141
. collect ( ) ,
140
- dynamic_bases :
141
- iter:: once ( & self . A )
142
+ dynamic_bases : iter:: once ( & self . A )
142
143
. chain ( iter:: once ( & self . S ) )
143
144
. chain ( value_commitments. iter ( ) )
144
145
. chain ( iter:: once ( & self . T_1 ) )
@@ -147,7 +148,7 @@ impl RangeProof {
147
148
. chain ( self . ipp_proof . R_vec . iter ( ) )
148
149
. cloned ( )
149
150
. collect ( ) ,
150
- verified : false
151
+ verified : false ,
151
152
}
152
153
}
153
154
@@ -159,81 +160,87 @@ impl RangeProof {
159
160
pub fn verify_batch < R : Rng , B : AsMut < [ Verification ] > > (
160
161
mut batch : B ,
161
162
gens : GeneratorsView ,
162
- rng : & mut R
163
+ rng : & mut R ,
163
164
) -> Result < ( ) , & ' static str > {
164
165
let batch = batch. as_mut ( ) ;
165
166
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
+ }
171
172
172
173
// Make sure we have enough static generators
173
174
let n = batch. iter ( ) . map ( |v| v. n ) . max ( ) . unwrap_or ( 0 ) ;
174
175
let m = batch. iter ( ) . map ( |v| v. m ) . max ( ) . unwrap_or ( 0 ) ;
175
176
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
+ ) ;
177
180
}
178
181
179
182
// First statement is used without a random factor
180
183
batch[ 0 ] . verified = true ;
181
184
let mut pedersen_base_scalars: ( Scalar , Scalar ) = batch[ 0 ] . pedersen_base_scalars ;
182
185
let mut g_scalars: Vec < Scalar > = batch[ 0 ] . g_scalars . clone ( ) ;
183
186
let mut h_scalars: Vec < Scalar > = batch[ 0 ] . h_scalars . clone ( ) ;
184
-
187
+
185
188
// 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 ( ) ) ;
188
191
189
192
let mut dynamic_base_scalars: Vec < Scalar > = batch[ 0 ] . dynamic_base_scalars . clone ( ) ;
190
193
let mut dynamic_bases: Vec < RistrettoPoint > = batch[ 0 ] . dynamic_bases . clone ( ) ;
191
-
194
+
192
195
// Other statements are added with a random factor per statement
193
196
for verification in & mut batch[ 1 ..] {
194
197
verification. verified = true ;
195
198
let batch_challenge = Scalar :: random ( rng) ;
196
199
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 ;
199
202
200
203
// Note: this loop may be shorter than the total amount of scalars if `m < max({m})`
201
204
for ( i, s) in verification. g_scalars . iter ( ) . enumerate ( ) {
202
- g_scalars[ i] += batch_challenge* s;
205
+ g_scalars[ i] += batch_challenge * s;
203
206
}
204
207
for ( i, s) in verification. h_scalars . iter ( ) . enumerate ( ) {
205
- h_scalars[ i] += batch_challenge* s;
208
+ h_scalars[ i] += batch_challenge * s;
206
209
}
207
210
208
- dynamic_base_scalars = dynamic_base_scalars. iter ( )
211
+ dynamic_base_scalars = dynamic_base_scalars
212
+ . iter ( )
209
213
. 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
+ } ) )
211
217
. collect ( ) ;
212
218
213
- dynamic_bases = dynamic_bases. iter ( )
219
+ dynamic_bases = dynamic_bases
220
+ . iter ( )
214
221
. chain ( verification. dynamic_bases . iter ( ) )
215
222
. cloned ( )
216
223
. collect ( ) ;
217
224
}
218
225
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
+ }
237
244
}
238
245
}
239
246
@@ -279,4 +286,4 @@ mod tests {
279
286
280
287
assert_eq ! ( power_g, delta( n, 1 , & y, & z) , ) ;
281
288
}
282
- }
289
+ }
0 commit comments