Skip to content

Commit

Permalink
Implements inner_snark_pk and outer_snark_pk parameter fetching and l…
Browse files Browse the repository at this point in the history
…oading
  • Loading branch information
howardwu committed May 27, 2020
1 parent 7730f5b commit 831eaf1
Show file tree
Hide file tree
Showing 46 changed files with 449 additions and 127 deletions.
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.idea/
**.DS_Store
**storage_db
dpc/src/parameters/inner_snark.params
dpc/src/parameters/outer_snark.params
**inner_snark_pk.params
**outer_snark_pk.params
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
**/target
**.DS_Store
**storage_db
dpc/src/parameters/inner_snark.params
dpc/src/parameters/outer_snark.params
**inner_snark_pk.params
**outer_snark_pk.params
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions dpc/src/dpc/base_dpc/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::dpc::base_dpc::BaseDPCComponents;
use snarkos_models::{algorithms::SNARK, parameters::Parameter};
use snarkos_models::{algorithms::SNARK, parameters::Parameters};
use snarkos_parameters::*;
use snarkos_utilities::bytes::FromBytes;

Expand All @@ -22,21 +22,21 @@ impl<C: BaseDPCComponents> CircuitParameters<C> {
// TODO (howardwu): Inspect what is going on with predicate_verification_key_commitment.
pub fn load() -> IoResult<Self> {
let account_commitment: C::AccountCommitment =
From::from(FromBytes::read(AccountCommitmentParameters::load_bytes().as_slice())?);
From::from(FromBytes::read(AccountCommitmentParameters::load_bytes()?.as_slice())?);
let account_signature: C::AccountSignature =
From::from(FromBytes::read(AccountSignatureParameters::load_bytes().as_slice())?);
From::from(FromBytes::read(AccountSignatureParameters::load_bytes()?.as_slice())?);
let record_commitment: C::RecordCommitment =
From::from(FromBytes::read(RecordCommitmentParameters::load_bytes().as_slice())?);
From::from(FromBytes::read(RecordCommitmentParameters::load_bytes()?.as_slice())?);
let predicate_verification_key_commitment: C::PredicateVerificationKeyCommitment =
From::from(FromBytes::read(vec![].as_slice())?);
let predicate_verification_key_hash: C::PredicateVerificationKeyHash =
From::from(FromBytes::read(PredicateVKCRHParameters::load_bytes().as_slice())?);
From::from(FromBytes::read(PredicateVKCRHParameters::load_bytes()?.as_slice())?);
let local_data_commitment: C::LocalDataCommitment =
From::from(FromBytes::read(LocalDataCommitmentParameters::load_bytes().as_slice())?);
From::from(FromBytes::read(LocalDataCommitmentParameters::load_bytes()?.as_slice())?);
let value_commitment: C::ValueCommitment =
From::from(FromBytes::read(ValueCommitmentParameters::load_bytes().as_slice())?);
From::from(FromBytes::read(ValueCommitmentParameters::load_bytes()?.as_slice())?);
let serial_number_nonce: C::SerialNumberNonceCRH = From::from(FromBytes::read(
SerialNumberNonceCRHParameters::load_bytes().as_slice(),
SerialNumberNonceCRHParameters::load_bytes()?.as_slice(),
)?);

Ok(Self {
Expand All @@ -63,9 +63,9 @@ impl<C: BaseDPCComponents> PredicateSNARKParameters<C> {
// TODO (howardwu): Why are we not preparing the VK here?
pub fn load() -> IoResult<Self> {
let proving_key: <C::PredicateSNARK as SNARK>::ProvingParameters =
From::from(FromBytes::read(PredicateSNARKPKParameters::load_bytes().as_slice())?);
From::from(FromBytes::read(PredicateSNARKPKParameters::load_bytes()?.as_slice())?);
let verification_key = From::from(<C::PredicateSNARK as SNARK>::VerificationParameters::read(
PredicateSNARKVKParameters::load_bytes().as_slice(),
PredicateSNARKVKParameters::load_bytes()?.as_slice(),
)?);

Ok(Self {
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<C: BaseDPCComponents> PublicParameters<C> {

let inner_snark_vk: <C::InnerSNARK as SNARK>::VerificationParameters =
From::from(<C::InnerSNARK as SNARK>::VerificationParameters::read(
InnerSNARKVKParameters::load_bytes().as_slice(),
InnerSNARKVKParameters::load_bytes()?.as_slice(),
)?);

(inner_snark_pk, inner_snark_vk.into())
Expand All @@ -182,7 +182,7 @@ impl<C: BaseDPCComponents> PublicParameters<C> {

let outer_snark_vk: <C::OuterSNARK as SNARK>::VerificationParameters =
From::from(<C::OuterSNARK as SNARK>::VerificationParameters::read(
OuterSNARKVKParameters::load_bytes().as_slice(),
OuterSNARKVKParameters::load_bytes()?.as_slice(),
)?);

(outer_snark_pk, outer_snark_vk.into())
Expand All @@ -204,7 +204,7 @@ impl<C: BaseDPCComponents> PublicParameters<C> {
let inner_snark_pk = None;
let inner_snark_vk: <C::InnerSNARK as SNARK>::VerificationParameters =
From::from(<C::InnerSNARK as SNARK>::VerificationParameters::read(
InnerSNARKVKParameters::load_bytes().as_slice(),
InnerSNARKVKParameters::load_bytes()?.as_slice(),
)?);
(inner_snark_pk, inner_snark_vk.into())
};
Expand All @@ -213,7 +213,7 @@ impl<C: BaseDPCComponents> PublicParameters<C> {
let outer_snark_pk = None;
let outer_snark_vk: <C::OuterSNARK as SNARK>::VerificationParameters =
From::from(<C::OuterSNARK as SNARK>::VerificationParameters::read(
OuterSNARKVKParameters::load_bytes().as_slice(),
OuterSNARKVKParameters::load_bytes()?.as_slice(),
)?);
(outer_snark_pk, outer_snark_vk.into())
};
Expand Down
4 changes: 2 additions & 2 deletions dpc/src/test_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use snarkos_models::{
algorithms::CRH,
dpc::{DPCScheme, Record},
objects::{AccountScheme, Transaction},
parameters::Parameter,
parameters::Parameters,
};
use snarkos_objects::Account;
use snarkos_parameters::LedgerMerkleTreeParameters;
Expand Down Expand Up @@ -37,7 +37,7 @@ pub fn setup_or_load_parameters<R: Rng>(
<InstantiatedDPC as DPCScheme<MerkleTreeLedger>>::Parameters,
) {
// TODO (howardwu): Resolve this inconsistency on import structure with a new model once MerkleParameters are refactored.
let crh_parameters = <MerkleTreeCRH as CRH>::Parameters::read(&LedgerMerkleTreeParameters::load_bytes()[..])
let crh_parameters = <MerkleTreeCRH as CRH>::Parameters::read(&LedgerMerkleTreeParameters::load_bytes().unwrap()[..])
.expect("read bytes as hash for MerkleParameters in ledger");
let merkle_tree_hash_parameters = <CommitmentMerkleParameters as MerkleParameters>::H::from(crh_parameters);
let ledger_merkle_tree_parameters = From::from(merkle_tree_hash_parameters);
Expand Down
1 change: 1 addition & 0 deletions errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = "2018"

[dependencies]
bincode = { version = "1.2.0" }
curl = { version = "0.4.29" }
hex = { version = "0.4.2" }
jsonrpc-core = { version = "14.0.5" }
rocksdb = { version = "0.13.0" }
Expand Down
10 changes: 10 additions & 0 deletions errors/src/dpc/dpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
algorithms::{CRHError, CommitmentError, PRFError, SNARKError, SignatureError},
dpc::{BindingSignatureError, LedgerError},
objects::AccountError,
parameters::ParametersError
};

#[derive(Debug, Error)]
Expand Down Expand Up @@ -33,6 +34,9 @@ pub enum DPCError {
#[error("missing outer snark proving parameters")]
MissingOuterSnarkProvingParameters,

#[error("{}", _0)]
ParametersError(ParametersError),

#[error("{}", _0)]
PRFError(PRFError),

Expand Down Expand Up @@ -85,6 +89,12 @@ impl From<SignatureError> for DPCError {
}
}

impl From<ParametersError> for DPCError {
fn from(error: ParametersError) -> Self {
DPCError::ParametersError(error)
}
}

impl From<SNARKError> for DPCError {
fn from(error: SNARKError) -> Self {
DPCError::SNARKError(error)
Expand Down
1 change: 1 addition & 0 deletions errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ pub mod gadgets;
pub mod network;
pub mod node;
pub mod objects;
pub mod parameters;
pub mod rpc;
pub mod storage;
2 changes: 2 additions & 0 deletions errors/src/parameters/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod parameters;
pub use self::parameters::*;
34 changes: 34 additions & 0 deletions errors/src/parameters/parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::fmt::Debug;

#[derive(Debug, Error)]
pub enum ParametersError {
#[error("{}: {}", _0, _1)]
Crate(&'static str, String),

#[error("{}", _0)]
Message(String),
}

impl From<curl::Error> for ParametersError {
fn from(error: curl::Error) -> Self {
ParametersError::Crate("curl::error", format!("{:?}", error))
}
}

impl From<std::io::Error> for ParametersError {
fn from(error: std::io::Error) -> Self {
ParametersError::Crate("std::io", format!("{:?}", error))
}
}

impl From<std::path::StripPrefixError> for ParametersError {
fn from(error: std::path::StripPrefixError) -> Self {
ParametersError::Crate("std::path", format!("{:?}", error))
}
}

impl From<ParametersError> for std::io::Error {
fn from(error: ParametersError) -> Self {
std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", error))
}
}
10 changes: 10 additions & 0 deletions errors/src/storage/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
algorithms::MerkleError,
objects::{BlockError, TransactionError},
parameters::ParametersError,
};

use bincode;
Expand Down Expand Up @@ -102,6 +103,9 @@ pub enum StorageError {
#[error("{}", _0)]
MerkleError(MerkleError),

#[error("{}", _0)]
ParametersError(ParametersError),

#[error("{}", _0)]
TransactionError(TransactionError),
}
Expand Down Expand Up @@ -154,6 +158,12 @@ impl From<MerkleError> for StorageError {
}
}

impl From<ParametersError> for StorageError {
fn from(error: ParametersError) -> Self {
StorageError::ParametersError(error)
}
}

impl From<TransactionError> for StorageError {
fn from(error: TransactionError) -> Self {
StorageError::TransactionError(error)
Expand Down
4 changes: 2 additions & 2 deletions models/src/parameters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod parameter;
pub use self::parameter::*;
pub mod parameters;
pub use self::parameters::*;
6 changes: 0 additions & 6 deletions models/src/parameters/parameter.rs

This file was deleted.

8 changes: 8 additions & 0 deletions models/src/parameters/parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use snarkos_errors::parameters::ParametersError;

pub trait Parameters {
const SIZE: u64;
const CHECKSUM: &'static str;

fn load_bytes() -> Result<Vec<u8>, ParametersError>;
}
6 changes: 5 additions & 1 deletion parameters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ license = "GPL-3.0"
edition = "2018"

[dependencies]
snarkos-errors = { path = "../errors", version = "0.8.0" }
snarkos-models = { path = "../models", version = "0.8.0" }

curl = { version = "0.4.29" }
lazy_static = { version = "1.4.0" }
lazy-static-include = { version = "2.2.2" }

[dev-dependencies]
snarkos-algorithms = { path = "../algorithms", version = "0.8.0" }
snarkos-dpc = { path = "../dpc", version = "0.8.0" }
snarkos-errors = { path = "../errors", version = "0.8.0" }
snarkos-utilities = { path = "../utilities", version = "0.8.0" }

rand = { version = "0.7" }
4 changes: 2 additions & 2 deletions parameters/examples/inner_snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use snarkos_dpc::base_dpc::{
BaseDPCComponents,
};
use snarkos_errors::dpc::DPCError;
use snarkos_models::{algorithms::SNARK, parameters::Parameter};
use snarkos_models::{algorithms::SNARK, parameters::Parameters};
use snarkos_parameters::LedgerMerkleTreeParameters;
use snarkos_utilities::{
bytes::{FromBytes, ToBytes},
Expand All @@ -25,7 +25,7 @@ pub fn setup<C: BaseDPCComponents>() -> Result<(Vec<u8>, Vec<u8>), DPCError> {

// TODO (howardwu): Resolve this inconsistency on import structure with a new model once MerkleParameters are refactored.
let merkle_tree_hash_parameters: <C::MerkleParameters as MerkleParameters>::H =
From::from(FromBytes::read(&LedgerMerkleTreeParameters::load_bytes()[..])?);
From::from(FromBytes::read(&LedgerMerkleTreeParameters::load_bytes()?[..])?);
let ledger_merkle_tree_parameters = From::from(merkle_tree_hash_parameters);

let circuit_parameters = CircuitParameters::<C>::load()?;
Expand Down
Loading

0 comments on commit 831eaf1

Please sign in to comment.