Skip to content

Commit

Permalink
Merge c4fda09 into 1f8374f
Browse files Browse the repository at this point in the history
  • Loading branch information
vlopes11 authored Jun 13, 2023
2 parents 1f8374f + c4fda09 commit 4c24a8e
Show file tree
Hide file tree
Showing 14 changed files with 569 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add and restructure tests for boolean and select components [#731]
- Add tests for `gate_add` and `gate_mul` [#736]
- Add and restructure tests for `component_decomposition` [#738]
- Add `Compile::compress` and `Compile::decompress` [#752]

### Removed

Expand Down
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ dusk-bls12_381 = {version = "0.11", default-features = false, features = ["group
dusk-jubjub = {version = "0.12", default-features = false}
itertools = {version = "0.9", default-features = false}
hashbrown = {version = "0.9", default-features=false, features = ["ahash"]}
msgpacker = {version = "0.4", default-features=false, features = ["alloc", "derive"], optional=true}
miniz_oxide = {version = "0.7", default-features=false, features = ["with-alloc"], optional = true}
rayon = {version = "1.3", optional = true}
sha2 = {version = "0.10", default-features = false, optional = true}
cfg-if = "1.0"
# Dusk related deps for WASMI serde
canonical = {version = "0.7", optional = true}
Expand Down Expand Up @@ -52,10 +55,12 @@ std = [
"dusk-jubjub/default",
"itertools/default",
"hashbrown/default",
"msgpacker/std",
"miniz_oxide/std",
"alloc",
"rayon"
]
alloc = ["dusk-bls12_381/alloc"]
alloc = ["dusk-bls12_381/alloc", "msgpacker", "miniz_oxide", "sha2"]
debug = ["dusk-cdf", "backtrace"]
canon = ["dusk-bls12_381/canon", "dusk-jubjub/canon", "canonical", "canonical_derive"]
rkyv-impl = ["dusk-bls12_381/rkyv-impl", "dusk-jubjub/rkyv-impl", "rkyv", "bytecheck"]
Expand Down
8 changes: 4 additions & 4 deletions benches/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ fn run<const DEGREE: usize>(
Compiler::compile::<BenchCircuit<DEGREE>>(&pp, label)
.expect("failed to compile circuit");

let circuit: BenchCircuit<DEGREE> = BenchCircuit::default();

// sanity run
let (proof, public_inputs) = prover
.prove(&mut rand_core::OsRng, &Default::default())
.prove(&mut rand_core::OsRng, &circuit)
.expect("failed to prove");

verifier
Expand All @@ -103,9 +105,7 @@ fn run<const DEGREE: usize>(
let description = format!("Prove 2^{} = {} gates", power, DEGREE);

c.bench_function(description.as_str(), |b| {
b.iter(|| {
black_box(prover.prove(&mut rand_core::OsRng, &Default::default()))
})
b.iter(|| black_box(prover.prove(&mut rand_core::OsRng, &circuit)))
});

let description = format!("Verify 2^{} = {} gates", power, DEGREE);
Expand Down
4 changes: 2 additions & 2 deletions src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {
///
/// A turbo composer expects the first witness to be always present and to
/// be zero.
const ZERO: Witness = Witness::new(0);
const ZERO: Witness = Witness::ZERO;

/// `One` representation inside the constraint system.
///
/// A turbo composer expects the 2nd witness to be always present and to
/// be one.
const ONE: Witness = Witness::new(1);
const ONE: Witness = Witness::ONE;

/// Identity point representation inside the constraint system
const IDENTITY: WitnessPoint = WitnessPoint::new(Self::ZERO, Self::ONE);
Expand Down
76 changes: 62 additions & 14 deletions src/composer/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
//
// 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::constraint_system::{Constraint, Selector, Witness};
use crate::error::Error;
use crate::fft::{EvaluationDomain, Evaluations, Polynomial as FftPolynomial};
use crate::proof_system::preprocess::Polynomials;
use crate::proof_system::{widget, ProverKey};

use super::{Builder, Circuit, Composer, Prover, Verifier};
use super::{Builder, Circuit, Composer, Polynomial, Prover, Verifier};

#[cfg(feature = "alloc")]
mod compress;

/// Generate the arguments to prove and verify a circuit
pub struct Compiler;
Expand All @@ -24,11 +31,16 @@ impl Compiler {
pub fn compile<C>(
pp: &PublicParameters,
label: &[u8],
) -> Result<(Prover<C>, Verifier<C>), Error>
) -> Result<(Prover, Verifier), Error>
where
C: Circuit,
{
Self::compile_with_circuit(pp, label, &Default::default())
let max_size = Self::max_size(pp);
let mut builder = Builder::initialized(max_size);

C::default().circuit(&mut builder)?;

Self::compile_with_builder(pp, label, &builder)
}

/// Create a new arguments set from a given circuit instance
Expand All @@ -38,34 +50,70 @@ impl Compiler {
pp: &PublicParameters,
label: &[u8],
circuit: &C,
) -> Result<(Prover<C>, Verifier<C>), Error>
) -> Result<(Prover, Verifier), Error>
where
C: Circuit,
{
let max_size = (pp.commit_key.powers_of_g.len() - 1) >> 1;
let mut prover = Builder::initialized(max_size);
let max_size = Self::max_size(pp);
let mut builder = Builder::initialized(max_size);

circuit.circuit(&mut prover)?;
circuit.circuit(&mut builder)?;

let n = (prover.constraints() + 6).next_power_of_two();
Self::compile_with_builder(pp, label, &builder)
}

/// Return a bytes representation of a compressed circuit, capable of
/// generating its prover and verifier instances.
#[cfg(feature = "alloc")]
pub fn compress<C>(pp: &PublicParameters) -> Result<Vec<u8>, Error>
where
C: Circuit,
{
compress::CompressedCircuit::from_circuit::<C>(
pp,
compress::Version::V2,
)
}

/// Generates a [Prover] and [Verifier] from a buffer created by
/// [Self::compress].
pub fn decompress(
pp: &PublicParameters,
label: &[u8],
compressed: &[u8],
) -> Result<(Prover, Verifier), Error> {
compress::CompressedCircuit::from_bytes(pp, label, compressed)
}

/// Returns the maximum constraints length for the parameters.
fn max_size(pp: &PublicParameters) -> usize {
(pp.commit_key.powers_of_g.len() - 1) >> 1
}

/// Create a new arguments set from a given circuit instance
///
/// Use the default implementation of the circuit
fn compile_with_builder(
pp: &PublicParameters,
label: &[u8],
builder: &Builder,
) -> Result<(Prover, Verifier), Error> {
let n = (builder.constraints() + 6).next_power_of_two();

let (commit, opening) = pp.trim(n)?;

let (prover, verifier) =
Self::preprocess(label, commit, opening, &prover)?;
Self::preprocess(label, commit, opening, &builder)?;

Ok((prover, verifier))
}

fn preprocess<C>(
fn preprocess(
label: &[u8],
commit_key: CommitKey,
opening_key: OpeningKey,
prover: &Builder,
) -> Result<(Prover<C>, Verifier<C>), Error>
where
C: Circuit,
{
) -> Result<(Prover, Verifier), Error> {
let mut perm = prover.perm.clone();

let constraints = prover.constraints();
Expand Down
Loading

0 comments on commit 4c24a8e

Please sign in to comment.