Skip to content

Commit b4e09c8

Browse files
more consensus layer support in kzg package
1 parent 8891367 commit b4e09c8

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

crypto/kzg/kzg_bytes.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ import (
1414
type KZGCommitment [48]byte
1515
type KZGProof [48]byte
1616
type VersionedHash [32]byte
17+
type Root [32]byte
18+
type Slot uint64
19+
20+
type BlobsSidecar struct {
21+
BeaconBlockRoot Root
22+
BeaconBlockSlot Slot
23+
Blobs BlobSequence
24+
KZGAggregatedProof KZGProof
25+
}
1726

1827
type BlobSequence interface {
1928
Len() int
@@ -30,6 +39,10 @@ type KZGCommitmentSequence interface {
3039
At(int) KZGCommitment
3140
}
3241

42+
var (
43+
invalidKZGProofError = errors.New("invalid kzg proof")
44+
)
45+
3346
// PointEvaluationPrecompile implements point_evaluation_precompile from EIP-4844
3447
func PointEvaluationPrecompile(input []byte) ([]byte, error) {
3548
if len(input) != 192 {
@@ -61,7 +74,7 @@ func PointEvaluationPrecompile(input []byte) ([]byte, error) {
6174
return nil, fmt.Errorf("verify_kzg_proof error: %v", err)
6275
}
6376
if !ok {
64-
return nil, errors.New("failed to verify kzg proof")
77+
return nil, invalidKZGProofError
6578
}
6679
return []byte{}, nil
6780
}
@@ -136,3 +149,30 @@ func ComputeAggregateKZGProof(blobs BlobSequence) (KZGProof, error) {
136149
}
137150
return ComputeAggregateKZGProofFromPolynomials(polynomials)
138151
}
152+
153+
// ValidateBlobsSidecar implements validate_blobs_sidecar from the EIP-4844 consensus spec:
154+
// https://github.com/roberto-bayardo/consensus-specs/blob/dev/specs/eip4844/beacon-chain.md#validate_blobs_sidecar
155+
func ValidateBlobsSidecar(slot Slot, beaconBlockRoot Root, expectedKZGCommitments KZGCommitmentSequence, blobsSidecar BlobsSidecar) error {
156+
if slot != blobsSidecar.BeaconBlockSlot {
157+
return fmt.Errorf(
158+
"slot doesn't match sidecar's beacon block slot (%v != %v)",
159+
slot, blobsSidecar.BeaconBlockSlot)
160+
}
161+
if beaconBlockRoot != blobsSidecar.BeaconBlockRoot {
162+
return errors.New("roots not equal")
163+
}
164+
blobs := blobsSidecar.Blobs
165+
if blobs.Len() != expectedKZGCommitments.Len() {
166+
return fmt.Errorf(
167+
"blob len doesn't match expected kzg commitments len (%v != %v)",
168+
blobs.Len(), expectedKZGCommitments.Len())
169+
}
170+
ok, err := VerifyAggregateKZGProof(blobs, expectedKZGCommitments, blobsSidecar.KZGAggregatedProof)
171+
if err != nil {
172+
return fmt.Errorf("verify_aggregate_kzg_proof error: %v", err)
173+
}
174+
if !ok {
175+
return invalidKZGProofError
176+
}
177+
return nil
178+
}

0 commit comments

Comments
 (0)