Skip to content

Commit

Permalink
feat: implement optional zk by const generic
Browse files Browse the repository at this point in the history
  • Loading branch information
han0110 committed Jul 18, 2022
1 parent d88de9a commit 57225ee
Show file tree
Hide file tree
Showing 40 changed files with 697 additions and 358 deletions.
19 changes: 14 additions & 5 deletions halo2_gadgets/benches/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use rand::rngs::OsRng;
use halo2_proofs::transcript::TranscriptReadBuffer;
use halo2_proofs::transcript::TranscriptWriterBuffer;

const ZK: bool = true;

#[derive(Clone, Copy)]
struct HashCircuit<S, const WIDTH: usize, const RATE: usize, const L: usize>
where
Expand Down Expand Up @@ -198,9 +200,9 @@ fn bench_poseidon<S, const WIDTH: usize, const RATE: usize, const L: usize>(
};

// Initialize the proving key
let vk = keygen_vk::<IPACommitmentScheme<EqAffine>, _>(&params, &empty_circuit)
let vk = keygen_vk::<IPACommitmentScheme<EqAffine>, _, ZK>(&params, &empty_circuit)
.expect("keygen_vk should not fail");
let pk = keygen_pk::<IPACommitmentScheme<EqAffine>, _>(&params, vk, &empty_circuit)
let pk = keygen_pk::<IPACommitmentScheme<EqAffine>, _, ZK>(&params, vk, &empty_circuit)
.expect("keygen_pk should not fail");

