mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-25 03:04:13 +03:00
Merge pull request #759 from ljedrz/simplify_circuit_synthesizer
introduce a helper ConstraintSet object
This commit is contained in:
commit
88db9455c5
@ -122,9 +122,7 @@ impl Command for Build {
|
||||
// Generate the program on the constraint system and verify correctness
|
||||
{
|
||||
let mut cs = CircuitSynthesizer::<Bls12_377> {
|
||||
at: Default::default(),
|
||||
bt: Default::default(),
|
||||
ct: Default::default(),
|
||||
constraints: Default::default(),
|
||||
public_variables: Default::default(),
|
||||
private_variables: Default::default(),
|
||||
namespaces: Default::default(),
|
||||
|
@ -25,11 +25,25 @@ pub struct Namespace {
|
||||
private_var_indices: Vec<usize>,
|
||||
}
|
||||
|
||||
pub struct ConstraintSet<E: PairingEngine> {
|
||||
pub at: Vec<(E::Fr, Index)>,
|
||||
pub bt: Vec<(E::Fr, Index)>,
|
||||
pub ct: Vec<(E::Fr, Index)>,
|
||||
}
|
||||
|
||||
impl<E: PairingEngine> Default for ConstraintSet<E> {
|
||||
fn default() -> Self {
|
||||
ConstraintSet {
|
||||
at: Default::default(),
|
||||
bt: Default::default(),
|
||||
ct: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CircuitSynthesizer<E: PairingEngine> {
|
||||
// Constraints
|
||||
pub at: OptionalVec<Vec<(E::Fr, Index)>>,
|
||||
pub bt: OptionalVec<Vec<(E::Fr, Index)>>,
|
||||
pub ct: OptionalVec<Vec<(E::Fr, Index)>>,
|
||||
pub constraints: OptionalVec<ConstraintSet<E>>,
|
||||
|
||||
// Assignments of variables
|
||||
pub public_variables: OptionalVec<E::Fr>,
|
||||
@ -79,13 +93,11 @@ impl<E: PairingEngine> ConstraintSystem<E::Fr> for CircuitSynthesizer<E> {
|
||||
LB: FnOnce(LinearCombination<E::Fr>) -> LinearCombination<E::Fr>,
|
||||
LC: FnOnce(LinearCombination<E::Fr>) -> LinearCombination<E::Fr>,
|
||||
{
|
||||
let index = self.at.insert(Vec::new());
|
||||
self.bt.insert(Vec::new());
|
||||
self.ct.insert(Vec::new());
|
||||
let index = self.constraints.insert(Default::default());
|
||||
|
||||
push_constraints(a(LinearCombination::zero()), &mut self.at, index);
|
||||
push_constraints(b(LinearCombination::zero()), &mut self.bt, index);
|
||||
push_constraints(c(LinearCombination::zero()), &mut self.ct, index);
|
||||
push_constraints(a(LinearCombination::zero()), &mut self.constraints[index].at);
|
||||
push_constraints(b(LinearCombination::zero()), &mut self.constraints[index].bt);
|
||||
push_constraints(c(LinearCombination::zero()), &mut self.constraints[index].ct);
|
||||
|
||||
if let Some(ref mut ns) = self.namespaces.last_mut() {
|
||||
ns.constraint_indices.push(index);
|
||||
@ -103,9 +115,7 @@ impl<E: PairingEngine> ConstraintSystem<E::Fr> for CircuitSynthesizer<E> {
|
||||
fn pop_namespace(&mut self) {
|
||||
if let Some(ns) = self.namespaces.pop() {
|
||||
for idx in ns.constraint_indices {
|
||||
self.at.remove(idx);
|
||||
self.bt.remove(idx);
|
||||
self.ct.remove(idx);
|
||||
self.constraints.remove(idx);
|
||||
}
|
||||
|
||||
for idx in ns.private_var_indices {
|
||||
@ -123,7 +133,7 @@ impl<E: PairingEngine> ConstraintSystem<E::Fr> for CircuitSynthesizer<E> {
|
||||
}
|
||||
|
||||
fn num_constraints(&self) -> usize {
|
||||
self.at.len()
|
||||
self.constraints.len()
|
||||
}
|
||||
|
||||
fn num_public_variables(&self) -> usize {
|
||||
@ -135,15 +145,11 @@ impl<E: PairingEngine> ConstraintSystem<E::Fr> for CircuitSynthesizer<E> {
|
||||
}
|
||||
}
|
||||
|
||||
fn push_constraints<F: Field>(
|
||||
l: LinearCombination<F>,
|
||||
constraints: &mut OptionalVec<Vec<(F, Index)>>,
|
||||
this_constraint: usize,
|
||||
) {
|
||||
fn push_constraints<F: Field>(l: LinearCombination<F>, constraint: &mut Vec<(F, Index)>) {
|
||||
for (var, coeff) in l.as_ref() {
|
||||
match var.get_unchecked() {
|
||||
Index::Public(i) => constraints[this_constraint].push((*coeff, Index::Public(i))),
|
||||
Index::Private(i) => constraints[this_constraint].push((*coeff, Index::Private(i))),
|
||||
Index::Public(i) => constraint.push((*coeff, Index::Public(i))),
|
||||
Index::Private(i) => constraint.push((*coeff, Index::Private(i))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ use snarkvm_curves::{bls12_377::Bls12_377, traits::PairingEngine};
|
||||
use snarkvm_fields::FieldError;
|
||||
use snarkvm_r1cs::{ConstraintSystem, Index, OptionalVec};
|
||||
|
||||
use crate::{CircuitSynthesizer, SerializedField, SerializedIndex};
|
||||
use crate::{CircuitSynthesizer, ConstraintSet, SerializedField, SerializedIndex};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct SerializedCircuit {
|
||||
@ -86,17 +86,17 @@ impl<E: PairingEngine> From<CircuitSynthesizer<E>> for SerializedCircuit {
|
||||
for i in 0..num_constraints {
|
||||
// Serialize at[i]
|
||||
|
||||
let a_constraints = get_serialized_constraints::<E>(&synthesizer.at[i]);
|
||||
let a_constraints = get_serialized_constraints::<E>(&synthesizer.constraints[i].at);
|
||||
at.push(a_constraints);
|
||||
|
||||
// Serialize bt[i]
|
||||
|
||||
let b_constraints = get_serialized_constraints::<E>(&synthesizer.bt[i]);
|
||||
let b_constraints = get_serialized_constraints::<E>(&synthesizer.constraints[i].bt);
|
||||
bt.push(b_constraints);
|
||||
|
||||
// Serialize ct[i]
|
||||
|
||||
let c_constraints = get_serialized_constraints::<E>(&synthesizer.ct[i]);
|
||||
let c_constraints = get_serialized_constraints::<E>(&synthesizer.constraints[i].ct);
|
||||
ct.push(c_constraints);
|
||||
}
|
||||
|
||||
@ -151,31 +151,27 @@ impl TryFrom<SerializedCircuit> for CircuitSynthesizer<Bls12_377> {
|
||||
Ok(deserialized)
|
||||
}
|
||||
|
||||
let mut at = OptionalVec::with_capacity(serialized.num_constraints);
|
||||
let mut bt = OptionalVec::with_capacity(serialized.num_constraints);
|
||||
let mut ct = OptionalVec::with_capacity(serialized.num_constraints);
|
||||
let mut constraints = OptionalVec::with_capacity(serialized.num_constraints);
|
||||
|
||||
for i in 0..serialized.num_constraints {
|
||||
// Deserialize at[i]
|
||||
|
||||
let a_constraints = get_deserialized_constraints(&serialized.at[i])?;
|
||||
at.insert(a_constraints);
|
||||
|
||||
// Deserialize bt[i]
|
||||
|
||||
let b_constraints = get_deserialized_constraints(&serialized.bt[i])?;
|
||||
bt.insert(b_constraints);
|
||||
|
||||
// Deserialize ct[i]
|
||||
|
||||
let c_constraints = get_deserialized_constraints(&serialized.ct[i])?;
|
||||
ct.insert(c_constraints);
|
||||
|
||||
constraints.insert(ConstraintSet {
|
||||
at: a_constraints,
|
||||
bt: b_constraints,
|
||||
ct: c_constraints,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(CircuitSynthesizer::<Bls12_377> {
|
||||
at,
|
||||
bt,
|
||||
ct,
|
||||
constraints,
|
||||
public_variables,
|
||||
private_variables,
|
||||
namespaces: Default::default(),
|
||||
|
Loading…
Reference in New Issue
Block a user