Skip to content

Commit a5bb572

Browse files
committed
cargo fmt
1 parent 65a56c2 commit a5bb572

File tree

2 files changed

+90
-88
lines changed

2 files changed

+90
-88
lines changed

src/range_proof/mod.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ impl RangeProof {
135135
n: usize,
136136
) -> Result<(), &'static str> {
137137
RangeProof::verify_batch(
138-
[self.prepare_verification(value_commitments, transcript, rng, n)],
139-
gens,
140-
rng
138+
[
139+
self.prepare_verification(value_commitments, transcript, rng, n),
140+
],
141+
gens,
142+
rng,
141143
)
142144
}
143145
}
@@ -166,7 +168,7 @@ mod tests {
166168
// Both prover and verifier have access to the generators and the proof
167169
let generators = Generators::new(PedersenGenerators::default(), n, m);
168170

169-
let (proof_bytes, value_commitments) = singleparty_create_helper(n,m);
171+
let (proof_bytes, value_commitments) = singleparty_create_helper(n, m);
170172

171173
println!(
172174
"Aggregated rangeproof of m={} proofs of n={} bits has size {} bytes",
@@ -206,30 +208,21 @@ mod tests {
206208
let mut rng = OsRng::new().unwrap();
207209
let transcript = ProofTranscript::new(b"AggregatedRangeProofTest");
208210

209-
let verifications = nm.iter().map(|(n,m)| {
210-
let (p, vc) = singleparty_create_helper(*n,*m);
211-
bincode::deserialize::<RangeProof>(&p)
212-
.unwrap()
213-
.prepare_verification(
214-
&vc,
215-
&mut transcript.clone(),
216-
&mut rng,
217-
*n
218-
)
219-
}).collect::<Vec<_>>();
211+
let verifications = nm.iter()
212+
.map(|(n, m)| {
213+
let (p, vc) = singleparty_create_helper(*n, *m);
214+
bincode::deserialize::<RangeProof>(&p)
215+
.unwrap()
216+
.prepare_verification(&vc, &mut transcript.clone(), &mut rng, *n)
217+
})
218+
.collect::<Vec<_>>();
220219

221220
let max_n = verifications.iter().map(|v| v.n).max().unwrap_or(0);
222221
let max_m = verifications.iter().map(|v| v.m).max().unwrap_or(0);
223222

224223
let generators = Generators::new(PedersenGenerators::default(), max_n, max_m);
225224

226-
assert!(
227-
RangeProof::verify_batch(
228-
verifications,
229-
generators.all(),
230-
&mut rng
231-
).is_ok()
232-
);
225+
assert!(RangeProof::verify_batch(verifications, generators.all(), &mut rng).is_ok());
233226
}
234227

235228

@@ -276,7 +269,9 @@ mod tests {
276269
value_commitments = values
277270
.iter()
278271
.zip(blindings.iter())
279-
.map(|(&v, &v_blinding)| pg.commit(Scalar::from_u64(v), v_blinding))
272+
.map(|(&v, &v_blinding)| {
273+
pg.commit(Scalar::from_u64(v), v_blinding)
274+
})
280275
.collect();
281276

282277
(proof_bytes, value_commitments)
@@ -337,12 +332,12 @@ mod tests {
337332

338333
#[test]
339334
fn batch_verify_n_differ_m_differ_total_64() {
340-
batch_verify_helper(&[(64, 1), (32, 2), (16,4)]);
335+
batch_verify_helper(&[(64, 1), (32, 2), (16, 4)]);
341336
}
342337

343338
#[test]
344339
fn batch_verify_n_differ_m_differ_total_256() {
345-
batch_verify_helper(&[(16, 1), (32, 2), (64,4)]);
340+
batch_verify_helper(&[(16, 1), (32, 2), (64, 4)]);
346341
}
347342

348343
#[test]

src/range_proof/verification.rs

+70-63
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,32 @@ use proof_transcript::ProofTranscript;
1515
use range_proof::RangeProof;
1616
use 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.
2121
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,
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 {
5050
impl 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

5961
impl 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

Comments
 (0)