From 1dc6915dea1bdce130c83fd3c80b8c03099d81a5 Mon Sep 17 00:00:00 2001 From: Nikita Masych Date: Fri, 8 Nov 2024 14:46:01 +0200 Subject: [PATCH] feat: added validation crate --- src/lib.rs | 1 + src/validate.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/validate.rs diff --git a/src/lib.rs b/src/lib.rs index 65366eb..dc9b685 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ extern crate alloc; mod deserializer; +pub mod validate; use deserializer::{deserialize_proof_with_pubs, deserialize_vk}; diff --git a/src/validate.rs b/src/validate.rs new file mode 100644 index 0000000..f88485f --- /dev/null +++ b/src/validate.rs @@ -0,0 +1,49 @@ +//! Validation crate centered for plonky2-verifier. + +use crate::deserializer::deserialize_vk; +use crate::DeserializeError; +use plonky2::plonk::config::{GenericConfig, KeccakGoldilocksConfig, PoseidonGoldilocksConfig}; +use plonky2::util::serialization::DefaultGateSerializer; +use snafu::Snafu; + +/// Validation error. +#[derive(Debug, Snafu)] +pub enum ValidateError { + /// Invalid data. + #[snafu(display("Invalid data: [{}]", cause))] + InvalidVK { + /// Internal error. + #[snafu(source)] + cause: DeserializeError, + }, +} + +impl From for ValidateError { + fn from(value: DeserializeError) -> Self { + ValidateError::InvalidVK { cause: value } + } +} + +/// Validate vk with preset Poseidon over Goldilocks config available in `plonky2`. +/// Uses `DefaultGateSerializer`. +pub fn validate_vk_default_poseidon(vk: &[u8]) -> Result<(), ValidateError> { + const D: usize = 2; + type C = PoseidonGoldilocksConfig; + type F = >::F; + + deserialize_vk::(vk, &DefaultGateSerializer) + .map(|_| ()) // Discard `Ok` value, map it to `()` + .map_err(ValidateError::from) // Convert `DeserializeError` to `ValidateError` +} + +/// Validate vk with preset Keccak over Goldilocks config available in `plonky2`. +/// Uses `DefaultGateSerializer`. +pub fn validate_vk_default_keccak(vk: &[u8]) -> Result<(), ValidateError> { + const D: usize = 2; + type C = KeccakGoldilocksConfig; + type F = >::F; + + deserialize_vk::(vk, &DefaultGateSerializer) + .map(|_| ()) // Discard `Ok` value, map it to `()` + .map_err(ValidateError::from) // Convert `DeserializeError` to `ValidateError` +}