------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- This file is part of 'Finite Field Arithmetic', aka 'FFA'.               --
--                                                                          --
-- (C) 2017 Stanislav Datskovskiy ( www.loper-os.org )                      --
-- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
--                                                                          --
-- You do not have, nor can you ever acquire the right to use, copy or      --
-- distribute this software ; Should you use this software for any purpose, --
-- or copy and distribute it to anyone or in any manner, you are breaking   --
-- the laws of whatever soi-disant jurisdiction, and you promise to         --
-- continue doing so for the indefinite future. In any case, please         --
-- always : read and understand any software ; verify any PGP signatures    --
-- that you use - for any purpose.                                          --
--                                                                          --
-- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
------------------------------------------------------------------------------
------------------------------------------------------------------------------

with Word_Ops; use Word_Ops;


package body FZ_Basic is
   
   ---------------------------------------------------------------------------
   -- Fundamental Operations on FZ (finite integers)
   ---------------------------------------------------------------------------
   
   -- Determine the Bitness of N
   function FZ_Bitness(N : in FZ) return Bit_Count is
   begin
      return N'Length * Words.Bitness;
   end FZ_Bitness;
   pragma Inline_Always(FZ_Bitness);
   
   
   -- N := 0
   procedure FZ_Clear(N : out FZ) is
   begin
      N := (others => 0);
   end FZ_Clear;
   pragma Inline_Always(FZ_Clear);
   
   
   -- Set given FZ to a given truth value
   procedure WBool_To_FZ(V : in WBool; N : out FZ) is
   begin
      FZ_Clear(N);
      FZ_Set_Head(N, V);
   end WBool_To_FZ;
   pragma Inline_Always(WBool_To_FZ);
   
   
   -- First word of N := Source
   procedure FZ_Set_Head(N : out FZ; Source : in Word) is
   begin
      N(N'First) := Source;
   end FZ_Set_Head;
   pragma Inline_Always(FZ_Set_Head);
   
   
   -- First word of N
   function FZ_Get_Head(N : in FZ) return Word is
   begin
      return N(N'First);
   end FZ_Get_Head;
   pragma Inline_Always(FZ_Get_Head);
   
   
   -- Exchange X and Y
   procedure FZ_Swap(X : in out FZ; Y : in out FZ) is
      T : FZ(X'Range);
   begin
      T := X;
      X := Y;
      Y := T;
   end FZ_Swap;
   pragma Inline_Always(FZ_Swap);
   
   
   -- Constant-time MUX: Sel = 0: Result := X; Sel = 1: Result := Y
   procedure FZ_Mux(X : in FZ; Y : in FZ; Result : out FZ; Sel : in WBool) is
   begin
      for i in X'Range loop
         Result(i) := W_Mux(X(i), Y(i), Sel);
      end loop;
   end FZ_Mux;
   pragma Inline_Always(FZ_Mux);
   
end FZ_Basic;