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

core/vm: BLS gas changes for pectra-devnet-5 per EIP-2537 #13346

Merged
merged 29 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4841826
Add pectra devnet 5 tests (eest disabled)
somnathb1 Dec 24, 2024
b19673b
Update to main branch
somnathb1 Jan 9, 2025
b608b5d
Update 2935 address
somnathb1 Dec 26, 2024
79d7dfc
Implement calldata cost increase per EIP-7623
somnathb1 Dec 11, 2024
abe46a7
Fix
somnathb1 Dec 12, 2024
181c83f
Implement EIP-7623
somnathb1 Dec 27, 2024
f8f873c
Fix EIP-7623
somnathb1 Dec 28, 2024
fbff0cb
Fix gas accounting
somnathb1 Jan 8, 2025
0b17efb
Update addresses for EIPs 7002 and 7251
somnathb1 Dec 10, 2024
ae7113e
Update 7251 address again
somnathb1 Dec 28, 2024
880d1c4
Make merge.go append requests
somnathb1 Dec 3, 2024
31dae03
Adjust request log in block builder func
somnathb1 Dec 3, 2024
88d0738
Add empty hash test
somnathb1 Dec 3, 2024
1d329ea
Change empty req hash string
somnathb1 Dec 3, 2024
8bd403d
Remove nil requests from being processed
somnathb1 Dec 3, 2024
786c2f2
Update GetAssembled block of EthereumExecutionModule
somnathb1 Dec 3, 2024
6056a08
Fix nil reqs
somnathb1 Dec 28, 2024
9ddbbfb
Implement EXTCODE* changes for pdn-5
somnathb1 Dec 13, 2024
c934120
Fix ext* logic
somnathb1 Dec 19, 2024
39e8492
Fix 7702 256 bits
somnathb1 Jan 7, 2025
8d861e0
Remove dynamic gas for EXTCODE*
somnathb1 Jan 8, 2025
63a7894
2537 changes for pdn5
somnathb1 Dec 26, 2024
ec6bb08
Fix discount table
somnathb1 Jan 8, 2025
45bcc9e
core/vm: update pricing, rm bls MUL precompiles
s1na Dec 31, 2024
874b051
Restore allprecompiled list
somnathb1 Jan 14, 2025
8f5589b
Merge branch 'main' into som/eip2537
somnathb1 Jan 14, 2025
41f8727
Remove redundant file
somnathb1 Jan 14, 2025
3db4530
Revert test addresses
somnathb1 Jan 14, 2025
b2e0d11
Lint
somnathb1 Jan 14, 2025
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
104 changes: 12 additions & 92 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,12 @@ var PrecompiledContractsPrague = map[libcommon.Address]PrecompiledContract{
libcommon.BytesToAddress([]byte{0x09}): &blake2F{},
libcommon.BytesToAddress([]byte{0x0a}): &pointEvaluation{},
libcommon.BytesToAddress([]byte{0x0b}): &bls12381G1Add{},
libcommon.BytesToAddress([]byte{0x0c}): &bls12381G1Mul{},
libcommon.BytesToAddress([]byte{0x0d}): &bls12381G1MultiExp{},
libcommon.BytesToAddress([]byte{0x0e}): &bls12381G2Add{},
libcommon.BytesToAddress([]byte{0x0f}): &bls12381G2Mul{},
libcommon.BytesToAddress([]byte{0x10}): &bls12381G2MultiExp{},
libcommon.BytesToAddress([]byte{0x11}): &bls12381Pairing{},
libcommon.BytesToAddress([]byte{0x12}): &bls12381MapFpToG1{},
libcommon.BytesToAddress([]byte{0x13}): &bls12381MapFp2ToG2{},
libcommon.BytesToAddress([]byte{0x0c}): &bls12381G1MultiExp{},
libcommon.BytesToAddress([]byte{0x0d}): &bls12381G2Add{},
libcommon.BytesToAddress([]byte{0x0e}): &bls12381G2MultiExp{},
libcommon.BytesToAddress([]byte{0x0f}): &bls12381Pairing{},
libcommon.BytesToAddress([]byte{0x10}): &bls12381MapFpToG1{},
libcommon.BytesToAddress([]byte{0x11}): &bls12381MapFp2ToG2{},
}

