From c6d3851277fa8368b64888db48b9eabf9bd4fbff Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Tue, 13 May 2025 11:18:38 +0800 Subject: [PATCH] core/vm: implement EIP 7823 - Set upper bounds for MODEXP --- core/vm/contracts.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index f395de9703..aa2a54e1f8 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -66,7 +66,7 @@ var PrecompiledContractsByzantium = PrecompiledContracts{ common.BytesToAddress([]byte{0x2}): &sha256hash{}, common.BytesToAddress([]byte{0x3}): &ripemd160hash{}, common.BytesToAddress([]byte{0x4}): &dataCopy{}, - common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false, eip7883: false}, + common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false, eip7823: false, eip7883: false}, common.BytesToAddress([]byte{0x6}): &bn256AddByzantium{}, common.BytesToAddress([]byte{0x7}): &bn256ScalarMulByzantium{}, common.BytesToAddress([]byte{0x8}): &bn256PairingByzantium{}, @@ -79,7 +79,7 @@ var PrecompiledContractsIstanbul = PrecompiledContracts{ common.BytesToAddress([]byte{0x2}): &sha256hash{}, common.BytesToAddress([]byte{0x3}): &ripemd160hash{}, common.BytesToAddress([]byte{0x4}): &dataCopy{}, - common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false, eip7883: false}, + common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false, eip7823: false, eip7883: false}, common.BytesToAddress([]byte{0x6}): &bn256AddIstanbul{}, common.BytesToAddress([]byte{0x7}): &bn256ScalarMulIstanbul{}, common.BytesToAddress([]byte{0x8}): &bn256PairingIstanbul{}, @@ -93,7 +93,7 @@ var PrecompiledContractsBerlin = PrecompiledContracts{ common.BytesToAddress([]byte{0x2}): &sha256hash{}, common.BytesToAddress([]byte{0x3}): &ripemd160hash{}, common.BytesToAddress([]byte{0x4}): &dataCopy{}, - common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: true, eip7883: false}, + common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false, eip7823: false, eip7883: false}, common.BytesToAddress([]byte{0x6}): &bn256AddIstanbul{}, common.BytesToAddress([]byte{0x7}): &bn256ScalarMulIstanbul{}, common.BytesToAddress([]byte{0x8}): &bn256PairingIstanbul{}, @@ -107,7 +107,7 @@ var PrecompiledContractsCancun = PrecompiledContracts{ common.BytesToAddress([]byte{0x2}): &sha256hash{}, common.BytesToAddress([]byte{0x3}): &ripemd160hash{}, common.BytesToAddress([]byte{0x4}): &dataCopy{}, - common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: true, eip7883: false}, + common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false, eip7823: false, eip7883: false}, common.BytesToAddress([]byte{0x6}): &bn256AddIstanbul{}, common.BytesToAddress([]byte{0x7}): &bn256ScalarMulIstanbul{}, common.BytesToAddress([]byte{0x8}): &bn256PairingIstanbul{}, @@ -122,7 +122,7 @@ var PrecompiledContractsPrague = PrecompiledContracts{ common.BytesToAddress([]byte{0x02}): &sha256hash{}, common.BytesToAddress([]byte{0x03}): &ripemd160hash{}, common.BytesToAddress([]byte{0x04}): &dataCopy{}, - common.BytesToAddress([]byte{0x05}): &bigModExp{eip2565: true, eip7883: false}, + common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false, eip7823: false, eip7883: false}, common.BytesToAddress([]byte{0x06}): &bn256AddIstanbul{}, common.BytesToAddress([]byte{0x07}): &bn256ScalarMulIstanbul{}, common.BytesToAddress([]byte{0x08}): &bn256PairingIstanbul{}, @@ -163,6 +163,28 @@ var PrecompiledContractsOsaka = PrecompiledContracts{ common.BytesToAddress([]byte{0x11}): &bls12381MapG2{}, } +// PrecompiledContractsOsaka contains the set of pre-compiled Ethereum +// contracts used in the Osaka release. +var PrecompiledContractsOsaka = PrecompiledContracts{ + common.BytesToAddress([]byte{0x01}): &ecrecover{}, + common.BytesToAddress([]byte{0x02}): &sha256hash{}, + common.BytesToAddress([]byte{0x03}): &ripemd160hash{}, + common.BytesToAddress([]byte{0x04}): &dataCopy{}, + common.BytesToAddress([]byte{0x05}): &bigModExp{eip2565: true, eip7823: true}, + common.BytesToAddress([]byte{0x06}): &bn256AddIstanbul{}, + common.BytesToAddress([]byte{0x07}): &bn256ScalarMulIstanbul{}, + common.BytesToAddress([]byte{0x08}): &bn256PairingIstanbul{}, + common.BytesToAddress([]byte{0x09}): &blake2F{}, + common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{}, + common.BytesToAddress([]byte{0x0b}): &bls12381G1Add{}, + common.BytesToAddress([]byte{0x0c}): &bls12381G1MultiExp{}, + common.BytesToAddress([]byte{0x0d}): &bls12381G2Add{}, + common.BytesToAddress([]byte{0x0e}): &bls12381G2MultiExp{}, + common.BytesToAddress([]byte{0x0f}): &bls12381Pairing{}, + common.BytesToAddress([]byte{0x10}): &bls12381MapG1{}, + common.BytesToAddress([]byte{0x11}): &bls12381MapG2{}, +} + var ( PrecompiledAddressesPrague []common.Address PrecompiledAddressesCancun []common.Address @@ -341,6 +363,7 @@ func (c *dataCopy) Run(in []byte) ([]byte, error) { // bigModExp implements a native big integer exponential modular operation. type bigModExp struct { eip2565 bool + eip7823 bool eip7883 bool } @@ -497,6 +520,9 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) { mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen)) v []byte ) + if c.eip7823 && max(max(baseLen, expLen), modLen) > 1024 { + return nil, fmt.Errorf("one or more of base/exponent/modulus length exceeded 1024 bytes") + } switch { case mod.BitLen() == 0: // Modulo 0 is undefined, return zero