Skip to content

Commit

Permalink
Trim size as param (#314)
Browse files Browse the repository at this point in the history
* Add getter and setter for trim_size

On the cases where we have variable-size circuits,
we need a way to use different trim_sizes for our Circuit
instance. With the previous API this was not possible.
  • Loading branch information
CPerezz authored Oct 5, 2020
1 parent c900d2c commit 228d0ea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.1] - 05-10-20
### Added
- Method to change the `trim_params_size` for the `Circuit` trait.

## [0.3.0] - 05-10-20
### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dusk-plonk"
version = "0.3.0"
version = "0.3.1"
authors = ["Kevaundray Wedderburn <kevtheappdev@gmail.com>",
"Luke Pearson <luke@dusk.network>",
"CPerezz <carlos@dusk.network>"]
Expand Down
24 changes: 17 additions & 7 deletions src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
fn compile(&mut self, pub_params: &PublicParameters) -> Result<(ProverKey, VerifierKey)> {
use crate::proof_system::{Prover, Verifier};
// Setup PublicParams
let (ck, _) = pub_params.trim(self.trim_size())?;
let (ck, _) = pub_params.trim(self.get_trim_size())?;
// Generate & save `ProverKey` with some random values.
let mut prover = Prover::new(b"CircuitCompilation");
self.gadget(prover.mut_cs())?;
Expand Down Expand Up @@ -86,7 +86,7 @@ where

/// Build PI vector for Proof verifications.
fn build_pi(&self, pub_inputs: &[PublicInput]) -> Result<Vec<BlsScalar>> {
let mut pi = vec![BlsScalar::zero(); self.trim_size()];
let mut pi = vec![BlsScalar::zero(); self.get_trim_size()];
pub_inputs
.iter()
.zip(self.get_pi_positions())
Expand All @@ -108,7 +108,11 @@ where
/// Returns the size at which we trim the `PublicParameters`
/// to compile the circuit or perform proving/verification
/// actions.
fn trim_size(&self) -> usize;
fn get_trim_size(&self) -> usize;

/// Sets the trim size that will be used by this circuit when
/// trimming the Public Parameters.
fn set_trim_size(&mut self, size: usize);

/// Generates a proof using the provided `CircuitInputs` & `ProverKey` instances.
fn gen_proof(
Expand All @@ -118,7 +122,7 @@ where
transcript_initialisation: &'static [u8],
) -> Result<Proof> {
use crate::proof_system::Prover;
let (ck, _) = pub_params.trim(self.trim_size())?;
let (ck, _) = pub_params.trim(self.get_trim_size())?;
// New Prover instance
let mut prover = Prover::new(transcript_initialisation);
// Fill witnesses for Prover
Expand All @@ -138,7 +142,7 @@ where
pub_inputs: &[PublicInput],
) -> Result<()> {
use crate::proof_system::Verifier;
let (_, vk) = pub_params.trim(self.trim_size())?;
let (_, vk) = pub_params.trim(self.get_trim_size())?;
// New Verifier instance
let mut verifier = Verifier::new(transcript_initialisation);
// Fill witnesses for Verifier
Expand Down Expand Up @@ -176,13 +180,15 @@ mod tests {
pub struct TestCircuit<'a> {
inputs: Option<&'a [BlsScalar]>,
pi_positions: Vec<PublicInput>,
trim_size: usize,
}

impl<'a> Default for TestCircuit<'a> {
fn default() -> Self {
TestCircuit {
inputs: None,
pi_positions: vec![],
trim_size: 1 << 9,
}
}
}
Expand Down Expand Up @@ -229,8 +235,12 @@ mod tests {
}

#[inline]
fn trim_size(&self) -> usize {
1 << 9
fn get_trim_size(&self) -> usize {
self.trim_size
}

fn set_trim_size(&mut self, size: usize) {
self.trim_size = size;
}

fn get_mut_pi_positions(&mut self) -> &mut Vec<PublicInput> {
Expand Down

0 comments on commit 228d0ea

Please sign in to comment.