-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathics23.cdc
392 lines (339 loc) · 10.4 KB
/
ics23.cdc
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import Crypto
access(all) contract ICS23 {
// Data structures and helper functions
pub enum HashOp: UInt8 {
pub case NO_HASH
pub case SHA256
pub case SHA512
pub case KECCAK
pub case RIPEMD160
pub case BITCOIN
}
pub enum LengthOp: UInt8 {
pub case NO_PREFIX
pub case VAR_PROTO
pub case VAR_RLP
pub case FIXED32_BIG
pub case FIXED32_LITTLE
pub case FIXED64_BIG
pub case FIXED64_LITTLE
pub case REQUIRE_32_BYTES
pub case REQUIRE_64_BYTES
}
pub struct ExistenceProof {
pub var valid: Bool
pub var key: [UInt8]
pub var value: [UInt8]
pub var leaf: LeafOp
pub var path:[InnerOp]
init(
valid: Bool,
key: [UInt8],
value: [UInt8] ,
leaf: LeafOp,
path:[InnerOp] ,
){
self.valid = valid
self.key = key
self.value = value
self.leaf = leaf
self.path = path
}
}
pub struct NonExistenceProof {
pub var valid: Bool
pub var key:[UInt8]
pub var left:ExistenceProof
pub var right:ExistenceProof
init(
valid: Bool,
key:[UInt8],
left:ExistenceProof,
right:ExistenceProof,
) {
self.valid = valid
self.key = key
self.left = left
self.right = right
}
}
pub struct LeafOp {
pub var valid: Bool
pub var hash:HashOp
pub var prehash_key:HashOp
pub var prehash_value:HashOp
pub var len:LengthOp
pub var prefix: [UInt8]
init(
valid: Bool,
hash:HashOp,
prehash_key:HashOp,
prehash_value:HashOp,
len:LengthOp,
prefix: [UInt8]
) {
self.valid=valid
self.hash=hash
self.prehash_key=prehash_key
self.prehash_value=prehash_value
self.len=len
self.prefix=prefix
}
}
pub struct InnerOp {
pub var valid: Bool
pub var hash:HashOp
pub var prefix: [UInt8]
pub var suffix: [UInt8]
init(
valid: Bool,
hash:HashOp,
prefix: [UInt8],
suffix: [UInt8],
) {
self.valid=valid
self.hash=hash
self.suffix=suffix
self.prefix=prefix
}
}
pub struct ProofSpec {
pub var leafSpec: LeafOp
pub var innerSpec: InnerSpec
pub var maxDepth: Int
pub var minDepth: Int
init(
leafSpec: LeafOp,
innerSpec:InnerSpec,
) {
self.leafSpec=leafSpec
self.innerSpec=innerSpec
self.maxDepth=0
self.minDepth=0
}
}
pub struct InnerSpec {
pub var childOrder: [Int]
pub var childSize: Int
pub var minPrefixLength: Int
pub var maxPrefixLength: Int
pub var emptyChild: [UInt8]
pub var hash: HashOp
init(
childOrder: [Int] ,
childSize: Int ,
minPrefixLength: Int ,
maxPrefixLength: Int ,
emptyChild: [UInt8] ,
hash: HashOp ,
) {
self.childOrder=childOrder
self.childSize=childSize
self.minPrefixLength=minPrefixLength
self.maxPrefixLength=maxPrefixLength
self.emptyChild=emptyChild
self.hash=hash
}
}
pub fun getIavlSpec() : ProofSpec {
let leafSpec = LeafOp(
valid: true,
hash: HashOp.SHA256,
prehash_key: HashOp.NO_HASH,
prehash_value: HashOp.SHA256,
len: LengthOp.VAR_PROTO,
prefix: [0 as UInt8]
);
let innerSpec = InnerSpec(
childOrder: [0 as Int,1 as Int],
childSize: 33,
minPrefixLength: 4,
maxPrefixLength: 12,
emptyChild: [],
hash: HashOp.SHA256
);
return ProofSpec(leafSpec: leafSpec, innerSpec: innerSpec
)
}
pub enum Ordering: UInt8{
pub case LT
pub case EQ
pub case GT
}
pub fun checkAgainstSpecLeafOp(op: LeafOp, spec: ProofSpec)
{
assert(op.hash == spec.leafSpec.hash,message: "Unexpected HashOp");
assert(
op.prehash_key == spec.leafSpec.prehash_key,
message: "Unexpected PrehashKey"
);
assert(
op.prehash_value == spec.leafSpec.prehash_value,
message: "Unexpected PrehashKey"
);
assert(op.len == spec.leafSpec.len,message: "UnexpecteleafSpec LengthOp");
let leafSpecPrefix = String.encodeHex(spec.leafSpec.prefix)
let opPrefix = String.encodeHex(op.prefix)
assert(
spec.leafSpec.prefix.contains(op.prefix.removeFirst()),
message: "LeafOpLib: wrong prefix"
);
}
pub fun applyValueLeafOp(
op:LeafOp,
key:[UInt8],
value:[UInt8],
) : [UInt8] {
assert(key.length > 0, message: "Leaf op needs key");
assert(value.length > 0,message: "Leaf op needs value");
let data =
op.prefix.concat(
self.prepareLeafData(hashOp:op.prehash_key, lengthOp :op.len, data: key)
).concat(
self.prepareLeafData(hashOp:op.prehash_value, lengthOp :op.len, data: value)
);
return self.doHash(hashOp:op.hash, data: data);
}
pub fun prepareLeafData(
hashOp:HashOp,
lengthOp:LengthOp,
data:[UInt8] ,
) : [UInt8] {
return self.doLength(lengthOp: lengthOp, data :self.doHashOrNoop(hashOp: hashOp, data: data));
}
pub fun doHashOrNoop(
hashOp:HashOp ,
data:[UInt8] ,
) : [UInt8]
{
if (hashOp == HashOp.NO_HASH) {
return data;
}
return self.doHash(hashOp: hashOp, data: data);
}
pub fun checkAgainstSpecInnerOp(op: InnerOp, spec: ProofSpec)
{
assert(op.hash == spec.leafSpec.hash,message: "Unexpected HashOp");
let leafSpecPrefix = String.encodeHex(spec.leafSpec.prefix)
let opPrefix = String.encodeHex(op.prefix)
assert(
spec.leafSpec.prefix.contains(op.prefix.removeFirst()),
message: "LeafOpLib: wrong prefix"
);
assert(
op.prefix.length >= spec.innerSpec.minPrefixLength,
message: "InnerOp prefix too short"
);
let maxLeftChildLen = (spec.innerSpec.childOrder.length - 1) * spec.innerSpec.childSize;
assert(
op.prefix.length <=
(spec.innerSpec.maxPrefixLength) + maxLeftChildLen,
message: "InnerOp prefix too short"
);
}
pub fun applyValueInnerOp(op: InnerOp, child:[UInt8]): [UInt8]
{
assert(child.length > 0, message: "Inner op needs child value");
return self.doHash(
hashOp: op.hash,
data: op.prefix.concat(child).concat(op.suffix)
);
}
pub fun doHash(hashOp: HashOp, data: [UInt8]):[UInt8]
{
let a = Crypto.hash(data, algorithm: HashAlgorithm.SHA3_256)
if (hashOp == HashOp.SHA256) {
return a
}
panic("Unsupported hashop");
}
pub fun doLength(lengthOp: LengthOp , data: [UInt8]): [UInt8]
{
if (lengthOp == LengthOp.NO_PREFIX) {
return data;
}
if (lengthOp == LengthOp.VAR_PROTO) {
return self.encodeVarintProto(n: data.length).concat(data);
}
if (lengthOp == LengthOp.REQUIRE_32_BYTES) {
assert(data.length == 32, message: "Expected 32 [UInt8]");
return data;
}
if (lengthOp == LengthOp.REQUIRE_64_BYTES) {
assert(data.length == 64, message: "Expected 64 [UInt8]");
return data;
}
panic("Unsupported lengthop");
}
pub fun encodeVarintProto(n: Int) : [UInt8] {
// Count the number of groups of 7 bits
// We need this pre-processing step since Solidity doesn't allow dynamic resizing
var tmp: Int = n
var num_bytes: Int = 1
while (tmp > 0x7F) {
tmp = tmp >> 7
num_bytes = num_bytes + 1
}
var buf: [UInt8] = []
tmp = n
var i: Int = 0
while (i < num_bytes) {
// Set the first bit in the byte for each group of 7 bits
buf[i] = (0x80 | UInt8(tmp & 0x7F))
tmp = tmp >> 7
i = i + 1
}
// Unset the first bit of the last byte
buf[num_bytes - 1] = buf[num_bytes - 1] & 0x7F
return buf
}
pub fun verify(
proof: ExistenceProof,
spec: ProofSpec,
root: [UInt8],
key: [UInt8],
value: [UInt8],
) {
self.checkAgainstSpec(proof: proof, spec: spec);
assert(
String.encodeHex(proof.key) == String.encodeHex(key),
message: "Provided key doesn't match proof"
)
assert(
String.encodeHex(proof.value) == String.encodeHex(value),
message: "Provided value doesn't match proof"
)
assert(
String.encodeHex(self.calculate(p: proof)) == String.encodeHex(root),
message: "Calculcated root doesn't match provided root"
)
}
pub fun checkAgainstSpec(proof: ExistenceProof, spec: ProofSpec)
{
self.checkAgainstSpecLeafOp(op: proof.leaf, spec: spec)
assert(
spec.minDepth == 0 || proof.path.length >= (spec.minDepth),
message: "InnerOps depth too short"
)
assert(
spec.maxDepth == 0 || proof.path.length >= (spec.maxDepth),
message: "InnerOps depth too short"
)
for path in proof.path {
self.checkAgainstSpecInnerOp(op: path, spec: spec)
}
}
// Calculate determines the root hash that matches the given proof.
// You must validate the result is what you have in a header.
// Returns error if the calculations cannot be performed.
pub fun calculate(p: ExistenceProof) : [UInt8]
{
// leaf step takes the key and value as input
var res = self.applyValueLeafOp(op: p.leaf, key: p.key, value: p.value)
// the rest just take the output of the last step (reducing it)
for path in p.path {
res = self.applyValueInnerOp(op: path, child: res)
}
return res
}
}