diff --git a/blob/service_test.go b/blob/service_test.go index 4bda8d993b..2c5da1c0c8 100644 --- a/blob/service_test.go +++ b/blob/service_test.go @@ -3,7 +3,6 @@ package blob import ( "bytes" "context" - "crypto/sha256" "encoding/json" "fmt" "sort" @@ -203,7 +202,7 @@ func TestBlobService_Get(t *testing.T) { for _, p := range *proof { from := to to = p.End() - p.Start() + from - eq := p.VerifyInclusion(sha256.New(), namespace.ToNMT(), rawShares[from:to], row) + eq := p.VerifyInclusion(share.NewSHA256Hasher(), namespace.ToNMT(), rawShares[from:to], row) if eq == true { return } diff --git a/share/eds/byzantine/bad_encoding_test.go b/share/eds/byzantine/bad_encoding_test.go index e42e3c287c..470d792cbe 100644 --- a/share/eds/byzantine/bad_encoding_test.go +++ b/share/eds/byzantine/bad_encoding_test.go @@ -2,7 +2,6 @@ package byzantine import ( "context" - "crypto/sha256" "hash" "testing" "time" @@ -253,7 +252,7 @@ func newNamespacedBlockService() *namespacedBlockService { sha256NamespaceFlagged := uint64(0x7701) // register the nmt hasher to validate the order of namespaces mhcore.Register(sha256NamespaceFlagged, func() hash.Hash { - nh := nmt.NewNmtHasher(sha256.New(), share.NamespaceSize, true) + nh := nmt.NewNmtHasher(share.NewSHA256Hasher(), share.NamespaceSize, true) nh.Reset() return nh }) @@ -266,7 +265,7 @@ func newNamespacedBlockService() *namespacedBlockService { Codec: sha256NamespaceFlagged, MhType: sha256NamespaceFlagged, // equals to NmtHasher.Size() - MhLength: sha256.New().Size() + 2*share.NamespaceSize, + MhLength: share.NewSHA256Hasher().Size() + 2*share.NamespaceSize, } return bs } diff --git a/share/eds/byzantine/share_proof.go b/share/eds/byzantine/share_proof.go index 98b58ebbec..ae830c0391 100644 --- a/share/eds/byzantine/share_proof.go +++ b/share/eds/byzantine/share_proof.go @@ -2,7 +2,6 @@ package byzantine import ( "context" - "crypto/sha256" "github.com/ipfs/boxo/blockservice" "github.com/ipfs/go-cid" @@ -45,7 +44,7 @@ func NewShareWithProof(index int, share share.Share, pathToLeaf []cid.Cid) *Shar // Validate validates inclusion of the share under the given root CID. func (s *ShareWithProof) Validate(root cid.Cid) bool { return s.Proof.VerifyInclusion( - sha256.New(), // TODO(@Wondertan): This should be defined somewhere globally + share.NewSHA256Hasher(), share.GetNamespace(s.Share).ToNMT(), [][]byte{share.GetData(s.Share)}, ipld.NamespacedSha256FromCID(root), diff --git a/share/eds/eds.go b/share/eds/eds.go index e0433a1b6b..b8a332f275 100644 --- a/share/eds/eds.go +++ b/share/eds/eds.go @@ -3,7 +3,6 @@ package eds import ( "bytes" "context" - "crypto/sha256" "errors" "fmt" "io" @@ -68,7 +67,7 @@ func writeHeader(eds *rsmt2d.ExtendedDataSquare, w io.Writer) error { // writeQuadrants reorders the shares to quadrant order and writes them to the CARv1 file. func writeQuadrants(eds *rsmt2d.ExtendedDataSquare, w io.Writer) error { - hasher := nmt.NewNmtHasher(sha256.New(), share.NamespaceSize, ipld.NMTIgnoreMaxNamespace) + hasher := nmt.NewNmtHasher(share.NewSHA256Hasher(), share.NamespaceSize, ipld.NMTIgnoreMaxNamespace) shares := quadrantOrder(eds) for _, share := range shares { leaf, err := hasher.HashLeaf(share) diff --git a/share/getter.go b/share/getter.go index 3fcc93de33..363225f971 100644 --- a/share/getter.go +++ b/share/getter.go @@ -2,7 +2,6 @@ package share import ( "context" - "crypto/sha256" "errors" "fmt" @@ -90,7 +89,7 @@ func (row *NamespacedRow) verify(rowRoot []byte, namespace Namespace) bool { // verify namespace return row.Proof.VerifyNamespace( - sha256.New(), + NewSHA256Hasher(), namespace.ToNMT(), leaves, rowRoot, diff --git a/share/ipld/get_shares_test.go b/share/ipld/get_shares_test.go index 580efcb69b..929528e7dc 100644 --- a/share/ipld/get_shares_test.go +++ b/share/ipld/get_shares_test.go @@ -3,7 +3,6 @@ package ipld import ( "bytes" "context" - "crypto/sha256" "errors" mrand "math/rand" "sort" @@ -384,7 +383,7 @@ func TestGetSharesWithProofsByNamespace(t *testing.T) { // verify namespace verified := proof.VerifyNamespace( - sha256.New(), + share.NewSHA256Hasher(), namespace.ToNMT(), leaves, NamespacedSha256FromCID(rcid)) @@ -392,7 +391,7 @@ func TestGetSharesWithProofsByNamespace(t *testing.T) { // verify inclusion verified = proof.VerifyInclusion( - sha256.New(), + share.NewSHA256Hasher(), namespace.ToNMT(), rowShares, NamespacedSha256FromCID(rcid)) @@ -483,7 +482,7 @@ func assertNoRowContainsNID( // if no error returned, check absence proof foundAbsenceRows++ - verified := data.Proof().VerifyNamespace(sha256.New(), namespace.ToNMT(), nil, rowRoot) + verified := data.Proof().VerifyNamespace(share.NewSHA256Hasher(), namespace.ToNMT(), nil, rowRoot) require.True(t, verified) } diff --git a/share/ipld/nmt.go b/share/ipld/nmt.go index 6dba300965..5e4db9ec25 100644 --- a/share/ipld/nmt.go +++ b/share/ipld/nmt.go @@ -61,7 +61,7 @@ const ( func init() { // required for Bitswap to hash and verify inbound data correctly mhcore.Register(sha256NamespaceFlagged, func() hash.Hash { - nh := nmt.NewNmtHasher(sha256.New(), share.NamespaceSize, true) + nh := nmt.NewNmtHasher(share.NewSHA256Hasher(), share.NamespaceSize, true) nh.Reset() return nh }) diff --git a/share/share.go b/share/share.go index 4079028d82..a0b18c8176 100644 --- a/share/share.go +++ b/share/share.go @@ -2,8 +2,10 @@ package share import ( "bytes" + "crypto/sha256" "encoding/hex" "fmt" + "hash" "github.com/celestiaorg/celestia-app/pkg/appconsts" ) @@ -71,3 +73,8 @@ func MustDataHashFromString(datahash string) DataHash { } return dh } + +// NewSHA256Hasher returns a new instance of a SHA-256 hasher. +func NewSHA256Hasher() hash.Hash { + return sha256.New() +}