Skip to content

Commit

Permalink
feat: remove lifetime param from CommitmentScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
han0110 authored and kilic committed Jul 4, 2022
1 parent d2d35fc commit 43f1211
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 67 deletions.
8 changes: 4 additions & 4 deletions halo2_proofs/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ impl<F: Field> Assignment<F> for Assembly<F> {
}

/// Generate a `VerifyingKey` from an instance of `Circuit`.
pub fn keygen_vk<'params, Scheme: CommitmentScheme<'params>, ConcreteCircuit>(
params: &'params Scheme::ParamsProver,
pub fn keygen_vk<Scheme: CommitmentScheme, ConcreteCircuit>(
params: &Scheme::ParamsProver,
circuit: &ConcreteCircuit,
) -> Result<VerifyingKey<Scheme::Curve>, Error>
where
Expand Down Expand Up @@ -242,8 +242,8 @@ where
}

/// Generate a `ProvingKey` from a `VerifyingKey` and an instance of `Circuit`.
pub fn keygen_pk<'params, Scheme: CommitmentScheme<'params>, ConcreteCircuit>(
params: &'params Scheme::ParamsProver,
pub fn keygen_pk<Scheme: CommitmentScheme, ConcreteCircuit>(
params: &Scheme::ParamsProver,
vk: VerifyingKey<Scheme::Curve>,
circuit: &ConcreteCircuit,
) -> Result<ProvingKey<Scheme::Curve>, Error>
Expand Down
6 changes: 3 additions & 3 deletions halo2_proofs/src/plonk/lookup/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<F: FieldExt> Argument<F> {
pub(in crate::plonk) fn commit_permuted<
'a,
'params: 'a,
Scheme: CommitmentScheme<'params, Scalar = F>,
Scheme: CommitmentScheme<Scalar = F>,
E: EncodedChallenge<Scheme::Curve>,
Ev: Copy + Send + Sync,
Ec: Copy + Send + Sync,
Expand Down Expand Up @@ -234,7 +234,7 @@ impl<C: CurveAffine, Ev: Copy + Send + Sync> Permuted<C, Ev> {
/// added to the Lookup and finally returned by the method.
pub(in crate::plonk) fn commit_product<
'params,
Scheme: CommitmentScheme<'params, Curve = C, Scalar = C::Scalar>,
Scheme: CommitmentScheme<Curve = C, Scalar = C::Scalar>,
E: EncodedChallenge<Scheme::Curve>,
R: RngCore,
T: TranscriptWrite<Scheme::Curve, E>,
Expand Down Expand Up @@ -574,7 +574,7 @@ type ExpressionPair<F> = (Polynomial<F, LagrangeCoeff>, Polynomial<F, LagrangeCo
/// - the first row in a sequence of like values in A' is the row
/// that has the corresponding value in S'.
/// This method returns (A', S') if no errors are encountered.
fn permute_expression_pair<'params, Scheme: CommitmentScheme<'params>, R: RngCore>(
fn permute_expression_pair<'params, Scheme: CommitmentScheme, R: RngCore>(
pk: &ProvingKey<Scheme::Curve>,
params: &'params Scheme::ParamsProver,
domain: &EvaluationDomain<Scheme::Scalar>,
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/plonk/permutation/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Assembly {
Ok(())
}

pub(crate) fn build_vk<'params, Scheme: CommitmentScheme<'params>>(
pub(crate) fn build_vk<'params, Scheme: CommitmentScheme>(
self,
params: &'params Scheme::ParamsProver,
domain: &EvaluationDomain<Scheme::Scalar>,
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Assembly {
VerifyingKey { commitments }
}

pub(crate) fn build_pk<'params, Scheme: CommitmentScheme<'params>>(
pub(crate) fn build_pk<'params, Scheme: CommitmentScheme>(
self,
params: &'params Scheme::ParamsProver,
domain: &EvaluationDomain<Scheme::Scalar>,
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/permutation/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub(crate) struct Evaluated<C: CurveAffine> {
impl Argument {
pub(in crate::plonk) fn commit<
'params,
Scheme: CommitmentScheme<'params>,
Scheme: CommitmentScheme,
E: EncodedChallenge<Scheme::Curve>,
Ev: Copy + Send + Sync,
R: RngCore,
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use group::prime::PrimeCurveAffine;
/// are zero-padded internally.
pub fn create_proof<
'params,
Scheme: CommitmentScheme<'params>,
Scheme: CommitmentScheme,
Prover: _Prover<'params, Scheme>,
E: EncodedChallenge<Scheme::Curve>,
R: RngCore,
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/plonk/vanishing/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(in crate::plonk) struct Evaluated<C: CurveAffine> {
impl<C: CurveAffine> Argument<C> {
pub(in crate::plonk) fn commit<
'params,
Scheme: CommitmentScheme<'params, Curve = C, Scalar = C::Scalar>,
Scheme: CommitmentScheme<Curve = C, Scalar = C::Scalar>,
E: EncodedChallenge<Scheme::Curve>,
R: RngCore,
T: TranscriptWrite<Scheme::Curve, E>,
Expand Down Expand Up @@ -69,7 +69,7 @@ impl<C: CurveAffine> Argument<C> {
impl<C: CurveAffine> Committed<C> {
pub(in crate::plonk) fn construct<
'params,
Scheme: CommitmentScheme<'params, Curve = C, Scalar = C::Scalar>,
Scheme: CommitmentScheme<Curve = C, Scalar = C::Scalar>,
E: EncodedChallenge<Scheme::Curve>,
Ev: Copy + Send + Sync,
R: RngCore,
Expand Down
10 changes: 3 additions & 7 deletions halo2_proofs/src/plonk/vanishing/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,13 @@ impl<C: CurveAffine> Constructed<C> {
}

impl<C: CurveAffine> PartiallyEvaluated<C> {
pub(in crate::plonk) fn verify<
'params,
Scheme: CommitmentScheme<'params, Curve = C, Scalar = C::ScalarExt>,
I,
>(
pub(in crate::plonk) fn verify<Scheme: CommitmentScheme<Curve = C, Scalar = C::ScalarExt>, I>(
self,
params: &'params Scheme::ParamsVerifier,
params: &Scheme::ParamsVerifier,
expressions: I,
y: ChallengeY<Scheme::Curve>,
xn: Scheme::Scalar,
) -> Evaluated<Scheme::Curve, Scheme::MSM>
) -> Evaluated<Scheme::Curve, <Scheme::ParamsVerifier as Params<Scheme::Curve>>::MSM>
where
I: Iterator<Item = Scheme::Scalar>,
{
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use crate::poly::commitment::ParamsVerifier;
/// Returns a boolean indicating whether or not the proof is valid
pub fn verify_proof<
'params,
Scheme: CommitmentScheme<'params>,
Scheme: CommitmentScheme,
E: EncodedChallenge<Scheme::Curve>,
T: TranscriptRead<Scheme::Curve, E>,
V: Verifier<'params, Scheme>,
Expand Down
42 changes: 21 additions & 21 deletions halo2_proofs/src/poly/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,22 @@ use std::{
};

/// Defines components of a commitment scheme.
pub trait CommitmentScheme<'params> {
pub trait CommitmentScheme {
/// Application field of this commitment scheme
type Scalar: FieldExt + halo2curves::Group;

/// Elliptic curve used to commit the application and witnesses
type Curve: CurveAffine<ScalarExt = Self::Scalar>;

/// Multi scalar multiplication engine
type MSM: MSM<Self::Curve> + 'params;

/// Constant prover parameters
type ParamsProver: ParamsProver<
type ParamsProver: for<'params> ParamsProver<
'params,
Self::Curve,
Self::MSM,
ParamsVerifier = Self::ParamsVerifier,
>;

/// Constant verifier parameters
type ParamsVerifier: ParamsVerifier<'params, Self::Curve, Self::MSM>;
type ParamsVerifier: for<'params> ParamsVerifier<'params, Self::Curve>;

/// Wrapper for parameter generator
fn new_params(k: u32) -> Self::ParamsProver;
Expand All @@ -45,7 +41,10 @@ pub trait CommitmentScheme<'params> {
}

/// Parameters for circuit sysnthesis and prover parameters.
pub trait Params<'params, C: CurveAffine, M: MSM<C> + 'params>: Sized + Clone {
pub trait Params<'params, C: CurveAffine>: Sized + Clone {
/// Multi scalar multiplication engine
type MSM: MSM<C> + 'params;

/// Logaritmic size of the circuit
fn k(&self) -> u32;

Expand All @@ -54,7 +53,7 @@ pub trait Params<'params, C: CurveAffine, M: MSM<C> + 'params>: Sized + Clone {

/// Generates an empty multiscalar multiplication struct using the
/// appropriate params.
fn empty_msm(&'params self) -> M;
fn empty_msm(&'params self) -> Self::MSM;

/// This commits to a polynomial using its evaluations over the $2^k$ size
/// evaluation domain. The commitment will be blinded by the blinding factor
Expand All @@ -73,11 +72,9 @@ pub trait Params<'params, C: CurveAffine, M: MSM<C> + 'params>: Sized + Clone {
}

/// Parameters for circuit sysnthesis and prover parameters.
pub trait ParamsProver<'params, C: CurveAffine, M: MSM<C> + 'params>:
Params<'params, C, M>
{
pub trait ParamsProver<'params, C: CurveAffine>: Params<'params, C> {
/// Constant verifier parameters.
type ParamsVerifier: ParamsVerifier<'params, C, M>;
type ParamsVerifier: ParamsVerifier<'params, C>;

/// Returns new instance of parameters
fn new(k: u32) -> Self;
Expand All @@ -96,10 +93,7 @@ pub trait ParamsProver<'params, C: CurveAffine, M: MSM<C> + 'params>:
}

/// Verifier specific functionality with circuit constaints
pub trait ParamsVerifier<'params, C: CurveAffine, M: MSM<C> + 'params>:
Params<'params, C, M>
{
}
pub trait ParamsVerifier<'params, C: CurveAffine>: Params<'params, C> {}

/// Multi scalar multiplication engine
pub trait MSM<C: CurveAffine>: Clone + Debug {
Expand Down Expand Up @@ -128,7 +122,7 @@ pub trait MSM<C: CurveAffine>: Clone + Debug {
}

/// Common multi-open prover interface for various commitment schemes
pub trait Prover<'params, Scheme: CommitmentScheme<'params>> {
pub trait Prover<'params, Scheme: CommitmentScheme> {
/// Creates new prover instance
fn new(params: &'params Scheme::ParamsProver) -> Self;

Expand All @@ -151,10 +145,10 @@ pub trait Prover<'params, Scheme: CommitmentScheme<'params>> {
}

/// Common multi-open verifier interface for various commitment schemes
pub trait Verifier<'params, Scheme: CommitmentScheme<'params>> {
pub trait Verifier<'params, Scheme: CommitmentScheme> {
/// Unfinalized verification result. This is returned in verification
/// to allow developer to compress or combined verification results
type Guard: Guard<'params, Scheme, MSMAccumulator = Self::MSMAccumulator>;
type Guard: Guard<Scheme, MSMAccumulator = Self::MSMAccumulator>;

/// Accumulator fot comressed verification
type MSMAccumulator;
Expand All @@ -176,7 +170,13 @@ pub trait Verifier<'params, Scheme: CommitmentScheme<'params>> {
) -> Result<Self::Guard, Error>
where
'params: 'com,
I: IntoIterator<Item = VerifierQuery<'com, Scheme::Curve, Scheme::MSM>> + Clone;
I: IntoIterator<
Item = VerifierQuery<
'com,
Scheme::Curve,
<Scheme::ParamsVerifier as Params<'params, Scheme::Curve>>::MSM,
>,
> + Clone;
}

/// Wrapper type around a blinding factor.
Expand Down
13 changes: 7 additions & 6 deletions halo2_proofs/src/poly/ipa/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ pub struct IPACommitmentScheme<C: CurveAffine> {
_marker: PhantomData<C>,
}

impl<'params, C: CurveAffine> CommitmentScheme<'params> for IPACommitmentScheme<C> {
impl<C: CurveAffine> CommitmentScheme for IPACommitmentScheme<C> {
type Scalar = C::ScalarExt;
type Curve = C;
type MSM = MSMIPA<'params, C>;

type ParamsProver = ParamsIPA<C>;
type ParamsVerifier = ParamsVerifierIPA<C>;
Expand All @@ -61,9 +60,11 @@ impl<'params, C: CurveAffine> CommitmentScheme<'params> for IPACommitmentScheme<
/// Verifier parameters
pub type ParamsVerifierIPA<C> = ParamsIPA<C>;

impl<'params, C: CurveAffine> ParamsVerifier<'params, C, MSMIPA<'params, C>> for ParamsIPA<C> {}
impl<'params, C: CurveAffine> ParamsVerifier<'params, C> for ParamsIPA<C> {}

impl<'params, C: CurveAffine> Params<'params, C> for ParamsIPA<C> {
type MSM = MSMIPA<'params, C>;

impl<'params, C: CurveAffine> Params<'params, C, MSMIPA<'params, C>> for ParamsIPA<C> {
fn k(&self) -> u32 {
self.k
}
Expand Down Expand Up @@ -136,7 +137,7 @@ impl<'params, C: CurveAffine> Params<'params, C, MSMIPA<'params, C>> for ParamsI
}
}

impl<'params, C: CurveAffine> ParamsProver<'params, C, MSMIPA<'params, C>> for ParamsIPA<C> {
impl<'params, C: CurveAffine> ParamsProver<'params, C> for ParamsIPA<C> {
type ParamsVerifier = ParamsVerifierIPA<C>;

fn verifier_params(&'params self) -> &'params Self::ParamsVerifier {
Expand Down Expand Up @@ -336,7 +337,7 @@ mod test {

let params = ParamsIPA::<EpAffine>::new(K);
let mut params_buffer = vec![];
<ParamsIPA<_> as Params<_, _>>::write(&params, &mut params_buffer).unwrap();
<ParamsIPA<_> as Params<_>>::write(&params, &mut params_buffer).unwrap();
let params: ParamsIPA<EpAffine> = Params::read::<_>(&mut &params_buffer[..]).unwrap();

let domain = EvaluationDomain::new(1, K);
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/poly/ipa/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct GuardIPA<'params, C: CurveAffine> {
}

/// Define accumulator type as `MSMIPA`
impl<'params, C: CurveAffine> Guard<'params, IPACommitmentScheme<C>> for GuardIPA<'params, C> {
impl<'params, C: CurveAffine> Guard<IPACommitmentScheme<C>> for GuardIPA<'params, C> {
type MSMAccumulator = MSMIPA<'params, C>;
}

Expand Down
25 changes: 19 additions & 6 deletions halo2_proofs/src/poly/kzg/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ pub struct KZGCommitmentScheme<E: Engine> {
_marker: PhantomData<E>,
}

impl<'params, E: Engine + Debug> CommitmentScheme<'params> for KZGCommitmentScheme<E> {
impl<E: Engine + Debug> CommitmentScheme for KZGCommitmentScheme<E> {
type Scalar = E::Scalar;
type Curve = E::G1Affine;
type MSM = MSMKZG<E>;

type ParamsProver = ParamsKZG<E>;
type ParamsVerifier = ParamsVerifierKZG<E>;
Expand All @@ -55,12 +54,26 @@ impl<'params, E: Engine + Debug> CommitmentScheme<'params> for KZGCommitmentSche
}
}

impl<E: Engine + Debug> ParamsKZG<E> {
/// Doc
pub fn g2(&self) -> E::G2Affine {
self.g2
}

/// Doc
pub fn s_g2(&self) -> E::G2Affine {
self.s_g2
}
}

// TODO: see the issue at https://github.com/appliedzkp/halo2/issues/45
// So we probably need much smaller verifier key. However for new bases in g1 should be in verifier keys.
/// KZG multi-open verification parameters
pub type ParamsVerifierKZG<C> = ParamsKZG<C>;

impl<'params, E: Engine + Debug> Params<'params, E::G1Affine, MSMKZG<E>> for ParamsKZG<E> {
impl<'params, E: Engine + Debug> Params<'params, E::G1Affine> for ParamsKZG<E> {
type MSM = MSMKZG<E>;

fn k(&self) -> u32 {
self.k
}
Expand Down Expand Up @@ -158,9 +171,9 @@ impl<'params, E: Engine + Debug> Params<'params, E::G1Affine, MSMKZG<E>> for Par
}
}

impl<'params, E: Engine + Debug> ParamsVerifier<'params, E::G1Affine, MSMKZG<E>> for ParamsKZG<E> {}
impl<'params, E: Engine + Debug> ParamsVerifier<'params, E::G1Affine> for ParamsKZG<E> {}

impl<'params, E: Engine + Debug> ParamsProver<'params, E::G1Affine, MSMKZG<E>> for ParamsKZG<E> {
impl<'params, E: Engine + Debug> ParamsProver<'params, E::G1Affine> for ParamsKZG<E> {
type ParamsVerifier = ParamsVerifierKZG<E>;

fn verifier_params(&'params self) -> &'params Self::ParamsVerifier {
Expand Down Expand Up @@ -313,7 +326,7 @@ mod test {

let params0 = ParamsKZG::<Bn256>::new(K);
let mut data = vec![];
<ParamsKZG<_> as Params<_, _>>::write(&params0, &mut data).unwrap();
<ParamsKZG<_> as Params<_>>::write(&params0, &mut data).unwrap();
let params1: ParamsKZG<Bn256> = Params::read::<_>(&mut &data[..]).unwrap();

assert_eq!(params0.k, params1.k);
Expand Down
4 changes: 1 addition & 3 deletions halo2_proofs/src/poly/kzg/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ pub struct GuardKZG<'params, E: MultiMillerLoop + Debug> {
}

/// Define accumulator type as `DualMSM`
impl<'params, E: MultiMillerLoop + Debug> Guard<'params, KZGCommitmentScheme<E>>
for GuardKZG<'params, E>
{
impl<'params, E: MultiMillerLoop + Debug> Guard<KZGCommitmentScheme<E>> for GuardKZG<'params, E> {
type MSMAccumulator = DualMSM<'params, E>;
}

Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/poly/multiopen_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ mod test {
fn verify<
'a,
'params,
Scheme: CommitmentScheme<'params>,
Scheme: CommitmentScheme,
TranscriptRead: TranscriptReadBuffer<&'a [u8], Scheme::Curve, Ch>,
Verifier: _Verifier<'params, Scheme>,
Strategy: VerificationStrategy<'params, Scheme, Verifier, R, Output = Strategy>,
Expand Down Expand Up @@ -210,7 +210,7 @@ mod test {

fn create_proof<
'params,
Scheme: CommitmentScheme<'params>,
Scheme: CommitmentScheme,
TranscriptWrite: TranscriptWriterBuffer<Vec<u8>, Scheme::Curve, Ch>,
Prover: _Prover<'params, Scheme>,
Ch,
Expand Down
Loading

0 comments on commit 43f1211

Please sign in to comment.