Skip to content

Commit 0a06c7c

Browse files
author
jagdeep sidhu
committed
fix blob verification precompile
1 parent 281fcfd commit 0a06c7c

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

core/types/data_blob.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,16 @@ func (n *NEVMBlob) FromWire(NEVMBlobWire *wire.NEVMBlob) error {
116116
if lenBlob > params.FieldElementsPerBlob {
117117
return errors.New("Blob too big")
118118
}
119+
if lenBlob < 1024 {
120+
return errors.New("Blob too small")
121+
}
122+
if lenBlob%32 != 0 {
123+
return errors.New("Blob should be a factor of 32")
124+
}
119125
n.Blob = make([]bls.Fr, params.FieldElementsPerBlob)
126+
numElements := lenBlob/32
120127
var inputPoint [32]byte
121-
for i := 0; i < lenBlob; i++ {
128+
for i := 0; i < numElements; i++ {
122129
copy(inputPoint[:32], NEVMBlobWire.Blob[i*32:(i+1)*32])
123130
ok := bls.FrFrom32(&n.Blob[i], inputPoint)
124131
if ok != true {
@@ -135,9 +142,16 @@ func (n *NEVMBlob) FromBytes(blob []byte) error {
135142
if lenBlob > params.FieldElementsPerBlob {
136143
return errors.New("Blob too big")
137144
}
145+
if lenBlob < 1024 {
146+
return errors.New("Blob too small")
147+
}
148+
if lenBlob%32 != 0 {
149+
return errors.New("Blob should be a factor of 32")
150+
}
138151
n.Blob = make([]bls.Fr, params.FieldElementsPerBlob)
152+
numElements := lenBlob/32
139153
var inputPoint [32]byte
140-
for i := 0; i < lenBlob; i++ {
154+
for i := 0; i < numElements; i++ {
141155
copy(inputPoint[:32], blob[i*32:(i+1)*32])
142156
ok := bls.FrFrom32(&n.Blob[i], inputPoint)
143157
if ok != true {

core/vm/contracts.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,12 +1094,12 @@ type blobVerification struct{}
10941094

10951095
// RequiredGas returns the gas required to execute the pre-compiled contract.
10961096
func (c *blobVerification) RequiredGas(input []byte) uint64 {
1097-
// 4096 (4096*32) is base gas (BlobVerificationGas), anything above should scale up the cost
1097+
// 4096 (4096*32 + 32) is base gas (BlobVerificationGas), anything above should scale up the cost
10981098
return params.BlobVerificationGas * (uint64(len(input))/131104)
10991099
}
11001100

11011101
func (c *blobVerification) Run(input []byte, interpreter *EVMInterpreter) ([]byte, error) {
1102-
if len(input) != 32+(32*params.FieldElementsPerBlob) {
1102+
if len(input) < 32+(32*32) || len(input) > 32+(32*params.FieldElementsPerBlob) {
11031103
return nil, errBlobVerificationInputLength
11041104
}
11051105

@@ -1110,10 +1110,14 @@ func (c *blobVerification) Run(input []byte, interpreter *EVMInterpreter) ([]byt
11101110
}
11111111

11121112
input = input[32:] // skip forward to the input points
1113-
1113+
lenInput := len(input)
1114+
if (lenInput%32) != 0 {
1115+
return nil, errInvalidChunk
1116+
}
1117+
numElements := lenInput/32
11141118
inputPoints := make([]bls.Fr, params.FieldElementsPerBlob, params.FieldElementsPerBlob)
11151119
var inputPoint [32]byte
1116-
for i := 0; i < params.FieldElementsPerBlob; i++ {
1120+
for i := 0; i < numElements; i++ {
11171121
copy(inputPoint[:32], input[i*32:(i+1)*32])
11181122
ok := bls.FrFrom32(&inputPoints[i], inputPoint)
11191123
if ok != true {

tests/kzg_bench_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
func randomBlob() []bls.Fr {
1616
blob := make([]bls.Fr, params.FieldElementsPerBlob)
17-
for i := 0; i < len(blob); i++ {
17+
for i := 0; i < params.FieldElementsPerBlob; i++ {
1818
blob[i] = *bls.RandomFr()
1919
}
2020
return blob
@@ -27,7 +27,7 @@ func BenchmarkBlobToKzg(b *testing.B) {
2727
kzg.BlobToKzg(blob)
2828
}
2929
}
30-
func BenchmarkVerify(b *testing.B) {
30+
func BenchmarkVerifyBlob(b *testing.B) {
3131
var blobs types.NEVMBlobs
3232
blobs.Blobs = make([]*types.NEVMBlob, 32)
3333
for i := 0; i < 32; i++ {

0 commit comments

Comments
 (0)