Skip to content

Commit

Permalink
Merge pull request #51 from renaynay/export-min-max
Browse files Browse the repository at this point in the history
nmt.go: export Min/MaxNamespace functions so we can use them in celestia-node
  • Loading branch information
mattdf authored Nov 10, 2021
2 parents 56714f3 + 013bb31 commit 56a05ae
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions nmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,14 @@ type leafRange struct {
start, end uint64
}

// minNamespace parses the minimum namespace id from a given hash
func minNamespace(hash []byte, size namespace.IDSize) []byte {
// MinNamespace parses the minimum namespace id from a given hash
func MinNamespace(hash []byte, size namespace.IDSize) []byte {
min := make([]byte, 0, size)
return append(min, hash[:size]...)
}

// maxNamespace parses the maximum namespace id from a given hash
func maxNamespace(hash []byte, size namespace.IDSize) []byte {
// MaxNamespace parses the maximum namespace id from a given hash
func MaxNamespace(hash []byte, size namespace.IDSize) []byte {
max := make([]byte, 0, size)
return append(max, hash[size:size*2]...)
}
4 changes: 2 additions & 2 deletions nmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func ExampleNamespacedMerkleTree() {
// compute the root
root := tree.Root()
// the root's min/max namespace is the min and max namespace of all leaves:
minNS := minNamespace(root, tree.NamespaceSize())
maxNS := maxNamespace(root, tree.NamespaceSize())
minNS := MinNamespace(root, tree.NamespaceSize())
maxNS := MaxNamespace(root, tree.NamespaceSize())
if bytes.Equal(minNS, namespace.ID{0}) {
fmt.Printf("Min namespace: %x\n", minNS)
}
Expand Down
8 changes: 4 additions & 4 deletions proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func NewAbsenceProof(proofStart, proofEnd int, proofNodes [][]byte, leafHash []b
// is complete and no leaf of that namespace was left out in the proof.
func (proof Proof) VerifyNamespace(h hash.Hash, nID namespace.ID, data [][]byte, root []byte) bool {
nth := NewNmtHasher(h, nID.Size(), proof.isMaxNamespaceIDIgnored)
min := namespace.ID(minNamespace(root, nID.Size()))
max := namespace.ID(maxNamespace(root, nID.Size()))
min := namespace.ID(MinNamespace(root, nID.Size()))
max := namespace.ID(MaxNamespace(root, nID.Size()))
if nID.Size() != min.Size() || nID.Size() != max.Size() {
// conflicting namespace sizes
return false
Expand Down Expand Up @@ -185,15 +185,15 @@ func (proof Proof) verifyLeafHashes(nth *Hasher, verifyCompleteness bool, nID na
if verifyCompleteness {
// leftSubtrees contains the subtree roots upto [0, r.Start)
for _, subtree := range leftSubtrees {
leftSubTreeMax := maxNamespace(subtree, nth.NamespaceSize())
leftSubTreeMax := MaxNamespace(subtree, nth.NamespaceSize())
if nID.LessOrEqual(namespace.ID(leftSubTreeMax)) {
return false
}
}
// rightSubtrees only contains the subtrees after [0, r.Start)
rightSubtrees := proof.nodes
for _, subtree := range rightSubtrees {
rightSubTreeMin := minNamespace(subtree, nth.NamespaceSize())
rightSubTreeMin := MinNamespace(subtree, nth.NamespaceSize())
if namespace.ID(rightSubTreeMin).LessOrEqual(nID) {
return false
}
Expand Down

0 comments on commit 56a05ae

Please sign in to comment.