Skip to content

Commit

Permalink
Update rollup_vk Based on Features in datatool (#569)
Browse files Browse the repository at this point in the history
* feat(datatool): rollup vk based on features

* fixes

* feat(rollup-cfg): create a new StrBuf32 type
  • Loading branch information
prajwolrg authored Dec 31, 2024
1 parent 214d184 commit 1d80075
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 31 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion bin/datatool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ path = "src/main.rs"
[dependencies]
strata-key-derivation.workspace = true
strata-primitives.workspace = true
strata-sp1-guest-builder = { path = "../../provers/sp1" }
strata-risc0-guest-builder = { path = "../../provers/risc0", optional = true }
strata-sp1-guest-builder = { path = "../../provers/sp1", optional = true }

anyhow.workspace = true
argh.workspace = true
bech32 = "0.11.0"
bitcoin = { workspace = true, features = ["std"] }
bytemuck = { version = "1.21.0", optional = true }
hex.workspace = true
rand_core.workspace = true
secp256k1 = { workspace = true, features = ["global-context", "std"] }
serde_json.workspace = true
terrors = "0.3.0"
zeroize.workspace = true

[features]
default = []
sp1 = ["strata-sp1-guest-builder"]
risc0 = ["strata-risc0-guest-builder", "bytemuck"]
58 changes: 52 additions & 6 deletions bin/datatool/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use strata_primitives::{
params::{ProofPublishMode, RollupParams},
proof::RollupVerifyingKey,
};
use strata_sp1_guest_builder::GUEST_CHECKPOINT_VK_HASH_STR;
use zeroize::Zeroize;

use crate::args::{
Expand Down Expand Up @@ -66,6 +65,55 @@ pub(super) fn exec_subc(cmd: Subcommand, ctx: &mut CmdContext) -> anyhow::Result
}
}

/// Returns the appropriate [`RollupVerifyingKey`] based on the enabled features.
///
/// # Behavior
///
/// - If the **sp1** feature is exclusively enabled, returns an `SP1VerifyingKey`.
/// - If the **risc0** feature is exclusively enabled, returns a `Risc0VerifyingKey`.
/// - If **both** `sp1` and `risc0` are enabled at once, this function will **panic**.
/// - If **neither** `sp1` nor `risc0` is enabled, returns a `NativeVerifyingKey`.
///
/// # Panics
///
/// Panics if both `sp1` and `risc0` features are enabled simultaneously, since
/// only one ZKVM can be supported at a time.
fn resolve_rollup_vk() -> RollupVerifyingKey {
// Use SP1 if only `sp1` feature is enabled
#[cfg(all(feature = "sp1", not(feature = "risc0")))]
{
use strata_sp1_guest_builder::GUEST_CHECKPOINT_VK_HASH_STR;
let vk_buf32: Buf32 = GUEST_CHECKPOINT_VK_HASH_STR
.parse()
.expect("invalid sp1 checkpoint verifier key hash");
RollupVerifyingKey::SP1VerifyingKey(vk_buf32)
}

// Use Risc0 if only `risc0` feature is enabled
#[cfg(all(feature = "risc0", not(feature = "sp1")))]
{
use strata_risc0_guest_builder::GUEST_RISC0_CHECKPOINT_ID;
let vk_u8: [u8; 32] = bytemuck::cast(GUEST_RISC0_CHECKPOINT_ID);
let vk_buf32 = vk_u8.into();
RollupVerifyingKey::Risc0VerifyingKey(vk_buf32)
}

// Panic if both `sp1` and `risc0` feature are enabled
#[cfg(all(feature = "risc0", feature = "sp1"))]
{
panic!(
"Conflicting ZKVM features: both 'sp1' and 'risc0' are enabled. \
Please disable one of them, as only a single ZKVM can be supported at a time."
)
}

// If neither `risc0` nor `sp1` is enabled, use the Native verifying key
#[cfg(all(not(feature = "risc0"), not(feature = "sp1")))]
{
RollupVerifyingKey::NativeVerifyingKey(Buf32::zero())
}
}

