From a96eb5ad19fa81ef48b5adacbec88c16fefec6c8 Mon Sep 17 00:00:00 2001 From: collin Date: Mon, 10 Aug 2020 11:46:45 -0700 Subject: [PATCH] serialize circuit to json --- Cargo.lock | 1 + Cargo.toml | 1 + leo/commands/build.rs | 54 +++++++++++++++++++++------------- package/src/outputs/circuit.rs | 10 +++---- 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9253e641e0..c115b88e0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -663,6 +663,7 @@ dependencies = [ "leo-input", "leo-package", "log", + "num-bigint", "rand", "rand_core", "rusty-hook", diff --git a/Cargo.toml b/Cargo.toml index f54cad5d53..8c0d3459ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ colored = { version = "2.0" } env_logger = { version = "0.7" } from-pest = { version = "0.3.1" } log = { version = "0.4" } +num-bigint = { version = "0.3" } rand = { version = "0.7" } rand_core = { version = "0.5.1" } serde = { version = "1.0", features = ["derive"] } diff --git a/leo/commands/build.rs b/leo/commands/build.rs index ba3eddd860..75a38dc5a9 100644 --- a/leo/commands/build.rs +++ b/leo/commands/build.rs @@ -115,21 +115,20 @@ impl CLI for BuildCommand { log::debug!("Compiled constraints - {:#?}", output); log::debug!("Number of constraints - {:#?}", cs.num_constraints()); - // Serialize circuit - let mut writer = Vec::new(); - as CanonicalSerialize>::serialize(&cs, &mut writer).unwrap(); - // serialize_circuit(cs, &mut writer); - println!("actual size bytes {:?}", writer.len()); + // Serialize the circuit + let keypair_object = SerializedKeypairAssembly::from(cs); + let json = keypair_object.to_json_string().unwrap(); + // println!("json: {}", json); - // Write serialized circuit to circuit `.bytes` file. + // Write serialized circuit to circuit `.json` file. let circuit_file = CircuitFile::new(&package_name); - circuit_file.write_to(&path, &writer[..])?; + circuit_file.write_to(&path, json)?; // Check that we can read the serialized circuit file - let serialized = circuit_file.read_from(&package_path)?; - let _deserialized = - as CanonicalDeserialize>::deserialize(&mut &serialized[..]).unwrap(); - // let _deserialized = deserialize_circuit::(&mut &serialized[..]); + // let serialized = circuit_file.read_from(&package_path)?; + + // Deserialize the circuit + // let deserialized = SerializedKeypairAssembly::from_json_string(&serialized).unwrap(); // println!("deserialized {:?}", deserialized); } @@ -162,7 +161,10 @@ impl CLI for BuildCommand { Ok(None) } } + +use num_bigint::BigUint; use serde::{Deserialize, Serialize}; +use snarkos_models::curves::Field; #[derive(Serialize, Deserialize)] pub struct SerializedKeypairAssembly { @@ -186,7 +188,9 @@ impl SerializedKeypairAssembly { impl From> for SerializedKeypairAssembly { fn from(assembly: KeypairAssembly) -> Self { - fn get_serialized_constraints(constraints: &Vec<(E::Fr, Index)>) -> Vec<(SerializedField, SerializedIndex)> { + fn get_serialized_constraints( + constraints: &Vec<(E::Fr, Index)>, + ) -> Vec<(SerializedField, SerializedIndex)> { let mut serialized = vec![]; for &(ref coeff, index) in constraints.iter() { let field = SerializedField::from(coeff); @@ -210,17 +214,17 @@ impl From> for SerializedKeypairAssembly { for i in 0..assembly.num_constraints { // Serialize at[i] - let a_constraints = get_serialized_constraints(&assembly.at[i]); + let a_constraints = get_serialized_constraints::(&assembly.at[i]); result.at.push(a_constraints); // Serialize bt[i] - let b_constraints = get_serialized_constraints(&assembly.bt[i]); + let b_constraints = get_serialized_constraints::(&assembly.bt[i]); result.bt.push(b_constraints); // Serialize ct[i] - let c_constraints = get_serialized_constraints(&assembly.ct[i]); + let c_constraints = get_serialized_constraints::(&assembly.ct[i]); result.ct.push(c_constraints); } @@ -229,15 +233,23 @@ impl From> for SerializedKeypairAssembly { } #[derive(Serialize, Deserialize)] -pub struct SerializedField(pub Vec); +pub struct SerializedField(pub String); -impl From<&::Fr> for SerializedField { - fn from(field: &::Fr) -> Self { - let mut writer = Vec::new(); +impl From<&F> for SerializedField { + fn from(field: &F) -> Self { + // write field to buffer - <::Fr as CanonicalSerialize>::serialize(field, &mut writer).unwrap(); + let mut buf = Vec::new(); - Self(writer) + 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) } } diff --git a/package/src/outputs/circuit.rs b/package/src/outputs/circuit.rs index 58869576b5..00d4312877 100644 --- a/package/src/outputs/circuit.rs +++ b/package/src/outputs/circuit.rs @@ -9,7 +9,7 @@ use std::{ path::PathBuf, }; -pub static CIRCUIT_FILE_EXTENSION: &str = ".bytes"; +pub static CIRCUIT_FILE_EXTENSION: &str = ".json"; #[derive(Deserialize)] pub struct CircuitFile { @@ -29,18 +29,18 @@ impl CircuitFile { } /// Reads the serialized circuit from the given file path if it exists. - pub fn read_from(&self, path: &PathBuf) -> Result, CircuitFileError> { + pub fn read_from(&self, path: &PathBuf) -> Result { let path = self.setup_file_path(path); - Ok(fs::read(&path).map_err(|_| CircuitFileError::FileReadError(path.clone()))?) + Ok(fs::read_to_string(&path).map_err(|_| CircuitFileError::FileReadError(path.clone()))?) } /// Writes the given serialized circuit to a file. - pub fn write_to(&self, path: &PathBuf, serialized_circuit: &[u8]) -> Result<(), CircuitFileError> { + pub fn write_to(&self, path: &PathBuf, circuit: String) -> Result<(), CircuitFileError> { let path = self.setup_file_path(path); let mut file = File::create(&path)?; - file.write_all(serialized_circuit)?; + file.write_all(circuit.as_bytes())?; Ok(()) }