Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: Move circuit serialization circuit into acir #3345

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions acvm-repo/acir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde.workspace = true
thiserror.workspace = true
flate2 = "1.0.24"
bincode.workspace = true
base64.workspace = true

[dev-dependencies]
serde_json = "1.0"
Expand Down
40 changes: 37 additions & 3 deletions acvm-repo/acir/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use thiserror::Error;

use std::{io::prelude::*, num::ParseIntError, str::FromStr};

use base64::Engine;
use flate2::Compression;
use serde::{de::Error as DeserializationError, Deserialize, Deserializer, Serialize, Serializer};

use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;

#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
Expand Down Expand Up @@ -125,21 +126,54 @@ impl Circuit {
PublicInputs(public_inputs)
}

pub fn write<W: std::io::Write>(&self, writer: W) -> std::io::Result<()> {
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
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)?;
encoder.finish()?;
Ok(())
}

pub fn read<R: std::io::Read>(reader: R) -> std::io::Result<Self> {
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)?;
bincode::deserialize(&buf_d)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidInput, err))
}

pub fn serialize_circuit(circuit: &Circuit) -> Vec<u8> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just serialize and serialize_base64?

let mut circuit_bytes: Vec<u8> = Vec::new();
circuit.write(&mut circuit_bytes).expect("expected circuit to be serializable");
circuit_bytes
}

pub fn deserialize_circuit(serialized_circuit: &[u8]) -> std::io::Result<Self> {
Circuit::read(&*serialized_circuit)
}

// Serialize and base64 encode circuit
pub fn serialize_circuit_base64<S>(circuit: &Circuit, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let circuit_bytes = Circuit::serialize_circuit(circuit);
let encoded_b64 = base64::engine::general_purpose::STANDARD.encode(circuit_bytes);
s.serialize_str(&encoded_b64)
}

// Deserialize and base64 decode circuit
pub fn deserialize_circuit_base64<'de, D>(deserializer: D) -> Result<Circuit, D::Error>
where
D: Deserializer<'de>,
{
let bytecode_b64: String = serde::Deserialize::deserialize(deserializer)?;
let circuit_bytes = base64::engine::general_purpose::STANDARD
.decode(bytecode_b64)
.map_err(D::Error::custom)?;
let circuit = Self::deserialize_circuit(&*circuit_bytes).map_err(D::Error::custom)?;
Ok(circuit)
}
}

impl std::fmt::Display for Circuit {
Expand Down
1 change: 0 additions & 1 deletion compiler/noirc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ acvm.workspace = true
iter-extended.workspace = true
fm.workspace = true
serde.workspace = true
base64.workspace = true
fxhash.workspace = true
6 changes: 4 additions & 2 deletions compiler/noirc_driver/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use noirc_errors::debug_info::DebugInfo;
use noirc_evaluator::errors::SsaReport;

use super::debug::DebugFile;
use crate::program::{deserialize_circuit, serialize_circuit};

/// Describes the types of smart contract functions that are allowed.
/// Unlike the similar enum in noirc_frontend, 'open' and 'unconstrained'
Expand Down Expand Up @@ -62,7 +61,10 @@ pub struct ContractFunction {

pub abi: Abi,

#[serde(serialize_with = "serialize_circuit", deserialize_with = "deserialize_circuit")]
#[serde(
serialize_with = "Circuit::serialize_circuit_base64",
deserialize_with = "Circuit::deserialize_circuit_base64"
)]
pub bytecode: Circuit,

pub debug: DebugInfo,
Expand Down
31 changes: 5 additions & 26 deletions compiler/noirc_driver/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use std::collections::BTreeMap;
use acvm::acir::circuit::Circuit;
use fm::FileId;

use base64::Engine;
use noirc_errors::debug_info::DebugInfo;
use noirc_evaluator::errors::SsaReport;
use serde::{de::Error as DeserializationError, ser::Error as SerializationError};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize};

use super::debug::DebugFile;

Expand All @@ -20,32 +18,13 @@ pub struct CompiledProgram {
/// Used to short-circuit compilation in the case of the source code not changing since the last compilation.
pub hash: u64,

#[serde(serialize_with = "serialize_circuit", deserialize_with = "deserialize_circuit")]
#[serde(
serialize_with = "Circuit::serialize_circuit_base64",
deserialize_with = "Circuit::deserialize_circuit_base64"
)]
pub circuit: Circuit,
pub abi: noirc_abi::Abi,
pub debug: DebugInfo,
pub file_map: BTreeMap<FileId, DebugFile>,
pub warnings: Vec<SsaReport>,
}

pub(crate) fn serialize_circuit<S>(circuit: &Circuit, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut circuit_bytes: Vec<u8> = Vec::new();
circuit.write(&mut circuit_bytes).map_err(S::Error::custom)?;

let encoded_b64 = base64::engine::general_purpose::STANDARD.encode(circuit_bytes);
s.serialize_str(&encoded_b64)
}

