Skip to content

Commit

Permalink
devnet-5: Update EIP-2537: Gas repricing (#2959)
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko authored Dec 19, 2024
1 parent c801a11 commit 6085457
Show file tree
Hide file tree
Showing 9 changed files with 780 additions and 764 deletions.
12 changes: 6 additions & 6 deletions nimbus/evm/interpreter/gas_costs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -845,11 +845,11 @@ const
GasQuadDivisor* = 20
GasQuadDivisorEIP2565* = 3
# https://eips.ethereum.org/EIPS/eip-2537
Bls12381G1AddGas* = GasInt 500
Bls12381G1AddGas* = GasInt 375
Bls12381G1MulGas* = GasInt 12000
Bls12381G2AddGas* = GasInt 800
Bls12381G2MulGas* = GasInt 45000
Bls12381PairingBaseGas* = GasInt 115000
Bls12381PairingPerPairGas* = GasInt 23000
Bls12381G2AddGas* = GasInt 600
Bls12381G2MulGas* = GasInt 22500
Bls12381PairingBaseGas* = GasInt 37700
Bls12381PairingPerPairGas* = GasInt 32600
Bls12381MapG1Gas* = GasInt 5500
Bls12381MapG2Gas* = GasInt 110000
Bls12381MapG2Gas* = GasInt 23800
68 changes: 42 additions & 26 deletions nimbus/evm/precompiles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func bn256ecPairing(c: Computation, fork: EVMFork = FkByzantium): EvmResultVoid
else:
GasECPairingBaseIstanbul + numPoints * GasECPairingPerPointIstanbul
? c.gasMeter.consumeGas(gasFee, reason="ecPairing Precompile")

if msglen == 0:
# we can discard here because we supply buffer of proper size
c.output.setLen(32)
Expand All @@ -357,12 +357,12 @@ func bn256ecPairing(c: Computation, fork: EVMFork = FkByzantium): EvmResultVoid
for i in 0..<count:
let s = i * 192
# Loading AffinePoint[G1], bytes from [0..63]
let p1 = ?G1.getPoint(c.msg.data.toOpenArray(s, s + 63))
let p1 = ?G1.getPoint(c.msg.data.toOpenArray(s, s + 63))
# Loading AffinePoint[G2], bytes from [64..191]
let p2 = ?G2.getPoint(c.msg.data.toOpenArray(s + 64, s + 191))
# Accumulate pairing result
acc = acc * pairing(p1, p2)

c.output.setLen(32)
if acc == FQ12.one():
# we can discard here because we supply buffer of proper size
Expand Down Expand Up @@ -434,35 +434,51 @@ func blsG1Mul(c: Computation): EvmResultVoid =
ok()

const
Bls12381MultiExpDiscountTable = [
1200, 888, 764, 641, 594, 547, 500, 453, 438, 423,
408, 394, 379, 364, 349, 334, 330, 326, 322, 318,
314, 310, 306, 302, 298, 294, 289, 285, 281, 277,
273, 269, 268, 266, 265, 263, 262, 260, 259, 257,
256, 254, 253, 251, 250, 248, 247, 245, 244, 242,
241, 239, 238, 236, 235, 233, 232, 231, 229, 228,
226, 225, 223, 222, 221, 220, 219, 219, 218, 217,
216, 216, 215, 214, 213, 213, 212, 211, 211, 210,
209, 208, 208, 207, 206, 205, 205, 204, 203, 202,
202, 201, 200, 199, 199, 198, 197, 196, 196, 195,
194, 193, 193, 192, 191, 191, 190, 189, 188, 188,
187, 186, 185, 185, 184, 183, 182, 182, 181, 180,
179, 179, 178, 177, 176, 176, 175, 174
]

func calcBlsMultiExpGas(K: GasInt, gasCost: GasInt): GasInt =
MSMG1DiscountTable = [
1000.GasInt, 949, 848, 797, 764, 750, 738, 728, 719, 712, 705,
698, 692, 687, 682, 677, 673, 669, 665, 661, 658, 654, 651,
648, 645, 642, 640, 637, 635, 632, 630, 627, 625, 623, 621,
619, 617, 615, 613, 611, 609, 608, 606, 604, 603, 601, 599,
598, 596, 595, 593, 592, 591, 589, 588, 586, 585, 584, 582,
581, 580, 579, 577, 576, 575, 574, 573, 572, 570, 569, 568,
567, 566, 565, 564, 563, 562, 561, 560, 559, 558, 557, 556,
555, 554, 553, 552, 551, 550, 549, 548, 547, 547, 546, 545,
544, 543, 542, 541, 540, 540, 539, 538, 537, 536, 536, 535,
534, 533, 532, 532, 531, 530, 529, 528, 528, 527, 526, 525,
525, 524, 523, 522, 522, 521, 520, 520, 519]

MSMG1MaxDiscount = 519.GasInt

MSMG2DiscountTable = [
1000.GasInt, 1000, 923, 884, 855, 832, 812, 796, 782, 770, 759,
749, 740, 732, 724, 717, 711, 704, 699, 693, 688, 683, 679,
674, 670, 666, 663, 659, 655, 652, 649, 646, 643, 640, 637,
634, 632, 629, 627, 624, 622, 620, 618, 615, 613, 611, 609,
607, 606, 604, 602, 600, 598, 597, 595, 593, 592, 590, 589,
587, 586, 584, 583, 582, 580, 579, 578, 576, 575, 574, 573,
571, 570, 569, 568, 567, 566, 565, 563, 562, 561, 560, 559,
558, 557, 556, 555, 554, 553, 552, 552, 551, 550, 549, 548,
547, 546, 545, 545, 544, 543, 542, 541, 541, 540, 539, 538,
537, 537, 536, 535, 535, 534, 533, 532, 532, 531, 530, 530,
529, 528, 528, 527, 526, 526, 525, 524, 524]

MSMG2MaxDiscount = 524.GasInt

func calcBlsMultiExpGas(K: int, gasCost: GasInt,
discountTable: openArray[GasInt],
maxDiscount: GasInt): GasInt =
# Calculate G1 point, scalar value pair length
if K == 0:
# Return 0 gas for small input length
return 0.GasInt

const dLen = Bls12381MultiExpDiscountTable.len
# Lookup discount value for G1 point, scalar value pair length
let discount = if K < dLen: GasInt Bls12381MultiExpDiscountTable[K-1]
else: GasInt Bls12381MultiExpDiscountTable[dLen-1]
let dLen = discountTable.len
let discount = if K < dLen: discountTable[K-1]
else: maxDiscount

# Calculate gas and return the result
result = (K * gasCost * discount) div 1000
(K.GasInt * gasCost * discount) div 1000

func blsG1MultiExp(c: Computation): EvmResultVoid =
template input: untyped =
Expand All @@ -474,7 +490,7 @@ func blsG1MultiExp(c: Computation): EvmResultVoid =

let
K = input.len div L
gas = calcBlsMultiExpGas(GasInt K, Bls12381G1MulGas)
gas = calcBlsMultiExpGas(K, Bls12381G1MulGas, MSMG1DiscountTable, MSMG1MaxDiscount)

? c.gasMeter.consumeGas(gas, reason="blsG1MultiExp Precompile")

Expand Down Expand Up @@ -563,7 +579,7 @@ func blsG2MultiExp(c: Computation): EvmResultVoid =

let
K = input.len div L
gas = calcBlsMultiExpGas(GasInt K, Bls12381G2MulGas)
gas = calcBlsMultiExpGas(K, Bls12381G2MulGas, MSMG2DiscountTable, MSMG2MaxDiscount)

? c.gasMeter.consumeGas(gas, reason="blsG2MultiExp Precompile")

Expand Down
Loading

0 comments on commit 6085457

Please sign in to comment.