-
Notifications
You must be signed in to change notification settings - Fork 948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
checked masp params in node #1619
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ use std::collections::hash_map::Entry; | |
use std::collections::{BTreeMap, HashMap, HashSet}; | ||
use std::env; | ||
use std::fmt::Debug; | ||
use std::fs::File; | ||
#[cfg(feature = "masp-tx-gen")] | ||
use std::ops::Deref; | ||
use std::path::PathBuf; | ||
|
@@ -44,9 +43,7 @@ use masp_primitives::transaction::{ | |
TransparentAddress, Unauthorized, | ||
}; | ||
use masp_primitives::zip32::{ExtendedFullViewingKey, ExtendedSpendingKey}; | ||
use masp_proofs::bellman::groth16::{ | ||
prepare_verifying_key, PreparedVerifyingKey, | ||
}; | ||
use masp_proofs::bellman::groth16::PreparedVerifyingKey; | ||
use masp_proofs::bls12_381::Bls12; | ||
use masp_proofs::prover::LocalTxProver; | ||
use masp_proofs::sapling::SaplingVerificationContext; | ||
|
@@ -90,70 +87,37 @@ pub const OUTPUT_NAME: &str = "masp-output.params"; | |
/// Convert circuit name | ||
pub const CONVERT_NAME: &str = "masp-convert.params"; | ||
|
||
/// Load Sapling spend params. | ||
pub fn load_spend_params() -> ( | ||
masp_proofs::bellman::groth16::Parameters<Bls12>, | ||
masp_proofs::bellman::groth16::PreparedVerifyingKey<Bls12>, | ||
fn load_pvks() -> ( | ||
PreparedVerifyingKey<Bls12>, | ||
PreparedVerifyingKey<Bls12>, | ||
PreparedVerifyingKey<Bls12>, | ||
) { | ||
let params_dir = get_params_dir(); | ||
let spend_path = params_dir.join(SPEND_NAME); | ||
if !spend_path.exists() { | ||
#[cfg(feature = "masp_proofs/download-params")] | ||
masp_proofs::download_parameters() | ||
.expect("MASP parameters not present or downloadable"); | ||
#[cfg(not(feature = "masp_proofs/download-params"))] | ||
panic!("MASP parameters not present or downloadable"); | ||
} | ||
let param_f = File::open(spend_path).unwrap(); | ||
let params = | ||
masp_proofs::bellman::groth16::Parameters::read(¶m_f, false) | ||
.unwrap(); | ||
let vk = prepare_verifying_key(¶ms.vk); | ||
(params, vk) | ||
} | ||
let [spend_path, convert_path, output_path] = | ||
[SPEND_NAME, CONVERT_NAME, OUTPUT_NAME].map(|p| params_dir.join(p)); | ||
|
||
/// Load Sapling convert params. | ||
pub fn load_convert_params() -> ( | ||
masp_proofs::bellman::groth16::Parameters<Bls12>, | ||
masp_proofs::bellman::groth16::PreparedVerifyingKey<Bls12>, | ||
) { | ||
let params_dir = get_params_dir(); | ||
let spend_path = params_dir.join(CONVERT_NAME); | ||
if !spend_path.exists() { | ||
#[cfg(feature = "masp_proofs/download-params")] | ||
masp_proofs::download_parameters() | ||
.expect("MASP parameters not present or downloadable"); | ||
#[cfg(not(feature = "masp_proofs/download-params"))] | ||
panic!("MASP parameters not present or downloadable"); | ||
} | ||
let param_f = File::open(spend_path).unwrap(); | ||
let params = | ||
masp_proofs::bellman::groth16::Parameters::read(¶m_f, false) | ||
.unwrap(); | ||
let vk = prepare_verifying_key(¶ms.vk); | ||
(params, vk) | ||
} | ||
|
||
/// Load Sapling output params. | ||
pub fn load_output_params() -> ( | ||
masp_proofs::bellman::groth16::Parameters<Bls12>, | ||
masp_proofs::bellman::groth16::PreparedVerifyingKey<Bls12>, | ||
) { | ||
let params_dir = get_params_dir(); | ||
let output_path = params_dir.join(OUTPUT_NAME); | ||
if !output_path.exists() { | ||
#[cfg(feature = "masp_proofs/download-params")] | ||
masp_proofs::download_parameters() | ||
.expect("MASP parameters not present or downloadable"); | ||
#[cfg(not(feature = "masp_proofs/download-params"))] | ||
panic!("MASP parameters not present or downloadable"); | ||
if !spend_path.exists() || !convert_path.exists() || !output_path.exists() { | ||
let paths = masp_proofs::download_masp_parameters(None).expect( | ||
"MASP parameters were not present, expected the download to \ | ||
succeed", | ||
); | ||
if paths.spend != spend_path | ||
|| paths.convert != convert_path | ||
|| paths.output != output_path | ||
{ | ||
panic!( | ||
"unrecoverable: downloaded missing masp params, but to an \ | ||
unfamiliar path" | ||
) | ||
} | ||
} | ||
let param_f = File::open(output_path).unwrap(); | ||
let params = | ||
masp_proofs::bellman::groth16::Parameters::read(¶m_f, false) | ||
.unwrap(); | ||
let vk = prepare_verifying_key(¶ms.vk); | ||
(params, vk) | ||
// size and blake2b checked here | ||
let params = masp_proofs::load_parameters( | ||
spend_path.as_path(), | ||
output_path.as_path(), | ||
convert_path.as_path(), | ||
); | ||
(params.spend_vk, params.convert_vk, params.output_vk) | ||
} | ||
|
||
/// check_spend wrapper | ||
|
@@ -285,9 +249,7 @@ pub fn verify_shielded_tx(transaction: &Transaction) -> bool { | |
|
||
tracing::info!("sighash computed"); | ||
|
||
let (_, spend_pvk) = load_spend_params(); | ||
let (_, convert_pvk) = load_convert_params(); | ||
let (_, output_pvk) = load_output_params(); | ||
let (spend_pvk, convert_pvk, output_pvk) = load_pvks(); | ||
|
||
let mut ctx = SaplingVerificationContext::new(true); | ||
let spends_valid = sapling_bundle.shielded_spends.iter().all(|spend| { | ||
|
@@ -373,7 +335,7 @@ impl<P1, R1, N1> | |
|
||
/// Abstracts platform specific details away from the logic of shielded pool | ||
/// operations. | ||
#[async_trait(?Send)] | ||
#[async_trait(? Send)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the hell did fmt do this |
||
pub trait ShieldedUtils: | ||
Sized + BorshDeserialize + BorshSerialize + Default + Clone | ||
{ | ||
|
@@ -1554,3 +1516,105 @@ fn convert_amount( | |
.expect("invalid value for amount"); | ||
(asset_type, amount) | ||
} | ||
|
||
mod tests { | ||
/// quick and dirty test. will fail on size check | ||
#[test] | ||
#[should_panic(expected = "parameter file size is not correct")] | ||
fn test_wrong_masp_params() { | ||
use std::io::Write; | ||
|
||
use super::{CONVERT_NAME, OUTPUT_NAME, SPEND_NAME}; | ||
|
||
let tempdir = tempfile::tempdir() | ||
.expect("expected a temp dir") | ||
.into_path(); | ||
let fake_params_paths = | ||
[SPEND_NAME, CONVERT_NAME, OUTPUT_NAME].map(|p| tempdir.join(p)); | ||
for path in fake_params_paths { | ||
let mut f = | ||
std::fs::File::create(path).expect("expected a temp file"); | ||
f.write_all(b"fake params") | ||
.expect("expected a writable temp file"); | ||
f.sync_all() | ||
.expect("expected a writable temp file (on sync)"); | ||
} | ||
|
||
std::env::set_var(super::ENV_VAR_MASP_PARAMS_DIR, tempdir.as_os_str()); | ||
// should panic here | ||
super::load_pvks(); | ||
} | ||
|
||
/// a more involved test, using dummy parameters with the right | ||
/// size but the wrong hash. | ||
#[test] | ||
#[should_panic(expected = "parameter file is not correct")] | ||
fn test_wrong_masp_params_hash() { | ||
use masp_primitives::ff::PrimeField; | ||
use masp_proofs::bellman::groth16::{ | ||
generate_random_parameters, Parameters, | ||
}; | ||
use masp_proofs::bellman::{Circuit, ConstraintSystem, SynthesisError}; | ||
use masp_proofs::bls12_381::{Bls12, Scalar}; | ||
|
||
use super::{CONVERT_NAME, OUTPUT_NAME, SPEND_NAME}; | ||
|
||
struct FakeCircuit<E: PrimeField> { | ||
x: E, | ||
} | ||
|
||
impl<E: PrimeField> Circuit<E> for FakeCircuit<E> { | ||
fn synthesize<CS: ConstraintSystem<E>>( | ||
self, | ||
cs: &mut CS, | ||
) -> Result<(), SynthesisError> { | ||
let x = cs.alloc(|| "x", || Ok(self.x)).unwrap(); | ||
cs.enforce( | ||
|| { | ||
"this is an extra long constraint name so that rustfmt \ | ||
is ok with wrapping the params of enforce()" | ||
}, | ||
|lc| lc + x, | ||
|lc| lc + x, | ||
|lc| lc + x, | ||
); | ||
Ok(()) | ||
} | ||
} | ||
|
||
let dummy_circuit = FakeCircuit { x: Scalar::zero() }; | ||
let mut rng = rand::thread_rng(); | ||
let fake_params: Parameters<Bls12> = | ||
generate_random_parameters(dummy_circuit, &mut rng) | ||
.expect("expected to generate fake params"); | ||
|
||
let tempdir = tempfile::tempdir() | ||
.expect("expected a temp dir") | ||
.into_path(); | ||
// TODO: get masp to export these consts | ||
let fake_params_paths = [ | ||
(SPEND_NAME, 49848572u64), | ||
(CONVERT_NAME, 22570940u64), | ||
(OUTPUT_NAME, 16398620u64), | ||
] | ||
.map(|(p, s)| (tempdir.join(p), s)); | ||
for (path, size) in fake_params_paths { | ||
let mut f = | ||
std::fs::File::create(path).expect("expected a temp file"); | ||
fake_params | ||
.write(&mut f) | ||
.expect("expected a writable temp file"); | ||
// the dummy circuit has one constraint, and therefore its | ||
// params should always be smaller than the large masp | ||
// circuit params. so this truncate extends the file, and | ||
// extra bytes at the end do not make it invalid. | ||
f.set_len(size).expect("expected to truncate the temp file"); | ||
f.sync_all() | ||
.expect("expected a writable temp file (on sync)"); | ||
} | ||
|
||
std::env::set_var(super::ENV_VAR_MASP_PARAMS_DIR, tempdir.as_os_str()); | ||
// should panic here | ||
super::load_pvks(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be fixed in the future. We shouldn't panic but rather return an error, because otherwise consumers of the SDK can't really handle these failures with error messages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is in the node, not the sdk