penumbra_proof_params/
lib.rs#![deny(clippy::unwrap_used)]
#![allow(clippy::redundant_static_lifetimes)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
use anyhow::{bail, Result};
use ark_groth16::{PreparedVerifyingKey, ProvingKey, VerifyingKey};
use ark_serialize::CanonicalDeserialize;
use decaf377::Bls12_377;
use once_cell::sync::{Lazy, OnceCell};
use std::ops::Deref;
pub const GROTH16_PROOF_LENGTH_BYTES: usize = 192;
mod traits;
pub use traits::{
generate_constraint_matrices, generate_prepared_test_parameters, generate_test_parameters,
DummyWitness, ProvingKeyExt, VerifyingKeyExt,
};
#[derive(Debug, Default)]
pub struct LazyProvingKey {
pk_id: &'static str,
inner: OnceCell<ProvingKey<Bls12_377>>,
}
impl LazyProvingKey {
fn new(pk_id: &'static str) -> Self {
LazyProvingKey {
pk_id,
inner: OnceCell::new(),
}
}
pub fn try_load(&self, bytes: &[u8]) -> Result<&ProvingKey<Bls12_377>> {
self.inner.get_or_try_init(|| {
let pk = ProvingKey::deserialize_uncompressed_unchecked(bytes)?;
let pk_id = pk.debug_id();
if pk_id != self.pk_id {
bail!(
"proving key ID mismatch: expected {}, loaded {}",
self.pk_id,
pk_id
);
}
Ok(pk)
})
}
pub fn try_load_unchecked(&self, bytes: &[u8]) -> Result<&ProvingKey<Bls12_377>> {
self.inner.get_or_try_init(|| {
let pk = ProvingKey::deserialize_uncompressed_unchecked(bytes)?;
Ok(pk)
})
}
}
impl Deref for LazyProvingKey {
type Target = ProvingKey<Bls12_377>;
fn deref(&self) -> &Self::Target {
self.inner.get().expect("Proving key cannot be loaded!")
}
}
pub static SPEND_PROOF_PROVING_KEY: Lazy<LazyProvingKey> = Lazy::new(|| {
let spend_proving_key = LazyProvingKey::new(spend::PROVING_KEY_ID);
#[cfg(feature = "bundled-proving-keys")]
spend_proving_key
.try_load(include_bytes!("gen/spend_pk.bin"))
.expect("bundled proving key is valid");
spend_proving_key
});
pub static SPEND_PROOF_VERIFICATION_KEY: Lazy<PreparedVerifyingKey<Bls12_377>> =
Lazy::new(|| spend_verification_parameters().into());
pub mod spend {
include!("gen/spend_id.rs");
}
pub static OUTPUT_PROOF_PROVING_KEY: Lazy<LazyProvingKey> = Lazy::new(|| {
let output_proving_key = LazyProvingKey::new(output::PROVING_KEY_ID);
#[cfg(feature = "bundled-proving-keys")]
output_proving_key
.try_load(include_bytes!("gen/output_pk.bin"))
.expect("bundled proving key is valid");
output_proving_key
});
pub static OUTPUT_PROOF_VERIFICATION_KEY: Lazy<PreparedVerifyingKey<Bls12_377>> =
Lazy::new(|| output_verification_parameters().into());
pub mod output {
include!("gen/output_id.rs");
}
pub static SWAP_PROOF_PROVING_KEY: Lazy<LazyProvingKey> = Lazy::new(|| {
let swap_proving_key = LazyProvingKey::new(swap::PROVING_KEY_ID);
#[cfg(feature = "bundled-proving-keys")]
swap_proving_key
.try_load(include_bytes!("gen/swap_pk.bin"))
.expect("bundled proving key is valid");
swap_proving_key
});
pub static SWAP_PROOF_VERIFICATION_KEY: Lazy<PreparedVerifyingKey<Bls12_377>> =
Lazy::new(|| swap_verification_parameters().into());
pub mod swap {
include!("gen/swap_id.rs");
}
pub static SWAPCLAIM_PROOF_PROVING_KEY: Lazy<LazyProvingKey> = Lazy::new(|| {
let swapclaim_proving_key = LazyProvingKey::new(swapclaim::PROVING_KEY_ID);
#[cfg(feature = "bundled-proving-keys")]
swapclaim_proving_key
.try_load(include_bytes!("gen/swapclaim_pk.bin"))
.expect("bundled proving key is valid");
swapclaim_proving_key
});
pub static SWAPCLAIM_PROOF_VERIFICATION_KEY: Lazy<PreparedVerifyingKey<Bls12_377>> =
Lazy::new(|| swapclaim_verification_parameters().into());
pub mod swapclaim {
include!("gen/swapclaim_id.rs");
}
pub static CONVERT_PROOF_PROVING_KEY: Lazy<LazyProvingKey> = Lazy::new(|| {
let convert_proving_key = LazyProvingKey::new(convert::PROVING_KEY_ID);
#[cfg(feature = "bundled-proving-keys")]
convert_proving_key
.try_load(include_bytes!("gen/convert_pk.bin"))
.expect("bundled proving key is valid");
convert_proving_key
});
pub static CONVERT_PROOF_VERIFICATION_KEY: Lazy<PreparedVerifyingKey<Bls12_377>> =
Lazy::new(|| convert_verification_parameters().into());
pub mod convert {
include!("gen/convert_id.rs");
}
pub static DELEGATOR_VOTE_PROOF_PROVING_KEY: Lazy<LazyProvingKey> = Lazy::new(|| {
let delegator_vote_proving_key = LazyProvingKey::new(delegator_vote::PROVING_KEY_ID);
#[cfg(feature = "bundled-proving-keys")]
delegator_vote_proving_key
.try_load(include_bytes!("gen/delegator_vote_pk.bin"))
.expect("bundled proving key is valid");
delegator_vote_proving_key
});
pub static DELEGATOR_VOTE_PROOF_VERIFICATION_KEY: Lazy<PreparedVerifyingKey<Bls12_377>> =
Lazy::new(|| delegator_vote_verification_parameters().into());
pub mod delegator_vote {
include!("gen/delegator_vote_id.rs");
}
pub static NULLIFIER_DERIVATION_PROOF_PROVING_KEY: Lazy<LazyProvingKey> = Lazy::new(|| {
let nullifier_proving_key = LazyProvingKey::new(nullifier_derivation::PROVING_KEY_ID);
#[cfg(feature = "bundled-proving-keys")]
nullifier_proving_key
.try_load(include_bytes!("gen/nullifier_derivation_pk.bin"))
.expect("bundled proving key is valid");
nullifier_proving_key
});
pub static NULLIFIER_DERIVATION_PROOF_VERIFICATION_KEY: Lazy<PreparedVerifyingKey<Bls12_377>> =
Lazy::new(|| nullifier_derivation_verification_parameters().into());
pub mod nullifier_derivation {
include!("gen/nullifier_derivation_id.rs");
}
fn spend_verification_parameters() -> VerifyingKey<Bls12_377> {
let vk_params = include_bytes!("gen/spend_vk.param");
VerifyingKey::deserialize_uncompressed_unchecked(&vk_params[..])
.expect("can deserialize VerifyingKey")
}
fn output_verification_parameters() -> VerifyingKey<Bls12_377> {
let vk_params = include_bytes!("gen/output_vk.param");
VerifyingKey::deserialize_uncompressed_unchecked(&vk_params[..])
.expect("can deserialize VerifyingKey")
}
fn swap_verification_parameters() -> VerifyingKey<Bls12_377> {
let vk_params = include_bytes!("gen/swap_vk.param");
VerifyingKey::deserialize_uncompressed_unchecked(&vk_params[..])
.expect("can deserialize VerifyingKey")
}
fn swapclaim_verification_parameters() -> VerifyingKey<Bls12_377> {
let vk_params = include_bytes!("gen/swapclaim_vk.param");
VerifyingKey::deserialize_uncompressed_unchecked(&vk_params[..])
.expect("can deserialize VerifyingKey")
}
fn convert_verification_parameters() -> VerifyingKey<Bls12_377> {
let vk_params = include_bytes!("gen/convert_vk.param");
VerifyingKey::deserialize_uncompressed_unchecked(&vk_params[..])
.expect("can deserialize VerifyingKey")
}
fn delegator_vote_verification_parameters() -> VerifyingKey<Bls12_377> {
let vk_params = include_bytes!("gen/delegator_vote_vk.param");
VerifyingKey::deserialize_uncompressed_unchecked(&vk_params[..])
.expect("can deserialize VerifyingKey")
}
fn nullifier_derivation_verification_parameters() -> VerifyingKey<Bls12_377> {
let vk_params = include_bytes!("gen/nullifier_derivation_vk.param");
VerifyingKey::deserialize_uncompressed_unchecked(&vk_params[..])
.expect("can deserialize VerifyingKey")
}