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

Fix modexp record gas #1290

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
67 changes: 38 additions & 29 deletions frame/evm/precompile/modexp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,6 @@ 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");

// if mod_len is 0 output must be empty
if mod_len == 0 {
return Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
output: vec![],
});
}

// Gas formula allows arbitrary large exp_len when base and modulus are empty, so we need to handle empty base first.
let r = if base_len == 0 && mod_len == 0 {
handle.record_cost(MIN_GAS_COST)?;
Expand Down Expand Up @@ -235,8 +227,9 @@ impl Precompile for Modexp {
output: ret.to_vec(),
})
} else {
Err(PrecompileFailure::Error {
exit_status: ExitError::Other("failed".into()),
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
output: vec![],
})
Comment on lines 229 to 233
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case seems unclear to me, it's basically when bytes.len() > mod_len, which shouldn't be possible except for this case of mod_len == 0. Maybe this is better written by handling that case specifically rather than assume it is the case in the final catch-all } else { block.

Also see the comment above this (line ~ 215) which is relevant:

// always true except in the case of zero-length modulus, which leads to
// output of length and value 1.

This seems to contradict the expectation of returning an empty vec.

}
}
Expand All @@ -255,11 +248,35 @@ mod tests {
Ok(())
}

#[test]
fn test_min_gas() {
let context: Context = Context {
address: Default::default(),
caller: Default::default(),
apparent_value: From::from(0),
};

assert_eq!(
Modexp::execute(&mut MockHandle::new(vec![], Some(199), context.clone())),
Err(PrecompileFailure::Error {
exit_status: ExitError::OutOfGas,
})
);

assert_eq!(
Modexp::execute(&mut MockHandle::new(vec![], Some(200), context)),
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
output: vec![],
})
);
}

#[test]
fn test_empty_input() {
let input = Vec::new();

let cost: u64 = 1;
let cost: u64 = 200;

let context: Context = Context {
address: Default::default(),
Expand Down Expand Up @@ -288,7 +305,7 @@ mod tests {
)
.expect("Decode failed");

let cost: u64 = 1;
let cost: u64 = 10000;

let context: Context = Context {
address: Default::default(),
Expand All @@ -310,15 +327,15 @@ mod tests {
}

#[test]
fn test_excessive_input() -> Result<(), PrecompileFailure> {
fn test_excessive_input() {
let input = hex::decode(
"1000000000000000000000000000000000000000000000000000000000000001\
0000000000000000000000000000000000000000000000000000000000000001\
0000000000000000000000000000000000000000000000000000000000000001",
)
.expect("Decode failed");

let cost: u64 = 1;
let cost: u64 = 200;

let context: Context = Context {
address: Default::default(),
Expand All @@ -328,20 +345,12 @@ mod tests {

let mut handle = MockHandle::new(input, Some(cost), context);

match Modexp::execute(&mut handle) {
Ok(_) => {
panic!("Test not expected to pass");
}
Err(e) => {
assert_eq!(
e,
PrecompileFailure::Error {
exit_status: ExitError::Other("unreasonably large base length".into())
}
);
Ok(())
}
}
assert_eq!(
Modexp::execute(&mut handle),
Err(PrecompileFailure::Error {
exit_status: ExitError::Other("unreasonably large base length".into())
})
);
}

#[test]
Expand Down Expand Up @@ -515,7 +524,7 @@ mod tests {
apparent_value: From::from(0),
};

let mut handle = MockHandle::new(input, Some(100_000), context);
let mut handle = MockHandle::new(input, Some(1_000_000), context);

let _ = Modexp::execute(&mut handle).expect("Modexp::execute() returned error");

Expand Down
10 changes: 8 additions & 2 deletions frame/evm/test-vector-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ impl PrecompileHandle for MockHandle {

fn record_cost(&mut self, cost: u64) -> Result<(), ExitError> {
self.gas_used += cost;

if let Some(gas_limit) = self.gas_limit {
if self.gas_used > gas_limit {
return Err(ExitError::OutOfGas);
}
}
Ok(())
}

Expand Down Expand Up @@ -132,7 +138,7 @@ pub fn test_precompile_test_vectors<P: Precompile>(filepath: &str) -> Result<(),
for test in tests {
let input: Vec<u8> = hex::decode(test.input).expect("Could not hex-decode test input data");

let cost: u64 = 10000000;
let cost: u64 = 100000000;

let context: Context = Context {
address: Default::default(),
Expand Down Expand Up @@ -184,7 +190,7 @@ pub fn test_precompile_failure_test_vectors<P: Precompile>(filepath: &str) -> Re
for test in tests {
let input: Vec<u8> = hex::decode(test.input).expect("Could not hex-decode test input data");

let cost: u64 = 10000000;
let cost: u64 = 100000000;

let context: Context = Context {
address: Default::default(),
Expand Down
Loading