-
Notifications
You must be signed in to change notification settings - Fork 5
/
bitcoin_like-transactions.adb
1429 lines (1295 loc) · 64.8 KB
/
bitcoin_like-transactions.adb
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) 2019-2020 Dmitry Petukhov https://github.com/dgpv
--
-- This file is part of spark-bitcoin-transaction-example
--
-- It is subject to the license terms in the LICENSE file found in the top-level
-- directory of this distribution.
--
-- No part of spark-bitcoin-transaction-example, including this file, may be copied, modified,
-- propagated, or distributed except according to the terms contained in the
-- LICENSE file.
pragma SPARK_Mode(On);
package body Bitcoin_Like.Transactions is
function Is_Transaction_Input_Witnesses_Empty(Input_Witnesses: Transaction_Input_Witnesses_Array_Type) return Boolean is
begin
for Index in Input_Witnesses'Range loop
if not Is_Input_Witness_Empty(Input_Witnesses(Index)) then
return False;
end if;
pragma Loop_Invariant(
for all I in Input_Witnesses'First..Index
=> Is_Input_Witness_Empty(Input_Witnesses(I)));
end loop;
return True;
end Is_Transaction_Input_Witnesses_Empty;
procedure Inputs_Serialized_Cumulative_Data_Lengths_Eq_Lemma(
Inputs_Array_First : Transaction_Inputs_Array_Type;
Inputs_Array_Second : Transaction_Inputs_Array_Type
)
is
Serialized_Sums_First: Inputs_Serialized_Cumulative_Sums_Type :=
Inputs_Serialized_Cumulative_Data_Lengths(Inputs_Array_First);
Serialized_Sums_Second: Inputs_Serialized_Cumulative_Sums_Type :=
Inputs_Serialized_Cumulative_Data_Lengths(Inputs_Array_Second);
begin
pragma Assert (
Serialized_Sums_First'Length = Serialized_Sums_Second'Length + 1
or
Serialized_Sums_First'Length = Serialized_Sums_Second'Length
);
for Index in 0..Serialized_Sums_Second'Last loop
if Index = 0 then
pragma Assert (
Serialized_Sums_First(Index) = Inputs.Input_Serialized_Data_Length(Inputs_Array_First(Index))
);
pragma Assert (
Serialized_Sums_Second(Index) = Inputs.Input_Serialized_Data_Length(Inputs_Array_First(Index))
);
pragma Assert (Serialized_Sums_First(Index) = Serialized_Sums_Second(Index));
else
pragma Assert (Serialized_Sums_First(Index - 1) = Serialized_Sums_Second(Index - 1));
pragma Assert (
Serialized_Sums_First(Index) =
Inputs.Input_Serialized_Data_Length(Inputs_Array_First(Index))
+ Serialized_Sums_First(Index - 1)
);
pragma Assert (
Serialized_Sums_Second(Index) =
Inputs.Input_Serialized_Data_Length(Inputs_Array_First(Index))
+ Serialized_Sums_First(Index - 1)
);
pragma Assert (Serialized_Sums_First(Index) = Serialized_Sums_Second(Index));
end if;
pragma Loop_Invariant (
for all I in Serialized_Sums_First'First..Index =>
Serialized_Sums_First(I) = Serialized_Sums_Second(I)
);
end loop;
pragma Assert (
for all I in 0..Serialized_Sums_Second'Last =>
Inputs_Serialized_Cumulative_Data_Lengths(Inputs_Array_First)(I)
= Inputs_Serialized_Cumulative_Data_Lengths(Inputs_Array_Second)(I)
);
pragma Assert (
if Inputs_Array_First'Length = Inputs_Array_Second'Length
then Inputs_Serialized_Cumulative_Data_Lengths(Inputs_Array_First)(Inputs_Array_First'Last)
= Inputs_Serialized_Cumulative_Data_Lengths(Inputs_Array_Second)(Inputs_Array_Second'Last)
else Inputs_Serialized_Cumulative_Data_Lengths(Inputs_Array_First)(Inputs_Array_First'Last)
= Inputs.Input_Serialized_Data_Length(Inputs_Array_First(Inputs_Array_First'Last))
+ Inputs_Serialized_Cumulative_Data_Lengths(Inputs_Array_Second)(Inputs_Array_Second'Last)
);
end;
function Inputs_Serialized_Cumulative_Data_Lengths(Inputs_Array: Transaction_Inputs_Array_Type)
return Inputs_Serialized_Cumulative_Sums_Type
is
Cumulative_Sizes: Inputs_Serialized_Cumulative_Sums_Type (Inputs_Array'Range) := (others => 0);
Element_Size: Long_Natural;
begin
pragma Assert (Inputs_Array'First = 0);
for Index in 0..Inputs_Array'Last loop
Element_Size := Inputs.Input_Serialized_Data_Length(Inputs_Array(Index));
pragma Assert (Element_Size <= Long_Natural(Inputs.Input_Model_Max_Serialized_Data_Size));
Cumulative_Sizes(Index) :=
Element_Size
+ (
if Index = 0
then 0
else Cumulative_Sizes(Index - 1)
);
pragma Loop_Invariant(
Cumulative_Sizes(Index) <=
Long_Natural(Inputs.Input_Model_Max_Serialized_Data_Size)
* Long_Natural(Index + 1)
);
pragma Loop_Invariant(
for all I in 0..Index =>
Cumulative_Sizes(I) <=
Long_Natural(Inputs.Input_Model_Max_Serialized_Data_Size)
* Long_Natural(I + 1)
);
pragma Loop_Invariant(
for all I in 0..Index =>
Cumulative_Sizes(I) =
Inputs.Input_Serialized_Data_Length(Inputs_Array(I))
+ (
if I = 0
then 0
else Cumulative_Sizes(I - 1)
)
);
end loop;
pragma Assert (
if Inputs_Array'Length > 0
then Cumulative_Sizes(Inputs_Array'Last)
<= Long_Natural(Inputs.Input_Model_Max_Serialized_Data_Size) * Inputs_Array'Length
);
return Cumulative_Sizes;
end Inputs_Serialized_Cumulative_Data_Lengths;
procedure Outputs_Serialized_Cumulative_Data_Lengths_Eq_Lemma(
Outputs_Array_First : Transaction_Outputs_Array_Type;
Outputs_Array_Second : Transaction_Outputs_Array_Type
)
is
Serialized_Sums_First: Outputs_Serialized_Cumulative_Sums_Type :=
Outputs_Serialized_Cumulative_Data_Lengths(Outputs_Array_First);
Serialized_Sums_Second: Outputs_Serialized_Cumulative_Sums_Type :=
Outputs_Serialized_Cumulative_Data_Lengths(Outputs_Array_Second);
begin
pragma Assert (Serialized_Sums_First'Length = Serialized_Sums_Second'Length + 1);
for Index in 0..Serialized_Sums_Second'Last loop
if Index = 0 then
pragma Assert (
Serialized_Sums_First(Index) = Outputs.Output_Serialized_Data_Length(Outputs_Array_First(Index))
);
pragma Assert (
Serialized_Sums_Second(Index) = Outputs.Output_Serialized_Data_Length(Outputs_Array_First(Index))
);
pragma Assert (Serialized_Sums_First(Index) = Serialized_Sums_Second(Index));
else
pragma Assert (Serialized_Sums_First(Index - 1) = Serialized_Sums_Second(Index - 1));
pragma Assert (
Serialized_Sums_First(Index) =
Outputs.Output_Serialized_Data_Length(Outputs_Array_First(Index))
+ Serialized_Sums_First(Index - 1)
);
pragma Assert (
Serialized_Sums_Second(Index) =
Outputs.Output_Serialized_Data_Length(Outputs_Array_First(Index))
+ Serialized_Sums_First(Index - 1)
);
pragma Assert (Serialized_Sums_First(Index) = Serialized_Sums_Second(Index));
end if;
pragma Loop_Invariant (
for all I in Serialized_Sums_First'First..Index =>
Serialized_Sums_First(I) = Serialized_Sums_Second(I)
);
end loop;
pragma Assert (
for all I in 0..Serialized_Sums_Second'Last =>
Outputs_Serialized_Cumulative_Data_Lengths(Outputs_Array_First)(I)
= Outputs_Serialized_Cumulative_Data_Lengths(Outputs_Array_Second)(I)
);
pragma Assert (
Outputs_Serialized_Cumulative_Data_Lengths(Outputs_Array_First)(Outputs_Array_First'Last)
= Outputs.Output_Serialized_Data_Length(Outputs_Array_First(Outputs_Array_First'Last))
+ Outputs_Serialized_Cumulative_Data_Lengths(Outputs_Array_Second)(Outputs_Array_Second'Last)
);
end;
function Outputs_Serialized_Cumulative_Data_Lengths(Outputs_Array: Transaction_Outputs_Array_Type)
return Outputs_Serialized_Cumulative_Sums_Type
is
Cumulative_Sizes: Outputs_Serialized_Cumulative_Sums_Type (Outputs_Array'Range) := (others => 0);
Element_Size: Long_Natural;
begin
pragma Assert (Outputs_Array'First = 0);
for Index in 0..Outputs_Array'Last loop
Element_Size := Outputs.Output_Serialized_Data_Length(Outputs_Array(Index));
pragma Assert (Element_Size <= Long_Natural(Outputs.Output_Model_Max_Serialized_Data_Size));
Cumulative_Sizes(Index) :=
Element_Size
+ (
if Index = 0
then 0
else Cumulative_Sizes(Index - 1)
);
pragma Loop_Invariant(
Cumulative_Sizes(Index) <=
Long_Natural(Outputs.Output_Model_Max_Serialized_Data_Size)
* Long_Natural(Index + 1)
);
pragma Loop_Invariant(
for all I in 0..Index =>
Cumulative_Sizes(I) <=
Long_Natural(Outputs.Output_Model_Max_Serialized_Data_Size)
* Long_Natural(I + 1)
);
pragma Loop_Invariant(
for all I in 0..Index =>
Cumulative_Sizes(I) =
Outputs.Output_Serialized_Data_Length(Outputs_Array(I))
+ (
if I = 0
then 0
else Cumulative_Sizes(I - 1)
)
);
end loop;
pragma Assert (
if Outputs_Array'Length > 0
then Cumulative_Sizes(Outputs_Array'Last)
<= Long_Natural(Outputs.Output_Model_Max_Serialized_Data_Size) * Outputs_Array'Length
);
return Cumulative_Sizes;
end Outputs_Serialized_Cumulative_Data_Lengths;
procedure Input_Witnesses_Serialized_Cumulative_Data_Lengths_Eq_Lemma(
Input_Witnesses_Array_First : Transaction_Input_Witnesses_Array_Type;
Input_Witnesses_Array_Second : Transaction_Input_Witnesses_Array_Type
)
is
Serialized_Sums_First: Input_Witnesses_Serialized_Cumulative_Sums_Type :=
Input_Witnesses_Serialized_Cumulative_Data_Lengths(Input_Witnesses_Array_First);
Serialized_Sums_Second: Input_Witnesses_Serialized_Cumulative_Sums_Type :=
Input_Witnesses_Serialized_Cumulative_Data_Lengths(Input_Witnesses_Array_Second);
begin
pragma Assert (Serialized_Sums_First'Length = Serialized_Sums_Second'Length + 1);
for Index in 0..Serialized_Sums_Second'Last loop
if Index = 0 then
pragma Assert (
Input_Witnesses_Array_First(Index) = Input_Witnesses_Array_Second(Index)
);
Input_Witness_Stack_Serialized_Cumulative_Data_Lengths_Eq_Lemma(
Input_Witness_Stack_Items(Input_Witnesses_Array_First(Index).Stack),
Input_Witness_Stack_Items(Input_Witnesses_Array_Second(Index).Stack)
);
pragma Assert (
Input_Witnesses.Input_Witness_Serialized_Data_Length(Input_Witnesses_Array_First(0))
= Input_Witnesses.Input_Witness_Serialized_Data_Length(Input_Witnesses_Array_Second(0))
);
pragma Assert (
Serialized_Sums_First(Index) = Input_Witnesses.Input_Witness_Serialized_Data_Length(Input_Witnesses_Array_First(Index))
);
pragma Assert (
Serialized_Sums_Second(Index) = Input_Witnesses.Input_Witness_Serialized_Data_Length(Input_Witnesses_Array_First(Index))
);
pragma Assert (Serialized_Sums_First(Index) = Serialized_Sums_Second(Index));
else
pragma Assert (Serialized_Sums_First(Index - 1) = Serialized_Sums_Second(Index - 1));
Input_Witness_Stack_Serialized_Cumulative_Data_Lengths_Eq_Lemma(
Input_Witness_Stack_Items(Input_Witnesses_Array_First(Index).Stack),
Input_Witness_Stack_Items(Input_Witnesses_Array_Second(Index).Stack)
);
pragma Assert (
Serialized_Sums_First(Index) =
Input_Witnesses.Input_Witness_Serialized_Data_Length(Input_Witnesses_Array_First(Index))
+ Serialized_Sums_First(Index - 1)
);
pragma Assert (
Serialized_Sums_Second(Index) =
Input_Witnesses.Input_Witness_Serialized_Data_Length(Input_Witnesses_Array_First(Index))
+ Serialized_Sums_First(Index - 1)
);
pragma Assert (Serialized_Sums_First(Index) = Serialized_Sums_Second(Index));
end if;
pragma Loop_Invariant (
for all I in Serialized_Sums_First'First..Index =>
Serialized_Sums_First(I) = Serialized_Sums_Second(I)
);
end loop;
pragma Assert (
for all I in 0..Serialized_Sums_Second'Last =>
Input_Witnesses_Serialized_Cumulative_Data_Lengths(Input_Witnesses_Array_First)(I)
= Input_Witnesses_Serialized_Cumulative_Data_Lengths(Input_Witnesses_Array_Second)(I)
);
pragma Assert (
Input_Witnesses_Serialized_Cumulative_Data_Lengths(Input_Witnesses_Array_First)(Input_Witnesses_Array_First'Last)
= Input_Witnesses.Input_Witness_Serialized_Data_Length(Input_Witnesses_Array_First(Input_Witnesses_Array_First'Last))
+ Input_Witnesses_Serialized_Cumulative_Data_Lengths(Input_Witnesses_Array_Second)(Input_Witnesses_Array_Second'Last)
);
end;
function Input_Witnesses_Serialized_Cumulative_Data_Lengths(Input_Witnesses_Array: Transaction_Input_Witnesses_Array_Type)
return Input_Witnesses_Serialized_Cumulative_Sums_Type
is
Cumulative_Sizes: Input_Witnesses_Serialized_Cumulative_Sums_Type (Input_Witnesses_Array'Range) := (others => 0);
Element_Size: Long_Natural;
begin
pragma Assert (Input_Witnesses_Array'First = 0);
for Index in 0..Input_Witnesses_Array'Last loop
Element_Size := Input_Witnesses.Input_Witness_Serialized_Data_Length(Input_Witnesses_Array(Index));
pragma Assert (Element_Size <= Long_Natural(Input_Witnesses.Input_Witness_Model_Max_Serialized_Data_Size));
Cumulative_Sizes(Index) :=
Element_Size
+ (
if Index = 0
then 0
else Cumulative_Sizes(Index - 1)
);
pragma Loop_Invariant(
Cumulative_Sizes(Index) <=
Long_Natural(Input_Witnesses.Input_Witness_Model_Max_Serialized_Data_Size)
* Long_Natural(Index + 1)
);
pragma Loop_Invariant(
for all I in 0..Index =>
Cumulative_Sizes(I) <=
Long_Natural(Input_Witnesses.Input_Witness_Model_Max_Serialized_Data_Size)
* Long_Natural(I + 1)
);
pragma Loop_Invariant(
for all I in 0..Index =>
Cumulative_Sizes(I) =
Input_Witnesses.Input_Witness_Serialized_Data_Length(Input_Witnesses_Array(I))
+ (
if I = 0
then 0
else Cumulative_Sizes(I - 1)
)
);
end loop;
pragma Assert (
if Input_Witnesses_Array'Length > 0
then Cumulative_Sizes(Input_Witnesses_Array'Last)
<= Long_Natural(Input_Witnesses.Input_Witness_Model_Max_Serialized_Data_Size) * Input_Witnesses_Array'Length
);
return Cumulative_Sizes;
end Input_Witnesses_Serialized_Cumulative_Data_Lengths;
procedure Deserialize_Inputs(Transaction: in out Transaction_Type; Inputs_Count: in Input_Array_Length_Type)
is
subtype Serialized_Length_Cumulative_Sums_Type is Inputs_Serialized_Cumulative_Sums_Type (Input_Index_Type);
Bytes_Processed_at_Loop_Start : Long_Natural with Ghost;
Bytes_Processed_before_Loop : Long_Natural := Long_Natural(Bytes_Processed) with Ghost;
Bytes_Processed_within_Loop : Long_Natural := 0 with Ghost;
Item_Serialized_Length : Long_Natural with Ghost;
Serialized_Cumulative_Data_Lengths : Serialized_Length_Cumulative_Sums_Type := (others => 0) with Ghost;
Transaction_Prev : Transaction_Type := Transaction with Ghost;
Saved_Data_Tag : Model_Tag_Type := Current_Data_Tag;
begin
Transaction.Inputs_Count := 0;
if Inputs_Count = 0 then
return;
end if;
for Index in 0..Integer(Inputs_Count)-1 loop
Bytes_Processed_at_Loop_Start := Long_Natural(Bytes_Processed);
Inputs.Deserialize(Transaction.Inputs(Index));
if Got_Error then
return; -- caller will check the error and set transaction to empty
end if;
Transaction.Inputs_Count := Index + 1;
pragma Assert (Transaction_Inputs(Transaction)'Length = Index + 1);
pragma Assert (Transaction_Inputs(Transaction)'Last = Index);
pragma Assert (
Transaction.Inputs = Transaction_Prev.Inputs'Update(
Index => Transaction.Inputs(Index)
)
);
Item_Serialized_Length := Long_Natural(Bytes_Processed) - Bytes_Processed_at_Loop_Start;
pragma Assert (Item_Serialized_Length > 0);
Bytes_Processed_within_Loop := Bytes_Processed_within_Loop + Item_Serialized_Length;
pragma Assert (Bytes_Processed_within_Loop > 0);
pragma Assert (Item_Serialized_Length <= Long_Natural(Inputs.Input_Model_Max_Serialized_Data_Size));
pragma Assert (
Inputs.Input_Serialized_Data_Length(Transaction.Inputs(Index))
= Item_Serialized_Length
);
Serialized_Cumulative_Data_Lengths(Index) :=
Item_Serialized_Length + (
if Index = 0
then 0
else Serialized_Cumulative_Data_Lengths(Index - 1)
);
if Index = 0 then
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Inputs.Input_Serialized_Data_Length(Transaction_Inputs(Transaction)(Index))
);
pragma Assert (
Inputs_Serialized_Cumulative_Data_Lengths(Transaction_Inputs(Transaction))(Index)
= Inputs.Input_Serialized_Data_Length(Transaction_Inputs(Transaction)(Index))
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Inputs_Serialized_Cumulative_Data_Lengths(Transaction.Inputs(0..Index))(Index)
);
else
Inputs_Serialized_Cumulative_Data_Lengths_Eq_Lemma(
Transaction.Inputs(0..Index), Transaction_Prev.Inputs(0..Index-1)
);
pragma Assert (
for all I in 0..Index-1 =>
Inputs_Serialized_Cumulative_Data_Lengths(Transaction.Inputs(0..Index))(I)
= Inputs_Serialized_Cumulative_Data_Lengths(Transaction_Prev.Inputs(0..Index-1))(I)
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index - 1)
= Inputs_Serialized_Cumulative_Data_Lengths(Transaction.Inputs(0..Index))(Index - 1)
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Inputs.Input_Serialized_Data_Length(Transaction.Inputs(Index))
+ Serialized_Cumulative_Data_Lengths(Index - 1)
);
pragma Assert (
Inputs_Serialized_Cumulative_Data_Lengths(Transaction.Inputs(0..Index))(Index)
= Inputs.Input_Serialized_Data_Length(Transaction.Inputs(Index))
+ Inputs_Serialized_Cumulative_Data_Lengths(Transaction.Inputs(0..Index))(Index - 1)
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Inputs_Serialized_Cumulative_Data_Lengths(Transaction.Inputs(0..Index))(Index)
);
end if;
pragma Loop_Invariant (
Serialized_Cumulative_Data_Lengths(Index)
= Inputs_Serialized_Cumulative_Data_Lengths(Transaction.Inputs(0..Index))(Index)
);
pragma Loop_Invariant (
for all I in 0..Index =>
Serialized_Cumulative_Data_Lengths(I)
= Inputs_Serialized_Cumulative_Data_Lengths(Transaction.Inputs(0..Index))(I)
);
pragma Loop_Invariant (Status_OK);
pragma Loop_Invariant (Bytes_Processed_within_Loop > 0);
pragma Loop_Invariant (Transaction_Inputs(Transaction)'Length = Index + 1);
pragma Loop_Invariant(
Serialized_Cumulative_Data_Lengths(Index) <=
Long_Natural(Inputs.Input_Model_Max_Serialized_Data_Size)
* Long_Natural(Index + 1)
);
pragma Loop_Invariant(
for all I in 0..Index =>
Serialized_Cumulative_Data_Lengths(I) <=
Long_Natural(Inputs.Input_Model_Max_Serialized_Data_Size)
* Long_Natural(I + 1)
);
pragma Loop_Invariant (
if Index = 0 then
Bytes_Processed_within_Loop = Item_Serialized_Length
else
Bytes_Processed_within_Loop =
Item_Serialized_Length + Serialized_Cumulative_Data_Lengths(Index - 1)
);
pragma Loop_Invariant (
Bytes_Processed_within_Loop = Serialized_Cumulative_Data_Lengths(Index)
);
pragma Loop_Invariant(
Bytes_Processed_within_Loop
= Long_Natural(Bytes_Processed) - Bytes_Processed_before_Loop
);
Transaction_Prev := Transaction;
end loop;
pragma Assert (Bytes_Processed_within_Loop > 0);
pragma Assert (Bytes_Processed_within_Loop = Long_Natural(Bytes_Processed) - Bytes_Processed_before_Loop);
pragma Assert (Transaction_Inputs(Transaction)'Last = Inputs_Count - 1);
pragma Assert (Transaction_Inputs(Transaction)'Length = Inputs_Count);
pragma Assert (
Inputs_Serialized_Cumulative_Data_Lengths(
Transaction_Inputs(Transaction)
)(Transaction_Inputs(Transaction)'Last)
= Serialized_Cumulative_Data_Lengths(Transaction_Inputs(Transaction)'Last)
);
pragma Assert (Inputs_Serialized_Data_Length(Transaction_Inputs(Transaction)) = Bytes_Processed_within_Loop);
Data_Checkpointing.Force_Set_Data_Tag(Saved_Data_Tag, Data_Tag_State);
pragma Assert_And_Cut (
Status_OK
and then
Transaction_Inputs_Count(Transaction) = Inputs_Count
and then
Current_Data_Tag = Saved_Data_Tag
and then
Inputs_Serialized_Data_Length(Transaction_Inputs(Transaction))
= Long_Natural(Bytes_Processed) - Bytes_Processed_before_Loop
and then
Inputs_Serialized_Data_Length(Transaction_Inputs(Transaction))
<= Long_Natural(Inputs.Input_Model_Max_Serialized_Data_Size) * Long_Natural(Inputs_Count)
);
end Deserialize_Inputs;
procedure Deserialize_Outputs(Transaction: in out Transaction_Type; Outputs_Count: in Output_Array_Length_Type)
is
subtype Serialized_Length_Cumulative_Sums_Type is Outputs_Serialized_Cumulative_Sums_Type (Output_Index_Type);
Bytes_Processed_at_Loop_Start : Long_Natural with Ghost;
Bytes_Processed_within_Loop : Long_Natural := 0 with Ghost;
Item_Serialized_Length : Long_Natural with Ghost;
Serialized_Cumulative_Data_Lengths : Serialized_Length_Cumulative_Sums_Type := (others => 0) with Ghost;
Transaction_Prev : Transaction_Type := Transaction with Ghost;
Transaction_Old : Transaction_Type := Transaction with Ghost;
Raw_Data_Status_Old : Raw_Data.Status_Type := Raw_Data_Status with Ghost;
Saved_Data_Tag : Model_Tag_Type := Current_Data_Tag;
begin
Transaction.Outputs_Count := 0;
if Outputs_Count = 0 then
return;
end if;
for Index in 0..Integer(Outputs_Count)-1 loop
Bytes_Processed_at_Loop_Start := Long_Natural(Bytes_Processed);
Outputs.Deserialize(Transaction.Outputs(Index));
if Got_Error then
return; -- caller will check the error and set transaction to empty
end if;
Transaction.Outputs_Count := Index + 1;
pragma Assert (Transaction_Outputs(Transaction)'Length = Index + 1);
pragma Assert (Transaction_Outputs(Transaction)'Last = Index);
pragma Assert (
Transaction = Transaction_Old'Update(
Outputs_Count => Index + 1,
Outputs => Transaction_Prev.Outputs'Update(
Index => Transaction.Outputs(Index)
)
)
);
Item_Serialized_Length := Long_Natural(Bytes_Processed) - Bytes_Processed_at_Loop_Start;
pragma Assert (Item_Serialized_Length > 0);
Bytes_Processed_within_Loop := Bytes_Processed_within_Loop + Item_Serialized_Length;
pragma Assert (Bytes_Processed_within_Loop > 0);
pragma Assert (Item_Serialized_Length <= Long_Natural(Outputs.Output_Model_Max_Serialized_Data_Size));
pragma Assert (
Outputs.Output_Serialized_Data_Length(Transaction.Outputs(Index))
= Item_Serialized_Length
);
Serialized_Cumulative_Data_Lengths(Index) :=
Item_Serialized_Length + (
if Index = 0
then 0
else Serialized_Cumulative_Data_Lengths(Index - 1)
);
if Index = 0 then
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Outputs.Output_Serialized_Data_Length(Transaction_Outputs(Transaction)(Index))
);
pragma Assert (
Outputs_Serialized_Cumulative_Data_Lengths(Transaction_Outputs(Transaction))(Index)
= Outputs.Output_Serialized_Data_Length(Transaction_Outputs(Transaction)(Index))
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Outputs_Serialized_Cumulative_Data_Lengths(Transaction.Outputs(0..Index))(Index)
);
else
Outputs_Serialized_Cumulative_Data_Lengths_Eq_Lemma(
Transaction.Outputs(0..Index), Transaction_Prev.Outputs(0..Index-1)
);
pragma Assert (
for all I in 0..Index-1 =>
Outputs_Serialized_Cumulative_Data_Lengths(Transaction.Outputs(0..Index))(I)
= Outputs_Serialized_Cumulative_Data_Lengths(Transaction_Prev.Outputs(0..Index-1))(I)
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index - 1)
= Outputs_Serialized_Cumulative_Data_Lengths(Transaction.Outputs(0..Index))(Index - 1)
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Outputs.Output_Serialized_Data_Length(Transaction.Outputs(Index))
+ Serialized_Cumulative_Data_Lengths(Index - 1)
);
pragma Assert (
Outputs_Serialized_Cumulative_Data_Lengths(Transaction.Outputs(0..Index))(Index)
= Outputs.Output_Serialized_Data_Length(Transaction.Outputs(Index))
+ Outputs_Serialized_Cumulative_Data_Lengths(Transaction.Outputs(0..Index))(Index - 1)
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Outputs_Serialized_Cumulative_Data_Lengths(Transaction.Outputs(0..Index))(Index)
);
end if;
pragma Loop_Invariant (
Serialized_Cumulative_Data_Lengths(Index)
= Outputs_Serialized_Cumulative_Data_Lengths(Transaction.Outputs(0..Index))(Index)
);
pragma Loop_Invariant (
for all I in 0..Index =>
Serialized_Cumulative_Data_Lengths(I)
= Outputs_Serialized_Cumulative_Data_Lengths(Transaction.Outputs(0..Index))(I)
);
pragma Loop_Invariant (Status_OK);
pragma Loop_Invariant (Bytes_Processed_within_Loop > 0);
pragma Loop_Invariant (Transaction_Outputs(Transaction)'Last = Index);
pragma Loop_Invariant(
Serialized_Cumulative_Data_Lengths(Index) <=
Long_Natural(Outputs.Output_Model_Max_Serialized_Data_Size)
* Long_Natural(Index + 1)
);
pragma Loop_Invariant(
for all I in 0..Index =>
Serialized_Cumulative_Data_Lengths(I) <=
Long_Natural(Outputs.Output_Model_Max_Serialized_Data_Size)
* Long_Natural(I + 1)
);
pragma Loop_Invariant (
if Index = 0 then
Bytes_Processed_within_Loop = Item_Serialized_Length
else
Bytes_Processed_within_Loop =
Item_Serialized_Length + Serialized_Cumulative_Data_Lengths(Index - 1)
);
pragma Loop_Invariant (
Bytes_Processed_within_Loop = Serialized_Cumulative_Data_Lengths(Index)
);
pragma Loop_Invariant(
Bytes_Processed_within_Loop = Long_Natural(Bytes_Processed_Since(Raw_Data_Status_Old))
);
pragma Loop_Invariant (
Transaction = Transaction_Old'Update(
Outputs_Count => Index + 1,
Outputs => Transaction_Prev.Outputs'Update(
Index => Transaction.Outputs(Index)
)
)
);
pragma Loop_Invariant (
Transaction_Version(Transaction) = Transaction_Version(Transaction_Old)
and then
Transaction_Inputs_Count(Transaction) = Transaction_Inputs_Count(Transaction_Old)
and then
Transaction_Inputs(Transaction) = Transaction_Inputs(Transaction_Old)
);
Transaction_Prev := Transaction;
end loop;
pragma Assert (Bytes_Processed_within_Loop > 0);
pragma Assert (Bytes_Processed_within_Loop = Long_Natural(Bytes_Processed_Since(Raw_Data_Status_Old)));
pragma Assert (Transaction.Outputs_Count > 0);
pragma Assert (Transaction_Outputs(Transaction)'Last = Outputs_Count - 1);
pragma Assert (Transaction_Outputs(Transaction)'Length = Outputs_Count);
pragma Assert (Serialized_Cumulative_Data_Lengths(Outputs_Count-1) = Bytes_Processed_within_Loop);
pragma Assert (
Outputs_Serialized_Cumulative_Data_Lengths(
Transaction.Outputs(0..Outputs_Count-1)
)(Outputs_Count-1)
= Serialized_Cumulative_Data_Lengths(Outputs_Count-1)
);
pragma Assert (
Outputs_Serialized_Cumulative_Data_Lengths(
Transaction.Outputs(0..Outputs_Count-1)
)(Outputs_Count-1)
= Outputs_Serialized_Data_Length(Transaction.Outputs(0..Outputs_Count-1))
);
Data_Checkpointing.Force_Set_Data_Tag(Saved_Data_Tag, Data_Tag_State);
pragma Assert_And_Cut (
Status_OK
and then
Transaction_Outputs_Count(Transaction) = Outputs_Count
and then
Current_Data_Tag = Saved_Data_Tag
and then
Outputs_Serialized_Data_Length(Transaction_Outputs(Transaction))
<= Long_Natural(Outputs.Output_Model_Max_Serialized_Data_Size) * Long_Natural(Outputs_Count)
and then
Outputs_Serialized_Data_Length(Transaction_Outputs(Transaction))
= Long_Natural(Bytes_Processed_Since(Raw_Data_Status_Old))
and then
Transaction_Version(Transaction) = Transaction_Version(Transaction_Old)
and then
Transaction_Inputs_Count(Transaction) = Transaction_Inputs_Count(Transaction_Old)
and then
Transaction_Inputs(Transaction) = Transaction_Inputs(Transaction_Old)
);
end Deserialize_Outputs;
procedure Deserialize_Input_Witnesses(Transaction: in out Transaction_Type)
is
subtype Serialized_Length_Cumulative_Sums_Type is Input_Witnesses_Serialized_Cumulative_Sums_Type (Input_Index_Type);
Bytes_Processed_at_Loop_Start : Long_Natural with Ghost;
Bytes_Processed_before_Loop : Long_Natural := Long_Natural(Bytes_Processed) with Ghost;
Bytes_Processed_within_Loop : Long_Natural := 0 with Ghost;
Item_Serialized_Length : Long_Natural with Ghost;
Serialized_Cumulative_Data_Lengths : Serialized_Length_Cumulative_Sums_Type := (others => 0) with Ghost;
Transaction_Prev : Transaction_Type := Transaction with Ghost;
Transaction_Old : Transaction_Type := Transaction with Ghost;
Saved_Data_Tag : Model_Tag_Type := Current_Data_Tag;
Inputs_Count : Input_Array_Length_Type := Transaction_Inputs_Count(Transaction);
begin
Transaction.Input_Witnesses_Count := 0;
if Inputs_Count = 0 then
return;
end if;
for Index in 0..Integer(Inputs_Count)-1 loop
Bytes_Processed_at_Loop_Start := Long_Natural(Bytes_Processed);
Input_Witnesses.Deserialize(Transaction.Input_Witnesses(Index));
if Got_Error then
return; -- caller will check the error and set transaction to empty
end if;
Transaction.Input_Witnesses_Count := Index + 1;
pragma Assert (Transaction_Input_Witnesses(Transaction)'Length = Index + 1);
pragma Assert (Transaction_Input_Witnesses(Transaction)'Last = Index);
pragma Assert (
Transaction.Input_Witnesses = Transaction_Prev.Input_Witnesses'Update(
Index => Transaction.Input_Witnesses(Index)
)
);
Item_Serialized_Length := Long_Natural(Bytes_Processed) - Bytes_Processed_at_Loop_Start;
pragma Assert (Item_Serialized_Length > 0);
Bytes_Processed_within_Loop := Bytes_Processed_within_Loop + Item_Serialized_Length;
pragma Assert (Bytes_Processed_within_Loop > 0);
pragma Assert (Item_Serialized_Length <= Long_Natural(Input_Witnesses.Input_Witness_Model_Max_Serialized_Data_Size));
pragma Assert (
Input_Witnesses.Input_Witness_Serialized_Data_Length(Transaction.Input_Witnesses(Index))
= Item_Serialized_Length
);
Serialized_Cumulative_Data_Lengths(Index) :=
Item_Serialized_Length + (
if Index = 0
then 0
else Serialized_Cumulative_Data_Lengths(Index - 1)
);
if Index = 0 then
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Input_Witnesses.Input_Witness_Serialized_Data_Length(Transaction_Input_Witnesses(Transaction)(Index))
);
pragma Assert (
Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction_Input_Witnesses(Transaction))(Index)
= Input_Witnesses.Input_Witness_Serialized_Data_Length(Transaction_Input_Witnesses(Transaction)(Index))
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction.Input_Witnesses(0..Index))(Index)
);
else
Input_Witnesses_Serialized_Cumulative_Data_Lengths_Eq_Lemma(
Transaction.Input_Witnesses(0..Index), Transaction_Prev.Input_Witnesses(0..Index-1)
);
pragma Assert (
for all I in 0..Index-1 =>
Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction.Input_Witnesses(0..Index))(I)
= Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction_Prev.Input_Witnesses(0..Index-1))(I)
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index - 1)
= Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction.Input_Witnesses(0..Index))(Index - 1)
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Input_Witnesses.Input_Witness_Serialized_Data_Length(Transaction.Input_Witnesses(Index))
+ Serialized_Cumulative_Data_Lengths(Index - 1)
);
pragma Assert (
Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction.Input_Witnesses(0..Index))(Index)
= Input_Witnesses.Input_Witness_Serialized_Data_Length(Transaction.Input_Witnesses(Index))
+ Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction.Input_Witnesses(0..Index))(Index - 1)
);
pragma Assert (
Serialized_Cumulative_Data_Lengths(Index)
= Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction.Input_Witnesses(0..Index))(Index)
);
end if;
pragma Loop_Invariant (
Serialized_Cumulative_Data_Lengths(Index)
= Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction.Input_Witnesses(0..Index))(Index)
);
pragma Loop_Invariant (
for all I in 0..Index =>
Serialized_Cumulative_Data_Lengths(I)
= Input_Witnesses_Serialized_Cumulative_Data_Lengths(Transaction.Input_Witnesses(0..Index))(I)
);
pragma Loop_Invariant (Status_OK);
pragma Loop_Invariant (Bytes_Processed_within_Loop > 0);
pragma Loop_Invariant (Transaction_Input_Witnesses(Transaction)'Length = Index + 1);
pragma Loop_Invariant(
Serialized_Cumulative_Data_Lengths(Index) <=
Long_Natural(Input_Witnesses.Input_Witness_Model_Max_Serialized_Data_Size)
* Long_Natural(Index + 1)
);
pragma Loop_Invariant(
for all I in 0..Index =>
Serialized_Cumulative_Data_Lengths(I) <=
Long_Natural(Input_Witnesses.Input_Witness_Model_Max_Serialized_Data_Size)
* Long_Natural(I + 1)
);
pragma Loop_Invariant (
if Index = 0 then
Bytes_Processed_within_Loop = Item_Serialized_Length
else
Bytes_Processed_within_Loop =
Item_Serialized_Length + Serialized_Cumulative_Data_Lengths(Index - 1)
);
pragma Loop_Invariant (
Bytes_Processed_within_Loop = Serialized_Cumulative_Data_Lengths(Index)
);
pragma Loop_Invariant(
Bytes_Processed_within_Loop
= Long_Natural(Bytes_Processed) - Bytes_Processed_before_Loop
);
Transaction_Prev := Transaction;
end loop;
pragma Assert (Bytes_Processed_within_Loop > 0);
pragma Assert (Bytes_Processed_within_Loop = Long_Natural(Bytes_Processed) - Bytes_Processed_before_Loop);
pragma Assert (Transaction_Input_Witnesses(Transaction)'Last = Inputs_Count - 1);
pragma Assert (Transaction_Input_Witnesses(Transaction)'Length = Inputs_Count);
pragma Assert (
Input_Witnesses_Serialized_Cumulative_Data_Lengths(
Transaction_Input_Witnesses(Transaction)
)(Transaction_Input_Witnesses(Transaction)'Last)
= Serialized_Cumulative_Data_Lengths(Transaction_Input_Witnesses(Transaction)'Last)
);
pragma Assert (
Input_Witnesses_Serialized_Data_Length(Transaction_Input_Witnesses(Transaction))
= Bytes_Processed_within_Loop
);
Data_Checkpointing.Force_Set_Data_Tag(Saved_Data_Tag, Data_Tag_State);
pragma Assert_And_Cut (
Status_OK
and then
Transaction_Inputs_Count(Transaction) = Transaction_Inputs_Count(Transaction_Old)
and then
Transaction_Outputs_Count(Transaction) = Transaction_Outputs_Count(Transaction_Old)
and then
Transaction_Input_Witnesses(Transaction)'Length = Transaction_Inputs_Count(Transaction)
and then
Current_Data_Tag = Saved_Data_Tag
and then
Input_Witnesses_Serialized_Data_Length(Transaction_Input_Witnesses(Transaction))
<= Long_Natural(Input_Witnesses.Input_Witness_Model_Max_Serialized_Data_Size)
* Long_Natural(Inputs_Count)
and then
Input_Witnesses_Serialized_Data_Length(Transaction_Input_Witnesses(Transaction))
= Long_Natural(Bytes_Processed) - Bytes_Processed_before_Loop
and then
Inputs_Serialized_Data_Length(Transaction_Inputs(Transaction))
= Inputs_Serialized_Data_Length(Transaction_Inputs(Transaction_Old))
and then
Outputs_Serialized_Data_Length(Transaction_Outputs(Transaction))
= Outputs_Serialized_Data_Length(Transaction_Outputs(Transaction_Old))
);
end Deserialize_Input_Witnesses;
procedure Deserialize(Transaction: out Transaction_Type)
is
use Data_Checkpoints;
Inputs_Count : Compact_Size_Type;
Outputs_Count : Compact_Size_Type;
U32_Value : Unsigned_Ranged_Int32;
Flag_Byte : Byte_Type;
Has_Witnesses : Boolean := False;
Raw_Data_Status_Old : Raw_Data.Status_Type := Raw_Data_Status with Ghost;
Transaction_Prev : Transaction_Type with Ghost;
Transaction_Inputs_Prev : Transaction_Inputs_Bounded_Array_Type with Ghost;
begin
-- Before we get 'Initialized attribute for arrays/records, we have to initialize
-- beforehand, as we might not fill all the items, and if we fill the itmes in the loop,
-- flow analysis currently cannot detect that all the items were actually initialized,
-- unfortunately. We will reset Transaction to empty on all errors, though, anyway.
Transaction := Empty_Transaction;
Data_Checkpoint(Tag_Version);
Data_Readers.Read_Unsigned_32(U32_Value);
if Got_Error then
Transaction := Empty_Transaction;
return;
end if;
if (
U32_Value not in Unsigned_Ranged_Int32(Version_Type'First)..Unsigned_Ranged_Int32(Version_Type'Last)
) then
Transaction := Empty_Transaction;
Register_Structural_Error(
"Transaction Version " & Unsigned_Ranged_Int32'Image(U32_Value) & " is not supported"
);
return;
end if;
Transaction.Version := Version_Type(U32_Value);
Data_Checkpoint(Tag_Witness_Marker);
pragma Assert_And_Cut(
Status_OK
and then
Current_Data_Tag = Tag_Witness_Marker
and then
Data_Checkpoints.Partial_State_Consistent(Bytes_Processed_Since(Raw_Data_Status_Old), Tag_Version)
and then (Bytes_Processed = Bytes_Processed_With(Raw_Data_Status_Old) + 4)
and then Data_Checkpoints.Checkpoint_States.Bytes_Processed_Prev(Data_Checkpoints.State) = Bytes_Processed
);
Data_Readers.Read_Compact_Size(Inputs_Count);
if Got_Error then
Transaction := Empty_Transaction;
return;
end if;
if Inputs_Count = 0 then
Data_Readers.Read_Byte(Flag_Byte);
if Got_Error then
Transaction := Empty_Transaction;
return;
end if;
if Flag_Byte /= 1 then
Transaction := Empty_Transaction;
Register_Structural_Error(
"Unexpected flag byte (" & Byte_Type'Image(Flag_Byte) & ") after segwit marker, "
& "expected 1"
);
return;
end if;
Has_Witnesses := True;
Data_Checkpoint(Tag_Inputs_Count);
pragma Assert (Data_Checkpoints.Element_Size_Match(Tag_Witness_Marker, 2));
Data_Readers.Read_Compact_Size(Inputs_Count);
if Got_Error then
Transaction := Empty_Transaction;
return;
end if;
if Inputs_Count = 0 then
Register_Structural_Error("Transaction has no inputs");
return;