| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . E X P _ M O D -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 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 |
-- This function performs exponentiation of a modular type with nonbinary |
| 33 |
-- modulus values. Arithmetic is done in Long_Long_Unsigned, with explicit |
| 34 |
-- accounting for the modulus value which is passed as the second argument. |
| 35 |
-- |
| 36 |
-- Note that 1 is a binary modulus (2**0), so the compiler should not (and |
| 37 |
-- will not) call this function with Modulus equal to 1. |
| 38 |
|
| 39 |
-- Preconditions in this unit are meant for analysis only, not for run-time |
| 40 |
-- checking, so that the expected exceptions are raised. This is enforced by |
| 41 |
-- setting the corresponding assertion policy to Ignore. Postconditions and |
| 42 |
-- contract cases should not be executed at runtime as well, in order not to |
| 43 |
-- slow down the execution of these functions. |
| 44 |
|
| 45 |
pragma Assertion_Policy (Pre => Ignore, |
| 46 |
Post => Ignore, |
| 47 |
Contract_Cases => Ignore, |
| 48 |
Ghost => Ignore); |
| 49 |
|
| 50 |
with Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 51 |
|
| 52 |
with System.Unsigned_Types; |
| 53 |
|
| 54 |
package System.Exp_Mod |
| 55 |
with Pure, SPARK_Mode |
| 56 |
is |
| 57 |
use type System.Unsigned_Types.Unsigned; |
| 58 |
subtype Unsigned is System.Unsigned_Types.Unsigned; |
| 59 |
|
| 60 |
use type Ada.Numerics.Big_Numbers.Big_Integers_Ghost.Big_Integer; |
| 61 |
subtype Big_Integer is |
| 62 |
Ada.Numerics.Big_Numbers.Big_Integers_Ghost.Big_Integer |
| 63 |
with Ghost; |
| 64 |
|
| 65 |
package Unsigned_Conversion is |
| 66 |
new Ada.Numerics.Big_Numbers.Big_Integers_Ghost.Unsigned_Conversions |
| 67 |
(Int => Unsigned); |
| 68 |
|
| 69 |
function Big (Arg : Unsigned) return Big_Integer is |
| 70 |
(Unsigned_Conversion.To_Big_Integer (Arg)) |
| 71 |
with Ghost; |
| 72 |
|
| 73 |
subtype Power_Of_2 is Unsigned with |
| 74 |
Dynamic_Predicate => |
| 75 |
Power_Of_2 /= 0 and then (Power_Of_2 and (Power_Of_2 - 1)) = 0; |
| 76 |
|
| 77 |
function Exp_Modular |
| 78 |
(Left : Unsigned; |
| 79 |
Modulus : Unsigned; |
| 80 |
Right : Natural) return Unsigned |
| 81 |
with |
| 82 |
Pre => Modulus /= 0 and then Modulus not in Power_Of_2, |
| 83 |
Post => Big (Exp_Modular'Result) = Big (Left) ** Right mod Big (Modulus); |
| 84 |
-- Return the power of ``Left`` by ``Right` modulo ``Modulus``. |
| 85 |
-- |
| 86 |
-- This function is implemented using the standard logarithmic approach: |
| 87 |
-- ``Right`` gets shifted right testing successive low order bits, and |
| 88 |
-- ``Left`` is raised to the next power of 2. The multiplications are |
| 89 |
-- performed using modular multiplications. |
| 90 |
|
| 91 |
end System.Exp_Mod; |