Skip to content

Commit

Permalink
Commitment scheme abstraction
Browse files Browse the repository at this point in the history
Add SHPLONK and GWC varians of KZG
  • Loading branch information
kilic authored and han0110 committed Aug 19, 2022
1 parent 2a19e44 commit 51d523d
Show file tree
Hide file tree
Showing 121 changed files with 4,413 additions and 2,066 deletions.
38 changes: 6 additions & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.56.1
override: true
override: false
- name: Run tests
uses: actions-rs/cargo@v1
with:
Expand All @@ -35,8 +34,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.56.1
override: true
override: false
- name: Add target
run: rustup target add ${{ matrix.target }}
- name: cargo build
Expand All @@ -53,35 +51,14 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.56.1
override: true
override: false
# Build benchmarks to prevent bitrot
- name: Build benchmarks
uses: actions-rs/cargo@v1
with:
command: build
args: --benches --examples --all-features

book:
name: Book tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.56.1
override: true
- name: cargo build
uses: actions-rs/cargo@v1
with:
command: build
- name: Setup mdBook
uses: peaceiris/actions-mdbook@v1
with:
mdbook-version: '0.4.5'
- name: Test halo2 book
run: mdbook test -L target/debug/deps book/

codecov:
name: Code coverage
runs-on: ubuntu-latest
Expand All @@ -91,8 +68,7 @@ jobs:
# Use stable for this to ensure that cargo-tarpaulin can be built.
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
override: false
- name: Install cargo-tarpaulin
uses: actions-rs/cargo@v1
with:
Expand All @@ -114,8 +90,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.56.1
override: true
override: false
- name: cargo fetch
uses: actions-rs/cargo@v1
with:
Expand All @@ -137,8 +112,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.56.1
override: true
override: false
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/lints-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: beta
components: clippy
override: true
override: false
- name: Run Clippy (beta)
uses: actions-rs/clippy-check@v1
continue-on-error: true
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/lints-stable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.56.1
components: clippy
override: true
override: false
- name: Run clippy
uses: actions-rs/clippy-check@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ff = "0.12"
group = "0.12"
halo2_proofs = { version = "0.2", path = "../halo2_proofs" }
lazy_static = "1"
pasta_curves = "0.4"
halo2curves = { git = 'https://github.com/privacy-scaling-explorations/halo2curves', tag = 'v0.2.0' }
proptest = { version = "1.0.0", optional = true }
rand = "0.8"
subtle = "2.3"
Expand Down
49 changes: 37 additions & 12 deletions halo2_gadgets/benches/poseidon.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use ff::Field;
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
pasta::Fp,
plonk::{
create_proof, keygen_pk, keygen_vk, verify_proof, Advice, Circuit, Column,
ConstraintSystem, Error, SingleVerifier,
ConstraintSystem, Error,
},
poly::commitment::Params,
transcript::{Blake2bRead, Blake2bWrite, Challenge255},
};
use pasta_curves::{pallas, vesta};
use halo2curves::pasta::{pallas, vesta, EqAffine, Fp};

