Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blake2 precompile to runtimes #935

Merged
merged 7 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions runtime/moonbase/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pallet-evm-precompile-dispatch = { git = "https://github.com/purestake/frontier"
pallet-evm-precompile-modexp = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-simple = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-sha3fips = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-blake2 = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-balances-erc20 = { path = "../../precompiles/balances-erc20", default-features = false }
pallet-evm-precompile-assets-erc20 = { path = "../../precompiles/assets-erc20", default-features = false }
xtokens-precompiles = { path = "../../precompiles/xtokens", default-features = false }
Expand Down
10 changes: 7 additions & 3 deletions runtime/moonbase/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use pallet_democracy_precompiles::DemocracyWrapper;
use pallet_evm::{AddressMapping, Precompile, PrecompileSet};
use pallet_evm_precompile_assets_erc20::Erc20AssetsPrecompileSet;
use pallet_evm_precompile_balances_erc20::{Erc20BalancesPrecompile, Erc20Metadata};
use pallet_evm_precompile_blake2::Blake2F;
use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
use pallet_evm_precompile_dispatch::Dispatch;
use pallet_evm_precompile_modexp::Modexp;
Expand Down Expand Up @@ -72,9 +73,11 @@ where
/// Return all addresses that contain precompiles. This can be used to populate dummy code
/// under the precompile.
pub fn used_addresses() -> impl Iterator<Item = R::AccountId> {
sp_std::vec![1, 2, 3, 4, 5, 6, 7, 8, 1024, 1025, 1026, 2048, 2049, 2050, 2051, 2052, 2053]
.into_iter()
.map(|x| R::AddressMapping::into_account_id(hash(x)))
sp_std::vec![
1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 2048, 2049, 2050, 2051, 2052, 2053
]
.into_iter()
.map(|x| R::AddressMapping::into_account_id(hash(x)))
}
}

Expand Down Expand Up @@ -108,6 +111,7 @@ where
a if a == hash(6) => Some(Bn128Add::execute(input, target_gas, context)),
a if a == hash(7) => Some(Bn128Mul::execute(input, target_gas, context)),
a if a == hash(8) => Some(Bn128Pairing::execute(input, target_gas, context)),
a if a == hash(9) => Some(Blake2F::execute(input, target_gas, context)),
// Non-Moonbeam specific nor Ethereum precompiles :
a if a == hash(1024) => Some(Sha3FIPS256::execute(input, target_gas, context)),
a if a == hash(1025) => Some(Dispatch::<R>::execute(input, target_gas, context)),
Expand Down
1 change: 1 addition & 0 deletions runtime/moonbeam/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pallet-evm-precompile-dispatch = { git = "https://github.com/purestake/frontier"
pallet-evm-precompile-modexp = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-simple = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-sha3fips = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-blake2 = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }

