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: 4844 opcode and precompile #27356

Merged
merged 3 commits into from Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions core/vm/contracts.go
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -1081,7 +1082,7 @@ func (b *kzgPointEvaluation) RequiredGas(input []byte) uint64 {

const (
blobVerifyInputLength = 192 // Max input length for the point evaluation precompile.
blobVerifyKZGVersion uint8 = 0x01 // Version byte for the point evaluation precompile.
blobCommitmentVersionKZG uint8 = 0x01 // Version byte for the point evaluation precompile.
Copy link
Member

Choose a reason for hiding this comment

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

This is part of the params package, shouldn't we use that?

blobPrecompileReturnValue = "000000000000000000000000000000000000000000000000000000000000100073eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"
)

Expand All @@ -1098,39 +1099,39 @@ func (b *kzgPointEvaluation) Run(input []byte) ([]byte, error) {
}
// versioned hash: first 32 bytes
var versionedHash common.Hash
copy(versionedHash[:], input[:32])
copy(versionedHash[:], input[:])

var (
point kzg4844.Point
claim kzg4844.Claim
)
// Evaluation point: next 32 bytes
copy(point[:], input[32:64])
copy(point[:], input[32:])
// Expected output: next 32 bytes
copy(claim[:], input[64:96])
copy(claim[:], input[64:])

// input kzg point: next 48 bytes
var commitment kzg4844.Commitment
copy(commitment[:], input[96:144])
if KZGToVersionedHash(commitment) != versionedHash {
copy(commitment[:], input[96:])
if kZGToVersionedHash(commitment) != versionedHash {
return nil, errBlobVerifyMismatchedVersion
}

// Proof: next 48 bytes
var proof kzg4844.Proof
copy(proof[:], input[144:blobVerifyInputLength])
copy(proof[:], input[144:])

if err := kzg4844.VerifyProof(commitment, point, claim, proof); err != nil {
return nil, errors.Join(errBlobVerifyKZGProof, err)
return nil, fmt.Errorf("%w: %v", errBlobVerifyKZGProof, err)
}

return common.Hex2Bytes(blobPrecompileReturnValue), nil
}

// KZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844
func KZGToVersionedHash(kzg kzg4844.Commitment) common.Hash {
// kZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844
func kZGToVersionedHash(kzg kzg4844.Commitment) common.Hash {
h := sha256.Sum256(kzg[:])
h[0] = blobVerifyKZGVersion
h[0] = blobCommitmentVersionKZG

return h
}
6 changes: 1 addition & 5 deletions core/vm/contracts_test.go
Expand Up @@ -25,7 +25,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
)

// precompiledTest defines the input/output pairs for precompiled contract tests.
Expand Down Expand Up @@ -313,10 +312,7 @@ func TestPrecompiledBLS12381G2MultiExp(t *testing.T) { testJson("blsG2MultiExp",
func TestPrecompiledBLS12381Pairing(t *testing.T) { testJson("blsPairing", "10", t) }
func TestPrecompiledBLS12381MapG1(t *testing.T) { testJson("blsMapG1", "11", t) }
func TestPrecompiledBLS12381MapG2(t *testing.T) { testJson("blsMapG2", "12", t) }
func TestPrecompiledPointEvaluation(t *testing.T) {
kzg4844.UseCKZG(false) // We need to call this to compute the PrecompileReturnValue
testJson("pointEvaluation", "14", t)
}
func TestPrecompiledPointEvaluation(t *testing.T) { testJson("pointEvaluation", "14", t) }

func BenchmarkPrecompiledBLS12381G1Add(b *testing.B) { benchJson("blsG1Add", "0a", b) }
func BenchmarkPrecompiledBLS12381G1Mul(b *testing.B) { benchJson("blsG1Mul", "0b", b) }
Expand Down