Skip to content

Updates Marlin constraints to latest LC representation #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rand_core = { version = "0.5", default-features = false }
digest = "0.8"
rayon = { version = "1", optional = true }
derivative = { version = "2", features = [ "use_core" ] }
tracing = { version = "0.1", default-features = false, features = [ "attributes" ] }

[dev-dependencies]
rand = { version = "0.7", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<TargetField: PrimeField, BaseField: PrimeField>
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
/// A collection of random data used in the polynomial commitment checking.
pub struct PCCheckRandomDataVar<TargetField: PrimeField, BaseField: PrimeField> {
/// Opening challenges.
Expand Down
146 changes: 72 additions & 74 deletions src/marlin_pc/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{
constraints::{EvaluationsVar, PCCheckVar, QuerySetVar},
constraints::{EvaluationsVar, LabeledPointVar, PCCheckRandomDataVar, PCCheckVar, QuerySetVar},
data_structures::LabeledCommitment,
kzg10::{Proof, VerifierKey as KZG10VerifierKey},
marlin_pc::{
data_structures::{Commitment, VerifierKey},
MarlinKZG10, PreparedCommitment, PreparedVerifierKey,
},
BTreeMap, BTreeSet, BatchLCProof, LinearCombinationVar, PrepareVar, String, ToString, Vec,
BTreeMap, BTreeSet, BatchLCProof, LinearCombinationCoeffVar, LinearCombinationVar,
PrepareGadget, String, ToString, Vec,
};
use ark_ec::{CycleEngine, PairingEngine};
use ark_ff::{fields::Field, PrimeField, ToConstraintField};
Expand Down Expand Up @@ -395,7 +396,7 @@ where
}
}

impl<CycleE, PG> PrepareVar<VerifierKeyVar<CycleE, PG>, <CycleE::E1 as PairingEngine>::Fr>
impl<CycleE, PG> PrepareGadget<VerifierKeyVar<CycleE, PG>, <CycleE::E1 as PairingEngine>::Fr>
for PreparedVerifierKeyVar<CycleE, PG>
where
CycleE: CycleEngine,
Expand Down Expand Up @@ -751,7 +752,7 @@ where
}
}

impl<CycleE, PG> PrepareVar<CommitmentVar<CycleE, PG>, <CycleE::E1 as PairingEngine>::Fr>
impl<CycleE, PG> PrepareGadget<CommitmentVar<CycleE, PG>, <CycleE::E1 as PairingEngine>::Fr>
for PreparedCommitmentVar<CycleE, PG>
where
CycleE: CycleEngine,
Expand Down Expand Up @@ -779,23 +780,6 @@ where
shifted_comm: unprepared.shifted_comm.clone(),
})
}

#[tracing::instrument(target = "r1cs", skip(unprepared))]
fn prepare_small(unprepared: &CommitmentVar<CycleE, PG>) -> R1CSResult<Self> {
let mut prepared_comm = Vec::<PG::G1Var>::new();
let supported_bits = 128;

let mut cur: PG::G1Var = unprepared.comm.clone();
for _ in 0..supported_bits {
prepared_comm.push(cur.clone());
cur.double_in_place()?;
}

Ok(Self {
prepared_comm,
shifted_comm: unprepared.shifted_comm.clone(),
})
}
}

impl<CycleE, PG> AllocVar<PreparedCommitment<CycleE::E2>, <CycleE::E1 as PairingEngine>::Fr>
Expand Down Expand Up @@ -1007,7 +991,7 @@ where
}
}

impl<CycleE, PG> PrepareVar<LabeledCommitmentVar<CycleE, PG>, <CycleE::E1 as PairingEngine>::Fr>
impl<CycleE, PG> PrepareGadget<LabeledCommitmentVar<CycleE, PG>, <CycleE::E1 as PairingEngine>::Fr>
for PreparedLabeledCommitmentVar<CycleE, PG>
where
CycleE: CycleEngine,
Expand Down Expand Up @@ -1192,7 +1176,7 @@ where

let BatchLCProof { proof, evals } = proof.borrow().clone();

