Skip to content
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

Multi circuit proof integration #1510

Closed
wants to merge 12 commits into from
25 changes: 9 additions & 16 deletions algorithms/benches/snark/marlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use snarkvm_utilities::{CanonicalDeserialize, CanonicalSerialize, TestRng};

use criterion::Criterion;

use itertools::Itertools;
use std::collections::BTreeMap;

type MarlinInst = MarlinSNARK<Bls12_377, FS, MarlinHidingMode>;
Expand Down Expand Up @@ -74,8 +73,11 @@ fn snark_prove(c: &mut Criterion) {

let (circuit, _) = TestCircuit::gen_rand(mul_depth, num_constraints, num_variables, rng);

let params = MarlinInst::circuit_setup(&universal_srs, &circuit).unwrap();
b.iter(|| MarlinInst::prove(&fs_parameters, &params.0, &circuit, rng).unwrap())
let (pk, _) = MarlinInst::circuit_setup(&universal_srs, &circuit).unwrap();
let mut keys_to_constraints = BTreeMap::new();
let constraints = [circuit];
keys_to_constraints.insert(&pk, &constraints[..]);
b.iter(move || MarlinInst::prove_batch(&fs_parameters, &keys_to_constraints, rng).unwrap())
});
}

Expand Down Expand Up @@ -110,14 +112,10 @@ fn snark_batch_prove(c: &mut Criterion) {
pks.push(pk);
all_circuits.push(circuits);
}
// We need to create references to the circuits we just created
let all_circuit_refs = (0..circuit_batch_size)
.map(|i| (0..instance_batch_size).map(|j| &all_circuits[i][j]).collect_vec())
.collect_vec();

for i in 0..circuit_batch_size {
keys_to_constraints.insert(&pks[i], all_circuit_refs[i].as_slice());
keys_to_constraints.insert(&pks[i], all_circuits[i].as_slice());
}

