Merge pull request #251 from str4d/bundled-prover

zcash_proofs: Add LocalProver::bundled
This commit is contained in:
str4d 2020-08-04 05:35:07 +12:00 committed by GitHub
commit a224d826fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 11 deletions

View File

@ -20,6 +20,7 @@ ff = { version = "0.6", path = "../ff" }
minreq = { version = "2", features = ["https"], optional = true }
pairing = { version = "0.16", path = "../pairing" }
rand_core = "0.5.1"
wagyu-zcash-parameters = { version = "0.2", optional = true }
zcash_primitives = { version = "0.2", path = "../zcash_primitives" }
[dev-dependencies]
@ -27,6 +28,7 @@ rand_xorshift = "0.2"
[features]
default = ["local-prover", "multicore"]
bundled-prover = ["wagyu-zcash-parameters"]
download-params = ["minreq"]
local-prover = ["directories"]
multicore = ["bellman/multicore"]

View File

@ -22,11 +22,13 @@ mod hashreader;
pub mod sapling;
pub mod sprout;
#[cfg(feature = "local-prover")]
#[cfg(any(feature = "local-prover", feature = "bundled-prover"))]
pub mod prover;
// Circuit names
#[cfg(feature = "local-prover")]
const SAPLING_SPEND_NAME: &str = "sapling-spend.params";
#[cfg(feature = "local-prover")]
const SAPLING_OUTPUT_NAME: &str = "sapling-output.params";
// Circuit hashes
@ -118,11 +120,27 @@ pub fn load_parameters(
let sprout_fs =
sprout_path.map(|p| File::open(p).expect("couldn't load Sprout groth16 parameters file"));
let mut spend_fs = hashreader::HashReader::new(BufReader::with_capacity(1024 * 1024, spend_fs));
let mut output_fs =
hashreader::HashReader::new(BufReader::with_capacity(1024 * 1024, output_fs));
let mut sprout_fs =
sprout_fs.map(|fs| hashreader::HashReader::new(BufReader::with_capacity(1024 * 1024, fs)));
parse_parameters(
BufReader::with_capacity(1024 * 1024, spend_fs),
BufReader::with_capacity(1024 * 1024, output_fs),
sprout_fs.map(|fs| BufReader::with_capacity(1024 * 1024, fs)),
)
}
fn parse_parameters<R: io::Read>(
spend_fs: R,
output_fs: R,
sprout_fs: Option<R>,
) -> (
Parameters<Bls12>,
PreparedVerifyingKey<Bls12>,
Parameters<Bls12>,
PreparedVerifyingKey<Bls12>,
Option<PreparedVerifyingKey<Bls12>>,
) {
let mut spend_fs = hashreader::HashReader::new(spend_fs);
let mut output_fs = hashreader::HashReader::new(output_fs);
let mut sprout_fs = sprout_fs.map(|fs| hashreader::HashReader::new(fs));
// Deserialize params
let spend_params = Parameters::<Bls12>::read(&mut spend_fs, false)

View File

@ -2,7 +2,6 @@
use bellman::groth16::{Parameters, PreparedVerifyingKey};
use pairing::bls12_381::{Bls12, Fr};
use std::path::Path;
use zcash_primitives::{
jubjub::{edwards, fs::Fs, Unknown},
primitives::{Diversifier, PaymentAddress, ProofGenerationKey},
@ -16,10 +15,15 @@ use zcash_primitives::{
JUBJUB,
};
use crate::{
default_params_folder, load_parameters, sapling::SaplingProvingContext, SAPLING_OUTPUT_NAME,
SAPLING_SPEND_NAME,
};
use crate::sapling::SaplingProvingContext;
#[cfg(feature = "local-prover")]
use crate::{default_params_folder, load_parameters, SAPLING_OUTPUT_NAME, SAPLING_SPEND_NAME};
#[cfg(feature = "local-prover")]
use std::path::Path;
#[cfg(feature = "bundled-prover")]
use crate::parse_parameters;
/// An implementation of [`TxProver`] using Sapling Spend and Output parameters from
/// locally-accessible paths.
@ -48,6 +52,7 @@ impl LocalTxProver {
///
/// This function will panic if the paths do not point to valid parameter files with
/// the expected hashes.
#[cfg(feature = "local-prover")]
pub fn new(spend_path: &Path, output_path: &Path) -> Self {
let (spend_params, spend_vk, output_params, _, _) =
load_parameters(spend_path, output_path, None);
@ -79,6 +84,7 @@ impl LocalTxProver {
///
/// This function will panic if the parameters in the default local location do not
/// have the expected hashes.
#[cfg(feature = "local-prover")]
pub fn with_default_location() -> Option<Self> {
let params_dir = default_params_folder()?;
let (spend_path, output_path) = if params_dir.exists() {
@ -95,6 +101,23 @@ impl LocalTxProver {
Some(LocalTxProver::new(&spend_path, &output_path))
}
/// Creates a `LocalTxProver` using Sapling parameters bundled inside the binary.
///
/// This requires the `bundled-prover` feature, which will increase the binary size by
/// around 50 MiB.
#[cfg(feature = "bundled-prover")]
pub fn bundled() -> Self {
let (spend_buf, output_buf) = wagyu_zcash_parameters::load_sapling_parameters();
let (spend_params, spend_vk, output_params, _, _) =
parse_parameters(&spend_buf[..], &output_buf[..], None);
LocalTxProver {
spend_params,
spend_vk,
output_params,
}
}
}
impl TxProver for LocalTxProver {