Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(share/sha-256): Consolidate SHA-256 Hash Function Usage #3330

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -254,7 +253,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 @@ -267,7 +266,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"
"errors"
"math"

Expand Down Expand Up @@ -48,7 +47,7 @@ func (s *ShareWithProof) Validate(dah *share.Root, axisType rsmt2d.Axis, axisIdx
namespace = share.GetNamespace(s.Share)
}
return s.Proof.VerifyInclusion(
sha256.New(), // TODO(@Wondertan): This should be defined somewhere globally
share.NewSHA256Hasher(),
namespace.ToNMT(),
[][]byte{s.Share},
rootHash,
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()
}
Loading