Debian Codegraph @

gcc/ada/libgnat/s-expmod.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . E X P _ M O D --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- Preconditions, postconditions, ghost code, loop invariants and assertions
33 -- in this unit are meant for analysis only, not for run-time checking, as it
34 -- would be too costly otherwise. This is enforced by setting the assertion
35 -- policy to Ignore.
36
37 pragma Assertion_Policy (Pre => Ignore,
38 Post => Ignore,
39 Ghost => Ignore,
40 Loop_Invariant => Ignore,
41 Assert => Ignore);
42
43 with Ada.Numerics.Big_Numbers.Big_Integers_Ghost;
44 use Ada.Numerics.Big_Numbers.Big_Integers_Ghost;
45
46 package body System.Exp_Mod
47 with SPARK_Mode
48 is
49 use System.Unsigned_Types;
50
51 -- Local lemmas
52
53 procedure Lemma_Add_Mod (X, Y : Big_Natural; B : Big_Positive)
54 with
55 Ghost,
56 Post => (X + Y) mod B = ((X mod B) + (Y mod B)) mod B;
57
58 procedure Lemma_Exp_Expand (A : Big_Integer; Exp : Natural)
59 with
60 Ghost,
61 Post =>
62 (if Exp rem 2 = 0 then
63 A ** Exp = A ** (Exp / 2) * A ** (Exp / 2)
64 else
65 A ** Exp = A ** (Exp / 2) * A ** (Exp / 2) * A);
66
67 procedure Lemma_Exp_Mod (A : Big_Natural; Exp : Natural; B : Big_Positive)
68 with
69 Ghost,
70 Subprogram_Variant => (Decreases => Exp),
71 Post => ((A mod B) ** Exp) mod B = (A ** Exp) mod B;
72
73 procedure Lemma_Mod_Ident (A : Big_Natural; B : Big_Positive)
74 with
75 Ghost,
76 Pre => A < B,
77 Post => A mod B = A;
78
79 procedure Lemma_Mod_Mod (A : Big_Integer; B : Big_Positive)
80 with
81 Ghost,
82 Post => A mod B mod B = A mod B;
83
84 procedure Lemma_Mult_Div (X : Big_Natural; Y : Big_Positive)
85 with
86 Ghost,
87 Post => X * Y / Y = X;
88
89 procedure Lemma_Mult_Mod (X, Y : Big_Natural; B : Big_Positive)
90 with
91 Ghost,
92 -- The following subprogram variant can be added as soon as supported
93 -- Subprogram_Variant => (Decreases => Y),
94 Post => (X * Y) mod B = ((X mod B) * (Y mod B)) mod B;
95
96 -----------------------------
97 -- Local lemma null bodies --
98 -----------------------------
99
100 procedure Lemma_Mod_Ident (A : Big_Natural; B : Big_Positive) is null;
101 procedure Lemma_Mod_Mod (A : Big_Integer; B : Big_Positive) is null;
102 procedure Lemma_Mult_Div (X : Big_Natural; Y : Big_Positive) is null;
103
104 -------------------
105 -- Lemma_Add_Mod --
106 -------------------
107
108 procedure Lemma_Add_Mod (X, Y : Big_Natural; B : Big_Positive) is
109
110 procedure Lemma_Euclidean_Mod (Q, F, R : Big_Natural) with
111 Pre => F /= 0,
112 Post => (Q * F + R) mod F = R mod F,
113 Subprogram_Variant => (Decreases => Q);
114
115 -------------------------
116 -- Lemma_Euclidean_Mod --
117 -------------------------
118
119 procedure Lemma_Euclidean_Mod (Q, F, R : Big_Natural) is
120 begin
121 if Q > 0 then
122 Lemma_Euclidean_Mod (Q - 1, F, R);
123 end if;
124 end Lemma_Euclidean_Mod;
125
126 -- Local variables
127
128 Left : constant Big_Natural := (X + Y) mod B;
129 Right : constant Big_Natural := ((X mod B) + (Y mod B)) mod B;
130 XQuot : constant Big_Natural := X / B;
131 YQuot : constant Big_Natural := Y / B;
132 AQuot : constant Big_Natural := (X mod B + Y mod B) / B;
133 begin
134 if Y /= 0 and B > 1 then
135 pragma Assert (X = XQuot * B + X mod B);
136 pragma Assert (Y = YQuot * B + Y mod B);
137 pragma Assert
138 (Left = ((XQuot + YQuot) * B + X mod B + Y mod B) mod B);
139 pragma Assert (X mod B + Y mod B = AQuot * B + Right);
140 pragma Assert (Left = ((XQuot + YQuot + AQuot) * B + Right) mod B);
141 Lemma_Euclidean_Mod (XQuot + YQuot + AQuot, B, Right);
142 pragma Assert (Left = (Right mod B));
143 pragma Assert (Left = Right);
144 end if;
145 end Lemma_Add_Mod;
146
147 ----------------------
148 -- Lemma_Exp_Expand --
149 ----------------------
150
151 procedure Lemma_Exp_Expand (A : Big_Integer; Exp : Natural) is
152
153 procedure Lemma_Exp_Distribution (Exp_1, Exp_2 : Natural) with
154 Pre => Natural'Last - Exp_2 >= Exp_1,
155 Post => A ** (Exp_1 + Exp_2) = A ** (Exp_1) * A ** (Exp_2);
156
157 ----------------------------
158 -- Lemma_Exp_Distribution --
159 ----------------------------
160
161 procedure Lemma_Exp_Distribution (Exp_1, Exp_2 : Natural) is null;
162
163 begin
164 if Exp rem 2 = 0 then
165 pragma Assert (Exp = Exp / 2 + Exp / 2);
166 else
167 pragma Assert (Exp = Exp / 2 + Exp / 2 + 1);
168 Lemma_Exp_Distribution (Exp / 2, Exp / 2 + 1);
169 Lemma_Exp_Distribution (Exp / 2, 1);
170 end if;
171 end Lemma_Exp_Expand;
172
173 -------------------
174 -- Lemma_Exp_Mod --
175 -------------------
176
177 procedure Lemma_Exp_Mod (A : Big_Natural; Exp : Natural; B : Big_Positive)
178 is
179 begin
180 if Exp /= 0 then
181 declare
182 Left : constant Big_Integer := ((A mod B) ** Exp) mod B;
183 Right : constant Big_Integer := (A ** Exp) mod B;
184 begin
185 Lemma_Mult_Mod (A mod B, (A mod B) ** (Exp - 1), B);
186 Lemma_Mod_Mod (A, B);
187 Lemma_Exp_Mod (A, Exp - 1, B);
188 Lemma_Mult_Mod (A, A ** (Exp - 1), B);
189 pragma Assert
190 ((A mod B) * (A mod B) ** (Exp - 1) = (A mod B) ** Exp);
191 pragma Assert (A * A ** (Exp - 1) = A ** Exp);
192 pragma Assert (Left = Right);
193 end;
194 end if;
195 end Lemma_Exp_Mod;
196
197 --------------------
198 -- Lemma_Mult_Mod --
199 --------------------
200
201 procedure Lemma_Mult_Mod (X, Y : Big_Natural; B : Big_Positive) is
202 Left : constant Big_Natural := (X * Y) mod B;
203 Right : constant Big_Natural := ((X mod B) * (Y mod B)) mod B;
204 begin
205 if Y /= 0 and B > 1 then
206 Lemma_Add_Mod (X * (Y - 1), X, B);
207 Lemma_Mult_Mod (X, Y - 1, B);
208 Lemma_Mod_Mod (X, B);
209 Lemma_Add_Mod ((X mod B) * ((Y - 1) mod B), X mod B, B);
210 Lemma_Add_Mod (Y - 1, 1, B);
211 pragma Assert (((Y - 1) mod B + 1) mod B = Y mod B);
212 if (Y - 1) mod B + 1 < B then
213 Lemma_Mod_Ident ((Y - 1) mod B + 1, B);
214 Lemma_Mod_Mod ((X mod B) * (Y mod B), B);
215 pragma Assert (Left = Right);
216 else
217 pragma Assert (Y mod B = 0);
218 pragma Assert (Y / B * B = Y);
219 pragma Assert ((X * Y) mod B = (X * Y) - (X * Y) / B * B);
220 pragma Assert
221 ((X * Y) mod B = (X * Y) - (X * (Y / B) * B) / B * B);
222 Lemma_Mult_Div (X * (Y / B), B);
223 pragma Assert (Left = 0);
224 pragma Assert (Left = Right);
225 end if;
226 end if;
227 end Lemma_Mult_Mod;
228
229 -----------------
230 -- Exp_Modular --
231 -----------------
232
233 function Exp_Modular
234 (Left : Unsigned;
235 Modulus : Unsigned;
236 Right : Natural) return Unsigned
237 is
238 Result : Unsigned := 1;
239 Factor : Unsigned := Left;
240 Exp : Natural := Right;
241
242 function Mult (X, Y : Unsigned) return Unsigned is
243 (Unsigned (Long_Long_Unsigned (X) * Long_Long_Unsigned (Y)
244 mod Long_Long_Unsigned (Modulus)))
245 with
246 Pre => Modulus /= 0;
247 -- Modular multiplication. Note that we can't take advantage of the
248 -- compiler's circuit, because the modulus is not known statically.
249
250 -- Local ghost variables, functions and lemmas
251
252 M : constant Big_Positive := Big (Modulus) with Ghost;
253
254 function Equal_Modulo (X, Y : Big_Integer) return Boolean is
255 (X mod M = Y mod M)
256 with
257 Ghost,
258 Pre => Modulus /= 0;
259
260 procedure Lemma_Mult (X, Y : Unsigned)
261 with
262 Ghost,
263 Post => Big (Mult (X, Y)) = (Big (X) * Big (Y)) mod M
264 and then Big (Mult (X, Y)) < M;
265
266 procedure Lemma_Mult (X, Y : Unsigned) is
267 begin
268 pragma Assert (Big (Mult (X, Y)) = (Big (X) * Big (Y)) mod M);
269 end Lemma_Mult;
270
271 Rest : Big_Integer with Ghost;
272 -- Ghost variable to hold Factor**Exp between Exp and Factor updates
273
274 begin
275 pragma Assert (Modulus /= 1);
276
277 -- We use the standard logarithmic approach, Exp gets shifted right
278 -- testing successive low order bits and Factor is the value of the
279 -- base raised to the next power of 2.
280
281 -- Note: it is not worth special casing the cases of base values -1,0,+1
282 -- since the expander does this when the base is a literal, and other
283 -- cases will be extremely rare.
284
285 if Exp /= 0 then
286 loop
287 pragma Loop_Invariant (Exp > 0);
288 pragma Loop_Invariant (Result < Modulus);
289 pragma Loop_Invariant (Equal_Modulo
290 (Big (Result) * Big (Factor) ** Exp, Big (Left) ** Right));
291 pragma Loop_Variant (Decreases => Exp);
292
293 if Exp rem 2 /= 0 then
294 pragma Assert
295 (Big (Factor) ** Exp
296 = Big (Factor) * Big (Factor) ** (Exp - 1));
297 pragma Assert (Equal_Modulo
298 ((Big (Result) * Big (Factor)) * Big (Factor) ** (Exp - 1),
299 Big (Left) ** Right));
300 pragma Assert (Big (Factor) >= 0);
301 Lemma_Mult_Mod (Big (Result) * Big (Factor),
302 Big (Factor) ** (Exp - 1),
303 Big (Modulus));
304 Lemma_Mult (Result, Factor);
305
306 Result := Mult (Result, Factor);
307
308 Lemma_Mod_Ident (Big (Result), Big (Modulus));
309 Lemma_Mod_Mod (Big (Factor) ** (Exp - 1), Big (Modulus));
310 Lemma_Mult_Mod (Big (Result),
311 Big (Factor) ** (Exp - 1),
312 Big (Modulus));
313 pragma Assert (Equal_Modulo
314 (Big (Result) * Big (Factor) ** (Exp - 1),
315 Big (Left) ** Right));
316 Lemma_Exp_Expand (Big (Factor), Exp - 1);
317 pragma Assert (Exp / 2 = (Exp - 1) / 2);
318 end if;
319
320 Lemma_Exp_Expand (Big (Factor), Exp);
321
322 Exp := Exp / 2;
323 exit when Exp = 0;
324
325 Rest := Big (Factor) ** Exp;
326 pragma Assert (Equal_Modulo
327 (Big (Result) * (Rest * Rest), Big (Left) ** Right));
328 Lemma_Exp_Mod (Big (Factor) * Big (Factor), Exp, Big (Modulus));
329 pragma Assert
330 ((Big (Factor) * Big (Factor)) ** Exp = Rest * Rest);
331 pragma Assert (Equal_Modulo
332 ((Big (Factor) * Big (Factor)) ** Exp,
333 Rest * Rest));
334 Lemma_Mult (Factor, Factor);
335
336 Factor := Mult (Factor, Factor);
337
338 Lemma_Mod_Mod (Rest * Rest, Big (Modulus));
339 Lemma_Mod_Ident (Big (Result), Big (Modulus));
340 Lemma_Mult_Mod (Big (Result), Rest * Rest, Big (Modulus));
341 pragma Assert (Big (Factor) >= 0);
342 Lemma_Mult_Mod (Big (Result), Big (Factor) ** Exp,
343 Big (Modulus));
344 pragma Assert (Equal_Modulo
345 (Big (Result) * Big (Factor) ** Exp, Big (Left) ** Right));
346 end loop;
347
348 pragma Assert (Big (Result) = Big (Left) ** Right mod Big (Modulus));
349 end if;
350
351 return Result;
352
353 end Exp_Modular;
354
355 end System.Exp_Mod;