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

Explorer integration 1.0 #266

Open
wants to merge 9 commits into
base: explorer-integration
Choose a base branch
from
Open
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
30 changes: 14 additions & 16 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ exclude = ["third-party/wasmi", "crates/playground"]
resolver = "2"

[workspace.dependencies]
anyhow = { version = "1.0.68", features = ["backtrace"] }
anyhow = { version = "1.0.86", features = ["backtrace"] }
cfg-if = "1.0.0"
halo2aggregator-s = { git = "https://github.com/DelphinusLab/halo2aggregator-s.git", features = ["unsafe"] }
halo2_proofs = { git = "https://github.com/DelphinusLab/halo2-gpu-specific.git", default-features = true }
parity-wasm = { version = "0.42.0", features = ["sign_ext"] }
wasmi = { path = "third-party/wasmi" }
circuits-batcher = { git = "https://github.com/DelphinusLab/continuation-batcher.git" }
zkwasm-host-circuits = { git = "https://github.com/DelphinusLab/zkWasm-host-circuits.git" }
zkwasm-host-circuits = { git = "https://github.com/DelphinusLab/zkWasm-host-circuits.git", branch="xgao/dynamic-db-uri" }

[profile.dev]
opt-level = 3

5 changes: 2 additions & 3 deletions crates/cli/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]
ark-std = { version = "0.3.0", features = ["print-trace"] }
ark-std = { version = "0.4.0"}
env_logger = "0.9.3"
log = "0.4.17"
md5 = "0.7.0"
Expand All @@ -20,7 +20,6 @@ serde_json = "1.0"
delphinus-zkwasm = { path = "../zkwasm" }
delphinus-host = { path = "../host" }
anyhow.workspace = true
halo2aggregator-s.workspace = true
halo2_proofs.workspace = true
wasmi.workspace = true
circuits-batcher.workspace = true
Expand All @@ -30,4 +29,4 @@ default = []
perf = ["circuits-batcher/perf"]
cuda = ["delphinus-zkwasm/cuda"]
uniform-circuit = ["delphinus-zkwasm/uniform-circuit"]

profile = ["ark-std/print-trace"]
4 changes: 2 additions & 2 deletions crates/cli/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ pub trait AppBuilder: CommandBuilder {
wasm_binary,
(),
phantom_functions,
&output_dir,
&param_dir,
),
HostMode::STANDARD => exec_image_checksum::<StandardEnvBuilder>(
zkwasm_k,
wasm_binary,
HostEnvConfig::default(),
phantom_functions,
&output_dir,
&param_dir,
),
},

Expand Down
70 changes: 33 additions & 37 deletions crates/cli/src/exec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use circuits_batcher::args::HashType::Poseidon;
use circuits_batcher::args::OpenSchema;
use circuits_batcher::proof::ParamsCache;
use circuits_batcher::proof::ProofGenerationInfo;
use circuits_batcher::proof::ProofInfo;
Expand All @@ -8,12 +9,12 @@ use circuits_batcher::proof::ProvingKeyCache;
use delphinus_zkwasm::loader::ZkWasmLoader;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use halo2_proofs::pairing::bn256::Bn256;
use halo2_proofs::plonk::verify_proof_with_shplonk;
use halo2_proofs::plonk::SingleVerifier;
use halo2_proofs::pairing::bn256::G1Affine;
use halo2_proofs::poly::commitment::Params;
use halo2_proofs::poly::commitment::ParamsVerifier;
use halo2aggregator_s::circuits::utils::load_or_build_unsafe_params;
use halo2aggregator_s::transcript::poseidon::PoseidonRead;
use log::info;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::PathBuf;

