Skip to content

Commit

Permalink
Jae/rangeprooffix (#75)
Browse files Browse the repository at this point in the history
* VerifyItem takes no index; Return keys/values in range; Fix

* Bump version

* 0.9.1 -- compute hash from rangeproof

* Require Verify() before Verify*()

* Review fixes from #75
  • Loading branch information
jaekwon authored and liamsi committed Jul 3, 2018
1 parent 2de525d commit 0b6f4eb
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 142 deletions.
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

0 comments on commit 0b6f4eb

Please sign in to comment.