- E0071E8372C50670886565CC5EF6F69E310AB5F4922ABD6CB20FA281087C6790CE0EC1980AF4A2504DCB189A8009CF5354D7581CF9B4F4D1F515086ECD08B248
+ B91E0448A3D5EC419C7E47701E8A3BFC53082601B860AC093EDD13606C093D1C6F112F514A8A90C0825713B45B5C273145D67D90D291631D6AAF940300DF9386
eucrypt/smg_keccak/tests/smg_keccak-test.adb
(11 . 7)(11 . 6)
243 type Test_Round is array(Round_Index) of Test_Vector;
244
245 --helper methods
246
247 procedure print_state(S: in State; Title: in String) is
248 Hex: array(0..15) of Character := ("0123456789ABCDEF");
249 Len: constant Natural := Z_Length / 4;
(32 . 6)(31 . 24)
251 end loop;
252 end;
253
254 procedure print_bitstream(B: in Bitstream; Title: in String) is
255 Hex : array(0..15) of Character := ("0123456789ABCDEF");
256 HexString : String(1..B'Length/4);
257 C : Natural;
258 Pos : Natural;
259 begin
260 for I in 1..B'Length/4 loop
261 Pos := (I-1)*4 + B'First;
262 C := Natural( B(Pos) ) * 8 +
263 Natural( B(Pos + 1) ) * 4 +
264 Natural( B(Pos + 2) ) * 2 +
265 Natural( B(Pos + 3) );
266 HexString(I) := Hex(C);
267 end loop;
268 Put_Line("---" & Title & "---");
269 Put_Line(HexString);
270 end print_bitstream;
271
272 function read_state(File: in FILE_TYPE; Oct: Positive :=8) return State is
273 S: State;
274 Line1: String := "0000000000000000 " &
(140 . 6)(157 . 117)
276 Input := Expected;
277 end loop;
278 end test_one_round;
279
280 procedure test_bits_to_word_conversion is
281 bits: Bitword := (others => 0);
282 obtained_bits: Bitword := (others => 0);
283 expected: ZWord;
284 obtained: ZWord;
285 begin
286 expected := 16#E7DDE140798F25F1#;
287 bits := (1,1,1,0, 0,1,1,1, 1,1,0,1, 1,1,0,1, 1,1,1,0, 0,0,0,1, 0,1,0,0,
288 0,0,0,0, 0,1,1,1, 1,0,0,1, 1,0,0,0, 1,1,1,1, 0,0,1,0, 0,1,0,1,
289 1,1,1,1, 0,0,0,1);
290 obtained := BitsToWord(bits);
291 obtained_bits := WordToBits(expected);
292
293 if obtained /= expected then
294 Put_Line("FAIL: bits to word");
295 Put_Line("Expected: " & ZWord'Image(expected));
296 Put_Line("Obtained: " & ZWord'Image(obtained));
297 else
298 Put_Line("PASSED: bits to word");
299 end if;
300
301 if obtained_bits /= bits then
302 Put_Line("FAIL: word to bits");
303 Put("Expected: ");
304 for I in Bitword'Range loop
305 Put(Bit'Image(bits(I)));
306 end loop;
307 Put_Line("");
308 Put_Line("Obtained: ");
309 for I in Bitword'Range loop
310 Put(Bit'Image(obtained_bits(I)));
311 end loop;
312 Put_Line("");
313 else
314 Put_Line("PASSED: word to bits");
315 end if;
316 end test_bits_to_word_conversion;
317
318 procedure test_sponge is
319 Bitrate : constant Keccak_Rate := 1344;
320 Input : Bitstream(1..5) := (1, 1, 0, 0, 1);
321 Output : Bitstream(1..Bitrate*2);
322 Hex : array(0..15) of Character := ("0123456789ABCDEF");
323 HexString : String(1..Bitrate/2);
324 C : Natural;
325 ExpHex : String(1..Bitrate/2);
326 Error : Natural;
327 Pos : Natural;
328 begin
329 ExpHex := "B57B7DAED6330F79BA5783C5D45EABFFA1461FAC6CEA09BD"&
330 "AAC114F17E23E5B349EECBC907E07FA36ECF8374079811E8"&
331 "5E49243D04182C389E68C733BE698468423DB9891D3A7B10"&
332 "320E0356AB4AB916F68C0EA20365A1D4DBA48218CA89CBB8"&
333 "6D08A34E04544D4100FFE9CB138EADC2D3FC0E8CC2BC15A7"&
334 "5B950776970BFC310F33BF609630D73CAD918CF54657589E"&
335 "42CF7CBF20DE677D2AB7E49389F6F6C3B3FE2992905325CE"&
336 "60931C1515043595ADC1619CB7E034EF52BDC485D03B7FDD"&
337 "7345E849FFB4C4426195C8D88C1E7BF9ADA41B92E006C3DA"&
338 "F1ED0FD63ADD9408A3FC815F727457692727637687C1F79D"&
339 "837DE20798E64C878181C02DF56A533F684459E8A03C8EF6"&
340 "234854531110E6CD9BDEFEA85E35C802B1ACDDF29C9332E2"&
341 "53C0FA72F3ED1ABA274838CFE6EF8BD572E89E1C2135F6A7"&
342 "5BC5D6EA4F85C9A757E68E561A56AC0FC19F1F086C43272F";
343
344 Put_Line("---sponge test---");
345 Sponge(Input, Bitrate, Output);
346 Put_Line("Input is:");
347 for I of Input loop
348 Put(Bit'Image(I));
349 end loop;
350 new_line(1);
351
352 Put_Line("Output is:");
353 for I of Output loop
354 Put(Bit'Image(I));
355 end loop;
356 new_line(1);
357
358 Error := 0;
359 for I in 1..Output'Length/4 loop
360 Pos := Output'First + (I-1)*4;
361 C := Natural( Output( Pos ) ) * 8 +
362 Natural( Output( Pos + 1 ) ) * 4 +
363 Natural( Output( Pos + 2 ) ) * 2 +
364 Natural( Output( Pos + 3 ) );
365 Hexstring(I) := Hex(C);
366 if Hexstring(I) /= ExpHex(I) then
367 Error := Error + 1;
368 end if;
369 end loop;
370 Put_Line("Expected: ");
371 Put_Line(ExpHex);
372 Put_Line("Obtained: ");
373 Put_Line(Hexstring);
374 Put_Line("Errors found: " & Natural'Image(Error));
375
376 end test_sponge;
377
378 procedure test_keccak_function(T: in Test_Round) is
379 S: State;
380 begin
381 Put_Line("---Full Keccak Function test---");
382 S := Keccak_Function(T(Round_Index'First)(None));
383 if S /= T(Round_Index'Last)(Iota) then
384 Put_Line("FAILED: full keccak function test");
385 else
386 Put_Line("PASSED: full keccak function test");
387 end if;
388 end test_keccak_function;
389
390 -- end of helper methods
391
392 --variables
(155 . 6)(283 . 9)
394 test_one_round(T(I), I);
395 end loop;
396
397 -- test also Keccak_Function as a whole --
398 test_keccak_function(T);
399
400 Put_Line("-----Testing with non-zero state as input------");
401 if (not read_from_file("testvectorsnonzero.txt", T)) then
402 return;
(165 . 4)(296 . 13)
404 test_one_round(T(I), I);
405 end loop;
406
407 -- test also Keccak_Function as a whole --
408 test_keccak_function(T);
409
410 -- test BitsToWord and WordToBits
411 test_bits_to_word_conversion;
412
413 -- test Sponge construction
414 test_sponge;
415
416 end SMG_Keccak.Test;