Skip to content

Commit

Permalink
Add darwinia's precompiles (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest authored Nov 22, 2022
1 parent 28db256 commit 4b40193
Show file tree
Hide file tree
Showing 10 changed files with 876 additions and 44 deletions.
273 changes: 245 additions & 28 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ members = [
"core/*",
"node",
"runtime/*",
"precompiles/*"
]
50 changes: 50 additions & 0 deletions precompiles/bls12-381/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[package]
authors = ["Darwinia Network <[email protected]>"]
description = "State storage precompiles for EVM pallet."
edition = "2021"
homepage = "https://darwinia.network"
license = "GPL-3.0"
name = "darwinia-precompile-bls12-381"
readme = "README.md"
repository = "https://github.com/darwinia-network/darwinia"
version = "6.0.0"

[dependencies]
# crates.io
milagro_bls = { default-features = false, git = "https://github.com/darwinia-network/milagro_bls" }
# frontier
fp-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }
pallet-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }
# moonbeam
precompile-utils = {default-features = false, git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30" }
# paritytech
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }

[dev-dependencies]
# crates.io
codec = { package = "parity-scale-codec", version = "3.2" }
scale-info = { version = "2.3", features = ["derive"] }
# moonbeam
precompile-utils = { git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30", features = [ "testing" ] }
# paritytech
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }

[features]
default = ["std"]

std = [
# frontier
"fp-evm/std",
"pallet-evm/std",
# moonbeam
"precompile-utils/std",
# paritytech
# "frame-support/std",
"sp-std/std"
]
60 changes: 60 additions & 0 deletions precompiles/bls12-381/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Darwinia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]

// std
use core::marker::PhantomData;
// crates.io
use milagro_bls::{AggregatePublicKey, AggregateSignature, PublicKey, Signature};
// moonbeam
use precompile_utils::prelude::*;
// substrate
use sp_std::vec::Vec;

pub(crate) const VERIFY_ESTIMATED_COST: u64 = 100_000;

pub struct BLS12381<T>(PhantomData<T>);

#[precompile_utils::precompile]
impl<Runtime: pallet_evm::Config> BLS12381<Runtime> {
#[precompile::public("fast_aggregate_verify(bytes[],bytes,bytes)")]
#[precompile::view]
fn state_storage_at(
handle: &mut impl PrecompileHandle,
pubkeys: Vec<UnboundedBytes>,
message: UnboundedBytes,
signature: UnboundedBytes,
) -> EvmResult<bool> {
handle.record_cost(VERIFY_ESTIMATED_COST)?;

let sig =
Signature::from_bytes(signature.as_bytes()).map_err(|_| revert("Invalid signature"))?;
let agg_sig = AggregateSignature::from_signature(&sig);

let public_keys: Result<Vec<PublicKey>, _> =
pubkeys.into_iter().map(|k| PublicKey::from_bytes(k.as_bytes())).collect();
let Ok(keys) = public_keys else {
return Err(revert("Invalid pubkeys"));
};

let agg_pub_key =
AggregatePublicKey::into_aggregate(&keys).map_err(|_| revert("Invalid aggregate"))?;
Ok(agg_sig.fast_aggregate_verify_pre_aggregated(message.as_bytes(), &agg_pub_key))
}
}
48 changes: 48 additions & 0 deletions precompiles/state-storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
authors = ["Darwinia Network <[email protected]>"]
description = "State storage precompiles for EVM pallet."
edition = "2021"
homepage = "https://darwinia.network"
license = "GPL-3.0"
name = "darwinia-precompile-state-storage"
readme = "README.md"
repository = "https://github.com/darwinia-network/darwinia"
version = "6.0.0"

[dependencies]
# frontier
fp-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }
pallet-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }
# moonbeam
precompile-utils = {default-features = false, git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30" }
# paritytech
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }

[dev-dependencies]
# crates.io
codec = { package = "parity-scale-codec", version = "3.2" }
scale-info = { version = "2.3", features = ["derive"] }
# moonbeam
precompile-utils = { git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30", features = [ "testing" ] }
# paritytech
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }

[features]
default = ["std"]

std = [
# frontier
"fp-evm/std",
"pallet-evm/std",
# moonbeam
"precompile-utils/std",
# paritytech
# "frame-support/std",
"sp-std/std"
]
72 changes: 72 additions & 0 deletions precompiles/state-storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Darwinia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

// std
use core::marker::PhantomData;
// moonbeam
use precompile_utils::prelude::*;
// substrate
use frame_support::{StorageHasher, Twox128};

const PALLET_PREFIX_LENGTH: usize = 16;

pub trait StorageFilterT {
fn allow(prefix: &[u8]) -> bool;
}

pub struct StateStorage<Runtime, Filter> {
_marker: PhantomData<(Runtime, Filter)>,
}

pub struct EthereumStorageFilter;
impl StorageFilterT for EthereumStorageFilter {
fn allow(prefix: &[u8]) -> bool {
prefix != Twox128::hash(b"EVM") && prefix != Twox128::hash(b"Ethereum")
}
}

#[precompile_utils::precompile]
impl<Runtime, Filter> StateStorage<Runtime, Filter>
where
Runtime: pallet_evm::Config,
Filter: StorageFilterT,
{
#[precompile::public("state_storage(bytes)")]
#[precompile::view]
fn state_storage_at(
handle: &mut impl PrecompileHandle,
key: UnboundedBytes,
) -> EvmResult<UnboundedBytes> {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let bytes = key.as_bytes();
if bytes.len() < PALLET_PREFIX_LENGTH || !Filter::allow(&bytes[0..PALLET_PREFIX_LENGTH]) {
return Err(revert("Read restriction"));
}

let output = frame_support::storage::unhashed::get_raw(&bytes);
Ok(output.unwrap_or_default().as_slice().into())
}
}
Loading

0 comments on commit 4b40193

Please sign in to comment.