From 772da8bb617870585a7c2ca6364104acafc54af7 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov <39522748+dmitrylavrenov@users.noreply.github.com> Date: Mon, 22 Apr 2024 14:57:47 +0300 Subject: [PATCH] Integrate BLS12-381 curve operations as frontier precompiles to `humanode-runtime` (#1012) Integrate BLS12-381 curve operations as precompiles to humanode-runtime --- Cargo.lock | 1 + crates/humanode-runtime/Cargo.toml | 2 + .../src/frontier_precompiles.rs | 42 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index aa4c78dfd..c7d58685c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4146,6 +4146,7 @@ dependencies = [ "pallet-vesting", "parity-scale-codec", "precompile-bioauth", + "precompile-bls12381", "precompile-currency-swap", "precompile-evm-accounts-mapping", "precompile-native-currency", diff --git a/crates/humanode-runtime/Cargo.toml b/crates/humanode-runtime/Cargo.toml index 05ba416bf..0e937312e 100644 --- a/crates/humanode-runtime/Cargo.toml +++ b/crates/humanode-runtime/Cargo.toml @@ -32,6 +32,7 @@ pallet-pot = { path = "../pallet-pot", default-features = false } pallet-token-claims = { path = "../pallet-token-claims", default-features = false } pallet-vesting = { path = "../pallet-vesting", default-features = false } precompile-bioauth = { path = "../precompile-bioauth", default-features = false } +precompile-bls12381 = { path = "../precompile-bls12381", default-features = false } precompile-currency-swap = { path = "../precompile-currency-swap", default-features = false } precompile-evm-accounts-mapping = { path = "../precompile-evm-accounts-mapping", default-features = false } precompile-native-currency = { path = "../precompile-native-currency", default-features = false } @@ -193,6 +194,7 @@ std = [ "pallet-utility/std", "pallet-vesting/std", "precompile-bioauth/std", + "precompile-bls12381/std", "precompile-currency-swap/std", "precompile-evm-accounts-mapping/std", "precompile-native-currency/std", diff --git a/crates/humanode-runtime/src/frontier_precompiles.rs b/crates/humanode-runtime/src/frontier_precompiles.rs index 004d768ef..fb6da6c68 100644 --- a/crates/humanode-runtime/src/frontier_precompiles.rs +++ b/crates/humanode-runtime/src/frontier_precompiles.rs @@ -6,6 +6,10 @@ use pallet_evm_precompile_modexp::Modexp; use pallet_evm_precompile_sha3fips::Sha3FIPS256; use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256}; use precompile_bioauth::Bioauth; +use precompile_bls12381::{ + Bls12381G1Add, Bls12381G1Mul, Bls12381G1MultiExp, Bls12381G2Add, Bls12381G2Mul, + Bls12381G2MultiExp, Bls12381MapG1, Bls12381MapG2, Bls12381Pairing, +}; use precompile_currency_swap::CurrencySwap; use precompile_evm_accounts_mapping::EvmAccountsMapping; use precompile_native_currency::NativeCurrency; @@ -28,6 +32,25 @@ pub mod precompiles_constants { /// `Modexp` precompile constant. pub const MODEXP: u64 = 5; + /// `Bls12381G1Add` precompile constant. + pub const BLS12381_G1_ADD: u64 = 11; + /// `Bls12381G1Mul` precompile constant. + pub const BLS12381_G1_MUL: u64 = 12; + /// `Bls12381G1MultiExp` precompile constant. + pub const BLS12381_G1_MULTI_EXP: u64 = 13; + /// `Bls12381G2Add` precompile constant. + pub const BLS12381_G2_ADD: u64 = 14; + /// `Bls12381G2Mul` precompile constant. + pub const BLS12381_G2_MUL: u64 = 15; + /// `Bls12381G2MultiExp` precompile constant. + pub const BLS12381_G2_MULTI_EXP: u64 = 16; + /// `Bls12381Pairing` precompile constant. + pub const BLS12381_PAIRING: u64 = 17; + /// `Bls12381MapG1` precompile constant. + pub const BLS12381_MAP_G1: u64 = 18; + /// `Bls12381MapG2` precompile constant. + pub const BLS12381_MAP_G2: u64 = 19; + /// `Sha3FIPS256` precompile constant. pub const SHA_3_FIPS256: u64 = 1024; /// `ECRecoverPublicKey` precompile constant. @@ -64,6 +87,15 @@ where RIPEMD_160, IDENTITY, MODEXP, + BLS12381_G1_ADD, + BLS12381_G1_MUL, + BLS12381_G1_MULTI_EXP, + BLS12381_G2_ADD, + BLS12381_G2_MUL, + BLS12381_G2_MULTI_EXP, + BLS12381_PAIRING, + BLS12381_MAP_G1, + BLS12381_MAP_G2, SHA_3_FIPS256, EC_RECOVER_PUBLIC_KEY, BIOAUTH, @@ -99,6 +131,16 @@ where a if a == hash(RIPEMD_160) => Some(Ripemd160::execute(handle)), a if a == hash(IDENTITY) => Some(Identity::execute(handle)), a if a == hash(MODEXP) => Some(Modexp::execute(handle)), + // BLS12-381 precompiles: + a if a == hash(BLS12381_G1_ADD) => Some(Bls12381G1Add::execute(handle)), + a if a == hash(BLS12381_G1_MUL) => Some(Bls12381G1Mul::execute(handle)), + a if a == hash(BLS12381_G1_MULTI_EXP) => Some(Bls12381G1MultiExp::execute(handle)), + a if a == hash(BLS12381_G2_ADD) => Some(Bls12381G2Add::execute(handle)), + a if a == hash(BLS12381_G2_MUL) => Some(Bls12381G2Mul::execute(handle)), + a if a == hash(BLS12381_G2_MULTI_EXP) => Some(Bls12381G2MultiExp::execute(handle)), + a if a == hash(BLS12381_PAIRING) => Some(Bls12381Pairing::execute(handle)), + a if a == hash(BLS12381_MAP_G1) => Some(Bls12381MapG1::execute(handle)), + a if a == hash(BLS12381_MAP_G2) => Some(Bls12381MapG2::execute(handle)), // Non-Frontier specific nor Ethereum precompiles : a if a == hash(SHA_3_FIPS256) => Some(Sha3FIPS256::execute(handle)), a if a == hash(EC_RECOVER_PUBLIC_KEY) => Some(ECRecoverPublicKey::execute(handle)),