Skip to content

Commit b515c03

Browse files
authored
Merge pull request #163 from dalek-cryptography/pedersen
Disentangle Pedersen generators from Bulletproofs generators
2 parents 13ca4bf + 03367d4 commit b515c03

File tree

9 files changed

+213
-234
lines changed

9 files changed

+213
-234
lines changed

benches/bulletproofs.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ extern crate criterion;
44
use criterion::Criterion;
55

66
extern crate rand;
7-
use rand::{rngs::OsRng, Rng};
7+
use rand::Rng;
88

99
extern crate curve25519_dalek;
1010
use curve25519_dalek::scalar::Scalar;
1111

1212
extern crate bulletproofs;
1313
use bulletproofs::RangeProof;
1414
use bulletproofs::Transcript;
15-
use bulletproofs::{Generators, PedersenGenerators};
15+
use bulletproofs::{BulletproofGens, PedersenGens};
1616

1717
static AGGREGATION_SIZES: [usize; 6] = [1, 2, 4, 8, 16, 32];
1818

@@ -22,8 +22,9 @@ fn create_aggregated_rangeproof_helper(n: usize, c: &mut Criterion) {
2222
c.bench_function_over_inputs(
2323
&label,
2424
move |b, &&m| {
25-
let generators = Generators::new(PedersenGenerators::default(), n, m);
26-
let mut rng = OsRng::new().unwrap();
25+
let pc_gens = PedersenGens::default();
26+
let bp_gens = BulletproofGens::new(n, m);
27+
let mut rng = rand::thread_rng();
2728

2829
let (min, max) = (0u64, ((1u128 << n) - 1) as u64);
2930
let values: Vec<u64> = (0..m).map(|_| rng.gen_range(min, max)).collect();
@@ -34,9 +35,9 @@ fn create_aggregated_rangeproof_helper(n: usize, c: &mut Criterion) {
3435
let mut transcript = Transcript::new(b"AggregateRangeProofBenchmark");
3536

3637
RangeProof::prove_multiple(
37-
&generators,
38+
&bp_gens,
39+
&pc_gens,
3840
&mut transcript,
39-
&mut rng,
4041
&values,
4142
&blindings,
4243
n,
@@ -69,42 +70,35 @@ fn verify_aggregated_rangeproof_helper(n: usize, c: &mut Criterion) {
6970
c.bench_function_over_inputs(
7071
&label,
7172
move |b, &&m| {
72-
let generators = Generators::new(PedersenGenerators::default(), n, m);
73-
let mut rng = OsRng::new().unwrap();
73+
let pc_gens = PedersenGens::default();
74+
let bp_gens = BulletproofGens::new(n, m);
75+
let mut rng = rand::thread_rng();
7476

7577
let (min, max) = (0u64, ((1u128 << n) - 1) as u64);
7678
let values: Vec<u64> = (0..m).map(|_| rng.gen_range(min, max)).collect();
7779
let blindings: Vec<Scalar> = (0..m).map(|_| Scalar::random(&mut rng)).collect();
7880

7981
let mut transcript = Transcript::new(b"AggregateRangeProofBenchmark");
8082
let proof = RangeProof::prove_multiple(
81-
&generators,
83+
&bp_gens,
84+
&pc_gens,
8285
&mut transcript,
83-
&mut rng,
8486
&values,
8587
&blindings,
8688
n,
8789
).unwrap();
8890

89-
// XXX would be nice to have some convenience API for this
90-
let pg = &generators.pedersen_gens;
9191
let value_commitments: Vec<_> = values
9292
.iter()
9393
.zip(blindings.iter())
94-
.map(|(&v, &v_blinding)| pg.commit(Scalar::from(v), v_blinding))
94+
.map(|(&v, &v_blinding)| pc_gens.commit(v.into(), v_blinding))
9595
.collect();
9696

9797
b.iter(|| {
9898
// Each proof creation requires a clean transcript.
9999
let mut transcript = Transcript::new(b"AggregateRangeProofBenchmark");
100100

101-
proof.verify(
102-
&value_commitments,
103-
&generators,
104-
&mut transcript,
105-
&mut rng,
106-
n,
107-
)
101+
proof.verify(&bp_gens, &pc_gens, &mut transcript, &value_commitments, n)
108102
});
109103
},
110104
&AGGREGATION_SIZES,

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2018-09-04
1+
nightly-2018-09-16

src/generators.rs

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ use sha3::{Sha3XofReader, Shake256};
1313

1414
/// Represents a pair of base points for Pedersen commitments.
1515
#[derive(Copy, Clone)]
16-
pub struct PedersenGenerators {
16+
pub struct PedersenGens {
1717
/// Base for the committed value
1818
pub B: RistrettoPoint,
1919

2020
/// Base for the blinding factor
2121
pub B_blinding: RistrettoPoint,
2222
}
2323

24-
impl PedersenGenerators {
24+
impl PedersenGens {
2525
/// Creates a Pedersen commitment using the value scalar and a blinding factor.
2626
pub fn commit(&self, value: Scalar, blinding: Scalar) -> RistrettoPoint {
2727
RistrettoPoint::multiscalar_mul(&[value, blinding], &[self.B, self.B_blinding])
2828
}
2929
}
3030

31-
impl Default for PedersenGenerators {
31+
impl Default for PedersenGens {
3232
fn default() -> Self {
33-
PedersenGenerators {
33+
PedersenGens {
3434
B: GeneratorsChain::new(b"Bulletproofs.Generators.B")
3535
.next()
3636
.unwrap(),
@@ -81,79 +81,65 @@ impl Iterator for GeneratorsChain {
8181
}
8282
}
8383

84-
/// The `Generators` struct contains all the generators needed for
84+
/// The `BulletproofGens` struct contains all the generators needed for
8585
/// aggregating `m` range proofs of `n` bits each.
8686
#[derive(Clone)]
87-
pub struct Generators {
88-
/// Bases for Pedersen commitments
89-
pub pedersen_gens: PedersenGenerators,
87+
pub struct BulletproofGens {
9088
/// The maximum number of usable generators for each party.
9189
pub gens_capacity: usize,
9290
/// Number of values or parties
9391
pub party_capacity: usize,
94-
/// Per-bit generators for the bit values
92+
/// Precomputed \\(\mathbf G\\) generators for each party.
9593
G_vec: Vec<Vec<RistrettoPoint>>,
96-
/// Per-bit generators for the bit blinding factors
94+
/// Precomputed \\(\mathbf H\\) generators for each party.
9795
H_vec: Vec<Vec<RistrettoPoint>>,
9896
}
9997

100-
impl Generators {
101-
/// Create a new `Generators` object.
98+
impl BulletproofGens {
99+
/// Create a new `BulletproofGens` object.
102100
///
103101
/// # Inputs
104102
///
105-
/// * `pedersen_gens` is a pair of generators used for Pedersen
106-
/// commitments.
107103
/// * `gens_capacity` is the number of generators to precompute
108104
/// for each party. For rangeproofs, it is sufficient to pass
109105
/// `64`, the maximum bitsize of the rangeproofs. For circuit
110106
/// proofs, the capacity must be greater than the number of
111107
/// multipliers, rounded up to the next power of two.
112108
/// * `party_capacity` is the maximum number of parties that can
113109
/// produce an aggregated proof.
114-
pub fn new(
115-
pedersen_gens: PedersenGenerators,
116-
gens_capacity: usize,
117-
party_capacity: usize,
118-
) -> Self {
110+
pub fn new(gens_capacity: usize, party_capacity: usize) -> Self {
119111
use byteorder::{ByteOrder, LittleEndian};
120112

121-
let G_vec = (0..party_capacity)
122-
.map(|i| {
123-
let party_index = i as u32;
124-
let mut label = [b'G', 0, 0, 0, 0];
125-
LittleEndian::write_u32(&mut label[1..5], party_index);
126-
127-
GeneratorsChain::new(&label)
128-
.take(gens_capacity)
129-
.collect::<Vec<_>>()
130-
}).collect();
131-
132-
let H_vec = (0..party_capacity)
133-
.map(|i| {
134-
let party_index = i as u32;
135-
let mut label = [b'H', 0, 0, 0, 0];
136-
LittleEndian::write_u32(&mut label[1..5], party_index);
137-
138-
GeneratorsChain::new(&label)
139-
.take(gens_capacity)
140-
.collect::<Vec<_>>()
141-
}).collect();
142-
143-
Generators {
144-
pedersen_gens,
113+
BulletproofGens {
145114
gens_capacity,
146115
party_capacity,
147-
G_vec,
148-
H_vec,
116+
G_vec: (0..party_capacity)
117+
.map(|i| {
118+
let party_index = i as u32;
119+
let mut label = [b'G', 0, 0, 0, 0];
120+
LittleEndian::write_u32(&mut label[1..5], party_index);
121+
122+
GeneratorsChain::new(&label)
123+
.take(gens_capacity)
124+
.collect::<Vec<_>>()
125+
}).collect(),
126+
H_vec: (0..party_capacity)
127+
.map(|i| {
128+
let party_index = i as u32;
129+
let mut label = [b'H', 0, 0, 0, 0];
130+
LittleEndian::write_u32(&mut label[1..5], party_index);
131+
132+
GeneratorsChain::new(&label)
133+
.take(gens_capacity)
134+
.collect::<Vec<_>>()
135+
}).collect(),
149136
}
150137
}
151138

152139
/// Returns j-th share of generators, with an appropriate
153140
/// slice of vectors G and H for the j-th range proof.
154-
pub fn share(&self, j: usize) -> GeneratorsView {
155-
GeneratorsView {
156-
pedersen_gens: &self.pedersen_gens,
141+
pub fn share(&self, j: usize) -> BulletproofGensShare {
142+
BulletproofGensShare {
157143
gens: &self,
158144
share: j,
159145
}
@@ -214,22 +200,20 @@ impl<'a> Iterator for AggregatedGensIter<'a> {
214200
}
215201
}
216202

217-
/// The `GeneratorsView` is produced by `Generators::share()`.
203+
/// The `BulletproofGensShare` is produced by `BulletproofGens::share()`.
218204
///
219-
/// The `Generators` struct represents generators for an aggregated
220-
/// range proof `m` proofs of `n` bits each; the `GeneratorsView`
205+
/// The `BulletproofGens` struct represents generators for an aggregated
206+
/// range proof `m` proofs of `n` bits each; the `BulletproofGensShare`
221207
/// represents the generators for one of the `m` parties' shares.
222208
#[derive(Copy, Clone)]
223-
pub struct GeneratorsView<'a> {
224-
/// Bases for Pedersen commitments
225-
pub pedersen_gens: &'a PedersenGenerators,
209+
pub struct BulletproofGensShare<'a> {
226210
/// The parent object that this is a view into
227-
gens: &'a Generators,
211+
gens: &'a BulletproofGens,
228212
/// Which share we are
229213
share: usize,
230214
}
231215

232-
impl<'a> GeneratorsView<'a> {
216+
impl<'a> BulletproofGensShare<'a> {
233217
/// Return an iterator over this party's G generators with given size `n`.
234218
pub(crate) fn G(&self, n: usize) -> impl Iterator<Item = &'a RistrettoPoint> {
235219
self.gens.G_vec[self.share].iter().take(n)
@@ -248,7 +232,7 @@ mod tests {
248232

249233
#[test]
250234
fn aggregated_gens_iter_matches_flat_map() {
251-
let gens = Generators::new(PedersenGenerators::default(), 64, 8);
235+
let gens = BulletproofGens::new(64, 8);
252236

253237
let helper = |n: usize, m: usize| {
254238
let agg_G: Vec<RistrettoPoint> = gens.G(n, m).cloned().collect();

src/inner_product_proof.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ mod tests {
336336
fn test_helper_create(n: usize) {
337337
let mut rng = OsRng::new().unwrap();
338338

339-
use generators::{Generators, PedersenGenerators};
340-
let gens = Generators::new(PedersenGenerators::default(), n, 1);
341-
let G: Vec<RistrettoPoint> = gens.share(0).G(n).cloned().collect();
342-
let H: Vec<RistrettoPoint> = gens.share(0).H(n).cloned().collect();
339+
use generators::BulletproofGens;
340+
let bp_gens = BulletproofGens::new(n, 1);
341+
let G: Vec<RistrettoPoint> = bp_gens.share(0).G(n).cloned().collect();
342+
let H: Vec<RistrettoPoint> = bp_gens.share(0).H(n).cloned().collect();
343343

344344
// Q would be determined upstream in the protocol, so we pick a random one.
345345
let Q = RistrettoPoint::hash_from_bytes::<Sha3_512>(b"test point");

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mod transcript;
4141
pub use merlin::Transcript;
4242

4343
pub use errors::ProofError;
44-
pub use generators::{Generators, GeneratorsView, PedersenGenerators};
44+
pub use generators::{BulletproofGens, BulletproofGensShare, PedersenGens};
4545
pub use range_proof::RangeProof;
4646

4747
#[doc(include = "../docs/aggregation-api.md")]

0 commit comments

Comments
 (0)