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

feat: introduce a host circuit friendly transaction slicer #296

Merged
merged 6 commits into from
Oct 19, 2024
Merged
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
46 changes: 42 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
env_logger = "0.9.3"
halo2aggregator-s = { git = "https://github.com/DelphinusLab/halo2aggregator-s.git", tag="on-prove-pairing-0.6.2" }
log = "0.4.17"
md5 = "0.7.0"
sha2 = "0.10.6"
Expand Down
103 changes: 55 additions & 48 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ impl Config {
println!("skip first {} slice(s)", skip);
}

#[cfg(feature = "continuation")]
let mut last_post_image_table_commitment: Option<(String, String)> = None;

let mut slices = Slices::new(self.k, tables, padding)?
.into_iter()
.enumerate()
Expand Down Expand Up @@ -408,12 +411,14 @@ impl Config {
transcript: name_of_transcript(&self.name, index),
};

let pkey = &cached_proving_key.as_ref().unwrap().1;

let proof = match circuit {
ZkWasmCircuit::Ongoing(circuit) => proof_piece_info.create_proof::<Bn256, _>(
&circuit,
&vec![instances.clone()],
&params,
&cached_proving_key.as_ref().unwrap().1,
pkey,
proof_load_info.hashtype,
self.scheme.into(),
),
Expand All @@ -422,12 +427,43 @@ impl Config {
&circuit,
&vec![instances.clone()],
&params,
&cached_proving_key.as_ref().unwrap().1,
pkey,
proof_load_info.hashtype,
self.scheme.into(),
),
};

#[cfg(feature = "continuation")]
{
use crate::utils::get_named_advice_commitment;
use delphinus_zkwasm::circuits::image_table::IMAGE_COL_NAME;
use delphinus_zkwasm::circuits::post_image_table::POST_IMAGE_TABLE;

// checks pre image col equals to last's post image col commitment
let pre_image_table_msm =
get_named_advice_commitment(pkey.get_vk(), &proof, IMAGE_COL_NAME);

let last_post_image_table_msm = last_post_image_table_commitment.take();
if let Some(last_post_image_table_msm) = last_post_image_table_msm {
assert_eq!(
pre_image_table_msm.x.to_string(),
last_post_image_table_msm.0
);
assert_eq!(
pre_image_table_msm.y.to_string(),
last_post_image_table_msm.1
);
}

let post_image_table_msm =
get_named_advice_commitment(pkey.get_vk(), &proof, POST_IMAGE_TABLE);

last_post_image_table_commitment = Some((
post_image_table_msm.x.to_string(),
post_image_table_msm.y.to_string(),
));
}

proof_piece_info.save_proof_data(&vec![instances.clone()], &proof, output_dir);

proof_load_info.append_single_proof(proof_piece_info);
Expand All @@ -450,6 +486,8 @@ impl Config {
}

