From 57974fb4198a1dae4fee8e2a473ba4caa0d4c5f3 Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Wed, 4 Jan 2023 10:18:27 +0000 Subject: [PATCH 1/9] pad input if too short --- frame/evm/precompile/bn128/src/lib.rs | 34 ++++++++++++--------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/frame/evm/precompile/bn128/src/lib.rs b/frame/evm/precompile/bn128/src/lib.rs index b620da7480..76cc9914f5 100644 --- a/frame/evm/precompile/bn128/src/lib.rs +++ b/frame/evm/precompile/bn128/src/lib.rs @@ -27,13 +27,11 @@ use fp_evm::{ use sp_core::U256; fn read_fr(input: &[u8], start_inx: usize) -> Result { - if input.len() < start_inx + 32 { - return Err(PrecompileFailure::Error { - exit_status: ExitError::Other("Input not long enough".into()), - }); - } + let mut buf = [0u8; 32]; + let mut input_iter = input[start_inx..].iter().copied(); + buf.fill_with(|| input_iter.next().unwrap_or(0)); - bn::Fr::from_slice(&input[start_inx..(start_inx + 32)]).map_err(|_| PrecompileFailure::Error { + bn::Fr::from_slice(&buf).map_err(|_| PrecompileFailure::Error { exit_status: ExitError::Other("Invalid field element".into()), }) } @@ -41,22 +39,20 @@ fn read_fr(input: &[u8], start_inx: usize) -> Result fn read_point(input: &[u8], start_inx: usize) -> Result { use bn::{AffineG1, Fq, Group, G1}; - if input.len() < start_inx + 64 { - return Err(PrecompileFailure::Error { - exit_status: ExitError::Other("Input not long enough".into()), - }); - } + let mut px_buf = [0u8; 32]; + let mut py_buf = [0u8; 32]; + let mut input_iter = input[start_inx..].iter().copied(); + px_buf.fill_with(|| input_iter.next().unwrap_or(0)); + py_buf.fill_with(|| input_iter.next().unwrap_or(0)); - let px = Fq::from_slice(&input[start_inx..(start_inx + 32)]).map_err(|_| { - PrecompileFailure::Error { - exit_status: ExitError::Other("Invalid point x coordinate".into()), - } + let px = Fq::from_slice(&px_buf).map_err(|_| PrecompileFailure::Error { + exit_status: ExitError::Other("Invalid point x coordinate".into()), })?; - let py = Fq::from_slice(&input[(start_inx + 32)..(start_inx + 64)]).map_err(|_| { - PrecompileFailure::Error { - exit_status: ExitError::Other("Invalid point y coordinate".into()), - } + + let py = Fq::from_slice(&py_buf).map_err(|_| PrecompileFailure::Error { + exit_status: ExitError::Other("Invalid point y coordinate".into()), })?; + Ok(if px == Fq::zero() && py == Fq::zero() { G1::zero() } else { From b9ca0070703a32bf159904e1635d5c34fc237403 Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Fri, 6 Jan 2023 10:19:58 +0000 Subject: [PATCH 2/9] pad input in modexp --- frame/evm/precompile/modexp/src/lib.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/frame/evm/precompile/modexp/src/lib.rs b/frame/evm/precompile/modexp/src/lib.rs index b94d985308..5d9c657da3 100644 --- a/frame/evm/precompile/modexp/src/lib.rs +++ b/frame/evm/precompile/modexp/src/lib.rs @@ -112,34 +112,33 @@ impl Precompile for Modexp { fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult { let input = handle.input(); - if input.len() < 96 { - return Err(PrecompileFailure::Error { - exit_status: ExitError::Other("input must contain at least 96 bytes".into()), - }); - }; + let base_len_buf = [0u8; 32]; + let exp_len_buf = [0u8; 32]; + let mod_len_buf = [0u8; 32]; + + let mut input_iter = input[start_inx..].iter().copied(); + base_len_buf.fill_with(|| input_iter.next().unwrap_or(0)); + exp_len_buf.fill_with(|| input_iter.next().unwrap_or(0)); + mod_len_buf.fill_with(|| input_iter.next().unwrap_or(0)); // reasonable assumption: this must fit within the Ethereum EVM's max stack size let max_size_big = BigUint::from_u32(1024).expect("can't create BigUint"); - let mut buf = [0; 32]; - buf.copy_from_slice(&input[0..32]); - let base_len_big = BigUint::from_bytes_be(&buf); + let base_len_big = BigUint::from_bytes_be(&base_len_buf); if base_len_big > max_size_big { return Err(PrecompileFailure::Error { exit_status: ExitError::Other("unreasonably large base length".into()), }); } - buf.copy_from_slice(&input[32..64]); - let exp_len_big = BigUint::from_bytes_be(&buf); + let exp_len_big = BigUint::from_bytes_be(&exp_len_buf); if exp_len_big > max_size_big { return Err(PrecompileFailure::Error { exit_status: ExitError::Other("unreasonably large exponent length".into()), }); } - buf.copy_from_slice(&input[64..96]); - let mod_len_big = BigUint::from_bytes_be(&buf); + let mod_len_big = BigUint::from_bytes_be(&mod_len_buf); if mod_len_big > max_size_big { return Err(PrecompileFailure::Error { exit_status: ExitError::Other("unreasonably large modulus length".into()), From 4d2ac734d728959d48916827474f8a543c92a5ac Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Fri, 6 Jan 2023 13:15:29 +0000 Subject: [PATCH 3/9] pad all input fields + fix wrong test --- frame/evm/precompile/modexp/src/lib.rs | 75 ++++++++++++-------------- 1 file changed, 33 insertions(+), 42 deletions(-) diff --git a/frame/evm/precompile/modexp/src/lib.rs b/frame/evm/precompile/modexp/src/lib.rs index 5d9c657da3..ce6bd2c51a 100644 --- a/frame/evm/precompile/modexp/src/lib.rs +++ b/frame/evm/precompile/modexp/src/lib.rs @@ -112,11 +112,13 @@ impl Precompile for Modexp { fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult { let input = handle.input(); - let base_len_buf = [0u8; 32]; - let exp_len_buf = [0u8; 32]; - let mod_len_buf = [0u8; 32]; + // Yellowpaper: whenever the input is too short, the missing bytes are + // considered to be zero. + let mut base_len_buf = [0u8; 32]; + let mut exp_len_buf = [0u8; 32]; + let mut mod_len_buf = [0u8; 32]; - let mut input_iter = input[start_inx..].iter().copied(); + let mut input_iter = input.iter().copied(); base_len_buf.fill_with(|| input_iter.next().unwrap_or(0)); exp_len_buf.fill_with(|| input_iter.next().unwrap_or(0)); mod_len_buf.fill_with(|| input_iter.next().unwrap_or(0)); @@ -150,11 +152,11 @@ impl Precompile for Modexp { let exp_len = exp_len_big.to_usize().expect("exp_len out of bounds"); let mod_len = mod_len_big.to_usize().expect("mod_len out of bounds"); - // input length should be at least 96 + user-specified length of base + exp + mod - let total_len = base_len + exp_len + mod_len + 96; - if input.len() < total_len { - return Err(PrecompileFailure::Error { - exit_status: ExitError::Other("insufficient input size".into()), + // if mod_len is 0 output must be empty + if mod_len == 0 { + return Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: vec![], }); } @@ -164,21 +166,23 @@ impl Precompile for Modexp { BigUint::zero() } else { // read the numbers themselves. - let base_start = 96; // previous 3 32-byte fields - let base = BigUint::from_bytes_be(&input[base_start..base_start + base_len]); + let mut base_buf = vec![0u8; base_len]; + base_buf.fill_with(|| input_iter.next().unwrap_or(0)); + let base = BigUint::from_bytes_be(&base_buf); - let exp_start = base_start + base_len; - let exponent = BigUint::from_bytes_be(&input[exp_start..exp_start + exp_len]); + let mut exp_buf = vec![0u8; exp_len]; + exp_buf.fill_with(|| input_iter.next().unwrap_or(0)); + let exponent = BigUint::from_bytes_be(&exp_buf); + + let mut mod_buf = vec![0u8; mod_len]; + mod_buf.fill_with(|| input_iter.next().unwrap_or(0)); + let modulus = BigUint::from_bytes_be(&mod_buf); // do our gas accounting let gas_cost = calculate_gas_cost(base_len as u64, exp_len as u64, mod_len as u64, &exponent); handle.record_cost(gas_cost)?; - let input = handle.input(); - - let mod_start = exp_start + exp_len; - let modulus = BigUint::from_bytes_be(&input[mod_start..mod_start + mod_len]); if modulus.is_zero() || modulus.is_one() { BigUint::zero() @@ -197,7 +201,7 @@ impl Precompile for Modexp { exit_status: ExitSucceed::Returned, output: bytes.to_vec(), }) - } else if bytes.len() < mod_len { + } else if dbg!(bytes.len()) < mod_len { let mut ret = Vec::with_capacity(mod_len); ret.extend(core::iter::repeat(0).take(mod_len - bytes.len())); ret.extend_from_slice(&bytes[..]); @@ -227,7 +231,7 @@ mod tests { } #[test] - fn test_empty_input() -> Result<(), PrecompileFailure> { + fn test_empty_input() { let input = Vec::new(); let cost: u64 = 1; @@ -241,25 +245,17 @@ mod tests { let mut handle = MockHandle::new(input, Some(cost), context); match Modexp::execute(&mut handle) { - Ok(_) => { - panic!("Test not expected to pass"); + Ok(precompile_result) => { + assert_eq!(precompile_result.output.len(), 0); } - Err(e) => { - assert_eq!( - e, - PrecompileFailure::Error { - exit_status: ExitError::Other( - "input must contain at least 96 bytes".into() - ) - } - ); - Ok(()) + Err(_) => { + panic!("Modexp::execute() returned error"); // TODO: how to pass error on? } } } #[test] - fn test_insufficient_input() -> Result<(), PrecompileFailure> { + fn test_insufficient_input() { let input = hex::decode( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ @@ -278,17 +274,12 @@ mod tests { let mut handle = MockHandle::new(input, Some(cost), context); match Modexp::execute(&mut handle) { - Ok(_) => { - panic!("Test not expected to pass"); + Ok(precompile_result) => { + assert_eq!(precompile_result.output.len(), 1); + assert_eq!(precompile_result.output, vec![0x00]); } - Err(e) => { - assert_eq!( - e, - PrecompileFailure::Error { - exit_status: ExitError::Other("insufficient input size".into()) - } - ); - Ok(()) + Err(_) => { + panic!("Modexp::execute() returned error"); // TODO: how to pass error on? } } } From fb5d3d9863bc4d1d02938dd67adb528ac1a53f2d Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Fri, 6 Jan 2023 14:04:02 +0000 Subject: [PATCH 4/9] fix modexp gas cost --- frame/evm/precompile/modexp/src/lib.rs | 64 +++++++++++++++++++++----- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/frame/evm/precompile/modexp/src/lib.rs b/frame/evm/precompile/modexp/src/lib.rs index ce6bd2c51a..494e0608c3 100644 --- a/frame/evm/precompile/modexp/src/lib.rs +++ b/frame/evm/precompile/modexp/src/lib.rs @@ -21,7 +21,7 @@ extern crate alloc; use alloc::vec::Vec; -use core::{cmp::max, ops::BitAnd}; +use core::cmp::max; use num::{BigUint, FromPrimitive, One, ToPrimitive, Zero}; @@ -41,6 +41,7 @@ fn calculate_gas_cost( exp_length: u64, mod_length: u64, exponent: &BigUint, + exponent_bytes: &[u8], ) -> u64 { fn calculate_multiplication_complexity(base_length: u64, mod_length: u64) -> u64 { let max_length = max(base_length, mod_length); @@ -56,7 +57,11 @@ fn calculate_gas_cost( words * words } - fn calculate_iteration_count(exp_length: u64, exponent: &BigUint) -> u64 { + fn calculate_iteration_count( + exp_length: u64, + exponent: &BigUint, + exponent_bytes: &[u8], + ) -> u64 { let mut iteration_count: u64 = 0; if exp_length <= 32 && exponent.is_zero() { @@ -64,10 +69,6 @@ fn calculate_gas_cost( } else if exp_length <= 32 { iteration_count = exponent.bits() - 1; } else if exp_length > 32 { - // construct BigUint to represent (2^256) - 1 - let bytes: [u8; 32] = [0xFF; 32]; - let max_256_bit_uint = BigUint::from_bytes_be(&bytes); - // from the EIP spec: // (8 * (exp_length - 32)) + ((exponent & (2**256 - 1)).bit_length() - 1) // @@ -77,15 +78,19 @@ fn calculate_gas_cost( // must be > 0) // * the addition can't overflow because the terms are both capped at roughly // 8 * max size of exp_length (1024) - iteration_count = - (8 * (exp_length - 32)) + exponent.bitand(max_256_bit_uint).bits() - 1; + // * the EIP spec is written in python, in which (exponent & (2**256 - 1)) takes the + // FIRST 32 bytes. However this `BigUint` `&` operator takes the LAST 32 bytes. + // We thus instead take the bytes manually. + let exponent_head = BigUint::from_bytes_be(&exponent_bytes[..32]); + + iteration_count = (8 * (exp_length - 32)) + exponent_head.bits() - 1; } max(iteration_count, 1) } let multiplication_complexity = calculate_multiplication_complexity(base_length, mod_length); - let iteration_count = calculate_iteration_count(exp_length, exponent); + let iteration_count = calculate_iteration_count(exp_length, exponent, exponent_bytes); max( MIN_GAS_COST, multiplication_complexity * iteration_count / 3, @@ -179,8 +184,13 @@ impl Precompile for Modexp { let modulus = BigUint::from_bytes_be(&mod_buf); // do our gas accounting - let gas_cost = - calculate_gas_cost(base_len as u64, exp_len as u64, mod_len as u64, &exponent); + let gas_cost = calculate_gas_cost( + base_len as u64, + exp_len as u64, + mod_len as u64, + &exponent, + &exp_buf, + ); handle.record_cost(gas_cost)?; @@ -464,4 +474,36 @@ mod tests { let expected = BigUint::parse_bytes(b"0", 10).unwrap(); assert_eq!(result, expected); } + + #[test] + fn test_long_exp_gas_cost_matches_specs() { + let input = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 16, 0, 0, 0, 255, 255, 255, 2, 0, 0, 179, 0, 0, 2, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 255, 251, 0, 0, 0, 0, 4, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 255, 255, 255, 2, 0, 0, 179, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, + 255, 255, 255, 249, + ]; + + let context: Context = Context { + address: Default::default(), + caller: Default::default(), + apparent_value: From::from(0), + }; + + let mut handle = MockHandle::new(input, Some(100_000), context); + + let _ = Modexp::execute(&mut handle).expect("Modexp::execute() returned error"); + + assert_eq!(handle.gas_used, 7104); // gas used when ran in geth + } } From d57800bfafb1aaa22712e9d3b6d0e04be971e648 Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Fri, 6 Jan 2023 14:16:11 +0000 Subject: [PATCH 5/9] remove dbg! --- frame/evm/precompile/modexp/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/evm/precompile/modexp/src/lib.rs b/frame/evm/precompile/modexp/src/lib.rs index 494e0608c3..3976984d52 100644 --- a/frame/evm/precompile/modexp/src/lib.rs +++ b/frame/evm/precompile/modexp/src/lib.rs @@ -211,7 +211,7 @@ impl Precompile for Modexp { exit_status: ExitSucceed::Returned, output: bytes.to_vec(), }) - } else if dbg!(bytes.len()) < mod_len { + } else if bytes.len() < mod_len { let mut ret = Vec::with_capacity(mod_len); ret.extend(core::iter::repeat(0).take(mod_len - bytes.len())); ret.extend_from_slice(&bytes[..]); From 922fda0d5831fc7f9687d63351078694128101e3 Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Fri, 6 Jan 2023 17:52:31 +0000 Subject: [PATCH 6/9] remove redundant arg + import vec! --- frame/evm/precompile/modexp/src/lib.rs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/frame/evm/precompile/modexp/src/lib.rs b/frame/evm/precompile/modexp/src/lib.rs index 3976984d52..de13c9c785 100644 --- a/frame/evm/precompile/modexp/src/lib.rs +++ b/frame/evm/precompile/modexp/src/lib.rs @@ -20,7 +20,7 @@ extern crate alloc; -use alloc::vec::Vec; +use alloc::{vec, vec::Vec}; use core::cmp::max; use num::{BigUint, FromPrimitive, One, ToPrimitive, Zero}; @@ -38,7 +38,6 @@ const MIN_GAS_COST: u64 = 200; // https://eips.ethereum.org/EIPS/eip-2565 fn calculate_gas_cost( base_length: u64, - exp_length: u64, mod_length: u64, exponent: &BigUint, exponent_bytes: &[u8], @@ -57,12 +56,9 @@ fn calculate_gas_cost( words * words } - fn calculate_iteration_count( - exp_length: u64, - exponent: &BigUint, - exponent_bytes: &[u8], - ) -> u64 { + fn calculate_iteration_count(exponent: &BigUint, exponent_bytes: &[u8]) -> u64 { let mut iteration_count: u64 = 0; + let exp_length = exponent_bytes.len() as u64; if exp_length <= 32 && exponent.is_zero() { iteration_count = 0; @@ -90,7 +86,7 @@ fn calculate_gas_cost( } let multiplication_complexity = calculate_multiplication_complexity(base_length, mod_length); - let iteration_count = calculate_iteration_count(exp_length, exponent, exponent_bytes); + let iteration_count = calculate_iteration_count(exponent, exponent_bytes); max( MIN_GAS_COST, multiplication_complexity * iteration_count / 3, @@ -184,13 +180,7 @@ impl Precompile for Modexp { let modulus = BigUint::from_bytes_be(&mod_buf); // do our gas accounting - let gas_cost = calculate_gas_cost( - base_len as u64, - exp_len as u64, - mod_len as u64, - &exponent, - &exp_buf, - ); + let gas_cost = calculate_gas_cost(base_len as u64, mod_len as u64, &exponent, &exp_buf); handle.record_cost(gas_cost)?; From 1ef1d6dff19bb6b01dfd73becbb5104e34a98c1a Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Mon, 9 Jan 2023 09:55:35 +0000 Subject: [PATCH 7/9] use copy_from_slice --- frame/evm/precompile/bn128/src/lib.rs | 20 ++++++++++++---- frame/evm/precompile/modexp/src/lib.rs | 32 +++++++++++++++++++------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/frame/evm/precompile/bn128/src/lib.rs b/frame/evm/precompile/bn128/src/lib.rs index 76cc9914f5..4cb8b37fb1 100644 --- a/frame/evm/precompile/bn128/src/lib.rs +++ b/frame/evm/precompile/bn128/src/lib.rs @@ -26,10 +26,21 @@ use fp_evm::{ }; use sp_core::U256; +/// Copy bytes from input to target. +fn read_input(source: &[u8], target: &mut [u8], offset: usize) { + // Out of bounds, nothing to copy. + if source.len() <= offset { + return; + } + + // Find len to copy up to target len, but not out of bounds. + let len = core::cmp::min(target.len(), source.len() - offset); + target[..len].copy_from_slice(&source[offset..][..len]); +} + fn read_fr(input: &[u8], start_inx: usize) -> Result { let mut buf = [0u8; 32]; - let mut input_iter = input[start_inx..].iter().copied(); - buf.fill_with(|| input_iter.next().unwrap_or(0)); + read_input(&input, &mut buf, start_inx); bn::Fr::from_slice(&buf).map_err(|_| PrecompileFailure::Error { exit_status: ExitError::Other("Invalid field element".into()), @@ -41,9 +52,8 @@ fn read_point(input: &[u8], start_inx: usize) -> Result PrecompileResult { let input = handle.input(); + let mut input_offset = 0; // Yellowpaper: whenever the input is too short, the missing bytes are // considered to be zero. let mut base_len_buf = [0u8; 32]; + read_input(&input, &mut base_len_buf, &mut input_offset); let mut exp_len_buf = [0u8; 32]; + read_input(&input, &mut exp_len_buf, &mut input_offset); let mut mod_len_buf = [0u8; 32]; - - let mut input_iter = input.iter().copied(); - base_len_buf.fill_with(|| input_iter.next().unwrap_or(0)); - exp_len_buf.fill_with(|| input_iter.next().unwrap_or(0)); - mod_len_buf.fill_with(|| input_iter.next().unwrap_or(0)); + read_input(&input, &mut mod_len_buf, &mut input_offset); // reasonable assumption: this must fit within the Ethereum EVM's max stack size let max_size_big = BigUint::from_u32(1024).expect("can't create BigUint"); @@ -168,15 +184,15 @@ impl Precompile for Modexp { } else { // read the numbers themselves. let mut base_buf = vec![0u8; base_len]; - base_buf.fill_with(|| input_iter.next().unwrap_or(0)); + read_input(&input, &mut base_buf, &mut input_offset); let base = BigUint::from_bytes_be(&base_buf); let mut exp_buf = vec![0u8; exp_len]; - exp_buf.fill_with(|| input_iter.next().unwrap_or(0)); + read_input(&input, &mut exp_buf, &mut input_offset); let exponent = BigUint::from_bytes_be(&exp_buf); let mut mod_buf = vec![0u8; mod_len]; - mod_buf.fill_with(|| input_iter.next().unwrap_or(0)); + read_input(&input, &mut mod_buf, &mut input_offset); let modulus = BigUint::from_bytes_be(&mod_buf); // do our gas accounting From 634812a86eeb78f568fea9622a5d045dd5c41166 Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Mon, 9 Jan 2023 14:43:10 +0000 Subject: [PATCH 8/9] clippy --- frame/evm/precompile/modexp/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/frame/evm/precompile/modexp/src/lib.rs b/frame/evm/precompile/modexp/src/lib.rs index 23aa8639eb..6932df40ab 100644 --- a/frame/evm/precompile/modexp/src/lib.rs +++ b/frame/evm/precompile/modexp/src/lib.rs @@ -134,11 +134,11 @@ impl Precompile for Modexp { // Yellowpaper: whenever the input is too short, the missing bytes are // considered to be zero. let mut base_len_buf = [0u8; 32]; - read_input(&input, &mut base_len_buf, &mut input_offset); + read_input(input, &mut base_len_buf, &mut input_offset); let mut exp_len_buf = [0u8; 32]; - read_input(&input, &mut exp_len_buf, &mut input_offset); + read_input(input, &mut exp_len_buf, &mut input_offset); let mut mod_len_buf = [0u8; 32]; - read_input(&input, &mut mod_len_buf, &mut input_offset); + read_input(input, &mut mod_len_buf, &mut input_offset); // reasonable assumption: this must fit within the Ethereum EVM's max stack size let max_size_big = BigUint::from_u32(1024).expect("can't create BigUint"); @@ -184,15 +184,15 @@ impl Precompile for Modexp { } else { // read the numbers themselves. let mut base_buf = vec![0u8; base_len]; - read_input(&input, &mut base_buf, &mut input_offset); + read_input(input, &mut base_buf, &mut input_offset); let base = BigUint::from_bytes_be(&base_buf); let mut exp_buf = vec![0u8; exp_len]; - read_input(&input, &mut exp_buf, &mut input_offset); + read_input(input, &mut exp_buf, &mut input_offset); let exponent = BigUint::from_bytes_be(&exp_buf); let mut mod_buf = vec![0u8; mod_len]; - read_input(&input, &mut mod_buf, &mut input_offset); + read_input(input, &mut mod_buf, &mut input_offset); let modulus = BigUint::from_bytes_be(&mod_buf); // do our gas accounting From b9703c3d73a00679e71ddc2221e04ce37449f2df Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Thu, 12 Jan 2023 10:33:39 +0000 Subject: [PATCH 9/9] clippy --- frame/evm/precompile/bn128/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/evm/precompile/bn128/src/lib.rs b/frame/evm/precompile/bn128/src/lib.rs index 4cb8b37fb1..beba8a0be6 100644 --- a/frame/evm/precompile/bn128/src/lib.rs +++ b/frame/evm/precompile/bn128/src/lib.rs @@ -40,7 +40,7 @@ fn read_input(source: &[u8], target: &mut [u8], offset: usize) { fn read_fr(input: &[u8], start_inx: usize) -> Result { let mut buf = [0u8; 32]; - read_input(&input, &mut buf, start_inx); + read_input(input, &mut buf, start_inx); bn::Fr::from_slice(&buf).map_err(|_| PrecompileFailure::Error { exit_status: ExitError::Other("Invalid field element".into()), @@ -52,8 +52,8 @@ fn read_point(input: &[u8], start_inx: usize) -> Result