Skip to content

Commit

Permalink
Improve compress API
Browse files Browse the repository at this point in the history
Resolves #804
  • Loading branch information
moCello committed Dec 21, 2023
1 parent 9f6b758 commit 6606427
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 41 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -555,6 +556,7 @@ is necessary since `rkyv/validation` was required as a bound.

<!-- ISSUES -->
[#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
Expand Down
22 changes: 5 additions & 17 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<C>() -> Result<Vec<u8>, Error>
where
C: Circuit,
{
let hades_optimization = true;
CompressedCircuit::from_circuit::<C>(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,
Expand Down
18 changes: 16 additions & 2 deletions src/composer/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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<Vec<u8>, Error> {
let mut composer = Composer::initialized();
Self::default().circuit(&mut composer)?;

let hades_optimization = true;
Ok(CompressedCircuit::from_composer(
hades_optimization,
composer,
))
}
}
22 changes: 3 additions & 19 deletions src/composer/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -80,15 +77,6 @@ pub struct CompressedCircuit {
}

impl CompressedCircuit {
pub fn from_circuit<C>(hades_optimization: bool) -> Result<Vec<u8>, 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,
Expand Down Expand Up @@ -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<Composer, Error> {
let compressed = miniz_oxide::inflate::decompress_to_vec(compressed)
.map_err(|_| Error::InvalidCompressedCircuit)?;
let (
Expand Down Expand Up @@ -358,6 +342,6 @@ impl CompressedCircuit {
composer.append_custom_gate(constraint);
}

Compiler::compile_with_composer(pp, label, &composer)
Ok(composer)
}
}
6 changes: 3 additions & 3 deletions tests/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ fn circuit_with_all_gates() {
let (prover, verifier) = Compiler::compile::<DummyCircuit>(&pp, label)
.expect("failed to compile circuit");

let compressed = Compiler::compress::<DummyCircuit>()
.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();
Expand Down

0 comments on commit 6606427

Please sign in to comment.