Skip to content

Commit

Permalink
simplify trait bound
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Feb 1, 2024
1 parent bba681c commit 2748862
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 56 deletions.
8 changes: 3 additions & 5 deletions snark-verifier/src/pcs/kzg.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! [KZG](<https://www.iacr.org/archive/asiacrypt2010/6477178/6477178.pdf>)
//! polynomial commitment scheme and accumulation scheme.
use crate::util::arithmetic::CurveAffine;

mod accumulation;
mod accumulator;
mod decider;
Expand All @@ -18,19 +16,19 @@ pub use accumulator::LimbsEncodingInstructions;

/// KZG succinct verifying key.
#[derive(Clone, Copy, Debug)]
pub struct KzgSuccinctVerifyingKey<C: CurveAffine> {
pub struct KzgSuccinctVerifyingKey<C> {
/// Generator.
pub g: C,
}

impl<C: CurveAffine> KzgSuccinctVerifyingKey<C> {
impl<C> KzgSuccinctVerifyingKey<C> {
/// Initialize a [`KzgSuccinctVerifyingKey`].
pub fn new(g: C) -> Self {
Self { g }
}
}

impl<C: CurveAffine> From<C> for KzgSuccinctVerifyingKey<C> {
impl<C> From<C> for KzgSuccinctVerifyingKey<C> {
fn from(g: C) -> KzgSuccinctVerifyingKey<C> {
KzgSuccinctVerifyingKey::new(g)
}
Expand Down
34 changes: 7 additions & 27 deletions snark-verifier/src/pcs/kzg/decider.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use halo2_curves::CurveAffine;

use crate::{pcs::kzg::KzgSuccinctVerifyingKey, util::arithmetic::MultiMillerLoop};
use std::marker::PhantomData;

/// KZG deciding key.
#[derive(Debug, Clone, Copy)]
pub struct KzgDecidingKey<M: MultiMillerLoop>
where
M::G1Affine: CurveAffine,
{
pub struct KzgDecidingKey<M: MultiMillerLoop> {
/// KZG succinct verifying key.
pub svk: KzgSuccinctVerifyingKey<M::G1Affine>,
/// Generator on G2.
Expand All @@ -18,11 +13,7 @@ where
_marker: PhantomData<M>,
}

impl<C: CurveAffine, M: MultiMillerLoop<G1Affine = C>> KzgDecidingKey<M>
where
M::G1Affine: CurveAffine,
M::G2Affine: CurveAffine,
{
impl<M: MultiMillerLoop> KzgDecidingKey<M> {
/// Initialize a [`KzgDecidingKey`]
pub fn new(
svk: impl Into<KzgSuccinctVerifyingKey<M::G1Affine>>,
Expand All @@ -38,20 +29,13 @@ where
}
}

impl<C: CurveAffine, M: MultiMillerLoop<G1Affine = C>> From<(M::G1Affine, M::G2Affine, M::G2Affine)>
for KzgDecidingKey<M>
where
M::G1Affine: CurveAffine,
M::G2Affine: CurveAffine,
{
impl<M: MultiMillerLoop> From<(M::G1Affine, M::G2Affine, M::G2Affine)> for KzgDecidingKey<M> {
fn from((g1, g2, s_g2): (M::G1Affine, M::G2Affine, M::G2Affine)) -> KzgDecidingKey<M> {
KzgDecidingKey::new(g1, g2, s_g2)
}
}

impl<C: CurveAffine, M: MultiMillerLoop<G1Affine = C>> AsRef<KzgSuccinctVerifyingKey<M::G1Affine>>
for KzgDecidingKey<M>
{
impl<M: MultiMillerLoop> AsRef<KzgSuccinctVerifyingKey<M::G1Affine>> for KzgDecidingKey<M> {
fn as_ref(&self) -> &KzgSuccinctVerifyingKey<M::G1Affine> {
&self.svk
}
Expand Down Expand Up @@ -132,9 +116,9 @@ mod evm {
impl<M, MOS> AccumulationDecider<M::G1Affine, Rc<EvmLoader>> for KzgAs<M, MOS>
where
M: MultiMillerLoop,
M::G1Affine: CurveAffine,
M::Fr: PrimeField<Repr = [u8; 0x20]>,
M::G1Affine: CurveAffine<ScalarExt = M::Fr>,
M::G2Affine: CurveAffine,
<M::G1Affine as CurveAffine>::ScalarExt: PrimeField<Repr = [u8; 0x20]>,
MOS: Clone + Debug,
{
type DecidingKey = KzgDecidingKey<M>;
Expand Down Expand Up @@ -183,11 +167,7 @@ mod evm {
loader.code_mut().runtime_append(code);
let challenge = loader.scalar(Value::Memory(challenge_ptr));

let powers_of_challenge =
LoadedScalar::<<M::G1Affine as CurveAffine>::ScalarExt>::powers(
&challenge,
lhs.len(),
);
let powers_of_challenge = LoadedScalar::<M::Fr>::powers(&challenge, lhs.len());
let [lhs, rhs] = [lhs, rhs].map(|msms| {
msms.iter()
.zip(powers_of_challenge.iter())
Expand Down
9 changes: 5 additions & 4 deletions snark-verifier/src/pcs/kzg/multiopen/bdfg21.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use halo2_curves::{ff::PrimeField, group::prime::PrimeCurveAffine};
use halo2_curves::ff::PrimeField;

use crate::{
cost::{Cost, CostEstimation},
Expand Down Expand Up @@ -29,7 +29,8 @@ pub struct Bdfg21;
impl<M, L> PolynomialCommitmentScheme<M::G1Affine, L> for KzgAs<M, Bdfg21>
where
M: MultiMillerLoop,
M::G1Affine: CurveAffine,
M::Fr: Ord,
M::G1Affine: CurveAffine<ScalarExt = M::Fr>,
L: Loader<M::G1Affine>,
{
type VerifyingKey = KzgSuccinctVerifyingKey<M::G1Affine>;
Expand All @@ -38,7 +39,7 @@ where

fn read_proof<T>(
_: &KzgSuccinctVerifyingKey<M::G1Affine>,
_: &[Query<<M::G1Affine as PrimeCurveAffine>::Scalar>],
_: &[Query<M::Fr>],
transcript: &mut T,
) -> Result<Bdfg21Proof<M::G1Affine, L>, Error>
where
Expand All @@ -51,7 +52,7 @@ where
svk: &KzgSuccinctVerifyingKey<M::G1Affine>,
commitments: &[Msm<M::G1Affine, L>],
z: &L::LoadedScalar,
queries: &[Query<<M::G1Affine as PrimeCurveAffine>::Scalar, L::LoadedScalar>],
queries: &[Query<M::Fr, L::LoadedScalar>],
proof: &Bdfg21Proof<M::G1Affine, L>,
) -> Result<Self::Output, Error> {
let sets = query_sets(queries);
Expand Down
9 changes: 4 additions & 5 deletions snark-verifier/src/pcs/kzg/multiopen/gwc19.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use halo2_curves::group::prime::PrimeCurveAffine;

use crate::{
cost::{Cost, CostEstimation},
loader::{LoadedScalar, Loader},
Expand All @@ -25,7 +23,8 @@ pub struct Gwc19;
impl<M, L> PolynomialCommitmentScheme<M::G1Affine, L> for KzgAs<M, Gwc19>
where
M: MultiMillerLoop,
M::G1Affine: CurveAffine,
M::Fr: Ord,
M::G1Affine: CurveAffine<ScalarExt = M::Fr>,
L: Loader<M::G1Affine>,
{
type VerifyingKey = KzgSuccinctVerifyingKey<M::G1Affine>;
Expand All @@ -34,7 +33,7 @@ where

fn read_proof<T>(
_: &Self::VerifyingKey,
queries: &[Query<<M::G1Affine as PrimeCurveAffine>::Scalar>],
queries: &[Query<M::Fr>],
transcript: &mut T,
) -> Result<Self::Proof, Error>
where
Expand All @@ -47,7 +46,7 @@ where
svk: &Self::VerifyingKey,
commitments: &[Msm<M::G1Affine, L>],
z: &L::LoadedScalar,
queries: &[Query<<M::G1Affine as PrimeCurveAffine>::Scalar, L::LoadedScalar>],
queries: &[Query<M::Fr, L::LoadedScalar>],
proof: &Self::Proof,
) -> Result<Self::Output, Error> {
let sets = query_sets(queries);
Expand Down
9 changes: 4 additions & 5 deletions snark-verifier/src/system/halo2/test/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ where
ParamsKZG::<M>::setup(k, ChaCha20Rng::from_seed(Default::default()))
}

pub fn main_gate_with_range_with_mock_kzg_accumulator<
C: CurveAffine,
M: MultiMillerLoop<G1Affine = C, Fr = C::ScalarExt, G1 = C::CurveExt>,
>() -> MainGateWithRange<<M::G1Affine as CurveAffine>::ScalarExt>
pub fn main_gate_with_range_with_mock_kzg_accumulator<M: MultiMillerLoop>(
) -> MainGateWithRange<M::Fr>
where
M::G2Affine: CurveAffine + SerdeObject,
M::G1Affine: CurveAffine + SerdeObject,
M::G1Affine: CurveAffine<CurveExt = M::G1, ScalarExt = M::Fr> + SerdeObject,
M::G1: CurveExt<AffineExt = M::G1Affine>,
{
let srs = read_or_create_srs(TESTDATA_DIR, 1, setup::<M>);
let [g1, s_g1] = [srs.get_g()[0], srs.get_g()[1]].map(|point| point.coordinates().unwrap());
Expand Down
7 changes: 2 additions & 5 deletions snark-verifier/src/system/halo2/test/kzg/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use crate::{
},
verifier::plonk::PlonkVerifier,
};
use halo2_curves::{
bn256::{Bn256, G1Affine},
pairing::Engine,
};
use halo2_curves::bn256::{Bn256, G1Affine};
use halo2_proofs::poly::kzg::multiopen::{ProverGWC, ProverSHPLONK, VerifierGWC, VerifierSHPLONK};
use paste::paste;
use rand_chacha::{rand_core::SeedableRng, ChaCha20Rng};
Expand Down Expand Up @@ -123,7 +120,7 @@ test!(
zk_main_gate_with_range_with_mock_kzg_accumulator,
9,
halo2_kzg_config!(true, 1, (0..4 * LIMBS).map(|idx| (0, idx)).collect()),
main_gate_with_range_with_mock_kzg_accumulator::<<Bn256 as Engine>::G1Affine, Bn256>()
main_gate_with_range_with_mock_kzg_accumulator::<Bn256>()
);
test!(
#[cfg(feature = "loader_halo2")],
Expand Down
7 changes: 2 additions & 5 deletions snark-verifier/src/system/halo2/test/kzg/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use crate::{
},
verifier::plonk::PlonkVerifier,
};
use halo2_curves::{
bn256::{Bn256, G1Affine},
pairing::Engine,
};
use halo2_curves::bn256::{Bn256, G1Affine};
use halo2_proofs::{
poly::kzg::multiopen::{ProverGWC, ProverSHPLONK, VerifierGWC, VerifierSHPLONK},
transcript::{Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer},
Expand Down Expand Up @@ -67,5 +64,5 @@ test!(
zk_main_gate_with_range_with_mock_kzg_accumulator,
9,
halo2_kzg_config!(true, 2, (0..4 * LIMBS).map(|idx| (0, idx)).collect()),
main_gate_with_range_with_mock_kzg_accumulator::<<Bn256 as Engine>::G1Affine, Bn256>()
main_gate_with_range_with_mock_kzg_accumulator::<Bn256>()
);

0 comments on commit 2748862

Please sign in to comment.