Skip to content

Commit

Permalink
Merge pull request #468 from dusk-network/release-0.7
Browse files Browse the repository at this point in the history
Release dusk-plonk-0.7
  • Loading branch information
CPerezz authored Apr 6, 2021
2 parents 2c700fa + ba56204 commit d175346
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 33 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.7.0] - 06-04-21

### Added

- Implement `VerifierData` structure. [#466](https://github.com/dusk-network/plonk/issues/466)

### Fixed

- Fix circuit debuggger compilation issues. [#488](https://github.com/dusk-network/plonk/issues/488)
- Fix import paths for lib components. [#489](https://github.com/dusk-network/plonk/issues/489)

## [0.6.1] - 12-03-21

### 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.6.1"
version = "0.7.0"
authors = ["Kevaundray Wedderburn <kevtheappdev@gmail.com>",
"Luke Pearson <luke@dusk.network>",
"CPerezz <carlos@dusk.network>"]
Expand Down
123 changes: 100 additions & 23 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use canonical::Canon;
#[cfg(feature = "canon")]
use canonical_derive::Canon;
use dusk_bls12_381::BlsScalar;
use dusk_bytes::{DeserializableSlice, Serializable, Write};
use dusk_jubjub::{JubJubAffine, JubJubScalar};

#[derive(Default, Debug, Clone)]
Expand All @@ -41,6 +42,68 @@ impl From<JubJubAffine> for PublicInputValue {
}
}

#[derive(Debug, Clone)]
/// Collection of structs/objects that the Verifier will use in order to
/// de/serialize data needed for Circuit proof verification.
/// This structure can be seen as a link between the [`Circuit`] public input
/// positions and the [`VerifierKey`] that the Verifier needs to use.
pub struct VerifierData {
key: VerifierKey,
pi_pos: Vec<usize>,
}

impl VerifierData {
/// Creates a new [`VerifierData`] from a [`VerifierKey`] and the public
/// input positions of the circuit that it represents.
pub const fn new(key: VerifierKey, pi_pos: Vec<usize>) -> Self {
Self { key, pi_pos }
}

/// Returns a reference to the contained [`VerifierKey`].
pub const fn key(&self) -> &VerifierKey {
&self.key
}

/// Returns a reference to the contained Public Input positions.
pub const fn pi_pos(&self) -> &Vec<usize> {
&self.pi_pos
}

/// Deserializes the [`VerifierData`] into a vector of bytes.
#[allow(unused_must_use)]
pub fn to_var_bytes(&self) -> Vec<u8> {
let mut buff =
vec![
0u8;
VerifierKey::SIZE + u32::SIZE + self.pi_pos.len() * u32::SIZE
];
let mut writer = &mut buff[..];

writer.write(&self.key.to_bytes());
writer.write(&(self.pi_pos.len() as u32).to_bytes());
self.pi_pos.iter().copied().for_each(|pos| {
// Omit the result since disk_bytes write can't fail here
// due to the fact that we're writing into a vector basically.
let _ = writer.write(&(pos as u32).to_bytes());
});

buff
}

/// Serializes [`VerifierData`] from a slice of bytes.
pub fn from_slice(mut buf: &[u8]) -> Result<Self, Error> {
let key = VerifierKey::from_reader(&mut buf)?;
let pos_num = u32::from_reader(&mut buf)? as usize;

let mut pi_pos = vec![];
for _ in 0..pos_num {
pi_pos.push(u32::from_reader(&mut buf)? as usize);
}

Ok(Self { key, pi_pos })
}
}

/// Circuit representation for a gadget with all of the tools that it
/// should implement.
pub trait Circuit
Expand All @@ -56,7 +119,7 @@ where
fn compile(
&mut self,
pub_params: &PublicParameters,
) -> Result<(ProverKey, VerifierKey, Vec<usize>), Error> {
) -> Result<(ProverKey, VerifierData), Error> {
use crate::proof_system::{Prover, Verifier};
// Setup PublicParams
let (ck, _) = pub_params.trim(self.padded_circuit_size())?;
Expand All @@ -74,10 +137,12 @@ where
prover
.prover_key
.expect("Unexpected error. Missing ProverKey in compilation"),
verifier
.verifier_key
.expect("Unexpected error. Missing VerifierKey in compilation"),
pi_pos,
VerifierData::new(
verifier.verifier_key.expect(
"Unexpected error. Missing VerifierKey in compilation",
),
pi_pos,
),
))
}

