Skip to content

feat: euclid v2 #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ rustdoc-args = ["--cfg", "docsrs"]

[workspace.dependencies]
# openvm
openvm = { git = "https://github.com/openvm-org/openvm.git", tag = "v1.0.0-rc.1" }
openvm-ecc-guest = { git = "https://github.com/openvm-org/openvm.git", tag = "v1.0.0-rc.1" }
openvm-pairing-guest = { git = "https://github.com/openvm-org/openvm.git", tag = "v1.0.0-rc.1" }
openvm-keccak256-guest = { git = "https://github.com/openvm-org/openvm.git", tag = "v1.0.0-rc.1" }
openvm-sha256-guest = { git = "https://github.com/openvm-org/openvm.git", tag = "v1.0.0-rc.1" }
openvm = { git = "https://github.com/openvm-org/openvm.git", rev = "f1b4844" }
openvm-ecc-guest = { git = "https://github.com/openvm-org/openvm.git", rev = "f1b4844" }
openvm-pairing-guest = { git = "https://github.com/openvm-org/openvm.git", rev = "f1b4844" }
openvm-keccak256-guest = { git = "https://github.com/openvm-org/openvm.git", rev = "f1b4844" }
openvm-sha256-guest = { git = "https://github.com/openvm-org/openvm.git", rev = "f1b4844" }

[profile.release]
lto = true
Expand Down
9 changes: 8 additions & 1 deletion crates/interpreter/src/gas/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,16 @@ pub fn calculate_initial_tx_gas(
}

// EIP-7702
if spec_id.is_enabled_in(SpecId::PRAGUE) {
#[cfg(not(feature = "scroll"))]
let eip7702_enabled = spec_id.is_enabled_in(SpecId::PRAGUE);
#[cfg(feature = "scroll")]
let eip7702_enabled = spec_id.is_enabled_in(SpecId::EUCLID_V2);

if eip7702_enabled {
gas.initial_gas += authorization_list_num * eip7702::PER_EMPTY_ACCOUNT_COST;
}

if spec_id.is_enabled_in(SpecId::PRAGUE) {
// Calculate gas floor for EIP-7623
gas.floor_gas = calc_tx_floor_cost(tokens_in_calldata);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/instructions/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ macro_rules! as_u64_or_fail_ret {
$interp.instruction_result = $reason;
return $ret;
}
x[0] as usize
x[0] as u64
}
}
};
Expand Down
1 change: 1 addition & 0 deletions crates/precompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ openvm = [
"dep:openvm-sha256-guest",
"dep:openvm-pairing-guest",
"openvm-ecc-guest/k256",
"openvm-ecc-guest/p256",
"openvm-pairing-guest/bn254",
]

Expand Down
22 changes: 21 additions & 1 deletion crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ impl Precompiles {
PrecompileSpecId::PRE_BERNOULLI => Self::pre_bernoulli(),
#[cfg(feature = "scroll")]
PrecompileSpecId::BERNOULLI => Self::bernoulli(),
#[cfg(feature = "scroll")]
PrecompileSpecId::EUCLID_V2 => Self::euclid_v2(),
PrecompileSpecId::CANCUN => Self::cancun(),
PrecompileSpecId::PRAGUE => Self::prague(),
PrecompileSpecId::LATEST => Self::latest(),
Expand Down Expand Up @@ -218,6 +220,20 @@ impl Precompiles {
})
}

/// Returns precompiles for Scroll
#[cfg(feature = "scroll")]
pub fn euclid_v2() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Self::bernoulli().clone();
precompiles.extend([
secp256r1::P256VERIFY, // 0x100
]);

Box::new(precompiles)
})
}

