diff --git a/zcash_proofs/CHANGELOG.md b/zcash_proofs/CHANGELOG.md index f544343b6..1971f9359 100644 --- a/zcash_proofs/CHANGELOG.md +++ b/zcash_proofs/CHANGELOG.md @@ -8,6 +8,8 @@ and this library adheres to Rust's notion of ## [Unreleased] ### Added - `zcash_proofs::{SpendParameters, OutputParameters}` +- `impl zcash_primitives::sapling::prover::{SpendProver, OutputProver}` for + `zcash_proofs::prover::LocalTxProver` ### Changed - The new `SpendParameters` and `OutputParameters` types are used in the @@ -110,7 +112,7 @@ and this library adheres to Rust's notion of ### Added - `zcash_proofs::ZcashParameters` - `zcash_proofs::parse_parameters` -- `zcash_proofs::prover::LocalProver::from_bytes` +- `zcash_proofs::prover::LocalTxProver::from_bytes` - The `zcash_proofs::constants` module, containing constants and helpers used by the `zcash_proofs::circuit::ecc::fixed_base_multiplication` gadget: - The `FixedGeneratorOwned` type alias. diff --git a/zcash_proofs/src/prover.rs b/zcash_proofs/src/prover.rs index 8bbd1abff..3ddbcfcd4 100644 --- a/zcash_proofs/src/prover.rs +++ b/zcash_proofs/src/prover.rs @@ -1,16 +1,17 @@ //! Abstractions over the proving system and parameters for ease of use. -use bellman::groth16::PreparedVerifyingKey; +use bellman::groth16::{PreparedVerifyingKey, Proof}; use bls12_381::Bls12; use std::path::Path; use zcash_primitives::{ sapling::{ - prover::TxProver, + self, + prover::{OutputProver, SpendProver, TxProver}, redjubjub::{PublicKey, Signature}, - value::ValueCommitment, + value::{NoteValue, ValueCommitTrapdoor, ValueCommitment}, Diversifier, MerklePath, PaymentAddress, ProofGenerationKey, Rseed, }, - transaction::components::{Amount, GROTH_PROOF_SIZE}, + transaction::components::{sapling::GrothProofBytes, Amount, GROTH_PROOF_SIZE}, }; use crate::{ @@ -143,6 +144,78 @@ impl LocalTxProver { } } +impl SpendProver for LocalTxProver { + type Proof = Proof; + + fn prepare_circuit( + proof_generation_key: ProofGenerationKey, + diversifier: Diversifier, + rseed: Rseed, + value: NoteValue, + alpha: jubjub::Fr, + rcv: ValueCommitTrapdoor, + anchor: bls12_381::Scalar, + merkle_path: MerklePath, + ) -> Option { + SpendParameters::prepare_circuit( + proof_generation_key, + diversifier, + rseed, + value, + alpha, + rcv, + anchor, + merkle_path, + ) + } + + fn create_proof( + &self, + circuit: sapling::circuit::Spend, + rng: &mut R, + ) -> Self::Proof { + self.spend_params.create_proof(circuit, rng) + } + + fn encode_proof(proof: Self::Proof) -> GrothProofBytes { + let mut zkproof = [0u8; GROTH_PROOF_SIZE]; + proof + .write(&mut zkproof[..]) + .expect("should be able to serialize a proof"); + zkproof + } +} + +impl OutputProver for LocalTxProver { + type Proof = Proof; + + fn prepare_circuit( + esk: jubjub::Fr, + payment_address: PaymentAddress, + rcm: jubjub::Fr, + value: NoteValue, + rcv: ValueCommitTrapdoor, + ) -> sapling::circuit::Output { + OutputParameters::prepare_circuit(esk, payment_address, rcm, value, rcv) + } + + fn create_proof( + &self, + circuit: sapling::circuit::Output, + rng: &mut R, + ) -> Self::Proof { + self.output_params.create_proof(circuit, rng) + } + + fn encode_proof(proof: Self::Proof) -> GrothProofBytes { + let mut zkproof = [0u8; GROTH_PROOF_SIZE]; + proof + .write(&mut zkproof[..]) + .expect("should be able to serialize a proof"); + zkproof + } +} + impl TxProver for LocalTxProver { type SaplingProvingContext = SaplingProvingContext;