let prover_name = name.to_string() + "-prover";
Expand All @@ -224,7 +226,7 @@ fn bench_poseidon<S, const WIDTH: usize, const RATE: usize, const L: usize>(
b.iter(|| {
// Create a proof
let mut transcript = Blake2bWrite::<_, EqAffine, Challenge255<_>>::init(vec![]);
create_proof::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _>(
create_proof::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _, ZK>(
&params,
&pk,
&[circuit],
Expand All @@ -238,7 +240,7 @@ fn bench_poseidon<S, const WIDTH: usize, const RATE: usize, const L: usize>(

// Create a proof
let mut transcript = Blake2bWrite::<_, EqAffine, Challenge255<_>>::init(vec![]);
create_proof::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _>(
create_proof::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _, ZK>(
&params,
&pk,
&[circuit],
Expand All @@ -254,7 +256,14 @@ fn bench_poseidon<S, const WIDTH: usize, const RATE: usize, const L: usize>(
use halo2_proofs::poly::VerificationStrategy;
let strategy = SingleStrategy::new(&params);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof[..]);
assert!(verify_proof(&params, pk.get_vk(), strategy, &[&[]], &mut transcript).is_ok());
assert!(verify_proof::<_, _, _, _, _, ZK>(
&params,
pk.get_vk(),
strategy,
&[&[]],
&mut transcript
)
.is_ok());
});
});
}
Expand Down
10 changes: 6 additions & 4 deletions halo2_gadgets/benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use criterion::{criterion_group, criterion_main, Criterion};

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

const ZK: bool = true;

#[allow(dead_code)]
fn bench(name: &str, k: u32, c: &mut Criterion) {
#[derive(Default)]
Expand Down Expand Up @@ -100,9 +102,9 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
let empty_circuit: MyCircuit = MyCircuit {};

// Initialize the proving key
let vk = keygen_vk::<IPACommitmentScheme<_>, _>(&params, &empty_circuit)
let vk = keygen_vk::<IPACommitmentScheme<_>, _, ZK>(&params, &empty_circuit)
.expect("keygen_vk should not fail");
let pk = keygen_pk::<IPACommitmentScheme<_>, _>(&params, vk, &empty_circuit)
let pk = keygen_pk::<IPACommitmentScheme<_>, _, ZK>(&params, vk, &empty_circuit)
.expect("keygen_pk should not fail");

let circuit: MyCircuit = MyCircuit {};
Expand All @@ -124,7 +126,7 @@ 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::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _>(
create_proof::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _, ZK>(
&params,
&pk,
&[circuit],
Expand All @@ -149,7 +151,7 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
use halo2_proofs::poly::VerificationStrategy;
let strategy = AccumulatorStrategy::new(&params);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof[..]);
let strategy = verify_proof::<IPACommitmentScheme<_>, _, _, VerifierIPA<_>, _>(
let strategy = verify_proof::<IPACommitmentScheme<_>, _, _, VerifierIPA<_>, _, ZK>(
&params,
pk.get_vk(),
strategy,
Expand Down
8 changes: 6 additions & 2 deletions halo2_gadgets/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,9 +895,11 @@ pub(crate) mod tests {

#[test]
fn ecc_chip() {
const ZK: bool = true;

let k = 13;
let circuit = MyCircuit { test_errors: true };
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
let prover = MockProver::run::<_, ZK>(k, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()))
}

Expand All @@ -906,13 +908,15 @@ pub(crate) mod tests {
fn print_ecc_chip() {
use plotters::prelude::*;

const ZK: bool = true;

let root = BitMapBackend::new("ecc-chip-layout.png", (1024, 7680)).into_drawing_area();
root.fill(&WHITE).unwrap();
let root = root.titled("Ecc Chip Layout", ("sans-serif", 60)).unwrap();

let circuit = MyCircuit { test_errors: false };
halo2_proofs::dev::CircuitLayout::default()
.render(13, &circuit, &root)
.render::<_, _, _, ZK>(13, &circuit, &root)
.unwrap();
}
}
6 changes: 4 additions & 2 deletions halo2_gadgets/src/ecc/chip/mul_fixed/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ pub mod tests {
Ok(())
}

const ZK: bool = true;

#[test]
fn invalid_magnitude_sign() {
use crate::{
Expand Down Expand Up @@ -563,7 +565,7 @@ pub mod tests {
];

for circuit in circuits.iter() {
let prover = MockProver::<pallas::Base>::run(11, circuit, vec![]).unwrap();
let prover = MockProver::<pallas::Base>::run::<_, ZK>(11, circuit, vec![]).unwrap();
circuit.magnitude_error.assert_if_known(|magnitude_error| {
assert_eq!(
prover.verify(),
Expand Down Expand Up @@ -621,7 +623,7 @@ pub mod tests {
.y()
};

let prover = MockProver::<pallas::Base>::run(11, &circuit, vec![]).unwrap();
let prover = MockProver::<pallas::Base>::run::<_, ZK>(11, &circuit, vec![]).unwrap();
assert_eq!(
prover.verify(),
Err(vec![
Expand Down
19 changes: 14 additions & 5 deletions halo2_gadgets/src/poseidon/pow5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,11 @@ mod tests {

#[test]
fn poseidon_permute() {
const ZK: bool = true;

let k = 6;
let circuit = PermuteCircuit::<OrchardNullifier, 3, 2>(PhantomData);
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
let prover = MockProver::run::<_, ZK>(k, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()))
}

Expand Down Expand Up @@ -810,6 +812,8 @@ mod tests {

#[test]
fn poseidon_hash() {
const ZK: bool = true;

let rng = OsRng;

let message = [Fp::random(rng), Fp::random(rng)];
Expand All @@ -822,12 +826,14 @@ mod tests {
output: Value::known(output),
_spec: PhantomData,
};
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
let prover = MockProver::run::<_, ZK>(k, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()))
}

#[test]
fn poseidon_hash_longer_input() {
const ZK: bool = true;

let rng = OsRng;

let message = [Fp::random(rng), Fp::random(rng), Fp::random(rng)];
Expand All @@ -840,12 +846,13 @@ mod tests {
output: Value::known(output),
_spec: PhantomData,
};
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
let prover = MockProver::run::<_, ZK>(k, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()))
}

#[test]
fn hash_test_vectors() {
const ZK: bool = true;
for tv in crate::poseidon::primitives::test_vectors::fp::hash() {
let message = [
pallas::Base::from_repr(tv.input[0]).unwrap(),
Expand All @@ -860,7 +867,7 @@ mod tests {
output: Value::known(output),
_spec: PhantomData,
};
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
let prover = MockProver::run::<_, ZK>(k, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()));
}
}
Expand All @@ -870,6 +877,8 @@ mod tests {
fn print_poseidon_chip() {
use plotters::prelude::*;

const ZK: bool = true;

let root = BitMapBackend::new("poseidon-chip-layout.png", (1024, 768)).into_drawing_area();
root.fill(&WHITE).unwrap();
let root = root
Expand All @@ -882,7 +891,7 @@ mod tests {
_spec: PhantomData,
};
halo2_proofs::dev::CircuitLayout::default()
.render(6, &circuit, &root)
.render::<_, _, _, ZK>(6, &circuit, &root)
.unwrap();
}
}
4 changes: 3 additions & 1 deletion halo2_gadgets/src/sha256/table16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ mod tests {
};
use halo2curves::pasta::pallas;

const ZK: bool = true;

#[test]
fn print_sha256_circuit() {
use plotters::prelude::*;
Expand Down Expand Up @@ -509,7 +511,7 @@ mod tests {

let circuit = MyCircuit {};
halo2_proofs::dev::CircuitLayout::default()
.render::<pallas::Base, _, _>(17, &circuit, &root)
.render::<pallas::Base, _, _, ZK>(17, &circuit, &root)
.unwrap();
}
}
4 changes: 3 additions & 1 deletion halo2_gadgets/src/sha256/table16/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,8 @@ mod tests {
};
use halo2curves::pasta::pallas;

const ZK: bool = true;

#[test]
fn compress() {
struct MyCircuit {}
Expand Down Expand Up @@ -996,7 +998,7 @@ mod tests {

let circuit: MyCircuit = MyCircuit {};

let prover = match MockProver::<pallas::Base>::run(17, &circuit, vec![]) {
let prover = match MockProver::<pallas::Base>::run::<_, ZK>(17, &circuit, vec![]) {
Ok(prover) => prover,
Err(e) => panic!("{:?}", e),
};
Expand Down
4 changes: 3 additions & 1 deletion halo2_gadgets/src/sha256/table16/message_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ mod tests {
};
use halo2curves::pasta::pallas;

const ZK: bool = true;

#[test]
fn message_schedule() {
struct MyCircuit {}
Expand Down Expand Up @@ -446,7 +448,7 @@ mod tests {

let circuit: MyCircuit = MyCircuit {};

let prover = match MockProver::<pallas::Base>::run(17, &circuit, vec![]) {
let prover = match MockProver::<pallas::Base>::run::<_, ZK>(17, &circuit, vec![]) {
Ok(prover) => prover,
Err(e) => panic!("{:?}", e),
};
Expand Down
4 changes: 3 additions & 1 deletion halo2_gadgets/src/sha256/table16/spread_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ mod tests {
};
use halo2curves::pasta::Fp;

const ZK: bool = true;

#[test]
fn lookup_table() {
/// This represents an advice column at a certain row in the ConstraintSystem
Expand Down Expand Up @@ -439,7 +441,7 @@ mod tests {

let circuit: MyCircuit = MyCircuit {};

let prover = match MockProver::<Fp>::run(17, &circuit, vec![]) {
let prover = match MockProver::<Fp>::run::<_, ZK>(17, &circuit, vec![]) {
Ok(prover) => prover,
Err(e) => panic!("{:?}", e),
};
Expand Down
8 changes: 6 additions & 2 deletions halo2_gadgets/src/sinsemilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,11 @@ pub(crate) mod tests {

#[test]
fn sinsemilla_chip() {
const ZK: bool = true;

let k = 11;
let circuit = MyCircuit {};
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
let prover = MockProver::run::<_, ZK>(k, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()))
}

Expand All @@ -742,14 +744,16 @@ pub(crate) mod tests {
fn print_sinsemilla_chip() {
use plotters::prelude::*;

const ZK: bool = true;

let root =
BitMapBackend::new("sinsemilla-hash-layout.png", (1024, 7680)).into_drawing_area();
root.fill(&WHITE).unwrap();
let root = root.titled("SinsemillaHash", ("sans-serif", 60)).unwrap();

let circuit = MyCircuit {};
halo2_proofs::dev::CircuitLayout::default()
.render(11, &circuit, &root)
.render::<_, _, _, ZK>(11, &circuit, &root)
.unwrap();
}
}
8 changes: 6 additions & 2 deletions halo2_gadgets/src/sinsemilla/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ pub mod tests {

#[test]
fn merkle_chip() {
const ZK: bool = true;

let mut rng = OsRng;

// Choose a random leaf and position
Expand All @@ -376,7 +378,7 @@ pub mod tests {
merkle_path: Value::known(path.try_into().unwrap()),
};

let prover = MockProver::run(11, &circuit, vec![]).unwrap();
let prover = MockProver::run::<_, ZK>(11, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()))
}

Expand All @@ -385,14 +387,16 @@ pub mod tests {
fn print_merkle_chip() {
use plotters::prelude::*;

const ZK: bool = true;

let root = BitMapBackend::new("merkle-path-layout.png", (1024, 7680)).into_drawing_area();
root.fill(&WHITE).unwrap();
let root = root.titled("MerkleCRH Path", ("sans-serif", 60)).unwrap();

let circuit = MyCircuit::default();
halo2_proofs::dev::CircuitLayout::default()
.show_labels(false)
.render(11, &circuit, &root)
.render::<_, _, _, ZK>(11, &circuit, &root)
.unwrap();
}
}
6 changes: 4 additions & 2 deletions halo2_gadgets/src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,17 @@ mod tests {
}
}

const ZK: bool = true;

for i in 0..8 {
let circuit: MyCircuit<8> = MyCircuit(i);
let prover = MockProver::<pallas::Base>::run(3, &circuit, vec![]).unwrap();
let prover = MockProver::<pallas::Base>::run::<_, ZK>(3, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()));
}

{
let circuit: MyCircuit<8> = MyCircuit(8);
let prover = MockProver::<pallas::Base>::run(3, &circuit, vec![]).unwrap();
let prover = MockProver::<pallas::Base>::run::<_, ZK>(3, &circuit, vec![]).unwrap();
assert_eq!(
prover.verify(),
Err(vec![VerifyFailure::ConstraintNotSatisfied {
Expand Down
Loading

0 comments on commit 57225ee

Please sign in to comment.