openvm_sdk/verifier/internal/
types.rsuse std::{array, borrow::BorrowMut};
use derivative::Derivative;
use openvm_circuit::circuit_derive::AlignedBorrow;
use openvm_native_compiler::{
ir::{Builder, Config, Felt},
prelude::DIGEST_SIZE,
};
use openvm_stark_sdk::{
config::baby_bear_poseidon2::BabyBearPoseidon2Config,
openvm_stark_backend::{
config::{Com, StarkGenericConfig, Val},
p3_field::PrimeField32,
prover::types::Proof,
},
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use static_assertions::assert_impl_all;
use crate::{verifier::common::types::VmVerifierPvs, SC};
#[derive(Serialize, Deserialize, Derivative)]
#[serde(bound = "")]
#[derivative(Clone(bound = "Com<SC>: Clone"))]
pub struct InternalVmVerifierInput<SC: StarkGenericConfig> {
pub self_program_commit: [Val<SC>; DIGEST_SIZE],
pub proofs: Vec<Proof<SC>>,
}
assert_impl_all!(InternalVmVerifierInput<BabyBearPoseidon2Config>: Serialize, DeserializeOwned);
impl InternalVmVerifierInput<SC> {
pub fn chunk_leaf_or_internal_proofs(
self_program_commit: [Val<SC>; DIGEST_SIZE],
proofs: &[Proof<SC>],
chunk: usize,
) -> Vec<Self> {
proofs
.chunks(chunk)
.map(|chunk| Self {
self_program_commit,
proofs: chunk.to_vec(),
})
.collect()
}
}
#[derive(Debug, Clone, Copy, AlignedBorrow)]
#[repr(C)]
pub struct InternalVmVerifierPvs<T> {
pub vm_verifier_pvs: VmVerifierPvs<T>,
pub extra_pvs: InternalVmVerifierExtraPvs<T>,
}
impl<F: PrimeField32> InternalVmVerifierPvs<Felt<F>> {
pub fn uninit<C: Config<F = F>>(builder: &mut Builder<C>) -> Self {
Self {
vm_verifier_pvs: VmVerifierPvs::<Felt<F>>::uninit(builder),
extra_pvs: InternalVmVerifierExtraPvs::<Felt<F>>::uninit(builder),
}
}
}
impl<F: Default + Clone> InternalVmVerifierPvs<Felt<F>> {
pub fn flatten(self) -> Vec<Felt<F>> {
let mut v = vec![Felt(0, Default::default()); InternalVmVerifierPvs::<u8>::width()];
*v.as_mut_slice().borrow_mut() = self;
v
}
}
#[derive(Debug, Clone, Copy, AlignedBorrow)]
#[repr(C)]
pub struct InternalVmVerifierExtraPvs<T> {
pub leaf_verifier_commit: [T; DIGEST_SIZE],
pub internal_program_commit: [T; DIGEST_SIZE],
}
impl<F: PrimeField32> InternalVmVerifierExtraPvs<Felt<F>> {
pub fn uninit<C: Config<F = F>>(builder: &mut Builder<C>) -> Self {
Self {
leaf_verifier_commit: array::from_fn(|_| builder.uninit()),
internal_program_commit: array::from_fn(|_| builder.uninit()),
}
}
}
impl<F: Default + Clone> InternalVmVerifierExtraPvs<Felt<F>> {
pub fn flatten(self) -> Vec<Felt<F>> {
let mut v = vec![Felt(0, Default::default()); InternalVmVerifierExtraPvs::<u8>::width()];
*v.as_mut_slice().borrow_mut() = self;
v
}
}