Skip to content

Commit

Permalink
Merge pull request #323 from dusk-network/pub_srs_size_proof
Browse files Browse the repository at this point in the history
Impl Canon & refactor byte conversions for Proof & ProofEvaluations
  • Loading branch information
CPerezz authored Nov 25, 2020
2 parents f5b9427 + 741c3c8 commit 36ee6cb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.5] - 25-11-20
### Changed
- Changed `Proof` & `ProofEvaluations` byte conversion fn signatures.
### Added
- Implemented `Canon` for `Proof`.

## [0.3.4] - 02-11-20
### Changed
- dusk-jubjub update to `v0.5.0` with API renaming
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dusk-plonk"
version = "0.3.4"
version = "0.3.5"
authors = ["Kevaundray Wedderburn <[email protected]>",
"Luke Pearson <[email protected]>",
"CPerezz <[email protected]>"]
Expand Down Expand Up @@ -33,6 +33,8 @@ anyhow = "1.0.32"
dusk-jubjub = "0.5.0"
thiserror = "1.0"
serde = "1.0"
# Dusk related deps for WASMI serde
canonical = {version = "0.4", optional = true}

[dev-dependencies]
rand = "0.7.0"
Expand All @@ -44,4 +46,4 @@ rustdoc-args = [ "--html-in-header", "katex-header.html" ]
nightly = []
trace = []
trace-print = ["trace"]
canon = ["dusk-bls12_381/canon", "dusk-jubjub/canon"]
canon = ["dusk-bls12_381/canon", "dusk-jubjub/canon", "canonical"]
42 changes: 20 additions & 22 deletions src/proof_system/linearisation_poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Evaluations {
}