use halo2_gadgets::poseidon::{
primitives::{self as poseidon, ConstantLength, Spec},
Expand All @@ -21,6 +19,19 @@ use std::marker::PhantomData;
use criterion::{criterion_group, criterion_main, Criterion};
use rand::rngs::OsRng;

use halo2_proofs::{
poly::{
commitment::ParamsProver,
ipa::{
commitment::{IPACommitmentScheme, ParamsIPA},
multiopen::ProverIPA,
strategy::SingleStrategy,
},
VerificationStrategy,
},
transcript::{TranscriptReadBuffer, TranscriptWriterBuffer},
};

#[derive(Clone, Copy)]
struct HashCircuit<S, const WIDTH: usize, const RATE: usize, const L: usize>
where
Expand Down Expand Up @@ -184,7 +195,7 @@ fn bench_poseidon<S, const WIDTH: usize, const RATE: usize, const L: usize>(
S: Spec<Fp, WIDTH, RATE> + Copy + Clone,
{
// Initialize the polynomial commitment parameters
let params: Params<vesta::Affine> = Params::new(K);
let params: ParamsIPA<vesta::Affine> = ParamsIPA::new(K);

let empty_circuit = HashCircuit::<S, WIDTH, RATE, L> {
message: Value::unknown(),
Expand Down Expand Up @@ -216,21 +227,35 @@ fn bench_poseidon<S, const WIDTH: usize, const RATE: usize, const L: usize>(
c.bench_function(&prover_name, |b| {
b.iter(|| {
// Create a proof
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
create_proof(&params, &pk, &[circuit], &[&[]], &mut rng, &mut transcript)
.expect("proof generation should not fail")
let mut transcript = Blake2bWrite::<_, EqAffine, Challenge255<_>>::init(vec![]);
create_proof::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _>(
&params,
&pk,
&[circuit],
&[&[]],
&mut rng,
&mut transcript,
)
.expect("proof generation should not fail")
})
});

// Create a proof
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
create_proof(&params, &pk, &[circuit], &[&[]], &mut rng, &mut transcript)
.expect("proof generation should not fail");
let mut transcript = Blake2bWrite::<_, EqAffine, Challenge255<_>>::init(vec![]);
create_proof::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _>(
&params,
&pk,
&[circuit],
&[&[]],
&mut rng,
&mut transcript,
)
.expect("proof generation should not fail");
let proof = transcript.finalize();

c.bench_function(&verifier_name, |b| {
b.iter(|| {
let strategy = SingleVerifier::new(&params);
let strategy = SingleStrategy::new(&params);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof[..]);
assert!(verify_proof(&params, pk.get_vk(), strategy, &[&[]], &mut transcript).is_ok());
});
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/benches/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use halo2_gadgets::{
sinsemilla::primitives as sinsemilla,
};

use pasta_curves::pallas;
use halo2curves::pasta::pallas;
#[cfg(unix)]
use pprof::criterion::{Output, PProfProfiler};
use rand::{rngs::OsRng, Rng};
Expand Down
49 changes: 37 additions & 12 deletions halo2_gadgets/benches/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
pasta::{pallas, EqAffine},
plonk::{
create_proof, keygen_pk, keygen_vk, verify_proof, Circuit, ConstraintSystem, Error,
SingleVerifier,
},
plonk::{create_proof, keygen_pk, keygen_vk, verify_proof, Circuit, ConstraintSystem, Error},
poly::commitment::Params,
transcript::{Blake2bRead, Blake2bWrite, Challenge255},
};
use halo2curves::pasta::{pallas, EqAffine};
use rand::rngs::OsRng;

use std::{
Expand All @@ -20,6 +17,18 @@ use criterion::{criterion_group, criterion_main, Criterion};

use halo2_gadgets::sha256::{BlockWord, Sha256, Table16Chip, Table16Config, BLOCK_SIZE};

use halo2_proofs::{
poly::{
commitment::ParamsProver,
ipa::{
commitment::{IPACommitmentScheme, ParamsIPA},
multiopen::{ProverIPA, VerifierIPA},
strategy::AccumulatorStrategy,
},
},
transcript::{TranscriptReadBuffer, TranscriptWriterBuffer},
};

#[allow(dead_code)]
fn bench(name: &str, k: u32, c: &mut Criterion) {
#[derive(Default)]
Expand Down Expand Up @@ -80,7 +89,7 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
// Initialize the polynomial commitment parameters
let params_path = Path::new("./benches/sha256_assets/sha256_params");
if File::open(&params_path).is_err() {
let params: Params<EqAffine> = Params::new(k);
let params: ParamsIPA<EqAffine> = ParamsIPA::new(k);
let mut buf = Vec::new();

params.write(&mut buf).expect("Failed to write params");
Expand All @@ -91,8 +100,8 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
}

let params_fs = File::open(&params_path).expect("couldn't load sha256_params");
let params: Params<EqAffine> =
Params::read::<_>(&mut BufReader::new(params_fs)).expect("Failed to read params");
let params: ParamsIPA<EqAffine> =
ParamsIPA::read::<_>(&mut BufReader::new(params_fs)).expect("Failed to read params");

let empty_circuit: MyCircuit = MyCircuit {};

Expand All @@ -119,8 +128,15 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
let proof_path = Path::new("./benches/sha256_assets/sha256_proof");
if File::open(&proof_path).is_err() {
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
create_proof(&params, &pk, &[circuit], &[], OsRng, &mut transcript)
.expect("proof generation should not fail");
create_proof::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _>(
&params,
&pk,
&[circuit],
&[],
OsRng,
&mut transcript,
)
.expect("proof generation should not fail");
let proof: Vec<u8> = transcript.finalize();
let mut file = File::create(&proof_path).expect("Failed to create sha256_proof");
file.write_all(&proof[..]).expect("Failed to write proof");
Expand All @@ -134,9 +150,18 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {

c.bench_function(&verifier_name, |b| {
b.iter(|| {
let strategy = SingleVerifier::new(&params);
use halo2_proofs::poly::VerificationStrategy;
let strategy = AccumulatorStrategy::new(&params);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof[..]);
assert!(verify_proof(&params, pk.get_vk(), strategy, &[], &mut transcript).is_ok());
let strategy = verify_proof::<IPACommitmentScheme<_>, VerifierIPA<_>, _, _, _>(
&params,
pk.get_vk(),
strategy,
&[],
&mut transcript,
)
.unwrap();
assert!(strategy.finalize());
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ pub(crate) mod tests {
dev::MockProver,
plonk::{Circuit, ConstraintSystem, Error},
};
use halo2curves::pasta::pallas;
use lazy_static::lazy_static;
use pasta_curves::pallas;

use super::{
chip::{
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/ecc/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use halo2_proofs::{
circuit::{AssignedCell, Chip, Layouter, Value},
plonk::{Advice, Assigned, Column, ConstraintSystem, Error, Fixed},
};
use pasta_curves::{arithmetic::CurveAffine, pallas};
use halo2curves::{pasta::pallas, CurveAffine};

use std::convert::TryInto;

Expand Down
4 changes: 2 additions & 2 deletions halo2_gadgets/src/ecc/chip/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use halo2_proofs::{
plonk::{Advice, Assigned, Column, ConstraintSystem, Constraints, Error, Expression, Selector},
poly::Rotation,
};
use pasta_curves::{arithmetic::FieldExt, pallas};
use halo2curves::{pasta::pallas, FieldExt};
use std::collections::HashSet;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -346,7 +346,7 @@ pub mod tests {
circuit::{Layouter, Value},
plonk::Error,
};
use pasta_curves::{arithmetic::CurveExt, pallas};
use halo2curves::{pasta::pallas, CurveExt};

use crate::ecc::{chip::EccPoint, EccInstructions, NonIdentityPoint};

Expand Down
4 changes: 2 additions & 2 deletions halo2_gadgets/src/ecc/chip/add_incomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use halo2_proofs::{
plonk::{Advice, Column, ConstraintSystem, Constraints, Error, Selector},
poly::Rotation,
};
use pasta_curves::pallas;
use halo2curves::pasta::pallas;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Config {
Expand Down Expand Up @@ -152,7 +152,7 @@ pub mod tests {
circuit::{Layouter, Value},
plonk::Error,
};
use pasta_curves::pallas;
use halo2curves::pasta::pallas;

use crate::ecc::{EccInstructions, NonIdentityPoint};

Expand Down
10 changes: 2 additions & 8 deletions halo2_gadgets/src/ecc/chip/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use group::{
Curve,
};
use halo2_proofs::arithmetic::lagrange_interpolate;
use pasta_curves::{
arithmetic::{CurveAffine, FieldExt},
pallas,
};
use halo2curves::{pasta::pallas, CurveAffine, FieldExt};

/// Window size for fixed-base scalar multiplication
pub const FIXED_BASE_WINDOW_SIZE: usize = 3;
Expand Down Expand Up @@ -233,10 +230,7 @@ pub fn test_lagrange_coeffs<C: CurveAffine>(base: C, num_windows: usize) {
#[cfg(test)]
mod tests {
use group::{ff::Field, Curve, Group};
use pasta_curves::{
arithmetic::{CurveAffine, FieldExt},
pallas,
};
use halo2curves::{pasta::pallas, CurveAffine, FieldExt};
use proptest::prelude::*;

use super::{compute_window_table, find_zs_and_us, test_lagrange_coeffs, H, NUM_WINDOWS};
Expand Down
Loading

0 comments on commit 51d523d

Please sign in to comment.