halo2_axiom/poly/kzg/
strategy.rsuse super::{
commitment::{KZGCommitmentScheme, ParamsKZG},
msm::DualMSM,
};
use crate::{
helpers::SerdeCurveAffine,
plonk::Error,
poly::{
commitment::Verifier,
strategy::{Guard, VerificationStrategy},
},
};
use ff::Field;
use pairing::{Engine, MultiMillerLoop};
use rand_core::OsRng;
use std::fmt::Debug;
#[derive(Debug, Clone)]
pub struct GuardKZG<'params, E: MultiMillerLoop + Debug> {
pub(crate) msm_accumulator: DualMSM<'params, E>,
}
impl<'params, E> Guard<KZGCommitmentScheme<E>> for GuardKZG<'params, E>
where
E: MultiMillerLoop + Debug,
E::G1Affine: SerdeCurveAffine<ScalarExt = E::Fr, CurveExt = E::G1>,
E::G2Affine: SerdeCurveAffine,
{
type MSMAccumulator = DualMSM<'params, E>;
}
impl<'params, E: MultiMillerLoop + Debug> GuardKZG<'params, E> {
pub(crate) fn new(msm_accumulator: DualMSM<'params, E>) -> Self {
Self { msm_accumulator }
}
}
#[derive(Clone, Debug)]
pub struct AccumulatorStrategy<'params, E: Engine> {
pub(crate) msm_accumulator: DualMSM<'params, E>,
}
impl<'params, E: MultiMillerLoop + Debug> AccumulatorStrategy<'params, E> {
pub fn new(params: &'params ParamsKZG<E>) -> Self {
AccumulatorStrategy {
msm_accumulator: DualMSM::new(params),
}
}
pub fn with(msm_accumulator: DualMSM<'params, E>) -> Self {
AccumulatorStrategy { msm_accumulator }
}
}
#[derive(Clone, Debug)]
pub struct SingleStrategy<'params, E: Engine> {
pub(crate) msm: DualMSM<'params, E>,
}
impl<'params, E: MultiMillerLoop + Debug> SingleStrategy<'params, E> {
pub fn new(params: &'params ParamsKZG<E>) -> Self {
SingleStrategy {
msm: DualMSM::new(params),
}
}
}
impl<
'params,
E: MultiMillerLoop + Debug,
V: Verifier<
'params,
KZGCommitmentScheme<E>,
MSMAccumulator = DualMSM<'params, E>,
Guard = GuardKZG<'params, E>,
>,
> VerificationStrategy<'params, KZGCommitmentScheme<E>, V> for AccumulatorStrategy<'params, E>
where
E::G1Affine: SerdeCurveAffine<ScalarExt = E::Fr, CurveExt = E::G1>,
E::G2Affine: SerdeCurveAffine,
{
type Output = Self;
fn new(params: &'params ParamsKZG<E>) -> Self {
AccumulatorStrategy::new(params)
}
fn process(
mut self,
f: impl FnOnce(V::MSMAccumulator) -> Result<V::Guard, Error>,
) -> Result<Self::Output, Error> {
self.msm_accumulator.scale(E::Fr::random(OsRng));
let guard = f(self.msm_accumulator)?;
Ok(Self {
msm_accumulator: guard.msm_accumulator,
})
}
fn finalize(self) -> bool {
self.msm_accumulator.check()
}
}
impl<
'params,
E: MultiMillerLoop + Debug,
V: Verifier<
'params,
KZGCommitmentScheme<E>,
MSMAccumulator = DualMSM<'params, E>,
Guard = GuardKZG<'params, E>,
>,
> VerificationStrategy<'params, KZGCommitmentScheme<E>, V> for SingleStrategy<'params, E>
where
E::G1Affine: SerdeCurveAffine<ScalarExt = E::Fr, CurveExt = E::G1>,
E::G2Affine: SerdeCurveAffine,
{
type Output = ();
fn new(params: &'params ParamsKZG<E>) -> Self {
Self::new(params)
}
fn process(
self,
f: impl FnOnce(V::MSMAccumulator) -> Result<V::Guard, Error>,
) -> Result<Self::Output, Error> {
let guard = f(self.msm)?;
let msm = guard.msm_accumulator;
if msm.check() {
Ok(())
} else {
Err(Error::ConstraintSystemFailure)
}
}
fn finalize(self) -> bool {
unreachable!();
}
}