Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

EIP198 and built-in activation #4926

Merged
merged 3 commits into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 36 additions & 5 deletions ethcore/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Pricer for Modexp {
let mod_len = read_len();

// floor(max(length_of_MODULUS, length_of_BASE) ** 2 * max(length_of_EXPONENT, 1) / GQUADDIVISOR)
// TODO: is saturating the best bahavior here?
// TODO: is saturating the best behavior here?
let m = max(mod_len, base_len);
match m.overflowing_mul(m) {
(_, true) => U256::max_value(),
Expand Down Expand Up @@ -260,10 +260,18 @@ impl Impl for ModexpImpl {

// calculate modexp: exponentiation by squaring.
fn modexp(mut base: BigUint, mut exp: BigUint, modulus: BigUint) -> BigUint {
if base == BigUint::zero() || modulus <= BigUint::one() { return BigUint::zero() }
match (base == BigUint::zero(), exp == BigUint::zero()) {
(_, true) => return BigUint::one(), // n^0 % m
(true, false) => return BigUint::zero(), // 0^n % m, n>0
(false, false) if modulus <= BigUint::one() => return BigUint::zero(), // a^b % 1 = 0.
_ => {}
}

let mut result = BigUint::one();
base = base % &modulus;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This may lead to base == 0 and we sould short circuit this case as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You could do this line at the very beginning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At the very least you have to check if the modulus is zero beforehand or the code will panic.


// fast path for base divisible by modulus.
if base == BigUint::zero() { return result }
while exp != BigUint::zero() {
// exp has to be on the right here to avoid move.
if BigUint::one() & &exp == BigUint::one() {
Expand All @@ -277,10 +285,15 @@ impl Impl for ModexpImpl {
result
}

// write output to given memory, left padded to same length as the modulus.
// write output to given memory, left padded and same length as the modulus.
let bytes = modexp(base, exp, modulus).to_bytes_be();
let res_start = mod_len - bytes.len();
output.write(res_start, &bytes);

// always true except in the case of zero-length modulus, which leads to
// output of length and value 1.
if bytes.len() <= mod_len {
let res_start = mod_len - bytes.len();
output.write(res_start, &bytes);
}
}
}

Expand Down Expand Up @@ -480,6 +493,24 @@ mod tests {
assert_eq!(output, expected);
assert_eq!(f.cost(&input[..]), expected_cost.into());
}

// zero-length modulus.
{
let input = FromHex::from_hex("\
0000000000000000000000000000000000000000000000000000000000000001\
0000000000000000000000000000000000000000000000000000000000000002\
0000000000000000000000000000000000000000000000000000000000000000\
03\
ffff"
).unwrap();

let mut output = vec![];
let expected_cost = 0;

f.execute(&input[..], &mut BytesRef::Flexible(&mut output));
assert_eq!(output.len(), 0); // shouldn't have written any output.
assert_eq!(f.cost(&input[..]), expected_cost.into());
}
}

#[test]
Expand Down
3 changes: 1 addition & 2 deletions ethcore/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
// Engines aren't supposed to return builtins until activation, but
// prefer to fail rather than silently break consensus.
if !builtin.is_active(self.info.number) {
let msg = format!("Engine returned unactivated built-in at {}", params.code_address);
return Err(evm::Error::Internal(msg));
panic!("Consensus failure: engine implementation prematurely enabled built-in at {}", params.code_address);
}

let default = [];
Expand Down