clean up files

This commit is contained in:
collin 2020-08-13 01:34:17 -07:00
parent 76009cb843
commit efce3df6cf
4 changed files with 119 additions and 101 deletions

View File

@ -3,3 +3,9 @@ pub use self::circuit_synthesizer::*;
pub mod serialized_circuit;
pub use self::serialized_circuit::*;
pub mod serialized_field;
pub use self::serialized_field::*;
pub mod serialized_index;
pub use self::serialized_index::*;

View File

@ -1,13 +1,14 @@
use crate::synthesizer::CircuitSynthesizer;
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use crate::synthesizer::{CircuitSynthesizer, SerializedField, SerializedIndex};
use snarkos_curves::bls12_377::Bls12_377;
use snarkos_errors::curves::FieldError;
use snarkos_models::{
curves::{Field, Fp256, Fp256Parameters, PairingEngine},
curves::PairingEngine,
gadgets::r1cs::{ConstraintSystem, Index},
};
use std::{convert::TryFrom, str::FromStr};
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
#[derive(Serialize, Deserialize)]
pub struct SerializedCircuit {
@ -35,6 +36,11 @@ 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_constraints = synthesizer.num_constraints();
// Serialize assignments
fn get_serialized_assignments<E: PairingEngine>(assignments: &Vec<E::Fr>) -> Vec<SerializedField> {
let mut serialized = vec![];
@ -46,6 +52,11 @@ impl<E: PairingEngine> From<CircuitSynthesizer<E>> for SerializedCircuit {
serialized
}
let input_assignment = get_serialized_assignments::<E>(&synthesizer.input_assignment);
let aux_assignment = get_serialized_assignments::<E>(&synthesizer.aux_assignment);
// Serialize constraints
fn get_serialized_constraints<E: PairingEngine>(
constraints: &Vec<(E::Fr, Index)>,
) -> Vec<(SerializedField, SerializedIndex)> {
@ -61,48 +72,37 @@ impl<E: PairingEngine> From<CircuitSynthesizer<E>> for SerializedCircuit {
serialized
}
let num_inputs = synthesizer.input_assignment.len();
let num_aux = synthesizer.aux_assignment.len();
let num_constraints = synthesizer.num_constraints();
let mut at = vec![];
let mut bt = vec![];
let mut ct = vec![];
let mut result = Self {
num_inputs,
num_aux,
num_constraints,
// Assignments
input_assignment: vec![],
aux_assignment: vec![],
// Constraints
at: vec![],
bt: vec![],
ct: vec![],
};
// Serialize assignments
result.input_assignment = get_serialized_assignments::<E>(&synthesizer.input_assignment);
result.aux_assignment = get_serialized_assignments::<E>(&synthesizer.aux_assignment);
// Serialize constraints
for i in 0..num_constraints {
// Serialize at[i]
let a_constraints = get_serialized_constraints::<E>(&synthesizer.at[i]);
result.at.push(a_constraints);
at.push(a_constraints);
// Serialize bt[i]
let b_constraints = get_serialized_constraints::<E>(&synthesizer.bt[i]);
result.bt.push(b_constraints);
bt.push(b_constraints);
// Serialize ct[i]
let c_constraints = get_serialized_constraints::<E>(&synthesizer.ct[i]);
result.ct.push(c_constraints);
ct.push(c_constraints);
}
result
Self {
num_inputs,
num_aux,
num_constraints,
input_assignment,
aux_assignment,
at,
bt,
ct,
}
}
}
@ -110,6 +110,7 @@ impl TryFrom<SerializedCircuit> for CircuitSynthesizer<Bls12_377> {
type Error = FieldError;
fn try_from(serialized: SerializedCircuit) -> Result<CircuitSynthesizer<Bls12_377>, Self::Error> {
// Deserialize assignments
fn get_deserialized_assignments(
assignments: &Vec<SerializedField>,
) -> Result<Vec<<Bls12_377 as PairingEngine>::Fr>, FieldError> {
@ -123,6 +124,11 @@ 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)?;
// Deserialize constraints
fn get_deserialized_constraints(
constraints: &Vec<(SerializedField, SerializedIndex)>,
) -> Result<Vec<(<Bls12_377 as PairingEngine>::Fr, Index)>, FieldError> {
@ -138,89 +144,33 @@ impl TryFrom<SerializedCircuit> for CircuitSynthesizer<Bls12_377> {
Ok(deserialized)
}
let mut result = CircuitSynthesizer::<Bls12_377> {
input_assignment: vec![],
aux_assignment: vec![],
at: vec![],
bt: vec![],
ct: vec![],
};
let mut at = vec![];
let mut bt = vec![];
let mut ct = vec![];
// Deserialize assignments
result.input_assignment = get_deserialized_assignments(&serialized.input_assignment)?;
result.aux_assignment = get_deserialized_assignments(&serialized.aux_assignment)?;
// Deserialize constraints
for i in 0..serialized.num_constraints {
// Deserialize at[i]
let a_constraints = get_deserialized_constraints(&serialized.at[i])?;
result.at.push(a_constraints);
at.push(a_constraints);
// Deserialize bt[i]
let b_constraints = get_deserialized_constraints(&serialized.bt[i])?;
result.bt.push(b_constraints);
bt.push(b_constraints);
// Deserialize ct[i]
let c_constraints = get_deserialized_constraints(&serialized.ct[i])?;
result.ct.push(c_constraints);
ct.push(c_constraints);
}
Ok(result)
}
}
#[derive(Serialize, Deserialize)]
pub struct SerializedField(pub String);
impl<F: Field> From<&F> for SerializedField {
fn from(field: &F) -> Self {
// write field to buffer
let mut buf = Vec::new();
field.write(&mut buf).unwrap();
// convert to big integer
let f_bigint = BigUint::from_bytes_le(&buf);
let f_string = f_bigint.to_str_radix(10);
Self(f_string)
}
}
impl<P: Fp256Parameters> TryFrom<&SerializedField> for Fp256<P> {
type Error = FieldError;
fn try_from(serialized: &SerializedField) -> Result<Self, Self::Error> {
Fp256::<P>::from_str(&serialized.0)
}
}
#[derive(Serialize, Deserialize)]
pub enum SerializedIndex {
Input(usize),
Aux(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),
}
}
}
impl From<&SerializedIndex> for Index {
fn from(serialized_index: &SerializedIndex) -> Self {
match serialized_index {
SerializedIndex::Input(idx) => Index::Input(idx.clone()),
SerializedIndex::Aux(idx) => Index::Aux(idx.clone()),
}
Ok(CircuitSynthesizer::<Bls12_377> {
input_assignment,
aux_assignment,
at,
bt,
ct,
})
}
}

