Skip to content

Commit 5686a4d

Browse files
feat: add batch commit to trait (#366)
* feat: add batch_commit and default implementation to CommitmentEngineTrait * feat: add batch_vartime_multiscalar_mul and default implementation to DlogGroup trait * feat: implement batch_commit in hyperkzg * fix: batch_commit should support using a slice of blinding factors
1 parent b2f2bd6 commit 5686a4d

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/provider/hyperkzg.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,27 @@ where
278278
}
279279
}
280280

281+
fn batch_commit(
282+
ck: &Self::CommitmentKey,
283+
v: &[Vec<<E as Engine>::Scalar>],
284+
r: &[<E as Engine>::Scalar],
285+
) -> Vec<Self::Commitment> {
286+
assert!(v.len() == r.len());
287+
288+
let max = v.iter().map(|v| v.len()).max().unwrap_or(0);
289+
assert!(ck.ck.len() >= max);
290+
291+
let h = <E::GE as DlogGroup>::group(&ck.h);
292+
293+
E::GE::batch_vartime_multiscalar_mul(v, &ck.ck[..max])
294+
.iter()
295+
.zip(r.iter())
296+
.map(|(commit, r_i)| Commitment {
297+
comm: *commit + (h * r_i),
298+
})
299+
.collect()
300+
}
301+
281302
fn derandomize(
282303
dk: &Self::DerandKey,
283304
commit: &Self::Commitment,
@@ -550,9 +571,10 @@ where
550571

551572
// We do not need to commit to the first polynomial as it is already committed.
552573
// Compute commitments in parallel
553-
let com: Vec<G1Affine<E>> = (1..polys.len())
554-
.into_par_iter()
555-
.map(|i| E::CE::commit(ck, &polys[i], &E::Scalar::ZERO).comm.affine())
574+
let r = vec![E::Scalar::ZERO; ell - 1];
575+
let com: Vec<G1Affine<E>> = E::CE::batch_commit(ck, &polys[1..], r.as_slice())
576+
.iter()
577+
.map(|i| i.comm.affine())
556578
.collect();
557579

558580
// Phase 2

src/provider/traits.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use core::{
33
fmt::Debug,
44
ops::{Add, AddAssign, Sub, SubAssign},
55
};
6+
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
67
use serde::{Deserialize, Serialize};
78

89
/// A helper trait for types with a group operation.
@@ -48,6 +49,17 @@ pub trait DlogGroup:
4849
/// A method to compute a multiexponentation
4950
fn vartime_multiscalar_mul(scalars: &[Self::Scalar], bases: &[Self::AffineGroupElement]) -> Self;
5051

52+
/// A method to compute a batch of multiexponentations
53+
fn batch_vartime_multiscalar_mul(
54+
scalars: &[Vec<Self::Scalar>],
55+
bases: &[Self::AffineGroupElement],
56+
) -> Vec<Self> {
57+
scalars
58+
.par_iter()
59+
.map(|scalar| Self::vartime_multiscalar_mul(scalar, &bases[..scalar.len()]))
60+
.collect::<Vec<_>>()
61+
}
62+
5163
/// Produce a vector of group elements using a static label
5264
fn from_label(label: &'static [u8], n: usize) -> Vec<Self::AffineGroupElement>;
5365

src/traits/commitment.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use core::{
55
fmt::Debug,
66
ops::{Add, Mul, MulAssign},
77
};
8+
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
89
use serde::{Deserialize, Serialize};
910

1011
/// A helper trait for types implementing scalar multiplication.
@@ -62,6 +63,19 @@ pub trait CommitmentEngineTrait<E: Engine>: Clone + Send + Sync {
6263
/// Commits to the provided vector using the provided generators and random blind
6364
fn commit(ck: &Self::CommitmentKey, v: &[E::Scalar], r: &E::Scalar) -> Self::Commitment;
6465

66+
/// Batch commits to the provided vectors using the provided generators and random blind
67+
fn batch_commit(
68+
ck: &Self::CommitmentKey,
69+
v: &[Vec<E::Scalar>],
70+
r: &[E::Scalar],
71+
) -> Vec<Self::Commitment> {
72+
assert!(v.len() == r.len());
73+
v.par_iter()
74+
.zip(r.par_iter())
75+
.map(|(v_i, r_i)| Self::commit(ck, v_i, r_i))
76+
.collect()
77+
}
78+
6579
/// Remove given blind from commitment
6680
fn derandomize(
6781
dk: &Self::DerandKey,

0 commit comments

Comments
 (0)