Skip to content

Commit

Permalink
feat: added validation crate
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaMasych committed Nov 8, 2024
1 parent 3d6da01 commit 1dc6915
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
extern crate alloc;

mod deserializer;
pub mod validate;

use deserializer::{deserialize_proof_with_pubs, deserialize_vk};

Expand Down
49 changes: 49 additions & 0 deletions src/validate.rs
Original file line number Diff line number Diff line change
@@ -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<DeserializeError> 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 = <C as GenericConfig<D>>::F;

deserialize_vk::<F, C, D>(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 = <C as GenericConfig<D>>::F;

deserialize_vk::<F, C, D>(vk, &DefaultGateSerializer)
.map(|_| ()) // Discard `Ok` value, map it to `()`
.map_err(ValidateError::from) // Convert `DeserializeError` to `ValidateError`
}

0 comments on commit 1dc6915

Please sign in to comment.