Finite Field Arithmetic

ffa.adb


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