Skip to content

Commit e13b0d1

Browse files
committed
crypto/kzg4844: add helpers for versioned blob hashes (ethereum#28827)
1 parent 10c954d commit e13b0d1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

crypto/kzg4844/kzg4844.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package kzg4844
2020
import (
2121
"embed"
2222
"errors"
23+
"hash"
2324
"sync/atomic"
2425
)
2526

@@ -108,3 +109,21 @@ func VerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
108109
}
109110
return gokzgVerifyBlobProof(blob, commitment, proof)
110111
}
112+
113+
// CalcBlobHashV1 calculates the 'versioned blob hash' of a commitment.
114+
// The given hasher must be a sha256 hash instance, otherwise the result will be invalid!
115+
func CalcBlobHashV1(hasher hash.Hash, commit *Commitment) (vh [32]byte) {
116+
if hasher.Size() != 32 {
117+
panic("wrong hash size")
118+
}
119+
hasher.Reset()
120+
hasher.Write(commit[:])
121+
hasher.Sum(vh[:0])
122+
vh[0] = 0x01 // version
123+
return vh
124+
}
125+
126+
// IsValidVersionedHash checks that h is a structurally-valid versioned blob hash.
127+
func IsValidVersionedHash(h []byte) bool {
128+
return len(h) == 32 && h[0] == 0x01
129+
}

0 commit comments

Comments
 (0)