-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.adb
146 lines (132 loc) · 5.69 KB
/
utils.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
-- 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 Utils is
package body Array_Summation is
function Cumulative_Sums(State: State_Type) return Cumulative_Sums_Type
is
Cumulative_Sizes: Cumulative_Sums_Type := (others => 0);
begin
for Index in Array_Index_Type'Range loop
Cumulative_Sizes(Index) :=
Element_Size_Function(Index, State)
+ (
if Index = Array_Index_Type'First
then 0
else Cumulative_Sizes(Array_Index_Type'Pred(Index))
);
pragma Loop_Invariant(
Cumulative_Sizes(Index) <=
Long_Natural(
Element_Size_Max
* (Array_Index_Type'Pos(Index) - Array_Index_Type'Pos(Array_Index_Type'First) + 1)
)
);
pragma Loop_Invariant(
for all I in Array_Index_Type'First..Index =>
Cumulative_Sizes(I) <=
Long_Natural(
Element_Size_Max
* (Array_Index_Type'Pos(I) - Array_Index_Type'Pos(Array_Index_Type'First) + 1)
)
);
pragma Loop_Invariant(
for all I in Array_Index_Type'First..Index =>
Cumulative_Sizes(I) =
Element_Size_Function(I, State)
+ (
if I = Array_Index_Type'First
then 0
else Cumulative_Sizes(Array_Index_Type'Pred(I))
)
);
end loop;
-- pragma Assert (
-- Cumulative_Sizes(Array_Index_Type'Last)
-- <= Long_Natural(
-- Element_Size_Max * (
-- Array_Index_Type'Pos(Array_Index_Type'Last)
-- - Array_Index_Type'Pos(Array_Index_Type'First) + 1
-- )
-- )
-- );
return Cumulative_Sizes;
end;
end Array_Summation;
package body Serialized_Data_Summation is
function Cumulative_Sums(Arr: Array_Type) return Cumulative_Sums_Type
is
Cumulative_Sizes: Cumulative_Sums_Type (Arr'Range) := (others => 0);
Element_Size: Long_Natural;
begin
for Index in Arr'Range loop
Element_Size := Element_Size_Function(Arr(Index));
pragma Assert (Element_Size <= Long_Natural(Element_Size_Max));
Cumulative_Sizes(Index) :=
Element_Size
+ (
if Index = Arr'First
then 0
else Cumulative_Sizes(Array_Index_Type'Pred(Index))
);
pragma Loop_Invariant(
Cumulative_Sizes(Index) <=
Long_Natural(Element_Size_Max)
* (Array_Index_Type'Pos(Index) - Array_Index_Type'Pos(Arr'First) + 1)
);
pragma Loop_Invariant(
for all I in Arr'First..Index =>
Cumulative_Sizes(I) <=
Long_Natural(Element_Size_Max)
* (Array_Index_Type'Pos(I) - Array_Index_Type'Pos(Arr'First) + 1)
);
pragma Loop_Invariant(
for all I in Arr'First..Index =>
Cumulative_Sizes(I) =
Element_Size_Function(Arr(I))
+ (
if I = Arr'First
then 0
else Cumulative_Sizes(Array_Index_Type'Pred(I))
)
);
end loop;
pragma Assert (Cumulative_Sizes(Arr'Last) <= Long_Natural(Element_Size_Max) * Arr'Length);
return Cumulative_Sizes;
end;
end Serialized_Data_Summation;
procedure Byte_Array_To_Hex(Bytes: in Byte_Array_Type; Output_String: out String)
is
begin
-- need to show to the prover that it is initialized, until we have appropriate
-- tools to track 'Initialized properies of the values
Output_String := (others => ' ');
for Index in 0..Bytes'Length-1 loop
Output_String(Output_String'First+Index*2..Output_String'First + Index*2+1) :=
Byte_To_Hex(Bytes(Bytes'First + Index));
pragma Loop_Invariant((Output_String'First + Index*2) <= Output_String'Last);
end loop;
end Byte_Array_To_Hex;
procedure Byte_Array_To_Hex_Reverse(Bytes: in Byte_Array_Type; Output_String: out String)
is
Reverse_Index: Natural;
begin
-- need to show to the prover that it is initialized, until we have appropriate
-- tools to track 'Initialized properies of the values
Output_String := (others => ' ');
for Index in 0..Bytes'Length-1 loop
Reverse_Index := (Bytes'Length - 1) - Index;
Output_String(Output_String'First+Index*2..Output_String'First + Index*2+1) :=
Byte_To_Hex(Bytes(Bytes'First + Reverse_Index));
pragma Loop_Invariant((Output_String'First + Index*2) <= Output_String'Last);
end loop;
end Byte_Array_To_Hex_Reverse;
end Utils;