Skip to content

Commit

Permalink
Merge pull request #57 from taikoxyz/bonsai-script
Browse files Browse the repository at this point in the history
add script to run bonsai
  • Loading branch information
CeciliaZ030 authored Mar 18, 2024
2 parents 50524f0 + d7809b5 commit 3268d70
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 19 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ cargo run --release --features "risc0 succinct"
RISC0_DEV_MODE=1 cargo run --release --features risc0
```

#### Bonsai
```
# edit run_bonsai.sh and run
run_bonsai.sh
# then
prove_block.sh testnet risc0-bonsai 10
```

#### CPU
```
cargo run --release --features risc0
Expand Down
11 changes: 10 additions & 1 deletion prove_block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ elif [ "$proof" == "risc0" ]; then
"execution_po2": 18
}
}'
elif [ "$proof" == "risc0-bonsai" ]; then
proofType='{
"risc0": {
"bonsai": true,
"snark": true,
"profile": false,
"execution_po2": 20
}
}'
else
echo "Invalid proof name. Please use 'native' or 'risc0'."
echo "Invalid proof name. Please use 'native' or 'risc0[-bonsai]'."
exit 1
fi

Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion raiko-host/src/prover/proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ pub mod risc0 {
ctx: &Context,
req: &Risc0ProofParams,
) -> Result<Risc0Response, String> {
Err("Feature not risc0 is enabled".to_string())
Err("Feature risc0 is not enabled".to_string())
}
}
12 changes: 8 additions & 4 deletions raiko-host/src/prover/proof/risc0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use hex::ToHex;
use risc0_guest::{RISC0_METHODS_ELF, RISC0_METHODS_ID};
use serde::{Deserialize, Serialize};
use tracing::info as traicing_info;
use tracing::info as tracing_info;
use zeth_lib::{
input::{GuestInput, GuestOutput},
EthereumTxEssence,
Expand Down Expand Up @@ -49,14 +49,18 @@ pub async fn execute_risc0(
.await
.map_err(|err| format!("Failed to convert STARK to SNARK: {:?}", err))?;

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

let snark_journal: String = snark_receipt.journal.encode_hex();
verify_groth16_snark(image_id, snark_receipt)
.await
.map_err(|err| format!("Failed to verify SNARK: {:?}", err))?;
Ok(Risc0Response {
journal: snark_journal,
})
} else {
Ok(Risc0Response { journal })
}

Ok(Risc0Response { journal })
}

// pub mod build;
Expand Down
33 changes: 20 additions & 13 deletions raiko-host/src/prover/proof/risc0/snarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;
use std::{str::FromStr, sync::Arc};

use alloy_primitives::U256;
use alloy_sol_types::{sol, SolValue};
use anyhow::anyhow;
use bonsai_sdk::alpha::responses::{Groth16Seal, SnarkReceipt};
use ethers_contract::abigen;
use ethers_core::types::H160;
use ethers_core::{abi::AbiDecode, types::H160};
use ethers_providers::{Http, Provider, RetryClient};
use once_cell::unsync::Lazy;
use risc0_zkvm::sha::{Digest, Digestible};

static RISC_ZERO_VERIFIER: ethers_core::types::Address = H160::zero();
use tracing::{error as tracing_err, info as tracing_info};

sol!(
/// A Groth16 seal over the claimed receipt claim.
Expand Down Expand Up @@ -85,7 +85,11 @@ pub async fn verify_groth16_snark(
image_id: Digest,
snark_receipt: SnarkReceipt,
) -> anyhow::Result<()> {
let verifier_rpc_url = "TODO: http://fuckBonsai:8545";
let verifier_rpc_url = env!("GROTH16_VERIFIER_RPC_URL");
let groth16_verifier_addr = {
let addr = env!("GROTH16_VERIFIER_ADDRESS");
H160::from_str(addr).unwrap()
};

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

let seal = <Groth16Seal as Into<Seal>>::into(snark_receipt.snark).abi_encode();
let journal_digest = snark_receipt.journal.digest();
log::info!("Verifying SNARK:");
log::info!("Seal: {}", hex::encode(&seal));
log::info!("Image ID: {}", hex::encode(image_id.as_bytes()));
log::info!(
tracing_info!("Verifying SNARK:");
tracing_info!("Seal: {}", hex::encode(&seal));
tracing_info!("Image ID: {}", hex::encode(image_id.as_bytes()));
tracing_info!(
"Post State Digest: {}",
hex::encode(&snark_receipt.post_state_digest)
);
log::info!("Journal Digest: {}", hex::encode(journal_digest.as_bytes()));
let verification = IRiscZeroVerifier::new(RISC_ZERO_VERIFIER, http_client)
tracing_info!("Journal Digest: {}", hex::encode(journal_digest.as_bytes()));
let verification: bool = IRiscZeroVerifier::new(groth16_verifier_addr, http_client)
.verify(
seal.into(),
image_id.as_bytes().try_into().unwrap(),
Expand All @@ -117,9 +121,12 @@ pub async fn verify_groth16_snark(
.await?;

if verification {
log::info!("SNARK verified successfully using {}!", RISC_ZERO_VERIFIER);
tracing_info!(
"SNARK verified successfully using {:?}!",
groth16_verifier_addr
);
} else {
log::error!("SNARK verification failed!");
tracing_err!("SNARK verification failed!");
}

Ok(())
Expand Down
12 changes: 12 additions & 0 deletions run_bonsai.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export BONSAI_API_KEY="1234"
export BONSAI_API_URL="https://api.bonsai.xyz/"
# https://dev.risczero.com/api/blockchain-integration/contracts/verifier
# RiscZeroGroth16Verifier.sol Sepolia 0x83C2e9CD64B2A16D3908E94C7654f3864212E2F8
# export GROTH16_VERIFIER_ADDRESS="83C2e9CD64B2A16D3908E94C7654f3864212E2F8"
export GROTH16_VERIFIER_ADDRESS="850EC3780CeDfdb116E38B009d0bf7a1ef1b8b38"
# use your own if sth wrong due to infura limits.
# export GROTH16_VERIFIER_RPC_URL="https://sepolia.infura.io/v3/4c76691f5f384d30bed910018c28ba1d"
export GROTH16_VERIFIER_RPC_URL="https://l1rpc.internal.taiko.xyz"
#export CC=gcc
#export CC_riscv32im_risc0_zkvm_elf=/opt/riscv/bin/riscv32-unknown-elf-gcc
RUST_LOG="[executor]=info" cargo run --release --features risc0

0 comments on commit 3268d70

Please sign in to comment.