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

Jae/rangeprooffix #75

Merged
merged 7 commits into from
Jul 3, 2018
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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## 0.9.1 (July 1, 2018)

IMPROVEMENTS

- RangeProof.ComputeRootHash() to compute root rather than provide as in Verify(hash)
- RangeProof.Verify\*() first require .Verify(root), which memoizes

## 0.9.0 (July 1, 2018)

BREAKING CHANGES

- RangeProof.VerifyItem doesn't require an index.
- Only return values in range when getting proof.
- Return keys as well.

BUG FIXES

- traversal bugs in traverseRange.

## 0.8.1

*July 1st, 2018*
Expand Down
2 changes: 1 addition & 1 deletion basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func TestTreeProof(t *testing.T) {
assert.Equal(t, key, value)
err := proof.Verify(root)
assert.NoError(t, err, "#### %v", proof.String())
err = proof.VerifyItem(0, key, key)
err = proof.VerifyItem(key, key)
assert.NoError(t, err, "#### %v", proof.String())
}
}
Expand Down
48 changes: 44 additions & 4 deletions proof_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@ func (pwl pathWithLeaf) StringIndented(indent string) string {
indent)
}

// `verify` checks that the leaf node's hash + the inner nodes merkle-izes to
// the given root. If it returns an error, it means the leafHash or the
// PathToLeaf is incorrect.
func (pwl pathWithLeaf) verify(root []byte) cmn.Error {
return pwl.Path.verify(pwl.Leaf.Hash(), root)
leafHash := pwl.Leaf.Hash()
return pwl.Path.verify(leafHash, root)
}

// `computeRootHash` computes the root hash with leaf node.
// Does not verify the root hash.
func (pwl pathWithLeaf) computeRootHash() []byte {
leafHash := pwl.Leaf.Hash()
return pwl.Path.computeRootHash(leafHash)
}

//----------------------------------------
Expand Down Expand Up @@ -62,9 +73,9 @@ func (pl PathToLeaf) StringIndented(indent string) string {
indent)
}

// verify checks that the leaf node's hash + the inner nodes merkle-izes to the
// given root. If it returns an error, it means the leafHash or the PathToLeaf
// is incorrect.
// `verify` checks that the leaf node's hash + the inner nodes merkle-izes to
// the given root. If it returns an error, it means the leafHash or the
// PathToLeaf is incorrect.
func (pl PathToLeaf) verify(leafHash []byte, root []byte) cmn.Error {
hash := leafHash
for i := len(pl) - 1; i >= 0; i-- {
Expand All @@ -77,6 +88,17 @@ func (pl PathToLeaf) verify(leafHash []byte, root []byte) cmn.Error {
return nil
}

// `computeRootHash` computes the root hash assuming some leaf hash.
// Does not verify the root hash.
func (pl PathToLeaf) computeRootHash(leafHash []byte) []byte {
hash := leafHash
for i := len(pl) - 1; i >= 0; i-- {
pin := pl[i]
hash = pin.Hash(hash)
}
return hash
}

func (pl PathToLeaf) isLeftmost() bool {
for _, node := range pl {
if len(node.Left) > 0 {
Expand Down Expand Up @@ -125,3 +147,21 @@ func (pl PathToLeaf) isLeftAdjacentTo(pl2 PathToLeaf) bool {

return pl.isRightmost() && pl2.isLeftmost()
}

// returns -1 if invalid.
func (pl PathToLeaf) Index() (idx int64) {
for i, node := range pl {
if node.Left == nil {
continue
} else if node.Right == nil {
if i < len(pl)-1 {
idx += node.Size - pl[i+1].Size
} else {
idx += node.Size - 1
}
} else {
return -1
}
}
return idx
}
Loading