referrerpolicy=no-referrer-when-downgrade

pallet_revive/precompiles/builtin/
ecrecover.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::{
19	precompiles::{BuiltinAddressMatcher, Error, Ext, PrimitivePrecompile},
20	vm::RuntimeCosts,
21	Config,
22};
23use alloc::vec::Vec;
24use core::{marker::PhantomData, num::NonZero};
25
26pub struct EcRecover<T>(PhantomData<T>);
27
28impl<T: Config> PrimitivePrecompile for EcRecover<T> {
29	type T = T;
30	const MATCHER: BuiltinAddressMatcher = BuiltinAddressMatcher::Fixed(NonZero::new(1).unwrap());
31	const HAS_CONTRACT_INFO: bool = false;
32
33	fn call(
34		_address: &[u8; 20],
35		i: Vec<u8>,
36		env: &mut impl Ext<T = Self::T>,
37	) -> Result<Vec<u8>, Error> {
38		env.gas_meter_mut().charge(RuntimeCosts::EcdsaRecovery)?;
39		let mut input = [0u8; 128];
40		let len = i.len().min(128);
41		input[..len].copy_from_slice(&i[..len]);
42
43		let mut msg = [0u8; 32];
44		let mut sig = [0u8; 65];
45
46		msg[0..32].copy_from_slice(&input[0..32]);
47		sig[0..32].copy_from_slice(&input[64..96]); // r
48		sig[32..64].copy_from_slice(&input[96..128]); // s
49		sig[64] = input[63]; // v
50
51		// v can only be 27 or 28 on the full 32 bytes value.
52		// https://github.com/ethereum/go-ethereum/blob/a907d7e81aaeea15d80b2d3209ad8e08e3bf49e0/core/vm/contracts.go#L177
53		if input[32..63] != [0u8; 31] || ![27, 28].contains(&input[63]) {
54			return Ok(Vec::new());
55		}
56
57		let data = match sp_io::crypto::secp256k1_ecdsa_recover(&sig, &msg) {
58			Ok(pubkey) => {
59				let mut address = sp_io::hashing::keccak_256(&pubkey);
60				address[0..12].copy_from_slice(&[0u8; 12]);
61				address.to_vec()
62			},
63			Err(_) => Vec::new(),
64		};
65
66		Ok(data)
67	}
68}
69
70#[cfg(test)]
71mod tests {
72	use super::*;
73	use crate::{precompiles::tests::run_test_vectors, tests::Test};
74
75	#[test]
76	fn test_ecrecover() {
77		run_test_vectors::<EcRecover<Test>>(include_str!("./testdata/1-ecRecover.json"));
78	}
79}