Skip to content

Commit

Permalink
feat: unexport GetPathBit (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
h5law committed Sep 26, 2023
1 parent c5998f1 commit 1df1f48
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func verifyProofWithUpdates(proof *SparseMerkleProof, root []byte, key []byte, v
node := make([]byte, hashSize(spec))
copy(node, proof.SideNodes[i])

if GetPathBit(path, len(proof.SideNodes)-1-i) == left {
if getPathBit(path, len(proof.SideNodes)-1-i) == left {
currentHash, currentData = digestNode(spec, currentHash, node)
} else {
currentHash, currentData = digestNode(spec, node, currentHash)
Expand Down Expand Up @@ -276,7 +276,7 @@ func DecompactProof(proof *SparseCompactMerkleProof, spec *TreeSpec) (*SparseMer
decompactedSideNodes := make([][]byte, proof.NumSideNodes)
position := 0
for i := 0; i < proof.NumSideNodes; i++ {
if GetPathBit(proof.BitMask, i) == 1 {
if getPathBit(proof.BitMask, i) == 1 {
decompactedSideNodes[i] = placeholder(spec)
} else {
decompactedSideNodes[i] = proof.SideNodes[position]
Expand Down
22 changes: 11 additions & 11 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (smt *SMT) Get(key []byte) ([]byte, error) {
}
}
inner := (*node).(*innerNode)
if GetPathBit(path, depth) == left {
if getPathBit(path, depth) == left {
node = &inner.leftChild
} else {
node = &inner.rightChild
Expand Down Expand Up @@ -172,7 +172,7 @@ func (smt *SMT) update(
*last = &ext
last = &ext.child
}
if GetPathBit(path, prefixlen) == left {
if getPathBit(path, prefixlen) == left {
*last = &innerNode{leftChild: newLeaf, rightChild: leaf}
} else {
*last = &innerNode{leftChild: leaf, rightChild: newLeaf}
Expand All @@ -195,7 +195,7 @@ func (smt *SMT) update(

inner := node.(*innerNode)
var child *treeNode
if GetPathBit(path, depth) == left {
if getPathBit(path, depth) == left {
child = &inner.leftChild
} else {
child = &inner.rightChild
Expand Down Expand Up @@ -266,7 +266,7 @@ func (smt *SMT) delete(node treeNode, depth int, path []byte, orphans *orphanNod

inner := node.(*innerNode)
var child, sib *treeNode
if GetPathBit(path, depth) == left {
if getPathBit(path, depth) == left {
child, sib = &inner.leftChild, &inner.rightChild
} else {
child, sib = &inner.rightChild, &inner.leftChild
Expand Down Expand Up @@ -335,7 +335,7 @@ func (smt *SMT) Prove(key []byte) (proof *SparseMerkleProof, err error) {
}
}
inner := node.(*innerNode)
if GetPathBit(path, depth) == left {
if getPathBit(path, depth) == left {
node, sib = inner.leftChild, inner.rightChild
} else {
node, sib = inner.rightChild, inner.leftChild
Expand Down Expand Up @@ -466,7 +466,7 @@ func (smt *SMT) ProveClosest(path []byte) (
if !ok { // this can only happen for an empty tree
break
}
if GetPathBit(workingPath, depth) == left {
if getPathBit(workingPath, depth) == left {
node, sib = inner.leftChild, inner.rightChild
} else {
node, sib = inner.rightChild, inner.leftChild
Expand Down Expand Up @@ -689,7 +689,7 @@ func (ext *extensionNode) match(path []byte, depth int) (int, bool) {
panic("depth != path_begin")
}
for i := ext.pathStart(); i < ext.pathEnd(); i++ {
if GetPathBit(ext.path, i) != GetPathBit(path, i) {
if getPathBit(ext.path, i) != getPathBit(path, i) {
return i - ext.pathStart(), false
}
}
Expand All @@ -700,7 +700,7 @@ func (ext *extensionNode) match(path []byte, depth int) (int, bool) {
func (ext *extensionNode) commonPrefix(path []byte) int {
count := 0
for i := ext.pathStart(); i < ext.pathEnd(); i++ {
if GetPathBit(ext.path, i) != GetPathBit(path, i) {
if getPathBit(ext.path, i) != getPathBit(path, i) {
break
}
count++
Expand All @@ -719,8 +719,8 @@ func (ext *extensionNode) split(path []byte, depth int) (treeNode, *treeNode, in
index := ext.pathStart()
var myBit, branchBit int
for ; index < ext.pathEnd(); index++ {
myBit = GetPathBit(ext.path, index)
branchBit = GetPathBit(path, index)
myBit = getPathBit(ext.path, index)
branchBit = getPathBit(path, index)
if myBit != branchBit {
break
}
Expand Down Expand Up @@ -773,7 +773,7 @@ func (ext *extensionNode) expand() treeNode {
last := ext.child
for i := ext.pathEnd() - 1; i >= ext.pathStart(); i-- {
var next innerNode
if GetPathBit(ext.path, i) == left {
if getPathBit(ext.path, i) == left {
next.leftChild = last
} else {
next.rightChild = last
Expand Down
10 changes: 5 additions & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func newNilPathHasher(hashSize int) PathHasher {
return &nilPathHasher{hashSize: hashSize}
}

// GetPathBit gets the bit at an offset from the most significant bit
// TODO: Unexport this method
func GetPathBit(data []byte, position int) int {
// getPathBit gets the bit at an offset (see position) in the data
// provided relative to the most significant bit
func getPathBit(data []byte, position int) int {
// get the byte at the position and then left shift one by the offset of the position
// from the leftmost bit in the byte. Check if the bitwise AND is the same
// Path: []byte{ {0 1 0 1 1 0 1 0}, {0 1 1 0 1 1 0 1}, {1 0 0 1 0 0 1 0} } (length = 24 bits / 3 bytes)
Expand Down Expand Up @@ -50,7 +50,7 @@ func flipPathBit(data []byte, position int) {
func countSetBits(data []byte) int {
count := 0
for i := 0; i < len(data)*8; i++ {
if GetPathBit(data, i) == 1 {
if getPathBit(data, i) == 1 {
count++
}
}
Expand All @@ -61,7 +61,7 @@ func countSetBits(data []byte) int {
func countCommonPrefix(data1, data2 []byte, from int) int {
count := 0
for i := from; i < len(data1)*8; i++ {
if GetPathBit(data1, i) == GetPathBit(data2, i) {
if getPathBit(data1, i) == getPathBit(data2, i) {
count++
} else {
break
Expand Down

0 comments on commit 1df1f48

Please sign in to comment.