Skip to content

Commit

Permalink
chore: removes the usage of the merkletree package (#126)
Browse files Browse the repository at this point in the history
## Overview

Inline with #125.
For more context, please see the following
[comment](#125 (comment)).

## 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
- [x] Linked issues closed with keywords
  • Loading branch information
staheri14 authored Mar 9, 2023
1 parent e8ad2aa commit 8be15c4
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 77 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ Where `nid_0 = nid_1 = 0` and `nid_2 = nid_3 = 1` and `data_i = "leaf_i"` for `i

## Related

This implementation (currently) uses NebulousLabs' [merkletree][NebulousLabs'] implementation
and was heavily inspired by the initial implementation in the celestiaorg [prototype].
This implementation was heavily inspired by the initial implementation in the celestiaorg [prototype].

<!--- TODO references --->
[academic paper]: https://arxiv.org/abs/1905.09274
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/celestiaorg/nmt
go 1.18

require (
github.com/celestiaorg/merkletree v0.0.0-20210714075610-a84dc3ddbbe4
github.com/google/gofuzz v1.2.0
github.com/stretchr/testify v1.8.1
)
Expand Down
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/celestiaorg/merkletree v0.0.0-20210714075610-a84dc3ddbbe4 h1:CJdIpo8n5MFP2MwK0gSRcOVlDlFdQJO1p+FqdxYzmvc=
github.com/celestiaorg/merkletree v0.0.0-20210714075610-a84dc3ddbbe4/go.mod h1:fzuHnhzj1pUygGz+1ZkB3uQbEUL4htqCGJ4Qs2LwMZA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -14,18 +12,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gitlab.com/NebulousLabs/errors v0.0.0-20171229012116-7ead97ef90b8 h1:gZfMjx7Jr6N8b7iJO4eUjDsn6xJqoyXg8D+ogdoAfKY=
gitlab.com/NebulousLabs/errors v0.0.0-20171229012116-7ead97ef90b8/go.mod h1:ZkMZ0dpQyWwlENaeZVBiQRjhMEZvk6VTXquzl3FOFP8=
gitlab.com/NebulousLabs/fastrand v0.0.0-20181126182046-603482d69e40 h1:dizWJqTWjwyD8KGcMOwgrkqu1JIkofYgKkmDeNE7oAs=
gitlab.com/NebulousLabs/fastrand v0.0.0-20181126182046-603482d69e40/go.mod h1:rOnSnoRyxMI3fe/7KIbVcsHRGxe30OONv8dEgo+vCfA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200109152110-61a87790db17 h1:nVJ3guKA9qdkEQ3TUdXI9QSINo2CUPM/cySEvw2w8I0=
golang.org/x/crypto v0.0.0-20200109152110-61a87790db17/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
63 changes: 3 additions & 60 deletions proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,11 @@ package nmt
import (
"bytes"
"crypto/sha256"
"io"
"testing"

"github.com/celestiaorg/merkletree"
"github.com/celestiaorg/nmt/namespace"
)

type treeHasher interface {
merkletree.TreeHasher
Size() int
}

// CachedSubtreeHasher implements SubtreeHasher using a set of precomputed
// leaf hashes.
type cachedSubtreeHasher struct {
leafHashes [][]byte
treeHasher
}

// NextSubtreeRoot implements SubtreeHasher.
func (csh *cachedSubtreeHasher) NextSubtreeRoot(subtreeSize int) ([]byte, error) {
if len(csh.leafHashes) == 0 {
return nil, io.EOF
}
tree := merkletree.NewFromTreehasher(csh.treeHasher)
for i := 0; i < subtreeSize && len(csh.leafHashes) > 0; i++ {
if err := tree.PushSubTree(0, csh.leafHashes[0]); err != nil {
return nil, err
}
csh.leafHashes = csh.leafHashes[1:]
}
return tree.Root(), nil
}

// Skip implements SubtreeHasher.
func (csh *cachedSubtreeHasher) Skip(n int) error {
if n > len(csh.leafHashes) {
return io.ErrUnexpectedEOF
}
csh.leafHashes = csh.leafHashes[n:]
return nil
}

// newCachedSubtreeHasher creates a CachedSubtreeHasher using the specified
// leaf hashes and hash function.
func newCachedSubtreeHasher(leafHashes [][]byte, h treeHasher) *cachedSubtreeHasher {
return &cachedSubtreeHasher{
leafHashes: leafHashes,
treeHasher: h,
}
}

func TestProof_VerifyNamespace_False(t *testing.T) {
const testNidLen = 3

Expand All @@ -75,7 +28,7 @@ func TestProof_VerifyNamespace_False(t *testing.T) {
if err != nil {
t.Fatalf("invalid test setup: error on ProveNamespace(): %v", err)
}
incompleteFirstNs := NewInclusionProof(0, 1, rangeProof(t, n, 0, 1), false)
incompleteFirstNs := NewInclusionProof(0, 1, n.buildRangeProof(0, 1), false)
type args struct {
nID namespace.ID
data [][]byte
Expand All @@ -86,7 +39,7 @@ func TestProof_VerifyNamespace_False(t *testing.T) {

// an invalid absence proof for an existing namespace ID (2) in the constructed tree
leafIndex := 3
inclusionProofOfLeafIndex := rangeProof(t, n, leafIndex, leafIndex+1)
inclusionProofOfLeafIndex := n.buildRangeProof(leafIndex, leafIndex+1)
n.computeLeafHashesIfNecessary()
leafHash := n.leafHashes[leafIndex] // the only data item with namespace ID = 2 in the constructed tree is at index 3
invalidAbsenceProof := NewAbsenceProof(leafIndex, leafIndex+1, inclusionProofOfLeafIndex, leafHash, false)
Expand Down Expand Up @@ -133,7 +86,7 @@ func TestProof_VerifyNamespace_False(t *testing.T) {
false,
},
{
"incomplete namespace proof (left)", NewInclusionProof(10, 11, rangeProof(t, n, 10, 11), false),
"incomplete namespace proof (left)", NewInclusionProof(10, 11, n.buildRangeProof(10, 11), false),
args{[]byte{0, 0, 8}, pushedLastNs[1:], n.Root()},
false,
},
Expand Down Expand Up @@ -172,16 +125,6 @@ func TestProof_VerifyNamespace_False(t *testing.T) {
}
}

func rangeProof(t *testing.T, n *NamespacedMerkleTree, start, end int) [][]byte {
n.computeLeafHashesIfNecessary()
subTreeHasher := newCachedSubtreeHasher(n.leafHashes, n.treeHasher)
incompleteRange, err := merkletree.BuildRangeProof(start, end, subTreeHasher)
if err != nil {
t.Fatalf("Could not create range proof: %v", err)
}
return incompleteRange
}

func TestProof_MultipleLeaves(t *testing.T) {
n := New(sha256.New())
ns := []byte{1, 2, 3, 4, 5, 6, 7, 8}
Expand Down

0 comments on commit 8be15c4

Please sign in to comment.