@@ -775,6 +775,8 @@ pub fn staticcall(
775
775
calculate_cost_and_gas_limit_call ( true , gas_from_stack, gas_left, call_gas_costs, 0 )
776
776
}
777
777
778
+ /// Approximates factor * e ** (numerator / denominator) using Taylor expansion
779
+ /// https://eips.ethereum.org/EIPS/eip-4844#helpers
778
780
pub fn fake_exponential ( factor : U256 , numerator : U256 , denominator : u64 ) -> Result < U256 , VMError > {
779
781
if denominator == 0 {
780
782
return Err ( InternalError :: DivisionByZero . into ( ) ) ;
@@ -784,7 +786,6 @@ pub fn fake_exponential(factor: U256, numerator: U256, denominator: u64) -> Resu
784
786
return Ok ( factor) ;
785
787
}
786
788
787
- let mut i: u64 = 1 ;
788
789
let mut output: U256 = U256 :: zero ( ) ;
789
790
let denominator_u256: U256 = denominator. into ( ) ;
790
791
@@ -793,9 +794,11 @@ pub fn fake_exponential(factor: U256, numerator: U256, denominator: u64) -> Resu
793
794
. checked_mul ( denominator_u256)
794
795
. ok_or ( InternalError :: Overflow ) ?;
795
796
797
+ let mut denominator_by_i = denominator_u256;
798
+
796
799
#[ expect(
797
800
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"
799
802
) ]
800
803
{
801
804
while !numerator_accum. is_zero ( ) {
@@ -808,10 +811,10 @@ pub fn fake_exponential(factor: U256, numerator: U256, denominator: u64) -> Resu
808
811
numerator_accum = numerator_accum
809
812
. checked_mul ( numerator)
810
813
. ok_or ( InternalError :: Overflow ) ?
811
- / denominator . checked_mul ( i ) . ok_or ( InternalError :: Overflow ) ? ;
814
+ / denominator_by_i ;
812
815
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 ;
815
818
}
816
819
817
820
Ok ( output / denominator)
0 commit comments