Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var PrecompiledContractsByzantium = PrecompiledContracts{
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false, eip7823: false},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false, eip7823: false, eip7883: false},
common.BytesToAddress([]byte{6}): &bn256AddByzantium{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulByzantium{},
common.BytesToAddress([]byte{8}): &bn256PairingByzantium{},
Expand All @@ -78,7 +78,7 @@ var PrecompiledContractsIstanbul = PrecompiledContracts{
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false, eip7823: false},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false, eip7823: false, eip7883: false},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
Expand All @@ -96,7 +96,7 @@ var PrecompiledContractsXDCv2 = PrecompiledContracts{
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false, eip7823: false},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false, eip7823: false, eip7883: false},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
Expand All @@ -110,7 +110,7 @@ var PrecompiledContractsEIP1559 = PrecompiledContracts{
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true, eip7823: false},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true, eip7823: false, eip7883: false},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
Expand All @@ -124,14 +124,15 @@ var PrecompiledContractsOsaka = PrecompiledContracts{
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true, eip7823: true},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true, eip7823: true, eip7883: true},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
}

var (
PrecompiledAddressesOsaka []common.Address
PrecompiledAddressesEIP1559 []common.Address
PrecompiledAddressesXDCv2 []common.Address
PrecompiledAddressesIstanbul []common.Address
Expand All @@ -155,6 +156,9 @@ func init() {
for k := range PrecompiledContractsEIP1559 {
PrecompiledAddressesEIP1559 = append(PrecompiledAddressesEIP1559, k)
}
for k := range PrecompiledContractsOsaka {
PrecompiledAddressesOsaka = append(PrecompiledAddressesOsaka, k)
}
}

func activePrecompiledContracts(rules params.Rules) PrecompiledContracts {
Expand Down Expand Up @@ -182,6 +186,8 @@ func ActivePrecompiledContracts(rules params.Rules) PrecompiledContracts {
// ActivePrecompiles returns the precompile addresses enabled with the current configuration.
func ActivePrecompiles(rules params.Rules) []common.Address {
switch {
case rules.IsOsaka:
return PrecompiledAddressesOsaka
case rules.IsEIP1559:
return PrecompiledAddressesEIP1559
case rules.IsXDCxDisable:
Expand Down Expand Up @@ -311,6 +317,7 @@ func (c *dataCopy) Run(in []byte) ([]byte, error) {
type bigModExp struct {
eip2565 bool
eip7823 bool
eip7883 bool
}

var (
Expand Down Expand Up @@ -388,7 +395,11 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
adjExpLen := new(big.Int)
if expLen.Cmp(big32) > 0 {
adjExpLen.Sub(expLen, big32)
adjExpLen.Lsh(adjExpLen, 3)
if c.eip7883 {
adjExpLen.Lsh(adjExpLen, 4)
} else {
adjExpLen.Lsh(adjExpLen, 3)
}
}
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
// Calculate the gas cost of the operation
Expand All @@ -398,6 +409,8 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
} else {
gas.Set(modLen)
}

maxLenOver32 := gas.Cmp(big32) > 0
if c.eip2565 {
// EIP-2565 has three changes
// 1. Different multComplexity (inlined here)
Expand All @@ -411,6 +424,16 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
gas.Rsh(gas, 3)
gas.Mul(gas, gas)

var minPrice uint64 = 200
if c.eip7883 {
minPrice = 500
if maxLenOver32 {
gas.Add(gas, gas)
} else {
gas = big.NewInt(16)
}
}

if adjExpLen.Cmp(big1) > 0 {
gas.Mul(gas, adjExpLen)
}
Expand All @@ -419,18 +442,15 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
if gas.BitLen() > 64 {
return math.MaxUint64
}
// 3. Minimum price of 200 gas
if gas.Uint64() < 200 {
return 200
}
return gas.Uint64()
return max(minPrice, gas.Uint64())
}

// Pre-Berlin logic.
gas = modexpMultComplexity(gas)
if adjExpLen.Cmp(big1) > 0 {
gas.Mul(gas, adjExpLen)
}
gas.Div(gas, big20)

if gas.BitLen() > 64 {
return math.MaxUint64
}
Expand Down
8 changes: 6 additions & 2 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ var allPrecompiles = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false},
common.BytesToAddress([]byte{0xf5}): &bigModExp{eip2565: true},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false, eip7883: false},
common.BytesToAddress([]byte{0xf5}): &bigModExp{eip2565: true, eip7883: false},
common.BytesToAddress([]byte{0xf6}): &bigModExp{eip2565: true, eip7883: true},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
Expand Down Expand Up @@ -678,6 +679,9 @@ func BenchmarkPrecompiledModExp(bench *testing.B) {
func TestPrecompiledModExpEip2565(t *testing.T) { testJson("modexp_eip2565", "f5", t) }
func BenchmarkPrecompiledModExpEip2565(b *testing.B) { benchJson("modexp_eip2565", "f5", b) }

func TestPrecompiledModExpEip7883(t *testing.T) { testJson("modexp_eip7883", "f6", t) }
func BenchmarkPrecompiledModExpEip7883(b *testing.B) { benchJson("modexp_eip7883", "f6", b) }

// Tests the sample inputs from the elliptic curve addition EIP 213.
func TestPrecompiledBn256Add(t *testing.T) {
for _, test := range bn256AddTests {
Expand Down
Loading