From 4685e71f909bd7ac23be8995421f97fe6d0a4c20 Mon Sep 17 00:00:00 2001 From: Vindaar Date: Thu, 30 May 2024 20:56:33 +0200 Subject: [PATCH] add Rust package for EVM precompiles --- Cargo.toml | 1 + .../Cargo.toml | 18 ++ .../src/lib.rs | 267 ++++++++++++++++++ constantine-rust/constantine-sys/src/lib.rs | 14 + 4 files changed, 300 insertions(+) create mode 100644 constantine-rust/constantine-ethereum-evm-precompiles/Cargo.toml create mode 100644 constantine-rust/constantine-ethereum-evm-precompiles/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 173d8e13..911074f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "constantine-rust/constantine-halo2-zal", "constantine-rust/constantine-ethereum-kzg", "constantine-rust/constantine-ethereum-bls-sig", + "constantine-rust/constantine-ethereum-evm-precompiles", ] # If Nim static library is compiled with Clang ThinLTO, enable it on Rust side diff --git a/constantine-rust/constantine-ethereum-evm-precompiles/Cargo.toml b/constantine-rust/constantine-ethereum-evm-precompiles/Cargo.toml new file mode 100644 index 00000000..c44fa082 --- /dev/null +++ b/constantine-rust/constantine-ethereum-evm-precompiles/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "constantine-ethereum-evm-precompiles" +version = "0.1.0" +edition = "2021" + +authors = ["Mamy André-Ratsimbazafy"] +license = "MIT/Apache-2.0" +repository = "https://github.com/mratsim/constantine" + +[dependencies] +constantine-sys = { path = "../constantine-sys" } +constantine-core = { path = "../constantine-core" } + +[dev-dependencies] +serde = { version = "1.0", default-features = false, features = ["derive"] } +serde_json = "1.0" +hex = { version = "0.4", default-features = false, features = ["serde"] } +glob = "0.3" diff --git a/constantine-rust/constantine-ethereum-evm-precompiles/src/lib.rs b/constantine-rust/constantine-ethereum-evm-precompiles/src/lib.rs new file mode 100644 index 00000000..eec48c0a --- /dev/null +++ b/constantine-rust/constantine-ethereum-evm-precompiles/src/lib.rs @@ -0,0 +1,267 @@ +//! Constantine +//! Copyright (c) 2018-2019 Status Research & Development GmbH +//! Copyright (c) 2020-Present Mamy André-Ratsimbazafy +//! Licensed and distributed under either of +//! * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). +//! * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). +//! at your option. This file may not be copied, modified, or distributed except according to those terms. + +use constantine_core::Threadpool; +use constantine_sys::*; + +// -------------------------------- +// ------- EVM precompiles -------- +// -------------------------------- + +#[must_use] +pub fn evm_sha256( + result: &mut [u8; 32], + message: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_sha256( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + message.as_ptr() as *const byte, + message.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_modexp( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_modexp( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bn254_g1add( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bn254_g1add( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bn254_g1mul( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bn254_g1mul( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bn254_ec_pairing_check( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bn254_ecpairingcheck( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} +pub fn evm_bls12381_g1add( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bls12381_g1add( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bls12381_g1mul( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bls12381_g1mul( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bls12381_g1msm( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bls12381_g1msm( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bls12381_g2add( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bls12381_g2add( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bls12381_g2mul( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bls12381_g2mul( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bls12381_g2msm( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bls12381_g2msm( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + + +pub fn evm_bls12381_pairing_check( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bls12381_pairingcheck( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bls12381_map_fp_to_g1( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bls12381_map_fp_to_g1( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} + +pub fn evm_bls12381_map_fp2_to_g2( + result: &mut [u8], + inputs: &[u8] +) -> Result { + unsafe { + let status = ctt_eth_evm_bls12381_map_fp2_to_g2( + result.as_mut_ptr() as *mut byte, + result.len() as isize, + inputs.as_ptr() as *const byte, + inputs.len() as isize, + ); + match status { + ctt_evm_status::cttEVM_Success => Ok(true), + _ => Err(status) + } + } +} diff --git a/constantine-rust/constantine-sys/src/lib.rs b/constantine-rust/constantine-sys/src/lib.rs index 43647bf7..1272aa59 100644 --- a/constantine-rust/constantine-sys/src/lib.rs +++ b/constantine-rust/constantine-sys/src/lib.rs @@ -44,3 +44,17 @@ impl std::fmt::Display for ctt_codec_ecc_status { write!(f, "{}", s) } } + +impl std::fmt::Display for ctt_evm_status { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let s = match self { + ctt_evm_status::cttEVM_Success => "cttEVM_Success", + ctt_evm_status::cttEVM_InvalidInputSize => "cttEVM_InvalidInputSize", + ctt_evm_status::cttEVM_InvalidOutputSize => "cttEVM_InvalidOutputSize", + ctt_evm_status::cttEVM_IntLargerThanModulus => "cttEVM_IntLargerThanModulus", + ctt_evm_status::cttEVM_PointNotOnCurve => "cttEVM_PointNotOnCurve", + ctt_evm_status::cttEVM_PointNotInSubgroup => "cttEVM_PointNotInSubgroup", + }; + write!(f, "{}", s) + } +}