Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress VerifyingKey #19

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ckb_bf_base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ halo2_gadgets = { path = "../halo2_gadgets" }
halo2_proofs = { path = "../halo2_proofs" }
ckb_bf_vm = {path = "../ckb_bf_vm"}
halo2curves = {path = "../halo2curves" }

lz4_flex = { version = "0.10", default-features = false }
37 changes: 36 additions & 1 deletion ckb_bf_base/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use halo2_proofs::halo2curves::bn256::Fr;
use halo2_proofs::helpers::SerdeCurveAffine;
use halo2_proofs::poly::kzg::commitment::ParamsVerifierKZG;
use halo2_proofs::{plonk::*, SerdeFormat};
use halo2curves::io;
use halo2curves::serde::SerdeObject;
use halo2curves::{io, io::Write, CurveAffine};
use lz4_flex::{compress_prepend_size};

use crate::SHRINK_K;

Expand Down Expand Up @@ -104,3 +106,36 @@ where
s_g2,
})
}

pub fn compress_vk<W: io::Write, C: CurveAffine + SerdeObject>(
vk: &VerifyingKey<C>,
writer: &mut W,
format: SerdeFormat,
) -> io::Result<()> {
writer.write_all(&vk.domain.k().to_be_bytes())?;
writer.write_all(&(vk.fixed_commitments.len() as u32).to_be_bytes())?;
for commitment in &vk.fixed_commitments {
commitment.write(writer, format)?;
}
vk.permutation.write(writer, format)?;

let mut sel_vec = vec![];
// write self.selectors
for selector in &vk.selectors {
// since `selector` is filled with `bool`, we pack them 8 at a time into bytes and then write
for bits in selector.chunks(8) {
sel_vec.write_all(&[halo2_proofs::helpers::pack(bits)])?;
}
}
let compressed_sel_vec = compress_prepend_size(&sel_vec);
writer.write_all(&compressed_sel_vec)?;
Ok(())
}

pub fn read_vk<R: io::Read, C: CurveAffine + SerdeObject>(
_vk: VerifyingKey<C>,
_reader: &mut R,
_format: SerdeFormat,
) -> io::Result<()> {
Ok(())
}
11 changes: 11 additions & 0 deletions ckb_bf_prover/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
use std::{fs::File, io::Write};

pub mod ckb_tx;

pub fn write_selectors(file_name: String, selectors: &Vec<Vec<bool>>) {
let mut writer = File::create(file_name).expect("File::create");
for selector in selectors {
for bits in selector.chunks(8) {
writer.write(&[halo2_proofs::helpers::pack(bits)]).expect("write");
}
}
}
19 changes: 12 additions & 7 deletions ckb_bf_prover/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ckb_bf_base::main_config::MyCircuit;
use ckb_bf_base::utils::{read_verifier_params, DOMAIN};
use ckb_bf_base::utils::{read_verifier_params, DOMAIN, compress_vk};
use ckb_bf_base::{GOD_PRIVATE_KEY, SHRINK_K};
use ckb_bf_prover::ckb_tx::build_ckb_tx;

Expand Down Expand Up @@ -42,16 +42,21 @@ fn prove_and_verify(k: u32, circuit: MyCircuit<Fr, DOMAIN>, _public_inputs: &[&[
info!("create_proof done");

let proof = transcript.finalize();
info!("proof length : {}", proof.len());
let vk = pk.get_vk();

let mut vk_buf = vec![];
// for "hello, world":
// verifying key can be compressed from 2760
// verifying key can be compressed(RawBytes->Processed) from 2760
// bytes to 1832 bytes with cost of cycles from 75M to 83M
//
pk.get_vk().write(&mut vk_buf, halo2_proofs::SerdeFormat::RawBytes).expect("write");

info!("proof length : {}", proof.len());
//
vk.write(&mut vk_buf, halo2_proofs::SerdeFormat::RawBytes).expect("write");
info!("vk length: {}", vk_buf.len());

let mut compressed_vk_buf = vec![];
compress_vk(vk, &mut compressed_vk_buf, halo2_proofs::SerdeFormat::RawBytes).expect("compress_vk");
info!("vk length (compressed): {}", compressed_vk_buf.len());

// verifier
verifier_params.shrink(SHRINK_K);
let mut verifier_params_buf = vec![];
Expand All @@ -66,7 +71,7 @@ fn prove_and_verify(k: u32, circuit: MyCircuit<Fr, DOMAIN>, _public_inputs: &[&[
Challenge255<G1Affine>,
Blake2bRead<&[u8], G1Affine, Challenge255<G1Affine>>,
SingleStrategy<'_, Bn256>,
>(&verifier_params, pk.get_vk(), strategy, &[], &mut verifier_transcript)
>(&verifier_params, vk, strategy, &[], &mut verifier_transcript)
.expect("verify_proof");

// build ckb tx
Expand Down
14 changes: 7 additions & 7 deletions halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ use evaluation::Evaluator;
/// particular circuit.
#[derive(Clone, Debug)]
pub struct VerifyingKey<C: CurveAffine> {
domain: EvaluationDomain<C::Scalar>,
fixed_commitments: Vec<C>,
permutation: permutation::VerifyingKey<C>,
cs: ConstraintSystem<C::Scalar>,
pub domain: EvaluationDomain<C::Scalar>,
pub fixed_commitments: Vec<C>,
pub permutation: permutation::VerifyingKey<C>,
pub cs: ConstraintSystem<C::Scalar>,
/// Cached maximum degree of `cs` (which doesn't change after construction).
cs_degree: usize,
pub cs_degree: usize,
/// The representative of this `VerifyingKey` in transcripts.
transcript_repr: C::Scalar,
selectors: Vec<Vec<bool>>,
pub transcript_repr: C::Scalar,
pub selectors: Vec<Vec<bool>>,
}

impl<C: SerdeCurveAffine> VerifyingKey<C>
Expand Down
6 changes: 3 additions & 3 deletions halo2_proofs/src/plonk/permutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<C: CurveAffine> VerifyingKey<C> {
&self.commitments
}

pub(crate) fn write<W: io::Write>(&self, writer: &mut W, format: SerdeFormat) -> io::Result<()>
pub fn write<W: io::Write>(&self, writer: &mut W, format: SerdeFormat) -> io::Result<()>
where
C: SerdeCurveAffine,
{
Expand All @@ -100,7 +100,7 @@ impl<C: CurveAffine> VerifyingKey<C> {
Ok(())
}

pub(crate) fn read<R: io::Read>(
pub fn read<R: io::Read>(
reader: &mut R,
argument: &Argument,
format: SerdeFormat,
Expand All @@ -114,7 +114,7 @@ impl<C: CurveAffine> VerifyingKey<C> {
Ok(VerifyingKey { commitments })
}

pub(crate) fn bytes_length(&self) -> usize {
pub fn bytes_length(&self) -> usize {
self.commitments.len() * C::default().to_bytes().as_ref().len()
}
}
Expand Down