-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28db256
commit 4b40193
Showing
10 changed files
with
876 additions
and
44 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ members = [ | |
"core/*", | ||
"node", | ||
"runtime/*", | ||
"precompiles/*" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
Oops, something went wrong.