EIP-7883: ModExp Gas 成本增加
    
    
      
        
       
    
    
      
        
       
    
  
  增加 ModExp 预编译的成本
| Authors | Marcin Sobczak (@marcindsobczak), Marek Moraczyński (@MarekM25), Marcos Maceo (@stdevMac) | 
|---|---|
| Created | 2025-02-11 | 
| Discussion Link | https://ethereum-magicians.org/t/eip-7883-modexp-gas-cost-increase/22841 | 
| Requires | EIP-2565 | 
Table of Contents
摘要
本 EIP 修改了 EIP-2565 中引入的 ModExp 预编译定价算法。
动机
在某些情况下,ModExp 预编译的定价低于其资源消耗。通过修改 ModExp 定价公式,这些情况将被覆盖,同时对实际应用程序的影响最小。目标是使 ModExp 在所有情况下至少与 EcRecover 预编译一样快。
规范
在激活此 EIP 后,调用地址 0x0000000000000000000000000000000000000005 处的预编译的 Gas 成本将按如下方式计算:
def calculate_multiplication_complexity(base_length, modulus_length):
    max_length = max(base_length, modulus_length)
    words = math.ceil(max_length / 8)
    multiplication_complexity = 16
    if max_length > 32: multiplication_complexity = 2 * words**2
    return multiplication_complexity
def calculate_iteration_count(exponent_length, exponent):
    iteration_count = 0
    if exponent_length <= 32 and exponent == 0: iteration_count = 0
    elif exponent_length <= 32: iteration_count = exponent.bit_length() - 1
    elif exponent_length > 32: iteration_count = (16 * (exponent_length - 32)) + ((exponent & (2**256 - 1)).bit_length() - 1)
    return max(iteration_count, 1)
def calculate_gas_cost(base_length, modulus_length, exponent_length, exponent):
    multiplication_complexity = calculate_multiplication_complexity(base_length, modulus_length)
    iteration_count = calculate_iteration_count(exponent_length, exponent)
    return max(500, math.floor(multiplication_complexity * iteration_count / 3))
更改(使用 EIP-2565 中的算法):
1. 将最低价格从 200 增加到 500
等式的这部分:
    return max(200, math.floor(multiplication_complexity * iteration_count / 3))
被替换为:
    return max(500, math.floor(multiplication_complexity * iteration_count / 3))
2. 增加 exponent 大于 32 字节时的成本
等式的这部分:
    elif exponent_length > 32: iteration_count = (8 * (exponent_length - 32)) + ((exponent & (2**256 - 1)).bit_length() - 1)
被替换为:
    elif exponent_length > 32: iteration_count = (16 * (exponent_length - 32)) + ((exponent & (2**256 - 1)).bit_length() - 1)
乘数 8 被替换为 16。
3. 假设最小 base / modulus 长度为 32,并增加大于 32 字节时的成本
等式的这部分:
def calculate_multiplication_complexity(base_length, modulus_length):
    max_length = max(base_length, modulus_length)
    words = math.ceil(max_length / 8)
    return words**2
被替换为:
def calculate_multiplication_complexity(base_length, modulus_length):
    max_length = max(base_length, modulus_length)
    words = math.ceil(max_length / 8)
    multiplication_complexity = 16
    if max_length > 32: multiplication_complexity = 2 * words**2
    return multiplication_complexity
如果 base 或 modulus 大于 32 字节,则乘法复杂度加倍。
理由
在对 ModExp 预编译进行基准测试后,我们发现某些情况的定价过低,需要重新定价以确保适当的成本。进一步的研究表明,可以通过调整当前 ModExp 定价公式中的参数来解决所有定价过低的极端情况。通过这些更改,使用 ModExp 预编译的最低成本将从 200 增加到 500(增加 150%),并且当 base、modulus 或 exponent 超过 32 字节时,成本将更高。这些调整将确保 ModExp 预编译的最差性能的极端情况不会比 EcRecover 预编译更差。
向后兼容性
此更改不向后兼容。但是,类似的 Gas 重新定价在以太坊生态系统中已经发生多次,并且它们的影响已被充分理解。
测试用例
底层接口或算术算法没有变化,因此可以重用现有的测试向量。下表是更新后的测试向量:
| 测试用例 | EIP-2565 定价 | EIP-7883 定价 | 增加 | 
|---|---|---|---|
| modexp_nagydani_1_square | 200 | 500 | 150% | 
| modexp_nagydani_1_qube | 200 | 500 | 150% | 
| modexp_nagydani_1_pow0x10001 | 341 | 682 | 100% | 
| modexp_nagydani_2_square | 200 | 500 | 150% | 
| modexp_nagydani_2_qube | 200 | 500 | 150% | 
| modexp_nagydani_2_pow0x10001 | 1365 | 2730 | 100% | 
| modexp_nagydani_3_square | 341 | 682 | 100% | 
| modexp_nagydani_3_qube | 341 | 682 | 100% | 
| modexp_nagydani_3_pow0x10001 | 5461 | 10922 | 100% | 
| modexp_nagydani_4_square | 1365 | 2730 | 100% | 
| modexp_nagydani_4_qube | 1365 | 2730 | 100% | 
| modexp_nagydani_4_pow0x10001 | 21845 | 43690 | 100% | 
| modexp_nagydani_5_square | 5461 | 10922 | 100% | 
| modexp_nagydani_5_qube | 5461 | 10922 | 100% | 
| modexp_nagydani_5_pow0x10001 | 87381 | 174762 | 100% | 
| modexp_marius_1_even | 2057 | 3774 | 83% | 
| modexp_guido_1_even | 2298 | 4261 | 85% | 
| modexp_guido_2_even | 2300 | 4262 | 85% | 
| modexp_guido_3_even | 5400 | 10800 | 100% | 
| modexp_guido_4_even | 1026 | 1967 | 92% | 
| modexp_marcin_1_base_heavy | 200 | 500 | 150% | 
| modexp_marcin_1_exp_heavy | 215 | 500 | 133% | 
| modexp_marcin_1_balanced | 200 | 500 | 150% | 
| modexp_marcin_2_base_heavy | 867 | 1734 | 100% | 
| modexp_marcin_2_exp_heavy | 852 | 1364 | 60% | 
| modexp_marcin_2_balanced | 996 | 1992 | 100% | 
| modexp_marcin_3_base_heavy | 677 | 677 | 0% | 
| modexp_marcin_3_exp_heavy | 765 | 765 | 0% | 
| modexp_marcin_3_balanced | 1360 | 1360 | 0% | 
安全考虑
由于没有引入新功能或降低成本,因此不存在安全问题。此 EIP 的主要考虑因素是可能定价过高的 ModExp 方案的风险。
版权
版权及相关权利通过 CC0 放弃。
Citation
Please cite this document as:
Marcin Sobczak (@marcindsobczak), Marek Moraczyński (@MarekM25), Marcos Maceo (@stdevMac), "EIP-7883: ModExp Gas 成本增加 [DRAFT]," Ethereum Improvement Proposals, no. 7883, February 2025. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7883.