diff --git a/leo/commands/build.rs b/leo/commands/build.rs index 75a38dc5a9..79f7925297 100644 --- a/leo/commands/build.rs +++ b/leo/commands/build.rs @@ -125,12 +125,12 @@ impl CLI for BuildCommand { circuit_file.write_to(&path, json)?; // Check that we can read the serialized circuit file - // let serialized = circuit_file.read_from(&package_path)?; + let serialized = circuit_file.read_from(&package_path)?; // Deserialize the circuit - // let deserialized = SerializedKeypairAssembly::from_json_string(&serialized).unwrap(); - - // println!("deserialized {:?}", deserialized); + let deserialized = SerializedKeypairAssembly::from_json_string(&serialized).unwrap(); + let keypair_assembly = KeypairAssembly::::try_from(deserialized).unwrap(); + println!("deserialized {:?}", keypair_assembly.num_constraints); } // If a checksum file exists, check if it differs from the new checksum @@ -164,7 +164,10 @@ impl CLI for BuildCommand { use num_bigint::BigUint; use serde::{Deserialize, Serialize}; -use snarkos_models::curves::Field; +use snarkos_curves::bls12_377::Fr; +use snarkos_errors::curves::FieldError; +use snarkos_models::curves::{Field, Fp256, Fp256Parameters, PrimeField}; +use std::{convert::TryInto, str::FromStr}; #[derive(Serialize, Deserialize)] pub struct SerializedKeypairAssembly { @@ -192,6 +195,7 @@ impl From> for SerializedKeypairAssembly { constraints: &Vec<(E::Fr, Index)>, ) -> Vec<(SerializedField, SerializedIndex)> { let mut serialized = vec![]; + for &(ref coeff, index) in constraints.iter() { let field = SerializedField::from(coeff); let index = SerializedIndex::from(index); @@ -232,6 +236,55 @@ impl From> for SerializedKeypairAssembly { } } +impl TryFrom for KeypairAssembly { + type Error = FieldError; + + fn try_from(serialized: SerializedKeypairAssembly) -> Result, Self::Error> { + fn get_deserialized_constraints( + constraints: &Vec<(SerializedField, SerializedIndex)>, + ) -> Result, FieldError> { + let mut deserialized = vec![]; + + for &(ref serialized_coeff, ref serialized_index) in constraints.iter() { + let field = Fr::try_from(serialized_coeff)?; + let index = Index::from(serialized_index); + + deserialized.push((field, index)); + } + + Ok(deserialized) + } + + let mut result = KeypairAssembly:: { + num_inputs: serialized.num_inputs, + num_aux: serialized.num_aux, + num_constraints: serialized.num_constraints, + at: vec![], + bt: vec![], + ct: vec![], + }; + + for i in 0..serialized.num_constraints { + // Deserialize at[i] + + let a_constraints = get_deserialized_constraints(&serialized.at[i])?; + result.at.push(a_constraints); + + // Deserialize bt[i] + + let b_constraints = get_deserialized_constraints(&serialized.bt[i])?; + result.bt.push(b_constraints); + + // Deserialize ct[i] + + let c_constraints = get_deserialized_constraints(&serialized.ct[i])?; + result.ct.push(c_constraints); + } + + Ok(result) + } +} + #[derive(Serialize, Deserialize)] pub struct SerializedField(pub String); @@ -253,6 +306,14 @@ impl From<&F> for SerializedField { } } +impl TryFrom<&SerializedField> for Fr { + type Error = FieldError; + + fn try_from(serialized: &SerializedField) -> Result { + Ok(Fr::from_str(&serialized.0).unwrap()) + } +} + #[derive(Serialize, Deserialize)] pub enum SerializedIndex { Input(usize), @@ -267,3 +328,12 @@ impl From for SerializedIndex { } } } + +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()), + } + } +}