Skip to content

Commit

Permalink
refactor(share/sha-256): Consolidate SHA-256 Hash Function Usage (cel…
Browse files Browse the repository at this point in the history
…estiaorg#3330)

This PR consolidates SHA-256 hash function into the share/hashutils.go, and replaces all the sha256.New() functions with share.NewSHA256Hasher()
  • Loading branch information
kien6034 authored and walldiss committed May 7, 2024
1 parent 13ac02f commit c0698c2
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 16 deletions.
3 changes: 1 addition & 2 deletions blob/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package blob
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"sort"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions share/eds/byzantine/bad_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package byzantine

import (
"context"
"crypto/sha256"
"hash"
"testing"
"time"
Expand Down Expand Up @@ -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
})
Expand All @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions share/eds/byzantine/share_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package byzantine

import (
"context"
"crypto/sha256"

"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 1 addition & 2 deletions share/eds/eds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package eds
import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions share/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package share

import (
"context"
"crypto/sha256"
"errors"
"fmt"

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions share/ipld/get_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ipld
import (
"bytes"
"context"
"crypto/sha256"
"errors"
mrand "math/rand"
"sort"
Expand Down Expand Up @@ -384,15 +383,15 @@ func TestGetSharesWithProofsByNamespace(t *testing.T) {

// verify namespace
verified := proof.VerifyNamespace(
sha256.New(),
share.NewSHA256Hasher(),
namespace.ToNMT(),
leaves,
NamespacedSha256FromCID(rcid))
require.True(t, verified)

// verify inclusion
verified = proof.VerifyInclusion(
sha256.New(),
share.NewSHA256Hasher(),
namespace.ToNMT(),
rowShares,
NamespacedSha256FromCID(rcid))
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion share/ipld/nmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
7 changes: 7 additions & 0 deletions share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package share

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
)
Expand Down Expand Up @@ -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()
}

0 comments on commit c0698c2

Please sign in to comment.