Skip to content

Commit f3a382f

Browse files
authored
Merge pull request ethereum#109 from kevaundray/kw/add-kzg-file
Add KZG file to go-ethereum
2 parents 22f9817 + 23b9a14 commit f3a382f

File tree

3 files changed

+119
-18
lines changed

3 files changed

+119
-18
lines changed

core/types/data_blob.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"fmt"
77
"io"
88

9-
"github.com/crate-crypto/go-proto-danksharding-crypto/eth"
10-
api "github.com/crate-crypto/go-proto-danksharding-crypto/serialization"
9+
"github.com/crate-crypto/go-proto-danksharding-crypto/serialization"
1110
"github.com/ethereum/go-ethereum/common"
1211
"github.com/ethereum/go-ethereum/common/hexutil"
12+
"github.com/ethereum/go-ethereum/crypto/kzg"
1313
"github.com/ethereum/go-ethereum/params"
1414

1515
"github.com/protolambda/ztyp/codec"
@@ -51,7 +51,7 @@ func (p *KZGCommitment) UnmarshalText(text []byte) error {
5151
}
5252

5353
func (c KZGCommitment) ComputeVersionedHash() common.Hash {
54-
return common.Hash(eth.KZGToVersionedHash(api.KZGCommitment(c)))
54+
return common.Hash(kzg.KZGToVersionedHash(serialization.KZGCommitment(c)))
5555
}
5656

5757
// Compressed BLS12-381 G1 element
@@ -266,41 +266,41 @@ func (blobs Blobs) ComputeCommitmentsAndProofs() (commitments []KZGCommitment, v
266266
versionedHashes = make([]common.Hash, len(blobs))
267267

268268
for i, blob := range blobs {
269-
commitment, err := eth.CryptoCtx.BlobToKZGCommitment(blob)
269+
commitment, err := kzg.CryptoCtx.BlobToKZGCommitment(blob)
270270
if err != nil {
271271
return nil, nil, nil, fmt.Errorf("could not convert blob to commitment: %v", err)
272272
}
273273

274-
proof, err := eth.CryptoCtx.ComputeBlobKZGProof(blob, commitment)
274+
proof, err := kzg.CryptoCtx.ComputeBlobKZGProof(blob, commitment)
275275
if err != nil {
276276
return nil, nil, nil, fmt.Errorf("could not compute proof for blob: %v", err)
277277
}
278278
commitments[i] = KZGCommitment(commitment)
279279
proofs[i] = KZGProof(proof)
280-
versionedHashes[i] = common.Hash(eth.KZGToVersionedHash(commitment))
280+
versionedHashes[i] = common.Hash(kzg.KZGToVersionedHash(commitment))
281281
}
282282

283283
return commitments, versionedHashes, proofs, nil
284284
}
285285

