zcash_proofs: Introduce newtype wrappers for Sapling parameters

This commit is contained in:
Jack Grigg 2023-10-02 14:03:09 +00:00
parent 2bfeef9430
commit ea0fed39eb
4 changed files with 35 additions and 15 deletions

View File

@ -6,7 +6,16 @@ and this library adheres to Rust's notion of
[Semantic Versioning](https://semver.org/spec/v2.0.0.html). [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
### Added
- `zcash_proofs::{SpendParameters, OutputParameters}`
### Changed ### Changed
- The new `SpendParameters` and `OutputParameters` types are used in the
following places:
- `zcash_proofs::ZcashParameters::{spend_params, output_params}` fields.
- `zcash_proofs::sapling::prover`:
- `SaplingProvingContext::{spend_proof, output_proof}` (the `proving_key`
arguments).
- `zcash_proofs::sapling::prover`: - `zcash_proofs::sapling::prover`:
- The `verifying_key` argument `SaplingProvingContext::spend_proof` has been - The `verifying_key` argument `SaplingProvingContext::spend_proof` has been
removed. Callers should instead use `SaplingVerifyingContext` to verify removed. Callers should instead use `SaplingVerifyingContext` to verify

View File

@ -283,11 +283,17 @@ fn stream_params_downloads_to_disk(
Ok(()) Ok(())
} }
/// The parameters for the Sapling Spend circuit.
pub struct SpendParameters(Parameters<Bls12>);
/// The parameters for the Sapling Output circuit.
pub struct OutputParameters(Parameters<Bls12>);
/// Zcash Sprout and Sapling groth16 circuit parameters. /// Zcash Sprout and Sapling groth16 circuit parameters.
pub struct ZcashParameters { pub struct ZcashParameters {
pub spend_params: Parameters<Bls12>, pub spend_params: SpendParameters,
pub spend_vk: PreparedVerifyingKey<Bls12>, pub spend_vk: PreparedVerifyingKey<Bls12>,
pub output_params: Parameters<Bls12>, pub output_params: OutputParameters,
pub output_vk: PreparedVerifyingKey<Bls12>, pub output_vk: PreparedVerifyingKey<Bls12>,
pub sprout_vk: Option<PreparedVerifyingKey<Bls12>>, pub sprout_vk: Option<PreparedVerifyingKey<Bls12>>,
} }
@ -429,9 +435,9 @@ pub fn parse_parameters<R: io::Read>(
let sprout_vk = sprout_vk.map(|vk| prepare_verifying_key(&vk)); let sprout_vk = sprout_vk.map(|vk| prepare_verifying_key(&vk));
ZcashParameters { ZcashParameters {
spend_params, spend_params: SpendParameters(spend_params),
spend_vk, spend_vk,
output_params, output_params: OutputParameters(output_params),
output_vk, output_vk,
sprout_vk, sprout_vk,
} }

View File

@ -1,6 +1,6 @@
//! Abstractions over the proving system and parameters for ease of use. //! Abstractions over the proving system and parameters for ease of use.
use bellman::groth16::{Parameters, PreparedVerifyingKey}; use bellman::groth16::PreparedVerifyingKey;
use bls12_381::Bls12; use bls12_381::Bls12;
use std::path::Path; use std::path::Path;
use zcash_primitives::{ use zcash_primitives::{
@ -13,7 +13,10 @@ use zcash_primitives::{
transaction::components::{Amount, GROTH_PROOF_SIZE}, transaction::components::{Amount, GROTH_PROOF_SIZE},
}; };
use crate::{load_parameters, parse_parameters, sapling::SaplingProvingContext}; use crate::{
load_parameters, parse_parameters, sapling::SaplingProvingContext, OutputParameters,
SpendParameters,
};
#[cfg(feature = "local-prover")] #[cfg(feature = "local-prover")]
use crate::{default_params_folder, SAPLING_OUTPUT_NAME, SAPLING_SPEND_NAME}; use crate::{default_params_folder, SAPLING_OUTPUT_NAME, SAPLING_SPEND_NAME};
@ -21,12 +24,12 @@ use crate::{default_params_folder, SAPLING_OUTPUT_NAME, SAPLING_SPEND_NAME};
/// An implementation of [`TxProver`] using Sapling Spend and Output parameters from /// An implementation of [`TxProver`] using Sapling Spend and Output parameters from
/// locally-accessible paths. /// locally-accessible paths.
pub struct LocalTxProver { pub struct LocalTxProver {
spend_params: Parameters<Bls12>, spend_params: SpendParameters,
// TODO: Either re-introduce verification-after-proving (once the verifier is // TODO: Either re-introduce verification-after-proving (once the verifier is
// refactored), or remove this. // refactored), or remove this.
#[allow(unused)] #[allow(unused)]
spend_vk: PreparedVerifyingKey<Bls12>, spend_vk: PreparedVerifyingKey<Bls12>,
output_params: Parameters<Bls12>, output_params: OutputParameters,
} }
impl LocalTxProver { impl LocalTxProver {

View File

@ -1,4 +1,4 @@
use bellman::groth16::{create_random_proof, Parameters, Proof}; use bellman::groth16::{create_random_proof, Proof};
use bls12_381::Bls12; use bls12_381::Bls12;
use group::GroupEncoding; use group::GroupEncoding;
use rand_core::OsRng; use rand_core::OsRng;
@ -13,6 +13,8 @@ use zcash_primitives::{
transaction::components::Amount, transaction::components::Amount,
}; };
use crate::{OutputParameters, SpendParameters};
/// A context object for creating the Sapling components of a Zcash transaction. /// A context object for creating the Sapling components of a Zcash transaction.
pub struct SaplingProvingContext { pub struct SaplingProvingContext {
bsk: TrapdoorSum, bsk: TrapdoorSum,
@ -48,7 +50,7 @@ impl SaplingProvingContext {
value: u64, value: u64,
anchor: bls12_381::Scalar, anchor: bls12_381::Scalar,
merkle_path: MerklePath, merkle_path: MerklePath,
proving_key: &Parameters<Bls12>, proving_key: &SpendParameters,
) -> Result<(Proof<Bls12>, ValueCommitment, PublicKey), ()> { ) -> Result<(Proof<Bls12>, ValueCommitment, PublicKey), ()> {
// Initialize secure RNG // Initialize secure RNG
let mut rng = OsRng; let mut rng = OsRng;
@ -96,8 +98,8 @@ impl SaplingProvingContext {
}; };
// Create proof // Create proof
let proof = let proof = create_random_proof(instance, &proving_key.0, &mut rng)
create_random_proof(instance, proving_key, &mut rng).expect("proving should not fail"); .expect("proving should not fail");
// Accumulate the value commitment in the context // Accumulate the value commitment in the context
self.cv_sum += &value_commitment; self.cv_sum += &value_commitment;
@ -114,7 +116,7 @@ impl SaplingProvingContext {
payment_address: PaymentAddress, payment_address: PaymentAddress,
rcm: jubjub::Fr, rcm: jubjub::Fr,
value: u64, value: u64,
proving_key: &Parameters<Bls12>, proving_key: &OutputParameters,
) -> (Proof<Bls12>, ValueCommitment) { ) -> (Proof<Bls12>, ValueCommitment) {
// Initialize secure RNG // Initialize secure RNG
let mut rng = OsRng; let mut rng = OsRng;
@ -143,8 +145,8 @@ impl SaplingProvingContext {
}; };
// Create proof // Create proof
let proof = let proof = create_random_proof(instance, &proving_key.0, &mut rng)
create_random_proof(instance, proving_key, &mut rng).expect("proving should not fail"); .expect("proving should not fail");
// Accumulate the value commitment in the context. We do this to check internal consistency. // Accumulate the value commitment in the context. We do this to check internal consistency.
self.cv_sum -= &value_commitment; // Outputs subtract from the total. self.cv_sum -= &value_commitment; // Outputs subtract from the total.