/// Returns the precompiles for the latest spec.
pub fn latest() -> &'static Self {
Self::prague()
Expand Down Expand Up @@ -319,6 +335,8 @@ pub enum PrecompileSpecId {
PRE_BERNOULLI,
#[cfg(feature = "scroll")]
BERNOULLI,
#[cfg(feature = "scroll")]
EUCLID_V2,
CANCUN,
PRAGUE,
LATEST,
Expand All @@ -341,7 +359,9 @@ impl PrecompileSpecId {
#[cfg(feature = "scroll")]
PRE_BERNOULLI => Self::PRE_BERNOULLI,
#[cfg(feature = "scroll")]
BERNOULLI | CURIE | EUCLID => Self::BERNOULLI,
BERNOULLI | CURIE => Self::BERNOULLI,
#[cfg(feature = "scroll")]
EUCLID_V2 => Self::EUCLID_V2,
#[cfg(feature = "optimism")]
BEDROCK | REGOLITH | CANYON => Self::BERLIN,
#[cfg(feature = "optimism")]
Expand Down
45 changes: 39 additions & 6 deletions crates/precompile/src/secp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! with the address that it is currently deployed at.
use crate::{u64_to_address, Precompile, PrecompileWithAddress};
use p256::ecdsa::{signature::hazmat::PrehashVerifier, Signature, VerifyingKey};
use revm_primitives::{Bytes, PrecompileError, PrecompileOutput, PrecompileResult, B256};
use revm_primitives::{Bytes, PrecompileError, PrecompileOutput, PrecompileResult, B256, U256};

/// Base gas fee for secp256r1 p256verify operation.
const P256VERIFY_BASE: u64 = 3450;
Expand Down Expand Up @@ -62,12 +62,45 @@ pub fn verify_impl(input: &[u8]) -> Option<()> {
uncompressed_pk[0] = 0x04;
uncompressed_pk[1..].copy_from_slice(pk);

// Can fail only if the input is not exact length.
let signature = Signature::from_slice(sig).ok()?;
// Can fail if the input is not valid, so we have to propagate the error.
let public_key = VerifyingKey::from_sec1_bytes(&uncompressed_pk).ok()?;
#[cfg(feature = "openvm")]
{
use openvm_ecc_guest::ecdsa::Coordinate;
use openvm_ecc_guest::p256::{P256Coord, P256Scalar};
use openvm_ecc_guest::weierstrass::IntrinsicCurve;
use openvm_ecc_guest::{
algebra::IntMod, ecdsa::VerifyingKey, weierstrass::WeierstrassPoint,
};
use openvm_keccak256_guest::keccak256;

let (r_be, s_be) = sig.split_at(32);
let r_be: [u8; 32] = r_be.try_into().unwrap();
let s_be: [u8; 32] = s_be.try_into().unwrap();
let r_bigint = U256::from_be_bytes(r_be);
if r_bigint == U256::ZERO || r_bigint >= U256::from_le_bytes(P256Scalar::MODULUS) {
return None;
}
let s_bigint = U256::from_be_bytes(s_be);
if s_bigint == U256::ZERO || s_bigint >= U256::from_le_bytes(P256Scalar::MODULUS) {
return None;
}

let x = Coordinate::<p256::NistP256>::from_be_bytes(&pk[..32]);
let y = Coordinate::<p256::NistP256>::from_be_bytes(&pk[32..]);
let point = <p256::NistP256 as IntrinsicCurve>::Point::from_xy(x, y)?;

public_key.verify_prehash(msg, &signature).ok()
let public_key = openvm_ecc_guest::ecdsa::PublicKey::<p256::NistP256>::new(point);
let verifying_key =
openvm_ecc_guest::ecdsa::VerifyingKey::<p256::NistP256>::new(public_key);
verifying_key.verify_prehashed(&msg, &sig).ok()
}
#[cfg(not(feature = "openvm"))]
{
// Can fail only if the input is not exact length.
let signature = Signature::from_slice(sig).ok()?;
// Can fail if the input is not valid, so we have to propagate the error.
let public_key = VerifyingKey::from_sec1_bytes(&uncompressed_pk).ok()?;
public_key.verify_prehash(msg, &signature).ok()
}
}

#[cfg(test)]
Expand Down
11 changes: 10 additions & 1 deletion crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ impl Env {
}

// check if EIP-7702 transaction is enabled.
if !SPEC::enabled(SpecId::PRAGUE) && self.tx.authorization_list.is_some() {
#[cfg(not(feature = "scroll"))]
let eip7702_enabled = SPEC::enabled(SpecId::PRAGUE);
#[cfg(feature = "scroll")]
let eip7702_enabled = SPEC::enabled(SpecId::EUCLID_V2);
if !eip7702_enabled && self.tx.authorization_list.is_some() {
return Err(InvalidTransaction::AuthorizationListNotSupported);
}

Expand Down Expand Up @@ -274,6 +278,11 @@ impl Env {
// Add transaction cost to balance to ensure execution doesn't fail.
account.info.balance = balance_check;
} else {
#[cfg(feature = "scroll")]
if self.tx.scroll.is_l1_msg && SPEC::enabled(SpecId::EUCLID_V2) {
return Ok(());
}

return Err(InvalidTransaction::LackOfFundForMaxFee {
fee: Box::new(balance_check),
balance: Box::new(account.info.balance),
Expand Down
Loading
Loading