286-
func toBlobs(_blobs Blobs) []api.Blob {
287-
blobs := make([]api.Blob, len(_blobs))
286+
func toBlobs(_blobs Blobs) []serialization.Blob {
287+
blobs := make([]serialization.Blob, len(_blobs))
288288
for i, _blob := range _blobs {
289-
blobs[i] = api.Blob(_blob)
289+
blobs[i] = serialization.Blob(_blob)
290290
}
291291
return blobs
292292
}
293-
func toComms(_comms BlobKzgs) []api.KZGCommitment {
294-
comms := make([]api.KZGCommitment, len(_comms))
293+
func toComms(_comms BlobKzgs) []serialization.KZGCommitment {
294+
comms := make([]serialization.KZGCommitment, len(_comms))
295295
for i, _comm := range _comms {
296-
comms[i] = api.KZGCommitment(_comm)
296+
comms[i] = serialization.KZGCommitment(_comm)
297297
}
298298
return comms
299299
}
300-
func toProofs(_proofs KZGProofs) []api.KZGProof {
301-
proofs := make([]api.KZGProof, len(_proofs))
300+
func toProofs(_proofs KZGProofs) []serialization.KZGProof {
301+
proofs := make([]serialization.KZGProof, len(_proofs))
302302
for i, _proof := range _proofs {
303-
proofs[i] = api.KZGProof(_proof)
303+
proofs[i] = serialization.KZGProof(_proof)
304304
}
305305
return proofs
306306
}
@@ -357,7 +357,7 @@ func (b *BlobTxWrapData) validateBlobTransactionWrapper(inner TxData) error {
357357
if l1 > params.MaxBlobsPerBlock {
358358
return fmt.Errorf("number of blobs exceeds max: %v", l1)
359359
}
360-
err := eth.CryptoCtx.VerifyBlobKZGProofBatch(toBlobs(b.Blobs), toComms(b.BlobKzgs), toProofs(b.Proofs))
360+
err := kzg.CryptoCtx.VerifyBlobKZGProofBatch(toBlobs(b.Blobs), toComms(b.BlobKzgs), toProofs(b.Proofs))
361361
if err != nil {
362362
return fmt.Errorf("error during proof verification: %v", err)
363363
}

core/vm/contracts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import (
2222
"errors"
2323
"math/big"
2424

25-
"github.com/crate-crypto/go-proto-danksharding-crypto/eth"
2625
"github.com/ethereum/go-ethereum/common"
2726
"github.com/ethereum/go-ethereum/common/math"
2827
"github.com/ethereum/go-ethereum/crypto"
2928
"github.com/ethereum/go-ethereum/crypto/blake2b"
3029
"github.com/ethereum/go-ethereum/crypto/bls12381"
3130
"github.com/ethereum/go-ethereum/crypto/bn256"
31+
"github.com/ethereum/go-ethereum/crypto/kzg"
3232
"github.com/ethereum/go-ethereum/params"
3333
"golang.org/x/crypto/ripemd160"
3434
)
@@ -1079,5 +1079,5 @@ func (c *pointEvaluation) RequiredGas(input []byte) uint64 {
10791079
}
10801080

10811081
func (c *pointEvaluation) Run(input []byte) ([]byte, error) {
1082-
return eth.PointEvaluationPrecompile(input)
1082+
return kzg.PointEvaluationPrecompile(input)
10831083
}

crypto/kzg/kzg.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package kzg
2+
3+
import (
4+
"crypto/sha256"
5+
"errors"
6+
"fmt"
7+
"math/big"
8+
9+
"github.com/crate-crypto/go-proto-danksharding-crypto/api"
10+
"github.com/crate-crypto/go-proto-danksharding-crypto/serialization"
11+
)
12+
13+
const (
14+
BlobCommitmentVersionKZG uint8 = 0x01
15+
FieldElementsPerBlob int = 4096
16+
)
17+
18+
type VersionedHash [32]byte
19+
type Root [32]byte
20+
type Slot uint64
21+
22+
type BlobsSidecar struct {
23+
BeaconBlockRoot Root
24+
BeaconBlockSlot Slot
25+
Blobs []serialization.Blob
26+
Proofs []serialization.KZGProof
27+
}
28+
29+
const (
30+
BlobTxType = 5
31+
PrecompileInputLength = 192
32+
BlobVersionedHashesOffset = 258 // position of blob_versioned_hashes offset in a serialized blob tx, see TxPeekBlobVersionedHashes
33+
)
34+
35+
var (
36+
errInvalidInputLength = errors.New("invalid input length")
37+
)
38+
39+
// The value that gets returned when the `verify_kzg_proof“ precompile is called
40+
var precompileReturnValue [64]byte
41+
42+
// The context object stores all of the necessary configurations
43+
// to allow one to create and verify blob proofs
44+
var CryptoCtx api.Context
45+
46+
func init() {
47+
// Initialize context to match the configurations that the
48+
// specs are using.
49+
ctx, err := api.NewContext4096Insecure1337()
50+
if err != nil {
51+
panic(fmt.Sprintf("could not create context, err : %v", err))
52+
}
53+
CryptoCtx = *ctx
54+
// Initialize the precompile return value
55+
new(big.Int).SetUint64(serialization.ScalarsPerBlob).FillBytes(precompileReturnValue[:32])
56+
copy(precompileReturnValue[32:], api.MODULUS[:])
57+
}
58+
59+
// PointEvaluationPrecompile implements point_evaluation_precompile from EIP-4844
60+
func PointEvaluationPrecompile(input []byte) ([]byte, error) {
61+
if len(input) != PrecompileInputLength {
62+
return nil, errInvalidInputLength
63+
}
64+
// versioned hash: first 32 bytes
65+
var versionedHash [32]byte
66+
copy(versionedHash[:], input[:32])
67+
68+
var x, y [32]byte
69+
// Evaluation point: next 32 bytes
70+
copy(x[:], input[32:64])
71+
// Expected output: next 32 bytes
72+
copy(y[:], input[64:96])
73+
74+
// input kzg point: next 48 bytes
75+
var dataKZG [48]byte
76+
copy(dataKZG[:], input[96:144])
77+
if KZGToVersionedHash(serialization.KZGCommitment(dataKZG)) != VersionedHash(versionedHash) {
78+
return nil, errors.New("mismatched versioned hash")
79+
}
80+
81+
// Quotient kzg: next 48 bytes
82+
var quotientKZG [48]byte
83+
copy(quotientKZG[:], input[144:PrecompileInputLength])
84+
85+
err := CryptoCtx.VerifyKZGProof(dataKZG, quotientKZG, x, y)
86+
if err != nil {
87+
return nil, fmt.Errorf("verify_kzg_proof error: %v", err)
88+
}
89+
90+
result := precompileReturnValue // copy the value
91+
92+
return result[:], nil
93+
}
94+
95+
// KZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844
96+
func KZGToVersionedHash(kzg serialization.KZGCommitment) VersionedHash {
97+
h := sha256.Sum256(kzg[:])
98+
h[0] = BlobCommitmentVersionKZG
99+
100+
return VersionedHash(h)
101+
}

0 commit comments

Comments
 (0)