var (
Expand Down Expand Up @@ -735,45 +733,6 @@ func (c *bls12381G1Add) Run(input []byte) ([]byte, error) {
return encodePointG1(p0), nil
}

// bls12381G1Mul implements EIP-2537 G1Mul precompile.
type bls12381G1Mul struct{}

// RequiredGas returns the gas required to execute the pre-compiled contract.
func (c *bls12381G1Mul) RequiredGas(input []byte) uint64 {
return params.Bls12381G1MulGas
}

func (c *bls12381G1Mul) Run(input []byte) ([]byte, error) {
// Implements EIP-2537 G1Mul precompile.
// > G1 multiplication call expects `160` bytes as an input that is interpreted as byte concatenation of encoding of G1 point (`128` bytes) and encoding of a scalar value (`32` bytes).
// > Output is an encoding of multiplication operation result - single G1 point (`128` bytes).
if len(input) != 160 {
return nil, errBLS12381InvalidInputLength
}
var err error
var p0 *bls12381.G1Affine

// Decode G1 point
if p0, err = decodePointG1(input[:128]); err != nil {
return nil, err
}

// Fast subgroup check
if !p0.IsInSubGroup() {
return nil, errBLS12381G1PointSubgroup
}

// Decode scalar value
e := new(big.Int).SetBytes(input[128:])

// Compute r = e * p_0
r := new(bls12381.G1Affine)
r.ScalarMultiplication(p0, e)

// Encode the G1 point into 128 bytes
return encodePointG1(r), nil
}

// bls12381G1MultiExp implements EIP-2537 G1MultiExp precompile.
type bls12381G1MultiExp struct{}

Expand All @@ -787,10 +746,10 @@ func (c *bls12381G1MultiExp) RequiredGas(input []byte) uint64 {
}
// Lookup discount value for G1 point, scalar value pair length
var discount uint64
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
discount = params.Bls12381MultiExpDiscountTable[k-1]
if dLen := len(params.Bls12381MSMDiscountTableG1); k < dLen {
discount = params.Bls12381MSMDiscountTableG1[k-1]
} else {
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
discount = params.Bls12381MSMDiscountTableG1[dLen-1]
}
// Calculate gas and return the result
return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000
Expand Down Expand Up @@ -868,45 +827,6 @@ func (c *bls12381G2Add) Run(input []byte) ([]byte, error) {
return encodePointG2(r), nil
}

// bls12381G2Mul implements EIP-2537 G2Mul precompile.
type bls12381G2Mul struct{}

// RequiredGas returns the gas required to execute the pre-compiled contract.
func (c *bls12381G2Mul) RequiredGas(input []byte) uint64 {
return params.Bls12381G2MulGas
}

func (c *bls12381G2Mul) Run(input []byte) ([]byte, error) {
// Implements EIP-2537 G2MUL precompile logic.
// > G2 multiplication call expects `288` bytes as an input that is interpreted as byte concatenation of encoding of G2 point (`256` bytes) and encoding of a scalar value (`32` bytes).
// > Output is an encoding of multiplication operation result - single G2 point (`256` bytes).
if len(input) != 288 {
return nil, errBLS12381InvalidInputLength
}
var err error
var p0 *bls12381.G2Affine

// Decode G2 point
if p0, err = decodePointG2(input[:256]); err != nil {
return nil, err
}

// Fast subgroup check
if !p0.IsInSubGroup() {
return nil, errBLS12381G2PointSubgroup
}

// Decode scalar value
e := new(big.Int).SetBytes(input[256:])

// Compute r = e * p_0
r := new(bls12381.G2Affine)
r.ScalarMultiplication(p0, e)

// Encode the G2 point into 256 bytes
return encodePointG2(r), nil
}

// bls12381G2MultiExp implements EIP-2537 G2MultiExp precompile.
type bls12381G2MultiExp struct{}

Expand All @@ -920,10 +840,10 @@ func (c *bls12381G2MultiExp) RequiredGas(input []byte) uint64 {
}
// Lookup discount value for G2 point, scalar value pair length
var discount uint64
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
discount = params.Bls12381MultiExpDiscountTable[k-1]
if dLen := len(params.Bls12381MSMDiscountTableG2); k < dLen {
discount = params.Bls12381MSMDiscountTableG2[k-1]
} else {
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
discount = params.Bls12381MSMDiscountTableG2[dLen-1]
}
// Calculate gas and return the result
return (uint64(k) * params.Bls12381G2MulGas * discount) / 1000
Expand Down
34 changes: 1 addition & 33 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ var allPrecompiles = map[libcommon.Address]PrecompiledContract{
libcommon.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
libcommon.BytesToAddress([]byte{9}): &blake2F{},
libcommon.BytesToAddress([]byte{10}): &bls12381G1Add{},
libcommon.BytesToAddress([]byte{11}): &bls12381G1Mul{},
libcommon.BytesToAddress([]byte{12}): &bls12381G1MultiExp{},
libcommon.BytesToAddress([]byte{13}): &bls12381G2Add{},
libcommon.BytesToAddress([]byte{14}): &bls12381G2Mul{},
libcommon.BytesToAddress([]byte{15}): &bls12381G2MultiExp{},
libcommon.BytesToAddress([]byte{16}): &bls12381Pairing{},
libcommon.BytesToAddress([]byte{17}): &bls12381MapFpToG1{},
Expand Down Expand Up @@ -313,10 +311,8 @@ func benchJson(name, addr string, b *testing.B) {
}

func TestPrecompiledBLS12381G1Add(t *testing.T) { testJson("blsG1Add", "0a", t) }
func TestPrecompiledBLS12381G1Mul(t *testing.T) { testJson("blsG1Mul", "0b", t) }
func TestPrecompiledBLS12381G1MultiExp(t *testing.T) { testJson("blsG1MultiExp", "0c", t) }
func TestPrecompiledBLS12381G2Add(t *testing.T) { testJson("blsG2Add", "0d", t) }
func TestPrecompiledBLS12381G2Mul(t *testing.T) { testJson("blsG2Mul", "0e", t) }
func TestPrecompiledBLS12381G2MultiExp(t *testing.T) { testJson("blsG2MultiExp", "0f", t) }
func TestPrecompiledBLS12381Pairing(t *testing.T) { testJson("blsPairing", "10", t) }
func TestPrecompiledBLS12381MapG1(t *testing.T) { testJson("blsMapG1", "11", t) }
Expand All @@ -327,48 +323,20 @@ func BenchmarkPrecompiledBLS12381G1Add(b *testing.B) { benchJson("blsG1Add"
func BenchmarkPrecompiledBLS12381G1Mul(b *testing.B) { benchJson("blsG1Mul", "0b", b) }
func BenchmarkPrecompiledBLS12381G1MultiExp(b *testing.B) { benchJson("blsG1MultiExp", "0c", b) }
func BenchmarkPrecompiledBLS12381G2Add(b *testing.B) { benchJson("blsG2Add", "0d", b) }
func BenchmarkPrecompiledBLS12381G2Mul(b *testing.B) { benchJson("blsG2Mul", "0e", b) }
func BenchmarkPrecompiledBLS12381G2MultiExp(b *testing.B) { benchJson("blsG2MultiExp", "0f", b) }
func BenchmarkPrecompiledBLS12381Pairing(b *testing.B) { benchJson("blsPairing", "10", b) }
func BenchmarkPrecompiledBLS12381MapG1(b *testing.B) { benchJson("blsMapG1", "11", b) }
func BenchmarkPrecompiledBLS12381MapG2(b *testing.B) { benchJson("blsMapG2", "12", b) }

// Failure tests
func TestPrecompiledBLS12381G1AddFail(t *testing.T) { testJsonFail("blsG1Add", "0a", t) }
func TestPrecompiledBLS12381G1MulFail(t *testing.T) { testJsonFail("blsG1Mul", "0b", t) }
func TestPrecompiledBLS12381G1MultiExpFail(t *testing.T) { testJsonFail("blsG1MultiExp", "0c", t) }
func TestPrecompiledBLS12381G2AddFail(t *testing.T) { testJsonFail("blsG2Add", "0d", t) }
func TestPrecompiledBLS12381G2MulFail(t *testing.T) { testJsonFail("blsG2Mul", "0e", t) }
func TestPrecompiledBLS12381G2MultiExpFail(t *testing.T) { testJsonFail("blsG2MultiExp", "0f", t) }
func TestPrecompiledBLS12381PairingFail(t *testing.T) { testJsonFail("blsPairing", "10", t) }
func TestPrecompiledBLS12381MapG1Fail(t *testing.T) { testJsonFail("blsMapG1", "11", t) }
func TestPrecompiledBLS12381MapG2Fail(t *testing.T) { testJsonFail("blsMapG2", "12", t) }

// Tests from https://github.com/ethereum/EIPs/tree/master/assets/eip-2537
func TestPrecompiledBLS12381G1AddEip(t *testing.T) { testJson("blsG1Add-eip", "0a", t) }
func TestPrecompiledBLS12381G1MulEip(t *testing.T) { testJson("blsG1Mul-eip", "0b", t) }
func TestPrecompiledBLS12381G1MultiExpEip(t *testing.T) { testJson("blsG1MultiExp-eip", "0c", t) }
func TestPrecompiledBLS12381G2AddEip(t *testing.T) { testJson("blsG2Add-eip", "0d", t) }
func TestPrecompiledBLS12381G2MulEip(t *testing.T) { testJson("blsG2Mul-eip", "0e", t) }
func TestPrecompiledBLS12381G2MultiExpEip(t *testing.T) { testJson("blsG2MultiExp-eip", "0f", t) }
func TestPrecompiledBLS12381PairingEip(t *testing.T) { testJson("blsPairing-eip", "10", t) }
func TestPrecompiledBLS12381MapG1Eip(t *testing.T) { testJson("blsMapG1-eip", "11", t) }
func TestPrecompiledBLS12381MapG2Eip(t *testing.T) { testJson("blsMapG2-eip", "12", t) }

func TestPrecompiledBLS12381G1AddFailEip(t *testing.T) { testJsonFail("blsG1Add-eip", "0a", t) }
func TestPrecompiledBLS12381G1MulFailEip(t *testing.T) { testJsonFail("blsG1Mul-eip", "0b", t) }
func TestPrecompiledBLS12381G1MultiExpFailEip(t *testing.T) {
testJsonFail("blsG1MultiExp-eip", "0c", t)
}
func TestPrecompiledBLS12381G2AddFailEip(t *testing.T) { testJsonFail("blsG2Add-eip", "0d", t) }
func TestPrecompiledBLS12381G2MulFailEip(t *testing.T) { testJsonFail("blsG2Mul-eip", "0e", t) }
func TestPrecompiledBLS12381G2MultiExpFailEip(t *testing.T) {
testJsonFail("blsG2MultiExp-eip", "0f", t)
}
func TestPrecompiledBLS12381PairingFailEip(t *testing.T) { testJsonFail("blsPairing-eip", "10", t) }
func TestPrecompiledBLS12381MapG1FailEip(t *testing.T) { testJsonFail("blsMapG1-eip", "11", t) }
func TestPrecompiledBLS12381MapG2FailEip(t *testing.T) { testJsonFail("blsMapG2-eip", "12", t) }

func loadJson(name string) ([]precompiledTest, error) {
data, err := os.ReadFile(fmt.Sprintf("testdata/precompiles/%v.json", name))
if err != nil {
Expand Down Expand Up @@ -439,7 +407,7 @@ func BenchmarkPrecompiledP256Verify(b *testing.B) {
}

func TestPrecompiledP256Verify(t *testing.T) {
t.Parallel()
// t.Parallel()
Copy link
Member

Choose a reason for hiding this comment

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

Why was it commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will uncomment. Needed it for testing.


testJson("p256Verify", "100", t)
}
Loading
Loading