Skip to content

Commit

Permalink
feat: KZG commitments with single strategy for proving (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-camuto authored Dec 18, 2022
1 parent 2f3a4cc commit f228d23
Show file tree
Hide file tree
Showing 13 changed files with 754 additions and 596 deletions.
76 changes: 72 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,82 @@ jobs:

steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

library-tests:

runs-on: self-hosted

steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy
- name: Doc tests
run: cargo test --doc --verbose
- name: Library tests
run: cargo test --lib --verbose

mock-proving-tests:

runs-on: self-hosted
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy

- name: Mock proving tests (public outputs)
run: cargo test --release --verbose tests::mock_public_outputs_
- name: Mock proving tests (public inputs)
run: cargo test --release --verbose tests::mock_public_inputs_
- name: Mock proving tests (public params)
run: cargo test --release --verbose tests::mock_public_params_

full-proving-tests:

runs-on: self-hosted
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy

- name: IPA full-prove tests
run: cargo test --release --verbose tests::ipa_fullprove_ -- --test-threads 1
- name: KZG full-prove tests
run: cargo test --release --verbose tests::kzg_fullprove_ -- --test-threads 1

prove-and-verify-tests:

runs-on: self-hosted
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy

- name: IPA prove and verify tests
run: cargo test --release --verbose tests::ipa_prove_and_verify_ -- --test-threads 1
- name: KZG prove and verify tests
run: cargo test --release --verbose tests::kzg_prove_and_verify_ -- --test-threads 1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ target
Cargo.lock
data
*.pf
*.vk
*.params
*~
\#*\#
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2", tag = "v2022_10_22"}
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2", rev = "50ee8ad"}
halo2curves = { git = 'https://github.com/privacy-scaling-explorations/halo2curves', tag = "0.3.0" }
# nalgebra = "0.31"
rand = "0.8"
Expand Down Expand Up @@ -35,6 +35,7 @@ plonk_verifier = { git = "https://github.com/privacy-scaling-explorations/plonk
criterion = {version = "0.3", features = ["html_reports"]}
seq-macro = "0.3.1"
test-case = "2.2.2"
ctor = "0.1.26"

[[bench]]
name = "affine"
Expand Down
192 changes: 115 additions & 77 deletions src/bin/ezkl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,42 @@ use ezkl::abort;
use ezkl::commands::{Cli, Commands, ProofSystem};
use ezkl::fieldutils::i32_to_felt;
use ezkl::graph::Model;
use ezkl::pfsys::ipa::{create_ipa_proof, verify_ipa_proof};
#[cfg(feature = "evm")]
use ezkl::pfsys::kzg::{
use ezkl::pfsys::kzg::aggregation::{
aggregation::AggregationCircuit, evm_verify, gen_aggregation_evm_verifier,
gen_application_snark, gen_kzg_proof, gen_pk, gen_srs,
};
use ezkl::pfsys::Proof;
use ezkl::pfsys::{parse_prover_errors, prepare_circuit_and_public_input, prepare_data};
use ezkl::pfsys::{create_keys, load_params, load_vk, Proof};
#[cfg(not(feature = "evm"))]
use ezkl::pfsys::{
create_proof_model, parse_prover_errors, prepare_circuit_and_public_input, prepare_data,
save_params, save_vk, verify_proof_model,
};
#[cfg(feature = "evm")]
use halo2_proofs::poly::commitment::Params;
use halo2_proofs::poly::ipa::commitment::IPACommitmentScheme;
use halo2_proofs::poly::ipa::multiopen::ProverIPA;
use halo2_proofs::poly::kzg::commitment::KZGCommitmentScheme;
use halo2_proofs::poly::kzg::multiopen::ProverGWC;
#[cfg(not(feature = "evm"))]
use halo2_proofs::poly::kzg::{
commitment::ParamsKZG, multiopen::VerifierGWC, strategy::SingleStrategy as KZGSingleStrategy,
};
use halo2_proofs::{
dev::MockProver,
plonk::verify_proof,
poly::{
commitment::ParamsProver,
ipa::{commitment::ParamsIPA, strategy::SingleStrategy},
ipa::{commitment::ParamsIPA, strategy::SingleStrategy as IPASingleStrategy},
VerificationStrategy,
},
transcript::{Blake2bRead, Challenge255, TranscriptReadBuffer},
};
#[cfg(feature = "evm")]
use halo2curves::bn256::G1Affine;
use halo2curves::bn256::{Bn256, Fr};
use halo2curves::pasta::vesta;
use halo2curves::pasta::Fp;
use log::{error, info, trace};
#[cfg(feature = "evm")]
use plonk_verifier::system::halo2::transcript::evm::EvmTranscript;
use rand::seq::SliceRandom;
use std::fs::File;
use std::io::{Read, Write};
use std::ops::Deref;
use std::time::Instant;
use tabled::Table;

pub fn main() {
Expand Down Expand Up @@ -81,46 +85,59 @@ pub fn main() {
model: _,
pfsys,
} => {
// A direct proof
let args = Cli::parse();
let data = prepare_data(data);
let (circuit, public_inputs) = prepare_circuit_and_public_input(&data);
info!("full proof with {}", pfsys);

match pfsys {
ProofSystem::IPA => {
// A direct proof
let (circuit, public_inputs) = prepare_circuit_and_public_input::<Fp>(&data);
info!("full proof with {}", pfsys);

let params: ParamsIPA<vesta::Affine> = ParamsIPA::new(args.logrows);
let pk = create_keys::<IPACommitmentScheme<_>, Fp>(&circuit, &params);
let strategy = IPASingleStrategy::new(&params);
trace!("params computed");

let (pk, proof, _dims) =
create_ipa_proof(circuit, public_inputs.clone(), &params);
let (proof, _dims) = create_proof_model::<
IPACommitmentScheme<_>,
Fp,
ProverIPA<_>,
>(
&circuit, &public_inputs, &params, &pk
);

assert!(verify_proof_model(proof, &params, pk.get_vk(), strategy));
}
#[cfg(not(feature = "evm"))]
ProofSystem::KZG => {
// A direct proof
let (circuit, public_inputs) = prepare_circuit_and_public_input::<Fr>(&data);
let params: ParamsKZG<Bn256> = ParamsKZG::new(args.logrows);
let pk = create_keys::<KZGCommitmentScheme<_>, Fr>(&circuit, &params);
let strategy = KZGSingleStrategy::new(&params);
trace!("params computed");

let pi_inner: Vec<Vec<Fp>> = public_inputs
.iter()
.map(|i| i.iter().map(|e| i32_to_felt::<Fp>(*e)).collect::<Vec<Fp>>())
.collect::<Vec<Vec<Fp>>>();
let pi_inner = pi_inner.iter().map(|e| e.deref()).collect::<Vec<&[Fp]>>();
let pi_for_real_prover: &[&[&[Fp]]] = &[&pi_inner];
let (proof, _dims) = create_proof_model::<
KZGCommitmentScheme<_>,
Fr,
ProverGWC<_>,
>(
&circuit, &public_inputs, &params, &pk
);

let now = Instant::now();
let strategy = SingleStrategy::new(&params);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof[..]);
assert!(verify_proof(
assert!(verify_proof_model::<_, VerifierGWC<'_, Bn256>, _, _>(
proof,
&params,
pk.get_vk(),
strategy,
pi_for_real_prover,
&mut transcript
)
.is_ok());
info!("verify took {}", now.elapsed().as_secs());
strategy
));
}
#[cfg(not(feature = "evm"))]
ProofSystem::KZG => todo!(),
#[cfg(feature = "evm")]
ProofSystem::KZG => {
// We will need aggregator k > application k > bits
// let application_logrows = args.logrows; //bits + 1;
let (circuit, public_inputs) = prepare_circuit_and_public_input(&data);
let aggregation_logrows = args.logrows + 6;

let params = gen_srs(aggregation_logrows);
Expand Down Expand Up @@ -159,63 +176,84 @@ pub fn main() {
Commands::Prove {
data,
model: _,
output,
proof_path,
vk_path,
params_path,
pfsys,
} => {
let args = Cli::parse();
let data = prepare_data(data);
let (circuit, public_inputs) = prepare_circuit_and_public_input(&data);
info!("proof with {}", pfsys);
let params: ParamsIPA<vesta::Affine> = ParamsIPA::new(args.logrows);
trace!("params computed");

let (_pk, proof, _input_dims) =
create_ipa_proof(circuit.clone(), public_inputs.clone(), &params);
match pfsys {
ProofSystem::IPA => {
info!("proof with {}", pfsys);
let (circuit, public_inputs) = prepare_circuit_and_public_input::<Fp>(&data);
let params: ParamsIPA<vesta::Affine> = ParamsIPA::new(args.logrows);
let pk = create_keys::<IPACommitmentScheme<_>, Fp>(&circuit, &params);
trace!("params computed");

let (proof, _) = create_proof_model::<IPACommitmentScheme<_>, Fp, ProverIPA<_>>(
&circuit,
&public_inputs,
&params,
&pk,
);

let pi: Vec<_> = public_inputs
.into_iter()
.map(|i| i.into_iter().collect())
.collect();
proof.save(&proof_path);
save_params::<IPACommitmentScheme<_>>(&params_path, &params);
save_vk::<IPACommitmentScheme<_>>(&vk_path, pk.get_vk());
}
ProofSystem::KZG => {
info!("proof with {}", pfsys);
let (circuit, public_inputs) = prepare_circuit_and_public_input(&data);
let params: ParamsKZG<Bn256> = ParamsKZG::new(args.logrows);
let pk = create_keys::<KZGCommitmentScheme<Bn256>, Fr>(&circuit, &params);
trace!("params computed");

let checkable_pf = Proof {
input_shapes: circuit.inputs.iter().map(|i| i.dims().to_vec()).collect(),
public_inputs: pi,
proof,
};
let (proof, _input_dims) = create_proof_model::<
KZGCommitmentScheme<Bn256>,
Fr,
ProverGWC<'_, Bn256>,
>(
&circuit, &public_inputs, &params, &pk
);

let serialized = match serde_json::to_string(&checkable_pf) {
Ok(s) => s,
Err(e) => {
abort!("failed to convert proof json to string {:?}", e);
proof.save(&proof_path);
save_params::<KZGCommitmentScheme<Bn256>>(&params_path, &params);
save_vk::<KZGCommitmentScheme<Bn256>>(&vk_path, pk.get_vk());
}
};

let mut file = std::fs::File::create(output).expect("create failed");
file.write_all(serialized.as_bytes()).expect("write failed");
}
Commands::Verify {
model: _,
proof,
pfsys: _,
proof_path,
vk_path,
params_path,
pfsys,
} => {
let mut file = match File::open(proof) {
Ok(f) => f,
Err(e) => {
abort!("failed to open proof file {:?}", e);
let proof = Proof::load(&proof_path);
match pfsys {
ProofSystem::IPA => {
let params: ParamsIPA<vesta::Affine> =
load_params::<IPACommitmentScheme<_>>(params_path);
let strategy = IPASingleStrategy::new(&params);
let vk = load_vk::<IPACommitmentScheme<_>, Fp>(vk_path, &params);
let result = verify_proof_model(proof, &params, &vk, strategy);
info!("verified: {}", result);
assert!(result);
}
};
let mut data = String::new();
match file.read_to_string(&mut data) {
Ok(_) => {}
Err(e) => {
abort!("failed to read file {:?}", e);
ProofSystem::KZG => {
let params: ParamsKZG<Bn256> =
load_params::<KZGCommitmentScheme<Bn256>>(params_path);
let strategy = KZGSingleStrategy::new(&params);
let vk = load_vk::<KZGCommitmentScheme<Bn256>, Fr>(vk_path, &params);
let result = verify_proof_model::<_, VerifierGWC<'_, Bn256>, _, _>(
proof, &params, &vk, strategy,
);
info!("verified: {}", result);
assert!(result);
}
};
let proof: Proof = serde_json::from_str(&data).expect("JSON was not well-formatted");

let result = verify_ipa_proof(proof);
info!("verified: {}", result);
assert!(result);
}
}
}
}
Expand Down
Loading

0 comments on commit f228d23

Please sign in to comment.