Skip to content

Commit

Permalink
Merge branch 'main' into feat/eclair-v0
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon H. Gomes <bhgomes@pm.me>
  • Loading branch information
bhgomes authored Jun 23, 2022
2 parents b07e150 + df9b2f7 commit 238a4a5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased]
### Added
- [\#126](https://github.com/Manta-Network/manta-rs/pull/126) Add ECLAIR v0 scaffolding and deprecate old compiler patterns
- [\#128](https://github.com/Manta-Network/manta-rs/pull/128) Add more parameter loading utilities

### Changed

Expand Down
4 changes: 3 additions & 1 deletion manta-accounting/src/transfer/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ pub fn assert_valid_proof<C>(verifying_context: &VerifyingContext<C>, post: &Tra
where
C: Configuration,
<C::ProofSystem as ProofSystem>::Error: Debug,
TransferPost<C>: Debug,
{
assert!(
C::ProofSystem::verify(
Expand All @@ -415,6 +416,7 @@ where
&post.validity_proof,
)
.expect("Unable to verify proof."),
"Invalid proof.",
"Invalid proof: {:?}",
post,
);
}
6 changes: 3 additions & 3 deletions manta-pay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ arkworks = [
]

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

# Enable Groth16 ZKP System
groth16 = ["ark-groth16", "ark-snark", "arkworks"]
Expand Down Expand Up @@ -86,7 +86,7 @@ simulation = [
std = ["manta-accounting/std", "manta-util/std"]

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

# Wallet
wallet = ["bip32", "manta-crypto/getrandom", "std"]
Expand Down Expand Up @@ -123,7 +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-parameters = { path = "../manta-parameters", optional = true, default-features = false }
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 Down
155 changes: 112 additions & 43 deletions manta-pay/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
//! Generate Parameters and Proving/Verifying Contexts
use crate::config::{
FullParameters, Mint, MultiProvingContext, MultiVerifyingContext, Parameters, PrivateTransfer,
ProofSystemError, Reclaim, UtxoAccumulatorModel,
FullParameters, Mint, MultiProvingContext, MultiVerifyingContext, NoteEncryptionScheme,
Parameters, PrivateTransfer, ProofSystemError, Reclaim, UtxoAccumulatorModel,
UtxoCommitmentScheme, VerifyingContext, VoidNumberCommitmentScheme,
};
use manta_crypto::rand::{Rand, SeedableRng};
use manta_util::codec::Decode;
use rand_chacha::ChaCha20Rng;

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

Expand Down Expand Up @@ -101,7 +100,7 @@ pub fn generate() -> Result<
generate_from_seed(SEED)
}

/// Loads parameters from the `manta-parameters`, using `directory` as a temporary directory to store files.
/// Loads parameters from [`manta-parameters`], using `directory` as a temporary directory to store files.
#[cfg(feature = "download")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "download")))]
#[inline]
Expand All @@ -116,6 +115,24 @@ pub fn load_parameters(
),
ProofSystemError,
> {
Ok((
load_proving_context(directory),
MultiVerifyingContext {
mint: load_mint_verifying_context(),
private_transfer: load_private_transfer_verifying_context(),
reclaim: load_reclaim_verifying_context(),
},
load_transfer_parameters(),
load_utxo_accumulator_model(),
))
}

/// Loads the [`MultiProvingContext`] from [`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_proving_context(directory: &Path) -> MultiProvingContext {
let mint_path = directory.join("mint.dat");
manta_parameters::pay::testnet::proving::Mint::download(&mint_path)
.expect("Unable to download MINT proving context.");
Expand All @@ -125,39 +142,91 @@ pub fn load_parameters(
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 {
decode_proving_context(&mint_path, &private_transfer_path, &reclaim_path)
}

/// Loads the [`MultiProvingContext`] from [`manta_parameters`], using `directory` as
/// a temporary directory to store files.
///
/// This function skips downloading the proving contexts if they have been
/// downloaded before and their checksum matches the expected one. See
/// [`manta_parameters::verify_file`] for more on checksum verification.
#[cfg(feature = "download")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "download")))]
#[inline]
pub fn try_load_proving_context(directory: &Path) -> MultiProvingContext {
let mint_path = directory.join("mint.dat");
manta_parameters::pay::testnet::proving::Mint::download_if_invalid(&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_if_invalid(
&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_if_invalid(&reclaim_path)
.expect("Unable to download RECLAIM proving context.");
decode_proving_context(&mint_path, &private_transfer_path, &reclaim_path)
}

/// Decodes [`MultiProvingContext`] by loading from `mint_path`, `private_transfer_path`, and `reclaim_path`.
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[inline]
pub fn decode_proving_context(
mint_path: &Path,
private_transfer_path: &Path,
reclaim_path: &Path,
) -> MultiProvingContext {
MultiProvingContext {
mint: ProvingContext::decode(IoReader(
File::open(mint_path).expect("Unable to read MINT proving context from disk."),
File::open(mint_path).expect("Unable to open MINT proving context file."),
))
.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 open PRIVATE_TRANSFER proving context file."),
))
.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."),
File::open(reclaim_path).expect("Unable to open RECLAIM proving context file."),
))
.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 {
}
}

/// Loads the `Mint` verifying contexts from [`manta_parameters`].
#[inline]
pub fn load_mint_verifying_context() -> VerifyingContext {
VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::Mint::get().expect("Checksum did not match."),
)
.expect("Unable to decode MINT verifying context.")
}

/// Loads the `PrivateTransfer` verifying context from [`manta_parameters`].
#[inline]
pub fn load_private_transfer_verifying_context() -> VerifyingContext {
VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::PrivateTransfer::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode PRIVATE_TRANSFER verifying context.")
}

/// Loads the `Reclaim` verifying context from [`manta_parameters`].
#[inline]
pub fn load_reclaim_verifying_context() -> VerifyingContext {
VerifyingContext::decode(
manta_parameters::pay::testnet::verifying::Reclaim::get().expect("Checksum did not match."),
)
.expect("Unable to decode RECLAIM verifying context.")
}

/// Loads the transfer [`Parameters`] from [`manta_parameters`].
#[inline]
pub fn load_transfer_parameters() -> Parameters {
Parameters {
note_encryption_scheme: NoteEncryptionScheme::decode(
manta_parameters::pay::testnet::parameters::NoteEncryptionScheme::get()
.expect("Checksum did not match."),
Expand All @@ -173,15 +242,15 @@ pub fn load_parameters(
.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."),
))
}
}

/// Loads the [`UtxoAccumulatorModel`] from [`manta_parameters`].
#[inline]
pub fn load_utxo_accumulator_model() -> UtxoAccumulatorModel {
UtxoAccumulatorModel::decode(
manta_parameters::pay::testnet::parameters::UtxoAccumulatorModel::get()
.expect("Checksum did not match."),
)
.expect("Unable to decode UTXO_ACCUMULATOR_MODEL.")
}

0 comments on commit 238a4a5

Please sign in to comment.