Expand All @@ -29,22 +30,27 @@ pub fn exec_setup<Builder: HostEnvBuilder>(
) -> Result<()> {
info!("Setup Params and VerifyingKey");

macro_rules! prepare_params {
($k: expr) => {{
let params_path = &param_dir.join(format!("K{}.params", $k));
let prepare_params = |k: u32| {
let params_path = &param_dir.join(format!("K{}.params", k));

if params_path.exists() {
info!("Found Params with K = {} at {:?}", $k, params_path);
} else {
info!("Create Params with K = {} to {:?}", $k, params_path);
}
if params_path.exists() {
info!("Found Params with K = {} at {:?}", k, params_path);

load_or_build_unsafe_params::<Bn256>($k, Some(params_path))
}};
}
Ok::<_, io::Error>(Params::read(&mut File::open(params_path)?)?)
} else {
info!("Create Params with K = {} to {:?}", k, params_path);

let params = Params::<G1Affine>::unsafe_setup::<Bn256>(k);

let mut fd = std::fs::File::create(params_path)?;
params.write(&mut fd)?;

Ok(params)
}
};

let params = prepare_params!(zkwasm_k);
prepare_params!(aggregate_k);
let params = prepare_params(zkwasm_k)?;
prepare_params(aggregate_k)?;

// Setup ZkWasm Vkey
{
Expand Down Expand Up @@ -75,7 +81,7 @@ pub fn exec_image_checksum<Builder>(
wasm_binary: Vec<u8>,
hostenv: Builder::HostConfig,
phantom_functions: Vec<String>,
output_dir: &PathBuf,
params_dir: &PathBuf,
) -> Result<()>
where
Builder: HostEnvBuilder,
Expand All @@ -86,10 +92,9 @@ where
phantom_functions,
)?;

let params = load_or_build_unsafe_params::<Bn256>(
zkwasm_k,
Some(&output_dir.join(format!("K{}.params", zkwasm_k))),
);
let params = Params::read(&mut File::open(
&params_dir.join(format!("K{}.params", zkwasm_k)),
)?)?;

let checksum = loader.checksum(&params, hostenv)?;
assert_eq!(checksum.len(), 1);
Expand All @@ -98,7 +103,7 @@ where
println!("image checksum: {:?}", checksum);

let mut fd =
std::fs::File::create(&output_dir.join(format!("checksum.data",)).as_path()).unwrap();
std::fs::File::create(&params_dir.join(format!("checksum.data",)).as_path()).unwrap();

write!(fd, "{:?}", checksum)?;

Expand Down Expand Up @@ -170,7 +175,7 @@ pub fn exec_create_proof<Builder: HostEnvBuilder>(
&mut pkey_cache,
&mut param_cache,
circuits_batcher::args::HashType::Poseidon,
circuits_batcher::args::OpenSchema::GWC,
circuits_batcher::args::OpenSchema::Shplonk,
);

prover.save_proof_data(&vec![instances], &transcript, output_dir);
Expand All @@ -193,10 +198,9 @@ pub fn exec_verify_proof(
let proofloadinfo = ProofGenerationInfo::load(&load_info);
let proofs: Vec<ProofInfo<Bn256>> =
ProofInfo::load_proof(&output_dir, &param_dir, &proofloadinfo);
let params = load_or_build_unsafe_params::<Bn256>(
proofloadinfo.k as u32,
Some(&param_dir.join(format!("K{}.params", proofloadinfo.k))),
);
let params = Params::read(&mut File::open(
&param_dir.join(format!("K{}.params", proofloadinfo.k)),
)?)?;
let mut public_inputs_size = 0;
for proof in proofs.iter() {
public_inputs_size = usize::max(
Expand All @@ -210,15 +214,7 @@ pub fn exec_verify_proof(

let params_verifier: ParamsVerifier<Bn256> = params.verifier(public_inputs_size).unwrap();
for (_, proof) in proofs.into_iter().enumerate() {
let strategy = SingleVerifier::new(&params_verifier);
verify_proof_with_shplonk::<Bn256, _, _, _>(
&params_verifier,
&proof.vkey,
strategy,
&[&proof.instances.iter().map(|x| &x[..]).collect::<Vec<_>>()[..]],
&mut PoseidonRead::init(&proof.transcripts[..]),
)
.unwrap();
proof.verify_proof(&params_verifier, OpenSchema::GWC)?;
}
info!("Verifing proof passed");

Expand Down
8 changes: 4 additions & 4 deletions crates/cli/test_cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ rm -rf params/*.data
rm -rf output/*.data

# Single test
RUST_LOG=info cargo run --release --features cuda -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm setup
RUST_LOG=info cargo run --release --features cuda -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm checksum
RUST_LOG=info cargo run --release --features cuda -- --host default -k 22 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm setup
RUST_LOG=info cargo run --release --features cuda -- --host default -k 22 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm checksum

RUST_LOG=info cargo run --release --features cuda -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-prove --public 133:i64 --public 2:i64
RUST_LOG=info cargo run --release --features cuda -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-verify
RUST_LOG=info cargo run --release --features cuda -- --host default -k 22 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-prove --public 133:i64 --public 2:i64
RUST_LOG=info cargo run --release --features cuda -- --host default -k 22 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-verify
4 changes: 2 additions & 2 deletions crates/host/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]
ark-std = { version = "0.3.0", features = ["print-trace"] }
ark-std = { version = "0.4.0"}
bitvec = "1.0.1"
downcast-rs = "1.2.0"
hex = "0.4.3"
Expand All @@ -29,7 +29,6 @@ sha2 = "0.10.6"
poseidon = { git = "https://github.com/DelphinusLab/poseidon" }

anyhow.workspace = true
halo2aggregator-s.workspace = true
halo2_proofs.workspace = true
parity-wasm.workspace = true
wasmi.workspace = true
Expand All @@ -43,3 +42,4 @@ rusty-fork = "0.3.0"
[features]
default = []
cuda = ["halo2_proofs/cuda", "specs/cuda"]
profile = ["ark-std/print-trace"]
10 changes: 5 additions & 5 deletions crates/host/src/host/merkle_helper/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ impl MerkleContext {
.mongo_merkle
.as_ref()
.expect("merkle db not initialized");
let (leaf, _) = mt
.get_leaf_with_proof(index)
.expect("Unexpected failure: get leaf fail");
let values = leaf.data_as_u64();
if self.data_cursor == 0 {
let (leaf, _) = mt
.get_leaf_with_proof(index)
.expect("Unexpected failure: get leaf fail");
let values = leaf.data_as_u64();
self.data = values;
}
let v = values[self.data_cursor];
let v = self.data[self.data_cursor];
self.data_cursor += 1;
return v;
}
Expand Down
1 change: 0 additions & 1 deletion crates/playground/Cargo.lock

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

Loading
Loading