Skip to content

Commit

Permalink
feat: makes revm sp1 compatible through feature controlling
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhAnGeek committed Nov 6, 2024
1 parent 0978bcb commit 86fc74b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 39 deletions.
32 changes: 6 additions & 26 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions crates/precompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ secp256k1 = { version = ">=0.28, <=0.29", default-features = false, features = [
"rand",
"global-context",
], optional = true }
cometbft = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "1282547" }
cometbft-light-client-verifier = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "1282547" }
cometbft-proto = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "1282547" }
cometbft-light-client = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "1282547" }
cometbft = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "5582327" }
cometbft-light-client-verifier = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "5582327" }
cometbft-proto = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "5582327" }
cometbft-light-client = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "5582327", default-features = false }
prost = { version = "0.12.6" }
bls_on_arkworks = "0.3.0"
tendermint = { git = "https://github.com/bnb-chain/tendermint-rs-parlia", rev = "8c21ccbd58a174e07eed2c9343e63ccd00f0fbd5", features = ["secp256k1"] }
tendermint = { git = "https://github.com/bnb-chain/tendermint-rs-parlia", rev = "8c21ccbd58a174e07eed2c9343e63ccd00f0fbd5", optional = true, features = ["secp256k1"] }
parity-bytes = { version = "0.1.2", default-features = false }

# SHA2-256 and RIPEMD-160
Expand Down Expand Up @@ -99,7 +99,7 @@ std = [
hashbrown = ["revm-primitives/hashbrown"]
asm-keccak = ["revm-primitives/asm-keccak"]

bsc = ["revm-primitives/bsc", "secp256k1", "secp256r1"]
bsc = ["revm-primitives/bsc", "secp256k1", "secp256r1", "tendermint"]

optimism = ["revm-primitives/optimism", "secp256r1"]
# Optimism default handler enabled Optimism handler register by default in EvmBuilder.
Expand Down
2 changes: 1 addition & 1 deletion crates/precompile/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bls_on_arkworks as bls;
use revm_primitives::{Bytes, PrecompileOutput};
use std::vec::Vec;

pub(crate) const BLS_SIGNATURE_VALIDATION: PrecompileWithAddress = PrecompileWithAddress(
pub const BLS_SIGNATURE_VALIDATION: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(102),
Precompile::Standard(bls_signature_validation_run),
);
Expand Down
2 changes: 1 addition & 1 deletion crates/precompile/src/cometbft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use prost::Message;
use revm_primitives::{Bytes, PrecompileOutput};
use std::{borrow::ToOwned, string::String, vec::Vec};

pub(crate) const COMETBFT_LIGHT_BLOCK_VALIDATION: PrecompileWithAddress = PrecompileWithAddress(
pub const COMETBFT_LIGHT_BLOCK_VALIDATION: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(103),
Precompile::Standard(cometbft_light_block_validation_run),
);
Expand Down
15 changes: 15 additions & 0 deletions crates/precompile/src/iavl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![allow(dead_code)]
#![allow(unused_imports)]

use crate::{Bytes, Error, Precompile, PrecompileError, PrecompileResult, PrecompileWithAddress};
use parity_bytes::BytesRef;
use revm_primitives::PrecompileOutput;
#[cfg(feature = "bsc")]
use tendermint::lite::iavl_proof;

/// Iavl proof validation precompile for BSC.
Expand Down Expand Up @@ -61,6 +63,7 @@ fn iavl_proof_validation_run_plato(input: &Bytes, gas_limit: u64) -> PrecompileR
}

/// Run Iavl proof validation with given hardfork toggles.
#[cfg(feature = "bsc")]
fn iavl_proof_validation_run_inner(
input: &Bytes,
gas_limit: u64,
Expand All @@ -86,7 +89,19 @@ fn iavl_proof_validation_run_inner(
}
}

#[cfg(not(feature = "bsc"))]
fn iavl_proof_validation_run_inner(
_input: &Bytes,
_gas_limit: u64,
_is_moran: bool,
_is_planck: bool,
_is_plato: bool,
) -> PrecompileResult {
Ok(PrecompileOutput::new(0, Bytes::default()))
}

