diff --git a/Cargo.toml b/Cargo.toml index c335917bc..0d35fa5c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ stdlib = { package = "acvm_stdlib", version = "0.18.2", path = "stdlib", default brillig = { version = "0.18.2", path = "brillig", default-features = false } blackbox_solver = { package = "acvm_blackbox_solver", version = "0.18.2", path = "blackbox_solver", default-features = false } +bincode = "1.3.3" rmp-serde = "1.1.0" num-bigint = "0.4" diff --git a/acir/Cargo.toml b/acir/Cargo.toml index 47e4906ac..0baeed24a 100644 --- a/acir/Cargo.toml +++ b/acir/Cargo.toml @@ -15,8 +15,9 @@ acir_field.workspace = true brillig.workspace = true serde.workspace = true thiserror.workspace = true -rmp-serde.workspace = true +rmp-serde = { workspace = true, optional = true } flate2 = "1.0.24" +bincode.workspace = true [dev-dependencies] serde_json = "1.0" diff --git a/acir/src/circuit/mod.rs b/acir/src/circuit/mod.rs index 22f140999..a9cd30b8f 100644 --- a/acir/src/circuit/mod.rs +++ b/acir/src/circuit/mod.rs @@ -6,10 +6,12 @@ pub mod opcodes; use crate::native_types::Witness; pub use opcodes::Opcode; -use flate2::{read::DeflateDecoder, write::DeflateEncoder, Compression}; +use std::io::prelude::*; + +use flate2::Compression; + use serde::{Deserialize, Serialize}; use std::collections::BTreeSet; -use std::io::prelude::*; #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Default)] pub struct Circuit { @@ -49,21 +51,41 @@ impl Circuit { PublicInputs(public_inputs) } + #[cfg(feature = "serialize-messagepack")] pub fn write(&self, writer: W) -> std::io::Result<()> { let buf = rmp_serde::to_vec(&self).unwrap(); - let mut deflater = DeflateEncoder::new(writer, Compression::best()); + let mut deflater = flate2::write::DeflateEncoder::new(writer, Compression::best()); deflater.write_all(&buf).unwrap(); Ok(()) } + #[cfg(feature = "serialize-messagepack")] pub fn read(reader: R) -> std::io::Result { - let mut deflater = DeflateDecoder::new(reader); + let mut deflater = flate2::read::DeflateDecoder::new(reader); let mut buf_d = Vec::new(); deflater.read_to_end(&mut buf_d).unwrap(); let circuit = rmp_serde::from_slice(buf_d.as_slice()).unwrap(); Ok(circuit) } + #[cfg(not(feature = "serialize-messagepack"))] + pub fn write(&self, writer: W) -> std::io::Result<()> { + let buf = bincode::serialize(&self).unwrap(); + let mut encoder = flate2::write::GzEncoder::new(writer, Compression::default()); + encoder.write_all(&buf).unwrap(); + encoder.finish().unwrap(); + Ok(()) + } + + #[cfg(not(feature = "serialize-messagepack"))] + pub fn read(reader: R) -> std::io::Result { + let mut gz_decoder = flate2::read::GzDecoder::new(reader); + let mut buf_d = Vec::new(); + gz_decoder.read_to_end(&mut buf_d).unwrap(); + let circuit = bincode::deserialize(&buf_d).unwrap(); + Ok(circuit) + } + /// Initial list of labels attached to opcodes. pub fn initial_opcode_labels(&self) -> Vec { (0..self.opcodes.len()).map(|label| OpcodeLabel::Resolved(label as u64)).collect() diff --git a/acir/src/native_types/witness_map.rs b/acir/src/native_types/witness_map.rs index 4df598dbd..400e0a8ca 100644 --- a/acir/src/native_types/witness_map.rs +++ b/acir/src/native_types/witness_map.rs @@ -5,15 +5,15 @@ use std::{ }; use acir_field::FieldElement; -use flate2::{ - bufread::{DeflateDecoder, DeflateEncoder}, - Compression, -}; +use flate2::bufread::GzDecoder; +use flate2::bufread::GzEncoder; +use flate2::Compression; use serde::{Deserialize, Serialize}; use thiserror::Error; use crate::native_types::Witness; +#[cfg(feature = "serialize-messagepack")] #[derive(Debug, Error)] enum SerializationError { #[error(transparent)] @@ -26,6 +26,13 @@ enum SerializationError { Deflate(#[from] std::io::Error), } +#[cfg(not(feature = "serialize-messagepack"))] +#[derive(Debug, Error)] +enum SerializationError { + #[error(transparent)] + Deflate(#[from] std::io::Error), +} + #[derive(Debug, Error)] #[error(transparent)] pub struct WitnessMapError(#[from] SerializationError); @@ -85,23 +92,38 @@ impl From> for WitnessMap { } } +#[cfg(feature = "serialize-messagepack")] impl TryFrom for Vec { type Error = WitnessMapError; fn try_from(val: WitnessMap) -> Result { let buf = rmp_serde::to_vec(&val).map_err(|err| WitnessMapError(err.into()))?; - let mut deflater = DeflateEncoder::new(buf.as_slice(), Compression::best()); + let mut deflater = flate2::write::DeflateEncoder::new(buf.as_slice(), Compression::best()); let mut buf_c = Vec::new(); deflater.read_to_end(&mut buf_c).map_err(|err| WitnessMapError(err.into()))?; Ok(buf_c) } } +#[cfg(not(feature = "serialize-messagepack"))] +impl TryFrom for Vec { + type Error = WitnessMapError; + + fn try_from(val: WitnessMap) -> Result { + let buf = bincode::serialize(&val).unwrap(); + let mut deflater = GzEncoder::new(buf.as_slice(), Compression::best()); + let mut buf_c = Vec::new(); + deflater.read_to_end(&mut buf_c).map_err(|err| WitnessMapError(err.into()))?; + Ok(buf_c) + } +} + +#[cfg(feature = "serialize-messagepack")] impl TryFrom<&[u8]> for WitnessMap { type Error = WitnessMapError; fn try_from(bytes: &[u8]) -> Result { - let mut deflater = DeflateDecoder::new(bytes); + let mut deflater = flate2::bufread::DeflateDecoder::new(bytes); let mut buf_d = Vec::new(); deflater.read_to_end(&mut buf_d).map_err(|err| WitnessMapError(err.into()))?; let witness_map = @@ -109,3 +131,16 @@ impl TryFrom<&[u8]> for WitnessMap { Ok(Self(witness_map)) } } + +#[cfg(not(feature = "serialize-messagepack"))] +impl TryFrom<&[u8]> for WitnessMap { + type Error = WitnessMapError; + + fn try_from(bytes: &[u8]) -> Result { + let mut deflater = GzDecoder::new(bytes); + let mut buf_d = Vec::new(); + deflater.read_to_end(&mut buf_d).map_err(|err| WitnessMapError(err.into()))?; + let witness_map = bincode::deserialize(buf_d.as_slice()).unwrap(); + Ok(Self(witness_map)) + } +}