Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
npwardberkeley committed Nov 3, 2020
1 parent d04a9e9 commit 8f17d50
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 163 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
211 changes: 58 additions & 153 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,173 +588,78 @@ pub trait PolynomialCommitment<F: Field>: 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<Item = &'a LabeledPolynomial<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
point: F,
opening_challenges: &dyn Fn(usize) -> F,
query_set: &QuerySet<F>,
opening_challenges: &dyn Fn(u64) -> F,
rands: impl IntoIterator<Item = &'a Self::Randomness>,
rng: Option<&mut dyn RngCore>,
) -> Result<Self::Proof, Self::Error>
) -> Result<Self::BatchProof, Self::Error>
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<Item = &'a LabeledCommitment<Self::Commitment>>,
point: F,
values: impl IntoIterator<Item = F>,
proof: &Self::Proof,
opening_challenges: &dyn Fn(usize) -> F,
rng: Option<&mut dyn RngCore>,
) -> Result<bool, Self::Error>
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<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
evaluations: &Evaluations<F>,
proof: &Self::BatchProof,
opening_challenges: &dyn Fn(usize) -> F,
rng: &mut R,
) -> Result<bool, Self::Error>
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<Item = &'a LinearCombination<F>>,
polynomials: impl IntoIterator<Item = &'a LabeledPolynomial<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
opening_challenges: &dyn Fn(usize) -> F,
rands: impl IntoIterator<Item = &'a Self::Randomness>,
rng: Option<&mut dyn RngCore>,
) -> Result<BatchLCProof<F, Self>, 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<Item = &'a LinearCombination<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
evaluations: &Evaluations<F>,
proof: &BatchLCProof<F, Self>,
opening_challenges: &dyn Fn(usize) -> F,
rng: &mut R,
) -> Result<bool, Self::Error>
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<Self::Commitment>> = 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<Item = &'a LabeledPolynomial<F>>,
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<F>,
opening_challenges: &dyn Fn(u64) -> F,
rands: impl IntoIterator<Item = &'a Self::Randomness>,
rng: Option<&mut dyn RngCore>,
) -> Result<Self::BatchProof, Self::Error>
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())
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/marlin_pc/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -1366,7 +1366,10 @@ 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());
Expand Down Expand Up @@ -1659,7 +1662,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<Self::LabeledCommitmentVar> = Vec::new();
let mut values_to_combine = Vec::new();
for label in labels.into_iter() {
Expand All @@ -1670,7 +1673,10 @@ 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());
Expand Down
9 changes: 3 additions & 6 deletions src/pc_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,17 @@ pub struct QuerySetVar<TargetField: PrimeField, BaseField: PrimeField>(
/// An allocated version of `Evaluations`.
#[derive(Clone)]
pub struct EvaluationsVar<TargetField: PrimeField, BaseField: PrimeField>(
pub BTreeMap<
(String, NonNativeFieldVar<TargetField, BaseField>),
NonNativeFieldVar<TargetField, BaseField>,
>,
pub BTreeMap<(String, String), NonNativeFieldVar<TargetField, BaseField>>,
);

impl<TargetField: PrimeField, BaseField: PrimeField> EvaluationsVar<TargetField, BaseField> {
/// find the evaluation result
pub fn get_lc_eval(
&self,
lc_string: &String,
point: &NonNativeFieldVar<TargetField, BaseField>,
point_label: &String,
) -> Result<NonNativeFieldVar<TargetField, BaseField>, 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())
}
}

0 comments on commit 8f17d50

Please sign in to comment.