Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Minor improvements #60

Merged
merged 1 commit into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
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
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,33 @@ A Go library that implements a Sparse Merkle tree for a key-value map. The tree
```go
package main

import(
"fmt"
"crypto/sha256"
"github.com/celestiaorg/smt"
import (
"crypto/sha256"
"fmt"

"github.com/celestiaorg/smt"
)

func main() {
// Initialise two new key-value store to store the nodes and values of the tree
nodeStore := smt.NewSimpleMap()
valueStore := smt.NewSimpleMap()
// Initialise the tree
tree := smt.NewSparseMerkleTree(nodeStore, valueStore, sha256.New())

// Update the key "foo" with the value "bar"
tree.Update([]byte("foo"), []byte("bar"))

// Generate a Merkle proof for foo=bar
proof, _ := tree.Prove([]byte("foo"))
root := tree.Root() // We also need the current tree root for the proof

// Verify the Merkle proof for foo=bar
if smt.VerifyProof(proof, root, []byte("foo"), []byte("bar"), sha256.New()) {
fmt.Println("Proof verification succeeded.")
} else {
fmt.Println("Proof verification failed.")
}
// Initialise two new key-value store to store the nodes and values of the tree
nodeStore := smt.NewSimpleMap()
valueStore := smt.NewSimpleMap()
// Initialise the tree
tree := smt.NewSparseMerkleTree(nodeStore, valueStore, sha256.New())

// Update the key "foo" with the value "bar"
_, _ = tree.Update([]byte("foo"), []byte("bar"))

// Generate a Merkle proof for foo=bar
proof, _ := tree.Prove([]byte("foo"))
root := tree.Root() // We also need the current tree root for the proof

// Verify the Merkle proof for foo=bar
if smt.VerifyProof(proof, root, []byte("foo"), []byte("bar"), sha256.New()) {
fmt.Println("Proof verification succeeded.")
} else {
fmt.Println("Proof verification failed.")
}
}
```

Expand Down
4 changes: 2 additions & 2 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (smt *SparseMerkleTree) UpdateForRoot(key []byte, value []byte, root []byte

} else {
// Insert or update operation.
newRoot, err = smt.updateWithSideNodes(key, path, value, sideNodes, pathNodes, oldLeafData)
newRoot, err = smt.updateWithSideNodes(path, value, sideNodes, pathNodes, oldLeafData)
}
return newRoot, err
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (smt *SparseMerkleTree) deleteWithSideNodes(path []byte, sideNodes [][]byte
return currentHash, nil
}

func (smt *SparseMerkleTree) updateWithSideNodes(key, path []byte, value []byte, sideNodes [][]byte, pathNodes [][]byte, oldLeafData []byte) ([]byte, error) {
func (smt *SparseMerkleTree) updateWithSideNodes(path []byte, value []byte, sideNodes [][]byte, pathNodes [][]byte, oldLeafData []byte) ([]byte, error) {
valueHash := smt.th.digest(value)
currentHash, currentData := smt.th.digestLeaf(path, valueHash)
if err := smt.nodes.Set(currentHash, currentData); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func getBitAtFromMSB(data []byte, position int) int {
// setBitAtFromMSB sets the bit at an offset from the most significant bit
func setBitAtFromMSB(data []byte, position int) {
n := int(data[position/8])
n |= (1 << (8 - 1 - uint(position)%8))
n |= 1 << (8 - 1 - uint(position)%8)
data[position/8] = byte(n)
}

Expand Down