pub(crate) fn verify(self, params_dir: &Path, output_dir: &PathBuf) -> anyhow::Result<()> {
let mut maximal_public_inputs_size = 0;

let mut proofs = {
println!(
"{} Reading proofs from {:?}",
Expand All @@ -463,6 +501,16 @@ impl Config {
let proofs: Vec<ProofInfo<Bn256>> =
ProofInfo::load_proof(output_dir, params_dir, &proof_load_info);

for proof in &proofs {
maximal_public_inputs_size = usize::max(
maximal_public_inputs_size,
proof
.instances
.iter()
.fold(0, |acc, x| usize::max(acc, x.len())),
);
}

proofs
}
.into_iter()
Expand All @@ -474,18 +522,13 @@ impl Config {
proofs.len()
);

let params_verifier = {
let params = self.read_params(params_dir)?;
params.verifier(maximal_public_inputs_size)?
};

let progress_bar = ProgressBar::new(proofs.len() as u64);
while let Some(proof) = proofs.next() {
let params_verifier = {
let public_inputs_size = proof
.instances
.iter()
.fold(0, |acc, x| usize::max(acc, x.len()));

let params = self.read_params(params_dir)?;
params.verifier(public_inputs_size)?
};

{
let mut buf = Vec::new();
proof.vkey.write(&mut Cursor::new(&mut buf))?;
Expand Down Expand Up @@ -514,42 +557,6 @@ impl Config {
.verify_proof(&params_verifier, self.scheme.into())
.unwrap();

// TODO: handle checksum sanity check
// #[cfg(feature = "uniform-circuit")]
// {
// use delphinus_zkwasm::circuits::image_table::IMAGE_COL_NAME;
// use halo2_proofs::plonk::get_advice_commitments_from_transcript;
// use halo2aggregator_s::transcript::poseidon::PoseidonRead;

// let _img_col_idx = proof
// .vkey
// .cs
// .named_advices
// .iter()
// .find(|(k, _)| k == IMAGE_COL_NAME)
// .unwrap()
// .1;
// let _img_col_commitment: Vec<G1Affine> =
// get_advice_commitments_from_transcript::<Bn256, _, _>(
// &proof.vkey,
// &mut PoseidonRead::init(&proof.transcripts[..]),
// )
// .unwrap();

// assert!(
// vec![_img_col_commitment[_img_col_idx as usize]][0]
// .x
// .to_string()
// == self.checksum.0
// );
// assert!(
// vec![_img_col_commitment[_img_col_idx as usize]][0]
// .y
// .to_string()
// == self.checksum.1
// );
// }

progress_bar.inc(1);
}
progress_bar.finish_and_clear();
Expand Down
9 changes: 6 additions & 3 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use delphinus_zkwasm::runtime::host::default_env::ExecutionArg;
use args::HostMode;
use config::Config;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use delphinus_zkwasm::zkwasm_host_circuits::host::db::MongoDB;
use file_backend::FileBackendBuilder;
use names::name_of_config;
use specs::args::parse_args;
Expand All @@ -29,6 +30,8 @@ mod config;
mod file_backend;
mod names;

pub mod utils;

const TRIVIAL_WASM: &str = r#"
(module
(func (export "zkmain"))
Expand Down Expand Up @@ -84,7 +87,7 @@ fn main() -> Result<()> {
private_inputs,
context_inputs,
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
tree_db: Some(Rc::new(RefCell::new(MongoDB::new([0; 32], None)))),
},
arg.running_arg.context_output,
arg.instruction_limit,
Expand Down Expand Up @@ -121,7 +124,7 @@ fn main() -> Result<()> {
private_inputs,
context_inputs,
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
tree_db: Some(Rc::new(RefCell::new(MongoDB::new([0; 32], None)))),
},
arg.running_arg.context_output,
arg.mock_test,
Expand All @@ -142,7 +145,7 @@ fn main() -> Result<()> {
private_inputs,
context_inputs,
indexed_witness: Rc::new(RefCell::new(HashMap::default())),
tree_db: None,
tree_db: Some(Rc::new(RefCell::new(MongoDB::new([0; 32], None)))),
},
arg.running_arg.context_output,
arg.mock_test,
Expand Down
22 changes: 22 additions & 0 deletions crates/cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use halo2_proofs::pairing::bn256::Bn256;
use halo2_proofs::pairing::bn256::G1Affine;
use halo2_proofs::plonk::get_advice_commitments_from_transcript;
use halo2_proofs::plonk::VerifyingKey;
use halo2aggregator_s::transcript::poseidon::PoseidonRead;

pub fn get_named_advice_commitment(
vkey: &VerifyingKey<G1Affine>,
proof: &[u8],
named_advice: &str,
) -> G1Affine {
let img_col_idx = vkey
.cs
.named_advices
.iter()
.find(|(k, _)| k == named_advice)
.unwrap()
.1;

get_advice_commitments_from_transcript::<Bn256, _, _>(vkey, &mut PoseidonRead::init(proof))
.unwrap()[img_col_idx as usize]
}
Loading
Loading