From 6606427b7eb4246816b11a80ce47c367a81cb295 Mon Sep 17 00:00:00 2001 From: moana Date: Thu, 21 Dec 2023 14:54:51 +0100 Subject: [PATCH] Improve compress API Resolves #804 --- CHANGELOG.md | 2 ++ src/compiler.rs | 22 +++++----------------- src/composer/circuit.rs | 18 ++++++++++++++++-- src/composer/compress.rs | 22 +++------------------- tests/composer.rs | 6 +++--- 5 files changed, 29 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93b64fae..ac477283 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 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..55ff0c26 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -4,9 +4,6 @@ // // 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}; @@ -61,31 +58,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( + pub fn compile_with_compressed( pp: &PublicParameters, label: &[u8], compressed: &[u8], ) -> Result<(Prover, Verifier), Error> { - CompressedCircuit::from_bytes(pp, label, compressed) + let composer = CompressedCircuit::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/circuit.rs b/src/composer/circuit.rs index 20272ff2..3e29673a 100644 --- a/src/composer/circuit.rs +++ b/src/composer/circuit.rs @@ -4,9 +4,9 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use crate::error::Error; +use crate::prelude::Error; -use super::Composer; +use super::{Composer, CompressedCircuit}; /// Circuit implementation that can be proved by a Composer /// @@ -23,4 +23,18 @@ pub trait Circuit: Default { Err(_) => 0, } } + + /// Return a bytes representation of a compressed circuit, capable of + /// generating its prover and verifier instances. + #[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();