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).
## [Unreleased]
### Added
- `zcash_proofs::{SpendParameters, OutputParameters}`
### 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`:
- The `verifying_key` argument `SaplingProvingContext::spend_proof` has been
removed. Callers should instead use `SaplingVerifyingContext` to verify

View File

@ -283,11 +283,17 @@ fn stream_params_downloads_to_disk(
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.
pub struct ZcashParameters {
pub spend_params: Parameters<Bls12>,
pub spend_params: SpendParameters,
pub spend_vk: PreparedVerifyingKey<Bls12>,
pub output_params: Parameters<Bls12>,
pub output_params: OutputParameters,
pub output_vk: 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));
ZcashParameters {
spend_params,
spend_params: SpendParameters(spend_params),
spend_vk,
output_params,
output_params: OutputParameters(output_params),
output_vk,
sprout_vk,
}

View File

@ -1,6 +1,6 @@
//! 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 std::path::Path;
use zcash_primitives::{
@ -13,7 +13,10 @@ use zcash_primitives::{
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")]
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
/// locally-accessible paths.
pub struct LocalTxProver {
spend_params: Parameters<Bls12>,
spend_params: SpendParameters,
// TODO: Either re-introduce verification-after-proving (once the verifier is
// refactored), or remove this.
#[allow(unused)]
spend_vk: PreparedVerifyingKey<Bls12>,
output_params: Parameters<Bls12>,
output_params: OutputParameters,
}
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 group::GroupEncoding;
use rand_core::OsRng;
@ -13,6 +13,8 @@ use zcash_primitives::{
transaction::components::Amount,
};
use crate::{OutputParameters, SpendParameters};
/// A context object for creating the Sapling components of a Zcash transaction.
pub struct SaplingProvingContext {
bsk: TrapdoorSum,
@ -48,7 +50,7 @@ impl SaplingProvingContext {
value: u64,
anchor: bls12_381::Scalar,
merkle_path: MerklePath,
proving_key: &Parameters<Bls12>,
proving_key: &SpendParameters,
) -> Result<(Proof<Bls12>, ValueCommitment, PublicKey), ()> {
// Initialize secure RNG
let mut rng = OsRng;
@ -96,8 +98,8 @@ impl SaplingProvingContext {
};
// Create proof
let proof =
create_random_proof(instance, proving_key, &mut rng).expect("proving should not fail");
let proof = create_random_proof(instance, &proving_key.0, &mut rng)
.expect("proving should not fail");
// Accumulate the value commitment in the context
self.cv_sum += &value_commitment;
@ -114,7 +116,7 @@ impl SaplingProvingContext {
payment_address: PaymentAddress,
rcm: jubjub::Fr,
value: u64,
proving_key: &Parameters<Bls12>,
proving_key: &OutputParameters,
) -> (Proof<Bls12>, ValueCommitment) {
// Initialize secure RNG
let mut rng = OsRng;
@ -143,8 +145,8 @@ impl SaplingProvingContext {
};
// Create proof
let proof =
create_random_proof(instance, proving_key, &mut rng).expect("proving should not fail");
let proof = create_random_proof(instance, &proving_key.0, &mut rng)
.expect("proving should not fail");
// Accumulate the value commitment in the context. We do this to check internal consistency.
self.cv_sum -= &value_commitment; // Outputs subtract from the total.