tree checksum vpatch file split hunks
all signers: asciilifeform bvt diana_coman
antecedents: ffa_ch7_turbo_egyptians.kv ffa_ch6_simplest_rsa.kv ffa_ch4_ffacalc.kv
press order:
patch:
(34 . 12)(34 . 12)
5 procedure Get_Argument(Number : in Natural;
6 Result : out String);
7
8 function Len_Arg (Arg_Num : Integer) return Integer;
9 pragma Import(C, Len_Arg, "__gnat_len_arg");
10
11 private
12
13 procedure Fill_Arg (A : System.Address; Arg_Num : Integer);
14 pragma Import(C, Fill_Arg, "__gnat_fill_arg");
15
16 function Len_Arg (Arg_Num : Integer) return Integer;
17 pragma Import(C, Len_Arg, "__gnat_len_arg");
18
19 end CmdLine;
- 7176998BBC09E3C197329341463BF445BA9E05D1C2FA295A8AEA710D7A4CFC60F4810134C74B92FCB71DDBAE6D60DDB5A349EA9E4E3067AC7CAF4EA7D43C7CF3(18 . 8)(18 . 8)-
24 ------------------------------------------------------------------------------
25
26 -- Basics
27 with OS; use OS;
28 with CmdLine; use CmdLine;
29 with OS; use OS;
30 with CmdLine; use CmdLine;
31
32 -- FFA
33 with FZ_Lim; use FZ_Lim;
(39 . 14)(39 . 19)
35 -- For Output
36 with FFA_IO; use FFA_IO;
37
38 -- For RNG:
39 with FFA_RNG; use FFA_RNG;
40
41
42 procedure FFA_Calc is
43
44 Width : Positive; -- Desired FFA Width
45 Height : Positive; -- Desired Height of Stack
46 Width : Positive; -- Desired FFA Width
47 Height : Positive; -- Desired Height of Stack
48 RNG : RNG_Device; -- The active RNG device.
49
50 begin
51 if Arg_Count /= 3 then
52 Eggog("Usage: ./ffa_calc WIDTH HEIGHT");
53 if Arg_Count < 3 or Arg_Count > 4 then
54 Eggog("Usage: ./ffa_calc WIDTH HEIGHT [/dev/rng]");
55 end if;
56
57 declare
(57 . 6)(62 . 24)
59 Get_Argument(1, Arg1); -- First arg
60 Get_Argument(2, Arg2); -- Second arg
61
62 if Arg_Count = 4 then
63 -- RNG was specified:
64 declare
65 Arg3 : CmdLineArg;
66 begin
67 Get_Argument(3, Arg3); -- Third arg (optional)
68
69 -- Ada.Sequential_IO chokes on paths with trailing whitespace!
70 -- So we have to give it a trimmed path. But we can't use
71 -- Ada.Strings.Fixed.Trim, because it suffers from
72 -- SecondaryStackism-syphilis. Instead we are stuck doing this:
73 Init_RNG(RNG, Arg3(Arg3'First .. Len_Arg(3)));
74 end;
75 else
76 -- RNG was NOT specified:
77 Init_RNG(RNG); -- Use the machine default then
78 end if;
79
80 -- Parse into Positives:
81 Width := Positive'Value(Arg1);
82 Height := Positive'Value(Arg2);
(422 . 6)(445 . 12)
84 -- Other --
85 -----------
86
87 -- Push a FZ of RNGolade onto the stack
88 when '?' =>
89 Push;
90 FZ_Clear(Stack(SP));
91 FZ_Random(RNG, Stack(SP));
92
93 -- mUx
94 when 'U' =>
95 Want(3);
(0 . 0)(1 . 58)
100 ------------------------------------------------------------------------------
101 ------------------------------------------------------------------------------
102 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
103 -- --
104 -- (C) 2017 Stanislav Datskovskiy ( www.loper-os.org ) --
105 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
106 -- --
107 -- You do not have, nor can you ever acquire the right to use, copy or --
108 -- distribute this software ; Should you use this software for any purpose, --
109 -- or copy and distribute it to anyone or in any manner, you are breaking --
110 -- the laws of whatever soi-disant jurisdiction, and you promise to --
111 -- continue doing so for the indefinite future. In any case, please --
112 -- always : read and understand any software ; verify any PGP signatures --
113 -- that you use - for any purpose. --
114 -- --
115 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
116 ------------------------------------------------------------------------------
117 ------------------------------------------------------------------------------
118
119 with OS; use OS;
120
121 with FZ_Type; use FZ_Type;
122
123
124 package body FFA_RNG is
125
126 -- Prepare an RNG for use; at given path, or will use default
127 procedure Init_RNG(RNG : out RNG_Device;
128 RNG_Unix_Path : in String := Default_RNG_Path) is
129 begin
130 begin
131 -- Open the RNG at the offered path:
132 Word_IO.Open(File => RNG.F,
133 Mode => Word_IO.In_File,
134 Name => RNG_Unix_Path);
135 exception
136 when others =>
137 Eggog("Could not open RNG at : " & RNG_Unix_Path & "!");
138 end;
139 end Init_RNG;
140
141
142 -- Fill a FZ from RNG
143 procedure FZ_Random(RNG : in RNG_Device;
144 N : out FZ) is
145 begin
146 begin
147 -- Fill the destination FZ from this RNG:
148 for i in N'Range loop
149 Word_IO.Read(RNG.F, N(i));
150 end loop;
151 exception
152 when others =>
153 Eggog("Could not read from RNG!");
154 end;
155 end FZ_Random;
156
157 end FFA_RNG;
-(0 . 0)(1 . 46)
162 ------------------------------------------------------------------------------
163 ------------------------------------------------------------------------------
164 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
165 -- --
166 -- (C) 2017 Stanislav Datskovskiy ( www.loper-os.org ) --
167 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
168 -- --
169 -- You do not have, nor can you ever acquire the right to use, copy or --
170 -- distribute this software ; Should you use this software for any purpose, --
171 -- or copy and distribute it to anyone or in any manner, you are breaking --
172 -- the laws of whatever soi-disant jurisdiction, and you promise to --
173 -- continue doing so for the indefinite future. In any case, please --
174 -- always : read and understand any software ; verify any PGP signatures --
175 -- that you use - for any purpose. --
176 -- --
177 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
178 ------------------------------------------------------------------------------
179 ------------------------------------------------------------------------------
180
181 with Ada.Sequential_IO;
182
183 with Words; use Words;
184 with FZ_Type; use FZ_Type;
185
186
187 package FFA_RNG is
188
189 Default_RNG_Path : constant String := "/dev/random";
190
191 -- For reading from RNGs:
192 package Word_IO is new Ada.Sequential_IO(Element_Type => Word);
193
194 -- Represents an RNG Device:
195 type RNG_Device is record
196 F : Word_IO.File_Type;
197 end record;
198
199 -- Prepare an RNG for use; at given path, or will use default
200 procedure Init_RNG(RNG : out RNG_Device;
201 RNG_Unix_Path : in String := Default_RNG_Path);
202
203 -- Fill a FZ from RNG
204 procedure FZ_Random(RNG : in RNG_Device;
205 N : out FZ);
206
207 end FFA_RNG;
- B9880001CD7A0ADC289E3F1EBC71B48EFF6CB1079EEB5A21BF32A81BA4E0E0F3F9917FFFDD02E0C1AC2F06553610FA0F396408680C411C8DB3B6A80A8E1E978D(60 . 41)(60 . 44)
212 Modulus : in FZ;
213 Result : out FZ) is
214
215 -- Working register for the squaring
216 -- Working register for the squaring; initially is copy of Base
217 B : FZ(Base'Range) := Base;
218
219 -- Register for cycling through the bits of E
220 -- Copy of Exponent, for cycling through its bits
221 E : FZ(Exponent'Range) := Exponent;
222
223 -- Register for the Mux operation
224 T : FZ(Result'Range);
225
226 -- Buffer register for the Result
227 R : FZ(Result'Range);
228
229 begin
230 -- Result := 1
231 WBool_To_FZ(1, Result);
232 WBool_To_FZ(1, R);
233
234 -- For each bit of Result width:
235 for i in 1 .. FZ_Bitness(Result) loop
236 -- For each bit of R width:
237 for i in 1 .. FZ_Bitness(R) loop
238
239 -- T := Result * B mod Modulus
240 FZ_Mod_Mul(X => Result, Y => B, Modulus => Modulus,
241 Product => T);
242 FZ_Mod_Mul(X => R, Y => B, Modulus => Modulus, Product => T);
243
244 -- Sel is the current low bit of E;
245 -- When Sel=0 -> Result := Result;
246 -- When Sel=1 -> Result := T
247 FZ_Mux(X => Result, Y => T, Result => Result,
248 Sel => FZ_OddP(E));
249 FZ_Mux(X => R, Y => T, Result => R, Sel => FZ_OddP(E));
250
251 -- Advance to the next bit of E
252 FZ_ShiftRight(E, E, 1);
253
254 -- B := B*B mod Modulus
255 FZ_Mod_Mul(X => B, Y => B, Modulus => Modulus,
256 Product => B);
257 FZ_Mod_Mul(X => B, Y => B, Modulus => Modulus, Product => B);
258
259 end loop;
260
261 -- Output the Result:
262 Result := R;
263
264 end FZ_Mod_Exp;
265 pragma Inline_Always(FZ_Mod_Exp);
266