Expand Down Expand Up @@ -149,8 +214,7 @@ fn build_pi(
mod tests {
use super::*;
use crate::constraint_system::{ecc::*, StandardComposer};
use crate::proof_system::{ProverKey, VerifierKey};
use dusk_bytes::{DeserializableSlice, Serializable};
use crate::proof_system::ProverKey;

// Implements a circuit that checks:
// 1) a + b = c where C is a PI
Expand Down Expand Up @@ -229,46 +293,52 @@ mod tests {
use std::io::Write;
use tempdir::TempDir;

let tmp = TempDir::new("plonk-keys-test-full").unwrap().into_path();
let tmp = TempDir::new("plonk-keys-test-full")
.expect("IO error")
.into_path();
let pp_path = tmp.clone().join("pp_testcirc");
let pk_path = tmp.clone().join("pk_testcirc");
let vk_path = tmp.clone().join("vk_testcirc");
let vd_path = tmp.clone().join("vd_testcirc");

// Generate CRS
let pp_p = PublicParameters::setup(1 << 12, &mut rand::thread_rng())?;
File::create(&pp_path)
.and_then(|mut f| f.write(pp_p.to_raw_var_bytes().as_slice()))
.unwrap();
.expect("IO error");

// Read PublicParameters
let pp = fs::read(pp_path).unwrap();
let pp = fs::read(pp_path).expect("IO error");
let pp =
unsafe { PublicParameters::from_slice_unchecked(pp.as_slice()) };

// Initialize the circuit
let mut circuit = TestCircuit::default();

// Compile the circuit
let (pk_p, vk_p, pi_pos) = circuit.compile(&pp)?;
let (pk_p, og_verifier_data) = circuit.compile(&pp)?;

// Write the keys
File::create(&pk_path)
.and_then(|mut f| f.write(pk_p.to_var_bytes().as_slice()))
.unwrap();
File::create(&vk_path)
.and_then(|mut f| f.write(&vk_p.to_bytes()))
.unwrap();
.expect("IO error");

// Read ProverKey
let pk = fs::read(pk_path).unwrap();
let pk = fs::read(pk_path).expect("IO error");
let pk = ProverKey::from_slice(pk.as_slice())?;

// Read VerifierKey
let vk = fs::read(vk_path).unwrap();
let vk = VerifierKey::from_slice(vk.as_slice())?;

assert_eq!(pk, pk_p);
assert_eq!(vk, vk_p);

// Store the VerifierData just for the verifier side:
// (You could also store pi_pos and VerifierKey sepparatedly).
File::create(&vd_path)
.and_then(|mut f| {
f.write(og_verifier_data.to_var_bytes().as_slice())
})
.expect("IO error");
let vd = fs::read(vd_path).expect("IO error");
let verif_data = VerifierData::from_slice(vd.as_slice())?;
assert_eq!(og_verifier_data.key(), verif_data.key());
assert_eq!(og_verifier_data.pi_pos(), verif_data.pi_pos());

// Prover POV
let proof = {
Expand Down Expand Up @@ -297,6 +367,13 @@ mod tests {
.into(),
];

verify_proof(&pp, &vk, &proof, &public_inputs2, &pi_pos, b"Test")
verify_proof(
&pp,
&verif_data.key(),
&proof,
&public_inputs2,
&verif_data.pi_pos(),
b"Test",
)
}
}
4 changes: 3 additions & 1 deletion src/commitment_scheme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ trait CommitmentScheme {
type Proof;
}

pub mod kzg10;
pub(crate) mod kzg10;
pub use kzg10::key;
pub use kzg10::PublicParameters;
3 changes: 2 additions & 1 deletion src/constraint_system/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ impl StandardComposer {
let f_3 = f - BlsScalar::from(3);
f * f_1 * f_2 * f_3
};
let pi_vec = self.construct_dense_pi_vec();
let four = BlsScalar::from(4);
for i in 0..self.n {
let qm = self.q_m[i];
Expand All @@ -436,7 +437,7 @@ impl StandardComposer {
let qlogic = self.q_logic[i];
let qfixed = self.q_fixed_group_add[i];
let qvar = self.q_variable_group_add[i];
let pi = self.construct_dense_pi_vec();
let pi = pi_vec[i];

let a = w_l[i];
let a_next = w_l[(i + 1) % self.n];
Expand Down
3 changes: 2 additions & 1 deletion src/constraint_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ pub mod logic;
pub mod range;

pub use composer::StandardComposer;
pub use variable::{Variable, WireData};
pub use variable::Variable;
pub(crate) use variable::WireData;
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@

mod bit_iterator;
pub mod circuit;
mod commitment_scheme;
pub mod commitment_scheme;
pub mod constraint_system;
mod error;
pub mod error;
mod fft;
mod permutation;
pub mod prelude;
mod proof_system;
pub mod proof_system;
mod transcript;
mod util;

Expand Down
6 changes: 3 additions & 3 deletions src/proof_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ pub(crate) mod linearisation_poly;
mod preprocess;

/// Represents a PLONK Proof
pub mod proof;
pub(crate) mod proof;
/// Represents a PLONK Prover
pub mod prover;
pub(crate) mod prover;
pub(crate) mod quotient_poly;
/// Represents a PLONK Verifier
pub mod verifier;
pub(crate) mod verifier;
pub(crate) mod widget;

pub use proof::Proof;
Expand Down

0 comments on commit d175346

Please sign in to comment.