/// Proof Evaluations is a subset of all of the evaluations. These evaluations will be added to the proof
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct ProofEvaluations {
// Evaluation of the witness polynomial for the left wire at `z`
pub a_eval: BlsScalar,
Expand Down Expand Up @@ -56,27 +56,25 @@ pub struct ProofEvaluations {

impl ProofEvaluations {
/// Serialises a Proof Evaluation struct to bytes
pub fn to_bytes(&self) -> Vec<u8> {
use crate::serialisation::write_scalar;
pub fn to_bytes(&self) -> [u8; ProofEvaluations::serialised_size()] {
let mut bytes = [0u8; ProofEvaluations::serialised_size()];

let mut bytes = Vec::with_capacity(ProofEvaluations::serialised_size());

write_scalar(&self.a_eval, &mut bytes);
write_scalar(&self.b_eval, &mut bytes);
write_scalar(&self.c_eval, &mut bytes);
write_scalar(&self.d_eval, &mut bytes);
write_scalar(&self.a_next_eval, &mut bytes);
write_scalar(&self.b_next_eval, &mut bytes);
write_scalar(&self.d_next_eval, &mut bytes);
write_scalar(&self.q_arith_eval, &mut bytes);
write_scalar(&self.q_c_eval, &mut bytes);
write_scalar(&self.q_l_eval, &mut bytes);
write_scalar(&self.q_r_eval, &mut bytes);
write_scalar(&self.left_sigma_eval, &mut bytes);
write_scalar(&self.right_sigma_eval, &mut bytes);
write_scalar(&self.out_sigma_eval, &mut bytes);
write_scalar(&self.lin_poly_eval, &mut bytes);
write_scalar(&self.perm_eval, &mut bytes);
bytes[0..32].copy_from_slice(&self.a_eval.to_bytes()[..]);
bytes[32..64].copy_from_slice(&self.b_eval.to_bytes()[..]);
bytes[64..96].copy_from_slice(&self.c_eval.to_bytes()[..]);
bytes[96..128].copy_from_slice(&self.d_eval.to_bytes()[..]);
bytes[128..160].copy_from_slice(&self.a_next_eval.to_bytes()[..]);
bytes[160..192].copy_from_slice(&self.b_next_eval.to_bytes()[..]);
bytes[192..224].copy_from_slice(&self.d_next_eval.to_bytes()[..]);
bytes[224..256].copy_from_slice(&self.q_arith_eval.to_bytes()[..]);
bytes[256..288].copy_from_slice(&self.q_c_eval.to_bytes()[..]);
bytes[288..320].copy_from_slice(&self.q_l_eval.to_bytes()[..]);
bytes[320..352].copy_from_slice(&self.q_r_eval.to_bytes()[..]);
bytes[352..384].copy_from_slice(&self.left_sigma_eval.to_bytes()[..]);
bytes[384..416].copy_from_slice(&self.right_sigma_eval.to_bytes()[..]);
bytes[416..448].copy_from_slice(&self.out_sigma_eval.to_bytes()[..]);
bytes[448..480].copy_from_slice(&self.lin_poly_eval.to_bytes()[..]);
bytes[480..512].copy_from_slice(&self.perm_eval.to_bytes()[..]);

bytes
}
Expand Down Expand Up @@ -126,7 +124,7 @@ impl ProofEvaluations {
Ok(proof_evals)
}

pub(crate) const fn serialised_size() -> usize {
pub const fn serialised_size() -> usize {
const NUM_SCALARS: usize = 16;
const SCALAR_SIZE: usize = 32;
NUM_SCALARS * SCALAR_SIZE
Expand Down
63 changes: 44 additions & 19 deletions src/proof_system/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ use merlin::Transcript;
use serde::de::Visitor;
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "canon")]
use canonical::{Canon, InvalidEncoding, Sink, Source, Store};

/// Byte-size of a serialised `Proof`.
pub const PROOF_SIZE: usize = Proof::serialised_size();

/// A Proof is a composition of `Commitments` to the witness, permutation,
/// quotient, shifted and opening polynomials as well as the
/// `ProofEvaluations`.
///
/// It's main goal is to have a `verify()` method attached which contains the
/// logic of the operations that the `Verifier` will need to do in order to
/// formally verify the `Proof`.
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Proof {
/// Commitment to the witness polynomial for the left wires.
pub a_comm: Commitment,
Expand Down Expand Up @@ -62,29 +68,47 @@ pub struct Proof {

impl_serde!(Proof);

impl Proof {
/// Serialises a Proof struct
pub fn to_bytes(&self) -> Vec<u8> {
use crate::serialisation::write_commitment;
#[cfg(feature = "canon")]
impl<S: Store> Canon<S> for Proof {
fn write(&self, sink: &mut impl Sink<S>) -> Result<(), S::Error> {
sink.copy_bytes(&self.to_bytes());
Ok(())
}

let mut bytes = Vec::with_capacity(Proof::serialised_size());
fn read(source: &mut impl Source<S>) -> Result<Self, S::Error> {
let mut bytes = [0u8; PROOF_SIZE];
bytes.copy_from_slice(source.read_bytes(PROOF_SIZE));
match Proof::from_bytes(&bytes) {
Ok(proof) => Ok(proof),
_ => Err(InvalidEncoding.into()),
}
}

write_commitment(&self.a_comm, &mut bytes);
write_commitment(&self.b_comm, &mut bytes);
write_commitment(&self.c_comm, &mut bytes);
write_commitment(&self.d_comm, &mut bytes);
write_commitment(&self.z_comm, &mut bytes);
write_commitment(&self.t_1_comm, &mut bytes);
write_commitment(&self.t_2_comm, &mut bytes);
write_commitment(&self.t_3_comm, &mut bytes);
write_commitment(&self.t_4_comm, &mut bytes);
write_commitment(&self.w_z_comm, &mut bytes);
write_commitment(&self.w_zw_comm, &mut bytes);
fn encoded_len(&self) -> usize {
PROOF_SIZE
}
}

bytes.extend(self.evaluations.to_bytes());
impl Proof {
/// Serialises a Proof struct
pub fn to_bytes(&self) -> [u8; PROOF_SIZE] {
let mut bytes = [0u8; PROOF_SIZE];
bytes[0..48].copy_from_slice(&self.a_comm.0.to_compressed()[..]);
bytes[48..96].copy_from_slice(&self.b_comm.0.to_compressed()[..]);
bytes[96..144].copy_from_slice(&self.c_comm.0.to_compressed()[..]);
bytes[144..192].copy_from_slice(&self.d_comm.0.to_compressed()[..]);
bytes[192..240].copy_from_slice(&self.z_comm.0.to_compressed()[..]);
bytes[240..288].copy_from_slice(&self.t_1_comm.0.to_compressed()[..]);
bytes[288..336].copy_from_slice(&self.t_2_comm.0.to_compressed()[..]);
bytes[336..384].copy_from_slice(&self.t_3_comm.0.to_compressed()[..]);
bytes[384..432].copy_from_slice(&self.t_4_comm.0.to_compressed()[..]);
bytes[432..480].copy_from_slice(&self.w_z_comm.0.to_compressed()[..]);
bytes[480..528].copy_from_slice(&self.w_zw_comm.0.to_compressed()[..]);
bytes[528..PROOF_SIZE].copy_from_slice(&self.evaluations.to_bytes()[..]);

bytes
}

/// Deserialises a Proof struct
pub fn from_bytes(bytes: &[u8]) -> Result<Proof, Error> {
use crate::serialisation::read_commitment;
Expand Down Expand Up @@ -120,7 +144,8 @@ impl Proof {
Ok(proof)
}

const fn serialised_size() -> usize {
/// Returns the serialised size of a [`Proof`] object.
pub const fn serialised_size() -> usize {
const NUM_COMMITMENTS: usize = 11;
const COMMITMENT_SIZE: usize = 48;
(NUM_COMMITMENTS * COMMITMENT_SIZE) + ProofEvaluations::serialised_size()
Expand Down

0 comments on commit 36ee6cb

Please sign in to comment.