Skip to content

Commit

Permalink
feat: add load_parameters as a library function (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boyuan Feng authored Jun 18, 2022
1 parent c8f9a3f commit 8917a0d
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 110 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added
- [\#90](https://github.com/Manta-Network/manta-rs/pull/90) Add Binary Compatibility Test for `manta-pay`
- [\#102](https://github.com/Manta-Network/manta-rs/pull/102) Add concrete parameters to `manta-parameters`
- [\#106](https://github.com/Manta-Network/manta-rs/pull/106) Add `load_parameter` as a library function

### Changed

Expand Down
14 changes: 6 additions & 8 deletions manta-pay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ arkworks = [
"ark-std",
]

# Enable Download Parameters
download = ["manta-parameters", "std"]

# Enable Groth16 ZKP System
groth16 = ["ark-groth16", "ark-snark", "arkworks"]

# Enable HTTP Signer Client
http = ["reqwest", "serde"]

# TODO: Enable PLONK ZKP System
# TODO: plonk = ["zk-garage-plonk"]

# SCALE Codec and Type Info
scale = ["scale-codec", "scale-info"]

Expand Down Expand Up @@ -86,7 +86,7 @@ simulation = [
std = ["manta-accounting/std", "manta-util/std"]

# Testing Frameworks
test = ["anyhow", "manta-accounting/test", "manta-crypto/test", "tempfile"]
test = ["manta-accounting/test", "manta-crypto/test", "tempfile"]

# Wallet
wallet = ["bip32", "manta-crypto/getrandom", "std"]
Expand All @@ -104,7 +104,6 @@ websocket = [

[dependencies]
aes-gcm = { version = "0.9.4", default-features = false, features = ["aes", "alloc"] }
anyhow = { version = "1.0.57", optional = true, default-features = false }
ark-bls12-381 = { version = "0.3.0", optional = true, default-features = false, features = ["curve"] }
ark-ec = { version = "0.3.0", optional = true, default-features = false }
ark-ed-on-bls12-381 = { version = "0.3.0", optional = true, default-features = false, features = ["r1cs"] }
Expand All @@ -124,6 +123,7 @@ futures = { version = "0.3.21", optional = true, default-features = false }
indexmap = { version = "1.8.2", optional = true, default-features = false }
manta-accounting = { path = "../manta-accounting", default-features = false }
manta-crypto = { path = "../manta-crypto", default-features = false }
manta-parameters = { path = "../manta-parameters", optional = true, default-features = false, features = ["download"] }
manta-util = { path = "../manta-util", default-features = false }
parking_lot = { version = "0.12.1", optional = true, default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }
Expand All @@ -137,9 +137,7 @@ tide = { version = "0.16.0", optional = true, default-features = false, features
tokio = { version = "1.18.2", optional = true, default-features = false }
tokio-tungstenite = { version = "0.17.1", optional = true, default-features = false, features = ["native-tls"] }
ws_stream_wasm = { version = "0.7.3", optional = true, default-features = false }
# TODO: zk-garage-plonk = { package = "plonk", git = "https://github.com/zk-garage/plonk", optional = true, default-features = false }

[dev-dependencies]
manta-crypto = { path = "../manta-crypto", default-features = false, features = ["getrandom"] }
manta-parameters = { path = "../manta-parameters", default-features = false, features = ["download"] }
manta-pay = { path = ".", default-features = false, features = ["groth16", "scale", "scale-std", "std", "test"] }
manta-pay = { path = ".", default-features = false, features = ["download", "groth16", "scale", "scale-std", "std", "test"] }
4 changes: 0 additions & 4 deletions manta-pay/src/crypto/constraint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@
#[cfg(feature = "arkworks")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "arkworks")))]
pub mod arkworks;

#[cfg(feature = "plonk")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "plonk")))]
pub mod plonk;
17 changes: 0 additions & 17 deletions manta-pay/src/crypto/constraint/plonk.rs

This file was deleted.

95 changes: 95 additions & 0 deletions manta-pay/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ use crate::config::{
use manta_crypto::rand::{Rand, SeedableRng};
use rand_chacha::ChaCha20Rng;

#[cfg(feature = "download")]
use {
crate::config::{
NoteEncryptionScheme, ProvingContext, UtxoCommitmentScheme, VerifyingContext,
VoidNumberCommitmentScheme,
},
manta_util::codec::{Decode, IoReader},
std::{fs::File, path::Path},
};

/// Parameter Generation Seed
///
/// This is a nothing-up-my-sleve parameter generation number. Its just the numbers from `0` to `31`
Expand Down Expand Up @@ -90,3 +100,88 @@ pub fn generate() -> Result<
> {
generate_from_seed(SEED)
}

/// Loads parameters from the `manta-parameters`, using `directory` as a temporary directory to store files.
#[cfg(feature = "download")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "download")))]
#[inline]
pub fn load_parameters(
directory: &Path,
) -> Result<
(
MultiProvingContext,
MultiVerifyingContext,
Parameters,
UtxoAccumulatorModel,
),
ProofSystemError,
> {
let mint_path = directory.join("mint.dat");
manta_parameters::pay::testnet::proving::Mint::download(&mint_path)
.expect("Unable to download MINT proving context.");
let private_transfer_path = directory.join("private-transfer.dat");
manta_parameters::pay::testnet::proving::PrivateTransfer::download(&private_transfer_path)
.expect("Unable to download PRIVATE_TRANSFER proving context.");
let reclaim_path = directory.join("reclaim.dat");
manta_parameters::pay::testnet::proving::Reclaim::download(&reclaim_path)
.expect("Unable to download RECLAIM proving context.");
let proving_context = MultiProvingContext {
mint: ProvingContext::decode(IoReader(
File::open(mint_path).expect("Unable to read MINT proving context from disk."),
))
.expect("Unable to decode MINT proving context."),
private_transfer: ProvingContext::decode(IoReader(
File::open(private_transfer_path)
.expect("Unable to read PRIVATE_TRANSFER proving context from disk."),
))
.expect("Unable to decode PRIVATE_TRANSFER proving context."),
reclaim: ProvingContext::decode(IoReader(
File::open(reclaim_path).expect("Unable to read RECLAIM provin context from disk."),
))
.expect("Unable to decode RECLAIM proving context."),
};
let verifying_context = MultiVerifyingContext {
mint: VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::Mint::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode MINT verifying context."),
private_transfer: VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::PrivateTransfer::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode PRIVATE_TRANSFER verifying context."),
reclaim: VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::Reclaim::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode RECLAIM verifying context."),
};
let parameters = Parameters {
note_encryption_scheme: NoteEncryptionScheme::decode(
manta_parameters::pay::testnet::parameters::NoteEncryptionScheme::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode NOTE_ENCRYPTION_SCHEME parameters."),
utxo_commitment: UtxoCommitmentScheme::decode(
manta_parameters::pay::testnet::parameters::UtxoCommitmentScheme::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode UTXO_COMMITMENT_SCHEME parameters."),
void_number_commitment: VoidNumberCommitmentScheme::decode(
manta_parameters::pay::testnet::parameters::VoidNumberCommitmentScheme::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode VOID_NUMBER_COMMITMENT_SCHEME parameters."),
};
Ok((
proving_context,
verifying_context,
parameters,
UtxoAccumulatorModel::decode(
manta_parameters::pay::testnet::parameters::UtxoAccumulatorModel::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode UTXO_ACCUMULATOR_MODEL."),
))
}
82 changes: 1 addition & 81 deletions manta-pay/src/test/compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,91 +18,11 @@
//! Checks if the current circuit implementation is compatible with precomputed parameters.

use crate::{
config::{
MultiProvingContext, MultiVerifyingContext, NoteEncryptionScheme, Parameters,
ProvingContext, UtxoAccumulatorModel, UtxoCommitmentScheme, VerifyingContext,
VoidNumberCommitmentScheme,
},
parameters::load_parameters,
test::payment::{prove_mint, prove_private_transfer, prove_reclaim},
};
use anyhow::Result;
use manta_accounting::transfer::test::assert_valid_proof;
use manta_crypto::rand::{OsRng, Rand};
use manta_util::codec::{Decode, IoReader};
use std::{fs::File, path::Path};

/// Loads parameters from the `manta-parameters`, using `directory` as a temporary directory to store files.
#[inline]
fn load_parameters(
directory: &Path,
) -> Result<(
MultiProvingContext,
MultiVerifyingContext,
Parameters,
UtxoAccumulatorModel,
)> {
println!("Loading parameters...");
let mint_path = directory.join("mint.dat");
manta_parameters::pay::testnet::proving::Mint::download(&mint_path)?;
let private_transfer_path = directory.join("private-transfer.dat");
manta_parameters::pay::testnet::proving::PrivateTransfer::download(&private_transfer_path)?;
let reclaim_path = directory.join("reclaim.dat");
manta_parameters::pay::testnet::proving::Reclaim::download(&reclaim_path)?;
println!("mint_path: {:?}", mint_path);
let proving_context = MultiProvingContext {
mint: ProvingContext::decode(IoReader(File::open(mint_path)?))
.expect("Unable to decode MINT proving context."),
private_transfer: ProvingContext::decode(IoReader(File::open(private_transfer_path)?))
.expect("Unable to decode PRIVATE_TRANSFER proving context."),
reclaim: ProvingContext::decode(IoReader(File::open(reclaim_path)?))
.expect("Unable to decode RECLAIM proving context."),
};
let verifying_context = MultiVerifyingContext {
mint: VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::Mint::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode MINT verifying context."),
private_transfer: VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::PrivateTransfer::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode PRIVATE_TRANSFER verifying context."),
reclaim: VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::Reclaim::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode RECLAIM verifying context."),
};
let parameters = Parameters {
note_encryption_scheme: NoteEncryptionScheme::decode(
manta_parameters::pay::testnet::parameters::NoteEncryptionScheme::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode NOTE_ENCRYPTION_SCHEME parameters."),
utxo_commitment: UtxoCommitmentScheme::decode(
manta_parameters::pay::testnet::parameters::UtxoCommitmentScheme::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode UTXO_COMMITMENT_SCHEME parameters."),
void_number_commitment: VoidNumberCommitmentScheme::decode(
manta_parameters::pay::testnet::parameters::VoidNumberCommitmentScheme::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode VOID_NUMBER_COMMITMENT_SCHEME parameters."),
};
println!("Loading parameters Done.");
Ok((
proving_context,
verifying_context,
parameters,
UtxoAccumulatorModel::decode(
manta_parameters::pay::testnet::parameters::UtxoAccumulatorModel::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode UTXO_ACCUMULATOR_MODEL."),
))
}

/// Tests that the circuit is compatible with the current known parameters in `manta-parameters`.
#[test]
Expand Down

0 comments on commit 8917a0d

Please sign in to comment.