Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply kingster's change to Leeren's branch #1

Draft
wants to merge 2 commits into
base: feat/add-ip-and-p256-verify-precompiles
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
common.BytesToAddress([]byte{8}): {Balance: big.NewInt(1)}, // ECPairing
common.BytesToAddress([]byte{9}): {Balance: big.NewInt(1)}, // BLAKE2b
common.BytesToAddress([]byte{26}): {Balance: big.NewInt(1)}, // ipGraph
common.BytesToAddress([]byte{27}): {Balance: big.NewInt(1)}, // ipGraphWithKind

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add to genesis


// Pre-deploy EIP-4788 system contract
params.BeaconRootsAddress: {Nonce: 1, Code: params.BeaconRootsCode, Balance: common.Big0},
Expand Down
111 changes: 35 additions & 76 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"math/big"

"github.com/ethereum/go-ethereum/crypto"

"github.com/consensys/gnark-crypto/ecc"
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fp"
Expand All @@ -34,7 +35,8 @@ import (
"github.com/ethereum/go-ethereum/crypto/blake2b"
"github.com/ethereum/go-ethereum/crypto/bn256"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/crypto/secp256r1"

// "github.com/ethereum/go-ethereum/crypto/secp256r1"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not delete this

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"golang.org/x/crypto/ripemd160"
Expand All @@ -55,7 +57,7 @@ var PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{0x2}): &sha256hash{},
common.BytesToAddress([]byte{0x3}): &ripemd160hash{},
common.BytesToAddress([]byte{0x4}): &dataCopy{},
common.BytesToAddress([]byte{0x1a}): &ipGraph{},
// common.BytesToAddress([]byte{0x1a}): &ipGraph{},
}

// PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum
Expand All @@ -69,7 +71,7 @@ var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{0x6}): &bn256AddByzantium{},
common.BytesToAddress([]byte{0x7}): &bn256ScalarMulByzantium{},
common.BytesToAddress([]byte{0x8}): &bn256PairingByzantium{},
common.BytesToAddress([]byte{0x1a}): &ipGraph{},
// common.BytesToAddress([]byte{0x1a}): &ipGraph{},
}

// PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
Expand All @@ -84,7 +86,7 @@ var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{0x7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{0x8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{0x9}): &blake2F{},
common.BytesToAddress([]byte{0x1a}): &ipGraph{},
// common.BytesToAddress([]byte{0x1a}): &ipGraph{},
}

// PrecompiledContractsBerlin contains the default set of pre-compiled Ethereum
Expand All @@ -99,7 +101,7 @@ var PrecompiledContractsBerlin = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{0x7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{0x8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{0x9}): &blake2F{},
common.BytesToAddress([]byte{0x1a}): &ipGraph{},
// common.BytesToAddress([]byte{0x1a}): &ipGraph{},
}

// PrecompiledContractsCancun contains the default set of pre-compiled Ethereum
Expand Down Expand Up @@ -140,42 +142,39 @@ var PrecompiledContractsPrague = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{0x11}): &bls12381Pairing{},
common.BytesToAddress([]byte{0x12}): &bls12381MapG1{},
common.BytesToAddress([]byte{0x13}): &bls12381MapG2{},
common.BytesToAddress([]byte{0x1a}): &ipGraph{},
}

// PrecompiledContractsNostoi contains the set of pre-compiled Ethereum
// contracts used in the iliad nostoi release.
var PrecompiledContractsNostoi = map[common.Address]PrecompiledContract{
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},
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}): &bls12381G1Mul{},
common.BytesToAddress([]byte{0x0d}): &bls12381G1MultiExp{},
common.BytesToAddress([]byte{0x0e}): &bls12381G2Add{},
common.BytesToAddress([]byte{0x0f}): &bls12381G2Mul{},
common.BytesToAddress([]byte{0x10}): &bls12381G2MultiExp{},
common.BytesToAddress([]byte{0x11}): &bls12381Pairing{},
common.BytesToAddress([]byte{0x12}): &bls12381MapG1{},
common.BytesToAddress([]byte{0x13}): &bls12381MapG2{},
common.BytesToAddress([]byte{0x1a}): &ipGraph{},
common.BytesToAddress([]byte{0x1b}): &ipGraphWithPolicyKind{},
common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
}
// common.BytesToAddress([]byte{0x1a}): &ipGraph{},
}