/// Executes the `genxpriv` subcommand.
///
/// Generates a new [`Xpriv`] that will [`Zeroize`](zeroize) on [`Drop`] and writes it to a file.
Expand Down Expand Up @@ -200,9 +248,7 @@ fn exec_genparams(cmd: SubcParams, ctx: &mut CmdContext) -> anyhow::Result<()> {
.unwrap_or(1_000_000_000);

// Parse the checkpoint verification key.
let rollup_vk: Buf32 = GUEST_CHECKPOINT_VK_HASH_STR
.parse()
.expect("invalid checkpoint verifier key hash");
let rollup_vk = resolve_rollup_vk();

let config = ParamsConfig {
name: cmd.name.unwrap_or_else(|| "strata-testnet".to_string()),
Expand Down Expand Up @@ -357,7 +403,7 @@ pub struct ParamsConfig {
/// Operators' keys.
opkeys: Vec<Xpub>,
/// Verifier's key.
rollup_vk: Buf32,
rollup_vk: RollupVerifyingKey,
/// Amount of sats to deposit.
deposit_sats: u64,
/// Timeout for proofs.
Expand Down Expand Up @@ -407,7 +453,7 @@ fn construct_params(config: ParamsConfig) -> RollupParams {
target_l2_batch_size: config.epoch_slots as u64,
address_length: 20,
deposit_amount: config.deposit_sats,
rollup_vk: RollupVerifyingKey::SP1VerifyingKey(config.rollup_vk),
rollup_vk: config.rollup_vk,
// TODO make configurable
dispatch_assignment_dur: 64,
proof_publish_mode: config
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus-logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ strata-eectl.workspace = true
strata-primitives.workspace = true
strata-risc0-adapter.workspace = true
strata-rpc-types.workspace = true
strata-sp1-adapter = { workspace = true, features = ["prover"] }
strata-sp1-adapter.workspace = true
strata-state.workspace = true
strata-status.workspace = true
strata-storage.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/consensus-logic/src/l1_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ pub fn verify_proof(checkpoint: &BatchCheckpoint, rollup_params: &RollupParams)
RollupVerifyingKey::SP1VerifyingKey(vk) => {
strata_sp1_adapter::verify_groth16(proof, vk.as_ref(), &public_params_raw)
}
// In Native Execution mode, we do not actually generate the proof to verify. Checking
// public parameters is sufficient.
RollupVerifyingKey::NativeVerifyingKey(_) => Ok(()),
}
}

Expand Down
10 changes: 10 additions & 0 deletions crates/primitives/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{buf::Buf32, l1::L1BlockId, l2::L2BlockId};
/// This enum encapsulates verifying keys for different ZKVMs:
/// - `SP1VerifyingKey`: Used for verifying proofs generated using SP1.
/// - `Risc0VerifyingKey`: Used for verifying proofs generated using Risc0.
/// - `Native`: For functional testing purposes without ZKVM overhead.
#[derive(Clone, Debug, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RollupVerifyingKey {
Expand All @@ -18,6 +19,15 @@ pub enum RollupVerifyingKey {
/// Verifying Key for proofs generated by Risc0.
#[serde(rename = "risc0")]
Risc0VerifyingKey(Buf32),

/// Placeholder variant for functional testing.
///
/// This variant allows skipping guest code compilation (e.g., ELFs for SP1 or Risc0) and is
/// used to test the prover-client and proof logic without the overhead of ZKVM
/// compilation. It is strictly for internal testing and must not be used in production
/// deployments.
#[serde(rename = "native")]
NativeVerifyingKey(Buf32),
}

/// Represents a context for different types of proofs.
Expand Down
32 changes: 24 additions & 8 deletions functional-tests/rollup_params_cfg.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from pydantic import BaseModel
from typing import Annotated, Union

from pydantic import BaseModel, StringConstraints

# A string that optionally starts with 0x, followed by exactly 64 hex characters
StrBuf32 = Annotated[str, StringConstraints(pattern=r"^(0x)?[0-9A-Fa-f]{64}$")]


class CredRule(BaseModel):
schnorr_key: str
schnorr_key: StrBuf32


class OperatorConfigItem(BaseModel):
signing_pk: str
wallet_pk: str
signing_pk: StrBuf32
wallet_pk: StrBuf32


class OperatorConfig(BaseModel):
Expand All @@ -17,8 +22,19 @@ def get_operators_pubkeys(self) -> list[str]:
return [operator.wallet_pk for operator in self.static]


class RollupVk(BaseModel):
sp1: str
class Sp1RollupVk(BaseModel):
sp1: StrBuf32


class Risc0RollupVk(BaseModel):
risc0: StrBuf32


class NativeRollupVk(BaseModel):
native: StrBuf32


RollupVk = Union[Sp1RollupVk, Risc0RollupVk, NativeRollupVk]


class ProofPublishMode(BaseModel):
Expand All @@ -37,8 +53,8 @@ class RollupConfig(BaseModel):
horizon_l1_height: int
genesis_l1_height: int
operator_config: OperatorConfig
evm_genesis_block_hash: str
evm_genesis_block_state_root: str
evm_genesis_block_hash: StrBuf32
evm_genesis_block_state_root: StrBuf32
l1_reorg_safe_depth: int
target_l2_batch_size: int
address_length: int
Expand Down
2 changes: 1 addition & 1 deletion provers/risc0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ methods = [
]

[features]
default = []
default = ["prover"]
prover = ["dep:strata-risc0-adapter"]
12 changes: 6 additions & 6 deletions provers/risc0/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ fn main() {

let elf = r#"
pub const GUEST_RISC0_EVM_EE_STF_ELF: &[u8] = &[];
pub const GUEST_RISC0_EVM_EE_STF_ID: &[u8] = &[];
pub const GUEST_RISC0_EVM_EE_STF_ID: &[u32; 8] = &[0u32; 8];
pub const GUEST_RISC0_CL_STF_ELF: &[u8] = &[];
pub const GUEST_RISC0_CL_STF_ID: &[u8] = &[];
pub const GUEST_RISC0_CL_STF_ID: [u32; 8] = [0u32; 8];
pub const GUEST_RISC0_CL_AGG_ELF: &[u8] = &[];
pub const GUEST_RISC0_CL_AGG_ID: &[u32; 8] = &[0u32; 8];
pub const GUEST_RISC0_CL_AGG_ID: [u32; 8] = [0u32; 8];
pub const GUEST_RISC0_BTC_BLOCKSPACE_ELF: &[u8] = &[];
pub const GUEST_RISC0_BTC_BLOCKSPACE_ID: &[u8] = &[];
pub const GUEST_RISC0_BTC_BLOCKSPACE_ID: [u32; 8] = [0u32; 8];
pub const GUEST_RISC0_L1_BATCH_ELF: &[u8] = &[];
pub const GUEST_RISC0_L1_BATCH_ID: &[u8] = &[];
pub const GUEST_RISC0_L1_BATCH_ID: [u32; 8] = [0u32; 8];
pub const GUEST_RISC0_CHECKPOINT_ELF: &[u8] = &[];
pub const GUEST_RISC0_CHECKPOINT_ID: &[u8] = &[];
pub const GUEST_RISC0_CHECKPOINT_ID: [u32; 8] = [0u32; 8];
"#;

std::fs::write(methods_path, elf).expect("Failed to write mock rollup elf");
Expand Down
4 changes: 3 additions & 1 deletion provers/risc0/guest-checkpoint/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion provers/risc0/guest-cl-agg/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion provers/risc0/guest-cl-stf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1d80075

Please sign in to comment.