File : ffa.adb


   1 ------------------------------------------------------------------------------
   2 ------------------------------------------------------------------------------
   3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'.               --
   4 --                                                                          --
   5 -- (C) 2018 Stanislav Datskovskiy ( www.loper-os.org )                      --
   6 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
   7 --                                                                          --
   8 -- You do not have, nor can you ever acquire the right to use, copy or      --
   9 -- distribute this software ; Should you use this software for any purpose, --
  10 -- or copy and distribute it to anyone or in any manner, you are breaking   --
  11 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
  12 -- continue doing so for the indefinite future. In any case, please         --
  13 -- always : read and understand any software ; verify any PGP signatures    --
  14 -- that you use - for any purpose.                                          --
  15 --                                                                          --
  16 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
  17 ------------------------------------------------------------------------------
  18 ------------------------------------------------------------------------------
  19 
  20 with FZ_Arith;
  21 with FZ_Shift;
  22 with FZ_Mul;
  23 
  24 
  25 -- Wrapper bodies for routines that we inline, but must enforce preconditions
  26 -- on when called by FFA user.
  27 package body FFA is
  28    
  29    ----------------------------------------------------------------------------
  30    --- FZ Basics
  31    ----------------------------------------------------------------------------
  32    
  33    -- Exchange X and Y
  34    procedure FFA_FZ_Swap(X : in out FZ; Y : in out FZ) is
  35    begin
  36       FZ_Basic.FZ_Swap(X => X, Y => Y);
  37    end FFA_FZ_Swap;
  38    
  39    -- Constant-time MUX: Sel = 0: Result := X; Sel = 1: Result := Y
  40    procedure FFA_FZ_Mux(X : in FZ; Y : in FZ;
  41                         Result : out FZ; Sel : in WBool) is
  42    begin
  43       FZ_Basic.FZ_Mux(X => X, Y => Y, Result => Result, Sel => Sel);
  44    end FFA_FZ_Mux;
  45    
  46    ----------------------------------------------------------------------------
  47    --- Bitwise Operations on FZ
  48    ----------------------------------------------------------------------------
  49    
  50    -- Result := X & Y
  51    procedure FFA_FZ_And(X : in FZ; Y : in FZ; Result : out FZ) is
  52    begin
  53       FZ_BitOp.FZ_And(X => X, Y => Y, Result => Result);
  54    end FFA_FZ_And;
  55    
  56    -- Result := X | Y
  57    procedure FFA_FZ_Or(X : in FZ; Y : in FZ; Result : out FZ) is
  58    begin
  59       FZ_BitOp.FZ_Or(X => X, Y => Y, Result => Result);
  60    end FFA_FZ_Or;
  61    
  62    -- Result := X ^ Y
  63    procedure FFA_FZ_Xor(X : in FZ; Y : in FZ; Result : out FZ) is
  64    begin
  65       FZ_BitOp.FZ_Xor(X => X, Y => Y, Result => Result);
  66    end FFA_FZ_Xor;
  67    
  68    -- NotN := ~N ('ones complement')
  69    procedure FFA_FZ_Not(N : in  FZ; NotN : out FZ) is
  70    begin
  71       FZ_BitOp.FZ_Not(N => N, NotN => NotN);
  72    end FFA_FZ_Not;
  73    
  74    ----------------------------------------------------------------------------
  75    --- Arithmetic on FZ
  76    ----------------------------------------------------------------------------
  77    
  78    -- Sum := X + Y; Overflow := Carry
  79    procedure FFA_FZ_Add(X          : in  FZ;
  80                         Y          : in  FZ;
  81                         Sum        : out FZ;
  82                         Overflow   : out WBool) is
  83    begin
  84       FZ_Arith.FZ_Add(X => X, Y => Y, Sum => Sum, Overflow => Overflow);
  85    end FFA_FZ_Add;
  86    
  87    -- Difference := X - Y; Underflow := Borrow
  88    procedure FFA_FZ_Subtract(X          : in  FZ;
  89                              Y          : in  FZ;
  90                              Difference : out FZ;
  91                              Underflow  : out WBool) is
  92    begin
  93       FZ_Arith.FZ_Sub(X => X, Y => Y, Difference => Difference,
  94                       Underflow => Underflow);
  95    end FFA_FZ_Subtract;
  96    
  97    ----------------------------------------------------------------------------
  98    --- Multiplication on FZ
  99    ----------------------------------------------------------------------------
 100    
 101    procedure FFA_FZ_Multiply(X     : in  FZ;
 102                              Y     : in  FZ;
 103                              XY_Lo : out FZ;
 104                              XY_Hi : out FZ) is
 105    begin
 106       FZ_Mul.FZ_Multiply_Buffered(X     => X,     Y     => Y,
 107                                   XY_Lo => XY_Lo, XY_Hi => XY_Hi);
 108    end FFA_FZ_Multiply;
 109    
 110 end FFA;