pub(crate) fn deserialize_circuit<'de, D>(deserializer: D) -> Result<Circuit, D::Error>
where
D: Deserializer<'de>,
{
let bytecode_b64: String = serde::Deserialize::deserialize(deserializer)?;
let circuit_bytes =
base64::engine::general_purpose::STANDARD.decode(bytecode_b64).map_err(D::Error::custom)?;
let circuit = Circuit::read(&*circuit_bytes).map_err(D::Error::custom)?;
Ok(circuit)
}
16 changes: 4 additions & 12 deletions tooling/backend_interface/src/proof_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Backend {

// Create a temporary file for the circuit
let circuit_path = temp_directory.join("circuit").with_extension("bytecode");
let serialized_circuit = serialize_circuit(circuit);
let serialized_circuit = Circuit::serialize_circuit(circuit);
write_to_file(&serialized_circuit, &circuit_path);

GatesCommand { crs_path: self.crs_directory(), bytecode_path: circuit_path }
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Backend {
// Create a temporary file for the circuit
//
let bytecode_path = temp_directory.join("circuit").with_extension("bytecode");
let serialized_circuit = serialize_circuit(circuit);
let serialized_circuit = Circuit::serialize_circuit(circuit);
write_to_file(&serialized_circuit, &bytecode_path);

// Create proof and store it in the specified path
Expand Down Expand Up @@ -97,7 +97,7 @@ impl Backend {

// Create a temporary file for the circuit
let bytecode_path = temp_directory.join("circuit").with_extension("bytecode");
let serialized_circuit = serialize_circuit(circuit);
let serialized_circuit = Circuit::serialize_circuit(circuit);
write_to_file(&serialized_circuit, &bytecode_path);

// Create the verification key and write it to the specified path
Expand Down Expand Up @@ -130,7 +130,7 @@ impl Backend {
// Create a temporary file for the circuit
//
let bytecode_path = temp_directory.join("circuit").with_extension("bytecode");
let serialized_circuit = serialize_circuit(circuit);
let serialized_circuit = Circuit::serialize_circuit(circuit);
write_to_file(&serialized_circuit, &bytecode_path);

// Create the verification key and write it to the specified path
Expand Down Expand Up @@ -174,11 +174,3 @@ pub(super) fn write_to_file(bytes: &[u8], path: &Path) -> String {
Ok(_) => display.to_string(),
}
}

// TODO: See nargo/src/artifacts/mod.rs
// TODO: This method should live in ACVM and be the default method for serializing/deserializing circuits
pub(super) fn serialize_circuit(circuit: &Circuit) -> Vec<u8> {
let mut circuit_bytes: Vec<u8> = Vec::new();
circuit.write(&mut circuit_bytes).unwrap();
circuit_bytes
}
4 changes: 2 additions & 2 deletions tooling/backend_interface/src/smart_contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::proof_system::{serialize_circuit, write_to_file};
use super::proof_system::write_to_file;
use crate::{
cli::{ContractCommand, WriteVkCommand},
Backend, BackendError,
Expand All @@ -16,7 +16,7 @@ impl Backend {

// Create a temporary file for the circuit
let bytecode_path = temp_directory_path.join("circuit").with_extension("bytecode");
let serialized_circuit = serialize_circuit(circuit);
let serialized_circuit = Circuit::serialize_circuit(circuit);
write_to_file(&serialized_circuit, &bytecode_path);

// Create the verification key and write it to the specified path
Expand Down
1 change: 0 additions & 1 deletion tooling/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ noirc_printable_type.workspace = true
iter-extended.workspace = true
serde.workspace = true
thiserror.workspace = true
base64.workspace = true
codespan-reporting.workspace = true
4 changes: 2 additions & 2 deletions tooling/nargo/src/artifacts/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub struct PreprocessedContractFunction {
pub abi: Abi,

#[serde(
serialize_with = "super::serialize_circuit",
deserialize_with = "super::deserialize_circuit"
serialize_with = "Circuit::serialize_circuit_base64",
deserialize_with = "Circuit::deserialize_circuit_base64"
)]
pub bytecode: Circuit,
}
28 changes: 0 additions & 28 deletions tooling/nargo/src/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,6 @@
//! These artifacts are intended to remain independent of any applications being built on top of Noir.
//! Should any projects require/desire a different artifact format, it's expected that they will write a transformer
//! to generate them using these artifacts as a starting point.
use acvm::acir::circuit::Circuit;
use base64::Engine;
use serde::{
de::Error as DeserializationError, ser::Error as SerializationError, Deserializer, Serializer,
};

pub mod contract;
pub mod debug;
pub mod program;

// TODO: move these down into ACVM.
fn serialize_circuit<S>(circuit: &Circuit, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut circuit_bytes: Vec<u8> = Vec::new();
circuit.write(&mut circuit_bytes).map_err(S::Error::custom)?;
let encoded_b64 = base64::engine::general_purpose::STANDARD.encode(circuit_bytes);
s.serialize_str(&encoded_b64)
}

fn deserialize_circuit<'de, D>(deserializer: D) -> Result<Circuit, D::Error>
where
D: Deserializer<'de>,
{
let bytecode_b64: String = serde::Deserialize::deserialize(deserializer)?;
let circuit_bytes =
base64::engine::general_purpose::STANDARD.decode(bytecode_b64).map_err(D::Error::custom)?;
let circuit = Circuit::read(&*circuit_bytes).map_err(D::Error::custom)?;
Ok(circuit)
}
4 changes: 2 additions & 2 deletions tooling/nargo/src/artifacts/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub struct PreprocessedProgram {
pub abi: Abi,

#[serde(
serialize_with = "super::serialize_circuit",
deserialize_with = "super::deserialize_circuit"
serialize_with = "Circuit::serialize_circuit_base64",
deserialize_with = "Circuit::deserialize_circuit_base64"
)]
pub bytecode: Circuit,
}
Loading