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: STARK to SNARK conversion with Bonsai #86

Merged
merged 201 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
201 commits
Select commit Hold shift + click to select a range
7b08224
transactions module
hashcashier Aug 31, 2023
07c0335
TxEssence trait
hashcashier Aug 31, 2023
ff74eeb
Generic Transaction struct
hashcashier Aug 31, 2023
7fd240c
tx essence as a generic parameter
hashcashier Aug 31, 2023
cb1b7d5
OptimismTxEssence
hashcashier Aug 31, 2023
a3d826b
op chain spec
hashcashier Aug 31, 2023
c27cfcf
OpTxExecStrategy
hashcashier Aug 31, 2023
133e881
Merge branch 'main' of github.com:risc0/zeth into rkhalil/op
hashcashier Aug 31, 2023
b261dbf
redundant block builder type params
hashcashier Sep 5, 2023
3dc1629
strategy bundles
hashcashier Sep 5, 2023
ca30106
host binary parameters
hashcashier Sep 5, 2023
7e5ce52
profiling flag
hashcashier Sep 6, 2023
b267f3f
optimism
hashcashier Sep 6, 2023
16db242
new derivation binary skeleton, copy over libs
hashcashier Sep 6, 2023
dc4a251
providers and conversion utils
hashcashier Sep 7, 2023
2fa1919
epoch transitioning
hashcashier Sep 7, 2023
2ca612d
basic derive flow
hashcashier Sep 8, 2023
2482e7d
host-side derivation
hashcashier Sep 8, 2023
b67f8ba
read metadata from op head
hashcashier Sep 8, 2023
c8fdfb3
op-derive guest
hashcashier Sep 8, 2023
2b1df5c
disable guest memory leaks
hashcashier Sep 8, 2023
25b97c0
heapless batch derivation
hashcashier Sep 11, 2023
4839400
Merge branch 'main' of github.com:risc0/zeth into rkhalil/op-derive
hashcashier Sep 19, 2023
912786d
Pull from main
intoverflow Oct 16, 2023
f2c8076
Merge with main
intoverflow Oct 23, 2023
dd471d5
Add missing import
intoverflow Oct 23, 2023
187cebc
Remove heapless BinaryHeap
intoverflow Oct 27, 2023
33df195
Remove heapless
intoverflow Oct 27, 2023
4805a4b
Merge from main
intoverflow Oct 31, 2023
83f482d
Merge branch 'rkhalil/op-derive' of github.com:risc0/zeth into tcarst…
intoverflow Oct 31, 2023
5f958a7
Introduce op-derive tool
intoverflow Oct 31, 2023
a479a33
Remove ethers types from BatcherDb trait
intoverflow Nov 2, 2023
531a5e3
Verify new op block has correct transaction list
intoverflow Nov 2, 2023
1f94958
Move derive logic into library
intoverflow Nov 2, 2023
5aeb237
Fix bug in transaction trie reconstruction
intoverflow Nov 2, 2023
3842e72
Introduce get_op_header() to BatcherDb
intoverflow Nov 2, 2023
c6c02a2
Merge with main
intoverflow Nov 20, 2023
973c451
Clippy warning
intoverflow Nov 20, 2023
41d88a7
Default Serde value for FileProvider::receipts
intoverflow Nov 21, 2023
4a90227
Disable bloom filter checks
intoverflow Nov 21, 2023
7aa11fd
Clippy
intoverflow Nov 21, 2023
b541602
Clippy
intoverflow Nov 21, 2023
151b4e8
Fix parsing of from and to fields for deposits
intoverflow Nov 21, 2023
782c1ab
Merge branch 'main' of github.com:risc0/zeth into tcarstens/op-derive
intoverflow Nov 22, 2023
394f5ea
Reintroduce filtering by log bloom
intoverflow Nov 22, 2023
13254a9
Merge branch 'main' of github.com:risc0/zeth into tcarstens/op-derive
hashcashier Nov 27, 2023
8516a7d
fmt
hashcashier Nov 27, 2023
76402a3
Add support for local exec to op-derive
intoverflow Nov 27, 2023
a7cc496
Enforce block_number is correct in MemDb
intoverflow Nov 28, 2023
6a5435b
Split derive() into multiple functions
intoverflow Nov 30, 2023
7fbcce1
Remove redundant check for batch parent hash
intoverflow Nov 30, 2023
1e9441e
Remove redundant copy of system config
intoverflow Nov 30, 2023
fc696d2
Remove redundant block number check
intoverflow Nov 30, 2023
d9573ca
Cleanup
intoverflow Nov 30, 2023
a8748f6
Remove redundant vector of eth blocks
intoverflow Nov 30, 2023
65d04d8
Add base_fee_per_gas to Epoch
intoverflow Nov 30, 2023
1591bf1
Store deposits in Epoch
intoverflow Nov 30, 2023
b9a5eb6
Add Eth tail to DeriveOutput
intoverflow Nov 30, 2023
b843875
Cleanup
intoverflow Nov 30, 2023
7ed041f
Move deque_next_epoch_if_none to State
intoverflow Nov 30, 2023
944e6b0
Move eth block processing to Batches
intoverflow Nov 30, 2023
fe1c95e
Pull from main
intoverflow Dec 12, 2023
ab6aa20
update zkvm, basic guest
hashcashier Dec 13, 2023
e38c1f1
more compose guest code, comment bonsai code
hashcashier Dec 13, 2023
f571159
untested composition draft guest code
hashcashier Dec 13, 2023
f24a477
Reorg and cleanup
intoverflow Dec 14, 2023
f25b30e
More cleanup
intoverflow Dec 14, 2023
76c98f6
run cargo fmt --all
Wollac Dec 14, 2023
f633fe0
fix clippy warnings
Wollac Dec 14, 2023
e2badf2
Merge branch 'main' into tcarstens/op-derive
Wollac Dec 14, 2023
a480a14
remove unused imports
Wollac Dec 14, 2023
a33f842
basic prep/left/fin in-memory flow
hashcashier Dec 14, 2023
5540c1e
in memory aggregation workflow
hashcashier Dec 14, 2023
6d7364c
format guest code
Wollac Dec 14, 2023
acd5573
composition with receipts
hashcashier Dec 14, 2023
14ffa70
Merge branch 'tcarstens/op-derive' of github.com:risc0/zeth into rkha…
hashcashier Dec 14, 2023
1538b26
add op-derive cmd test
Wollac Dec 15, 2023
0e08feb
add cmd tests
Wollac Dec 15, 2023
0cbb413
cleanup optimism/mod
Wollac Dec 15, 2023
2b0ada8
Rename command line args
intoverflow Dec 15, 2023
3b166b5
Fix test arguments
intoverflow Dec 15, 2023
bc1ec04
Rename config field to max_channel_bank_size
intoverflow Dec 15, 2023
2ab54b2
Enforce decompression limit of MAX_RLP_BYTES_PER_CHANNEL
intoverflow Dec 15, 2023
2d007dd
Use constant OPTIMISM_DEPOSITED_TX_TYPE when checking batch validity
intoverflow Dec 15, 2023
c81dd60
Import from std instead of alloc/core
intoverflow Dec 15, 2023
2f6a865
Re-enable core::mem::forget() optimization
intoverflow Dec 15, 2023
707b89b
Replace asserts with ensures; enforce absence of receipts for Op blocks
intoverflow Dec 15, 2023
098d82d
Simplfy iteration through derived transactions
intoverflow Dec 15, 2023
dae216a
Add Bonsai support to op-derive. Also add Bonsai session status to ou…
intoverflow Dec 15, 2023
040aa3a
More println
intoverflow Dec 15, 2023
2fd7c89
Rework Batcher initialization
intoverflow Dec 15, 2023
a08b13d
More logging
intoverflow Dec 16, 2023
2b7186a
More log output if Bonsai workflow fails
intoverflow Dec 16, 2023
cb2ddcb
Merge branch 'tcarstens/op-derive' of github.com:risc0/zeth into rkha…
hashcashier Dec 18, 2023
3435c2a
Merge branch 'main' of github.com:risc0/zeth into rkhalil/op-compose
hashcashier Dec 18, 2023
0240472
refactor rpc db
hashcashier Dec 18, 2023
7de2483
variable derive step support
hashcashier Dec 19, 2023
64240f2
re-enable profiling
hashcashier Dec 19, 2023
3b314ab
ignore rpc_cache dir
hashcashier Dec 19, 2023
8595dda
add -profile support to compose binary
hashcashier Dec 19, 2023
facbd33
upgrade zkvm, modify code comments
hashcashier Dec 20, 2023
83e1d8f
Merge branch 'main' of github.com:risc0/zeth into rkhalil/op-compose
hashcashier Dec 20, 2023
7c2470a
Merge branch 'main' of github.com:risc0/zeth into rkhalil/op-compose
hashcashier Jan 8, 2024
61e5847
initial monolith
hashcashier Jan 8, 2024
fa9c279
refactor prompt style
hashcashier Jan 9, 2024
5425737
op-info support
hashcashier Jan 9, 2024
c8dad6b
delete old host binaries
hashcashier Jan 9, 2024
be6f732
nits
hashcashier Jan 10, 2024
9af3aa8
fix tests
hashcashier Jan 10, 2024
451de67
help nit
hashcashier Jan 10, 2024
786be8a
short params
hashcashier Jan 10, 2024
93d0e0b
bump zkvm, fix CI
hashcashier Jan 11, 2024
74e4c64
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Jan 11, 2024
8b036ef
clippies
hashcashier Jan 11, 2024
451de61
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Jan 11, 2024
08eadac
save receipts
hashcashier Jan 12, 2024
abbc3c5
refactor
hashcashier Jan 12, 2024
a926380
move clap structs to cli module
hashcashier Jan 12, 2024
3a72eae
refactor
hashcashier Jan 12, 2024
bdf9521
refactor main monolith
hashcashier Jan 12, 2024
ce5aac1
fixes and changes
hashcashier Jan 16, 2024
9eb70d9
vs code change
hashcashier Jan 16, 2024
df5b43b
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Jan 16, 2024
0b6a455
default cache folder nit
hashcashier Jan 17, 2024
dc98f83
build after derive
hashcashier Jan 19, 2024
5422ad5
disambiguate merkle ranges and proofs
hashcashier Jan 19, 2024
b1a1436
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Jan 19, 2024
b77f900
Merge branch 'rkhalil/tidy-host' of github.com:risc0/zeth into rkhali…
hashcashier Jan 19, 2024
bdf939f
missing cache files for test
hashcashier Jan 19, 2024
54b5ee8
update crates
hashcashier Jan 22, 2024
06fcb9a
prove on bonsai
hashcashier Jan 22, 2024
abab8e9
verify bonsai receipts
hashcashier Jan 22, 2024
48320be
readme,cache,cuda,no-dev-mode,receipt uuid output, etc..
hashcashier Jan 22, 2024
15d3ea9
logging nit
hashcashier Jan 23, 2024
ecd68ff
Merge branch 'main' of github.com:risc0/zeth into rkhalil/op-compose
hashcashier Jan 23, 2024
4c18859
Merge branch 'main' of github.com:risc0/zeth into rkhalil/op-compose
hashcashier Jan 23, 2024
d5b4b1e
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Jan 23, 2024
cbc65a1
Merge branch 'rkhalil/tidy-host' of github.com:risc0/zeth into rkhali…
hashcashier Jan 23, 2024
7069754
rlp decoding transactions
hashcashier Jan 23, 2024
e96c68f
provably bad blocks
hashcashier Jan 24, 2024
f3cb39e
build on derive
hashcashier Jan 24, 2024
8a7c1f6
derivation test fix
hashcashier Jan 24, 2024
eda3d11
support erroneous preflights
hashcashier Jan 25, 2024
bc125d3
host: derive with receipts
hashcashier Jan 25, 2024
1665e74
fix op head
hashcashier Jan 25, 2024
d6ee0db
Merge branch 'rkhalil/derive-tx-exec' of github.com:risc0/zeth into r…
hashcashier Jan 26, 2024
3ef9e55
verification nit
hashcashier Jan 26, 2024
ce01345
Merge branch 'rkhalil/derive-tx-exec' of github.com:risc0/zeth into r…
hashcashier Jan 26, 2024
fd56298
repeat bonsai requests until success
hashcashier Jan 26, 2024
d92882d
wider bonsai loop
hashcashier Jan 26, 2024
df84324
switch to async bonsai
hashcashier Jan 27, 2024
4e46903
Merge branch 'rkhalil/bonsai-composition' of github.com:risc0/zeth in…
hashcashier Jan 27, 2024
c8cf68c
update readme
hashcashier Jan 29, 2024
dd70e6d
patch blocking code
hashcashier Jan 29, 2024
b3b6b38
patch blocking code
hashcashier Jan 29, 2024
9053a3e
Merge branch 'rkhalil/derive-tx-exec' of github.com:risc0/zeth into r…
hashcashier Jan 29, 2024
a419751
Merge branch 'rkhalil/bonsai-composition' of github.com:risc0/zeth in…
hashcashier Jan 29, 2024
285e05d
rpc_cache -> cache_rpc
hashcashier Jan 29, 2024
2947a98
det build, receipt cache, info->debug
hashcashier Jan 29, 2024
2074c13
fix nondet receipt label
hashcashier Jan 29, 2024
d477739
logging and TransactionKind rlp decode fix
hashcashier Jan 30, 2024
da933fe
update risc0 to release v0.20
hashcashier Jan 30, 2024
707be50
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Jan 30, 2024
bf4cb50
Merge branch 'rkhalil/tidy-host' of github.com:risc0/zeth into rkhali…
hashcashier Jan 30, 2024
148a089
cargo locks
hashcashier Jan 30, 2024
b45d5bf
Merge branch 'rkhalil/derive-tx-exec' of github.com:risc0/zeth into r…
hashcashier Jan 30, 2024
ecc2428
Merge branch 'rkhalil/bonsai-composition' of github.com:risc0/zeth in…
hashcashier Jan 30, 2024
9938491
nit
hashcashier Jan 30, 2024
68b3b74
move more info output to trace
hashcashier Jan 31, 2024
563c5bf
update GH action to 0.20
Wollac Jan 31, 2024
8349be3
use old actions
Wollac Jan 31, 2024
01bce1d
stark2snark
hashcashier Feb 1, 2024
d5efdc1
changes
hashcashier Feb 2, 2024
07dc903
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Feb 2, 2024
1bb1942
Merge branch 'rkhalil/tidy-host' of github.com:risc0/zeth into rkhali…
hashcashier Feb 2, 2024
9fa10e9
refactor rlp & comments
hashcashier Feb 2, 2024
072e39f
Merge branch 'rkhalil/derive-tx-exec' of github.com:risc0/zeth into r…
hashcashier Feb 2, 2024
699116f
Merge branch 'rkhalil/bonsai-composition' of github.com:risc0/zeth in…
hashcashier Feb 2, 2024
ece1be6
Merge branch 'rkhalil/polish' of github.com:risc0/zeth into rkhalil/s…
hashcashier Feb 2, 2024
ed39709
update to v20.1
hashcashier Feb 2, 2024
d2ab2f6
refactor: Unified zeth utility (#72)
hashcashier Feb 5, 2024
9a58965
cleanup toml files
Wollac Feb 5, 2024
0f58854
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Feb 5, 2024
573a2a1
remove unused imports
Wollac Feb 5, 2024
076e3bd
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Feb 5, 2024
d09b3cf
fmt
hashcashier Feb 5, 2024
7789915
SNARK Verification via RPC call
hashcashier Feb 6, 2024
1358376
cli cleanup
Wollac Feb 6, 2024
1147152
change block count to u32
Wollac Feb 7, 2024
6bf0eee
test composition
Wollac Feb 7, 2024
90b5ea0
fix arguments
Wollac Feb 7, 2024
4e8640f
update risc0 to 0.20.1
Wollac Feb 7, 2024
90c9fe8
compact json files
Wollac Feb 7, 2024
3fb1f45
update lock files
Wollac Feb 7, 2024
24905a7
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Feb 9, 2024
b48f089
test for warnings
Wollac Feb 9, 2024
7f44cb2
nits
hashcashier Feb 12, 2024
3f7905a
docker in readme
hashcashier Feb 12, 2024
f75538f
Merge branch 'rkhalil/op-compose' of github.com:risc0/zeth into rkhal…
hashcashier Feb 12, 2024
7f7f122
Merge branch 'main' of github.com:risc0/zeth into rkhalil/stark2snark
hashcashier Feb 12, 2024
a845b9c
remove env unset code because #91
hashcashier Feb 12, 2024
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
515 changes: 305 additions & 210 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ edition = "2021"
default-run = "zeth"

[dependencies]
alloy-primitives = { version = "0.6.2", default-features = false, features = [
"rlp",
"serde",
"std",
] }
alloy-sol-types = "0.6"
anyhow = "1.0"
bincode = "1.3.3"
bonsai-sdk = { workspace = true }
bytemuck = "1.13"
clap = { version = "4.0", features = ["derive"] }
env_logger = "0.11"
ethers-contract = { version = "2.0", features = ["optimism"] }
ethers-core = { version = "2.0", features = ["optimism"] }
ethers-providers = { version = "2.0", features = ["optimism"] }
hex = "0.4.3"
log = "0.4"
risc0-zkvm = { workspace = true, features = ["prove"] }
Expand Down
52 changes: 52 additions & 0 deletions host/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,39 @@ impl Cli {
Cli::Verify(..) => unimplemented!(),
}
}

