use midnight_proofs::poly::kzg::params::ParamsKZG;
use midnight_zk_stdlib::{MidnightVK, ZkStdLibArch};
use super::{
circuit::{InnerCircuitsContext, ProofAggregation},
claims::{Claim, TypedStatement},
AggregableRelation,
};
use crate::ivc::{self, IvcError, IvcInstance, E};
impl ProofAggregation {
pub fn setup(
aggregator_srs: ParamsKZG<E>,
aggregator_k: u32,
inner_ctx: InnerCircuitsContext,
) -> (Aggregator, Verifier) {
ivc::setup::<ProofAggregation>(aggregator_srs, aggregator_k, inner_ctx)
}
}
#[derive(Clone, Debug)]
pub struct AggregationWitness {
pub(crate) claim: Claim,
pub(crate) inner_proof: Vec<u8>,
arch: ZkStdLibArch,
}
impl AggregationWitness {
pub fn new<R: AggregableRelation + Default + std::fmt::Debug + 'static>(
vk: MidnightVK,
instance: R::Instance,
inner_proof: Vec<u8>,
) -> Self
where
R::Instance: std::fmt::Debug + Clone,
{
let statement = Box::new(TypedStatement::<R>::new(instance));
AggregationWitness {
claim: Claim { vk, statement },
inner_proof,
arch: R::default().used_chips(),
}
}
}
pub type Aggregator = ivc::IvcProver<ProofAggregation>;
impl Aggregator {
pub fn aggregate(&mut self, witness: AggregationWitness) -> Result<Vec<u8>, IvcError> {
if witness.arch != self.relation.ctx().arch() {
return Err(IvcError::InvalidWitness(format!(
"architecture mismatch: expected {:?}, got {:?}",
self.relation.ctx().arch(),
witness.arch,
)));
}
self.prove_step(witness)
}
}
pub type Verifier = ivc::IvcVerifier<ProofAggregation>;
impl Verifier {
pub fn verify_aggregation(
&self,
instance: &IvcInstance<ProofAggregation>,
proof: &[u8],
) -> Result<(), IvcError> {
self.verify(instance, proof)
}
}