penumbra_proof_params/
traits.rsuse ark_ec::pairing::Pairing;
use ark_groth16::{
r1cs_to_qap::LibsnarkReduction, Groth16, PreparedVerifyingKey, ProvingKey, VerifyingKey,
};
use ark_relations::r1cs::{self, ConstraintMatrices, ConstraintSynthesizer};
use ark_serialize::CanonicalSerialize;
use ark_snark::SNARK;
use decaf377::Bls12_377;
use rand_core::CryptoRngCore;
pub trait DummyWitness: ConstraintSynthesizer<<Bls12_377 as Pairing>::ScalarField> {
fn with_dummy_witness() -> Self;
}
pub fn generate_constraint_matrices<T: DummyWitness>(
) -> ConstraintMatrices<<Bls12_377 as Pairing>::ScalarField> {
let circuit = T::with_dummy_witness();
let cs = r1cs::ConstraintSystem::new_ref();
cs.set_optimization_goal(r1cs::OptimizationGoal::Constraints);
cs.set_mode(r1cs::SynthesisMode::Setup);
circuit
.generate_constraints(cs.clone())
.expect("can generate constraints from circuit");
cs.finalize();
cs.to_matrices()
.expect("can convert R1CS constraints into matrices")
}
pub fn generate_test_parameters<T: DummyWitness>(
rng: &mut impl CryptoRngCore,
) -> (ProvingKey<Bls12_377>, VerifyingKey<Bls12_377>) {
let circuit = T::with_dummy_witness();
Groth16::<Bls12_377, LibsnarkReduction>::circuit_specific_setup(circuit, rng)
.expect("can generate constraints from circuit")
}
pub fn generate_prepared_test_parameters<T: DummyWitness>(
rng: &mut impl CryptoRngCore,
) -> (ProvingKey<Bls12_377>, PreparedVerifyingKey<Bls12_377>) {
let (pk, vk) = generate_test_parameters::<T>(rng);
(pk, vk.into())
}
pub trait VerifyingKeyExt {
fn debug_id(&self) -> String;
}
impl VerifyingKeyExt for VerifyingKey<Bls12_377> {
fn debug_id(&self) -> String {
let mut buf = Vec::new();
self.serialize_compressed(&mut buf)
.expect("can serialize vk");
use sha2::Digest;
let hash = sha2::Sha256::digest(&buf);
use bech32::ToBase32;
bech32::encode("groth16vk", hash.to_base32(), bech32::Variant::Bech32m)
.expect("can encode vk as bech32")
}
}
impl VerifyingKeyExt for PreparedVerifyingKey<Bls12_377> {
fn debug_id(&self) -> String {
self.vk.debug_id()
}
}
pub trait ProvingKeyExt {
fn debug_id(&self) -> String;
}
impl ProvingKeyExt for ProvingKey<Bls12_377> {
fn debug_id(&self) -> String {
let mut buf = Vec::new();
self.serialize_compressed(&mut buf)
.expect("can serialize pk");
use sha2::Digest;
let hash = sha2::Sha256::digest(&buf);
use bech32::ToBase32;
bech32::encode("groth16pk", hash.to_base32(), bech32::Variant::Bech32m)
.expect("can encode pk as bech32")
}
}