-
Notifications
You must be signed in to change notification settings - Fork 2
/
proof.go
165 lines (144 loc) · 4.13 KB
/
proof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package ancon
import (
"fmt"
"math/bits"
ics23 "github.com/confio/ics23/go"
"github.com/cosmos/cosmos-sdk/client"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/tendermint/tendermint/crypto/merkle"
txtypes "github.com/tendermint/tendermint/types"
)
func CreateProof(e client.Context, txs []txtypes.Tx, rootKeyPath string) ([]byte, []*ics23.ExistenceProof, []*ics23.CommitmentProof, error) {
l := len(txs)
bzs := make([][]byte, l)
for i := 0; i < l; i++ {
bzs[i] = txs[i].Hash()
}
var kp merkle.KeyPath
root, proofs := merkle.ProofsFromByteSlices(bzs)
keys, err := merkle.KeyPathToKeys(rootKeyPath)
if err != nil {
return nil, nil, nil, err
}
for i := 0; i < len(keys); i++ {
kp = kp.AppendKey(keys[i], merkle.KeyEncodingURL)
}
var exps []*ics23.ExistenceProof
var commitments []*ics23.CommitmentProof
for i := 0; i < len(proofs); i++ {
err := proofs[i].Verify(root, txs[i].Hash())
if err != nil {
return nil, nil, nil, err
}
k := kp.AppendKey(txs[i].Hash(), merkle.KeyEncodingHex)
d := (merkle.NewValueOp([]byte(k.String()), proofs[i]))
path, err := convertInnerOps(d.Proof)
if err != nil {
return nil, nil, nil, err
}
px := ics23.CommitmentProof_Exist{
Exist: &ics23.ExistenceProof{
Key: []byte(k.String()),
Value: txs[i].Hash(),
Leaf: convertLeafOp(),
Path: path,
},
}
c := storetypes.CommitmentOp{
Type: storetypes.ProofOpSimpleMerkleCommitment,
Spec: ics23.TendermintSpec,
Key: []byte(k.String()),
Proof: &ics23.CommitmentProof{
Proof: &px,
},
}
if err != nil {
return nil, nil, nil, err
}
// err = res.Verify(ics23.IavlSpec, root, []byte(k.String()), txs[i].Hash())
// if err != nil {
// return nil, nil, err
// }
exps = append(exps, c.Proof.GetExist())
commitments = append(commitments, c.Proof)
}
return root, exps, commitments, nil
}
// ConvertExistenceProof will convert the given proof into a valid
// existence proof, if that's what it is.
//
// This is the simplest case of the range proof and we will focus on
// demoing compatibility here
func ConvertExistenceProof(p *merkle.Proof, key, value []byte) (*ics23.ExistenceProof, error) {
path, err := convertInnerOps(p)
if err != nil {
return nil, err
}
proof := &ics23.ExistenceProof{
Key: key,
Value: value,
Leaf: convertLeafOp(),
Path: path,
}
return proof, nil
}
// this is adapted from merkle/hash.go:leafHash()
// and merkle/simple_map.go:KVPair.Bytes()
func convertLeafOp() *ics23.LeafOp {
prefix := []byte{0}
return &ics23.LeafOp{
Hash: ics23.HashOp_SHA256,
PrehashKey: ics23.HashOp_NO_HASH,
PrehashValue: ics23.HashOp_SHA256,
Length: ics23.LengthOp_VAR_PROTO,
Prefix: prefix,
}
}
func convertInnerOps(p *merkle.Proof) ([]*ics23.InnerOp, error) {
var inners []*ics23.InnerOp
path := buildPath(int(p.Index), int(p.Total))
if len(p.Aunts) != len(path) {
return nil, fmt.Errorf("Calculated a path different length (%d) than provided by SimpleProof (%d)", len(path), len(p.Aunts))
}
for i, aunt := range p.Aunts {
auntRight := path[i]
// combine with: 0x01 || lefthash || righthash
inner := &ics23.InnerOp{Hash: ics23.HashOp_SHA256}
if auntRight {
inner.Prefix = []byte{1}
inner.Suffix = aunt
} else {
inner.Prefix = append([]byte{1}, aunt...)
}
inners = append(inners, inner)
}
return inners, nil
}
// buildPath returns a list of steps from leaf to root
// in each step, true means index is left side, false index is right side
// code adapted from merkle/simple_proof.go:computeHashFromAunts
func buildPath(idx int, total int) []bool {
if total < 2 {
return nil
}
numLeft := getSplitPoint(total)
goLeft := idx < numLeft
// we put goLeft at the end of the array, as we recurse from top to bottom,
// and want the leaf to be first in array, root last
if goLeft {
return append(buildPath(idx, numLeft), goLeft)
}
return append(buildPath(idx-numLeft, total-numLeft), goLeft)
}
func getSplitPoint(length int) int {
if length < 1 {
panic("Trying to split a tree with size < 1")
}
uLength := uint(length)
bitlen := bits.Len(uLength)
k := 1 << uint(bitlen-1)
if k == length {
k >>= 1
}
return k
}