Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add size method to Circuit trait #768

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `size` method to the `Circuit` trait [#767]

### Removed

- Remove public parametes as parameters for circuit compression [#767]

## [0.15.0] - 2023-08-30

### Fixed
Expand Down
37 changes: 18 additions & 19 deletions src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {
since = "13.0",
note = "this function is meant for internal use. call `initialized` instead"
)]
fn uninitialized(capacity: usize) -> Self;
fn uninitialized() -> Self;

/// Constraints count
fn constraints(&self) -> usize;
Expand All @@ -82,6 +82,22 @@ pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {
/// PLONK runtime controller
fn runtime(&mut self) -> &mut Runtime;

/// Initialize the constraint system with dummy gates
fn initialized() -> Self {
#[allow(deprecated)]
let mut slf = Self::uninitialized();

let zero = slf.append_witness(0);
let one = slf.append_witness(1);

slf.assert_equal_constant(zero, 0, None);
slf.assert_equal_constant(one, 1, None);

slf.append_dummy_gates();

slf
}

/// Allocate a witness value into the composer and return its index.
fn append_witness<W: Into<BlsScalar>>(&mut self, witness: W) -> Witness {
let witness = witness.into();
Expand Down Expand Up @@ -378,23 +394,6 @@ pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {
Ok(WitnessPoint::new(acc_x, acc_y))
}

/// Initialize the constraint system with dummy gates
fn initialized(capacity: usize) -> Self {
#[allow(deprecated)]
let mut slf = Self::uninitialized(capacity);

let zero = slf.append_witness(0);
let one = slf.append_witness(1);

slf.assert_equal_constant(zero, 0, None);
slf.assert_equal_constant(one, 1, None);

slf.append_dummy_gates();
slf.append_dummy_gates();

slf
}

/// Append a new width-4 poly gate/constraint.
///
/// The constraint added will enforce the following:
Expand Down Expand Up @@ -1051,7 +1050,7 @@ pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {
where
C: Circuit,
{
let mut builder = Self::initialized(constraints);
let mut builder = Self::initialized();

circuit.circuit(&mut builder)?;

Expand Down
8 changes: 4 additions & 4 deletions src/composer/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ impl ops::Index<Witness> for Builder {
}

impl Composer for Builder {
fn uninitialized(capacity: usize) -> Self {
fn uninitialized() -> Self {
Self {
constraints: Vec::with_capacity(capacity),
constraints: Vec::new(),
public_inputs: HashMap::new(),
witnesses: Vec::with_capacity(capacity),
witnesses: Vec::new(),
perm: Permutation::new(),
runtime: Runtime::with_capacity(capacity),
runtime: Runtime::new(),
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/composer/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ pub trait Circuit: Default {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
C: Composer;

/// Returns the size of the circuit.
fn size<C>(&self) -> usize
where
C: Composer,
{
let mut composer = C::initialized();
match self.circuit(&mut composer) {
Ok(_) => composer.constraints(),
Err(_) => 0,
}
}
}
20 changes: 4 additions & 16 deletions src/composer/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ impl Compiler {
where
C: Circuit,
{
let max_size = Self::max_size(pp);
let mut builder = Builder::initialized(max_size);

let mut builder = Builder::initialized();
C::default().circuit(&mut builder)?;

Self::compile_with_builder(pp, label, &builder)
Expand All @@ -54,9 +52,7 @@ impl Compiler {
where
C: Circuit,
{
let max_size = Self::max_size(pp);
let mut builder = Builder::initialized(max_size);

let mut builder = Builder::initialized();
circuit.circuit(&mut builder)?;

Self::compile_with_builder(pp, label, &builder)
Expand All @@ -65,14 +61,11 @@ impl Compiler {
/// 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>
pub fn compress<C>() -> Result<Vec<u8>, Error>
where
C: Circuit,
{
compress::CompressedCircuit::from_circuit::<C>(
pp,
compress::Version::V2,
)
compress::CompressedCircuit::from_circuit::<C>(compress::Version::V2)
}

/// Generates a [Prover] and [Verifier] from a buffer created by
Expand All @@ -85,11 +78,6 @@ impl Compiler {
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
Expand Down
24 changes: 10 additions & 14 deletions src/composer/compiler/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,15 @@ pub struct CompressedCircuit {
witnesses: usize,
scalars: Vec<[u8; BlsScalar::SIZE]>,
polynomials: Vec<CompressedPolynomial>,
circuit: Vec<CompressedConstraint>,
constraints: Vec<CompressedConstraint>,
}

impl CompressedCircuit {
pub fn from_circuit<C>(
pp: &PublicParameters,
version: Version,
) -> Result<Vec<u8>, Error>
pub fn from_circuit<C>(version: Version) -> Result<Vec<u8>, Error>
where
C: Circuit,
{
let max_size = Compiler::max_size(pp);
let mut builder = Builder::initialized(max_size);
let mut builder = Builder::initialized();
C::default().circuit(&mut builder)?;
Ok(Self::from_builder(version, builder))
}
Expand All @@ -114,11 +110,11 @@ impl CompressedCircuit {
let witnesses = builder.witnesses.len();
let polynomials = builder.constraints;

let circuit = polynomials.into_iter();
let constraints = polynomials.into_iter();
let mut scalars = version.into_scalars();
let base_scalars_len = scalars.len();
let mut polynomials = HashMap::new();
let circuit = circuit
let constraints = constraints
.map(
|Polynomial {
q_m,
Expand Down Expand Up @@ -213,12 +209,12 @@ impl CompressedCircuit {
witnesses,
scalars,
polynomials,
circuit,
constraints,
};
let mut buf = Vec::with_capacity(
1 + compressed.scalars.len() * BlsScalar::SIZE
+ compressed.polynomials.len() * 88
+ compressed.circuit.len() * 40,
+ compressed.constraints.len() * 40,
);
compressed.pack(&mut buf);
miniz_oxide::deflate::compress_to_vec(&buf, 10)
Expand All @@ -239,7 +235,7 @@ impl CompressedCircuit {
witnesses,
scalars,
polynomials,
circuit,
constraints,
},
) = Self::unpack(&compressed)
.map_err(|_| Error::InvalidCompressedCircuit)?;
Expand All @@ -258,7 +254,7 @@ impl CompressedCircuit {
#[allow(deprecated)]
// we use `uninitialized` because the decompressor will also contain the
// dummy constraints, if they were part of the prover when encoding.
let mut builder = Builder::uninitialized(circuit.len());
let mut builder = Builder::uninitialized();

let mut pi = 0;
(0..witnesses).for_each(|_| {
Expand All @@ -274,7 +270,7 @@ impl CompressedCircuit {
w_d,
w_o,
},
) in circuit.into_iter().enumerate()
) in constraints.into_iter().enumerate()
{
let CompressedPolynomial {
q_m,
Expand Down
7 changes: 7 additions & 0 deletions src/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ impl Debugger {
}
}

pub(crate) fn new() -> Self {
Self {
witnesses: Vec::new(),
constraints: Vec::new(),
}
}

pub(crate) fn with_capacity(capacity: usize) -> Self {
Self {
witnesses: Vec::with_capacity(capacity),
Expand Down
9 changes: 9 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ pub struct Runtime {
}

impl Runtime {
/// Create a new PLONK runtime
#[allow(unused_variables)]
pub fn new() -> Self {
Self {
#[cfg(feature = "debug")]
debugger: Debugger::new(),
}
}

/// Create a new PLONK runtime with the provided capacity
#[allow(unused_variables)]
pub fn with_capacity(capacity: usize) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion tests/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn circuit_with_all_gates() {
let (prover, verifier) = Compiler::compile::<DummyCircuit>(&pp, label)
.expect("failed to compile circuit");

let compressed = Compiler::compress::<DummyCircuit>(&pp)
let compressed = Compiler::compress::<DummyCircuit>()
.expect("failed to compress circuit");

let (decompressed_prover, decompressed_verifier) =
Expand Down
Loading