From c24586f71a452427735963d6327ccf24f16d9fa0 Mon Sep 17 00:00:00 2001 From: Guruprasad Kamath Date: Wed, 18 Dec 2024 17:02:13 +0100 Subject: [PATCH 1/3] update gas pricing for pairing check in eip-2537 See https://github.com/ethereum/EIPs/pull/9098/files --- .../vm/precompiled_contracts/bls12_381/bls12_381_pairing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_pairing.py b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_pairing.py index a3deb5ee64..3431317212 100644 --- a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_pairing.py +++ b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_pairing.py @@ -43,7 +43,7 @@ def bls12_pairing(evm: Evm) -> None: # GAS k = len(data) // 384 - gas_cost = Uint(43000 * k + 65000) + gas_cost = Uint(32600 * k + 37700) charge_gas(evm, gas_cost) # OPERATION From 6094009fbfda2e0f883319ea296c6bc84c7814ff Mon Sep 17 00:00:00 2001 From: Guruprasad Kamath Date: Wed, 18 Dec 2024 17:11:56 +0100 Subject: [PATCH 2/3] gas price for BLS MAP, MUL, ADD See https://github.com/ethereum/EIPs/pull/9097/files --- .../vm/precompiled_contracts/bls12_381/bls12_381_g1.py | 2 +- .../vm/precompiled_contracts/bls12_381/bls12_381_g2.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g1.py b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g1.py index 1474e72a70..e4f19b2acd 100644 --- a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g1.py +++ b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g1.py @@ -54,7 +54,7 @@ def bls12_g1_add(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(500)) + charge_gas(evm, Uint(375)) # OPERATION p1 = bytes_to_G1(buffer_read(data, U256(0), U256(128))) diff --git a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g2.py b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g2.py index e5dcaede0c..37434e238d 100644 --- a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g2.py +++ b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g2.py @@ -54,7 +54,7 @@ def bls12_g2_add(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(800)) + charge_gas(evm, Uint(600)) # OPERATION p1 = bytes_to_G2(buffer_read(data, U256(0), U256(256))) @@ -84,7 +84,7 @@ def bls12_g2_multiply(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(45000)) + charge_gas(evm, Uint(22500)) # OPERATION p, m = decode_G2_scalar_pair(data) @@ -160,7 +160,7 @@ def bls12_map_fp2_to_g2(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(75000)) + charge_gas(evm, Uint(23800)) # OPERATION field_element = bytes_to_FQ2(data, True) From bf9b95a3a04f013feb933b90b4be4c23384bb045 Mon Sep 17 00:00:00 2001 From: Guruprasad Kamath Date: Wed, 18 Dec 2024 18:21:01 +0100 Subject: [PATCH 3/3] reprice gas for BLS pre-compiles --- src/ethereum/prague/vm/gas.py | 6 + .../bls12_381/__init__.py | 388 ++++++++++++------ .../bls12_381/bls12_381_g1.py | 23 +- .../bls12_381/bls12_381_g2.py | 23 +- 4 files changed, 294 insertions(+), 146 deletions(-) diff --git a/src/ethereum/prague/vm/gas.py b/src/ethereum/prague/vm/gas.py index 428c91212f..09b01204e4 100644 --- a/src/ethereum/prague/vm/gas.py +++ b/src/ethereum/prague/vm/gas.py @@ -66,6 +66,12 @@ GAS_INIT_CODE_WORD_COST = 2 GAS_BLOBHASH_OPCODE = Uint(3) GAS_POINT_EVALUATION = Uint(50000) +GAS_BLS_G1_ADD = Uint(375) +GAS_BLS_G1_MUL = Uint(12000) +GAS_BLS_G1_MAP = Uint(5500) +GAS_BLS_G2_ADD = Uint(600) +GAS_BLS_G2_MUL = Uint(22500) +GAS_BLS_G2_MAP = Uint(23800) TARGET_BLOB_GAS_PER_BLOCK = U64(786432) GAS_PER_BLOB = Uint(2**17) diff --git a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/__init__.py b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/__init__.py index 89e05bafc4..d38ea938f9 100644 --- a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/__init__.py +++ b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/__init__.py @@ -33,138 +33,270 @@ P = FQ.field_modulus -K_DISCOUNT = [ - 1200, - 888, +G1_K_DISCOUNT = [ + 1000, + 949, + 848, + 797, 764, - 641, - 594, + 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, - 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, + 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, +] + +G2_K_DISCOUNT = [ + 1000, + 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, ] -MAX_DISCOUNT = 174 +G1_MAX_DISCOUNT = 519 +G2_MAX_DISCOUNT = 524 MULTIPLIER = 1000 diff --git a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g1.py b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g1.py index e4f19b2acd..5629c2252c 100644 --- a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g1.py +++ b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g1.py @@ -19,12 +19,17 @@ from ethereum.base_types import U256, Uint from ....vm import Evm -from ....vm.gas import charge_gas +from ....vm.gas import ( + GAS_BLS_G1_ADD, + GAS_BLS_G1_MAP, + GAS_BLS_G1_MUL, + charge_gas, +) from ....vm.memory import buffer_read from ...exceptions import InvalidParameter from . import ( - K_DISCOUNT, - MAX_DISCOUNT, + G1_K_DISCOUNT, + G1_MAX_DISCOUNT, MULTIPLIER, G1_to_bytes, bytes_to_FQ, @@ -54,7 +59,7 @@ def bls12_g1_add(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(375)) + charge_gas(evm, Uint(GAS_BLS_G1_ADD)) # OPERATION p1 = bytes_to_G1(buffer_read(data, U256(0), U256(128))) @@ -84,7 +89,7 @@ def bls12_g1_multiply(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(12000)) + charge_gas(evm, Uint(GAS_BLS_G1_MUL)) # OPERATION p, m = decode_G1_scalar_pair(data) @@ -118,11 +123,11 @@ def bls12_g1_msm(evm: Evm) -> None: # GAS k = len(data) // LENGTH_PER_PAIR if k <= 128: - discount = K_DISCOUNT[k - 1] + discount = G1_K_DISCOUNT[k - 1] else: - discount = MAX_DISCOUNT + discount = G1_MAX_DISCOUNT - gas_cost = Uint(k * 12000 * discount // MULTIPLIER) + gas_cost = Uint(k * GAS_BLS_G1_MUL * discount // MULTIPLIER) charge_gas(evm, gas_cost) # OPERATION @@ -160,7 +165,7 @@ def bls12_map_fp_to_g1(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(5500)) + charge_gas(evm, Uint(GAS_BLS_G1_MAP)) # OPERATION field_element = bytes_to_FQ(data, True) diff --git a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g2.py b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g2.py index 37434e238d..ebd873fe81 100644 --- a/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g2.py +++ b/src/ethereum/prague/vm/precompiled_contracts/bls12_381/bls12_381_g2.py @@ -19,12 +19,17 @@ from ethereum.base_types import U256, Uint from ....vm import Evm -from ....vm.gas import charge_gas +from ....vm.gas import ( + GAS_BLS_G2_ADD, + GAS_BLS_G2_MAP, + GAS_BLS_G2_MUL, + charge_gas, +) from ....vm.memory import buffer_read from ...exceptions import InvalidParameter from . import ( - K_DISCOUNT, - MAX_DISCOUNT, + G2_K_DISCOUNT, + G2_MAX_DISCOUNT, MULTIPLIER, G2_to_bytes, bytes_to_FQ2, @@ -54,7 +59,7 @@ def bls12_g2_add(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(600)) + charge_gas(evm, Uint(GAS_BLS_G2_ADD)) # OPERATION p1 = bytes_to_G2(buffer_read(data, U256(0), U256(256))) @@ -84,7 +89,7 @@ def bls12_g2_multiply(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(22500)) + charge_gas(evm, Uint(GAS_BLS_G2_MUL)) # OPERATION p, m = decode_G2_scalar_pair(data) @@ -118,11 +123,11 @@ def bls12_g2_msm(evm: Evm) -> None: # GAS k = len(data) // LENGTH_PER_PAIR if k <= 128: - discount = K_DISCOUNT[k - 1] + discount = G2_K_DISCOUNT[k - 1] else: - discount = MAX_DISCOUNT + discount = G2_MAX_DISCOUNT - gas_cost = Uint(k * 45000 * discount // MULTIPLIER) + gas_cost = Uint(k * GAS_BLS_G2_MUL * discount // MULTIPLIER) charge_gas(evm, gas_cost) # OPERATION @@ -160,7 +165,7 @@ def bls12_map_fp2_to_g2(evm: Evm) -> None: raise InvalidParameter("Invalid Input Length") # GAS - charge_gas(evm, Uint(23800)) + charge_gas(evm, Uint(GAS_BLS_G2_MAP)) # OPERATION field_element = bytes_to_FQ2(data, True)