Skip to content

Commit 3268d70

Browse files
authored
Merge pull request #57 from taikoxyz/bonsai-script
add script to run bonsai
2 parents 50524f0 + d7809b5 commit 3268d70

File tree

7 files changed

+59
-19
lines changed

7 files changed

+59
-19
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ cargo run --release --features "risc0 succinct"
5656
RISC0_DEV_MODE=1 cargo run --release --features risc0
5757
```
5858

59+
#### Bonsai
60+
```
61+
# edit run_bonsai.sh and run
62+
run_bonsai.sh
63+
# then
64+
prove_block.sh testnet risc0-bonsai 10
65+
```
66+
5967
#### CPU
6068
```
6169
cargo run --release --features risc0

prove_block.sh

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@ elif [ "$proof" == "risc0" ]; then
3333
"execution_po2": 18
3434
}
3535
}'
36+
elif [ "$proof" == "risc0-bonsai" ]; then
37+
proofType='{
38+
"risc0": {
39+
"bonsai": true,
40+
"snark": true,
41+
"profile": false,
42+
"execution_po2": 20
43+
}
44+
}'
3645
else
37-
echo "Invalid proof name. Please use 'native' or 'risc0'."
46+
echo "Invalid proof name. Please use 'native' or 'risc0[-bonsai]'."
3847
exit 1
3948
fi
4049

Binary file not shown.

raiko-host/src/prover/proof/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ pub mod risc0 {
7878
ctx: &Context,
7979
req: &Risc0ProofParams,
8080
) -> Result<Risc0Response, String> {
81-
Err("Feature not risc0 is enabled".to_string())
81+
Err("Feature risc0 is not enabled".to_string())
8282
}
8383
}

raiko-host/src/prover/proof/risc0/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
use hex::ToHex;
77
use risc0_guest::{RISC0_METHODS_ELF, RISC0_METHODS_ID};
88
use serde::{Deserialize, Serialize};
9-
use tracing::info as traicing_info;
9+
use tracing::info as tracing_info;
1010
use zeth_lib::{
1111
input::{GuestInput, GuestOutput},
1212
EthereumTxEssence,
@@ -49,14 +49,18 @@ pub async fn execute_risc0(
4949
.await
5050
.map_err(|err| format!("Failed to convert STARK to SNARK: {:?}", err))?;
5151

52-
traicing_info!("Validating SNARK uuid: {}", snark_uuid);
52+
tracing_info!("Validating SNARK uuid: {}", snark_uuid);
5353

54+
let snark_journal: String = snark_receipt.journal.encode_hex();
5455
verify_groth16_snark(image_id, snark_receipt)
5556
.await
5657
.map_err(|err| format!("Failed to verify SNARK: {:?}", err))?;
58+
Ok(Risc0Response {
59+
journal: snark_journal,
60+
})
61+
} else {
62+
Ok(Risc0Response { journal })
5763
}
58-
59-
Ok(Risc0Response { journal })
6064
}
6165

6266
// pub mod build;

raiko-host/src/prover/proof/risc0/snarks.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::sync::Arc;
15+
use std::{str::FromStr, sync::Arc};
1616

1717
use alloy_primitives::U256;
1818
use alloy_sol_types::{sol, SolValue};
1919
use anyhow::anyhow;
2020
use bonsai_sdk::alpha::responses::{Groth16Seal, SnarkReceipt};
2121
use ethers_contract::abigen;
22-
use ethers_core::types::H160;
22+
use ethers_core::{abi::AbiDecode, types::H160};
2323
use ethers_providers::{Http, Provider, RetryClient};
24+
use once_cell::unsync::Lazy;
2425
use risc0_zkvm::sha::{Digest, Digestible};
25-
26-
static RISC_ZERO_VERIFIER: ethers_core::types::Address = H160::zero();
26+
use tracing::{error as tracing_err, info as tracing_info};
2727