pub fn submit_to_bonsai(&self) -> bool {
if let Cli::Prove(prove_args) = self {
prove_args.submit_to_bonsai
} else {
false
}
}

pub fn snark(&self) -> bool {
if let Cli::Prove(prove_args) = self {
prove_args.snark_args.snark
} else {
false
}
}

pub fn verifier_or_eth_rpc_url(&self) -> Option<String> {
let verifier_rpc_url = if let Cli::Prove(prove_args) = self {
prove_args.snark_args.verifier_rpc_url.clone()
} else {
None
};
verifier_rpc_url.or(self.build_args().eth_rpc_url.clone())
}

pub fn verifier_contract(&self) -> Option<String> {
if let Cli::Prove(prove_args) = self {
prove_args.snark_args.verifier_contract.clone()
} else {
None
}
}
}

#[derive(Debug, Clone, clap::ValueEnum)]
Expand Down Expand Up @@ -160,6 +193,25 @@ pub struct ProveArgs {
#[clap(short, long, default_value_t = false)]
/// Prove remotely using Bonsai
pub submit_to_bonsai: bool,

#[clap(flatten)]
pub snark_args: SnarkArgs,
}

#[derive(clap::Args, Debug, Clone)]
pub struct SnarkArgs {
/// Convert the resulting STARK receipt into a Groth-16 SNARK using Bonsai
#[clap(short, long, default_value_t = false)]
pub snark: bool,

#[clap(short, long, require_equals = true)]
/// URL of the Ethereum RPC node for SNARK verification.
pub verifier_rpc_url: Option<String>,

#[clap(short, long, require_equals = true)]
/// Address of the RiscZeroGroth16Verifier contract. Requires `eth_rpc_url` or
/// `verifier_rpc_url` to be set.
pub verifier_contract: Option<String>,
}