View File

@ -0,0 +1,35 @@
use snarkos_errors::curves::FieldError;
use snarkos_models::curves::{Field, Fp256, Fp256Parameters};
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use std::{convert::TryFrom, str::FromStr};
#[derive(Serialize, Deserialize)]
pub struct SerializedField(pub String);
impl<F: Field> From<&F> for SerializedField {
fn from(field: &F) -> Self {
// write field to buffer
let mut buf = Vec::new();
field.write(&mut buf).unwrap();
// convert to base 10 integer
let f_bigint = BigUint::from_bytes_le(&buf);
let f_string = f_bigint.to_str_radix(10);
Self(f_string)
}
}
impl<P: Fp256Parameters> TryFrom<&SerializedField> for Fp256<P> {
type Error = FieldError;
fn try_from(serialized: &SerializedField) -> Result<Self, Self::Error> {
Fp256::<P>::from_str(&serialized.0)
}
}

View File

@ -0,0 +1,27 @@
use snarkos_models::gadgets::r1cs::Index;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub enum SerializedIndex {
Input(usize),
Aux(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),
}
}
}
impl From<&SerializedIndex> for Index {
fn from(serialized_index: &SerializedIndex) -> Self {
match serialized_index {
SerializedIndex::Input(idx) => Index::Input(idx.clone()),
SerializedIndex::Aux(idx) => Index::Aux(idx.clone()),
}
}
}