diff --git a/README.md b/README.md index de80a1d..eedfed3 100644 --- a/README.md +++ b/README.md @@ -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]. [academic paper]: https://arxiv.org/abs/1905.09274 diff --git a/go.mod b/go.mod index 7e464d2..1edcd6d 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 4c5d75c..f62dd11 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/proof_test.go b/proof_test.go index 8a29276..29ea48d 100644 --- a/proof_test.go +++ b/proof_test.go @@ -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 @@ -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 @@ -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) @@ -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, }, @@ -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}