Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

feat!: move to bincode and GzEncoding for artifacts #436

Merged
merged 13 commits into from
Jul 15, 2023
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion acir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 26 additions & 4 deletions acir/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -49,21 +51,41 @@ impl Circuit {
PublicInputs(public_inputs)
}

#[cfg(feature = "serialize-messagepack")]
pub fn write<W: std::io::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<R: std::io::Read>(reader: R) -> std::io::Result<Self> {
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<W: std::io::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<R: std::io::Read>(reader: R) -> std::io::Result<Self> {
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<OpcodeLabel> {
(0..self.opcodes.len()).map(|label| OpcodeLabel::Resolved(label as u64)).collect()
Expand Down
47 changes: 41 additions & 6 deletions acir/src/native_types/witness_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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);
Expand Down Expand Up @@ -85,27 +92,55 @@ impl From<BTreeMap<Witness, FieldElement>> for WitnessMap {
}
}

#[cfg(feature = "serialize-messagepack")]
impl TryFrom<WitnessMap> for Vec<u8> {
type Error = WitnessMapError;

fn try_from(val: WitnessMap) -> Result<Self, Self::Error> {
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<WitnessMap> for Vec<u8> {
type Error = WitnessMapError;

fn try_from(val: WitnessMap) -> Result<Self, Self::Error> {
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<Self, Self::Error> {
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 =
rmp_serde::from_slice(buf_d.as_slice()).map_err(|err| WitnessMapError(err.into()))?;
Ok(Self(witness_map))
}
}

#[cfg(not(feature = "serialize-messagepack"))]
impl TryFrom<&[u8]> for WitnessMap {
type Error = WitnessMapError;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
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))
}
}