diff --git a/CHANGELOG.md b/CHANGELOG.md index 93b64fae..fed9e08d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Move compress module under composer - Move constraint_system module under composer - Move permutation module under composer +- Change API for circuit (de-)compression [#804] ### Removed @@ -555,6 +556,7 @@ is necessary since `rkyv/validation` was required as a bound. [#805]: https://github.com/dusk-network/plonk/issues/805 +[#804]: https://github.com/dusk-network/plonk/issues/804 [#802]: https://github.com/dusk-network/plonk/issues/802 [#797]: https://github.com/dusk-network/plonk/issues/797 [#796]: https://github.com/dusk-network/plonk/issues/796 diff --git a/src/compiler.rs b/src/compiler.rs index 4afed7a2..8dbfe431 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -4,13 +4,9 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -#[cfg(feature = "alloc")] -use alloc::vec::Vec; - use dusk_bls12_381::BlsScalar; use crate::commitment_scheme::{CommitKey, OpeningKey, PublicParameters}; -use crate::composer::CompressedCircuit; use crate::error::Error; use crate::fft::{EvaluationDomain, Evaluations, Polynomial}; use crate::proof_system::preprocess::Polynomials; @@ -61,31 +57,22 @@ impl Compiler { Self::compile_with_composer(pp, label, &composer) } - /// Return a bytes representation of a compressed circuit, capable of - /// generating its prover and verifier instances. - #[cfg(feature = "alloc")] - pub fn compress() -> Result, Error> - where - C: Circuit, - { - let hades_optimization = true; - CompressedCircuit::from_circuit::(hades_optimization) - } - /// Generates a [Prover] and [Verifier] from a buffer created by - /// [Self::compress]. - pub fn decompress( + /// [Circuit::compress]. + pub fn compile_with_compressed( pp: &PublicParameters, label: &[u8], compressed: &[u8], ) -> Result<(Prover, Verifier), Error> { - CompressedCircuit::from_bytes(pp, label, compressed) + let composer = Composer::from_bytes(compressed)?; + + Self::compile_with_composer(pp, label, &composer) } /// Create a new arguments set from a given circuit instance /// /// Use the default implementation of the circuit - pub(crate) fn compile_with_composer( + fn compile_with_composer( pp: &PublicParameters, label: &[u8], composer: &Composer, diff --git a/src/composer.rs b/src/composer.rs index 51611dd3..6a441d5b 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -25,11 +25,9 @@ mod gate; pub(crate) mod permutation; pub use circuit::Circuit; -use constraint_system::ecc::WnafRound; pub use constraint_system::{Constraint, Witness, WitnessPoint}; pub use gate::Gate; -pub(crate) use compress::CompressedCircuit; pub(crate) use constraint_system::{Selector, WireData, WiredWitness}; pub(crate) use permutation::Permutation; @@ -83,6 +81,11 @@ impl Composer { self.constraints.len() } + /// Create a [`Composer`] instance from a compressed circuit + pub(crate) fn from_bytes(compressed: &[u8]) -> Result { + compress::CompressedCircuit::from_bytes(compressed) + } + /// Allocate a witness value into the composer and return its index. fn append_witness_internal(&mut self, witness: BlsScalar) -> Witness { let n = self.witnesses.len(); @@ -474,7 +477,7 @@ impl Composer { let xy_alpha = self.append_witness(xy_alphas[i]); let xy_beta = x_beta * y_beta; - let wnaf_round = WnafRound { + let wnaf_round = constraint_system::ecc::WnafRound { acc_x, acc_y, accumulated_bit, diff --git a/src/composer/circuit.rs b/src/composer/circuit.rs index 20272ff2..42e2267a 100644 --- a/src/composer/circuit.rs +++ b/src/composer/circuit.rs @@ -4,9 +4,12 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use crate::error::Error; +#[cfg(feature = "alloc")] +use alloc::vec::Vec; -use super::Composer; +use crate::prelude::{Composer, Error}; + +use super::compress::CompressedCircuit; /// Circuit implementation that can be proved by a Composer /// @@ -23,4 +26,22 @@ pub trait Circuit: Default { Err(_) => 0, } } + + /// Return a bytes representation of a compressed circuit, capable of + /// being compiled into its prover and verifier instances with + /// [`Compiler::compile_with_compressed`]. + /// + /// [`Compiler::compile_with_compressed`]: + /// [`crate::prelude::Compiler::compile_with_compressed`] + #[cfg(feature = "alloc")] + fn compress() -> Result, Error> { + let mut composer = Composer::initialized(); + Self::default().circuit(&mut composer)?; + + let hades_optimization = true; + Ok(CompressedCircuit::from_composer( + hades_optimization, + composer, + )) + } } diff --git a/src/composer/compress.rs b/src/composer/compress.rs index 12d90e27..58030296 100644 --- a/src/composer/compress.rs +++ b/src/composer/compress.rs @@ -10,10 +10,7 @@ use msgpacker::{MsgPacker, Packable, Unpackable}; use alloc::vec::Vec; -use super::{ - BlsScalar, Circuit, Composer, Constraint, Error, Gate, Selector, Witness, -}; -use crate::prelude::{Compiler, Prover, PublicParameters, Verifier}; +use super::{BlsScalar, Composer, Constraint, Error, Gate, Selector, Witness}; mod hades; @@ -80,15 +77,6 @@ pub struct CompressedCircuit { } impl CompressedCircuit { - pub fn from_circuit(hades_optimization: bool) -> Result, Error> - where - C: Circuit, - { - let mut composer = Composer::initialized(); - C::default().circuit(&mut composer)?; - Ok(Self::from_composer(hades_optimization, composer)) - } - pub fn from_composer( hades_optimization: bool, composer: Composer, @@ -210,11 +198,7 @@ impl CompressedCircuit { miniz_oxide::deflate::compress_to_vec(&buf, 10) } - pub fn from_bytes( - pp: &PublicParameters, - label: &[u8], - compressed: &[u8], - ) -> Result<(Prover, Verifier), Error> { + pub fn from_bytes(compressed: &[u8]) -> Result { let compressed = miniz_oxide::inflate::decompress_to_vec(compressed) .map_err(|_| Error::InvalidCompressedCircuit)?; let ( @@ -358,6 +342,6 @@ impl CompressedCircuit { composer.append_custom_gate(constraint); } - Compiler::compile_with_composer(pp, label, &composer) + Ok(composer) } } diff --git a/tests/composer.rs b/tests/composer.rs index 56ee9936..a8fee548 100644 --- a/tests/composer.rs +++ b/tests/composer.rs @@ -84,11 +84,11 @@ fn circuit_with_all_gates() { let (prover, verifier) = Compiler::compile::(&pp, label) .expect("failed to compile circuit"); - let compressed = Compiler::compress::() - .expect("failed to compress circuit"); + let compressed = + DummyCircuit::compress().expect("failed to compress circuit"); let (decompressed_prover, decompressed_verifier) = - Compiler::decompress(&pp, label, &compressed).unwrap(); + Compiler::compile_with_compressed(&pp, label, &compressed).unwrap(); let decoded_prover_bytes = decompressed_prover.to_bytes(); let len = prover.serialized_size();