File : fz_basic.adb
1 ------------------------------------------------------------------------------
2 ------------------------------------------------------------------------------
3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
4 -- --
5 -- (C) 2017 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 Word_Ops; use Word_Ops;
21
22
23 package body FZ_Basic is
24
25 ---------------------------------------------------------------------------
26 -- Fundamental Operations on FZ (finite integers)
27 ---------------------------------------------------------------------------
28
29 -- Determine the Bitness of N
30 function FZ_Bitness(N : in FZ) return Bit_Count is
31 begin
32 return N'Length * Words.Bitness;
33 end FZ_Bitness;
34
35
36 -- N := 0
37 procedure FZ_Clear(N : out FZ) is
38 begin
39 N := (others => 0);
40 end FZ_Clear;
41
42
43 -- Set given FZ to a given truth value
44 procedure WBool_To_FZ(V : in WBool; N : out FZ) is
45 begin
46 FZ_Clear(N);
47 FZ_Set_Head(N, V);
48 end WBool_To_FZ;
49
50
51 -- First word of N := Source
52 procedure FZ_Set_Head(N : out FZ; Source : in Word) is
53 begin
54 N(N'First) := Source;
55 end FZ_Set_Head;
56
57
58 -- First word of N
59 function FZ_Get_Head(N : in FZ) return Word is
60 begin
61 return N(N'First);
62 end FZ_Get_Head;
63
64
65 -- Exchange X and Y
66 procedure FZ_Swap(X : in out FZ; Y : in out FZ) is
67 T : FZ(X'Range);
68 begin
69 T := X;
70 X := Y;
71 Y := T;
72 end FZ_Swap;
73
74
75 -- Constant-time MUX: Sel = 0: Result := X; Sel = 1: Result := Y
76 procedure FZ_Mux(X : in FZ; Y : in FZ; Result : out FZ; Sel : in WBool) is
77 begin
78 for i in X'Range loop
79 Result(i) := W_Mux(X(i), Y(i), Sel);
80 end loop;
81 end FZ_Mux;
82
83 end FZ_Basic;