#[cfg(test)]
#[cfg(feature = "bsc")]
mod tests {
use super::*;
use revm_primitives::hex;
Expand Down
12 changes: 9 additions & 3 deletions crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub mod secp256k1;
pub mod secp256r1;
pub mod utilities;

mod bls;
mod cometbft;
pub mod bls;
pub mod cometbft;
mod double_sign;
mod iavl;
mod tendermint;
Expand Down Expand Up @@ -181,7 +181,9 @@ impl Precompiles {
pub fn nano() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
#[allow(unused_mut)]
let mut precompiles = Self::istanbul().clone();
#[cfg(feature = "bsc")]
precompiles.extend([
tendermint::TENDERMINT_HEADER_VALIDATION_NANO,
iavl::IAVL_PROOF_VALIDATION_NANO,
Expand All @@ -195,7 +197,9 @@ impl Precompiles {
pub fn moran() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
#[allow(unused_mut)]
let mut precompiles = Self::istanbul().clone();
#[cfg(feature = "bsc")]
precompiles.extend([
tendermint::TENDERMINT_HEADER_VALIDATION,
iavl::IAVL_PROOF_VALIDATION_MORAN,
Expand All @@ -209,7 +213,9 @@ impl Precompiles {
pub fn planck() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
#[allow(unused_mut)]
let mut precompiles = Self::istanbul().clone();
#[cfg(feature = "bsc")]
precompiles.extend([
tendermint::TENDERMINT_HEADER_VALIDATION,
iavl::IAVL_PROOF_VALIDATION_PLANCK,
Expand Down Expand Up @@ -263,7 +269,7 @@ impl Precompiles {
precompiles.extend([double_sign::DOUBLE_SIGN_EVIDENCE_VALIDATION]);

// this feature is enabled with bsc
#[cfg(feature = "secp256k1")]
#[cfg(all(feature = "secp256k1", feature = "bsc"))]
precompiles.extend([tm_secp256k1::TM_SECP256K1_SIGNATURE_RECOVER]);

Box::new(precompiles)
Expand Down
7 changes: 7 additions & 0 deletions crates/precompile/src/tendermint.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
#![allow(unused_imports)]
use crate::{Bytes, Error, Precompile, PrecompileError, PrecompileResult, PrecompileWithAddress};
use parity_bytes::BytesRef;
use revm_primitives::PrecompileOutput;
#[cfg(feature = "bsc")]
use tendermint::lite::light_client;

/// Tendermint precompile for BSC.
#[cfg(feature = "bsc")]
pub(crate) const TENDERMINT_HEADER_VALIDATION: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(100),
Precompile::Standard(crate::tendermint::tendermint_header_validation_run),
);

#[allow(dead_code)]
/// Tendermint precompile for BSC after Nano hardfork.
pub(crate) const TENDERMINT_HEADER_VALIDATION_NANO: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(100),
Precompile::Standard(crate::tendermint::tendermint_header_validation_run_nano),
);

#[allow(dead_code)]
/// Run the Tendermint header validation precompile after Nano hardfork.
fn tendermint_header_validation_run_nano(_input: &Bytes, _gas_limit: u64) -> PrecompileResult {
Err(PrecompileError::other("suspended").into())
}

/// Run the Tendermint header validation precompile.
#[cfg(feature = "bsc")]
fn tendermint_header_validation_run(input: &Bytes, gas_limit: u64) -> PrecompileResult {
const TENDERMINT_HEADER_VALIDATION_BASE: u64 = 3_000;

Expand All @@ -41,6 +47,7 @@ fn tendermint_header_validation_run(input: &Bytes, gas_limit: u64) -> Precompile
}

#[cfg(test)]
#[cfg(feature = "bsc")]
mod tests {
use super::*;
use revm_primitives::hex;
Expand Down
11 changes: 9 additions & 2 deletions crates/precompile/src/tm_secp256k1.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#![allow(unused_imports)]

use crate::{Bytes, Error, Precompile, PrecompileError, PrecompileResult, PrecompileWithAddress};
use revm_primitives::PrecompileOutput;
use secp256k1::{ecdsa, Message, PublicKey};
#[cfg(feature = "bsc")]
use tendermint::{account, public_key};

/// Tendermint SECP256K1 signature recover precompile for BSC.
#[cfg(feature = "bsc")]
pub(crate) const TM_SECP256K1_SIGNATURE_RECOVER: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(105),
Precompile::Standard(tm_secp256k1_signature_recover_run),
);

#[allow(dead_code)]
const SECP256K1_PUBKEY_LENGTH: usize = 33;
#[allow(dead_code)]
const SECP256K1_SIGNATURE_LENGTH: usize = 64;
#[allow(dead_code)]
const SECP256K1_SIGNATURE_MSGHASH_LENGTH: usize = 32;

/// Runs the Tendermint SECP256K1 signature recover precompile.
Expand All @@ -20,6 +27,7 @@ const SECP256K1_SIGNATURE_MSGHASH_LENGTH: usize = 32;
/// | PubKey | Signature | SignatureMsgHash |
///
/// | 33 bytes | 64 bytes | 32 bytes |
#[cfg(feature = "bsc")]
fn tm_secp256k1_signature_recover_run(input: &Bytes, gas_limit: u64) -> PrecompileResult {
const TM_SECP256K1_SIGNATURE_RECOVER_BASE: u64 = 3_000;

Expand Down Expand Up @@ -57,20 +65,19 @@ fn tm_secp256k1_signature_recover_run(input: &Bytes, gas_limit: u64) -> Precompi
if !res {
return Err(PrecompileError::other("invalid signature").into());
}

let tm_pub_key =
match public_key::PublicKey::from_raw_secp256k1(&input[..SECP256K1_PUBKEY_LENGTH]) {
Some(pk) => pk,
None => return Err(PrecompileError::other("invalid pubkey").into()),
};

return Ok(PrecompileOutput::new(
TM_SECP256K1_SIGNATURE_RECOVER_BASE,
Bytes::copy_from_slice(account::Id::from(tm_pub_key).as_bytes()),
));
}

#[cfg(test)]
#[cfg(feature = "bsc")]
mod tests {
use super::*;
use revm_primitives::hex;
Expand Down

0 comments on commit 86fc74b

Please sign in to comment.