use bellman::groth16::{Parameters, PreparedVerifyingKey};
use bls12_381::Bls12;
use zcash_primitives::{
merkle_tree::MerklePath,
primitives::{Diversifier, PaymentAddress, ProofGenerationKey, Rseed},
prover::TxProver,
redjubjub::{PublicKey, Signature},
sapling::Node,
transaction::components::{Amount, GROTH_PROOF_SIZE},
};
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;
pub struct LocalTxProver {
spend_params: Parameters<Bls12>,
spend_vk: PreparedVerifyingKey<Bls12>,
output_params: Parameters<Bls12>,
}
impl LocalTxProver {
#[cfg(feature = "local-prover")]
#[cfg_attr(docsrs, doc(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);
LocalTxProver {
spend_params,
spend_vk,
output_params,
}
}
#[cfg(feature = "local-prover")]
#[cfg_attr(docsrs, doc(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() {
(
params_dir.join(SAPLING_SPEND_NAME),
params_dir.join(SAPLING_OUTPUT_NAME),
)
} else {
return None;
};
if !(spend_path.exists() && output_path.exists()) {
return None;
}
Some(LocalTxProver::new(&spend_path, &output_path))
}
#[cfg(feature = "bundled-prover")]
#[cfg_attr(docsrs, doc(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 {
type SaplingProvingContext = SaplingProvingContext;
fn new_sapling_proving_context(&self) -> Self::SaplingProvingContext {
SaplingProvingContext::new()
}
fn spend_proof(
&self,
ctx: &mut Self::SaplingProvingContext,
proof_generation_key: ProofGenerationKey,
diversifier: Diversifier,
rseed: Rseed,
ar: jubjub::Fr,
value: u64,
anchor: bls12_381::Scalar,
merkle_path: MerklePath<Node>,
) -> Result<([u8; GROTH_PROOF_SIZE], jubjub::ExtendedPoint, PublicKey), ()> {
let (proof, cv, rk) = ctx.spend_proof(
proof_generation_key,
diversifier,
rseed,
ar,
value,
anchor,
merkle_path,
&self.spend_params,
&self.spend_vk,
)?;
let mut zkproof = [0u8; GROTH_PROOF_SIZE];
proof
.write(&mut zkproof[..])
.expect("should be able to serialize a proof");
Ok((zkproof, cv, rk))
}
fn output_proof(
&self,
ctx: &mut Self::SaplingProvingContext,
esk: jubjub::Fr,
payment_address: PaymentAddress,
rcm: jubjub::Fr,
value: u64,
) -> ([u8; GROTH_PROOF_SIZE], jubjub::ExtendedPoint) {
let (proof, cv) = ctx.output_proof(esk, payment_address, rcm, value, &self.output_params);
let mut zkproof = [0u8; GROTH_PROOF_SIZE];
proof
.write(&mut zkproof[..])
.expect("should be able to serialize a proof");
(zkproof, cv)
}
fn binding_sig(
&self,
ctx: &mut Self::SaplingProvingContext,
value_balance: Amount,
sighash: &[u8; 32],
) -> Result<Signature, ()> {
ctx.binding_sig(value_balance, sighash)
}
}