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.1", path = "stdlib", default
brillig = { version = "0.18.1", path = "brillig", default-features = false }
blackbox_solver = { package = "acvm_blackbox_solver", version = "0.18.1", path = "blackbox_solver", default-features = false }

bincode = { version = "2.0.0-rc.3", features = ["serde"] }
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: 29 additions & 1 deletion acir/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
use crate::native_types::Witness;
pub use opcodes::Opcode;

use flate2::{read::DeflateDecoder, write::DeflateEncoder, Compression};

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

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)
use flate2::{read::DeflateDecoder, write::DeflateEncoder};
use std::io::prelude::*;

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

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)
use flate2::write::GzEncoder;
use flate2::Compression;

// use flate2::{read::DeflateDecoder, write::DeflateEncoder, Compression};
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::io::prelude::*;
Expand Down Expand Up @@ -49,13 +58,15 @@
PublicInputs(public_inputs)
}

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

Check warning on line 61 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());
deflater.write_all(&buf).unwrap();

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

Check warning on line 69 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 buf_d = Vec::new();
Expand All @@ -64,6 +75,23 @@
Ok(circuit)
}

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

Check warning on line 78 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::serde::encode_to_vec(&self, bincode::config::standard()).unwrap();

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (bincode)

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (bincode)
let mut encoder = GzEncoder::new(writer, Compression::default());
encoder.write_all(&buf).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, _len): (Circuit, usize) = bincode::serde::decode_from_slice(buf_d.as_slice(), bincode::config::standard()).unwrap();

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (bincode)

Check warning on line 91 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
41 changes: 37 additions & 4 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 acir_field::FieldElement;
use flate2::{
bufread::{DeflateDecoder, DeflateEncoder},
Compression,
};
use flate2::bufread::GzDecoder;
#[cfg(feature="serialize-messagepack")]

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (messagepack)
use flate2::{bufread::DeflateDecoder, write::DeflateEncoder, 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 @@
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,6 +92,7 @@
}
}

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

Expand All @@ -97,6 +105,17 @@
}
}

#[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::serde::encode_to_vec(&val, bincode::config::standard()).unwrap();
Ok(buf)
}
}

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

Expand All @@ -109,3 +128,17 @@
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, _len) =
bincode::serde::decode_from_slice(buf_d.as_slice(), bincode::config::standard()).unwrap();
Ok(Self(witness_map))
}
}
Loading