Skip to content

Commit

Permalink
Separate precompiles to individual crates (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas authored Dec 14, 2020
1 parent 5dc830e commit 23cdfb5
Show file tree
Hide file tree
Showing 23 changed files with 857 additions and 535 deletions.
60 changes: 55 additions & 5 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
members = [
"frame/ethereum",
"frame/evm",
"frame/evm/precompile/simple",
"frame/evm/precompile/modexp",
"frame/evm/precompile/ed25519",
"frame/evm/precompile/bn128",
"frame/evm/precompile/blake2",
"client/consensus",
"client/rpc-core",
"client/rpc",
Expand Down
15 changes: 1 addition & 14 deletions frame/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,9 @@ evm = { version = "0.19.0", default-features = false, features = ["with-codec"]
evm-runtime = { version = "0.19.0", default-features = false }
evm-gasometer = { version = "0.19.0", default-features = false }
sha3 = { version = "0.8", default-features = false }
impl-trait-for-tuples = "0.1"
ripemd160 = { version = "0.9", default-features = false }

num = { version = "0.3", features = ["alloc"], default-features = false, optional = true }
ethereum-types = { version = "0.9.2", default-features = false, optional = true }
bn = { package = "substrate-bn", version = "0.5", default-features = false, optional = true }
ed25519-dalek = { version = "1.0.0", features = ["alloc", "u64_backend"], default-features = false, optional = true }


[features]
default = ["std", "modexp", "ed25519", "bn128", "blake2f"]
blake2f = []
modexp = ["num"]
ed25519 = ["ed25519-dalek"]
bn128 = ["bn", "ethereum-types"]
default = ["std"]
std = [
"serde",
"codec/std",
Expand All @@ -64,5 +52,4 @@ std = [
"evm-runtime/std",
"evm-gasometer/std",
"pallet-timestamp/std",
"ripemd160/std",
]
24 changes: 24 additions & 0 deletions frame/evm/precompile/blake2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "pallet-evm-precompile-blake2"
version = "2.0.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
description = "BLAKE2 precompiles for EVM pallet."

[dependencies]
sp-core = { version = "2.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-io = { version = "2.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
fp-evm = { version = "0.8.0", default-features = false, path = "../../../../primitives/evm" }
evm = { version = "0.19.0", default-features = false, features = ["with-codec"] }

[features]
default = ["std"]
std = [
"sp-core/std",
"sp-io/std",
"fp-evm/std",
"evm/std",
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
// SPDX-License-Identifier: Apache-2.0
// This file is part of Frontier.
//
// Copyright (c) 2020 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// The precomputed values for BLAKE2b [from the spec](https://tools.ietf.org/html/rfc7693#section-2.7)
/// There are 10 16-byte arrays - one for each round
/// the entries are calculated from the sigma constants.
Expand Down
95 changes: 95 additions & 0 deletions frame/evm/precompile/blake2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: Apache-2.0
// This file is part of Frontier.
//
// Copyright (c) 2020 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

extern crate alloc;

mod eip_152;

use alloc::vec::Vec;
use core::mem::size_of;
use fp_evm::LinearCostPrecompile;
use evm::{ExitSucceed, ExitError};

pub struct Blake2F;

impl LinearCostPrecompile for Blake2F {
const BASE: usize = 15;
const WORD: usize = 3;

/// Format of `input`:
/// [4 bytes for rounds][64 bytes for h][128 bytes for m][8 bytes for t_0][8 bytes for t_1][1 byte for f]
fn execute(
input: &[u8],
_: usize,
) -> core::result::Result<(ExitSucceed, Vec<u8>), ExitError> {
const BLAKE2_F_ARG_LEN: usize = 213;

if input.len() != BLAKE2_F_ARG_LEN {
return Err(ExitError::Other("input length for Blake2 F precompile should be exactly 213 bytes".into()));
}

let mut rounds_buf: [u8; 4] = [0; 4];
rounds_buf.copy_from_slice(&input[0..4]);
let rounds: u32 = u32::from_le_bytes(rounds_buf);

let mut h_buf: [u8; 64] = [0; 64];
h_buf.copy_from_slice(&input[4..48]);
let mut h = [0u64; 8];
let mut ctr = 0;
for state_word in &mut h {
let mut temp: [u8; 8] = Default::default();
temp.copy_from_slice(&h_buf[(ctr + 8)..(ctr + 1) * 8]);
*state_word = u64::from_le_bytes(temp).into();
ctr += 1;
}

let mut m_buf: [u8; 128] = [0; 128];
m_buf.copy_from_slice(&input[68..196]);
let mut m = [0u64; 16];
ctr = 0;
for msg_word in &mut m {
let mut temp: [u8; 8] = Default::default();
temp.copy_from_slice(&m_buf[(ctr + 8)..(ctr + 1) * 8]);
*msg_word = u64::from_le_bytes(temp).into();
ctr += 1;
}


let mut t_0_buf: [u8; 8] = [0; 8];
t_0_buf.copy_from_slice(&input[196..204]);
let t_0 = u64::from_le_bytes(t_0_buf);

let mut t_1_buf: [u8; 8] = [0; 8];
t_1_buf.copy_from_slice(&input[204..212]);
let t_1 = u64::from_le_bytes(t_1_buf);

let f = if input[212] == 1 { true } else if input[212] == 0 { false } else {
return Err(ExitError::Other("incorrect final block indicator flag".into()))
};

crate::eip_152::compress(&mut h, m, [t_0.into(), t_1.into()], f, rounds as usize);

let mut output_buf = [0u8; 8 * size_of::<u64>()];
for (i, state_word) in h.iter().enumerate() {
output_buf[i * 8..(i + 1) * 8].copy_from_slice(&state_word.to_le_bytes());
}

Ok((ExitSucceed::Returned, output_buf.to_vec()))
}
}
25 changes: 25 additions & 0 deletions frame/evm/precompile/bn128/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "pallet-evm-precompile-bn128"
version = "2.0.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
description = "BN128 precompiles for EVM pallet."

[dependencies]
sp-core = { version = "2.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-io = { version = "2.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
fp-evm = { version = "0.8.0", default-features = false, path = "../../../../primitives/evm" }
evm = { version = "0.19.0", default-features = false, features = ["with-codec"] }
bn = { package = "substrate-bn", version = "0.5", default-features = false }

[features]
default = ["std"]
std = [
"sp-core/std",
"sp-io/std",
"fp-evm/std",
"evm/std",
]
Loading

0 comments on commit 23cdfb5

Please sign in to comment.