Update synthesizer to support new terminology

This commit is contained in:
howardwu 2021-02-27 13:21:29 -08:00
parent 8a05594b42
commit 2bfb64f83e
6 changed files with 48 additions and 40 deletions

View File

@ -23,7 +23,7 @@ use snarkvm_errors::gadgets::SynthesisError;
use snarkvm_models::{
curves::PrimeField,
gadgets::{
curves::{FieldGadget, FpGadget},
curves::{AllocatedFp, FieldGadget, FpGadget},
r1cs::ConstraintSystem,
utilities::{
alloc::AllocGadget,
@ -187,7 +187,7 @@ impl<F: PrimeField> FieldType<F> {
match self {
FieldType::Constant(constant) => FpGadget::alloc(&mut cs.ns(|| format!("{:?}", constant)), || Ok(constant)),
FieldType::Allocated(allocated) => FpGadget::alloc(&mut cs.ns(|| format!("{:?}", allocated)), || {
allocated.value.ok_or(SynthesisError::AssignmentMissing)
allocated.get_value().ok_or(SynthesisError::AssignmentMissing)
}),
}
}
@ -273,7 +273,7 @@ impl<F: PrimeField> ConditionalEqGadget<F> for FieldType<F> {
// c - a = a - c
(FieldType::Constant(constant_value), FieldType::Allocated(allocated_value))
| (FieldType::Allocated(allocated_value), FieldType::Constant(constant_value)) => {
let constant_gadget = FpGadget::from(&mut cs, constant_value);
let constant_gadget = FpGadget::from(AllocatedFp::from(&mut cs, constant_value));
constant_gadget.conditional_enforce_equal(cs, allocated_value, condition)
}

View File

@ -26,7 +26,7 @@ use snarkvm_gadgets::curves::edwards_bls12::EdwardsBlsGadget;
use snarkvm_models::{
curves::{AffineCurve, Fp256, One, TEModelParameters, Zero},
gadgets::{
curves::{FieldGadget, FpGadget, GroupGadget},
curves::{AllocatedFp, FieldGadget, FpGadget, GroupGadget},
r1cs::ConstraintSystem,
utilities::{
alloc::AllocGadget,
@ -459,8 +459,8 @@ impl ConditionalEqGadget<Fq> for EdwardsGroupType {
// c - a = a - c
(EdwardsGroupType::Constant(constant_value), EdwardsGroupType::Allocated(allocated_value))
| (EdwardsGroupType::Allocated(allocated_value), EdwardsGroupType::Constant(constant_value)) => {
let x = FpGadget::from(&mut cs, &constant_value.x);
let y = FpGadget::from(&mut cs, &constant_value.y);
let x = FpGadget::from(AllocatedFp::from(&mut cs, &constant_value.x));
let y = FpGadget::from(AllocatedFp::from(&mut cs, &constant_value.y));
let constant_gadget = EdwardsBlsGadget::new(x, y);
constant_gadget.conditional_enforce_equal(cs, allocated_value, condition)

View File

@ -125,8 +125,8 @@ impl Command for Build {
at: vec![],
bt: vec![],
ct: vec![],
input_assignment: vec![],
aux_assignment: vec![],
public_variables: vec![],
private_variables: vec![],
};
let temporary_program = program.clone();
let output = temporary_program.compile_constraints(&mut cs)?;

View File

@ -27,8 +27,8 @@ pub struct CircuitSynthesizer<E: PairingEngine> {
pub ct: Vec<Vec<(E::Fr, Index)>>,
// Assignments of variables
pub input_assignment: Vec<E::Fr>,
pub aux_assignment: Vec<E::Fr>,
pub public_variables: Vec<E::Fr>,
pub private_variables: Vec<E::Fr>,
}
impl<E: PairingEngine> ConstraintSystem<E::Fr> for CircuitSynthesizer<E> {
@ -41,9 +41,9 @@ impl<E: PairingEngine> ConstraintSystem<E::Fr> for CircuitSynthesizer<E> {
A: FnOnce() -> AR,
AR: AsRef<str>,
{
let index = self.aux_assignment.len();
self.aux_assignment.push(f()?);
Ok(Variable::new_unchecked(Index::Aux(index)))
let index = self.private_variables.len();
self.private_variables.push(f()?);
Ok(Variable::new_unchecked(Index::Private(index)))
}
#[inline]
@ -53,9 +53,9 @@ impl<E: PairingEngine> ConstraintSystem<E::Fr> for CircuitSynthesizer<E> {
A: FnOnce() -> AR,
AR: AsRef<str>,
{
let index = self.input_assignment.len();
self.input_assignment.push(f()?);
Ok(Variable::new_unchecked(Index::Input(index)))
let index = self.public_variables.len();
self.public_variables.push(f()?);
Ok(Variable::new_unchecked(Index::Public(index)))
}
#[inline]
@ -99,13 +99,21 @@ impl<E: PairingEngine> ConstraintSystem<E::Fr> for CircuitSynthesizer<E> {
fn num_constraints(&self) -> usize {
self.at.len()
}
fn num_public_variables(&self) -> usize {
self.public_variables.len()
}
fn num_private_variables(&self) -> usize {
self.private_variables.len()
}
}
fn push_constraints<F: Field>(l: LinearCombination<F>, constraints: &mut [Vec<(F, Index)>], this_constraint: usize) {
for (var, coeff) in l.as_ref() {
match var.get_unchecked() {
Index::Input(i) => constraints[this_constraint].push((*coeff, Index::Input(i))),
Index::Aux(i) => constraints[this_constraint].push((*coeff, Index::Aux(i))),
Index::Public(i) => constraints[this_constraint].push((*coeff, Index::Public(i))),
Index::Private(i) => constraints[this_constraint].push((*coeff, Index::Private(i))),
}
}
}

View File

@ -28,12 +28,12 @@ use crate::{CircuitSynthesizer, SerializedField, SerializedIndex};
#[derive(Serialize, Deserialize)]
pub struct SerializedCircuit {
pub num_inputs: usize,
pub num_aux: usize,
pub num_public_variables: usize,
pub num_private_variables: usize,
pub num_constraints: usize,
pub input_assignment: Vec<SerializedField>,
pub aux_assignment: Vec<SerializedField>,
pub public_variables: Vec<SerializedField>,
pub private_variables: Vec<SerializedField>,
pub at: Vec<Vec<(SerializedField, SerializedIndex)>>,
pub bt: Vec<Vec<(SerializedField, SerializedIndex)>>,
@ -52,8 +52,8 @@ impl SerializedCircuit {
impl<E: PairingEngine> From<CircuitSynthesizer<E>> for SerializedCircuit {
fn from(synthesizer: CircuitSynthesizer<E>) -> Self {
let num_inputs = synthesizer.input_assignment.len();
let num_aux = synthesizer.aux_assignment.len();
let num_public_variables = synthesizer.num_public_variables();
let num_private_variables = synthesizer.num_private_variables();
let num_constraints = synthesizer.num_constraints();
// Serialize assignments
@ -61,8 +61,8 @@ impl<E: PairingEngine> From<CircuitSynthesizer<E>> for SerializedCircuit {
assignments.iter().map(SerializedField::from).collect()
}
let input_assignment = get_serialized_assignments::<E>(&synthesizer.input_assignment);
let aux_assignment = get_serialized_assignments::<E>(&synthesizer.aux_assignment);
let public_variables = get_serialized_assignments::<E>(&synthesizer.public_variables);
let private_variables = get_serialized_assignments::<E>(&synthesizer.private_variables);
// Serialize constraints
fn get_serialized_constraints<E: PairingEngine>(
@ -102,11 +102,11 @@ impl<E: PairingEngine> From<CircuitSynthesizer<E>> for SerializedCircuit {
}
Self {
num_inputs,
num_aux,
num_public_variables,
num_private_variables,
num_constraints,
input_assignment,
aux_assignment,
public_variables,
private_variables,
at,
bt,
ct,
@ -133,8 +133,8 @@ impl TryFrom<SerializedCircuit> for CircuitSynthesizer<Bls12_377> {
Ok(deserialized)
}
let input_assignment = get_deserialized_assignments(&serialized.input_assignment)?;
let aux_assignment = get_deserialized_assignments(&serialized.aux_assignment)?;
let public_variables = get_deserialized_assignments(&serialized.public_variables)?;
let private_variables = get_deserialized_assignments(&serialized.private_variables)?;
// Deserialize constraints
fn get_deserialized_constraints(
@ -177,8 +177,8 @@ impl TryFrom<SerializedCircuit> for CircuitSynthesizer<Bls12_377> {
at,
bt,
ct,
input_assignment,
aux_assignment,
public_variables,
private_variables,
})
}
}

View File

@ -19,15 +19,15 @@ use snarkvm_models::gadgets::r1cs::Index;
#[derive(Serialize, Deserialize)]
pub enum SerializedIndex {
Input(usize),
Aux(usize),
Public(usize),
Private(usize),
}
impl From<Index> for SerializedIndex {
fn from(index: Index) -> Self {
match index {
Index::Input(idx) => Self::Input(idx),
Index::Aux(idx) => Self::Aux(idx),
Index::Public(idx) => Self::Public(idx),
Index::Private(idx) => Self::Private(idx),
}
}
}
@ -35,8 +35,8 @@ impl From<Index> for SerializedIndex {
impl From<&SerializedIndex> for Index {
fn from(serialized_index: &SerializedIndex) -> Self {
match serialized_index {
SerializedIndex::Input(idx) => Index::Input(*idx),
SerializedIndex::Aux(idx) => Index::Aux(*idx),
SerializedIndex::Public(idx) => Index::Public(*idx),
SerializedIndex::Private(idx) => Index::Private(*idx),
}
}
}