Skip to content

Commit

Permalink
give AggregationCircuit a simple generic trait type (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
smtmfft authored Sep 21, 2023
1 parent a440ff9 commit 0e7ff92
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 67 deletions.
100 changes: 33 additions & 67 deletions snark-verifier-sdk/src/halo2/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,41 @@ where
(previous_instances, accumulator)
}

/// SDK trait which makes it possible for other project directly use the AggregationCircuit with any `AccumulationScheme' like GWC & SHPLONK
pub trait AccumulationSchemeSDK:
for<'a> PolynomialCommitmentScheme<
G1Affine,
Rc<Halo2Loader<'a>>,
VerifyingKey = Svk,
Output = KzgAccumulator<G1Affine, Rc<Halo2Loader<'a>>>,
> + for<'a> AccumulationScheme<
G1Affine,
Rc<Halo2Loader<'a>>,
Accumulator = KzgAccumulator<G1Affine, Rc<Halo2Loader<'a>>>,
VerifyingKey = KzgAsVerifyingKey,
> + PolynomialCommitmentScheme<
G1Affine,
NativeLoader,
VerifyingKey = Svk,
Output = KzgAccumulator<G1Affine, NativeLoader>,
> + AccumulationScheme<
G1Affine,
NativeLoader,
Accumulator = KzgAccumulator<G1Affine, NativeLoader>,
VerifyingKey = KzgAsVerifyingKey,
> + AccumulationSchemeProver<G1Affine, ProvingKey = KzgAsProvingKey<G1Affine>>
{
}

/// `AS` should be the [`AccumulationScheme`] and [`PolynomialCommitmentScheme`] used to create `snarks`.
/// Many things will fail if `AS` does not match how `snarks` were actually created.
///
/// In practice, `AS` is either `SHPLONK` or `GWC`.
#[derive(Clone)]
pub struct AggregationCircuit<AS> {
pub struct AggregationCircuit<AS>
where
AS: AccumulationSchemeSDK,
{
svk: Svk,
pub snarks: Vec<SnarkWitness>,
instances: Vec<Fr>,
Expand All @@ -153,29 +182,8 @@ pub struct AggregationCircuit<AS> {
}

impl<AS> AggregationCircuit<AS>
// without unstable rust, I don't know how to make this where clause go away...
where
for<'a> AS: PolynomialCommitmentScheme<
G1Affine,
Rc<Halo2Loader<'a>>,
VerifyingKey = Svk,
Output = KzgAccumulator<G1Affine, Rc<Halo2Loader<'a>>>,
> + AccumulationScheme<
G1Affine,
Rc<Halo2Loader<'a>>,
Accumulator = KzgAccumulator<G1Affine, Rc<Halo2Loader<'a>>>,
VerifyingKey = KzgAsVerifyingKey,
> + PolynomialCommitmentScheme<
G1Affine,
NativeLoader,
VerifyingKey = Svk,
Output = KzgAccumulator<G1Affine, NativeLoader>,
> + AccumulationScheme<
G1Affine,
NativeLoader,
Accumulator = KzgAccumulator<G1Affine, NativeLoader>,
VerifyingKey = KzgAsVerifyingKey,
> + AccumulationSchemeProver<G1Affine, ProvingKey = KzgAsProvingKey<G1Affine>>,
AS: AccumulationSchemeSDK,
{
/// Given snarks, this creates a circuit and runs the `GateThreadBuilder` to verify all the snarks.
/// By default, the returned circuit has public instances equal to the limbs of the pair of elliptic curve points, referred to as the `accumulator`, that need to be verified in a final pairing check.
Expand Down Expand Up @@ -327,29 +335,8 @@ impl AggregationConfig {
}

impl<AS> Circuit<Fr> for AggregationCircuit<AS>
// without unstable rust, I don't know how to make this where clause go away...
where
for<'a> AS: PolynomialCommitmentScheme<
G1Affine,
Rc<Halo2Loader<'a>>,
VerifyingKey = Svk,
Output = KzgAccumulator<G1Affine, Rc<Halo2Loader<'a>>>,
> + AccumulationScheme<
G1Affine,
Rc<Halo2Loader<'a>>,
Accumulator = KzgAccumulator<G1Affine, Rc<Halo2Loader<'a>>>,
VerifyingKey = KzgAsVerifyingKey,
> + PolynomialCommitmentScheme<
G1Affine,
NativeLoader,
VerifyingKey = Svk,
Output = KzgAccumulator<G1Affine, NativeLoader>,
> + AccumulationScheme<
G1Affine,
NativeLoader,
Accumulator = KzgAccumulator<G1Affine, NativeLoader>,
VerifyingKey = KzgAsVerifyingKey,
> + AccumulationSchemeProver<G1Affine, ProvingKey = KzgAsProvingKey<G1Affine>>,
AS: AccumulationSchemeSDK,
{
type Config = AggregationConfig;
type FloorPlanner = SimpleFloorPlanner;
Expand Down Expand Up @@ -398,29 +385,8 @@ where
}

impl<AS> CircuitExt<Fr> for AggregationCircuit<AS>
// without unstable rust, I don't know how to make this where clause go away...
where
for<'a> AS: PolynomialCommitmentScheme<
G1Affine,
Rc<Halo2Loader<'a>>,
VerifyingKey = Svk,
Output = KzgAccumulator<G1Affine, Rc<Halo2Loader<'a>>>,
> + AccumulationScheme<
G1Affine,
Rc<Halo2Loader<'a>>,
Accumulator = KzgAccumulator<G1Affine, Rc<Halo2Loader<'a>>>,
VerifyingKey = KzgAsVerifyingKey,
> + PolynomialCommitmentScheme<
G1Affine,
NativeLoader,
VerifyingKey = Svk,
Output = KzgAccumulator<G1Affine, NativeLoader>,
> + AccumulationScheme<
G1Affine,
NativeLoader,
Accumulator = KzgAccumulator<G1Affine, NativeLoader>,
VerifyingKey = KzgAsVerifyingKey,
> + AccumulationSchemeProver<G1Affine, ProvingKey = KzgAsProvingKey<G1Affine>>,
AS: AccumulationSchemeSDK,
{
fn num_instance(&self) -> Vec<usize> {
vec![self.instances.len()]
Expand Down
4 changes: 4 additions & 0 deletions snark-verifier-sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "display")]
use ark_std::{end_timer, start_timer};
use halo2::aggregation::AccumulationSchemeSDK;
use halo2_proofs::{
circuit::Value,
halo2curves::{
Expand Down Expand Up @@ -40,6 +41,9 @@ pub type PlonkSuccinctVerifier<AS> =
pub type SHPLONK = KzgAs<Bn256, Bdfg21>;
pub type GWC = KzgAs<Bn256, Gwc19>;

impl AccumulationSchemeSDK for GWC {}
impl AccumulationSchemeSDK for SHPLONK {}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "derive_serde", derive(Serialize, Deserialize))]
pub struct Snark {
Expand Down

0 comments on commit 0e7ff92

Please sign in to comment.