Skip to content

Commit

Permalink
chore!: remove unused API functions Sha256Namespace8FlaggedLeaf and S…
Browse files Browse the repository at this point in the history
…ha256Namespace8FlaggedInner from the hasher (#103)

## Overview
The following two functions `Sha256Namespace8FlaggedLeaf` and
`Sha256Namespace8FlaggedInner` are part of the public APIs, however,
they are no longer needed as mentioned by @liamsi in
#87 (comment). I
also verified it against the main branch and the latest release
(v0.7.0-rc4@8b9937f) of celestia-node codebase and no usage was found.
Likewise, no usage was available in the clestia-core repo.

**Summary of changes:**
- This PR deletes these `Sha256Namespace8FlaggedLeaf` and
`Sha256Namespace8FlaggedInner` functions and the relevant tests.
- After performing this update, it was found that there was no longer
any use case for the `defaultHasher` in the main hasher file, and it is
now only being used in the test file. As a result, it has been moved
accordingly.

## Checklist
- [x] New and updated code has appropriate documentation
- [x] New and updated code has new and/or updated testing
- [x] Required CI checks are passing
- [x] Visual proof for any user facing features like CLI or
documentation updates (based on the responses from code-owners, it is
safe to say there is no mention to these functions in any doc)
- [x] Linked issues closed with keywords

This change is inline with
celestiaorg/celestia-app#1296 and
#95.
staheri14 authored Feb 17, 2023
1 parent b67b45e commit fa98a59
Showing 2 changed files with 4 additions and 88 deletions.
37 changes: 0 additions & 37 deletions hasher.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ package nmt

import (
"bytes"
"crypto/sha256"
"hash"

"github.com/celestiaorg/nmt/namespace"
@@ -15,42 +14,6 @@ const (

var _ hash.Hash = (*Hasher)(nil)

// defaultHasher uses sha256 as a base-hasher, 8 bytes for the namespace IDs and
// ignores the maximum possible namespace.
var defaultHasher = NewNmtHasher(sha256.New(), DefaultNamespaceIDLen, true)

// Sha256Namespace8FlaggedLeaf uses sha256 as a base-hasher, 8 bytes for the
// namespace IDs and ignores the maximum possible namespace.
//
// Sha256Namespace8FlaggedLeaf(namespacedData) results in: ns(rawData) ||
// ns(rawData) || sha256(LeafPrefix || rawData), where rawData is the leaf's
// data minus the namespace.ID prefix (namely namespacedData[NamespaceLen:]).
//
// Note that different from other cryptographic hash functions, this here makes
// assumptions on the input: len(namespacedData) >= DefaultNamespaceIDLen has to
// hold, as the first DefaultNamespaceIDLen bytes are interpreted as the
// namespace ID). If the input does not fulfil this, we will panic. The output
// will be of length 2*DefaultNamespaceIDLen+sha256.Size = 48 bytes.
func Sha256Namespace8FlaggedLeaf(namespacedData []byte) []byte {
return defaultHasher.HashLeaf(namespacedData)
}

// Sha256Namespace8FlaggedInner hashes inner nodes to: minNID || maxNID ||
// sha256(NodePrefix || leftRight), where leftRight consists of the full left
// and right child node bytes, including their respective min and max namespace
// IDs. Hence, the input has to be of size: 48 = 32 + 8 + 8 = sha256.Size +
// 2*DefaultNamespaceIDLen bytes. If the input does not fulfil this, we will
// panic. The output will also be of length 2*DefaultNamespaceIDLen+sha256.Size
// = 48 bytes.
func Sha256Namespace8FlaggedInner(leftRight []byte) []byte {
const flagLen = DefaultNamespaceIDLen * 2
sha256Len := defaultHasher.baseHasher.Size()
left := leftRight[:flagLen+sha256Len]
right := leftRight[flagLen+sha256Len:]

return defaultHasher.HashNode(left, right)
}

type Hasher struct {
baseHasher hash.Hash
NamespaceLen namespace.IDSize
55 changes: 4 additions & 51 deletions hasher_test.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,10 @@ const (
innerSize = 2 * hashSize
)

// defaultHasher uses sha256 as a base-hasher, 8 bytes for the namespace IDs and
// ignores the maximum possible namespace.
var defaultHasher = NewNmtHasher(sha256.New(), DefaultNamespaceIDLen, true)

func Test_namespacedTreeHasher_HashLeaf(t *testing.T) {
zeroNID := []byte{0}
oneNID := []byte{1}
@@ -114,57 +118,6 @@ func Test_namespacedTreeHasher_HashNode(t *testing.T) {
}
}

func TestSha256Namespace8FlaggedLeaf(t *testing.T) {
tests := []struct {
name string
data []byte
wantPanic bool
wantLen int
}{
{"input too short: panic", []byte("smaller"), true, 0},
{"input 8 byte: Ok", []byte("8bytesss"), false, 48},
{"input greater 8 byte: Ok", []byte("8bytesssSomeNotSoRandData"), false, 48},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.wantPanic {
shouldPanic(t, func() {
Sha256Namespace8FlaggedLeaf(tt.data)
})
} else if got := Sha256Namespace8FlaggedLeaf(tt.data); len(got) != tt.wantLen {
t.Errorf("len(Sha256Namespace8FlaggedLeaf()) = %v, want %v", got, tt.wantLen)
}
})
}
}

func TestSha256Namespace8FlaggedInner(t *testing.T) {
nilHash := sha256.Sum256(nil)
nid1 := []byte("nid01234")
nid2 := []byte("nid12345")
tests := []struct {
name string
data []byte
wantPanic bool
wantLen int
}{
{"input smaller 48: panic", []byte("smaller48"), true, 0},
{"input still too small: panic", append(append(nid1, nid2...), []byte("data")...), true, 0},
{"valid input: ok", append(append(append(nid1, nilHash[:]...), nid2...), nilHash[:]...), false, 48},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.wantPanic {
shouldPanic(t, func() {
Sha256Namespace8FlaggedInner(tt.data)
})
} else if got := Sha256Namespace8FlaggedInner(tt.data); len(got) != tt.wantLen {
t.Errorf("len(Sha256Namespace8FlaggedLeaf()) = %v, want %v", got, tt.wantLen)
}
})
}
}

func sum(hash crypto.Hash, data ...[]byte) []byte {
h := hash.New()
for _, d := range data {

0 comments on commit fa98a59

Please sign in to comment.