Skip to content

Commit

Permalink
update ecrecover ref: polkadot-evm/frontier#964
Browse files Browse the repository at this point in the history
  • Loading branch information
zjb0807 committed Jan 27, 2024
1 parent f0013a4 commit 047de87
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 2 additions & 0 deletions modules/evm/src/precompiles/blake2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Precompile for Blake2F {
const BLAKE2_F_ARG_LEN: usize = 213;

let input = handle.input();

if input.len() != BLAKE2_F_ARG_LEN {
return Err(PrecompileFailure::Error {
exit_status: ExitError::Other(
Expand All @@ -50,6 +51,7 @@ impl Precompile for Blake2F {

let gas_cost: u64 = (rounds as u64) * Blake2F::GAS_COST_PER_ROUND;
handle.record_cost(gas_cost)?;

let input = handle.input();

// we use from_le_bytes below to effectively swap byte order to LE if architecture is BE
Expand Down
18 changes: 9 additions & 9 deletions modules/evm/src/precompiles/ecrecover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ impl LinearCostPrecompile for ECRecover {
let mut sig = [0u8; 65];

msg[0..32].copy_from_slice(&input[0..32]);
sig[0..32].copy_from_slice(&input[64..96]);
sig[32..64].copy_from_slice(&input[96..128]);

sig[64] = match input[63] {
v if v > 26 && input[32..63] == [0; 31] => v - 27,
_ => {
return Ok((ExitSucceed::Returned, [0u8; 0].to_vec()));
}
};
sig[0..32].copy_from_slice(&input[64..96]); // r
sig[32..64].copy_from_slice(&input[96..128]); // s
sig[64] = input[63]; // v

// v can only be 27 or 28 on the full 32 bytes value.
// https://github.com/ethereum/go-ethereum/blob/a907d7e81aaeea15d80b2d3209ad8e08e3bf49e0/core/vm/contracts.go#L177
if input[32..63] != [0u8; 31] || ![27, 28].contains(&input[63]) {
return Ok((ExitSucceed::Returned, [0u8; 0].to_vec()));
}

let result = match sp_io::crypto::secp256k1_ecdsa_recover(&sig, &msg) {
Ok(pubkey) => {
Expand Down

0 comments on commit 047de87

Please sign in to comment.