From 26ea38ef122bbbcfe48abcbb56f2beaf016085c3 Mon Sep 17 00:00:00 2001 From: Sinka Date: Wed, 14 Aug 2024 17:37:07 +1000 Subject: [PATCH] support host slice --- Cargo.lock | 144 ++++++++++++++----- Cargo.toml | 5 +- crates/host/src/host/merkle_helper/merkle.rs | 2 +- crates/host/src/lib.rs | 84 +++++++++-- crates/zkwasm/src/runtime/host/mod.rs | 2 +- 5 files changed, 180 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 95dbb2706..6618941bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,22 +62,65 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.81" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" dependencies = [ "backtrace", ] [[package]] -name = "ark-std" -version = "0.3.0" +name = "ark-ff" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1df2c09229cbc5a028b1d70e00fdb2acee28b1055dfb5ca73eea49c5a25c4e7c" +checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" dependencies = [ - "colored", + "ark-ff-asm", + "ark-ff-macros", + "ark-serialize", + "ark-std", + "derivative", + "digest", + "itertools", + "num-bigint", "num-traits", - "rand", + "paste", + "rustc_version 0.4.0", + "zeroize", +] + +[[package]] +name = "ark-ff-asm" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed4aa4fe255d0bc6d79373f7e31d2ea147bcf486cba1be5ba7ea85abdb92348" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-ff-macros" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abe79b0e4288889c4574159ab790824d0033b9fdcb2a112a3182fac2e514565" +dependencies = [ + "num-bigint", + "num-traits", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-serialize" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" +dependencies = [ + "ark-std", + "digest", + "num-bigint", ] [[package]] @@ -354,10 +397,10 @@ dependencies = [ [[package]] name = "circuits-batcher" version = "0.1.0" -source = "git+https://github.com/DelphinusLab/continuation-batcher.git#12cefe8364f44c89afeb1f28fae701aeb90d06a0" +source = "git+https://github.com/DelphinusLab/continuation-batcher.git?tag=on-prove-pairing-2.0#feb05d624504bbdd22834c5580519f5c2615d8fc" dependencies = [ "anyhow", - "ark-std 0.4.0", + "ark-std", "bitvec", "clap", "downcast-rs", @@ -627,7 +670,7 @@ name = "delphinus-host" version = "0.1.0" dependencies = [ "anyhow", - "ark-std 0.4.0", + "ark-std", "bitvec", "delphinus-zkwasm", "downcast-rs", @@ -659,7 +702,7 @@ name = "delphinus-zkwasm" version = "0.1.0" dependencies = [ "anyhow", - "ark-std 0.4.0", + "ark-std", "bitvec", "cfg-if 1.0.0", "downcast-rs", @@ -774,7 +817,7 @@ name = "ec-gpu-gen" version = "0.4.0" source = "git+https://github.com/lanbones/ec-gpu?branch=halo2-opt-v2#aedc591657644f047f25a2e089f0b489eba0bf38" dependencies = [ - "ark-std 0.4.0", + "ark-std", "bitvec", "crossbeam-channel", "ec-gpu", @@ -1104,9 +1147,9 @@ dependencies = [ [[package]] name = "halo2_proofs" version = "0.1.0-beta.1" -source = "git+https://github.com/DelphinusLab/halo2-gpu-specific.git#fe464bd14d7d1155964d39a029c9903b453aeac8" +source = "git+https://github.com/DelphinusLab/halo2-gpu-specific.git#12655ca7406a8c21a154bfd36dd2f8e520c569b1" dependencies = [ - "ark-std 0.4.0", + "ark-std", "blake2b_simd", "bumpalo", "cfg-if 0.1.10", @@ -1133,16 +1176,20 @@ dependencies = [ [[package]] name = "halo2aggregator-s" version = "1.0.2" -source = "git+https://github.com/DelphinusLab/halo2aggregator-s.git?tag=1.0.3#79bb1d5c491af63052c292713aa27e54a9432203" +source = "git+https://github.com/DelphinusLab/halo2aggregator-s.git?tag=on-prove-pairing-0.4.0#555bfa360e49a9da5a642426c125180e2d9ca7ed" dependencies = [ - "ark-std 0.4.0", + "ark-ff", + "ark-std", "blake2b_simd", + "ff", "halo2_proofs", - "halo2ecc-s 0.3.2 (git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.5.8)", + "halo2ecc-s", "lazy_static", "num-bigint", "num-integer", + "num-traits", "poseidon", + "rand_core", "sha2", "sha3", "tera", @@ -1151,24 +1198,13 @@ dependencies = [ [[package]] name = "halo2ecc-s" version = "0.3.2" -source = "git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.5.6#3b8b424f#3b8b424f009fd945fc982f530ed29b555f8002e6" +source = "git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=on-prove-pairing-0.6.0#713b34d76625e127156309d3f3847a483d57fa56" dependencies = [ - "ark-std 0.4.0", - "halo2_proofs", - "num-bigint", - "num-integer", - "rayon", -] - -[[package]] -name = "halo2ecc-s" -version = "0.3.2" -source = "git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.5.8#ddad2af04c28c33c2aa1192f4529d59abdf624b8" -dependencies = [ - "ark-std 0.4.0", + "ark-std", "halo2_proofs", "num-bigint", "num-integer", + "num-traits", "rayon", ] @@ -1702,11 +1738,10 @@ dependencies = [ [[package]] name = "num-bigint" -version = "0.4.4" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" dependencies = [ - "autocfg", "num-integer", "num-traits", "rand", @@ -1829,12 +1864,14 @@ dependencies = [ [[package]] name = "pairing_bn256" version = "0.1.1" -source = "git+https://github.com/lanbones/pairing#5ab08062f53cd29c946f96572aefdb688952d725" +source = "git+https://github.com/lanbones/pairing#30b052f29d7ec3e68e20528584d9495de678ea05" dependencies = [ "bls12_381", "ec-gpu", "ff", "group", + "num-bigint", + "num-traits", "rand", "rand_core", "static_assertions", @@ -1879,6 +1916,12 @@ dependencies = [ "regex", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pbkdf2" version = "0.11.0" @@ -3437,6 +3480,26 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.52", +] + [[package]] name = "zkwasm-cli" version = "0.1.0" @@ -3466,21 +3529,24 @@ dependencies = [ [[package]] name = "zkwasm-host-circuits" version = "0.1.0" -source = "git+https://github.com/DelphinusLab/zkWasm-host-circuits.git#924a0088929e4aa323827e74ae2bfe45fdf1c5b9" +source = "git+https://github.com/DelphinusLab/zkWasm-host-circuits.git?branch=host-op-1.8#ed9ca0d33b6146e88280b1ef4d67283f1de1f919" dependencies = [ - "ark-std 0.3.0", + "anyhow", + "ark-std", "cfg-if 1.0.0", "circuits-batcher", "clap", "ff", "halo2_proofs", - "halo2ecc-s 0.3.2 (git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.5.6#3b8b424f)", + "halo2ecc-s", "hex", "itertools", "lazy_static", "lru", "mongodb", "num-bigint", + "num-derive", + "num-traits", "poseidon", "rand", "rand_core", @@ -3497,7 +3563,7 @@ name = "zkwasm-prover" version = "0.1.0" source = "git+https://github.com/DelphinusLab/zkWasm-prover.git#d7244c637ca7d64afee4e66caea6f0cf4ccc6cc5" dependencies = [ - "ark-std 0.4.0", + "ark-std", "cc", "cuda-runtime-sys", "halo2_proofs", diff --git a/Cargo.toml b/Cargo.toml index 9daa25d25..fbb6cd334 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,10 +14,9 @@ rayon = "1.8.0" regex = "1.10.2" static_assertions = "1.1.0" wasmi = { path = "third-party/wasmi" } -zkwasm-host-circuits = { git = "https://github.com/DelphinusLab/zkWasm-host-circuits.git" } -circuits-batcher = { git = "https://github.com/DelphinusLab/continuation-batcher.git" } +zkwasm-host-circuits = { git = "https://github.com/DelphinusLab/zkWasm-host-circuits.git", branch="host-op-1.8" } +circuits-batcher = { git = "https://github.com/DelphinusLab/continuation-batcher.git", tag="on-prove-pairing-2.0" } poseidon = { git = "https://github.com/DelphinusLab/poseidon" } [profile.dev] opt-level = 3 - diff --git a/crates/host/src/host/merkle_helper/merkle.rs b/crates/host/src/host/merkle_helper/merkle.rs index b27e4111d..661012d17 100644 --- a/crates/host/src/host/merkle_helper/merkle.rs +++ b/crates/host/src/host/merkle_helper/merkle.rs @@ -18,7 +18,7 @@ use zkwasm_host_circuits::host::ForeignInst::MerkleSetRoot; use zkwasm_host_circuits::host::Reduce; use zkwasm_host_circuits::host::ReduceRule; -const MERKLE_TREE_HEIGHT: usize = 32; +use crate::MERKLE_TREE_HEIGHT; pub struct MerkleContext { pub k: u32, diff --git a/crates/host/src/lib.rs b/crates/host/src/lib.rs index ad6270401..ac87b3144 100644 --- a/crates/host/src/lib.rs +++ b/crates/host/src/lib.rs @@ -1,6 +1,7 @@ #![deny(warnings)] pub mod host; +use num_traits::FromPrimitive; use std::cell::RefCell; use std::rc::Rc; @@ -15,10 +16,16 @@ use delphinus_zkwasm::runtime::host::HostEnvBuilder; use delphinus_zkwasm::runtime::monitor::plugins::table::Command; use delphinus_zkwasm::runtime::monitor::plugins::table::Event; use delphinus_zkwasm::runtime::monitor::plugins::table::FlushStrategy; +use halo2_proofs::pairing::bn256::Fr; use serde::Deserialize; use serde::Serialize; use std::collections::HashMap; +use zkwasm_host_circuits::circuits::babyjub::AltJubChip; +use zkwasm_host_circuits::circuits::host::HostOpSelector; +use zkwasm_host_circuits::circuits::merkle::MerkleChip; +use zkwasm_host_circuits::circuits::poseidon::PoseidonChip; use zkwasm_host_circuits::host::db::TreeDB; +use zkwasm_host_circuits::host::ForeignInst; use zkwasm_host_circuits::proof::OpType; #[derive(Serialize, Deserialize, Debug)] @@ -26,6 +33,8 @@ pub struct HostEnvConfig { pub ops: Vec, } +pub const MERKLE_TREE_HEIGHT: usize = 32; + impl HostEnvConfig { fn register_op(op: &OpType, env: &mut HostEnv, tree_db: Option>>) { match op { @@ -54,6 +63,32 @@ pub struct StandardHostEnvBuilder { ops: Vec, } +trait GroupedForeign { + fn get_optype(&self) -> Option; +} + +impl GroupedForeign for ForeignInst { + fn get_optype(&self) -> Option { + match self { + ForeignInst::MerkleSet => Some(OpType::MERKLE), + ForeignInst::MerkleGet => Some(OpType::MERKLE), + ForeignInst::MerkleSetRoot => Some(OpType::MERKLE), + ForeignInst::MerkleGetRoot => Some(OpType::MERKLE), + ForeignInst::MerkleAddress => Some(OpType::MERKLE), + + ForeignInst::PoseidonPush => Some(OpType::POSEIDONHASH), + ForeignInst::PoseidonNew => Some(OpType::POSEIDONHASH), + ForeignInst::PoseidonFinalize => Some(OpType::POSEIDONHASH), + + ForeignInst::JubjubSumNew => Some(OpType::JUBJUBSUM), + ForeignInst::JubjubSumPush => Some(OpType::JUBJUBSUM), + ForeignInst::JubjubSumResult => Some(OpType::JUBJUBSUM), + + _ => None, + } + } +} + impl Default for StandardHostEnvBuilder { fn default() -> Self { Self { @@ -71,32 +106,55 @@ impl Default for StandardHostEnvBuilder { #[derive(Default)] struct StandardHostEnvFlushStrategy { k: u32, - ops: HashMap, + ops: HashMap, +} + +fn get_group_size(optype: &OpType) -> usize { + match optype { + OpType::MERKLE => 1 + 4 + 4 + 4, // address + set_root + get/set + get_root + OpType::JUBJUBSUM => 1 + 4 + 8 + 8, // new + scalar + point + result point + OpType::POSEIDONHASH => 1 + 4 * 8 + 4, // new + push + result + _ => unreachable!(), + } +} + +fn get_max_bound(optype: &OpType, k: usize) -> usize { + match optype { + OpType::MERKLE => MerkleChip::::max_rounds(k as usize), + OpType::JUBJUBSUM => AltJubChip::::max_rounds(k as usize), + OpType::POSEIDONHASH => PoseidonChip::max_rounds(k as usize), + _ => unreachable!(), + } } impl FlushStrategy for StandardHostEnvFlushStrategy { #[allow(unreachable_code)] fn notify(&mut self, op: Event) -> Command { - let _k = self.k; match op { Event::HostCall(op) => { - let count = self.ops.entry(op).or_insert(0); - *count += 1; - let _plugin_id = todo!("op to plugin id"); - - if todo!("host table is full to accommodate more ops") { - Command::Abort - } else if todo!("an ops block is met") { - Command::Start(_plugin_id) - } else if todo!("finish a block") { - Command::Commit(_plugin_id) + let op_type = ForeignInst::from_usize(op).unwrap().get_optype(); + if let Some(optype) = op_type { + let (count, total) = self.ops.entry(optype.clone() as usize).or_insert((0, 0)); + if *total >= get_max_bound(&optype, self.k as usize) { + Command::Abort + } else if *count == 0 { + Command::Start(optype as usize) + } else { + *count += 1; + if *count == get_group_size(&optype) { + *total += 1; + *count = 0; + Command::Commit(optype as usize) + } else { + Command::Noop + } + } } else { Command::Noop } } Event::Reset => { self.ops.clear(); - todo!("return Err if including uncommitted entries, otherwise reset self"); Command::Noop } } diff --git a/crates/zkwasm/src/runtime/host/mod.rs b/crates/zkwasm/src/runtime/host/mod.rs index 88da0cb4e..ec81877da 100644 --- a/crates/zkwasm/src/runtime/host/mod.rs +++ b/crates/zkwasm/src/runtime/host/mod.rs @@ -104,5 +104,5 @@ pub trait HostEnvBuilder { /// Create an env with execution parameters, this is used by dry-run, run fn create_env(&self, k: u32, env: ExecutionArg) -> HostEnv; // Create a flush strategy to hint the monitor when to flush the table - fn create_flush_strategy(&self, k:u32) -> Box; + fn create_flush_strategy(&self, k: u32) -> Box; }