b.iter(|| MarlinInst::prove_batch(&fs_parameters, &keys_to_constraints, rng).unwrap())
});
}
Expand All @@ -137,7 +135,7 @@ fn snark_verify(c: &mut Criterion) {

let (pk, vk) = MarlinInst::circuit_setup(&universal_srs, &circuit).unwrap();

let proof = MarlinInst::prove(&fs_parameters, &pk, &circuit, rng).unwrap();
let proof = MarlinInst::prove(&fs_parameters, &pk, circuit, rng).unwrap();
b.iter(|| {
let verification = MarlinInst::verify(&fs_parameters, &vk, public_inputs.as_slice(), &proof).unwrap();
assert!(verification);
Expand Down Expand Up @@ -181,13 +179,8 @@ fn snark_batch_verify(c: &mut Criterion) {
all_circuits.push(circuits);
all_inputs.push(inputs);
}
// We need to create references to the circuits and inputs we just created
let all_circuit_refs = (0..circuit_batch_size)
.map(|i| (0..instance_batch_size).map(|j| &all_circuits[i][j]).collect_vec())
.collect_vec();

for i in 0..circuit_batch_size {
keys_to_constraints.insert(&pks[i], all_circuit_refs[i].as_slice());
keys_to_constraints.insert(&pks[i], all_circuits[i].as_slice());
keys_to_inputs.insert(&vks[i], all_inputs[i].as_slice());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod third;
impl<F: PrimeField, MM: MarlinMode> AHPForR1CS<F, MM> {
/// Initialize the AHP prover.
pub fn init_prover<'a, C: ConstraintSynthesizer<F>>(
circuits_to_constraints: &BTreeMap<&'a Circuit<F, MM>, &[&C]>,
circuits_to_constraints: &BTreeMap<&'a Circuit<F, MM>, &[C]>,
) -> Result<prover::State<'a, F, MM>, AHPError> {
let init_time = start_timer!(|| "AHP::Prover::Init");

Expand Down
4 changes: 2 additions & 2 deletions algorithms/src/snark/marlin/marlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ where
/// https://github.com/AleoHQ/protocol-docs/tree/main/marlin
fn prove_batch_with_terminator<C: ConstraintSynthesizer<E::Fr>, R: Rng + CryptoRng>(
fs_parameters: &Self::FSParameters,
keys_to_constraints: &BTreeMap<&CircuitProvingKey<E, MM>, &[&C]>,
keys_to_constraints: &BTreeMap<&CircuitProvingKey<E, MM>, &[C]>,
terminator: &AtomicBool,
zk_rng: &mut R,
) -> Result<Self::Proof, SNARKError> {
Expand Down Expand Up @@ -966,7 +966,7 @@ pub mod test {
// Test native proof and verification.
let fs_parameters = FS::sample_parameters();

let proof = TestSNARK::prove(&fs_parameters, &pk, &circ, &mut rng).unwrap();
let proof = TestSNARK::prove(&fs_parameters, &pk, circ, &mut rng).unwrap();

assert!(
TestSNARK::verify(&fs_parameters, &vk.clone(), public_inputs.as_slice(), &proof).unwrap(),
Expand Down
30 changes: 9 additions & 21 deletions algorithms/src/snark/marlin/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod marlin {
let certificate = $marlin_inst::prove_vk(&fs_parameters, &index_vk, &index_pk).unwrap();
assert!($marlin_inst::verify_vk(&fs_parameters, &circ, &index_vk, &certificate).unwrap());

let proof = $marlin_inst::prove(&fs_parameters, &index_pk, &circ, rng).unwrap();
let proof = $marlin_inst::prove(&fs_parameters, &index_pk, circ, rng).unwrap();
println!("Called prover");

assert!($marlin_inst::verify(&fs_parameters, &index_vk, public_inputs, &proof).unwrap());
Expand Down Expand Up @@ -88,20 +88,12 @@ mod marlin {

let mut pks_to_constraints = BTreeMap::new();
let mut vks_to_inputs = BTreeMap::new();
let mut constraint_refs = Vec::with_capacity(index_keys.len());
for (index_pk, index_vk) in index_keys.iter() {
let circuit_constraints = &constraints[&index_pk.circuit.id];
let mut circuit_constraint_refs = Vec::with_capacity(circuit_constraints.len());
for constraint in circuit_constraints.iter() {
circuit_constraint_refs.push(constraint)
}
constraint_refs.push(circuit_constraint_refs);
pks_to_constraints.insert(index_pk, circuit_constraints.as_slice());
let circuit_inputs = &inputs[&index_pk.circuit.id];
vks_to_inputs.insert(index_vk, circuit_inputs.as_slice());
}
for (i, (index_pk, _)) in index_keys.iter().enumerate() {
pks_to_constraints.insert(index_pk, constraint_refs[i].as_slice());
}

let proof =
$marlin_inst::prove_batch(&fs_parameters, &pks_to_constraints, rng).unwrap();
Expand Down Expand Up @@ -304,7 +296,7 @@ mod marlin_hiding {
let (index_pk, index_vk) = MarlinInst::circuit_setup(&universal_srs, &circuit).unwrap();
println!("Called circuit setup");

let proof = MarlinInst::prove(&fs_parameters, &index_pk, &circuit, rng).unwrap();
let proof = MarlinInst::prove(&fs_parameters, &index_pk, circuit, rng).unwrap();
println!("Called prover");

assert!(MarlinInst::verify(&fs_parameters, &index_vk, public_inputs, &proof).unwrap());
Expand Down Expand Up @@ -434,13 +426,12 @@ mod marlin_hiding {
let fs_parameters = FS::sample_parameters();

let (index_pk, index_vk) = MarlinInst::circuit_setup(&universal_srs, &circuit).unwrap();
println!("Called circuit setup");

let proof = MarlinInst::prove(&fs_parameters, &index_pk, &circuit, rng).unwrap();
println!("Called prover");

universal_srs.download_powers_for(0..2usize.pow(18)).unwrap();
let (new_pk, new_vk) = MarlinInst::circuit_setup(&universal_srs, &circuit).unwrap();

let proof = MarlinInst::prove(&fs_parameters, &index_pk, circuit, rng).unwrap();

assert_eq!(index_pk, new_pk);
assert_eq!(index_vk, new_vk);
assert!(MarlinInst::verify(&fs_parameters, &index_vk, public_inputs.clone(), &proof).unwrap());
Expand All @@ -461,10 +452,8 @@ mod marlin_hiding {
let num_variables = 2usize.pow(15) - 10;
let (circuit1, public_inputs1) = TestCircuit::gen_rand(mul_depth, num_constraints, num_variables, rng);
let (pk1, vk1) = MarlinInst::circuit_setup(&universal_srs, &circuit1).unwrap();
println!("Called circuit setup");

let proof1 = MarlinInst::prove(&fs_parameters, &pk1, &circuit1, rng).unwrap();
println!("Called prover");
let proof1 = MarlinInst::prove(&fs_parameters, &pk1, circuit1, rng).unwrap();
assert!(MarlinInst::verify(&fs_parameters, &vk1, public_inputs1.clone(), &proof1).unwrap());

/*****************************************************************************/
Expand All @@ -475,10 +464,9 @@ mod marlin_hiding {
let num_variables = 2usize.pow(19) - 10;
let (circuit2, public_inputs2) = TestCircuit::gen_rand(mul_depth, num_constraints, num_variables, rng);
let (pk2, vk2) = MarlinInst::circuit_setup(&universal_srs, &circuit2).unwrap();
println!("Called circuit setup");

let proof2 = MarlinInst::prove(&fs_parameters, &pk2, &circuit2, rng).unwrap();
println!("Called prover");
let proof2 = MarlinInst::prove(&fs_parameters, &pk2, circuit2, rng).unwrap();

assert!(MarlinInst::verify(&fs_parameters, &vk2, public_inputs2, &proof2).unwrap());
/*****************************************************************************/
assert!(MarlinInst::verify(&fs_parameters, &vk1, public_inputs1, &proof1).unwrap());
Expand Down
19 changes: 3 additions & 16 deletions algorithms/src/traits/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub trait SNARK {

fn prove_batch<C: ConstraintSynthesizer<Self::ScalarField>, R: Rng + CryptoRng>(
fs_parameters: &Self::FSParameters,
keys_to_constraints: &BTreeMap<&Self::ProvingKey, &[&C]>,
keys_to_constraints: &BTreeMap<&Self::ProvingKey, &[C]>,
rng: &mut R,
) -> Result<Self::Proof, SNARKError> {
Self::prove_batch_with_terminator(fs_parameters, keys_to_constraints, &AtomicBool::new(false), rng)
Expand All @@ -104,7 +104,7 @@ pub trait SNARK {
fn prove<C: ConstraintSynthesizer<Self::ScalarField>, R: Rng + CryptoRng>(
fs_parameters: &Self::FSParameters,
proving_key: &Self::ProvingKey,
constraints: &C,
constraints: C,
rng: &mut R,
) -> Result<Self::Proof, SNARKError> {
let mut keys_to_constraints = BTreeMap::new();
Expand All @@ -115,24 +115,11 @@ pub trait SNARK {

fn prove_batch_with_terminator<C: ConstraintSynthesizer<Self::ScalarField>, R: Rng + CryptoRng>(
fs_parameters: &Self::FSParameters,
keys_to_constraints: &BTreeMap<&Self::ProvingKey, &[&C]>,
keys_to_constraints: &BTreeMap<&Self::ProvingKey, &[C]>,
terminator: &AtomicBool,
rng: &mut R,
) -> Result<Self::Proof, SNARKError>;

fn prove_with_terminator<C: ConstraintSynthesizer<Self::ScalarField>, R: Rng + CryptoRng>(
fs_parameters: &Self::FSParameters,
proving_key: &Self::ProvingKey,
constraints: &C,
terminator: &AtomicBool,
rng: &mut R,
) -> Result<Self::Proof, SNARKError> {
let mut keys_to_constraints = BTreeMap::new();
let constraints = [constraints];
keys_to_constraints.insert(proving_key, &constraints[..]);
Self::prove_batch_with_terminator(fs_parameters, &keys_to_constraints, terminator, rng)
}

fn verify_vk<C: ConstraintSynthesizer<Self::ScalarField>>(
fs_parameters: &Self::FSParameters,
circuit: &C,
Expand Down
2 changes: 1 addition & 1 deletion circuit/environment/src/helpers/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ mod tests {
let (index_pk, index_vk) = MarlinInst::circuit_setup(&universal_srs, &assignment).unwrap();
println!("Called circuit setup");

let proof = MarlinInst::prove(&fs_pp, &index_pk, &assignment, rng).unwrap();
let proof = MarlinInst::prove(&fs_pp, &index_pk, assignment, rng).unwrap();
println!("Called prover");

let one = <Circuit as Environment>::BaseField::one();
Expand Down
2 changes: 1 addition & 1 deletion circuit/environment/src/helpers/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ mod tests {
let (index_pk, index_vk) = MarlinInst::circuit_setup(&universal_srs, &Circuit).unwrap();
println!("Called circuit setup");

let proof = MarlinInst::prove(&fs_pp, &index_pk, &Circuit, rng).unwrap();
let proof = MarlinInst::prove(&fs_pp, &index_pk, Circuit, rng).unwrap();
println!("Called prover");

assert!(MarlinInst::verify(&fs_pp, &index_vk, [*one, *one], &proof).unwrap());
Expand Down
14 changes: 14 additions & 0 deletions console/program/src/data/identifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ impl<N: Network> TryFrom<&str> for Identifier<N> {
}
}

impl<N: Network> Ord for Identifier<N> {
/// Ordering is determined by the field element.
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}

impl<N: Network> PartialOrd for Identifier<N> {
/// Ordering is determined by the field element.
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

#[cfg(test)]
pub(crate) mod tests {
use super::*;
Expand Down
20 changes: 10 additions & 10 deletions console/program/src/id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ impl<N: Network> Ord for ProgramID<N> {
/// Ordering is determined by the network first, then the program name second.
fn cmp(&self, other: &Self) -> Ordering {
match self.network == other.network {
true => self.name.to_string().cmp(&other.name.to_string()),
false => self.network.to_string().cmp(&other.network.to_string()),
true => self.name.cmp(&other.name),
false => self.network.cmp(&other.network),
}
}
}
Expand Down Expand Up @@ -135,23 +135,23 @@ mod tests {
let import4 = ProgramID::<CurrentNetwork>::from_str("foo.aleo")?;

assert_eq!(import1.partial_cmp(&import1), Some(Ordering::Equal));
assert_eq!(import1.partial_cmp(&import2), Some(Ordering::Less));
assert_eq!(import1.partial_cmp(&import2), Some(Ordering::Greater));
assert_eq!(import1.partial_cmp(&import3), Some(Ordering::Equal));
assert_eq!(import1.partial_cmp(&import4), Some(Ordering::Less));
assert_eq!(import1.partial_cmp(&import4), Some(Ordering::Greater));

assert_eq!(import2.partial_cmp(&import1), Some(Ordering::Greater));
assert_eq!(import2.partial_cmp(&import1), Some(Ordering::Less));
assert_eq!(import2.partial_cmp(&import2), Some(Ordering::Equal));
assert_eq!(import2.partial_cmp(&import3), Some(Ordering::Greater));
assert_eq!(import2.partial_cmp(&import3), Some(Ordering::Less));
assert_eq!(import2.partial_cmp(&import4), Some(Ordering::Equal));

assert_eq!(import3.partial_cmp(&import1), Some(Ordering::Equal));
assert_eq!(import3.partial_cmp(&import2), Some(Ordering::Less));
assert_eq!(import3.partial_cmp(&import2), Some(Ordering::Greater));
assert_eq!(import3.partial_cmp(&import3), Some(Ordering::Equal));
assert_eq!(import3.partial_cmp(&import4), Some(Ordering::Less));
assert_eq!(import3.partial_cmp(&import4), Some(Ordering::Greater));

assert_eq!(import4.partial_cmp(&import1), Some(Ordering::Greater));
assert_eq!(import4.partial_cmp(&import1), Some(Ordering::Less));
assert_eq!(import4.partial_cmp(&import2), Some(Ordering::Equal));
assert_eq!(import4.partial_cmp(&import3), Some(Ordering::Greater));
assert_eq!(import4.partial_cmp(&import3), Some(Ordering::Less));
assert_eq!(import4.partial_cmp(&import4), Some(Ordering::Equal));

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion parameters/examples/inclusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn inclusion<N: Network, A: Aleo<Network = N>>() -> Result<()> {
let (proving_key, verifying_key) = universal_srs.to_circuit_key(inclusion_function_name, &assignment)?;

// Ensure the proving key and verifying keys are valid.
let proof = proving_key.prove(inclusion_function_name, &assignment, &mut thread_rng())?;
let proof = proving_key.prove(inclusion_function_name, assignment, &mut thread_rng())?;
assert!(verifying_key.verify(
inclusion_function_name,
&[N::Field::one(), **state_path.global_state_root(), *Field::<N>::zero(), *serial_number],
Expand Down
2 changes: 1 addition & 1 deletion synthesizer/coinbase/src/helpers/puzzle_commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::*;
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct PuzzleCommitment<N: Network> {
/// The commitment for the solution.
commitment: KZGCommitment<<N as Environment>::PairingCurve>,
commitment: KZGCommitment<N::PairingCurve>,
}

impl<N: Network> PuzzleCommitment<N> {
Expand Down
Loading