Skip to content

Commit 124a74f

Browse files
huyuncongPratyushmmaker
authored
Add streaming KZG (#95)
Co-authored-by: Pratyush Mishra <pratyushmishra@berkeley.edu> Co-authored-by: Michele Orrù <michele.orru@berkeley.edu>
1 parent 4d78d53 commit 124a74f

File tree

15 files changed

+1356
-29
lines changed

15 files changed

+1356
-29
lines changed

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ ark-sponge = {version = "^0.3.0", default-features = false}
3030
ark-std = { version = "^0.3.0", default-features = false }
3131
ark-relations = { version = "^0.3.0", default-features = false, optional = true }
3232
ark-r1cs-std = { version = "^0.3.0", default-features = false, optional = true }
33-
ark-nonnative-field = { version = "^0.3.0", default-features = false, optional = true }
3433
hashbrown = { version = "0.9", optional = true }
3534

3635
digest = "0.9"
@@ -61,14 +60,17 @@ debug = true
6160
ark-std = { git = "https://github.com/arkworks-rs/std" }
6261
ark-ec = { git = "https://github.com/arkworks-rs/algebra" }
6362
ark-ff = { git = "https://github.com/arkworks-rs/algebra" }
63+
ark-poly = { git = "https://github.com/arkworks-rs/algebra" }
6464
ark-serialize = { git = "https://github.com/arkworks-rs/algebra" }
6565
ark-bls12-381 = { git = "https://github.com/arkworks-rs/curves" }
6666
ark-bls12-377 = { git = "https://github.com/arkworks-rs/curves" }
6767
ark-ed-on-bls12-381 = { git = "https://github.com/arkworks-rs/curves" }
68+
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std" }
69+
ark-sponge = { git = "https://github.com/arkworks-rs/sponge" }
6870

6971
[features]
7072
default = [ "std", "parallel" ]
71-
std = [ "ark-ff/std", "ark-ec/std", "ark-nonnative-field/std", "ark-poly/std", "ark-std/std", "ark-relations/std", "ark-serialize/std", "ark-sponge/std"]
72-
r1cs = [ "ark-relations", "ark-r1cs-std", "ark-nonnative-field", "hashbrown", "ark-sponge/r1cs"]
73+
std = [ "ark-ff/std", "ark-ec/std", "ark-poly/std", "ark-std/std", "ark-relations/std", "ark-serialize/std", "ark-sponge/std"]
74+
r1cs = [ "ark-relations", "ark-r1cs-std", "hashbrown", "ark-sponge/r1cs"]
7375
print-trace = [ "ark-std/print-trace" ]
7476
parallel = [ "std", "ark-ff/parallel", "ark-ec/parallel", "ark-poly/parallel", "ark-std/parallel", "rayon" ]

src/constraints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::{
33
PolynomialCommitment, String, Vec,
44
};
55
use ark_ff::PrimeField;
6-
use ark_nonnative_field::NonNativeFieldVar;
76
use ark_poly::Polynomial;
7+
use ark_r1cs_std::fields::nonnative::NonNativeFieldVar;
88
use ark_r1cs_std::{fields::fp::FpVar, prelude::*};
99
use ark_relations::r1cs::{ConstraintSystemRef, Namespace, Result as R1CSResult, SynthesisError};
1010
use ark_sponge::CryptographicSponge;

src/ipa_pc/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ where
6262
randomizer: Option<G::ScalarField>,
6363
) -> G::Projective {
6464
let scalars_bigint = ark_std::cfg_iter!(scalars)
65-
.map(|s| s.into_repr())
65+
.map(|s| s.into_bigint())
6666
.collect::<Vec<_>>();
6767

6868
let mut comm = VariableBase::msm(comm_key, &scalars_bigint);
@@ -160,7 +160,7 @@ where
160160
let h_prime = vk.h.mul(round_challenge);
161161

162162
let mut round_commitment_proj =
163-
combined_commitment_proj + &h_prime.mul(&combined_v.into_repr());
163+
combined_commitment_proj + &h_prime.mul(&combined_v.into_bigint());
164164

165165
let l_iter = proof.l_vec.iter();
166166
let r_iter = proof.r_vec.iter();

src/kzg10/data_structures.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ pub struct PreparedVerifierKey<E: PairingEngine> {
391391
impl<E: PairingEngine> PreparedVerifierKey<E> {
392392
/// prepare `PreparedVerifierKey` from `VerifierKey`
393393
pub fn prepare(vk: &VerifierKey<E>) -> Self {
394-
let supported_bits = E::Fr::size_in_bits();
394+
let supported_bits = E::Fr::MODULUS_BIT_SIZE as usize;
395395

396396
let mut prepared_g = Vec::<E::G1Affine>::new();
397397
let mut g = E::G1Projective::from(vk.g.clone());
@@ -458,7 +458,7 @@ where
458458
impl<'a, E: PairingEngine> AddAssign<(E::Fr, &'a Commitment<E>)> for Commitment<E> {
459459
#[inline]
460460
fn add_assign(&mut self, (f, other): (E::Fr, &'a Commitment<E>)) {
461-
let mut other = other.0.mul(f.into_repr());
461+
let mut other = other.0.mul(f.into_bigint());
462462
other.add_assign_mixed(&self.0);
463463
self.0 = other.into();
464464
}
@@ -485,7 +485,7 @@ impl<E: PairingEngine> PreparedCommitment<E> {
485485
let mut prepared_comm = Vec::<E::G1Affine>::new();
486486
let mut cur = E::G1Projective::from(comm.0.clone());
487487

488-
let supported_bits = E::Fr::size_in_bits();
488+
let supported_bits = E::Fr::MODULUS_BIT_SIZE as usize;
489489

490490
for _ in 0..supported_bits {
491491
prepared_comm.push(cur.clone().into());

src/kzg10/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ where
6060

6161
let window_size = FixedBase::get_mul_window_size(max_degree + 1);
6262

63-
let scalar_bits = E::Fr::size_in_bits();
63+
let scalar_bits = E::Fr::MODULUS_BIT_SIZE as usize;
6464
let g_time = start_timer!(|| "Generating powers of G");
6565
let g_table = FixedBase::get_window_table(scalar_bits, window_size, g);
6666
let powers_of_g =
@@ -331,8 +331,8 @@ where
331331
if let Some(random_v) = proof.random_v {
332332
gamma_g_multiplier += &(randomizer * &random_v);
333333
}
334-
total_c += &c.mul(randomizer.into_repr());
335-
total_w += &w.mul(randomizer.into_repr());
334+
total_c += &c.mul(randomizer.into_bigint());
335+
total_w += &w.mul(randomizer.into_bigint());
336336
// We don't need to sample randomizers from the full field,
337337
// only from 128-bit strings.
338338
randomizer = u128::rand(rng).into();
@@ -430,7 +430,7 @@ fn skip_leading_zeros_and_convert_to_bigints<F: PrimeField, P: UVPolynomial<F>>(
430430
fn convert_to_bigints<F: PrimeField>(p: &[F]) -> Vec<F::BigInt> {
431431
let to_bigint_time = start_timer!(|| "Converting polynomial coeffs to bigints");
432432
let coeffs = ark_std::cfg_iter!(p)
433-
.map(|s| s.into_repr())
433+
.map(|s| s.into_bigint())
434434
.collect::<Vec<_>>();
435435
end_timer!(to_bigint_time);
436436
coeffs

src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ use ark_sponge::{CryptographicSponge, FieldElementSize};
118118
/// [marlin]: https://eprint.iacr.org/2019/104
119119
pub use marlin::marlin_pst13_pc;
120120

121+
/// Streaming polynomial commitment based on the construction in
122+
/// [[BCHO22, "Gemini"]][gemini] with batching techniques inspired
123+
/// by [[BDFG20]][bdfg].
124+
///
125+
/// [gemini]:
126+
/// [bdfg]: https://eprint.iacr.org/2020/081.pdf
127+
pub mod streaming_kzg;
128+
121129
/// `QuerySet` is the set of queries that are to be made to a set of labeled polynomials/equations
122130
/// `p` that have previously been committed to. Each element of a `QuerySet` is a pair of
123131
/// `(label, (point_label, point))`, where `label` is the label of a polynomial in `p`,
@@ -527,7 +535,7 @@ fn lc_query_set_to_poly_query_set<'a, F: Field, T: Clone + Ord>(
527535
pub mod tests {
528536
use crate::*;
529537
use ark_poly::Polynomial;
530-
use ark_sponge::poseidon::{PoseidonParameters, PoseidonSponge};
538+
use ark_sponge::poseidon::{PoseidonConfig, PoseidonSponge};
531539
use ark_std::rand::{
532540
distributions::{Distribution, Uniform},
533541
Rng, SeedableRng,
@@ -1287,7 +1295,7 @@ pub mod tests {
12871295
///
12881296
/// WARNING: This poseidon parameter is not secure. Please generate
12891297
/// your own parameters according the field you use.
1290-
pub(crate) fn poseidon_parameters_for_test<F: PrimeField>() -> PoseidonParameters<F> {
1298+
pub(crate) fn poseidon_parameters_for_test<F: PrimeField>() -> PoseidonConfig<F> {
12911299
let full_rounds = 8;
12921300
let partial_rounds = 31;
12931301
let alpha = 17;
@@ -1309,6 +1317,6 @@ pub mod tests {
13091317
}
13101318
ark.push(res);
13111319
}
1312-
PoseidonParameters::new(full_rounds, partial_rounds, alpha, mds, ark)
1320+
PoseidonConfig::new(full_rounds, partial_rounds, alpha, mds, ark, 2, 1)
13131321
}
13141322
}

src/marlin/marlin_pc/data_structures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<E: PairingEngine> PCPreparedVerifierKey<VerifierKey<E>> for PreparedVerifie
193193
fn prepare(vk: &VerifierKey<E>) -> Self {
194194
let prepared_vk = kzg10::PreparedVerifierKey::<E>::prepare(&vk.vk);
195195

196-
let supported_bits = E::Fr::size_in_bits();
196+
let supported_bits = E::Fr::MODULUS_BIT_SIZE as usize;
197197

198198
let prepared_degree_bounds_and_shift_powers: Option<Vec<(usize, Vec<E::G1Affine>)>> =
199199
if vk.degree_bounds_and_shift_powers.is_some() {

src/marlin/marlin_pst13_pc/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<E: PairingEngine, P: MVPolynomial<E::Fr>, S: CryptographicSponge> MarlinPST
134134
/// Convert polynomial coefficients to `BigInt`
135135
fn convert_to_bigints(p: &P) -> Vec<<E::Fr as PrimeField>::BigInt> {
136136
let plain_coeffs = ark_std::cfg_into_iter!(p.terms())
137-
.map(|(coeff, _)| coeff.into_repr())
137+
.map(|(coeff, _)| coeff.into_bigint())
138138
.collect();
139139
plain_coeffs
140140
}
@@ -213,7 +213,7 @@ where
213213
})
214214
.unzip();
215215

216-
let scalar_bits = E::Fr::size_in_bits();
216+
let scalar_bits = E::Fr::MODULUS_BIT_SIZE as usize;
217217
let g_time = start_timer!(|| "Generating powers of G");
218218
let window_size = FixedBase::get_mul_window_size(max_degree + 1);
219219
let g_table = FixedBase::get_window_table(scalar_bits, window_size, g);
@@ -256,7 +256,7 @@ where
256256
.collect();
257257
let beta_h: Vec<_> = betas
258258
.iter()
259-
.map(|b| h.mul(&(*b).into_repr()).into_affine())
259+
.map(|b| h.mul(&(*b).into_bigint()).into_affine())
260260
.collect();
261261
let h = h.into_affine();
262262
let prepared_h = h.into();
@@ -625,16 +625,16 @@ where
625625
if let Some(random_v) = proof.random_v {
626626
gamma_g_multiplier += &(randomizer * &random_v);
627627
}
628-
total_c += &c.mul(&randomizer.into_repr());
628+
total_c += &c.mul(&randomizer.into_bigint());
629629
ark_std::cfg_iter_mut!(total_w)
630630
.enumerate()
631631
.for_each(|(i, w_i)| *w_i += &w[i].mul(randomizer));
632632
// We don't need to sample randomizers from the full field,
633633
// only from 128-bit strings.
634634
randomizer = u128::rand(rng).into();
635635
}
636-
total_c -= &g.mul(&g_multiplier.into_repr());
637-
total_c -= &gamma_g.mul(&gamma_g_multiplier.into_repr());
636+
total_c -= &g.mul(&g_multiplier.into_bigint());
637+
total_c -= &gamma_g.mul(&gamma_g_multiplier.into_bigint());
638638
end_timer!(combination_time);
639639

640640
let to_affine_time = start_timer!(|| "Converting results to affine for pairing");

src/multilinear_pc/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<E: PairingEngine> MultilinearPC<E> {
3232
let mut powers_of_g = Vec::new();
3333
let mut powers_of_h = Vec::new();
3434
let t: Vec<_> = (0..num_vars).map(|_| E::Fr::rand(rng)).collect();
35-
let scalar_bits = E::Fr::size_in_bits();
35+
let scalar_bits = E::Fr::MODULUS_BIT_SIZE as usize;
3636

3737
let mut eq: LinkedList<DenseMultilinearExtension<E::Fr>> =
3838
LinkedList::from_iter(eq_extension(&t).into_iter());
@@ -144,7 +144,7 @@ impl<E: PairingEngine> MultilinearPC<E> {
144144
let scalars: Vec<_> = polynomial
145145
.to_evaluations()
146146
.into_iter()
147-
.map(|x| x.into_repr())
147+
.map(|x| x.into_bigint())
148148
.collect();
149149
let g_product = VariableBase::msm(&ck.powers_of_g[0], scalars.as_slice()).into_affine();
150150
Commitment { nv, g_product }
@@ -175,7 +175,7 @@ impl<E: PairingEngine> MultilinearPC<E> {
175175
+ &(r[k][(b << 1) + 1] * &point_at_k);
176176
}
177177
let scalars: Vec<_> = (0..(1 << k))
178-
.map(|x| q[k][x >> 1].into_repr()) // fine
178+
.map(|x| q[k][x >> 1].into_bigint()) // fine
179179
.collect();
180180

181181
let pi_h = VariableBase::msm(&ck.powers_of_h[i], &scalars).into_affine(); // no need to move outside and partition
@@ -199,7 +199,7 @@ impl<E: PairingEngine> MultilinearPC<E> {
199199
vk.h,
200200
);
201201

202-
let scalar_size = E::Fr::size_in_bits();
202+
let scalar_size = E::Fr::MODULUS_BIT_SIZE as usize;
203203
let window_size = FixedBase::get_mul_window_size(vk.nv);
204204

205205
let g_table = FixedBase::get_window_table(scalar_size, window_size, vk.g.into_projective());

src/sonic_pc/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ where
6666
let mut comm_with_challenge: E::G1Projective = comm.0.mul(curr_challenge);
6767

6868
if let Some(randomizer) = randomizer {
69-
comm_with_challenge = comm_with_challenge.mul(&randomizer.into_repr());
69+
comm_with_challenge = comm_with_challenge.mul(&randomizer.into_bigint());
7070
}
7171

7272
// Accumulate values in the BTreeMap
@@ -85,7 +85,7 @@ where
8585

8686
if let Some(randomizer) = randomizer {
8787
witness = proof.w.mul(randomizer);
88-
adjusted_witness = adjusted_witness.mul(&randomizer.into_repr());
88+
adjusted_witness = adjusted_witness.mul(&randomizer.into_bigint());
8989
}
9090

9191
*combined_witness += &witness;

0 commit comments

Comments
 (0)