Skip to content

Commit

Permalink
Merge 9b39f24 into 277cad3
Browse files Browse the repository at this point in the history
  • Loading branch information
edjroz committed Dec 5, 2019
2 parents 277cad3 + 9b39f24 commit 1d29dd9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 45 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -12,8 +12,9 @@ package main

import (
"crypto/sha256"
"fmt"

"github.com/onrik/gomerkle"
"github.com/pokt-network/gomerkle"
)

func main() {
Expand All @@ -37,6 +38,6 @@ func main() {
// Proof for Jessie
proof := tree.GetProof(4)
leaf := tree.GetLeaf(4)
println(tree.VerifyProof(proof, tree.Root(), leaf))
fmt.Println(tree.VerifyProof(proof, tree.Root(), leaf))
}
```
6 changes: 5 additions & 1 deletion go.mod
@@ -1 +1,5 @@
module github.com/onrik/gomerkle
module github.com/pokt-network/gomerkle

go 1.13

require github.com/stretchr/testify v1.4.0
10 changes: 10 additions & 0 deletions go.sum
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
14 changes: 7 additions & 7 deletions gomerkle.go
Expand Up @@ -7,7 +7,7 @@ import (
)

// Proof presents merkle tree proof
type Proof []map[string][]byte
type Proof []map[bool][]byte

// Node presents merkle tree node
type Node struct {
Expand Down Expand Up @@ -104,7 +104,7 @@ func (tree *Tree) GetLeaf(index int) []byte {
func (tree *Tree) GetProof(index int) Proof {
proof := Proof{}
var siblingIndex int
var siblingPosition string
var siblingPosition bool

for i := len(tree.Levels) - 1; i > 0; i-- {
levelLen := len(tree.Levels[i])
Expand All @@ -115,13 +115,13 @@ func (tree *Tree) GetProof(index int) Proof {

if index%2 == 0 {
siblingIndex = index + 1
siblingPosition = "right"
siblingPosition = true
} else {
siblingIndex = index - 1
siblingPosition = "left"
siblingPosition = false
}

proof = append(proof, map[string][]byte{
proof = append(proof, map[bool][]byte{
siblingPosition: tree.Levels[i][siblingIndex].Hash,
})
index = int(index / 2)
Expand All @@ -135,9 +135,9 @@ func (tree *Tree) VerifyProof(proof Proof, root, value []byte) bool {
proofHash := value

for _, p := range proof {
if sibling, exist := p["left"]; exist {
if sibling, exist := p[false]; exist {
proofHash = tree.hash(append(sibling, proofHash...))
} else if sibling, exist := p["right"]; exist {
} else if sibling, exist := p[true]; exist {
proofHash = tree.hash(append(proofHash, sibling...))
} else {
return false
Expand Down
70 changes: 35 additions & 35 deletions gomerkle_test.go
Expand Up @@ -520,20 +520,20 @@ func TestGetProof(t *testing.T) {

proof := tree.GetProof(5)
expected := Proof{
map[string][]byte{
"left": {182, 0, 252, 107, 110, 161, 149, 93, 17, 72, 97, 196, 41, 52, 198, 89},
map[bool][]byte{
false: {182, 0, 252, 107, 110, 161, 149, 93, 17, 72, 97, 196, 41, 52, 198, 89},
},
map[string][]byte{
"right": {254, 179, 55, 70, 100, 189, 9, 46, 252, 26, 168, 125, 179, 98, 109, 86},
map[bool][]byte{
true: {254, 179, 55, 70, 100, 189, 9, 46, 252, 26, 168, 125, 179, 98, 109, 86},
},
map[string][]byte{
"left": {98, 40, 179, 120, 254, 245, 143, 248, 208, 179, 3, 215, 114, 144, 115, 87},
map[bool][]byte{
false: {98, 40, 179, 120, 254, 245, 143, 248, 208, 179, 3, 215, 114, 144, 115, 87},
},
map[string][]byte{
"right": {86, 22, 191, 37, 72, 68, 16, 116, 208, 179, 61, 82, 214, 0, 3, 226},
map[bool][]byte{
true: {86, 22, 191, 37, 72, 68, 16, 116, 208, 179, 61, 82, 214, 0, 3, 226},
},
map[string][]byte{
"right": {37, 153, 197, 153, 191, 14, 1, 134, 75, 185, 94, 199, 7, 212, 201, 178},
map[bool][]byte{
true: {37, 153, 197, 153, 191, 14, 1, 134, 75, 185, 94, 199, 7, 212, 201, 178},
},
}
require.Equal(t, expected, proof)
Expand Down Expand Up @@ -561,20 +561,20 @@ func TestVerifyProof(t *testing.T) {
// Test verify with empty tree
tree = NewTree(md5.New())
proof := Proof{
map[string][]byte{
"left": {182, 0, 252, 107, 110, 161, 149, 93, 17, 72, 97, 196, 41, 52, 198, 89},
map[bool][]byte{
false: {182, 0, 252, 107, 110, 161, 149, 93, 17, 72, 97, 196, 41, 52, 198, 89},
},
map[string][]byte{
"right": {254, 179, 55, 70, 100, 189, 9, 46, 252, 26, 168, 125, 179, 98, 109, 86},
map[bool][]byte{
true: {254, 179, 55, 70, 100, 189, 9, 46, 252, 26, 168, 125, 179, 98, 109, 86},
},
map[string][]byte{
"left": {98, 40, 179, 120, 254, 245, 143, 248, 208, 179, 3, 215, 114, 144, 115, 87},
map[bool][]byte{
false: {98, 40, 179, 120, 254, 245, 143, 248, 208, 179, 3, 215, 114, 144, 115, 87},
},
map[string][]byte{
"right": {86, 22, 191, 37, 72, 68, 16, 116, 208, 179, 61, 82, 214, 0, 3, 226},
map[bool][]byte{
true: {86, 22, 191, 37, 72, 68, 16, 116, 208, 179, 61, 82, 214, 0, 3, 226},
},
map[string][]byte{
"right": {37, 153, 197, 153, 191, 14, 1, 134, 75, 185, 94, 199, 7, 212, 201, 178},
map[bool][]byte{
true: {37, 153, 197, 153, 191, 14, 1, 134, 75, 185, 94, 199, 7, 212, 201, 178},
},
}
root = []byte{198, 171, 151, 198, 99, 102, 171, 160, 114, 231, 230, 66, 133, 203, 93, 244}
Expand All @@ -583,31 +583,31 @@ func TestVerifyProof(t *testing.T) {

// Test invalid proof
proof = Proof{
map[string][]byte{
"left": {182, 0, 252, 107, 110, 161, 149, 93, 17, 72, 97, 196, 41, 52, 198, 89},
map[bool][]byte{
false: {182, 0, 252, 107, 110, 161, 149, 93, 17, 72, 97, 196, 41, 52, 198, 89},
},
map[string][]byte{
"right": {254, 179, 55, 70, 100, 189, 9, 46, 252, 26, 168, 125, 179, 98, 109, 86},
map[bool][]byte{
true: {254, 179, 55, 70, 100, 189, 9, 46, 252, 26, 168, 125, 179, 98, 109, 86},
},
map[string][]byte{
"left": {98, 40, 179, 120, 254, 245, 143, 248, 208, 179, 3, 215, 114, 144, 115, 87},
map[bool][]byte{
false: {98, 40, 179, 120, 254, 245, 143, 248, 208, 179, 3, 215, 114, 144, 115, 87},
},
map[string][]byte{
"right": {86, 22, 191, 37, 72, 68, 16, 116, 208, 179, 61, 82, 214, 0, 3, 226},
map[bool][]byte{
true: {86, 22, 191, 37, 72, 68, 16, 116, 208, 179, 61, 82, 214, 0, 3, 226},
},
map[string][]byte{
"left": {37, 153, 197, 153, 191, 14, 1, 134, 75, 185, 94, 199, 7, 212, 201, 178},
map[bool][]byte{
false: {37, 153, 197, 153, 191, 14, 1, 134, 75, 185, 94, 199, 7, 212, 201, 178},
},
}
require.False(t, tree.VerifyProof(proof, root, leaf))

proof = Proof{
map[string][]byte{
"left": {182, 0, 252, 107, 110, 161, 149, 93, 17, 72, 97, 196, 41, 52, 198, 89},
map[bool][]byte{
false: {182, 0, 252, 107, 110, 161, 149, 93, 17, 72, 97, 196, 41, 52, 198, 89},
},
map[string][]byte{},
map[string][]byte{
"right": {86, 22, 191, 37, 72, 68, 16, 116, 208, 179, 61, 82, 214, 0, 3, 226},
map[bool][]byte{},
map[bool][]byte{
true: {86, 22, 191, 37, 72, 68, 16, 116, 208, 179, 61, 82, 214, 0, 3, 226},
},
}
require.False(t, tree.VerifyProof(proof, root, leaf))
Expand Down

0 comments on commit 1d29dd9

Please sign in to comment.