Skip to content

Commit

Permalink
feat(raiko): update risc0 toolchain to v1.0.1 (#311)
Browse files Browse the repository at this point in the history
* update risc0 toolchain to v1.0.1

Signed-off-by: smtmfft <[email protected]>

* fix lint & fmt

Signed-off-by: smtmfft <[email protected]>

* fix fmt

Signed-off-by: smtmfft <[email protected]>

* fix ci error

Signed-off-by: smtmfft <[email protected]>

* fix CI build

Signed-off-by: smtmfft <[email protected]>

* fix CI

Signed-off-by: smtmfft <[email protected]>

* remove useless constant

Signed-off-by: smtmfft <[email protected]>

* temporarily fix unittest

Signed-off-by: smtmfft <[email protected]>

* fix fmt

Signed-off-by: smtmfft <[email protected]>

* Update script/install.sh

Co-authored-by: Brecht Devos <[email protected]>

---------

Signed-off-by: smtmfft <[email protected]>
Co-authored-by: Brecht Devos <[email protected]>
  • Loading branch information
smtmfft and Brechtpd authored Jul 5, 2024
1 parent e7cb6a9 commit d2b87e0
Show file tree
Hide file tree
Showing 18 changed files with 323 additions and 285 deletions.
405 changes: 224 additions & 181 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ reth-chainspec = { git = "https://github.com/taikoxyz/taiko-reth.git", branch =
reth-provider = { git = "https://github.com/taikoxyz/taiko-reth.git", branch = "v1.0.0-rc.2-taiko", default-features = false }

# risc zero
risc0-zkvm = { version = "0.21.0", features = ["prove", "getrandom"] }
bonsai-sdk = { version = "0.7.0", features = ["async"] }
risc0-build = { version = "0.21.0" }
risc0-binfmt = { version = "0.21.0" }
risc0-zkvm = { version = "1.0.1", features = ["prove", "getrandom"] }
bonsai-sdk = { version = "0.8.0", features = ["async"] }
risc0-build = { version = "1.0.1" }
risc0-binfmt = { version = "1.0.1" }

# SP1
sp1-sdk = { git = "https://github.com/succinctlabs/sp1.git", branch = "main" }
Expand Down
2 changes: 1 addition & 1 deletion core/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl ProofType {
let mut proof = match self {
ProofType::Native => NativeProver::run(input.clone(), output, config)
.await
.map_err(|e| e.into()),
.map_err(<ProverError as Into<RaikoError>>::into),
ProofType::Sp1 => {
#[cfg(feature = "sp1")]
return sp1_driver::Sp1Prover::run(input.clone(), output, config)
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ mod tests {
if !(is_ci() && proof_type == ProofType::Sp1) {
let network = Network::Ethereum.to_string();
let l1_network = Network::Ethereum.to_string();
let block_number = 19707175;
let block_number = 19907175;
let taiko_chain_spec = SupportedChainSpecs::default()
.get_chain_spec(&network)
.unwrap();
Expand Down
6 changes: 5 additions & 1 deletion host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use raiko_task_manager::{get_task_manager, TaskManager, TaskManagerOpts, TaskSta
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::sync::mpsc;
use tracing::{error, info};
use tracing::{debug, error, info};

use crate::{
interfaces::{HostError, HostResult},
Expand Down Expand Up @@ -211,6 +211,10 @@ impl ProverState {
Ok(proof) => {
let proof = proof.proof.unwrap_or_default();
let proof = proof.as_bytes();
debug!(
"Proof generated for block {} on {} is {:?}",
proof_request.block_number, proof_request.network, proof
);
if manager
.update_task_progress(
chain_id,
Expand Down
2 changes: 1 addition & 1 deletion host/src/server/api/v1/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ mod test {
async fn test_generate_input_from_cache() {
let l1 = &Network::Holesky.to_string();
let l2 = &Network::TaikoA7.to_string();
let block_number: u64 = 69627;
let block_number: u64 = 123456;
let (input, provider) = create_cache_input(l1, l2, block_number).await;
let cache_path = Some("./".into());
assert!(set_cached_input(&cache_path, block_number, l2, &input).is_ok());
Expand Down
2 changes: 0 additions & 2 deletions pipeline/src/risc0_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ impl GuestListEntry {

let upper = self.name.to_uppercase().replace('-', "_");
let image_id: [u32; DIGEST_WORDS] = self.image_id;
let elf_path: &str = &self.path;
format!(
r##"
pub const {upper}_ELF: &[u8] = include_bytes!("{relative_path}");
pub const {upper}_ID: [u32; 8] = {image_id:?};
pub const {upper}_PATH: &str = r#"{elf_path}"#;
"##
)
}
Expand Down
2 changes: 1 addition & 1 deletion provers/risc0/driver/src/bonsai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub fn prove_locally(

exec.run().unwrap()
};
session.prove().unwrap()
session.prove().unwrap().receipt
}

pub fn load_receipt<T: serde::de::DeserializeOwned>(
Expand Down
2 changes: 1 addition & 1 deletion provers/risc0/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ mod test {
let env = ExecutorEnv::builder().build().unwrap();
let prover = default_prover();
let receipt = prover.prove(env, TEST_RISC0_GUEST_ELF).unwrap();
receipt.verify(TEST_RISC0_GUEST_ID).unwrap();
receipt.receipt.verify(TEST_RISC0_GUEST_ID).unwrap();
}
}
4 changes: 1 addition & 3 deletions provers/risc0/driver/src/methods/ecdsa.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub const ECDSA_ELF: &[u8] =
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/ecdsa");
pub const ECDSA_ID: [u32; 8] = [
2112349872, 662412126, 2552816809, 3712154546, 3455470029, 3928252579, 3471286672, 2002692558,
3688490884, 2127892678, 3137078981, 1193344426, 4105663218, 3901516424, 3225864022, 13950036,
];
pub const ECDSA_PATH: &str =
r#"/home/ubuntu/raiko/provers/risc0/guest/target/riscv32im-risc0-zkvm-elf/release/ecdsa"#;
4 changes: 1 addition & 3 deletions provers/risc0/driver/src/methods/risc0_guest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub const RISC0_GUEST_ELF: &[u8] =
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest");
pub const RISC0_GUEST_ID: [u32; 8] = [
1106284168, 2121988569, 4030233466, 422073515, 2026351144, 1404012643, 690639581, 3252900346,
3656027178, 4281856604, 2184891755, 3275826590, 1143313757, 1779451290, 3242125827, 14009949,
];
pub const RISC0_GUEST_PATH: &str =
r#"/home/ubuntu/raiko/provers/risc0/guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest"#;
4 changes: 1 addition & 3 deletions provers/risc0/driver/src/methods/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub const SHA256_ELF: &[u8] =
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/sha256");
pub const SHA256_ID: [u32; 8] = [
3212202202, 3880734562, 3977985800, 3462182722, 1762988696, 2700707388, 359464217, 3702618422,
3390470592, 1661893947, 1504130727, 2880762366, 880654584, 3550598459, 34571735, 2700164568,
];
pub const SHA256_PATH: &str =
r#"/home/ubuntu/raiko/provers/risc0/guest/target/riscv32im-risc0-zkvm-elf/release/sha256"#;
5 changes: 2 additions & 3 deletions provers/risc0/driver/src/methods/test_risc0_guest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub const TEST_RISC0_GUEST_ELF: &[u8] = include_bytes!(
"../../../guest/target/riscv32im-risc0-zkvm-elf/release/deps/risc0_guest-f2134d0a9dc1b778"
"../../../guest/target/riscv32im-risc0-zkvm-elf/release/deps/risc0_guest-9d0bc3658d2edeaa"
);
pub const TEST_RISC0_GUEST_ID: [u32; 8] = [
3875868406, 1668916610, 1404967724, 4014172081, 1679226880, 1967164957, 1092356078, 3079385105,
1938439720, 3200608207, 3901424709, 2031136574, 850058303, 3058786714, 2432413607, 2771178900,
];
pub const TEST_RISC0_GUEST_PATH: &str = r#"provers/risc0/guest/target/riscv32im-risc0-zkvm-elf/release/deps/risc0_guest-f2134d0a9dc1b778"#;
69 changes: 30 additions & 39 deletions provers/risc0/driver/src/snarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@

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

use alloy_primitives::U256;
use alloy_sol_types::{sol, SolValue};
use alloy_sol_types::sol;
use anyhow::Result;
use bonsai_sdk::alpha::responses::SnarkReceipt;
use ethers_contract::abigen;
use ethers_core::types::H160;
use ethers_providers::{Http, Provider, RetryClient};
use log::{error, info};
use raiko_lib::primitives::keccak::keccak;
use risc0_zkvm::Receipt;
use risc0_zkvm::{
sha::{Digest, Digestible},
Groth16Seal,
Groth16ReceiptVerifierParameters, Receipt,
};

use tracing::{error as tracing_err, info as tracing_info};

use crate::save_receipt;
Expand Down Expand Up @@ -65,33 +65,32 @@ sol!(
abigen!(
IRiscZeroVerifier,
r#"[
function verify(bytes calldata seal, bytes32 imageId, bytes32 postStateDigest, bytes32 journalDigest) external view returns (bool)
function verify(bytes calldata seal, bytes32 imageId, bytes32 journalDigest) external view
]"#
);

fn to_u256_arr<const N: usize>(be_vecs: &[Vec<u8>]) -> [U256; N] {
let tmp: Vec<_> = be_vecs
.iter()
.map(|v| U256::from_be_slice(v.as_slice()))
.collect();
tmp.try_into().unwrap()
}

impl From<Groth16Seal> for Seal {
fn from(val: Groth16Seal) -> Self {
Seal {
a: to_u256_arr(&val.a),
b: [to_u256_arr(&val.b[0]), to_u256_arr(&val.b[1])],
c: to_u256_arr(&val.c),
}
}
// /// ABI encoding of the seal.
// pub fn abi_encode(seal: Vec<u8>) -> Result<Vec<u8>> {
// Ok(encode(seal)?.abi_encode())
// }

/// encoding of the seal with selector.
pub fn encode(seal: Vec<u8>) -> Result<Vec<u8>> {
let verifier_parameters_digest = Groth16ReceiptVerifierParameters::default().digest();
let selector = &verifier_parameters_digest.as_bytes()[..4];
// Create a new vector with the capacity to hold both selector and seal
let mut selector_seal = Vec::with_capacity(selector.len() + seal.len());
selector_seal.extend_from_slice(selector);
selector_seal.extend_from_slice(&seal);

Ok(selector_seal)
}

pub async fn stark2snark(
image_id: Digest,
stark_uuid: String,
stark_receipt: Receipt,
) -> anyhow::Result<(String, SnarkReceipt)> {
) -> Result<(String, SnarkReceipt)> {
info!("Submitting SNARK workload");
// Label snark output as journal digest
let receipt_label = format!(
Expand Down Expand Up @@ -133,7 +132,7 @@ pub async fn stark2snark(
}
};

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

if stark_psd != snark_psd {
Expand All @@ -155,10 +154,7 @@ pub async fn stark2snark(
Ok(snark_data)
}

pub async fn verify_groth16_snark(
image_id: Digest,
snark_receipt: SnarkReceipt,
) -> anyhow::Result<()> {
pub async fn verify_groth16_snark(image_id: Digest, snark_receipt: SnarkReceipt) -> Result<()> {
let verifier_rpc_url =
std::env::var("GROTH16_VERIFIER_RPC_URL").expect("env GROTH16_VERIFIER_RPC_URL");
let groth16_verifier_addr = {
Expand All @@ -172,7 +168,7 @@ pub async fn verify_groth16_snark(
500,
)?);

let seal = <Groth16Seal as Into<Seal>>::into(snark_receipt.snark).abi_encode();
let seal = encode(snark_receipt.snark.to_vec())?;
let journal_digest = snark_receipt.journal.digest();
tracing_info!("Verifying SNARK:");
tracing_info!("Seal: {}", hex::encode(&seal));
Expand All @@ -181,24 +177,19 @@ pub async fn verify_groth16_snark(
"Post State Digest: {}",
hex::encode(&snark_receipt.post_state_digest)
);
tracing_info!("Journal Digest: {}", hex::encode(journal_digest.as_bytes()));
let verification: bool = IRiscZeroVerifier::new(groth16_verifier_addr, http_client)
tracing_info!("Journal Digest: {}", hex::encode(journal_digest));
let verify_call_res = IRiscZeroVerifier::new(groth16_verifier_addr, http_client)
.verify(
seal.into(),
image_id.as_bytes().try_into().unwrap(),
snark_receipt
.post_state_digest
.as_slice()
.try_into()
.unwrap(),
journal_digest.as_bytes().try_into().unwrap(),
journal_digest.into(),
)
.await?;
.await;

if verification {
if verify_call_res.is_ok() {
tracing_info!("SNARK verified successfully using {groth16_verifier_addr:?}!");
} else {
tracing_err!("SNARK verification failed!");
tracing_err!("SNARK verification failed: {:?}!", verify_call_res);
}

Ok(())
Expand Down
Loading

0 comments on commit d2b87e0

Please sign in to comment.