File : s-expmod.ads


   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-2015, 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 --                                                                          --
  19 --                                                                          --
  20 --                                                                          --
  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 --  Note that 1 is a binary modulus (2**0), so the compiler should not (and
  36 --  will not) call this function with Modulus equal to 1.
  37 
  38 with System.Unsigned_Types;
  39 
  40 package System.Exp_Mod is
  41    pragma Pure;
  42    use type System.Unsigned_Types.Unsigned;
  43 
  44    subtype Power_Of_2 is System.Unsigned_Types.Unsigned with
  45      Dynamic_Predicate =>
  46         Power_Of_2 /= 0 and then (Power_Of_2 and (Power_Of_2 - 1)) = 0;
  47 
  48    function Exp_Modular
  49      (Left    : System.Unsigned_Types.Unsigned;
  50       Modulus : System.Unsigned_Types.Unsigned;
  51       Right   : Natural) return System.Unsigned_Types.Unsigned
  52    with
  53        Pre  => Modulus /= 0 and then Modulus not in Power_Of_2,
  54        Post => Exp_Modular'Result = Left ** Right mod Modulus;
  55 
  56 end System.Exp_Mod;