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

Commit

Permalink
feat!: move to bincode and GzEncoding for artifacts (#436)
Browse files Browse the repository at this point in the history
Co-authored-by: Charlie Lye <karl.lye@gmail.com>
  • Loading branch information
kobyhallx and charlielye authored Jul 15, 2023
1 parent a7773a2 commit 4683240
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
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")]

Check warning on line 54 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)

Check warning on line 54 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (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")]

Check warning on line 62 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)

Check warning on line 62 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (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"))]

Check warning on line 71 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)

Check warning on line 71 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)
pub fn write<W: std::io::Write>(&self, writer: W) -> std::io::Result<()> {
let buf = bincode::serialize(&self).unwrap();

Check warning on line 73 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (bincode)

Check warning on line 73 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (bincode)
let mut encoder = flate2::write::GzEncoder::new(writer, Compression::default());
encoder.write_all(&buf).unwrap();
encoder.finish().unwrap();
Ok(())
}

#[cfg(not(feature = "serialize-messagepack"))]

Check warning on line 80 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)

Check warning on line 80 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (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();

Check warning on line 85 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (bincode)

Check warning on line 85 in acir/src/circuit/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (bincode)
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")]

Check warning on line 16 in acir/src/native_types/witness_map.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)

Check warning on line 16 in acir/src/native_types/witness_map.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (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"))]

Check warning on line 29 in acir/src/native_types/witness_map.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)

Check warning on line 29 in acir/src/native_types/witness_map.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (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")]

Check warning on line 95 in acir/src/native_types/witness_map.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)

Check warning on line 95 in acir/src/native_types/witness_map.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (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"))]

Check warning on line 108 in acir/src/native_types/witness_map.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)

Check warning on line 108 in acir/src/native_types/witness_map.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (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))
}
}

0 comments on commit 4683240

Please sign in to comment.