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 with FZ_Sqr;
  24 
  25 
  26 -- Wrapper bodies for routines that we inline, but must enforce preconditions
  27 -- on when called by FFA user.
  28 package body FFA is
  29    
  30    ----------------------------------------------------------------------------
  31    --- FZ Basics
  32    ----------------------------------------------------------------------------
  33    
  34    -- Exchange X and Y
  35    procedure FFA_FZ_Swap(X : in out FZ; Y : in out FZ) is
  36    begin
  37       FZ_Basic.FZ_Swap(X => X, Y => Y);
  38    end FFA_FZ_Swap;
  39    
  40    -- Constant-time MUX: Sel = 0: Result := X; Sel = 1: Result := Y
  41    procedure FFA_FZ_Mux(X : in FZ; Y : in FZ;
  42                         Result : out FZ; Sel : in WBool) is
  43    begin
  44       FZ_Basic.FZ_Mux(X => X, Y => Y, Result => Result, Sel => Sel);
  45    end FFA_FZ_Mux;
  46    
  47    ----------------------------------------------------------------------------
  48    --- Bitwise Operations on FZ
  49    ----------------------------------------------------------------------------
  50    
  51    -- Result := X & Y
  52    procedure FFA_FZ_And(X : in FZ; Y : in FZ; Result : out FZ) is
  53    begin
  54       FZ_BitOp.FZ_And(X => X, Y => Y, Result => Result);
  55    end FFA_FZ_And;
  56    
  57    -- Result := X | Y
  58    procedure FFA_FZ_Or(X : in FZ; Y : in FZ; Result : out FZ) is
  59    begin
  60       FZ_BitOp.FZ_Or(X => X, Y => Y, Result => Result);
  61    end FFA_FZ_Or;
  62    
  63    -- Result := X ^ Y
  64    procedure FFA_FZ_Xor(X : in FZ; Y : in FZ; Result : out FZ) is
  65    begin
  66       FZ_BitOp.FZ_Xor(X => X, Y => Y, Result => Result);
  67    end FFA_FZ_Xor;
  68    
  69    -- NotN := ~N ('ones complement')
  70    procedure FFA_FZ_Not(N : in  FZ; NotN : out FZ) is
  71    begin
  72       FZ_BitOp.FZ_Not(N => N, NotN => NotN);
  73    end FFA_FZ_Not;
  74    
  75    ----------------------------------------------------------------------------
  76    --- Arithmetic on FZ
  77    ----------------------------------------------------------------------------
  78    
  79    -- Sum := X + Y; Overflow := Carry
  80    procedure FFA_FZ_Add(X          : in  FZ;
  81                         Y          : in  FZ;
  82                         Sum        : out FZ;
  83                         Overflow   : out WBool) is
  84    begin
  85       FZ_Arith.FZ_Add(X => X, Y => Y, Sum => Sum, Overflow => Overflow);
  86    end FFA_FZ_Add;
  87    
  88    -- Difference := X - Y; Underflow := Borrow
  89    procedure FFA_FZ_Subtract(X          : in  FZ;
  90                              Y          : in  FZ;
  91                              Difference : out FZ;
  92                              Underflow  : out WBool) is
  93    begin
  94       FZ_Arith.FZ_Sub(X => X, Y => Y, Difference => Difference,
  95                       Underflow => Underflow);
  96    end FFA_FZ_Subtract;
  97    
  98    ----------------------------------------------------------------------------
  99    --- Multiplication on FZ
 100    ----------------------------------------------------------------------------
 101    
 102    procedure FFA_FZ_Multiply(X     : in  FZ;
 103                              Y     : in  FZ;
 104                              XY_Lo : out FZ;
 105                              XY_Hi : out FZ) is
 106    begin
 107       FZ_Mul.FZ_Multiply_Buffered(X     => X,     Y     => Y,
 108                                   XY_Lo => XY_Lo, XY_Hi => XY_Hi);
 109    end FFA_FZ_Multiply;
 110    
 111    
 112    -- Square. Preserves the inputs.
 113    procedure FFA_FZ_Square(X     : in  FZ;
 114                            XX_Lo : out FZ;
 115                            XX_Hi : out FZ) is
 116    begin
 117       FZ_Sqr.FZ_Square_Buffered(X => X, XX_Lo => XX_Lo, XX_Hi => XX_Hi);
 118    end FFA_FZ_Square;
 119    
 120 end FFA;