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 ab6ce8d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 46 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 (de-)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
25 changes: 6 additions & 19 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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(
/// [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,
Expand Down
9 changes: 6 additions & 3 deletions src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Self, Error> {
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();
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 23 additions & 2 deletions src/composer/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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<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 ab6ce8d

Please sign in to comment.