Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Wollac committed Oct 11, 2024
1 parent 90c8b6d commit 3b3e10c
Show file tree
Hide file tree
Showing 34 changed files with 870 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/op/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[workspace]
resolver = "2"
members = ["common", "l1-to-l2", "l2", "l2-to-l1"]

# Always optimize; otherwise tests take excessively long.
[profile.dev]
opt-level = 3

[profile.release]
debug = 1
lto = true

[workspace.dependencies]
risc0-zkvm = { git = "https://github.com/risc0/risc0", branch = "main" }
risc0-build = { git = "https://github.com/risc0/risc0", branch = "main" }
op-steel = { path = "../../crates/op-steel" }
risc0-steel = { path = "../../crates/steel" }
risc0-ethereum-contracts = { path = "../../contracts" }
examples-common = { path = "common" }
alloy = { version = "0.4" }
12 changes: 12 additions & 0 deletions examples/op/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "examples-common"
version = "0.1.0"
edition = "2021"

[dependencies]
alloy = { workspace = true, features = ["consensus", "node-bindings"] }
anyhow = "1.0"
log = "0.4"
risc0-ethereum-contracts = { workspace = true }
risc0-zkvm = { workspace = true }
url = "2.5"
39 changes: 39 additions & 0 deletions examples/op/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use alloy::providers::ProviderBuilder;
use anyhow::Context;
use risc0_zkvm::{sha::Digest, Receipt};
use url::Url;