impl Tag for ProveArgs {
Expand Down
8 changes: 5 additions & 3 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

use std::{fs, path::Path};

use risc0_zkvm::{is_dev_mode, Receipt};
use risc0_zkvm::is_dev_mode;
use tracing::debug;

pub mod cli;
pub mod operations;

pub fn load_receipt(file_name: &String) -> anyhow::Result<Option<(String, Receipt)>> {
pub fn load_receipt<T: serde::de::DeserializeOwned>(
file_name: &String,
) -> anyhow::Result<Option<(String, T)>> {
if is_dev_mode() {
// Nothing to load
return Ok(None);
Expand All @@ -38,7 +40,7 @@ pub fn load_receipt(file_name: &String) -> anyhow::Result<Option<(String, Receip
Ok(Some(bincode::deserialize(&receipt_serialized)?))
}

pub fn save_receipt(receipt_label: &String, receipt_data: &(String, Receipt)) {
pub fn save_receipt<T: serde::Serialize>(receipt_label: &String, receipt_data: &(String, T)) {
if !is_dev_mode() {
fs::write(
zkp_cache_path(receipt_label),
Expand Down
61 changes: 45 additions & 16 deletions host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use log::info;
use risc0_zkvm::sha::Digest;
use zeth::{
cli::{Cli, Network},
operations::{build, rollups},
operations::{build, rollups, snarks::verify_groth16_snark, stark2snark},
};
use zeth_guests::*;
use zeth_lib::{
Expand All @@ -41,33 +41,62 @@ async fn main() -> Result<()> {

// execute the command
let build_args = cli.build_args();
match build_args.network {
let (image_id, stark) = match build_args.network {
Network::Ethereum => {
let rpc_url = build_args.eth_rpc_url.clone();
build::build_block::<EthereumStrategy>(
cli,
rpc_url,
&ETH_MAINNET_CHAIN_SPEC,
ETH_BLOCK_ELF,
(
ETH_BLOCK_ID,
build::build_block::<EthereumStrategy>(
&cli,
rpc_url,
&ETH_MAINNET_CHAIN_SPEC,
ETH_BLOCK_ELF,
)
.await?,
)
.await
}
Network::Optimism => {
let rpc_url = build_args.op_rpc_url.clone();
build::build_block::<OptimismStrategy>(
cli,
rpc_url,
&OP_MAINNET_CHAIN_SPEC,
OP_BLOCK_ELF,
(
OP_BLOCK_ID,
build::build_block::<OptimismStrategy>(
&cli,
rpc_url,
&OP_MAINNET_CHAIN_SPEC,
OP_BLOCK_ELF,
)
.await?,
)
.await
}
Network::OptimismDerived => {
if let Some(composition_size) = build_args.composition {
rollups::compose_derived_rollup_blocks(cli, composition_size).await
(
OP_COMPOSE_ID,
rollups::compose_derived_rollup_blocks(&cli, composition_size).await?,
)
} else {
rollups::derive_rollup_blocks(cli).await
(OP_DERIVE_ID, rollups::derive_rollup_blocks(&cli).await?)
}
}
};

// Create/verify Groth16 SNARK
if cli.snark() {
let Some((stark_uuid, stark_receipt)) = stark else {
panic!("No STARK data to snarkify!");
};

if !cli.submit_to_bonsai() {
panic!("Bonsai submission flag required to create a SNARK!");
}

let image_id = Digest::from(image_id);
let (snark_uuid, snark_receipt) = stark2snark(image_id, stark_uuid, stark_receipt).await?;

info!("Validating SNARK uuid: {}", snark_uuid);

verify_groth16_snark(&cli, image_id, snark_receipt).await?;
}

Ok(())
}
25 changes: 13 additions & 12 deletions host/src/operations/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::fmt::Debug;
use anyhow::Context;
use ethers_core::types::Transaction as EthersTransaction;
use log::{info, warn};
use risc0_zkvm::compute_image_id;
use risc0_zkvm::{compute_image_id, Receipt};
use serde::{Deserialize, Serialize};
use zeth_lib::{
builder::BlockBuilderStrategy,
Expand All @@ -34,11 +34,11 @@ use crate::{

/// Build a single block using the specified strategy.
pub async fn build_block<N: BlockBuilderStrategy>(
cli: Cli,
cli: &Cli,
rpc_url: Option<String>,
chain_spec: &ChainSpec,
guest_elf: &[u8],
) -> anyhow::Result<()>
) -> anyhow::Result<Option<(String, Receipt)>>
where
N::TxEssence: 'static + Send + TryFrom<EthersTransaction> + Serialize + Deserialize<'static>,
<N::TxEssence as TryFrom<EthersTransaction>>::Error: Debug,
Expand Down Expand Up @@ -90,8 +90,8 @@ where
}

let compressed_output = output.with_state_hashed();
match &cli {
Cli::Build(..) => {}
let result = match cli {
Cli::Build(..) => None,
Cli::Run(run_args) => {
execute(
&input,
Expand All @@ -101,27 +101,28 @@ where
&compressed_output,
&cli.execution_tag(),
);
None
}
Cli::Prove(..) => {
maybe_prove(
&cli,
cli,
&input,
guest_elf,
&compressed_output,
Default::default(),
)
.await;
.await
}
Cli::Verify(verify_args) => {
Cli::Verify(verify_args) => Some(
verify_bonsai_receipt(
compute_image_id(guest_elf)?,
&compressed_output,
verify_args.bonsai_receipt_uuid.clone(),
4,
)
.await?;
}
}
.await?,
),
};

Ok(())
Ok(result)
}
76 changes: 74 additions & 2 deletions host/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,92 @@

pub mod build;
pub mod rollups;
pub mod snarks;

use std::fmt::Debug;

use bonsai_sdk::alpha::responses::SnarkReceipt;
use log::{debug, error, info, warn};
use risc0_zkvm::{
compute_image_id, default_prover, serde::to_vec, sha::Digest, Assumption, ExecutorEnv,
ExecutorImpl, FileSegmentRef, Receipt, Session,
compute_image_id, default_prover,
serde::to_vec,
sha::{Digest, Digestible},
Assumption, ExecutorEnv, ExecutorImpl, FileSegmentRef, Receipt, Session,
};
use serde::{de::DeserializeOwned, Serialize};
use tempfile::tempdir;
use zeth_primitives::keccak::keccak;

use crate::{cli::Cli, load_receipt, save_receipt};

pub async fn stark2snark(
image_id: Digest,
stark_uuid: String,
stark_receipt: Receipt,
) -> anyhow::Result<(String, SnarkReceipt)> {
info!("Submitting SNARK workload");
// Label snark output as journal digest
let receipt_label = format!(
"{}-{}",
hex::encode_upper(image_id),
hex::encode(keccak(stark_receipt.journal.bytes.digest()))
);
// Load cached receipt if found
if let Ok(Some(cached_data)) = load_receipt(&receipt_label) {
info!("Loaded locally cached receipt");
return Ok(cached_data);
}
// Otherwise compute on Bonsai
let stark_uuid = if stark_uuid.is_empty() {
upload_receipt(&stark_receipt).await?
} else {
stark_uuid
};

let client = bonsai_sdk::alpha_async::get_client_from_env(risc0_zkvm::VERSION).await?;
let snark_uuid = client.create_snark(stark_uuid)?;

let snark_receipt = loop {
let res = snark_uuid.status(&client)?;

if res.status == "RUNNING" {
info!("Current status: {} - continue polling...", res.status,);
std::thread::sleep(std::time::Duration::from_secs(15));
} else if res.status == "SUCCEEDED" {
break res
.output
.expect("Bonsai response is missing SnarkReceipt.");
} else {
panic!(
"Workflow exited: {} - | err: {}",
res.status,
res.error_msg.unwrap_or_default()
);
}
};

let stark_psd = stark_receipt.get_claim()?.post.digest();
let snark_psd = Digest::try_from(snark_receipt.post_state_digest.as_slice())?;

if stark_psd != snark_psd {
error!("SNARK/STARK Post State Digest mismatch!");
error!("STARK: {}", hex::encode(stark_psd));
error!("SNARK: {}", hex::encode(snark_psd));
}

if snark_receipt.journal != stark_receipt.journal.bytes {
error!("SNARK/STARK Receipt Journal mismatch!");
error!("STARK: {}", hex::encode(&stark_receipt.journal.bytes));
error!("SNARK: {}", hex::encode(&snark_receipt.journal));
};

let snark_data = (snark_uuid.uuid, snark_receipt);

save_receipt(&receipt_label, &snark_data);

Ok(snark_data)
}

pub async fn verify_bonsai_receipt<O: Eq + Debug + DeserializeOwned>(
image_id: Digest,
expected_output: &O,
Expand Down
Loading
Loading