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

Commit 7cdba58

Browse files
committed
Implement EIP-2565 (option 2)
1 parent eee06b3 commit 7cdba58

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

ethcore/builtin/src/lib.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct Linear {
102102
#[derive(Debug)]
103103
struct ModexpPricer {
104104
divisor: u64,
105+
new_formula: bool,
105106
}
106107

107108
impl Pricer for Linear {
@@ -180,7 +181,13 @@ impl Pricer for ModexpPricer {
180181

181182
let adjusted_exp_len = Self::adjusted_exp_len(exp_len, exp_low);
182183

183-
let (gas, overflow) = Self::mult_complexity(m).overflowing_mul(max(adjusted_exp_len, 1));
184+
let complexity_formula = if self.new_formula {
185+
Self::mult_complexity
186+
} else {
187+
Self::mult_complexity_new
188+
};
189+
190+
let (gas, overflow) = (complexity_formula)(m).overflowing_mul(max(adjusted_exp_len, 1));
184191
if overflow {
185192
return U256::max_value();
186193
}
@@ -205,6 +212,10 @@ impl ModexpPricer {
205212
x => (x * x) / 16 + 480 * x - 199_680,
206213
}
207214
}
215+
216+
fn mult_complexity_new(x: u64) -> u64 {
217+
((x / 64) + if x % 64 == 0 { 0 } else { 1 }) ^ 2
218+
}
208219
}
209220

210221
/// Pricing scheme, execution definition, and activation block for a built-in contract.
@@ -281,7 +292,19 @@ impl From<ethjson::spec::builtin::Pricing> for Pricing {
281292
10
282293
} else {
283294
exp.divisor
284-
}
295+
},
296+
new_formula: false,
297+
})
298+
}
299+
ethjson::spec::builtin::Pricing::Modexp2(exp) => {
300+
Pricing::Modexp(ModexpPricer {
301+
divisor: if exp.divisor == 0 {
302+
warn!(target: "builtin", "Zero modexp divisor specified. Falling back to default: 10.");
303+
10
304+
} else {
305+
exp.divisor
306+
},
307+
new_formula: true,
285308
})
286309
}
287310
ethjson::spec::builtin::Pricing::AltBn128Pairing(pricer) => {
@@ -1037,7 +1060,7 @@ mod tests {
10371060
#[test]
10381061
fn modexp() {
10391062
let f = Builtin {
1040-
pricer: btreemap![0 => Pricing::Modexp(ModexpPricer { divisor: 20 })],
1063+
pricer: btreemap![0 => Pricing::Modexp(ModexpPricer { divisor: 20, new_formula: false })],
10411064
native: EthereumBuiltin::from_str("modexp").unwrap(),
10421065
};
10431066

ethcore/res/ethereum/foundation.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4827,10 +4827,9 @@
48274827
"0x0000000000000000000000000000000000000005": {
48284828
"builtin": {
48294829
"name": "modexp",
4830-
"activate_at": "0x42ae50",
48314830
"pricing": {
4832-
"modexp": {
4833-
"divisor": 20
4831+
"0x42ae50": {
4832+
"price": { "modexp": { "divisor": 20 } }
48344833
}
48354834
}
48364835
}

json/src/spec/builtin.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ pub enum Pricing {
6969
},
7070
/// Linear pricing.
7171
Linear(Linear),
72-
/// Pricing for modular exponentiation.
72+
/// Pricing for modular exponentiation with original formula from EIP-198.
7373
Modexp(Modexp),
74+
/// Pricing for modular exponentiation that includes new formula from EIP-2565.
75+
Modexp2(Modexp),
7476
/// Pricing for alt_bn128_pairing exponentiation.
7577
AltBn128Pairing(AltBn128Pairing),
7678
/// Pricing for constant alt_bn128 operations

0 commit comments

Comments
 (0)