pub async fn verify_on_chain(
receipt: Receipt,
image_id: Digest,
rpc_url: Url,
) -> anyhow::Result<()> {
log::info!("Validating the receipt on {}...", rpc_url);

let seal = risc0_ethereum_contracts::encode_seal(&receipt).context("encode_seal failed")?;
let journal = receipt.journal.bytes;

let provider = ProviderBuilder::new()
.with_recommended_fillers()
.on_anvil_with_wallet_and_config(|anvil| anvil.fork(rpc_url));

alloy::sol!(
#[sol(rpc)]
Verifier,
"../contracts/out/Verifier.sol/Verifier.json"
);

let verifier = Verifier::deploy(provider)
.await
.context("failed to deploy Verifier")?;
let verify = verifier.verify(
journal.into(),
seal.into(),
<[u8; 32]>::from(image_id).into(),
);
log::debug!("Calling {} {}", verifier.address(), verify.calldata());
verify.call().await?;
log::info!("Receipt validated");

Ok(())
}
14 changes: 14 additions & 0 deletions examples/op/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
6 changes: 6 additions & 0 deletions examples/op/contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["../../../contracts/src/", "../../../lib/"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
3 changes: 3 additions & 0 deletions examples/op/contracts/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
forge-std/=../../../lib/forge-std/
openzeppelin/=../../../lib/openzeppelin-contracts/
risc0/=../../../contracts/src/
55 changes: 55 additions & 0 deletions examples/op/contracts/src/Verifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.9;

import {ControlID, RiscZeroGroth16Verifier} from "risc0/groth16/RiscZeroGroth16Verifier.sol";
import {RiscZeroVerifierRouter, IRiscZeroVerifier} from "risc0/RiscZeroVerifierRouter.sol";
import {RiscZeroMockVerifier} from "risc0/test/RiscZeroMockVerifier.sol";
import {Steel} from "risc0/steel/Steel.sol";
import {OpCommitmentValidator} from "risc0/steel/OpSteel.sol";

contract Verifier is OpCommitmentValidator {
address internal constant MAINNET_OPTIMISM_PORTAL_PROXY = address(0xbEb5Fc579115071764c7423A4f12eDde41f106Ed);

IRiscZeroVerifier public immutable riscZeroVerifier;

constructor() OpCommitmentValidator(MAINNET_OPTIMISM_PORTAL_PROXY) {
RiscZeroVerifierRouter router = new RiscZeroVerifierRouter(address(this));

RiscZeroGroth16Verifier verifier =
new RiscZeroGroth16Verifier(ControlID.CONTROL_ROOT, ControlID.BN254_CONTROL_ID);
router.addVerifier(verifier.SELECTOR(), verifier);

RiscZeroMockVerifier mock = new RiscZeroMockVerifier(bytes4(0));
router.addVerifier(mock.SELECTOR(), mock);

riscZeroVerifier = router;
}

function verify(bytes calldata journal, bytes calldata seal, bytes32 imageID)
external
view
returns (bytes memory payload)
{
Steel.Commitment memory commitment = abi.decode(journal[:96], (Steel.Commitment));
require(validateCommitment(commitment), "Invalid commitment");

riscZeroVerifier.verify(seal, imageID, sha256(journal));

return journal[64:];
}
}
21 changes: 21 additions & 0 deletions examples/op/l1-to-l2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "l1-to-l2"
version = "0.1.0"
edition = "2021"

[dependencies]
alloy = { workspace = true, features = ["full"] }
anyhow = "1.0.89"
clap = { version = "4.5.18", features = ["derive", "env"] }
examples-common = { workspace = true, optional = true }
l1-to-l2-core = { path = "core" }
l1-to-l2-methods = { path = "methods" }
log = "0.4"
op-steel = { workspace = true, features = ["host"] }
risc0-zkvm = { workspace = true, features = ["client"] }
tokio = { version = "1.40", features = ["full"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
url = "2.5"

[features]
verify = ["dep:examples-common"]
8 changes: 8 additions & 0 deletions examples/op/l1-to-l2/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "l1-to-l2-core"
version = "0.1.0"
edition = "2021"

[dependencies]
alloy-primitives = "0.8"
alloy-sol-types = "0.8"
18 changes: 18 additions & 0 deletions examples/op/l1-to-l2/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use alloy_primitives::{address, Address};
use alloy_sol_types::sol;

sol! {
interface IERC20 {
function balanceOf(address account) external view returns (uint);
}
}

/// Function to call, implements the `SolCall` trait.
pub const CALL: IERC20::balanceOfCall = IERC20::balanceOfCall {
account: address!("F977814e90dA44bFA03b6295A0616a897441aceC"),
};

/// Address of the deployed contract to call the function on (USDT contract on Eth Mainnet).
pub const CONTRACT: Address = address!("dAC17F958D2ee523a2206206994597C13D831ec7");

pub const CALLER: Address = Address::ZERO;
10 changes: 10 additions & 0 deletions examples/op/l1-to-l2/methods/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "l1-to-l2-methods"
version = "0.1.0"
edition = "2021"

[build-dependencies]
risc0-build = { workspace = true }

[package.metadata.risc0]
methods = ["guest"]
17 changes: 17 additions & 0 deletions examples/op/l1-to-l2/methods/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2024 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

fn main() {
risc0_build::embed_methods();
}
17 changes: 17 additions & 0 deletions examples/op/l1-to-l2/methods/guest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "l1-to-l2-guest"
version = "0.1.0"
edition = "2021"

[workspace]

[dependencies]
l1-to-l2-core = { path = "../../core" }
risc0-steel = { path = "../../../../../crates/steel" }
risc0-zkvm = { git = "https://github.com/risc0/risc0", branch = "main", default-features = false, features = ["std"] }

[patch.crates-io]
# use optimized risc0 circuit
crypto-bigint = { git = "https://github.com/risc0/RustCrypto-crypto-bigint", tag = "v0.5.5-risczero.0" }
k256 = { git = "https://github.com/risc0/RustCrypto-elliptic-curves", tag = "k256/v0.13.3-risczero.0" }
sha2 = { git = "https://github.com/risc0/RustCrypto-hashes", tag = "sha2-v0.10.8-risczero.0" }
41 changes: 41 additions & 0 deletions examples/op/l1-to-l2/methods/guest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(unused_doc_comments)]
#![no_main]

use l1_to_l2_core::{CALL, CALLER, CONTRACT};
use risc0_steel::{
ethereum::{EthEvmInput, ETH_MAINNET_CHAIN_SPEC},
Contract,
};
use risc0_zkvm::guest::env;

risc0_zkvm::guest::entry!(main);

fn main() {
// Read the input from the guest environment.
let input: EthEvmInput = env::read();

// Create the environment and add the commitment.
let env = input.into_env().with_chain_spec(&ETH_MAINNET_CHAIN_SPEC);
env::commit_slice(&env.commitment().abi_encode());

// Execute the call.
let contract = Contract::new(CONTRACT, &env);
let returns = contract.call_builder(&CALL).from(CALLER).call();

// Commit the result.
env::commit(&returns._0)
}
15 changes: 15 additions & 0 deletions examples/op/l1-to-l2/methods/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

include!(concat!(env!("OUT_DIR"), "/methods.rs"));
Loading

0 comments on commit 3b3e10c

Please sign in to comment.