-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMTrees.v
2375 lines (2233 loc) · 89.7 KB
/
MTrees.v
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** Copyright (c) 2015 Bill White **)
(** Distributed under the MIT/X11 software license **)
(** See http://www.opensource.org/licenses/mit-license.php **)
(** MTrees: Merkle tree representations of ledger functions. Hash values
are represented by option hashval where None always represents 'empty' data.
The empty asset list hashes to None, and if a node in the tree has two empty
children then its corresponding hash is also None.
The type hlist allows the approximation of asset lists by only listing
a prefix of the list and representing the rest by a hash value.
The type mtree n is a binary tree of depth n where a node may be
replaced by hash values representing the missing part.
The leaves are mtree 0 and consist of hlists which may approximate asset lists.
mtree 160 is the type of Merkle trees that may correspond to ledger functions.
In general mtree_approx_fun_p T f means (T:mtree n) approximates (f:bitseq n -> list asset).
An mtree is valid if it approximates a valid ledger function.
The function mtree_hashroot computes the hashvals corresponding to the given tree.
subqm T T' means T' has at least as much information at T.
equi T T' is an equivalence relation on trees.
mtree_supports_tx defines when a tree supports a transaction.
If T approximates f and T supports a transaction, then f also
supports the transaction (mtree_supports_tx_statefun).
Under some conditions on T, the converse also holds (mtree_supports_tx_statefun_conv).
The function tx_mtree_trans transforms an mtree using a transaction.
Together these results mean that if T has sufficient information about the ledger,
then it can be used to determine if the corresponding ledger function supports the transaction.
If T approximates f and the transaction is supported by T, then the
transformed T approximates the transformed f (mtree_approx_trans).
An mtree is normal if it does not contain nodes with empty children.
The function normalize_mtree normalizes mtree. If we normalize T after
transforming via a transaction, it also approximtes the transformed
ledger function (mtree_normal_approx_trans).
Transformations change the value of the assets in the ledger according to the
rewards/fees of the supported transaction (mtree_valid_tx_mtree_trans).
**)
Require Export LedgerStates.
(*** Approximation Code ***)
Inductive hlist : Type :=
| hlistH : hashval -> hlist
| hlistN : hlist
| hlistC : asset -> hlist -> hlist.
Inductive In_hlist (a:asset) : hlist -> Prop :=
| In_hlist_H hl : In_hlist a (hlistC a hl)
| In_hlist_C b hl : In_hlist a hl -> In_hlist a (hlistC b hl)
.
Fixpoint mtree (n:nat) : Type :=
match n with
| 0 => hlist
| S n => sum (option hashval) (mtree n * mtree n)
end.
Definition nehlist : Type := sum hashval (asset * hlist).
Definition nehlist_hlist (hl:nehlist) : hlist :=
match hl with
| inl h => hlistH h
| inr (a,hl) => hlistC a hl
end.
Definition hlist_nehlist (al:hlist) : option nehlist :=
match al with
| hlistH h => Some (inl h)
| hlistC a al' => Some (inr (a,al'))
| hlistN => None
end.
Fixpoint hlist_hashroot (al:hlist) : option hashval :=
match al with
| hlistH h => Some(h)
| hlistN => None
| hlistC a al' =>
match hlist_hashroot al' with
| None => Some (hashpair (hashnat 3) (hashasset a))
| Some k => Some (hashpair (hashnat 4) (hashpair (hashasset a) k))
end
end.
Fixpoint mtree_hashroot {n} : forall (T:mtree n), option hashval :=
match n with
| O => fun (hl:mtree 0) => hlist_hashroot hl
| S n' => fun (T:mtree (S n')) =>
match T with
| inl h => h
| inr (Tl,Tr) => hashopair (mtree_hashroot Tl) (mtree_hashroot Tr)
end
end.
Inductive approx_assetlist : hlist -> list asset -> Prop :=
| approx_assetlist_H h al : Some(h) = hashassetlist al -> approx_assetlist (hlistH h) al
| approx_assetlist_N : approx_assetlist hlistN nil
| approx_assetlist_C a hl al :
approx_assetlist hl al ->
approx_assetlist (hlistC a hl) (cons a al)
.
Definition mtreeH (n:nat) (h:option hashval) : mtree (S n) := inl h.
Definition mtreeB {n} (Tl Tr:mtree n) : mtree (S n) := inr (Tl,Tr).
Fixpoint mtree_approx_fun_p {n} : mtree n -> (bitseq n -> list asset) -> Prop :=
match n with
| O => fun (hl:mtree 0) (f:bitseq 0 -> list asset) =>
hashassetlist (f tt) = hlist_hashroot hl
| S n => fun (T:mtree (S n)) (f:bitseq (S n) -> list asset) =>
match T with
| inl h => exists Tl Tr:mtree n, h = hashopair (mtree_hashroot Tl) (mtree_hashroot Tr)
/\ mtree_approx_fun_p Tl (fun alpha => f (false,alpha))
/\ mtree_approx_fun_p Tr (fun alpha => f (true,alpha))
| inr (Tl,Tr) => mtree_approx_fun_p Tl (fun alpha => f (false,alpha))
/\ mtree_approx_fun_p Tr (fun alpha => f (true,alpha))
end
end.
Lemma mtree_hashroot_mtree_approx_fun_p {n} (T T':mtree n) (f:bitseq n -> list asset) :
mtree_hashroot T = mtree_hashroot T' ->
mtree_approx_fun_p T f ->
mtree_approx_fun_p T' f.
revert T T' f. induction n as [|n IH].
- intros hl hl' f. simpl. congruence.
- intros [h|[Tl Tr]] [h'|[Tl' Tr']] f.
+ intros H1. simpl in H1. congruence.
+ simpl. intros H1. intros [Tl [Tr [H2 [H3 H4]]]].
assert (L1: hashopair (mtree_hashroot Tl) (mtree_hashroot Tr) = hashopair (mtree_hashroot Tl') (mtree_hashroot Tr')) by congruence.
apply hashopairinj in L1. destruct L1 as [L1a L1b].
split.
* revert L1a H3. apply IH.
* revert L1b H4. apply IH.
+ simpl. intros H1 [H2 H3].
exists Tl. exists Tr. repeat split.
* congruence.
* assumption.
* assumption.
+ simpl. intros H1 [H2 H3].
assert (L1: hashopair (mtree_hashroot Tl) (mtree_hashroot Tr) = hashopair (mtree_hashroot Tl') (mtree_hashroot Tr')) by congruence.
apply hashopairinj in L1. destruct L1 as [L1a L1b].
split.
* revert L1a H2. apply IH.
* revert L1b H3. apply IH.
Qed.
Definition mtree_valid_ {n} (alphapre:bitseq n -> addr) (T:mtree n) : Prop :=
exists f:bitseq n -> list asset,
sf_valid_ alphapre f /\ mtree_approx_fun_p T f.
Definition mtree_valid (T:mtree 160) : Prop := mtree_valid_ (fun alpha => alpha) T.
(*** This may be interesting, but it's no longer needed. ***)
Inductive partofmtree {n} (T:mtree n) : forall m, mtree m -> Prop :=
| partofmtreeH : partofmtree T n T
| partofmtreeL m Tl Tr : partofmtree T m Tl -> partofmtree T (S m) (inr (Tl,Tr))
| partofmtreeR m Tl Tr : partofmtree T m Tr -> partofmtree T (S m) (inr (Tl,Tr))
| partofmtreeLH m Tl Tr h : partofmtree T m Tl -> @mtree_hashroot (S m) (inr (Tl,Tr)) = h -> partofmtree T (S m) (inl h)
| partofmtreeRH m Tl Tr h : partofmtree T m Tr -> @mtree_hashroot (S m) (inr (Tl,Tr)) = h -> partofmtree T (S m) (inl h)
.
Lemma mtree_hashroot_eq_valid_ {n} (alphapre:bitseq n -> addr) (T1 T2:mtree n) :
mtree_hashroot T1 = mtree_hashroot T2 ->
(mtree_valid_ alphapre T1 -> mtree_valid_ alphapre T2).
intros H1 [f [H2 H3]]. exists f. split.
- exact H2.
- revert H3. now apply mtree_hashroot_mtree_approx_fun_p.
Qed.
Lemma mtree_hashroot_eq_valid (T1 T2:mtree 160) :
mtree_hashroot T1 = mtree_hashroot T2 ->
(mtree_valid T1 <-> mtree_valid T2).
intros H1. split.
- exact (mtree_hashroot_eq_valid_ (fun alpha => alpha) T1 T2 H1).
- symmetry in H1.
exact (mtree_hashroot_eq_valid_ (fun alpha => alpha) T2 T1 H1).
Qed.
Lemma approx_fun_fnl {n} (Tf Tg:mtree n) f g :
mtree_hashroot Tf = mtree_hashroot Tg ->
mtree_approx_fun_p Tf f ->
mtree_approx_fun_p Tg g ->
forall alpha, f alpha = g alpha.
Proof.
induction n as [|n IH].
- simpl. intros Hfg H1 H2 []. rewrite Hfg in H1. rewrite <- H2 in H1. apply hashassetlistinj in H1. assumption.
- destruct Tf as [hf|[Tfl Tfr]]; destruct Tg as [hg|[Tgl Tgr]].
+ simpl. intros Hfg [Tfl [Tfr [H1a [H1b H1c]]]] [Tgl [Tgr [H2a [H2b H2c]]]] [[|] alpha].
* rewrite Hfg in H1a. rewrite H1a in H2a. apply hashopairinj in H2a. destruct H2a as [H3 H4].
apply (IH Tfr Tgr (fun alpha => f (true,alpha)) (fun alpha => g (true,alpha)) H4 H1c H2c).
* rewrite Hfg in H1a. rewrite H1a in H2a. apply hashopairinj in H2a. destruct H2a as [H3 H4].
apply (IH Tfl Tgl (fun alpha => f (false,alpha)) (fun alpha => g (false,alpha)) H3 H1b H2b).
+ simpl. intros Hfg [Tfl [Tfr [H1a [H1b H1c]]]] [H2b H2c] [[|] alpha].
* rewrite Hfg in H1a. apply hashopairinj in H1a. destruct H1a as [H3 H4].
symmetry in H4.
apply (IH Tfr Tgr (fun alpha => f (true,alpha)) (fun alpha => g (true,alpha)) H4 H1c H2c).
* rewrite Hfg in H1a. apply hashopairinj in H1a. destruct H1a as [H3 H4].
symmetry in H3.
apply (IH Tfl Tgl (fun alpha => f (false,alpha)) (fun alpha => g (false,alpha)) H3 H1b H2b).
+ simpl. intros Hfg [H1b H1c] [Tgl [Tgr [H2a [H2b H2c]]]] [[|] alpha].
* rewrite <- Hfg in H2a. apply hashopairinj in H2a. destruct H2a as [H3 H4].
apply (IH Tfr Tgr (fun alpha => f (true,alpha)) (fun alpha => g (true,alpha)) H4 H1c H2c).
* rewrite <- Hfg in H2a. apply hashopairinj in H2a. destruct H2a as [H3 H4].
apply (IH Tfl Tgl (fun alpha => f (false,alpha)) (fun alpha => g (false,alpha)) H3 H1b H2b).
+ simpl. intros Hfg [H1b H1c] [H2b H2c] [[|] alpha].
* apply hashopairinj in Hfg. destruct Hfg as [H3 H4].
apply (IH Tfr Tgr (fun alpha => f (true,alpha)) (fun alpha => g (true,alpha)) H4 H1c H2c).
* apply hashopairinj in Hfg. destruct Hfg as [H3 H4].
apply (IH Tfl Tgl (fun alpha => f (false,alpha)) (fun alpha => g (false,alpha)) H3 H1b H2b).
Qed.
Fixpoint mtree_supports_addr {n} : mtree n -> bitseq n -> Prop :=
match n with
| O => fun (T:mtree 0) (alpha:bitseq 0) => True
| S n => fun (T:mtree (S n)) (alpha:bitseq (S n)) =>
match T with
| inl None => True
| inl _ => False
| inr (Tl,Tr) =>
match alpha with
| (false,alphar) => mtree_supports_addr Tl alphar
| (true,alphar) => mtree_supports_addr Tr alphar
end
end
end.
Fixpoint mtree_supports_asset (a:asset) {n} : mtree n -> bitseq n -> Prop :=
match n with
| O => fun (hl:mtree 0) (alpha:bitseq 0) => In_hlist a hl
| S n => fun (T:mtree (S n)) (alpha:bitseq (S n)) =>
match T with
| inl _ => False
| inr (Tl,Tr) =>
match alpha with
| (false,alphar) => mtree_supports_asset a Tl alphar
| (true,alphar) => mtree_supports_asset a Tr alphar
end
end
end.
Inductive mtree_asset_value_in T : list addr_assetid -> nat -> Prop :=
| mtree_asset_value_in_nil : mtree_asset_value_in T nil 0
| mtree_asset_value_in_cons h a u inpl alpha v :
mtree_asset_value_in T inpl v ->
mtree_supports_asset a T alpha ->
asset_value a = Some u ->
h = assetid a ->
mtree_asset_value_in T ((alpha,h)::inpl) (u+v)
| mtree_asset_value_in_skip h a inpl alpha v :
mtree_asset_value_in T inpl v ->
mtree_supports_asset a T alpha ->
asset_value a = None ->
h = assetid a ->
mtree_asset_value_in T ((alpha,h)::inpl) v
.
(*** Precondition for checking if tx is a valid tx. ***)
Definition mtree_can_support_tx (tx:Tx) (T : mtree 160) : Prop :=
(forall alpha h, In (alpha,h) (tx_inputs tx) -> exists u, mtree_supports_asset (h,u) T alpha)
/\
(forall alpha u, In (alpha,u) (tx_outputs tx) -> mtree_supports_addr T alpha)
.
Definition mtree_supports_tx (tx:Tx) (T : mtree 160) fee rew : Prop :=
(forall alpha u, In (alpha,u) (tx_outputs tx) -> mtree_supports_addr T alpha)
/\
(exists utot:nat,
mtree_asset_value_in T (tx_inputs tx) utot (*** this condition also ensures all assets are supported ***)
/\
asset_value_out (tx_outputs tx) + fee = utot + rew)
.
(*** assumes hl1 and hl2 have the same hashroot and so are both hash reps of the same asset list ***)
Fixpoint hlist_lub (hl1 hl2:hlist) : hlist :=
match hl1 with
| hlistC h hr1 =>
match hl2 with
| hlistC _ hr2 =>
hlistC h (hlist_lub hr1 hr2)
| _ => hl1
end
| _ => hl2
end.
(*** assumes the two mtrees have the same hashroot and so are both Merkle Tree reps of the same statefun ***)
Fixpoint mtree_lub {n} : mtree n -> mtree n -> mtree n :=
match n with
| O => fun (hl1 hl2:mtree 0) => hlist_lub hl1 hl2
| S n => fun (T1 T2:mtree (S n)) =>
match T1 with
| inl _ => T2
| inr (T1l,T1r) =>
match T2 with
| inl _ => T1
| inr (T2l,T2r) =>
inr (mtree_lub T1l T2l,mtree_lub T1r T2r)
end
end
end.
Definition empty_mtree (n:nat) : mtree n :=
match n with
| O => hlistN
| S n => mtreeH n None
end.
Lemma mtree_hashroot_empty {n} : mtree_hashroot (empty_mtree n) = None.
destruct n as [|n]; reflexivity.
Qed.
Fixpoint empty_mtree_p {n:nat} : mtree n -> Prop :=
match n with
| O => fun hl => hl = hlistN
| S n => fun T =>
match T with
| inl None => True
| inl _ => False
| inr (Tl,Tr) => empty_mtree_p Tl /\ empty_mtree_p Tr
end
end.
Lemma mtree_hashroot_None_empty_mtree_p {n:nat} (T:mtree n) :
mtree_hashroot T = None <-> empty_mtree_p T.
induction n as [|n IH].
- destruct T as [h| |h hl]; simpl.
+ split; discriminate.
+ tauto.
+ destruct (hlist_hashroot hl); split; discriminate.
- destruct T as [[h|]|[Tl Tr]].
+ simpl. split.
* discriminate.
* tauto.
+ simpl. tauto.
+ simpl. split.
* generalize (IH Tl). generalize (IH Tr).
destruct (mtree_hashroot Tl); destruct (mtree_hashroot Tr); simpl; try discriminate.
tauto.
* intros [H1 H2]. apply IH in H1. apply IH in H2.
rewrite H1. rewrite H2. reflexivity.
Qed.
Lemma empty_mtree_p_empty_mtree n : empty_mtree_p (empty_mtree n).
apply mtree_hashroot_None_empty_mtree_p. destruct n; reflexivity.
Qed.
Inductive subqh : hlist -> hlist -> Prop :=
| subqhH h hl2 : hlist_hashroot hl2 = Some(h) -> subqh (hlistH h) hl2
| subqhN : subqh hlistN hlistN
| subqhC h hr1 hr2 : subqh hr1 hr2 -> subqh (hlistC h hr1) (hlistC h hr2).
Fixpoint subqm {n:nat} : mtree n -> mtree n -> Prop :=
match n with
| O => fun hl1 hl2 => subqh hl1 hl2
| S n => fun (T1 T2:mtree (S n)) =>
match T1 with
| inl h => mtree_hashroot T2 = h
| inr (T1l,T1r) =>
match T2 with
| inl _ => False
| inr (T2l,T2r) => subqm T1l T2l /\ subqm T1r T2r
end
end
end.
Lemma subqh_ref hl : subqh hl hl.
induction hl as [h| |h hr IH]; simpl.
- apply subqhH. reflexivity.
- apply subqhN.
- apply subqhC. exact IH.
Qed.
Lemma subqh_lub_1 hl1 hl2 :
hlist_hashroot hl1 = hlist_hashroot hl2 ->
subqh hl1 (hlist_lub hl1 hl2).
revert hl2. induction hl1 as [h1| |h1 hr1 IH]; simpl.
- intros hl2 H1. apply (subqhH h1 hl2). congruence.
- intros [h2| |h2 hr2] H1.
+ discriminate H1.
+ apply subqhN.
+ simpl in H1. destruct (hlist_hashroot hr2); discriminate H1.
- intros [h2| |h2 hr2] H1.
+ apply subqh_ref.
+ apply subqh_ref.
+ apply subqhC. apply IH. simpl in H1.
destruct (hlist_hashroot hr1) as [k1|] eqn: E1;
destruct (hlist_hashroot hr2) as [k2|] eqn: E2.
* inversion H1.
apply hashpairinj in H0. destruct H0 as [_ H0].
apply hashpairinj in H0. destruct H0 as [_ H0].
congruence.
* exfalso. inversion H1.
apply hashpairinj in H0. destruct H0 as [H0 _].
apply hashnatinj in H0. omega.
* exfalso. inversion H1.
apply hashpairinj in H0. destruct H0 as [H0 _].
apply hashnatinj in H0. omega.
* reflexivity.
Qed.
Lemma subqh_lub_2 hl1 hl2 :
hlist_hashroot hl1 = hlist_hashroot hl2 ->
subqh hl2 (hlist_lub hl1 hl2).
revert hl2. induction hl1 as [h1| |h1 hr1 IH]; simpl.
- intros hl2 H1. apply subqh_ref.
- intros [h2| |h2 hr2] H1.
+ discriminate H1.
+ apply subqhN.
+ simpl in H1. destruct (hlist_hashroot hr2); discriminate H1.
- intros [h2| |h2 hr2] H1.
+ apply subqhH. exact H1.
+ exfalso. destruct (hlist_hashroot hr1); discriminate H1.
+ assert (L1: h2 = h1 /\ hlist_hashroot hr1 = hlist_hashroot hr2).
{ simpl in H1; destruct (hlist_hashroot hr1) as [k1|]; destruct (hlist_hashroot hr2) as [k2|]; inversion H1.
- apply hashpairinj in H0. destruct H0 as [_ H0].
apply hashpairinj in H0. destruct H0 as [H2 H0].
apply hashassetinj in H2. split; congruence.
- exfalso.
apply hashpairinj in H0. destruct H0 as [H0 _].
apply hashnatinj in H0. discriminate H0.
- exfalso.
apply hashpairinj in H0. destruct H0 as [H0 _].
apply hashnatinj in H0. discriminate H0.
- apply hashpairinj in H0. destruct H0 as [_ H0].
apply hashassetinj in H0. split; congruence.
}
destruct L1 as [L1a L1b].
subst h2. apply subqhC. apply IH. exact L1b.
Qed.
Lemma subqm_ref {n} (T:mtree n) : subqm T T.
induction n as [|n IH].
- simpl. apply subqh_ref.
- destruct T as [h|[Tl Tr]]; simpl.
+ reflexivity.
+ split; apply IH.
Qed.
Lemma subqm_lub_1 {n} (T1 T2:mtree n) :
mtree_hashroot T1 = mtree_hashroot T2 ->
subqm T1 (mtree_lub T1 T2).
induction n as [|n IH].
- simpl. apply subqh_lub_1.
- destruct T1 as [h1|[T1l T1r]]; destruct T2 as [h2|[T2l T2r]]; simpl.
+ congruence.
+ congruence.
+ intros _. split; apply subqm_ref.
+ intros H1. apply hashopairinj in H1. destruct H1 as [H2 H3]. split.
* apply (IH _ _ H2).
* apply (IH _ _ H3).
Qed.
Lemma subqh_hashroot_eq hl1 hl2 : subqh hl1 hl2 -> hlist_hashroot hl1 = hlist_hashroot hl2.
intros H. induction H as [h hl2 H1| |h hr1 hr2 H1 IH].
- simpl; congruence.
- simpl; congruence.
- simpl. rewrite IH. reflexivity.
Qed.
Theorem subqm_hashroot_eq {n} (T1 T2:mtree n) : subqm T1 T2 -> mtree_hashroot T1 = mtree_hashroot T2.
induction n as [|n IH].
- simpl. apply subqh_hashroot_eq.
- destruct T1 as [h1|[T1l T1r]]; destruct T2 as [h2|[T2l T2r]]; simpl; try congruence; try tauto.
intros [H1 H2]. generalize (IH _ _ H1). generalize (IH _ _ H2). congruence.
Qed.
Lemma subqh_tra hl1 hl2 hl3 : subqh hl1 hl2 -> subqh hl2 hl3 -> subqh hl1 hl3.
intros H1. revert hl3. induction H1 as [h hl H1| |h hr1 hr2 H1 IH1 H2 IH2].
- intros hl3 H2. apply subqhH. apply subqh_hashroot_eq in H2. congruence.
- tauto.
- intros hl3 H2. inversion H2. apply subqhC. now apply IH1.
Qed.
Lemma subqm_tra {n} (T1 T2 T3:mtree n) : subqm T1 T2 -> subqm T2 T3 -> subqm T1 T3.
induction n as [|n IH].
- simpl. apply subqh_tra.
- destruct T1 as [[h1|]|[T1l T1r]]; destruct T2 as [[h2|]|[T2l T2r]]; simpl; intros H1; inversion H1; destruct T3 as [[h3|]|[T3l T3r]]; simpl; intros H2; inversion H2; try tauto.
+ rewrite (subqm_hashroot_eq _ _ H). rewrite (subqm_hashroot_eq _ _ H3).
reflexivity.
+ rewrite (subqm_hashroot_eq _ _ H). rewrite (subqm_hashroot_eq _ _ H3).
reflexivity.
+ split.
* apply (IH _ _ _ H H3).
* apply (IH _ _ _ H0 H4).
Qed.
Lemma hlist_lub_eq_1 (hl1 hl2:hlist) :
hlist_hashroot hl1 = hlist_hashroot hl2 ->
hlist_hashroot (hlist_lub hl1 hl2) = hlist_hashroot hl1.
intros H1. symmetry. apply subqh_hashroot_eq. now apply subqh_lub_1.
Qed.
Lemma hlist_lub_eq_2 (hl1 hl2:hlist) :
hlist_hashroot hl1 = hlist_hashroot hl2 ->
hlist_hashroot (hlist_lub hl1 hl2) = hlist_hashroot hl2.
intros H1. generalize (hlist_lub_eq_1 hl1 hl2 H1). congruence.
Qed.
Lemma mtree_lub_eq_1 {n} (T1 T2:mtree n) :
mtree_hashroot T1 = mtree_hashroot T2 ->
mtree_hashroot (mtree_lub T1 T2) = mtree_hashroot T1.
intros H1. symmetry. apply subqm_hashroot_eq. now apply subqm_lub_1.
Qed.
Lemma mtree_lub_eq_2 {n} (T1 T2:mtree n) :
mtree_hashroot T1 = mtree_hashroot T2 ->
mtree_hashroot (mtree_lub T1 T2) = mtree_hashroot T2.
intros H1. generalize (mtree_lub_eq_1 T1 T2 H1). congruence.
Qed.
Lemma subqh_In_hlist h hl1 hl2 : subqh hl1 hl2 -> In_hlist h hl1 -> In_hlist h hl2.
intros H. induction H as [k hl2| |k hr1 hr2 H IH].
- intros H1. inversion H1.
- intros H1. inversion H1.
- intros H1. inversion H1.
+ apply In_hlist_H.
+ apply In_hlist_C. now apply IH.
Qed.
Lemma empty_supports_addr_lem {n:nat} :
forall T:mtree n, forall alpha:bitseq n,
mtree_hashroot T = None ->
mtree_supports_addr T alpha.
induction n as [|n IH].
- intros [h| |h hl] [].
+ simpl. discriminate.
+ simpl. tauto.
+ simpl. destruct (hlist_hashroot hl); discriminate.
- intros [[h|]|[Tl Tr]].
+ simpl. discriminate.
+ simpl. tauto.
+ intros [[|] gamma] H1; simpl in H1; simpl.
* apply IH. destruct (mtree_hashroot Tr); try tauto.
destruct (mtree_hashroot Tl); discriminate H1.
* apply IH. destruct (mtree_hashroot Tl); try tauto.
destruct (mtree_hashroot Tr); discriminate H1.
Qed.
Lemma subqm_empty {n} (T:mtree n) :
empty_mtree_p T -> subqm (empty_mtree n) T.
induction n as [|n IH].
- simpl. intros H. subst T. apply subqhN.
- destruct T as [[h|]|[Tl Tr]]; try (simpl; tauto).
intros [H1 H2]. simpl.
apply mtree_hashroot_None_empty_mtree_p in H1.
apply mtree_hashroot_None_empty_mtree_p in H2.
rewrite H1. rewrite H2. reflexivity.
Qed.
Lemma mtree_hashroot_empty_p {n} (T:mtree n) :
empty_mtree_p T -> mtree_hashroot T = None.
intros H1. apply subqm_empty in H1.
apply subqm_hashroot_eq in H1. rewrite <- H1.
apply mtree_hashroot_empty.
Qed.
Lemma subqm_supports_addr {n} (T1 T2:mtree n) (alpha:bitseq n) :
subqm T1 T2
-> mtree_supports_addr T1 alpha
-> mtree_supports_addr T2 alpha.
induction n as [|n IH].
- simpl. tauto.
- destruct T1 as [[h1|]|[T1l T1r]]; destruct T2 as [[h2|]|[T2l T2r]]; try (simpl; tauto).
+ discriminate.
+ intros H1 _. apply empty_supports_addr_lem. exact H1.
+ intros [Hl Hr].
destruct alpha as [[|] alphar].
* now apply IH.
* now apply IH.
Qed.
Lemma empty_mtree_p_not_supports_asset (a:asset) {n} (T:mtree n) (alpha:bitseq n) :
empty_mtree_p T
-> ~ mtree_supports_asset a T alpha.
induction n as [|n IH].
- simpl. intros H1 H2. subst T. inversion H2.
- destruct alpha as [[|] alpha].
+ simpl. destruct T as [[h|]|[Tl Tr]].
* tauto.
* tauto.
* intros [H1 H2]. now apply IH.
+ simpl. destruct T as [[h|]|[Tl Tr]].
* tauto.
* tauto.
* intros [H1 H2]. now apply IH.
Qed.
Lemma subqm_supports_asset (a:asset) {n} (T1 T2:mtree n) (alpha:bitseq n) :
subqm T1 T2
-> mtree_supports_asset a T1 alpha
-> mtree_supports_asset a T2 alpha.
induction n as [|n IH].
- simpl. apply subqh_In_hlist.
- destruct T1 as [h1|[T1l T1r]]; destruct T2 as [h2|[T2l T2r]]; simpl; try tauto.
intros [Hl Hr].
destruct alpha as [[|] alphar].
+ now apply IH.
+ now apply IH.
Qed.
Lemma subqm_asset_value_in (T1 T2:mtree 160) (inpl:list addr_assetid) (utot:nat) :
subqm T1 T2
-> mtree_asset_value_in T1 inpl utot
-> mtree_asset_value_in T2 inpl utot.
intros H1 H2. induction H2 as [|h a u inpl alpha v H2 IH H3|h a inpl alpha v H2 IH H3 H3'].
- apply mtree_asset_value_in_nil.
- apply mtree_asset_value_in_cons with (a := a).
+ exact IH.
+ revert H3. apply subqm_supports_asset. exact H1.
+ assumption.
+ assumption.
- apply mtree_asset_value_in_skip with (a := a).
+ exact IH.
+ revert H3. apply subqm_supports_asset. exact H1.
+ exact H3'.
+ assumption.
Qed.
Lemma In_hlist_In_assetlist (a:asset) (hl:hlist) (al:list asset) :
hashassetlist al = hlist_hashroot hl ->
In_hlist a hl -> In a al.
intros H1 H. revert al H1. induction H as [hl|b hl H IH].
- intros [|c al].
+ simpl. destruct (hlist_hashroot hl); discriminate.
+ intros H1. left.
change (match hashassetlist al with
| Some k => Some (hashpair (hashnat 4) (hashpair (hashasset c) k))
| None => Some (hashpair (hashnat 3) (hashasset c))
end =
match hlist_hashroot hl with
| Some k => Some (hashpair (hashnat 4) (hashpair (hashasset a) k))
| None => Some (hashpair (hashnat 3) (hashasset a))
end) in H1.
destruct (hashassetlist al) as [k1|] eqn: E1; destruct (hlist_hashroot hl) as [k2|] eqn: E2.
* inversion H1.
apply hashpairinj in H0. destruct H0 as [_ H0].
apply hashpairinj in H0. destruct H0 as [H0 _].
now apply hashassetinj.
* exfalso. inversion H1.
apply hashpairinj in H0. destruct H0 as [H0 _].
apply hashnatinj in H0. omega.
* exfalso. inversion H1.
apply hashpairinj in H0. destruct H0 as [H0 _].
apply hashnatinj in H0. omega.
* inversion H1.
apply hashpairinj in H0. destruct H0 as [_ H0].
now apply hashassetinj.
- intros [|c al].
+ simpl. destruct (hlist_hashroot hl); discriminate.
+ intros H1.
change (match hashassetlist al with
| Some k => Some (hashpair (hashnat 4) (hashpair (hashasset c) k))
| None => Some (hashpair (hashnat 3) (hashasset c))
end =
match hlist_hashroot hl with
| Some k => Some (hashpair (hashnat 4) (hashpair (hashasset b) k))
| None => Some (hashpair (hashnat 3) (hashasset b))
end) in H1.
right. apply IH.
destruct (hashassetlist al) as [k1|] eqn: E1; destruct (hlist_hashroot hl) as [k2|] eqn: E2.
* inversion H1.
apply hashpairinj in H2. destruct H2 as [_ H2].
apply hashpairinj in H2. destruct H2 as [_ H2].
congruence.
* exfalso. inversion H1.
apply hashpairinj in H2. destruct H2 as [H2 _].
apply hashnatinj in H2. omega.
* exfalso. inversion H1.
apply hashpairinj in H2. destruct H2 as [H2 _].
apply hashnatinj in H2. omega.
* reflexivity.
Qed.
Lemma mtree_supports_asset_In_statefun (a:asset) {n} :
forall (T:mtree n) (f:bitseq n -> list asset),
forall (alpha:bitseq n),
mtree_approx_fun_p T f ->
mtree_supports_asset a T alpha -> In a (f alpha).
induction n as [|n IHn].
- intros hl f [] H1 H2. simpl in *|-*.
now apply In_hlist_In_assetlist with (hl := hl).
- intros [h|[Tl Tr]].
+ intros f alpha H1 [].
+ intros f [[|] alpha] [H1a H1b] H2; simpl in H2.
* now apply (IHn _ _ _ H1b).
* now apply (IHn _ _ _ H1a).
Qed.
Opaque mtree_supports_asset.
Lemma mtree_valid_supports_asset_uniq (a1 a2:asset) (T:mtree 160) (alpha:addr) :
mtree_valid T ->
mtree_supports_asset a1 T alpha ->
mtree_supports_asset a2 T alpha ->
assetid a1 = assetid a2 -> a1 = a2.
intros [f [[_ [Hf2 _]] HTf]] H1 H2 H3.
assert (L1: In a1 (f alpha)).
{ revert H1. apply mtree_supports_asset_In_statefun. exact HTf. }
assert (L2: In a2 (f alpha)).
{ revert H2. apply mtree_supports_asset_In_statefun. exact HTf. }
destruct a1 as [h oblu1].
destruct a2 as [h2 oblu2].
simpl in H3. subst h2.
destruct (Hf2 h alpha oblu1 alpha oblu2 L1 L2) as [_ H4].
congruence.
Qed.
Theorem mtree_supports_tx_can_support tx (T:mtree 160) fee rew :
mtree_supports_tx tx T fee rew ->
mtree_can_support_tx tx T.
intros [Hs1 [utot [Hs2 Hs3]]]. repeat split.
- destruct tx as [inpl outpl]. simpl. simpl in Hs2.
clear Hs1 fee rew Hs3.
induction Hs2 as [|h a u inpl alpha v H1 IH H2 H3 H4|h a inpl alpha v H1 IH H2 H3 H4].
+ intros alpha h [].
+ intros beta k [H5|H5].
* inversion H5. subst beta. subst k. subst h. exists (assetobl a,assetpre a).
destruct a as [h [obl w]]; exact H2.
* apply IH. exact H5.
+ intros beta k [H5|H5].
* inversion H5. subst beta. subst k. subst h. exists (assetobl a,assetpre a).
destruct a as [h [obl w]]; exact H2.
* apply IH. exact H5.
- exact Hs1.
Qed.
Theorem subqm_supports_tx tx (T1 T2:mtree 160) fee rew :
mtree_valid T2 ->
subqm T1 T2 ->
mtree_supports_tx tx T1 fee rew ->
mtree_supports_tx tx T2 fee rew.
intros H0 H1 Hs. generalize Hs. intros [Hs1 [utot [Hs2 Hs3]]].
apply mtree_supports_tx_can_support in Hs.
destruct Hs as [Hc1 Hc2].
split.
- intros alpha u H2. generalize (Hs1 alpha u H2).
apply subqm_supports_addr. exact H1.
- exists utot. split.
+ revert Hs2. apply subqm_asset_value_in. exact H1.
+ exact Hs3.
Qed.
Theorem mtree_supports_tx_lub_1 tx (T1 T2:mtree 160) fee rew :
mtree_valid T1 ->
mtree_hashroot T1 = mtree_hashroot T2 ->
mtree_supports_tx tx T1 fee rew ->
mtree_supports_tx tx (mtree_lub T1 T2) fee rew.
intros H0 H1. apply subqm_supports_tx.
- revert H0. apply mtree_hashroot_eq_valid.
apply mtree_lub_eq_1. exact H1.
- apply subqm_lub_1. exact H1.
Qed.
Fixpoint equi {n:nat} : mtree n -> mtree n -> Prop :=
match n with
| O => fun T1 T2 => T1 = T2
| S n => fun T1 T2 =>
match T1,T2 with
| inl h1, inl h2 => h1 = h2
| inr (T1l,T1r),inr (T2l,T2r) => equi T1l T2l /\ equi T1r T2r
| inl None,inr (T2l,T2r) => empty_mtree_p T2l /\ empty_mtree_p T2r
| inr (T1l,T1r),inl None => empty_mtree_p T1l /\ empty_mtree_p T1r
| _,_ => False
end
end.
Lemma equi_ref {n:nat} (T:mtree n) : equi T T.
induction n as [|n IH].
- simpl. reflexivity.
- destruct T as [[h|]|[Tl Tr]]; simpl.
+ reflexivity.
+ reflexivity.
+ split; apply IH.
Qed.
Lemma equi_sym {n:nat} (T1 T2:mtree n) : equi T1 T2 -> equi T2 T1.
induction n as [|n IH].
- simpl. now symmetry.
- destruct T1 as [[h1|]|[T1l T1r]]; destruct T2 as [[h2|]|[T2l T2r]]; simpl; try tauto; try discriminate.
+ intros H1. symmetry. assumption.
+ intros [H1 H2]. split; now apply IH.
Qed.
Lemma equi_empty_lem {n} : equi (mtreeH n None) (mtreeB (empty_mtree n) (empty_mtree n)).
simpl. split; apply empty_mtree_p_empty_mtree.
Qed.
Lemma equi_empty_1 {n:nat} (T1 T2:mtree n) : empty_mtree_p T1 -> equi T1 T2 -> empty_mtree_p T2.
induction n as [|n IH].
- simpl. congruence.
- destruct T1 as [[h1|]|[T1l T1r]]; destruct T2 as [[h2|]|[T2l T2r]]; simpl; try tauto; try discriminate; try congruence.
intros [H1 H2] [H3 H4]. split.
* apply (IH _ _ H1 H3).
* apply (IH _ _ H2 H4).
Qed.
Lemma equi_empty_2 {n:nat} (T1 T2:mtree n) : empty_mtree_p T1 -> empty_mtree_p T2 -> equi T1 T2.
induction n as [|n IH].
- simpl. congruence.
- destruct T1 as [[h1|]|[T1l T1r]]; destruct T2 as [[h2|]|[T2l T2r]]; simpl; try tauto; try discriminate; try congruence.
intros [H1 H2] [H3 H4]. split.
* apply (IH _ _ H1 H3).
* apply (IH _ _ H2 H4).
Qed.
Lemma equi_tra {n:nat} (T1 T2 T3:mtree n) : equi T1 T2 -> equi T2 T3 -> equi T1 T3.
induction n as [|n IH].
- simpl. congruence.
- destruct T1 as [[h1|]|[T1l T1r]]; destruct T2 as [[h2|]|[T2l T2r]]; destruct T3 as [[h3|]|[T3l T3r]]; simpl; try tauto; try discriminate; try congruence.
+ intros [H1 H2] [H3 H4]. split.
* exact (equi_empty_1 _ _ H1 H3).
* exact (equi_empty_1 _ _ H2 H4).
+ intros [H1 H2] [H3 H4]. split.
* exact (equi_empty_2 _ _ H1 H3).
* exact (equi_empty_2 _ _ H2 H4).
+ intros [H1 H2] [H3 H4]. split.
* apply equi_sym in H1. exact (equi_empty_1 _ _ H3 H1).
* apply equi_sym in H2. exact (equi_empty_1 _ _ H4 H2).
+ intros [H1 H2] [H3 H4]. split.
* now apply (IH _ _ _ H1).
* now apply (IH _ _ _ H2).
Qed.
Lemma mtreeB_equi {n} (Tl Tr Tl' Tr':mtree n) :
equi Tl Tl' -> equi Tr Tr' -> equi (mtreeB Tl Tr) (mtreeB Tl' Tr').
induction n as [|n].
- simpl. tauto.
- intros H1 H2. split; assumption.
Qed.
Lemma mtree_hashroot_equi {n} (T1 T2:mtree n) : equi T1 T2 -> mtree_hashroot T1 = mtree_hashroot T2.
induction n as [|n IH].
- simpl. unfold equi. intros H1. congruence.
- destruct T1 as [[h1|]|[T1l T1r]]; destruct T2 as [[h2|]|[T2l T2r]]; simpl; try tauto.
+ intros [H1 H2].
rewrite (mtree_hashroot_empty_p _ H1).
rewrite (mtree_hashroot_empty_p _ H2).
reflexivity.
+ intros [H1 H2].
rewrite (mtree_hashroot_empty_p _ H1).
rewrite (mtree_hashroot_empty_p _ H2).
reflexivity.
+ intros [H1 H2]. apply IH in H1. apply IH in H2.
congruence.
Qed.
Fixpoint hlist_new_assets (nw:list asset) (old:hlist) : hlist :=
match nw with
| nil => old
| cons u nw' => hlistC u (hlist_new_assets nw' old)
end.
Fixpoint remove_assets_hlist (hl:hlist) (spent:list hashval) : hlist :=
match hl with
| hlistC (h,u) hl' =>
if in_dec hashval_eq_dec h spent then
remove_assets_hlist hl' spent
else
hlistC (h,u) (remove_assets_hlist hl' spent)
| _ => hl
end.
Lemma remove_assets_hlist_iff h u hl spent :
In_hlist (h,u) (remove_assets_hlist hl spent)
<->
In_hlist (h,u) hl /\ ~In h spent.
induction hl as [h'| |[h' u'] ar IH].
- simpl. split.
+ intros H1. inversion H1.
+ intros [H1 _]. inversion H1.
- simpl. split.
+ intros H1. inversion H1.
+ intros [H1 _]. inversion H1.
- simpl. destruct (in_dec hashval_eq_dec h' spent) as [H1|H1]; split.
+ intros H2. apply IH in H2. split.
* apply In_hlist_C. tauto.
* tauto.
+ intros [H2 H3]. inversion H2.
* exfalso. congruence.
* apply IH. tauto.
+ intros H2. inversion H2.
* { split.
- apply In_hlist_H.
- subst h'. assumption.
}
* { apply IH in H0. split.
- apply In_hlist_C. tauto.
- tauto.
}
+ intros [H2 H3]. inversion H2.
* apply In_hlist_H.
* apply In_hlist_C. apply IH. tauto.
Qed.
Fixpoint tx_mtree_trans_ {n:nat} : forall (inpl:list (bitseq n * hashval)%type) (outpl:list (bitseq n * asset)%type) (T:mtree n), mtree n :=
match n with
| 0 =>
fun inpl outpl =>
match inpl,outpl with
| nil,nil => fun hl:mtree 0 => hl
| _,_ => fun hl:mtree 0 =>
hlist_new_assets (map (@snd (bitseq 0) asset) outpl) (remove_assets_hlist hl (map (@snd (bitseq 0) hashval) inpl))
end
| S n =>
fun inpl outpl =>
match inpl,outpl with
| nil,nil => fun (T:mtree (S n)) => T
| _,_ => fun (T:mtree (S n)) =>
match T with
| inl (Some h) => mtreeH n (Some h) (*** error actually ***)
| inl None => (*** assume inpl is nil but outpl isn't ***)
let outpll := strip_bitseq_false outpl in
let outplr := strip_bitseq_true outpl in
mtreeB (tx_mtree_trans_ nil outpll (empty_mtree n)) (tx_mtree_trans_ nil outplr (empty_mtree n))
| inr (Tl,Tr) =>
let inpll := strip_bitseq_false inpl in
let inplr := strip_bitseq_true inpl in
let outpll := strip_bitseq_false outpl in
let outplr := strip_bitseq_true outpl in
mtreeB (tx_mtree_trans_ inpll outpll Tl) (tx_mtree_trans_ inplr outplr Tr)
end
end
end.
Definition tx_mtree_trans (tx:Tx) (T:mtree 160) : mtree 160 :=
tx_mtree_trans_ (tx_inputs tx) (add_vout (hashtx tx) (tx_outputs tx) 0) T.
Lemma tx_mtree_trans_nochange_lem {n} :
forall T:mtree n,
tx_mtree_trans_ nil nil T = T.
destruct n as [|n].
- intros T. simpl. reflexivity.
- intros T. simpl. reflexivity.
Qed.
Fixpoint singlebranch_mtree (hl:nehlist) {n} : bitseq n -> mtree n :=
match n as n' return bitseq n' -> mtree n' with
| O => fun (alpha:bitseq 0) => nehlist_hlist hl
| S n => fun (alpha:bitseq (S n)) =>
match alpha with
| (false,alphar) => mtreeB (singlebranch_mtree hl alphar) (empty_mtree n)
| (true,alphar) => mtreeB (empty_mtree n) (singlebranch_mtree hl alphar)
end
end.
Lemma empty_mtree_supports_addr {n} (alpha:bitseq n) :
mtree_supports_addr (empty_mtree n) alpha.
destruct n as [|n]; simpl; tauto.
Qed.
Lemma singlebranch_mtree_supports_addr (hl:nehlist) {n} (gamma alpha:bitseq n) :
mtree_supports_addr (singlebranch_mtree hl gamma) alpha.
induction n as [|n IH].
- simpl. tauto.
- destruct gamma as [[|] gamma]; destruct alpha as [[|] alpha].
+ simpl. apply IH.
+ simpl. apply empty_mtree_supports_addr.
+ simpl. apply empty_mtree_supports_addr.
+ simpl. apply IH.
Qed.
Transparent mtree_supports_asset.
Lemma singlebranch_mtree_supports_asset_conv (a:asset) {n} (hl:nehlist) (gamma alpha:bitseq n) :
mtree_supports_asset a (singlebranch_mtree hl gamma) alpha ->
gamma = alpha /\ In_hlist a (nehlist_hlist hl).
intros H1. induction n as [|n IH].
- simpl in H1. destruct gamma as []. destruct alpha as [].
tauto.
- destruct gamma as [[|] gamma]; destruct alpha as [[|] alpha].
+ simpl. destruct (IH gamma alpha H1) as [IH1 IH2]. subst gamma. tauto.
+ exfalso. revert H1. simpl. apply empty_mtree_p_not_supports_asset.
apply empty_mtree_p_empty_mtree.
+ exfalso. revert H1. simpl. apply empty_mtree_p_not_supports_asset.
apply empty_mtree_p_empty_mtree.
+ simpl. destruct (IH gamma alpha H1) as [IH1 IH2]. subst gamma. tauto.
Qed.
Opaque mtree_supports_asset.
(*** Replaces "(None . None)" with "None" ***)
Fixpoint normalize_mtree {n} : mtree n -> mtree n :=
match n with
| O => fun hl:mtree 0 => hl
| S n => fun T:mtree (S n) =>
match T with
| inl h => inl h
| inr (Tl,Tr) =>
let Tl' := normalize_mtree Tl in
let Tr' := normalize_mtree Tr in
match mtree_hashroot Tl',mtree_hashroot Tr' with
| None,None => inl None
| _,_ => inr (Tl',Tr')
end
end
end.
(*** Rule out mtrees like "(None . None)" in favor of simply "None" ***)
Fixpoint mtree_normal_p {n} : mtree n -> Prop :=
match n with
| O => fun hl:mtree 0 => True
| S n => fun T:mtree (S n) =>
match T with