Source file merkle_list.ml
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
type error += Merkle_list_invalid_position
let max_depth ~count_limit =
let log2 n = Z.numbits (Z.of_int n) in
log2 count_limit
let _ =
register_error_kind
`Temporary
~id:"Merkle_list_invalid_position"
~title:"Merkle_list_invalid_position"
~description:"Merkle_list_invalid_position"
~pp:(fun ppf () -> Format.fprintf ppf "%s" "Merkle_list_invalid_position")
Data_encoding.empty
(function Merkle_list_invalid_position -> Some () | _ -> None)
(fun () -> Merkle_list_invalid_position)
module type T = sig
type t
type h
type elt
type path
val dummy_path : path
val pp_path : Format.formatter -> path -> unit
val nil : t
val empty : h
val root : t -> h
val snoc : t -> elt -> t
val snoc_tr : t -> elt -> t
val compute : elt list -> h
val path_encoding : path Data_encoding.t
val bounded_path_encoding : ?max_length:int -> unit -> path Data_encoding.t
val compute_path : t -> int -> path tzresult
val check_path : path -> int -> elt -> h -> bool tzresult
val path_depth : path -> int
val elt_bytes : elt -> Bytes.t
module Internal_for_tests : sig
val path_to_list : path -> h list
val equal : t -> t -> bool
val to_list : t -> h list
end
end
module Make (El : sig
type t
val to_bytes : t -> bytes
end)
(H : S.HASH) : T with type elt = El.t and type h = H.t = struct
type h = H.t
type elt = El.t
let elt_bytes = El.to_bytes
type tree = Empty | Leaf of h | Node of (h * tree * tree)
type t = {tree : tree; depth : int; next_pos : int}
type path = h list
let dummy_path = []
let pp_path ppf =
Format.fprintf
ppf
"%a"
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ";@ ")
H.pp)
let empty = H.zero
let root = function Empty -> empty | Leaf h -> h | Node (h, _, _) -> h
let nil = {tree = Empty; depth = 0; next_pos = 0}
let hash_elt el = H.hash_bytes [elt_bytes el]
let leaf_of el = Leaf (hash_elt el)
let hash2 h1 h2 = H.(hash_bytes [to_bytes h1; to_bytes h2])
let node_of t1 t2 = Node (hash2 (root t1) (root t2), t1, t2)
let to_bin ~pos ~depth =
let rec aux acc pos depth =
let (pos', dir) = (pos / 2, pos mod 2) in
match depth with
| 0 -> acc
| d -> aux (Compare.Int.(dir = 1) :: acc) pos' (d - 1)
in
aux [] pos depth
let make_spine_with el =
let rec aux left = function
| 0 -> left
| d -> (aux [@tailcall]) (node_of left Empty) (d - 1)
in
aux (leaf_of el)
let snoc t (el : elt) =
let rec traverse tree depth key =
match (tree, key) with
| (Node (_, t_left, Empty), true :: _key) ->
let t_right = make_spine_with el (depth - 1) in
node_of t_left t_right
| (Node (_, t_left, Empty), false :: key) ->
let t_left = traverse t_left (depth - 1) key in
node_of t_left Empty
| (Node (_, t_left, t_right), true :: key) ->
let t_right = traverse t_right (depth - 1) key in
node_of t_left t_right
| (_, _) ->
assert false
in
let (tree', depth') =
match (t.tree, t.depth, t.next_pos) with
| (Empty, 0, 0) -> (node_of (leaf_of el) Empty, 1)
| (tree, depth, pos) when Int32.(equal (shift_left 1l depth) (of_int pos))
->
let t_right = make_spine_with el depth in
(node_of tree t_right, depth + 1)
| (tree, depth, pos) ->
let key = to_bin ~pos ~depth in
(traverse tree depth key, depth)
in
{tree = tree'; depth = depth'; next_pos = t.next_pos + 1}
type zipper = Left of zipper * tree | Right of tree * zipper | Top
let rec rebuild_tree z t =
match z with
| Top -> t
| Left (z, r) -> (rebuild_tree [@tailcall]) z (node_of t r)
| Right (l, z) -> (rebuild_tree [@tailcall]) z (node_of l t)
let snoc_tr t (el : elt) =
let rec traverse (z : zipper) tree depth key =
match (tree, key) with
| (Node (_, t_left, Empty), true :: _key) ->
let t_right = make_spine_with el (depth - 1) in
rebuild_tree z (node_of t_left t_right)
| (Node (_, t_left, Empty), false :: key) ->
let z = Left (z, Empty) in
(traverse [@tailcall]) z t_left (depth - 1) key
| (Node (_, t_left, t_right), true :: key) ->
let z = Right (t_left, z) in
(traverse [@tailcall]) z t_right (depth - 1) key
| (_, _) ->
assert false
in
let (tree', depth') =
match (t.tree, t.depth, t.next_pos) with
| (Empty, 0, 0) -> (node_of (leaf_of el) Empty, 1)
| (tree, depth, pos) when Int32.(equal (shift_left 1l depth) (of_int pos))
->
let t_right = make_spine_with el depth in
(node_of tree t_right, depth + 1)
| (tree, depth, pos) ->
let key = to_bin ~pos ~depth in
(traverse Top tree depth key, depth)
in
{tree = tree'; depth = depth'; next_pos = t.next_pos + 1}
let rec tree_to_list = function
| Empty -> []
| Leaf h -> [h]
| Node (_, t_left, t_right) -> tree_to_list t_left @ tree_to_list t_right
let path_encoding = Data_encoding.(list H.encoding)
let bounded_path_encoding ?max_length () =
match max_length with
| None -> path_encoding
| Some max_length -> Data_encoding.((list ~max_length) H.encoding)
let compute_path {tree; depth; next_pos} pos =
if Compare.Int.(pos < 0 || pos >= next_pos) then
error Merkle_list_invalid_position
else
let key = to_bin ~pos ~depth in
let rec aux acc tree key =
match (tree, key) with
| (Leaf _, []) -> ok acc
| (Node (_, l, r), b :: key) ->
if b then aux (root l :: acc) r key else aux (root r :: acc) l key
| _ -> error Merkle_list_invalid_position
in
aux [] tree key
let check_path path pos el expected_root =
let depth = List.length path in
if
Compare.Int.(pos >= 0)
&& Compare.Z.(Z.of_int pos < Z.shift_left Z.one depth)
then
let key = List.rev @@ to_bin ~pos ~depth in
let computed_root =
List.fold_left
(fun acc (sibling, b) ->
if b then hash2 sibling acc else hash2 acc sibling)
(hash_elt el)
(List.combine_drop path key)
in
ok (H.equal computed_root expected_root)
else error Merkle_list_invalid_position
let path_depth path = List.length path
let compute l =
let rec aux l =
let rec pairs acc = function
| [] -> List.rev acc
| [x] -> List.rev (hash2 x empty :: acc)
| x :: y :: xs -> pairs (hash2 x y :: acc) xs
in
match pairs [] l with [] -> empty | [h] -> h | pl -> aux pl
in
aux (List.map hash_elt l)
let root t = root t.tree
module Internal_for_tests = struct
let path_to_list x = x
let to_list tree = tree_to_list tree.tree
let equal t1 t2 =
let rec eq_tree t1 t2 =
match (t1, t2) with
| (Empty, Empty) -> true
| (Leaf h1, Leaf h2) -> H.equal h1 h2
| (Node (h1, l1, r1), Node (h2, l2, r2)) ->
H.equal h1 h2 && eq_tree l1 l2 && eq_tree r1 r2
| _ -> false
in
Compare.Int.equal t1.depth t2.depth
&& Compare.Int.equal t1.next_pos t2.next_pos
&& eq_tree t1.tree t2.tree
end
end