-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgb_merkle_trees.erl
565 lines (515 loc) · 21 KB
/
gb_merkle_trees.erl
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
%% Licensed under the Apache License, Version 2.0 (the “License”);
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an “AS IS” BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc General balanced binary Merkle trees. Similar to {@link //stdlib/gb_trees}, but with Merkle proofs.
%%
%% Keys and values need to be binaries. Values are stored only in leaf nodes to shorten Merkle proofs.
%%
%% Hashes of leaf nodes are based on concatenation of hashes of key and value. Hashes of inner nodes are based on concatenation of hashes of left and right node.
%%
%% Similarly as in {@link //stdlib/gb_trees}, deletions do not cause trees to rebalance.
%%
%% SHA-256 is used as the default hashing algorithm. You can define the `GB_MERKLE_TREES_HASH_ALGORITHM' macro to use another algorithm. See documentation of {@link //crypto/crypto:hash/2} for available choices.
%%
%% @author Krzysztof Jurewicz <krzysztof.jurewicz@gmail.com> [http://jurewicz.org.pl]
%%
%% @reference See <a href="http://cglab.ca/~morin/teaching/5408/refs/a99.pdf">Arne Andersson’s “General Balanced Trees” article</a> for insights about the balancing algorithm. The original balance condition has been changed to 2^h(T) ≤ |T|^2.
%% @reference See <a href="https://github.com/tendermint/go-merkle">go-merkle</a> for a similar in purpose library written in Go which uses AVL trees instead of general balanced trees.
%% @see //stdlib/gb_trees
%% @see //crypto/crypto:hash/2
-module(gb_merkle_trees).
-export([balance/1,
delete/2,
empty/0,
enter/3,
foldr/3,
from_list/1,
from_orddict/1,
from_orddict/2,
keys/1,
lookup/2,
merkle_proof/2,
root_hash/1,
size/1,
to_orddict/1,
verify_merkle_proof/4]).
-ifdef(TEST).
-include_lib("triq/include/triq.hrl").
-include_lib("eunit/include/eunit.hrl").
-endif.
-ifndef(GB_MERKLE_TREES_HASH_ALGORITHM).
-define(GB_MERKLE_TREES_HASH_ALGORITHM, sha256).
-endif.
-define(HASH(X), crypto:hash(?GB_MERKLE_TREES_HASH_ALGORITHM, X)).
%% Trees are balanced using the condition 2^h(T) ≤ |T|^C
-define(C, 2).
-type key() :: binary().
-type value() :: binary().
-type hash() :: binary().
%% We distinguish inner nodes and tree nodes by tuple length instead of using records to save some space.
-type leaf_node() :: {key(), value(), hash()}.
-type inner_node() :: {key(), hash() | to_be_computed, Left :: inner_node() | leaf_node(), Right :: inner_node() | leaf_node()}.
-type tree_node() :: leaf_node() | inner_node() | empty.
-opaque tree() :: {Size :: non_neg_integer(), RootNode :: tree_node()}.
-type merkle_proof() :: {hash() | merkle_proof(), hash() | merkle_proof()}.
-export_type(
[key/0,
value/0,
hash/0,
tree/0,
merkle_proof/0]).
-spec delete(key(), tree()) -> tree().
%% @doc Remove key from tree. The key must be present in the tree.
delete(Key, {Size, RootNode}) ->
{Size - 1, delete_1(Key, RootNode)}.
-spec delete_1(key(), tree_node()) -> tree_node().
delete_1(Key, {Key, _, _}) ->
empty;
delete_1(Key, {InnerKey, _, LeftNode, RightNode}) ->
case Key < InnerKey of
true ->
case delete_1(Key, LeftNode) of
empty ->
RightNode;
NewLeftNode ->
{InnerKey, inner_hash(node_hash(NewLeftNode), node_hash(RightNode)), NewLeftNode, RightNode}
end;
_ ->
case delete_1(Key, RightNode) of
empty ->
LeftNode;
NewRightNode ->
{InnerKey, inner_hash(node_hash(LeftNode), node_hash(NewRightNode)), LeftNode, NewRightNode}
end
end.
-spec empty() -> tree().
%% @doc Return an empty tree.
empty() ->
{0, empty}.
-spec size(tree()) -> non_neg_integer().
%% @doc Return number of elements stored in the tree.
size({Size, _}) ->
Size.
-spec leaf_hash(key(), value()) -> hash().
leaf_hash(Key, Value) ->
KeyHash = ?HASH(Key),
ValueHash = ?HASH(Value),
?HASH(<<KeyHash/binary, ValueHash/binary>>).
-spec inner_hash(hash(), hash()) -> hash().
inner_hash(LeftHash, RightHash) ->
?HASH(<<LeftHash/binary, RightHash/binary>>).
-spec root_hash(tree()) -> hash() | undefined.
%% @doc Return the hash of root node.
root_hash({_, RootNode}) ->
node_hash(RootNode).
-spec merkle_proof(key(), tree()) -> merkle_proof().
%% @doc For a given key return a proof that, along with its value, it is contained in tree.
%% Hash for root node is not included in the proof.
merkle_proof(Key, {_Size, RootNode}) ->
merkle_proof_node(Key, RootNode).
-spec merkle_proof_node(key(), tree_node()) -> merkle_proof().
merkle_proof_node(Key, {Key, Value, _}) ->
{?HASH(Key), ?HASH(Value)};
merkle_proof_node(Key, {InnerKey, _, Left, Right}) ->
case Key < InnerKey of
true ->
{merkle_proof_node(Key, Left), node_hash(Right)};
_ ->
{node_hash(Left), merkle_proof_node(Key, Right)}
end.
-spec verify_merkle_proof(key(), value(), Root::hash(), merkle_proof()) ->
ok | {error, Reason} when
Reason :: {key_hash_mismatch, hash()}
| {value_hash_mismatch, hash()}
| {root_hash_mismatch, hash()}.
%% @doc Verify a proof against a leaf and a root node hash.
verify_merkle_proof(Key, Value, RootHash, Proof) ->
{KH, VH} = {?HASH(Key), ?HASH(Value)},
{PKH, PVH} = bottom_merkle_proof_pair(Proof),
if
PKH =/= KH ->
{error, {key_hash_mismatch, PKH}};
PVH =/= VH ->
{error, {value_hash_mismatch, PKH}};
true ->
PRH = merkle_fold(Proof),
if
PRH =/= RootHash ->
{error, {root_hash_mismatch, PRH}};
true ->
ok
end
end.
-spec from_list(list({key(), value()})) -> tree().
%% @doc Create a tree from a list.
%% This creates a tree by iteratively inserting elements and not necessarily results in a perfect balance, like the one obtained when running {@link from_orddict/1}.
from_list(List) ->
from_list(List, empty()).
-spec from_list(list({key(), value()}), Acc :: tree()) -> tree().
from_list([], Acc) ->
Acc;
from_list([{Key, Value}|Rest], Acc) ->
from_list(Rest, enter(Key, Value, Acc)).
-spec from_orddict(OrdDict :: list({key(), value()})) -> tree().
%% @equiv from_orddict(OrdDict, length(OrdDict))
from_orddict(OrdDict) ->
from_orddict(OrdDict, length(OrdDict)).
-spec from_orddict(list({key(), value()}), Size :: non_neg_integer()) -> tree().
%% @doc Create a perfectly balanced tree from an ordered dictionary.
from_orddict(OrdDict, Size) ->
{Size, balance_orddict(OrdDict, Size)}.
-spec to_orddict(tree()) -> list({key(), value()}).
%% @doc Convert tree to an orddict.
to_orddict(Tree) ->
foldr(
fun (KV, Acc) ->
[KV|Acc]
end,
[],
Tree).
-spec keys(tree()) -> list(key()).
%% @doc Return the keys as an ordered list.
keys(Tree) ->
foldr(
fun ({Key, _}, Acc) -> [Key|Acc] end,
[],
Tree).
-spec foldr(fun(({key(), value()}, Acc :: any()) -> any()), Acc :: any(), tree()) -> Acc :: any().
%% @doc Iterate through keys and values, from those with highest keys to lowest.
foldr(Fun, Acc, {_, RootNode}) ->
foldr_1(Fun, Acc, RootNode).
-spec foldr_1(fun(({key(), value()}, Acc :: any()) -> any()), Acc :: any(), tree_node()) -> Acc :: any().
foldr_1(_, Acc, empty) ->
Acc;
foldr_1(F, Acc, _LeafNode={Key, Value, _}) ->
F({Key, Value}, Acc);
foldr_1(F, Acc, {_, _, Left, Right}) ->
foldr_1(F, foldr_1(F, Acc, Right), Left).
-spec node_hash(tree_node()) -> hash() | undefined.
node_hash(empty) ->
undefined;
node_hash({_, _, Hash}) ->
Hash;
node_hash({_, Hash, _, _}) ->
Hash.
-spec enter(key(), value(), tree()) -> tree().
%% @doc Insert or update key and value into tree.
enter(Key, Value, {Size, RootNode}) ->
{NewRootNode, undefined, undefined, KeyExists} = enter_1(Key, Value, RootNode, 0, Size),
NewSize =
case KeyExists of
true -> Size;
_ -> Size + 1
end,
{NewSize, NewRootNode}.
-spec enter_1(key(), value(), tree_node(), Depth :: non_neg_integer(), TreeSize :: non_neg_integer()) ->
{tree_node(), RebalancingCount :: pos_integer() | undefined, Height :: non_neg_integer() | undefined, KeyExists :: boolean()}.
enter_1(Key, Value, empty, _, _) ->
{{Key, Value, leaf_hash(Key, Value)}, undefined, undefined, false};
enter_1(Key, Value, ExistingLeafNode={ExistingKey, _, _}, Depth, TreeSize) ->
NewLeafNode = {Key, Value, leaf_hash(Key, Value)},
case Key =:= ExistingKey of
true ->
{NewLeafNode, undefined, undefined, true};
_ ->
NewTreeSize = TreeSize + 1,
NewDepth = Depth + 1,
{InnerKey, LeftNode, RightNode} =
case Key > ExistingKey of
true ->
{Key, ExistingLeafNode, NewLeafNode};
_ ->
{ExistingKey, NewLeafNode, ExistingLeafNode}
end,
case rebalancing_needed(NewTreeSize, NewDepth) of
true ->
{{InnerKey, to_be_computed, LeftNode, RightNode},
2,
1,
false};
_ ->
{{InnerKey, inner_hash(node_hash(LeftNode), node_hash(RightNode)), LeftNode, RightNode},
undefined,
undefined,
false}
end
end;
enter_1(Key, Value, InnerNode={InnerKey, _, LeftNode, RightNode}, Depth, TreeSize) ->
NodeToFollowSymb =
case Key < InnerKey of
true -> left;
_ -> right
end,
{NodeToFollow, NodeNotChanged} =
case NodeToFollowSymb of
right -> {RightNode, LeftNode};
left -> {LeftNode, RightNode}
end,
{NewNode, RebalancingCount, Height, KeyExists} = enter_1(Key, Value, NodeToFollow, Depth + 1, TreeSize),
{NewLeftNode, NewRightNode} =
case NodeToFollowSymb of
right ->
{LeftNode, NewNode};
_ ->
{NewNode, RightNode}
end,
case RebalancingCount of
undefined ->
{update_inner_node(InnerNode, NewLeftNode, NewRightNode), undefined, undefined, KeyExists};
_ ->
Count = RebalancingCount + node_size(NodeNotChanged),
NewHeight = Height + 1,
NewInnerNodeUnbalanced = {InnerKey, to_be_computed, NewLeftNode, NewRightNode},
case may_be_rebalanced(Count, NewHeight) of
true ->
{balance_node(NewInnerNodeUnbalanced, Count),
undefined,
undefined,
KeyExists};
_ ->
{NewInnerNodeUnbalanced,
Count,
NewHeight,
KeyExists}
end
end.
-spec rebalancing_needed(TreeSize :: non_neg_integer(), Depth :: non_neg_integer()) -> boolean().
rebalancing_needed(TreeSize, Depth) ->
math:pow(2, Depth) > math:pow(TreeSize, ?C).
-spec may_be_rebalanced(Count :: non_neg_integer(), Height :: non_neg_integer()) -> boolean().
may_be_rebalanced(Count, Height) ->
math:pow(2, Height) > math:pow(Count, ?C).
-spec node_size(tree_node()) -> non_neg_integer().
node_size(empty) ->
0;
node_size({_, _, _}) ->
1;
node_size({_, _, Left, Right}) ->
node_size(Left) + node_size(Right).
-spec balance_orddict(list({key(), value()}), Size :: non_neg_integer()) -> tree_node().
balance_orddict(KVOrdDict, Size) ->
{Node, []} = balance_orddict_1(KVOrdDict, Size),
Node.
-spec balance_orddict_1(list({key(), value()}), Size :: non_neg_integer()) -> {tree_node(), list({key(), value()})}.
balance_orddict_1(OrdDict, Size) when Size > 1 ->
Size2 = Size div 2,
Size1 = Size - Size2,
{LeftNode, OrdDict1=[{Key, _} | _]} = balance_orddict_1(OrdDict, Size1),
{RightNode, OrdDict2} = balance_orddict_1(OrdDict1, Size2),
InnerNode = {Key, inner_hash(node_hash(LeftNode), node_hash(RightNode)), LeftNode, RightNode},
{InnerNode, OrdDict2};
balance_orddict_1([{Key, Value} | OrdDict], 1) ->
{{Key, Value, leaf_hash(Key, Value)}, OrdDict};
balance_orddict_1(OrdDict, 0) ->
{empty, OrdDict}.
-spec node_to_orddict(tree_node()) -> list({key(), value()}).
node_to_orddict(Node) ->
foldr_1(
fun (KV, Acc) ->
[KV|Acc]
end,
[],
Node).
-spec balance_node(tree_node(), Size :: non_neg_integer()) -> tree_node().
balance_node(Node, Size) ->
KVOrdDict = node_to_orddict(Node),
balance_orddict(KVOrdDict, Size).
-spec balance(tree()) -> tree().
%% @doc Perfectly balance a tree.
balance({Size, RootNode}) ->
{Size, balance_orddict(node_to_orddict(RootNode), Size)}.
-spec lookup(key(), tree()) -> value() | none.
%% @doc Fetch value for key from tree.
lookup(Key, {_, RootNode}) ->
lookup_1(Key, RootNode).
-spec lookup_1(key(), inner_node() | leaf_node()) -> value() | none.
lookup_1(Key, {Key, Value, _}) ->
Value;
lookup_1(Key, {InnerKey, _, Left, Right}) ->
case Key < InnerKey of
true ->
lookup_1(Key, Left);
_ ->
lookup_1(Key, Right)
end;
lookup_1(_, _) ->
none.
-spec update_inner_node(inner_node(), Left :: tree_node(), Right :: tree_node()) -> inner_node().
update_inner_node(Node={Key, _, Left, Right}, NewLeft, NewRight) ->
case lists:map(fun node_hash/1, [Left, Right, NewLeft, NewRight]) of
[LeftHash, RightHash, LeftHash, RightHash] ->
%% Nothing changed, no need to rehash.
Node;
[_, _, NewLeftHash, NewRightHash] ->
{Key, inner_hash(NewLeftHash, NewRightHash), NewLeft, NewRight}
end.
-spec merkle_fold(merkle_proof()) -> hash().
merkle_fold({Left, Right}) ->
LeftHash = merkle_fold(Left),
RightHash = merkle_fold(Right),
?HASH(<<LeftHash/binary, RightHash/binary>>);
merkle_fold(Hash) ->
Hash.
-spec bottom_merkle_proof_pair(merkle_proof()) -> {hash(), hash()}.
bottom_merkle_proof_pair({Pair, Hash}) when is_tuple(Pair), is_binary(Hash) ->
bottom_merkle_proof_pair(Pair);
bottom_merkle_proof_pair({_Hash, Pair}) when is_tuple(Pair) ->
bottom_merkle_proof_pair(Pair);
bottom_merkle_proof_pair(Pair) ->
Pair.
-ifdef(TEST).
empty_test_() ->
[?_assertEqual(0, ?MODULE:size(empty()))].
%% Types for Triq.
key() ->
binary().
value() ->
binary().
kv_orddict() ->
?LET(L, list({key(), value()}), orddict:from_list(L)).
tree() ->
%% The validity of data generated by this generator depends on the validity of the `from_list' function.
%% This should not be a problem as long as the `from_list' function itself is tested.
?LET(KVO, list({key(), value()}), from_list(KVO)).
non_empty_tree() ->
?SUCHTHAT(Tree, tree(), element(1, Tree) > 0).
%% Helper functions for Triq.
-spec height(tree()) -> non_neg_integer().
height({_, RootNode}) ->
node_height(RootNode).
-spec node_height(tree_node()) -> non_neg_integer().
node_height(empty) ->
%% Strictly speaking, there is no height for empty tree.
0;
node_height({_, _, _}) ->
0;
node_height({_, _, Left, Right}) ->
1 + max(node_height(Left), node_height(Right)).
-spec shallow_height(tree()) -> non_neg_integer().
shallow_height({_, RootNode}) ->
node_shallow_height(RootNode).
-spec node_shallow_height(tree_node()) -> non_neg_integer().
node_shallow_height(empty) ->
%% Strictly speaking, there is no height for empty tree.
0;
node_shallow_height({_, _, _}) ->
0;
node_shallow_height({_, _, Left, Right}) ->
1 + min(node_shallow_height(Left), node_shallow_height(Right)).
-spec is_perfectly_balanced(tree()) -> boolean().
is_perfectly_balanced(Tree) ->
height(Tree) - shallow_height(Tree) =< 1.
-spec fun_idempotent(F :: fun((X) -> X), X) -> boolean().
%% @doc Return true if F(X) =:= X.
fun_idempotent(F, X) ->
F(X) =:= X.
prop_lookup_does_not_fetch_deleted_key() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
none =:= lookup(Key, delete(Key, enter(Key, Value, Tree)))).
prop_deletion_decreases_size_by_1() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
?MODULE:size(enter(Key, Value, Tree)) - 1 =:= ?MODULE:size(delete(Key, enter(Key, Value, Tree)))).
prop_merkle_proofs_fold_to_root_hash() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
root_hash(enter(Key, Value, Tree)) =:= merkle_fold(merkle_proof(Key, enter(Key, Value, Tree)))).
prop_merkle_proofs_contain_kv_hashes_at_the_bottom() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
bottom_merkle_proof_pair(merkle_proof(Key, enter(Key, Value, Tree))) =:= {?HASH(Key), ?HASH(Value)}).
prop_merkle_proofs_can_be_verified() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
ok =:= verify_merkle_proof(Key, Value, root_hash(enter(Key, Value, Tree)), merkle_proof(Key, enter(Key, Value, Tree)))).
prop_merkle_proofs_verification_reports_mismatch_for_wrong_key() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
case verify_merkle_proof(<<"X", Key/binary>>, Value, root_hash(enter(Key, Value, Tree)), merkle_proof(Key, enter(Key, Value, Tree))) of
{error, {key_hash_mismatch, H}} when is_binary(H) ->
true;
_ ->
false
end).
prop_merkle_proofs_verification_reports_mismatch_for_wrong_value() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
case verify_merkle_proof(Key, <<"X", Value/binary>>, root_hash(enter(Key, Value, Tree)), merkle_proof(Key, enter(Key, Value, Tree))) of
{error, {value_hash_mismatch, H}} when is_binary(H) ->
true;
_ ->
false
end).
prop_merkle_proofs_verification_reports_mismatch_for_wrong_root_hash() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
case verify_merkle_proof(Key, Value, begin RH = root_hash(enter(Key, Value, Tree)), <<"X", RH/binary>> end, merkle_proof(Key, enter(Key, Value, Tree))) of
{error, {root_hash_mismatch, H}} when is_binary(H) ->
true;
_ ->
false
end).
prop_from_list_size() ->
?FORALL(KVList, list({key(), value()}),
length(proplists:get_keys(KVList)) =:= ?MODULE:size(from_list(KVList))).
prop_from_orddict_size() ->
?FORALL(KVO, kv_orddict(),
length(KVO) =:= ?MODULE:size(from_list(KVO))).
prop_orddict_conversion_idempotence() ->
?FORALL(KVO, kv_orddict(), KVO =:= to_orddict(from_orddict(KVO))).
prop_from_orddict_returns_a_perfectly_balanced_tree() ->
?FORALL(KVO, kv_orddict(), is_perfectly_balanced(from_orddict(KVO))).
prop_keys() ->
?FORALL(Tree, tree(), keys(Tree) =:= [Key || {Key, _} <- to_orddict(Tree)]).
from_list_sometimes_doesnt_return_a_perfectly_balanced_tree_test() ->
?assertNotEqual(
true,
triq:counterexample(
?FORALL(
KVList,
list({key(), value()}),
is_perfectly_balanced(from_list(KVList))))).
prop_foldr_iterates_on_proper_ordering_and_contains_no_duplicates() ->
?FORALL(Tree, tree(),
fun_idempotent(
fun lists:usort/1,
foldr(
fun({Key, _}, Acc) -> [Key|Acc] end,
[],
Tree)
)).
prop_enter_is_idempotent() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
fun_idempotent(
fun (Tree_) -> enter(Key, Value, Tree_) end,
enter(Key, Value, Tree))).
prop_entered_value_can_be_retrieved() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
Value =:= lookup(Key, enter(Key, Value, Tree))).
prop_entered_value_can_be_retrieved_after_balancing() ->
?FORALL({Tree, Key, Value},
{tree(), key(), value()},
Value =:= lookup(Key, balance(enter(Key, Value, Tree)))).
prop_height_constrained() ->
?FORALL(Tree, non_empty_tree(), math:pow(2, height(Tree)) =< math:pow(?MODULE:size(Tree), ?C)).
prop_balancing_yields_same_orddict() ->
?FORALL(Tree, tree(), to_orddict(Tree) =:= to_orddict(balance(Tree))).
prop_entering_key_second_time_does_not_increase_size() ->
?FORALL({Tree, Key, Value1, Value2},
{tree(), key(), value(), value()},
?MODULE:size(enter(Key, Value1, Tree)) =:= ?MODULE:size(enter(Key, Value2, enter(Key, Value1, Tree)))).
prop_tree_after_explicit_balancing_is_perfectly_balanced() ->
?FORALL(Tree, tree(), is_perfectly_balanced(balance(Tree))).
-endif.