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

Commit d2123e3

Browse files
committed
Implement EIP-2565 (option 2)
1 parent 1165549 commit d2123e3

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

ethcore/builtin/src/lib.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ struct Linear {
116116
#[derive(Debug)]
117117
struct ModexpPricer {
118118
divisor: u64,
119+
new_formula: bool,
119120
}
120121

121122
impl Pricer for Linear {
@@ -194,7 +195,13 @@ impl Pricer for ModexpPricer {
194195

195196
let adjusted_exp_len = Self::adjusted_exp_len(exp_len, exp_low);
196197

197-
let (gas, overflow) = Self::mult_complexity(m).overflowing_mul(max(adjusted_exp_len, 1));
198+
let complexity_formula = if self.new_formula {
199+
Self::mult_complexity_new
200+
} else {
201+
Self::mult_complexity
202+
};
203+
204+
let (gas, overflow) = (complexity_formula)(m).overflowing_mul(max(adjusted_exp_len, 1));
198205
if overflow {
199206
return U256::max_value();
200207
}
@@ -219,6 +226,10 @@ impl ModexpPricer {
219226
x => (x * x) / 16 + 480 * x - 199_680,
220227
}
221228
}
229+
230+
fn mult_complexity_new(x: u64) -> u64 {
231+
((x / 64) + if x % 64 == 0 { 0 } else { 1 }) ^ 2
232+
}
222233
}
223234

224235
/// Bls12 pairing price
@@ -407,7 +418,19 @@ impl From<ethjson::spec::builtin::Pricing> for Pricing {
407418
10
408419
} else {
409420
exp.divisor
410-
}
421+
},
422+
new_formula: false,
423+
})
424+
}
425+
ethjson::spec::builtin::Pricing::Modexp2(exp) => {
426+
Pricing::Modexp(ModexpPricer {
427+
divisor: if exp.divisor == 0 {
428+
warn!(target: "builtin", "Zero modexp divisor specified. Falling back to default: 10.");
429+
10
430+
} else {
431+
exp.divisor
432+
},
433+
new_formula: true,
411434
})
412435
}
413436
ethjson::spec::builtin::Pricing::AltBn128Pairing(pricer) => {
@@ -1360,7 +1383,7 @@ mod tests {
13601383
#[test]
13611384
fn modexp() {
13621385
let f = Builtin {
1363-
pricer: btreemap![0 => Pricing::Modexp(ModexpPricer { divisor: 20 })],
1386+
pricer: btreemap![0 => Pricing::Modexp(ModexpPricer { divisor: 20, new_formula: false })],
13641387
native: EthereumBuiltin::from_str("modexp").unwrap(),
13651388
};
13661389

ethcore/res/ethereum/foundation.json

+2-3
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ pub enum Pricing {
104104
},
105105
/// Linear pricing.
106106
Linear(Linear),
107-
/// Pricing for modular exponentiation.
107+
/// Pricing for modular exponentiation with original formula from EIP-198.
108108
Modexp(Modexp),
109+
/// Pricing for modular exponentiation that includes new formula from EIP-2565.
110+
Modexp2(Modexp),
109111
/// Pricing for alt_bn128_pairing exponentiation.
110112
AltBn128Pairing(AltBn128Pairing),
111113
/// Pricing for constant alt_bn128 operations

0 commit comments

Comments
 (0)