Skip to content

Commit

Permalink
core: crypto: fixed nitpicks, moved precompile return value
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed May 31, 2023
1 parent d49ed53 commit 5e0918a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 34 deletions.
25 changes: 13 additions & 12 deletions core/vm/contracts.go
Expand Up @@ -92,7 +92,7 @@ var PrecompiledContractsBerlin = map[common.Address]PrecompiledContract{
}

// PrecompiledContractsCancun contains the default set of pre-compiled Ethereum
// contracts used in the Berlin release.
// contracts used in the Cancun release.
var PrecompiledContractsCancun = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
Expand All @@ -103,7 +103,7 @@ var PrecompiledContractsCancun = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
common.BytesToAddress([]byte{20}): &pointEvaluation{},
common.BytesToAddress([]byte{20}): &kzgPointEvaluation{},
}

// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
Expand Down Expand Up @@ -1071,17 +1071,18 @@ func (c *bls12381MapG2) Run(input []byte) ([]byte, error) {
return g.EncodePoint(r), nil
}

// pointEvaluation implements the EIP-4844 point evaluation precompile.
type pointEvaluation struct{}
// kzgPointEvaluation implements the EIP-4844 point evaluation precompile.
type kzgPointEvaluation struct{}

// RequiredGas estimates the gas required for running the point evaluation precompile.
func (b *pointEvaluation) RequiredGas(input []byte) uint64 {
return params.PointEvaluationPrecompileGas
func (b *kzgPointEvaluation) RequiredGas(input []byte) uint64 {
return params.BlobTxPointEvaluationPrecompileGas
}

const (
pointBlobVerifyInputLength = 192 // Max input length for the point evaluation precompile.
blobVerifyKZGVersion uint8 = 0x01 // Version byte for the point evaluation precompile.
blobVerifyInputLength = 192 // Max input length for the point evaluation precompile.
blobVerifyKZGVersion uint8 = 0x01 // Version byte for the point evaluation precompile.
blobPrecompileReturnValue = "000000000000000000000000000000000000000000000000000000000000100073eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"
)

var (
Expand All @@ -1091,8 +1092,8 @@ var (
)

// Run executes the point evaluation precompile.
func (b *pointEvaluation) Run(input []byte) ([]byte, error) {
if len(input) != pointBlobVerifyInputLength {
func (b *kzgPointEvaluation) Run(input []byte) ([]byte, error) {
if len(input) != blobVerifyInputLength {
return nil, errBlobVerifyInvalidInputLength
}
// versioned hash: first 32 bytes
Expand All @@ -1117,13 +1118,13 @@ func (b *pointEvaluation) Run(input []byte) ([]byte, error) {

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

if err := kzg4844.VerifyProof(commitment, point, claim, proof); err != nil {
return nil, errors.Join(errBlobVerifyKZGProof, err)
}

return common.CopyBytes(kzg4844.PrecompileReturnValue[:]), nil
return common.Hex2Bytes(blobPrecompileReturnValue), nil
}

// KZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844
Expand Down
2 changes: 1 addition & 1 deletion core/vm/contracts_test.go
Expand Up @@ -66,7 +66,7 @@ var allPrecompiles = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{16}): &bls12381Pairing{},
common.BytesToAddress([]byte{17}): &bls12381MapG1{},
common.BytesToAddress([]byte{18}): &bls12381MapG2{},
common.BytesToAddress([]byte{20}): &pointEvaluation{},
common.BytesToAddress([]byte{20}): &kzgPointEvaluation{},
}

// EIP-152 test vectors
Expand Down
4 changes: 0 additions & 4 deletions crypto/kzg4844/kzg4844.go
Expand Up @@ -41,8 +41,6 @@ type Point [32]byte
// Claim is a claimed evaluation value in a specific point.
type Claim [32]byte

var PrecompileReturnValue [64]byte

// useCKZG controls whether the cryptography should use the Go or C backend.
var useCKZG atomic.Bool

Expand All @@ -63,8 +61,6 @@ func UseCKZG(use bool) error {
} else {
gokzgIniter.Do(gokzgInit)
}
// Compute the precompile return value using go-kzg
PrecompileReturnValue = goComputePrecompileReturnValue()
return nil
}

Expand Down
10 changes: 0 additions & 10 deletions crypto/kzg4844/kzg4844_gokzg.go
Expand Up @@ -17,7 +17,6 @@
package kzg4844

import (
"encoding/binary"
"encoding/json"
"sync"

Expand Down Expand Up @@ -97,12 +96,3 @@ func gokzgVerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {

return context.VerifyBlobKZGProof((gokzg4844.Blob)(blob), (gokzg4844.KZGCommitment)(commitment), (gokzg4844.KZGProof)(proof))
}

// goComputePrecompileReturnValue computes the precompile return value which is
// FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS as padded 32 byte big endian values.
func goComputePrecompileReturnValue() [64]byte {
var precompileReturnValue [64]byte
binary.BigEndian.PutUint64(precompileReturnValue[24:], gokzg4844.ScalarsPerBlob)
copy(precompileReturnValue[32:], gokzg4844.BlsModulus[:])
return precompileReturnValue
}
14 changes: 7 additions & 7 deletions params/protocol_params.go
Expand Up @@ -160,13 +160,13 @@ const (
RefundQuotient uint64 = 2
RefundQuotientEIP3529 uint64 = 5

BlobTxHashVersion = 0x01 // Version byte of the commitment hash
BlobTxMaxDataGasPerBlock = 1 << 19 // Maximum consumable data gas for data blobs per block
BlobTxTargetDataGasPerBlock = 1 << 18 // Target consumable data gas for data blobs per block (for 1559-like pricing)
BlobTxDataGasPerBlob = 1 << 17 // Gas consumption of a single data blob (== blob byte size)
BlobTxMinDataGasprice = 1 // Minimum gas price for data blobs
BlobTxDataGaspriceUpdateFraction = 2225652 // Controls the maximum rate of change for data gas price
PointEvaluationPrecompileGas = 50000 // Gas price for the point evaluation precompile.
BlobTxHashVersion = 0x01 // Version byte of the commitment hash
BlobTxMaxDataGasPerBlock = 1 << 19 // Maximum consumable data gas for data blobs per block
BlobTxTargetDataGasPerBlock = 1 << 18 // Target consumable data gas for data blobs per block (for 1559-like pricing)
BlobTxDataGasPerBlob = 1 << 17 // Gas consumption of a single data blob (== blob byte size)
BlobTxMinDataGasprice = 1 // Minimum gas price for data blobs
BlobTxDataGaspriceUpdateFraction = 2225652 // Controls the maximum rate of change for data gas price
BlobTxPointEvaluationPrecompileGas = 50000 // Gas price for the point evaluation precompile.
)

// Gas discount table for BLS12-381 G1 and G2 multi exponentiation operations
Expand Down

0 comments on commit 5e0918a

Please sign in to comment.