This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
EIP198 and built-in activation #4926
+303
−35
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(), | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could do this line at the very beginning. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.