Skip to content

Commit 1d5d073

Browse files
committed
comment
1 parent e73ca66 commit 1d5d073

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

crates/vm/levm/src/gas_cost.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,8 @@ pub fn staticcall(
775775
calculate_cost_and_gas_limit_call(true, gas_from_stack, gas_left, call_gas_costs, 0)
776776
}
777777

778+
/// Approximates factor * e ** (numerator / denominator) using Taylor expansion
779+
/// https://eips.ethereum.org/EIPS/eip-4844#helpers
778780
pub fn fake_exponential(factor: U256, numerator: U256, denominator: u64) -> Result<U256, VMError> {
779781
if denominator == 0 {
780782
return Err(InternalError::DivisionByZero.into());
@@ -784,7 +786,6 @@ pub fn fake_exponential(factor: U256, numerator: U256, denominator: u64) -> Resu
784786
return Ok(factor);
785787
}
786788

787-
let mut i: u64 = 1;
788789
let mut output: U256 = U256::zero();
789790
let denominator_u256: U256 = denominator.into();
790791

@@ -793,9 +794,11 @@ pub fn fake_exponential(factor: U256, numerator: U256, denominator: u64) -> Resu
793794
.checked_mul(denominator_u256)
794795
.ok_or(InternalError::Overflow)?;
795796

797+
let mut denominator_by_i = denominator_u256;
798+
796799
#[expect(
797800
clippy::arithmetic_side_effects,
798-
reason = "division can't overflow since denominator is not 0 and i is never 0"
801+
reason = "division can't overflow since denominator is not 0"
799802
)]
800803
{
801804
while !numerator_accum.is_zero() {
@@ -808,10 +811,10 @@ pub fn fake_exponential(factor: U256, numerator: U256, denominator: u64) -> Resu
808811
numerator_accum = numerator_accum
809812
.checked_mul(numerator)
810813
.ok_or(InternalError::Overflow)?
811-
/ denominator.checked_mul(i).ok_or(InternalError::Overflow)?;
814+
/ denominator_by_i;
812815

813-
// i will never realistically overflow before the other variables do
814-
i = i.wrapping_add(1);
816+
// denominator comes from a u64 value, will never overflow before other variables.
817+
denominator_by_i += denominator_u256;
815818
}
816819

817820
Ok(output / denominator)

0 commit comments

Comments
 (0)