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

test: ExampleNamespacedMerkleTree #203

Merged
merged 1 commit into from
Jun 1, 2023
Merged
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
51 changes: 17 additions & 34 deletions nmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,62 +52,45 @@ func newNamespaceDataPairRaw(nidSize int, data []byte) namespaceDataPair {
}
}

func ExampleNamespacedMerkleTree() {
// the tree will use this namespace size
nidSize := 1
// the leaves that will be pushed
func TestExampleNamespacedMerkleTree(t *testing.T) {
data := [][]byte{
append(namespace.ID{0}, []byte("leaf_0")...),
append(namespace.ID{0}, []byte("leaf_1")...),
append(namespace.ID{1}, []byte("leaf_2")...),
append(namespace.ID{1}, []byte("leaf_3")...),
}
// Init a tree with the namespace size as well as
// the underlying hash function:
nidSize := 1
tree := New(sha256.New(), NamespaceIDSize(nidSize))

for _, d := range data {
if err := tree.Push(d); err != nil {
panic(fmt.Sprintf("unexpected error: %v", err))
}
err := tree.Push(d)
assert.NoError(t, err)
}
// compute the root

root, err := tree.Root()
if err != nil {
panic("unexpected error")
}
assert.NoError(t, err)

// 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())
if bytes.Equal(minNS, namespace.ID{0}) {
fmt.Printf("Min namespace: %x\n", minNS)
}
if bytes.Equal(maxNS, namespace.ID{1}) {
fmt.Printf("Max namespace: %x\n", maxNS)
}
assert.Equal(t, minNS, []byte(namespace.ID{0}))
assert.Equal(t, maxNS, []byte(namespace.ID{1}))

// compute proof for namespace 0:
// compute proof for namespace 0
proof, err := tree.ProveNamespace(namespace.ID{0})
if err != nil {
panic("unexpected error")
}
assert.NoError(t, err)

// verify proof using the root and the leaves of namespace 0:
// verify proof using the root and the leaves of namespace 0
leafs := [][]byte{
append(namespace.ID{0}, []byte("leaf_0")...),
append(namespace.ID{0}, []byte("leaf_1")...),
}

if proof.VerifyNamespace(sha256.New(), namespace.ID{0}, leafs, root) {
fmt.Printf("Successfully verified namespace: %x\n", namespace.ID{0})
}
got := proof.VerifyNamespace(sha256.New(), namespace.ID{0}, leafs, root)
assert.True(t, got)

if proof.VerifyNamespace(sha256.New(), namespace.ID{2}, leafs, root) {
panic(fmt.Sprintf("Proof for namespace %x, passed for namespace: %x\n", namespace.ID{0}, namespace.ID{2}))
}
// Output:
// Min namespace: 00
// Max namespace: 01
// Successfully verified namespace: 00
got = proof.VerifyNamespace(sha256.New(), namespace.ID{2}, leafs, root)
assert.False(t, got) // namespace 2 is not in the tree, so the proof should fail to verify
}

func TestNamespacedMerkleTree_Push(t *testing.T) {
Expand Down