snarkvm_synthesizer_snark/verifying_key/
mod.rsuse super::*;
mod bytes;
mod parse;
mod serialize;
use std::collections::BTreeMap;
#[derive(Clone, PartialEq, Eq)]
pub struct VerifyingKey<N: Network> {
verifying_key: Arc<varuna::CircuitVerifyingKey<N::PairingCurve>>,
num_variables: u64,
}
impl<N: Network> VerifyingKey<N> {
pub const fn new(verifying_key: Arc<varuna::CircuitVerifyingKey<N::PairingCurve>>, num_variables: u64) -> Self {
Self { verifying_key, num_variables }
}
pub fn num_variables(&self) -> u64 {
self.num_variables
}
pub fn verify(&self, function_name: &str, inputs: &[N::Field], proof: &Proof<N>) -> bool {
#[cfg(feature = "aleo-cli")]
let timer = std::time::Instant::now();
let universal_verifier = N::varuna_universal_verifier();
let fiat_shamir = N::varuna_fs_parameters();
#[allow(clippy::manual_unwrap_or_default)]
match Varuna::<N>::verify(universal_verifier, fiat_shamir, self, inputs, proof) {
Ok(is_valid) => {
#[cfg(feature = "aleo-cli")]
println!(
"{}",
format!(" • Verified '{function_name}' (in {} ms)", timer.elapsed().as_millis()).dimmed()
);
is_valid
}
Err(error) => {
#[cfg(feature = "aleo-cli")]
println!("{}", format!(" • Verifier failed: {error}").dimmed());
false
}
}
}
#[allow(clippy::type_complexity)]
pub fn verify_batch(
locator: &str,
inputs: Vec<(VerifyingKey<N>, Vec<Vec<N::Field>>)>,
proof: &Proof<N>,
) -> Result<()> {
#[cfg(feature = "aleo-cli")]
let timer = std::time::Instant::now();
let num_expected_keys = inputs.len();
let keys_to_inputs: BTreeMap<_, _> =
inputs.iter().map(|(verifying_key, inputs)| (verifying_key.deref(), inputs.as_slice())).collect();
ensure!(keys_to_inputs.len() == num_expected_keys, "Incorrect number of verifying keys for batch proof");
let universal_verifier = N::varuna_universal_verifier();
let fiat_shamir = N::varuna_fs_parameters();
match Varuna::<N>::verify_batch(universal_verifier, fiat_shamir, &keys_to_inputs, proof) {
Ok(is_valid) => {
#[cfg(feature = "aleo-cli")]
println!(
"{}",
format!(" • Verified '{locator}': {is_valid} (in {} ms)", timer.elapsed().as_millis()).dimmed()
);
if is_valid { Ok(()) } else { bail!("'verify_batch' failed") }
}
Err(error) => {
#[cfg(feature = "aleo-cli")]
println!("{}", format!(" • Verifier failed: {error}").dimmed());
bail!(error)
}
}
}
}
impl<N: Network> Deref for VerifyingKey<N> {
type Target = varuna::CircuitVerifyingKey<N::PairingCurve>;
fn deref(&self) -> &Self::Target {
&self.verifying_key
}
}