let proofs: Vec<Proof<_>> = proof;
let proofs: Vec<Proof<_>> = proof.to_vec();
let proofs: Vec<ProofVar<CycleE, PG>> = proofs
.iter()
.map(|p| {
Expand Down Expand Up @@ -1361,10 +1345,10 @@ where

let mut query_to_labels_map = BTreeMap::new();

for (label, (point_label, point)) in query_set.0.iter() {
for (label, point) in query_set.0.iter() {
let labels = query_to_labels_map
.entry((*point_label).clone())
.or_insert((point, BTreeSet::new()));
.entry(point.name.clone())
.or_insert((point.value.clone(), BTreeSet::new()));
labels.1.insert(label);
}

Expand Down Expand Up @@ -1395,7 +1379,13 @@ 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(&LabeledPointVar {
name: label.clone(),
value: point.clone(),
})
.unwrap();

comms_to_combine.push(commitment_lc.1.clone());
values_to_combine.push(v_i.clone());
Expand Down Expand Up @@ -1669,28 +1659,22 @@ where
<CycleE::E1 as PairingEngine>::Fr,
>,
proofs: &[Self::ProofVar],
opening_challenges: &[NonNativeFieldVar<
<CycleE::E2 as PairingEngine>::Fr,
<CycleE::E1 as PairingEngine>::Fr,
>],
opening_challenges_bits: &[Vec<Boolean<<CycleE::E1 as PairingEngine>::Fr>>],
batching_rands: &[NonNativeFieldVar<
rand_data: &PCCheckRandomDataVar<
<CycleE::E2 as PairingEngine>::Fr,
<CycleE::E1 as PairingEngine>::Fr,
>],
batching_rands_bits: &[Vec<Boolean<<CycleE::E1 as PairingEngine>::Fr>>],
>,
) -> R1CSResult<Boolean<<CycleE::E1 as PairingEngine>::Fr>> {
let mut batching_rands = batching_rands.to_vec();
let mut batching_rands_bits = batching_rands_bits.to_vec();
let mut batching_rands = rand_data.batching_rands.to_vec();
let mut batching_rands_bits = rand_data.batching_rands_bits.to_vec();

let commitments: BTreeMap<_, _> =
commitments.iter().map(|c| (c.label.clone(), c)).collect();
let mut query_to_labels_map = BTreeMap::new();

for (label, (point_label, point)) in query_set.0.iter() {
for (label, point) in query_set.0.iter() {
let labels = query_to_labels_map
.entry((*point_label).clone())
.or_insert((point, BTreeSet::new()));
.entry(point.name.clone())
.or_insert((point.value.clone(), BTreeSet::new()));
labels.1.insert(label);
}

Expand All @@ -1709,7 +1693,13 @@ where
commitment.commitment.shifted_comm.is_some()
);

let v_i = evaluations.0.get(&(label.clone(), point.clone())).unwrap();
let v_i = evaluations
.0
.get(&LabeledPointVar {
name: label.clone(),
value: point.clone(),
})
.unwrap();

comms_to_combine.push(commitment.clone());
values_to_combine.push(v_i.clone());
Expand All @@ -1727,8 +1717,9 @@ where
for (labeled_commitment, value) in
comms_to_combine.into_iter().zip(values_to_combine.iter())
{
let challenge = opening_challenges[opening_challenges_counter].clone();
let challenge_bits = opening_challenges_bits[opening_challenges_counter].clone();
let challenge = rand_data.opening_challenges[opening_challenges_counter].clone();
let challenge_bits =
rand_data.opening_challenges_bits[opening_challenges_counter].clone();
opening_challenges_counter += 1;

let LabeledCommitmentVar {
Expand All @@ -1750,7 +1741,7 @@ where
// v_i is the evaluation, in the cocmbined commitment,
if let Some(degree_bound) = degree_bound {
let challenge_shifted_bits =
opening_challenges_bits[opening_challenges_counter].clone();
rand_data.opening_challenges_bits[opening_challenges_counter].clone();
opening_challenges_counter += 1;

let shifted_comm = shifted_comm.as_ref().unwrap().clone();
Expand Down Expand Up @@ -1873,16 +1864,10 @@ where
<CycleE::E1 as PairingEngine>::Fr,
>,
proof: &Self::BatchLCProofVar,
opening_challenges: &[NonNativeFieldVar<
<CycleE::E2 as PairingEngine>::Fr,
<CycleE::E1 as PairingEngine>::Fr,
>],
opening_challenges_bits: &[Vec<Boolean<<CycleE::E1 as PairingEngine>::Fr>>],
batching_rands: &[NonNativeFieldVar<
rand_data: &PCCheckRandomDataVar<
<CycleE::E2 as PairingEngine>::Fr,
<CycleE::E1 as PairingEngine>::Fr,
>],
batching_rands_bits: &[Vec<Boolean<<CycleE::E1 as PairingEngine>::Fr>>],
>,
) -> R1CSResult<Boolean<<CycleE::E1 as PairingEngine>::Fr>> {
let BatchLCProofVar { proofs, .. } = proof;

Expand All @@ -1903,35 +1888,48 @@ where

let mut coeffs_and_comms = Vec::new();

for (coeff, label, negate) in lc.terms.iter() {
for (coeff, label) in lc.terms.iter() {
if label.is_one() {
assert!(coeff.is_some());

let coeff = coeff.clone().unwrap();

for (&(ref label, _), ref mut eval) in evaluations.0.iter_mut() {
if label == &lc_label {
if negate.eq(&true) {
**eval = (**eval).clone() + &coeff;
} else {
**eval = (**eval).clone() - &coeff;
}
for (label, ref mut eval) in evaluations.0.iter_mut() {
if label.name == lc_label {
match coeff.clone() {
LinearCombinationCoeffVar::One => {
**eval = (**eval).clone() - &NonNativeFieldVar::one()
}
LinearCombinationCoeffVar::MinusOne => {
**eval = (**eval).clone() + &NonNativeFieldVar::one()
}
LinearCombinationCoeffVar::Var(variable) => {
**eval = (**eval).clone() - &variable
}
};
}
}
} else {
let label: &String = label.try_into().unwrap();
let &cur_comm = label_comm_map.get(label).unwrap();
let negate = match coeff {
LinearCombinationCoeffVar::One | LinearCombinationCoeffVar::Var(_) => false,
LinearCombinationCoeffVar::MinusOne => true,
};

if num_polys == 1 && cur_comm.degree_bound.is_some() {
assert!(coeff.is_none());
assert!(negate.eq(&false));
assert!(!negate);
}

let coeff = match coeff {
LinearCombinationCoeffVar::One => Some(NonNativeFieldVar::one()),
LinearCombinationCoeffVar::MinusOne => {
Some(NonNativeFieldVar::zero() - NonNativeFieldVar::one())
}
LinearCombinationCoeffVar::Var(variable) => Some(variable.clone()),
};

coeffs_and_comms.push((
coeff.clone(),
cur_comm.degree_bound.clone(),
cur_comm.prepared_commitment.clone(),
*negate,
negate,
));
}
}
Expand All @@ -1942,18 +1940,18 @@ where
Self::prepared_batch_check_evaluations(
cs,
prepared_verification_key,
&lc_info,
lc_info.as_slice(),
&query_set,
&evaluations,
proofs,
opening_challenges,
opening_challenges_bits,
batching_rands,
batching_rands_bits,
&rand_data.opening_challenges,
&rand_data.opening_challenges_bits,
&rand_data.batching_rands,
&rand_data.batching_rands_bits,
)
}

fn create_labeled_commitment_gadget(
fn create_labeled_commitment(
label: String,
commitment: Self::CommitmentVar,
degree_bound: Option<FpVar<<CycleE::E1 as PairingEngine>::Fr>>,
Expand All @@ -1965,7 +1963,7 @@ where
}
}

fn create_prepared_labeled_commitment_gadget(
fn create_prepared_labeled_commitment(
label: String,
prepared_commitment: Self::PreparedCommitmentVar,
degree_bound: Option<FpVar<<CycleE::E1 as PairingEngine>::Fr>>,
Expand Down
4 changes: 2 additions & 2 deletions src/marlin_pc/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl<E: PairingEngine> CommitterKey<E> {
/// Obtain powers for the underlying KZG10 construction
pub fn powers<'a>(&self) -> kzg10::Powers<'a, E> {
kzg10::Powers {
powers_of_g: self.powers.as_slice().into(),
powers_of_gamma_g: self.powers_of_gamma_g.as_slice().into(),
powers_of_g: ark_std::borrow::Cow::Owned(self.powers.clone()),
powers_of_gamma_g: ark_std::borrow::Cow::Owned(self.powers_of_gamma_g.clone()),
}
}

Expand Down