//var PrecompiledContractsNostoi = map[common.Address]PrecompiledContract{
// 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},
// 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}): &bls12381G1Mul{},
// common.BytesToAddress([]byte{0x0d}): &bls12381G1MultiExp{},
// common.BytesToAddress([]byte{0x0e}): &bls12381G2Add{},
// common.BytesToAddress([]byte{0x0f}): &bls12381G2Mul{},
// common.BytesToAddress([]byte{0x10}): &bls12381G2MultiExp{},
// common.BytesToAddress([]byte{0x11}): &bls12381Pairing{},
// common.BytesToAddress([]byte{0x12}): &bls12381MapG1{},
// common.BytesToAddress([]byte{0x13}): &bls12381MapG2{},
// common.BytesToAddress([]byte{0x1a}): &ipGraphDynamicGas{}, // redirect 0x1a to ipGraphDynamicGas
// common.BytesToAddress([]byte{0x1b}): &ipGraphWithPolicyKind{},
// common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
//}
Comment on lines +148 to +171

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to be activated


var PrecompiledContractsBLS = PrecompiledContractsPrague

var PrecompiledContractsVerkle = PrecompiledContractsPrague

var (
PrecompiledAddressesNostoi []common.Address

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not delete this

PrecompiledAddressesPrague []common.Address
PrecompiledAddressesCancun []common.Address
PrecompiledAddressesBerlin []common.Address
Expand Down Expand Up @@ -203,16 +202,11 @@ func init() {
for k := range PrecompiledContractsPrague {
PrecompiledAddressesPrague = append(PrecompiledAddressesPrague, k)
}
for k := range PrecompiledContractsNostoi {
PrecompiledAddressesNostoi = append(PrecompiledAddressesNostoi, k)
}
Comment on lines -206 to -208

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not delete these

}

// ActivePrecompiles returns the precompiles enabled with the current configuration.
func ActivePrecompiles(rules params.Rules) []common.Address {
switch {
case rules.IsStoryNostoi:
return PrecompiledAddressesNostoi
Comment on lines -214 to -215

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not delete this

case rules.IsPrague:
return PrecompiledAddressesPrague
case rules.IsCancun:
Expand Down Expand Up @@ -1266,38 +1260,3 @@ func kZGToVersionedHash(kzg kzg4844.Commitment) common.Hash {

return h
}

// P256VERIFY (secp256r1 signature verification)
// implemented as a native contract
type p256Verify struct{}

// RequiredGas returns the gas required to execute the precompiled contract
func (c *p256Verify) RequiredGas(input []byte) uint64 {
return params.P256VerifyGas
}

// Run executes the precompiled contract with the given input and EVM context, returning the output and the used gas
func (c *p256Verify) Run(evm *EVM, input []byte) ([]byte, error) {
// Required input length is 160 bytes
const p256VerifyInputLength = 160

// Check the input length
if len(input) != p256VerifyInputLength {
// Input length is invalid
return nil, nil
}

// Extract the hash, r, s, x, y from the input
hash := input[0:32]
r, s := new(big.Int).SetBytes(input[32:64]), new(big.Int).SetBytes(input[64:96])
x, y := new(big.Int).SetBytes(input[96:128]), new(big.Int).SetBytes(input[128:160])

// Verify the secp256r1 signature
if secp256r1.Verify(hash, r, s, x, y) {
// Signature is valid
return common.LeftPadBytes([]byte{1}, 32), nil
} else {
// Signature is invalid
return nil, nil
}
}
Comment on lines -1269 to -1303

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are the deleted?

72 changes: 42 additions & 30 deletions core/vm/ipgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,7 @@ var (
type ipGraph struct{}

func (c *ipGraph) RequiredGas(input []byte) uint64 {
if len(input) < 4 {
return intrinsicGas
}

selector := input[:4]

switch {
case bytes.Equal(selector, addParentIpSelector):
return ipGraphWriteGas
case bytes.Equal(selector, hasParentIpSelector):
return ipGraphReadGas * averageParentIpCount
case bytes.Equal(selector, getParentIpsSelector):
return ipGraphReadGas * averageParentIpCount
case bytes.Equal(selector, getParentIpsCountSelector):
return ipGraphReadGas
case bytes.Equal(selector, getAncestorIpsSelector):
return ipGraphReadGas * averageAncestorIpCount * 2
case bytes.Equal(selector, getAncestorIpsCountSelector):
return ipGraphReadGas * averageParentIpCount * 2
case bytes.Equal(selector, hasAncestorIpsSelector):
return ipGraphReadGas * averageAncestorIpCount * 2
case bytes.Equal(selector, setRoyaltySelector):
return ipGraphWriteGas
case bytes.Equal(selector, getRoyaltySelector):
return ipGraphReadGas * averageAncestorIpCount * 2
case bytes.Equal(selector, getRoyaltyStackSelector):
return ipGraphReadGas * averageAncestorIpCount * 2
default:
return intrinsicGas
}
return uint64(1)
}

func (c *ipGraph) Run(evm *EVM, input []byte) ([]byte, error) {
Expand Down Expand Up @@ -411,3 +382,44 @@ func (c *ipGraph) getRoyaltyStack(input []byte, evm *EVM, ipGraphAddress common.
}
return common.BigToHash(totalRoyalty).Bytes(), nil
}

type ipGraphDynamicGas struct {
ipGraph
}

func (c *ipGraphDynamicGas) RequiredGas(input []byte) uint64 {
if len(input) < 4 {
return intrinsicGas
}

selector := input[:4]

switch {
case bytes.Equal(selector, addParentIpSelector):
return ipGraphWriteGas
case bytes.Equal(selector, hasParentIpSelector):
return ipGraphReadGas * averageParentIpCount
case bytes.Equal(selector, getParentIpsSelector):
return ipGraphReadGas * averageParentIpCount
case bytes.Equal(selector, getParentIpsCountSelector):
return ipGraphReadGas
case bytes.Equal(selector, getAncestorIpsSelector):
return ipGraphReadGas * averageAncestorIpCount * 2
case bytes.Equal(selector, getAncestorIpsCountSelector):
return ipGraphReadGas * averageParentIpCount * 2
case bytes.Equal(selector, hasAncestorIpsSelector):
return ipGraphReadGas * averageAncestorIpCount * 2
case bytes.Equal(selector, setRoyaltySelector):
return ipGraphWriteGas
case bytes.Equal(selector, getRoyaltySelector):
return ipGraphReadGas * averageAncestorIpCount * 2
case bytes.Equal(selector, getRoyaltyStackSelector):
return ipGraphReadGas * averageAncestorIpCount * 2
default:
return intrinsicGas
}
}

func (c *ipGraphDynamicGas) Run(evm *EVM, input []byte) ([]byte, error) {
return c.ipGraph.Run(evm, input)
}
1 change: 1 addition & 0 deletions core/vm/ipgraph_with_kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (c *ipGraphWithPolicyKind) Run(evm *EVM, input []byte) ([]byte, error) {
}
}

// Royalty has two kinds of policies: LAP and LRP.
func (c *ipGraphWithPolicyKind) setRoyaltyWithKind(input []byte, evm *EVM, ipGraphAddress common.Address) ([]byte, error) {
allowed, err := c.ipGraph.isAllowed(evm)

Expand Down