-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathops.go
176 lines (161 loc) · 5.11 KB
/
ops.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
166
167
168
169
170
171
172
173
174
175
176
package ics23
import (
"bytes"
"crypto"
// adds sha256 capability to crypto.SHA256
_ "crypto/sha256"
// adds sha512 capability to crypto.SHA512
_ "crypto/sha512"
// adds ripemd160 capability to crypto.RIPEMD160
_ "golang.org/x/crypto/ripemd160"
"github.com/pkg/errors"
)
// Apply will calculate the leaf hash given the key and value being proven
func (op *LeafOp) Apply(key []byte, value []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errors.New("Leaf op needs key")
}
if len(value) == 0 {
return nil, errors.New("Leaf op needs value")
}
pkey, err := prepareLeafData(op.PrehashKey, op.Length, key)
if err != nil {
return nil, errors.Wrap(err, "prehash key")
}
pvalue, err := prepareLeafData(op.PrehashValue, op.Length, value)
if err != nil {
return nil, errors.Wrap(err, "prehash value")
}
data := append(op.Prefix, pkey...)
data = append(data, pvalue...)
return doHash(op.Hash, data)
}
// CheckAgainstSpec will verify the LeafOp is in the format defined in spec
func (op *LeafOp) CheckAgainstSpec(spec *ProofSpec) error {
lspec := spec.LeafSpec
if op.Hash != lspec.Hash {
return errors.Errorf("Unexpected HashOp: %d", op.Hash)
}
if op.PrehashKey != lspec.PrehashKey {
return errors.Errorf("Unexpected PrehashKey: %d", op.PrehashKey)
}
if op.PrehashValue != lspec.PrehashValue {
return errors.Errorf("Unexpected PrehashValue: %d", op.PrehashValue)
}
if op.Length != lspec.Length {
return errors.Errorf("Unexpected LengthOp: %d", op.Length)
}
if !bytes.HasPrefix(op.Prefix, lspec.Prefix) {
return errors.Errorf("Leaf Prefix doesn't start with %X", lspec.Prefix)
}
return nil
}
// Apply will calculate the hash of the next step, given the hash of the previous step
func (op *InnerOp) Apply(child []byte) ([]byte, error) {
if len(child) == 0 {
return nil, errors.Errorf("Inner op needs child value")
}
preimage := append(op.Prefix, child...)
preimage = append(preimage, op.Suffix...)
return doHash(op.Hash, preimage)
}
// CheckAgainstSpec will verify the InnerOp is in the format defined in spec
func (op *InnerOp) CheckAgainstSpec(spec *ProofSpec) error {
if op.Hash != spec.InnerSpec.Hash {
return errors.Errorf("Unexpected HashOp: %d", op.Hash)
}
leafPrefix := spec.LeafSpec.Prefix
if bytes.HasPrefix(op.Prefix, leafPrefix) {
return errors.Errorf("Inner Prefix starts with %X", leafPrefix)
}
if len(op.Prefix) < int(spec.InnerSpec.MinPrefixLength) {
return errors.Errorf("InnerOp prefix too short (%d)", len(op.Prefix))
}
maxLeftChildBytes := (len(spec.InnerSpec.ChildOrder) - 1) * int(spec.InnerSpec.ChildSize)
if len(op.Prefix) > int(spec.InnerSpec.MaxPrefixLength)+maxLeftChildBytes {
return errors.Errorf("InnerOp prefix too long (%d)", len(op.Prefix))
}
return nil
}
func prepareLeafData(hashOp HashOp, lengthOp LengthOp, data []byte) ([]byte, error) {
// TODO: lengthop before or after hash ???
hdata, err := doHashOrNoop(hashOp, data)
if err != nil {
return nil, err
}
ldata, err := doLengthOp(lengthOp, hdata)
return ldata, err
}
// doHashOrNoop will return the preimage untouched if hashOp == NONE,
// otherwise, perform doHash
func doHashOrNoop(hashOp HashOp, preimage []byte) ([]byte, error) {
if hashOp == HashOp_NO_HASH {
return preimage, nil
}
return doHash(hashOp, preimage)
}
// doHash will preform the specified hash on the preimage.
// if hashOp == NONE, it will return an error (use doHashOrNoop if you want different behavior)
func doHash(hashOp HashOp, preimage []byte) ([]byte, error) {
switch hashOp {
case HashOp_SHA256:
hash := crypto.SHA256.New()
hash.Write(preimage)
return hash.Sum(nil), nil
case HashOp_SHA512:
hash := crypto.SHA512.New()
hash.Write(preimage)
return hash.Sum(nil), nil
case HashOp_RIPEMD160:
hash := crypto.RIPEMD160.New()
hash.Write(preimage)
return hash.Sum(nil), nil
case HashOp_BITCOIN:
// ripemd160(sha256(x))
sha := crypto.SHA256.New()
sha.Write(preimage)
tmp := sha.Sum(nil)
hash := crypto.RIPEMD160.New()
hash.Write(tmp)
return hash.Sum(nil), nil
}
return nil, errors.Errorf("Unsupported hashop: %d", hashOp)
}
// doLengthOp will calculate the proper prefix and return it prepended
// doLengthOp(op, data) -> length(data) || data
func doLengthOp(lengthOp LengthOp, data []byte) ([]byte, error) {
switch lengthOp {
case LengthOp_NO_PREFIX:
return data, nil
case LengthOp_VAR_PROTO:
res := append(encodeVarintProto(len(data)), data...)
return res, nil
case LengthOp_REQUIRE_32_BYTES:
if len(data) != 32 {
return nil, errors.Errorf("Data was %d bytes, not 32", len(data))
}
return data, nil
case LengthOp_REQUIRE_64_BYTES:
if len(data) != 64 {
return nil, errors.Errorf("Data was %d bytes, not 64", len(data))
}
return data, nil
// TODO
// case LengthOp_VAR_RLP:
// case LengthOp_FIXED32_BIG:
// case LengthOp_FIXED64_BIG:
// case LengthOp_FIXED32_LITTLE:
// case LengthOp_FIXED64_LITTLE:
}
return nil, errors.Errorf("Unsupported lengthop: %d", lengthOp)
}
func encodeVarintProto(l int) []byte {
// avoid multiple allocs for normal case
res := make([]byte, 0, 8)
for l >= 1<<7 {
res = append(res, uint8(l&0x7f|0x80))
l >>= 7
}
res = append(res, uint8(l))
return res
}