Skip to content

Commit

Permalink
Extract blob key computation logic into static function
Browse files Browse the repository at this point in the history
Signed-off-by: litt3 <[email protected]>
  • Loading branch information
litt3 committed Jan 28, 2025
1 parent fd4444f commit 05126c3
Showing 1 changed file with 90 additions and 62 deletions.
152 changes: 90 additions & 62 deletions core/v2/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"math/big"

"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/wealdtech/go-merkletree/v2"
"github.com/wealdtech/go-merkletree/v2/keccak256"
Expand All @@ -27,7 +29,20 @@ type abiBlobCommitments struct {
DataLength uint32
}

func (b *BlobHeader) BlobKey() (BlobKey, error) {
// ComputeBlobKey accepts as parameters the elements which contribute to the hash of a BlobHeader. It computes the
// hash and returns the result, which represents a BlobKey.
//
// This function exists so that the BlobKey can be computed without first constructing a BlobHeader object. Since
// the BlobHeader contains the full payment metadata, and payment metadata isn't stored on chain, it isn't always
// possible to reconstruct from the data available.
func ComputeBlobKey(
blobVersion BlobVersion,
blobCommitments encoding.BlobCommitments,
quorumNumbers []core.QuorumID,
paymentMetadataHash [32]byte,
salt uint32,
) ([32]byte, error) {

versionType, err := abi.NewType("uint16", "", nil)
if err != nil {
return [32]byte{}, err
Expand All @@ -40,54 +55,55 @@ func (b *BlobHeader) BlobKey() (BlobKey, error) {
if err != nil {
return [32]byte{}, err
}
commitmentType, err := abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{
Name: "commitment",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256",
},
{
Name: "Y",
Type: "uint256",
commitmentType, err := abi.NewType(
"tuple", "", []abi.ArgumentMarshaling{
{
Name: "commitment",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256",
},
{
Name: "Y",
Type: "uint256",
},
},
},
},
{
Name: "lengthCommitment",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256[2]",
},
{
Name: "Y",
Type: "uint256[2]",
{
Name: "lengthCommitment",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256[2]",
},
{
Name: "Y",
Type: "uint256[2]",
},
},
},
},
{
Name: "lengthProof",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256[2]",
},
{
Name: "Y",
Type: "uint256[2]",
{
Name: "lengthProof",
Type: "tuple",
Components: []abi.ArgumentMarshaling{
{
Name: "X",
Type: "uint256[2]",
},
{
Name: "Y",
Type: "uint256[2]",
},
},
},
},
{
Name: "dataLength",
Type: "uint32",
},
})
{
Name: "dataLength",
Type: "uint32",
},
})
if err != nil {
return [32]byte{}, err
}
Expand All @@ -107,36 +123,36 @@ func (b *BlobHeader) BlobKey() (BlobKey, error) {
}

packedBytes, err := arguments.Pack(
b.BlobVersion,
b.QuorumNumbers,
blobVersion,
quorumNumbers,
abiBlobCommitments{
Commitment: abiG1Commit{
X: b.BlobCommitments.Commitment.X.BigInt(new(big.Int)),
Y: b.BlobCommitments.Commitment.Y.BigInt(new(big.Int)),
X: blobCommitments.Commitment.X.BigInt(new(big.Int)),
Y: blobCommitments.Commitment.Y.BigInt(new(big.Int)),
},
LengthCommitment: abiG2Commit{
X: [2]*big.Int{
b.BlobCommitments.LengthCommitment.X.A0.BigInt(new(big.Int)),
b.BlobCommitments.LengthCommitment.X.A1.BigInt(new(big.Int)),
blobCommitments.LengthCommitment.X.A0.BigInt(new(big.Int)),
blobCommitments.LengthCommitment.X.A1.BigInt(new(big.Int)),
},
Y: [2]*big.Int{
b.BlobCommitments.LengthCommitment.Y.A0.BigInt(new(big.Int)),
b.BlobCommitments.LengthCommitment.Y.A1.BigInt(new(big.Int)),
blobCommitments.LengthCommitment.Y.A0.BigInt(new(big.Int)),
blobCommitments.LengthCommitment.Y.A1.BigInt(new(big.Int)),
},
},
LengthProof: abiG2Commit{
X: [2]*big.Int{
b.BlobCommitments.LengthProof.X.A0.BigInt(new(big.Int)),
b.BlobCommitments.LengthProof.X.A1.BigInt(new(big.Int)),
blobCommitments.LengthProof.X.A0.BigInt(new(big.Int)),
blobCommitments.LengthProof.X.A1.BigInt(new(big.Int)),
},
Y: [2]*big.Int{
b.BlobCommitments.LengthProof.Y.A0.BigInt(new(big.Int)),
b.BlobCommitments.LengthProof.Y.A1.BigInt(new(big.Int)),
blobCommitments.LengthProof.Y.A0.BigInt(new(big.Int)),
blobCommitments.LengthProof.Y.A1.BigInt(new(big.Int)),
},
},
DataLength: uint32(b.BlobCommitments.Length),
DataLength: uint32(blobCommitments.Length),
},
b.Salt,
salt,
)
if err != nil {
return [32]byte{}, err
Expand Down Expand Up @@ -167,11 +183,6 @@ func (b *BlobHeader) BlobKey() (BlobKey, error) {
},
}

paymentMetadataHash, err := b.PaymentMetadata.Hash()
if err != nil {
return [32]byte{}, err
}

s2 := struct {
BlobHeaderHash [32]byte
PaymentMetadataHash [32]byte
Expand All @@ -193,6 +204,23 @@ func (b *BlobHeader) BlobKey() (BlobKey, error) {
return blobKey, nil
}

// BlobKey computes the BlobKey of the BlobHeader.
//
// A BlobKey simply the hash of the BlobHeader
func (b *BlobHeader) BlobKey() (BlobKey, error) {
paymentMetadataHash, err := b.PaymentMetadata.Hash()
if err != nil {
return [32]byte{}, fmt.Errorf("hash payment metadata: %w", err)
}

return ComputeBlobKey(
b.BlobVersion,
b.BlobCommitments,
b.QuorumNumbers,
paymentMetadataHash,
b.Salt)
}

func (c *BlobCertificate) Hash() ([32]byte, error) {
if c.BlobHeader == nil {
return [32]byte{}, fmt.Errorf("blob header is nil")
Expand Down

0 comments on commit 05126c3

Please sign in to comment.