File : fz_basic.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 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 -- Determine the Bitness of the given FZ's Length
37 function FZ_Bitness_Log2(N : in FZ) return Positive is
38 W : Bit_Count := N'Length;
39 R : Positive := 1;
40 begin
41 while W > 1 loop
42 W := W / 2;
43 R := R + 1;
44 end loop;
45 return R - 1;
46 end FZ_Bitness_Log2;
47
48
49 -- N := 0
50 procedure FZ_Clear(N : out FZ) is
51 begin
52 N := (others => 0);
53 end FZ_Clear;
54
55
56 -- Set given FZ to a given truth value
57 procedure WBool_To_FZ(V : in WBool; N : out FZ) is
58 begin
59 FZ_Clear(N);
60 FZ_Set_Head(N, V);
61 end WBool_To_FZ;
62
63
64 -- First word of N := Source
65 procedure FZ_Set_Head(N : out FZ; Source : in Word) is
66 begin
67 N(N'First) := Source;
68 end FZ_Set_Head;
69
70
71 -- First word of N
72 function FZ_Get_Head(N : in FZ) return Word is
73 begin
74 return N(N'First);
75 end FZ_Get_Head;
76
77
78 -- Exchange X and Y
79 procedure FZ_Swap(X : in out FZ; Y : in out FZ) is
80 T : FZ(X'Range);
81 begin
82 T := X;
83 X := Y;
84 Y := T;
85 end FZ_Swap;
86
87
88 -- Constant-time MUX: Sel = 0: Result := X; Sel = 1: Result := Y
89 procedure FZ_Mux(X : in FZ; Y : in FZ; Result : out FZ; Sel : in WBool) is
90 begin
91 for i in X'Range loop
92 Result(i) := W_Mux(X(i), Y(i), Sel);
93 end loop;
94 end FZ_Mux;
95
96 end FZ_Basic;