Skip to content

Commit

Permalink
feat: added preset poseidon and keccak
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaMasych committed Nov 8, 2024
1 parent 75a82d3 commit 3d6da01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
24 changes: 22 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use deserializer::{deserialize_proof_with_pubs, deserialize_vk};

use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::RichField;
use plonky2::plonk::config::GenericConfig;
use plonky2::util::serialization::GateSerializer;
use plonky2::plonk::config::{GenericConfig, KeccakGoldilocksConfig, PoseidonGoldilocksConfig};
use plonky2::util::serialization::{DefaultGateSerializer, GateSerializer};
use snafu::Snafu;

pub use deserializer::DeserializeError;
Expand Down Expand Up @@ -54,3 +54,23 @@ where

vk.verify(proof).map_err(|_| VerifyError::Failure)
}

/// Verification with preset Poseidon over Goldilocks config available in `plonky2`.
/// Uses `DefaultGateSerializer`.
pub fn verify_default_poseidon(vk: &[u8], proof: &[u8], pubs: &[u8]) -> Result<(), VerifyError> {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

verify::<F, C, D>(vk, proof, pubs, &DefaultGateSerializer)
}

/// Verification with preset Keccak over Goldilocks config available in `plonky2`.
/// Uses `DefaultGateSerializer`.
pub fn verify_default_keccak(vk: &[u8], proof: &[u8], pubs: &[u8]) -> Result<(), VerifyError> {
const D: usize = 2;
type C = KeccakGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

verify::<F, C, D>(vk, proof, pubs, &DefaultGateSerializer)
}
24 changes: 4 additions & 20 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#[path = "artifacts_generator.rs"]
mod artifacts_generator;

use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2::util::serialization::DefaultGateSerializer;
use plonky2_verifier::{verify, DeserializeError, VerifyError};
use plonky2_verifier::{verify_default_poseidon, DeserializeError, VerifyError};
use std::path::Path;

const PROOF_PATH: &str = "tests/artifacts/proof.bin";
Expand Down Expand Up @@ -31,24 +29,10 @@ fn load_data() -> Result<TestData, String> {
Ok((vk, proof, pubs))
}

/// Helper to run `verify` with preset types.
fn verify_non_generic(vk: &Vec<u8>, proof: &Vec<u8>, pubs: &Vec<u8>) -> Result<(), VerifyError> {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

verify::<F, C, D>(
vk.as_slice(),
proof.as_slice(),
pubs.as_slice(),
&DefaultGateSerializer,
)
}

#[test]
fn should_verify_valid_proof() {
let (vk, proof, pubs) = load_data().expect("Failed to load data");
assert!(verify_non_generic(&vk, &proof, &pubs).is_ok());
assert!(verify_default_poseidon(&vk, &proof, &pubs).is_ok());
}

#[test]
Expand All @@ -59,7 +43,7 @@ fn should_not_deserialize_invalid_pubs() {

assert!(
matches!(
verify_non_generic(&vk, &proof, &pubs),
verify_default_poseidon(&vk, &proof, &pubs),
Err(VerifyError::InvalidData {
cause: DeserializeError::InvalidProof
})
Expand All @@ -77,7 +61,7 @@ fn should_not_verify_false_proof() {

assert!(
matches!(
verify_non_generic(&vk, &proof, &pubs),
verify_default_poseidon(&vk, &proof, &pubs),
Err(VerifyError::Failure { .. })
),
"Expected a Failure error when `proof` is corrupted"
Expand Down

0 comments on commit 3d6da01

Please sign in to comment.