Skip to content

Commit

Permalink
feat: custom gate serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaMasych committed Dec 26, 2024
1 parent bdfdafa commit 66cd35b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ resolver = "2"

[dependencies]
plonky2 = { git = "https://github.com/distributed-lab/plonky2", branch = "stable", default-features = false}

snafu = { version = "0.8.5", default-features = false }

[dev-dependencies]
Expand All @@ -24,4 +25,4 @@ default = ["std"]
std = [
"plonky2/std",
"snafu/std",
]
]
47 changes: 47 additions & 0 deletions src/deserializer/custom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::RichField;
use plonky2::util::serialization::GateSerializer;
use plonky2::{get_gate_tag_impl, impl_gate_serializer, read_gate_impl};

use plonky2::gates::arithmetic_base::ArithmeticGate;
use plonky2::gates::arithmetic_extension::ArithmeticExtensionGate;
use plonky2::gates::base_sum::BaseSumGate;
use plonky2::gates::constant::ConstantGate;
use plonky2::gates::coset_interpolation::CosetInterpolationGate;
use plonky2::gates::exponentiation::ExponentiationGate;
use plonky2::gates::lookup::LookupGate;
use plonky2::gates::lookup_table::LookupTableGate;
use plonky2::gates::multiplication_extension::MulExtensionGate;
use plonky2::gates::noop::NoopGate;
use plonky2::gates::poseidon::PoseidonGate;
use plonky2::gates::poseidon_mds::PoseidonMdsGate;
use plonky2::gates::public_input::PublicInputGate;
use plonky2::gates::random_access::RandomAccessGate;
use plonky2::gates::reducing::ReducingGate;
use plonky2::gates::reducing_extension::ReducingExtensionGate;

/// zkVerify employs gate serializer which essentially supports the same set,
/// as `DefaultGateSerializer` from `plonky2`, but with possibility of extending it in future.
#[derive(Debug)]
pub struct ZKVerifyGateSerializer;
impl<F: RichField + Extendable<D>, const D: usize> GateSerializer<F, D> for ZKVerifyGateSerializer {
impl_gate_serializer! {
ZKVerifyGateSerializer,
ArithmeticGate,
ArithmeticExtensionGate<D>,
BaseSumGate<2>,
ConstantGate,
CosetInterpolationGate<F, D>,
ExponentiationGate<F, D>,
LookupGate,
LookupTableGate,
MulExtensionGate<D>,
NoopGate,
PoseidonMdsGate<F, D>,
PoseidonGate<F, D>,
PublicInputGate,
RandomAccessGate<F, D>,
ReducingExtensionGate<D>,
ReducingGate<D>
}
}
8 changes: 5 additions & 3 deletions src/deserializer.rs → src/deserializer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
pub mod custom;

use custom::ZKVerifyGateSerializer;

use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::RichField;
use plonky2::plonk::circuit_data::{CommonCircuitData, VerifierCircuitData};
use plonky2::plonk::config::GenericConfig;
use plonky2::plonk::proof::ProofWithPublicInputs;
use plonky2::util::serialization::GateSerializer;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
Expand All @@ -22,13 +25,12 @@ pub enum DeserializeError {

pub(crate) fn deserialize_vk<F, C, const D: usize>(
vk: &[u8],
gs: &dyn GateSerializer<F, D>,
) -> Result<VerifierCircuitData<F, C, D>, DeserializeError>
where
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
{
VerifierCircuitData::<F, C, D>::from_bytes(Vec::from(vk), gs)
VerifierCircuitData::<F, C, D>::from_bytes(Vec::from(vk), &ZKVerifyGateSerializer)
.map_err(|_| DeserializeError::InvalidVerificationKey)
}

Expand Down
18 changes: 5 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ use deserializer::{deserialize_proof_with_pubs, deserialize_vk};
use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::RichField;
use plonky2::plonk::config::{GenericConfig, KeccakGoldilocksConfig, PoseidonGoldilocksConfig};
use plonky2::util::serialization::{DefaultGateSerializer, GateSerializer};
use snafu::Snafu;

pub use deserializer::DeserializeError;
pub use deserializer::{custom::ZKVerifyGateSerializer, DeserializeError};

/// Verification error.
#[derive(Debug, Snafu)]
Expand All @@ -41,38 +40,31 @@ impl From<DeserializeError> for VerifyError {

/// Verify the given proof `proof` and public inputs `pubs` using verification key `vk`.
/// Use the given verification key `vk` to verify the proof `proof` against the public inputs `pubs`.
pub fn verify<F, C, const D: usize>(
vk: &[u8],
proof: &[u8],
pubs: &[u8],
gs: &dyn GateSerializer<F, D>,
) -> Result<(), VerifyError>
pub fn verify<F, C, const D: usize>(vk: &[u8], proof: &[u8], pubs: &[u8]) -> Result<(), VerifyError>
where
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
{
let vk = deserialize_vk::<F, C, D>(vk, gs)?;
let vk = deserialize_vk::<F, C, D>(vk)?;
let proof = deserialize_proof_with_pubs::<F, C, D>(proof, pubs, &vk.common)?;

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)
verify::<F, C, D>(vk, proof, pubs)
}

/// 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)
verify::<F, C, D>(vk, proof, pubs)
}
7 changes: 2 additions & 5 deletions src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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.
Expand All @@ -25,25 +24,23 @@ impl From<DeserializeError> for ValidateError {
}

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

0 comments on commit 66cd35b

Please sign in to comment.