From 4358263bc5858f666e12c5da21c8e03401f3e0ce Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Tue, 3 Nov 2020 11:38:54 -0800 Subject: [PATCH] fixes --- Cargo.toml | 2 + src/lib.rs | 213 ++++++++++------------------------- src/marlin_pc/constraints.rs | 8 +- src/pc_constraints.rs | 8 +- 4 files changed, 69 insertions(+), 162 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f00ead7f..9f486f33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,8 @@ ark-relations = { git = "https://github.com/arkworks-rs/snark", default-features ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std" } +ark-nonnative-field = { git = "https://github.com/arkworks-rs/nonnative" } + bench-utils = { git = "https://github.com/arkworks-rs/utils", default-features = false } rand_core = { version = "0.5", default-features = false } diff --git a/src/lib.rs b/src/lib.rs index 9208a2eb..7b06c6ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -587,174 +587,79 @@ pub trait PolynomialCommitment: Sized { Ok(true) } - - /// open but with individual challenges - /// the non-individual version `open` should call this method with - /// `opening_challenges = |pow| opening_challenge.pow(&[pow]);`, - /// i.e., the same impl as in MarlinKZG. - fn open_individual_opening_challenges<'a>( + + /// batch_open with individual challenges + fn batch_open_individual_opening_challenges<'a>( ck: &Self::CommitterKey, labeled_polynomials: impl IntoIterator>, commitments: impl IntoIterator>, - point: F, - opening_challenges: &dyn Fn(usize) -> F, + query_set: &QuerySet, + opening_challenges: &dyn Fn(u64) -> F, rands: impl IntoIterator, rng: Option<&mut dyn RngCore>, - ) -> Result + ) -> Result where Self::Randomness: 'a, Self::Commitment: 'a, { - Self::open( - ck, - labeled_polynomials, - commitments, - point, - opening_challenges(0), - rands, - rng, - ) - } + let rng = &mut crate::optional_rng::OptionalRng(rng); + let poly_rand_comm: BTreeMap<_, _> = labeled_polynomials + .into_iter() + .zip(rands) + .zip(commitments.into_iter()) + .map(|((poly, r), comm)| (poly.label(), (poly, r, comm))) + .collect(); - /// check but with individual challenges - /// The non-individual version `check` should call this method with - /// `opening_challenges = |pow| opening_challenge.pow(&[pow]);`, - /// i.e., the same impl as in MarlinKZG. - fn check_individual_opening_challenges<'a>( - vk: &Self::VerifierKey, - commitments: impl IntoIterator>, - point: F, - values: impl IntoIterator, - proof: &Self::Proof, - opening_challenges: &dyn Fn(usize) -> F, - rng: Option<&mut dyn RngCore>, - ) -> Result - where - Self::Commitment: 'a, - { - Self::check( - vk, - commitments, - point, - values, - proof, - opening_challenges(0), - rng, - ) - } + let open_time = start_timer!(|| format!( + "Opening {} polynomials at query set of size {}", + poly_rand_comm.len(), + query_set.len(), + )); - /// batch_check but with individual challenges - /// The non-individual version `batch_check` should call this method with - /// `opening_challenges = |pow| opening_challenge.pow(&[pow]);`, - /// i.e., the same impl as in MarlinKZG. - fn batch_check_individual_opening_challenges<'a, R: RngCore>( - vk: &Self::VerifierKey, - commitments: impl IntoIterator>, - query_set: &QuerySet, - evaluations: &Evaluations, - proof: &Self::BatchProof, - opening_challenges: &dyn Fn(usize) -> F, - rng: &mut R, - ) -> Result - where - Self::Commitment: 'a, - { - Self::batch_check( - vk, - commitments, - query_set, - evaluations, - proof, - opening_challenges(0), - rng, - ) - } + let mut query_to_labels_map = BTreeMap::new(); - /// open_combinations but with individual challenges - /// The non-individual version `open_combinations` should call this method with - /// `opening_challenges = |pow| opening_challenge.pow(&[pow]);`, - /// i.e., the same impl as in MarlinKZG. - fn open_combinations_individual_opening_challenges<'a>( - ck: &Self::CommitterKey, - lc_s: impl IntoIterator>, - polynomials: impl IntoIterator>, - commitments: impl IntoIterator>, - query_set: &QuerySet, - opening_challenges: &dyn Fn(usize) -> F, - rands: impl IntoIterator, - rng: Option<&mut dyn RngCore>, - ) -> Result, Self::Error> - where - Self::Randomness: 'a, - Self::Commitment: 'a, - { - Self::open_combinations( - ck, - lc_s, - polynomials, - commitments, - query_set, - opening_challenges(0), - rands, - rng, - ) - } + for (label, (point_label, point)) in query_set.iter() { + let labels = query_to_labels_map + .entry(point_label) + .or_insert((point, BTreeSet::new())); + labels.1.insert(label); + } - /// check_combinations but with individual challenges - /// The non-individual version `check_combinations` should call this method with - /// `opening_challenges = |pow| opening_challenge.pow(&[pow]);`, - /// i.e., the same impl as in MarlinKZG. - fn check_combinations_individual_opening_challenges<'a, R: RngCore>( - vk: &Self::VerifierKey, - lc_s: impl IntoIterator>, - commitments: impl IntoIterator>, - query_set: &QuerySet, - evaluations: &Evaluations, - proof: &BatchLCProof, - opening_challenges: &dyn Fn(usize) -> F, - rng: &mut R, - ) -> Result - where - Self::Commitment: 'a, - { - Self::check_combinations( - vk, - lc_s, - commitments, - query_set, - evaluations, - proof, - opening_challenges(0), - rng, - ) - } + let mut proofs = Vec::new(); + for (_point_label, (point, labels)) in query_to_labels_map.into_iter() { + let mut query_polys: Vec<&'a LabeledPolynomial<_>> = Vec::new(); + let mut query_rands: Vec<&'a Self::Randomness> = Vec::new(); + let mut query_comms: Vec<&'a LabeledCommitment> = Vec::new(); - /// batch_open but with individual challenges - /// The non-individual version `batch_open` should call this method with - /// `opening_challenges = |pow| opening_challenge.pow(&[pow]);`, - /// i.e., the same impl as in MarlinKZG. - fn batch_open_individual_opening_challenges<'a>( - ck: &Self::CommitterKey, - labeled_polynomials: impl IntoIterator>, - commitments: impl IntoIterator>, - query_set: &QuerySet, - opening_challenges: &dyn Fn(u64) -> F, - rands: impl IntoIterator, - rng: Option<&mut dyn RngCore>, - ) -> Result - where - Self::Randomness: 'a, - Self::Commitment: 'a, - { - Self::batch_open( - ck, - labeled_polynomials, - commitments, - query_set, - opening_challenges(0), - rands, - rng, - ) + for label in labels { + let (polynomial, rand, comm) = + poly_rand_comm.get(label).ok_or(Error::MissingPolynomial { + label: label.to_string(), + })?; + + query_polys.push(polynomial); + query_rands.push(rand); + query_comms.push(comm); + } + + let proof_time = start_timer!(|| "Creating proof"); + let proof = Self::open_individual_opening_challenges( + ck, + query_polys, + query_comms, + *point, + opening_challenges, + query_rands, + Some(rng), + )?; + + end_timer!(proof_time); + + proofs.push(proof); + } + end_timer!(open_time); + + Ok(proofs.into()) } } diff --git a/src/marlin_pc/constraints.rs b/src/marlin_pc/constraints.rs index 4ae74214..9f7557f2 100644 --- a/src/marlin_pc/constraints.rs +++ b/src/marlin_pc/constraints.rs @@ -1348,7 +1348,7 @@ where let mut combined_queries = Vec::new(); let mut combined_comms = Vec::new(); let mut combined_evals = Vec::new(); - for (_point_label, (point, labels)) in query_to_labels_map.into_iter() { + for (point_label, (point, labels)) in query_to_labels_map.into_iter() { let mut comms_to_combine = Vec::< Vec<( Option< @@ -1366,7 +1366,7 @@ where for label in labels.into_iter() { let commitment_lc = commitment_lcs.get(label).unwrap().clone(); - let v_i = evaluations.0.get(&(label.clone(), point.clone())).unwrap(); + let v_i = evaluations.0.get(&(label.clone(), point_label.clone())).unwrap(); comms_to_combine.push(commitment_lc.1.clone()); values_to_combine.push(v_i.clone()); @@ -1659,7 +1659,7 @@ where let mut combined_queries = Vec::new(); let mut combined_comms = Vec::new(); let mut combined_evals = Vec::new(); - for (_point_label, (point, labels)) in query_to_labels_map.into_iter() { + for (point_label, (point, labels)) in query_to_labels_map.into_iter() { let mut comms_to_combine: Vec = Vec::new(); let mut values_to_combine = Vec::new(); for label in labels.into_iter() { @@ -1670,7 +1670,7 @@ where commitment.commitment.shifted_comm.is_some() ); - let v_i = evaluations.0.get(&(label.clone(), point.clone())).unwrap(); + let v_i = evaluations.0.get(&(label.clone(), point_label.clone())).unwrap(); comms_to_combine.push(commitment.clone()); values_to_combine.push(v_i.clone()); diff --git a/src/pc_constraints.rs b/src/pc_constraints.rs index 288b086c..145b86c8 100644 --- a/src/pc_constraints.rs +++ b/src/pc_constraints.rs @@ -166,8 +166,8 @@ pub struct QuerySetVar( /// An allocated version of `Evaluations`. #[derive(Clone)] pub struct EvaluationsVar( - pub BTreeMap< - (String, NonNativeFieldVar), + pub BTreeMap< + (String, String), NonNativeFieldVar, >, ); @@ -177,9 +177,9 @@ impl EvaluationsVar, + point_label: &String, ) -> Result, SynthesisError> { - let key = (lc_string.clone(), point.clone()); + let key = (lc_string.clone(), point_label.clone()); Ok(self.0.get(&key).map(|v| (*v).clone()).unwrap()) } }