2828
sol!(
2929
/// A Groth16 seal over the claimed receipt claim.
@@ -85,7 +85,11 @@ pub async fn verify_groth16_snark(
8585
image_id: Digest,
8686
snark_receipt: SnarkReceipt,
8787
) -> anyhow::Result<()> {
88-
let verifier_rpc_url = "TODO: http://fuckBonsai:8545";
88+
let verifier_rpc_url = env!("GROTH16_VERIFIER_RPC_URL");
89+
let groth16_verifier_addr = {
90+
let addr = env!("GROTH16_VERIFIER_ADDRESS");
91+
H160::from_str(addr).unwrap()
92+
};
8993

9094
let http_client = Arc::new(Provider::<RetryClient<Http>>::new_client(
9195
&verifier_rpc_url,
@@ -95,15 +99,15 @@ pub async fn verify_groth16_snark(
9599

96100
let seal = <Groth16Seal as Into<Seal>>::into(snark_receipt.snark).abi_encode();
97101
let journal_digest = snark_receipt.journal.digest();
98-
log::info!("Verifying SNARK:");
99-
log::info!("Seal: {}", hex::encode(&seal));
100-
log::info!("Image ID: {}", hex::encode(image_id.as_bytes()));
101-
log::info!(
102+
tracing_info!("Verifying SNARK:");
103+
tracing_info!("Seal: {}", hex::encode(&seal));
104+
tracing_info!("Image ID: {}", hex::encode(image_id.as_bytes()));
105+
tracing_info!(
102106
"Post State Digest: {}",
103107
hex::encode(&snark_receipt.post_state_digest)
104108
);
105-
log::info!("Journal Digest: {}", hex::encode(journal_digest.as_bytes()));
106-
let verification = IRiscZeroVerifier::new(RISC_ZERO_VERIFIER, http_client)
109+
tracing_info!("Journal Digest: {}", hex::encode(journal_digest.as_bytes()));
110+
let verification: bool = IRiscZeroVerifier::new(groth16_verifier_addr, http_client)
107111
.verify(
108112
seal.into(),
109113
image_id.as_bytes().try_into().unwrap(),
@@ -117,9 +121,12 @@ pub async fn verify_groth16_snark(
117121
.await?;
118122

119123
if verification {
120-
log::info!("SNARK verified successfully using {}!", RISC_ZERO_VERIFIER);
124+
tracing_info!(
125+
"SNARK verified successfully using {:?}!",
126+
groth16_verifier_addr
127+
);
121128
} else {
122-
log::error!("SNARK verification failed!");
129+
tracing_err!("SNARK verification failed!");
123130
}
124131

125132
Ok(())

run_bonsai.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export BONSAI_API_KEY="1234"
2+
export BONSAI_API_URL="https://api.bonsai.xyz/"
3+
# https://dev.risczero.com/api/blockchain-integration/contracts/verifier
4+
# RiscZeroGroth16Verifier.sol Sepolia 0x83C2e9CD64B2A16D3908E94C7654f3864212E2F8
5+
# export GROTH16_VERIFIER_ADDRESS="83C2e9CD64B2A16D3908E94C7654f3864212E2F8"
6+
export GROTH16_VERIFIER_ADDRESS="850EC3780CeDfdb116E38B009d0bf7a1ef1b8b38"
7+
# use your own if sth wrong due to infura limits.
8+
# export GROTH16_VERIFIER_RPC_URL="https://sepolia.infura.io/v3/4c76691f5f384d30bed910018c28ba1d"
9+
export GROTH16_VERIFIER_RPC_URL="https://l1rpc.internal.taiko.xyz"
10+
#export CC=gcc
11+
#export CC_riscv32im_risc0_zkvm_elf=/opt/riscv/bin/riscv32-unknown-elf-gcc
12+
RUST_LOG="[executor]=info" cargo run --release --features risc0

0 commit comments

Comments
 (0)