# Substrate dependencies
sp-std = { git = "https://github.com/purestake/substrate", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
Expand Down
4 changes: 3 additions & 1 deletion runtime/moonbeam/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use crowdloan_rewards_precompiles::CrowdloanRewardsWrapper;
use evm::{executor::PrecompileOutput, Context, ExitError};
use pallet_evm::{AddressMapping, Precompile, PrecompileSet};
use pallet_evm_precompile_blake2::Blake2F;
use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
use pallet_evm_precompile_dispatch::Dispatch;
use pallet_evm_precompile_modexp::Modexp;
Expand All @@ -41,7 +42,7 @@ where
/// Return all addresses that contain precompiles. This can be used to populate dummy code
/// under the precompile.
pub fn used_addresses() -> impl Iterator<Item = R::AccountId> {
sp_std::vec![1, 2, 3, 4, 5, 6, 7, 8, 1024, 1025, 1026, 2048, 2049]
sp_std::vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 2048, 2049]
.into_iter()
.map(|x| R::AddressMapping::into_account_id(hash(x)))
}
Expand Down Expand Up @@ -73,6 +74,7 @@ where
a if a == hash(6) => Some(Bn128Add::execute(input, target_gas, context)),
a if a == hash(7) => Some(Bn128Mul::execute(input, target_gas, context)),
a if a == hash(8) => Some(Bn128Pairing::execute(input, target_gas, context)),
a if a == hash(9) => Some(Blake2F::execute(input, target_gas, context)),
// Non-Moonbeam specific nor Ethereum precompiles :
a if a == hash(1024) => Some(Sha3FIPS256::execute(input, target_gas, context)),
a if a == hash(1025) => Some(Dispatch::<R>::execute(input, target_gas, context)),
Expand Down
1 change: 1 addition & 0 deletions runtime/moonriver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pallet-evm-precompile-dispatch = { git = "https://github.com/purestake/frontier"
pallet-evm-precompile-modexp = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-simple = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-sha3fips = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
pallet-evm-precompile-blake2 = { git = "https://github.com/purestake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }

# Substrate dependencies
sp-std = { git = "https://github.com/purestake/substrate", default-features = false, branch = "moonbeam-polkadot-v0.9.11" }
Expand Down
4 changes: 3 additions & 1 deletion runtime/moonriver/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use crowdloan_rewards_precompiles::CrowdloanRewardsWrapper;
use evm::{executor::PrecompileOutput, Context, ExitError};
use pallet_evm::{AddressMapping, Precompile, PrecompileSet};
use pallet_evm_precompile_blake2::Blake2F;
use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
use pallet_evm_precompile_dispatch::Dispatch;
use pallet_evm_precompile_modexp::Modexp;
Expand All @@ -40,7 +41,7 @@ where
/// Return all addresses that contain precompiles. This can be used to populate dummy code
/// under the precompile.
pub fn used_addresses() -> impl Iterator<Item = R::AccountId> {
sp_std::vec![1, 2, 3, 4, 5, 6, 7, 8, 1024, 1025, 1026, 2048, 2049]
sp_std::vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 2048, 2049]
.into_iter()
.map(|x| R::AddressMapping::into_account_id(hash(x)))
}
Expand Down Expand Up @@ -72,6 +73,7 @@ where
a if a == hash(6) => Some(Bn128Add::execute(input, target_gas, context)),
a if a == hash(7) => Some(Bn128Mul::execute(input, target_gas, context)),
a if a == hash(8) => Some(Bn128Pairing::execute(input, target_gas, context)),
a if a == hash(9) => Some(Blake2F::execute(input, target_gas, context)),
// Non-Moonbeam specific nor Ethereum precompiles :
a if a == hash(1024) => Some(Sha3FIPS256::execute(input, target_gas, context)),
a if a == hash(1025) => Some(Dispatch::<R>::execute(input, target_gas, context)),
Expand Down
13,485 changes: 13,485 additions & 0 deletions tests/contracts/compiled/Blake2Check.json

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions tests/contracts/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,4 +839,58 @@ export const contractSources: { [key: string]: string } = {
);
}
}`,
// Blake2Check contract used to test blake2 precompile at address 0x9
// source: https://eips.ethereum.org/EIPS/eip-152#example-usage-in-solidity
Blake2Check: `
pragma solidity >=0.8.0;
contract Blake2Check {
function F(
uint32 rounds,
bytes32[2] memory h,
bytes32[4] memory m,
bytes8[2] memory t,
bool f
) public view returns (bytes32[2] memory) {
bytes32[2] memory output;
bytes memory args =
abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f);
assembly {
if iszero(staticcall(not(0), 0x09, add(args, 32), 0xd5, output, 0x40)) {
revert(0, 0)
}
}
return output;
}
function callF() public view returns (bytes32[2] memory) {
uint32 rounds = 12;
bytes32[2] memory h;
h[0] = hex"48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5";
h[1] = hex"d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b";
bytes32[4] memory m;
m[0] = hex"6162630000000000000000000000000000000000000000000000000000000000";
m[1] = hex"0000000000000000000000000000000000000000000000000000000000000000";
m[2] = hex"0000000000000000000000000000000000000000000000000000000000000000";
m[3] = hex"0000000000000000000000000000000000000000000000000000000000000000";
bytes8[2] memory t;
t[0] = hex"03000000";
t[1] = hex"00000000";
bool f = true;
// Expected output:
// ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1
// 7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923
return F(rounds, h, m, t, f);
}
}`,
};
21 changes: 21 additions & 0 deletions tests/tests/test-precompile-blake2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from "chai";
import { describeDevMoonbeam } from "../util/setup-dev-tests";
import { createContract } from "../util/transactions";

describeDevMoonbeam("Precompiles - Blake2", (context) => {
it("should be accessible from a smart contract", async function () {
const { contract, rawTx } = await createContract(context.web3, "Blake2Check");
const { txResults } = await context.createBlock({ transactions: [rawTx] });

// The contract should deploy successfully and the receipt should show success.
const receipt = await context.web3.eth.getTransactionReceipt(txResults[0].result);
expect(receipt.status).to.be.true;

// invoke the contract's test function 'callF'
const result = await contract.methods.callF().call();
expect(result).to.have.members([
"0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1",
"0x7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923",
]);
});
});