v2 / vlib / crypto / sha3 / xof_test.v
80 lines · 62 sloc · 1.69 KB · b615cd08d134956354a72dcc42a6a6ad4e39cb64
Raw
1module sha3
2
3fn test_shake256_streaming_matches_oneshot() {
4 data := 'hello world'.bytes()
5 // oneshot
6 expected := shake256(data, 64)
7
8 // streaming
9 mut s := new_shake256()
10 s.write(data)
11 result := s.read(64)
12
13 assert result == expected, 'streaming SHAKE-256 output differs from one-shot'
14}
15
16fn test_shake128_streaming_matches_oneshot() {
17 data := 'hello world'.bytes()
18 expected := shake128(data, 64)
19
20 mut s := new_shake128()
21 s.write(data)
22 result := s.read(64)
23
24 assert result == expected, 'streaming SHAKE-128 output differs from one-shot'
25}
26
27fn test_shake256_incremental_write() {
28 data := 'the quick brown fox jumps over the lazy dog'.bytes()
29 expected := shake256(data, 128)
30
31 mut s := new_shake256()
32 s.write(data[..10])
33 s.write(data[10..25])
34 s.write(data[25..])
35 result := s.read(128)
36
37 assert result == expected, 'incremental write produced different output'
38}
39
40fn test_shake256_incremental_read() {
41 data := 'test data for incremental reads'.bytes()
42
43 // all at once
44 mut s1 := new_shake256()
45 s1.write(data)
46 all_at_once := s1.read(200)
47
48 // in chunks
49 mut s2 := new_shake256()
50 s2.write(data)
51 mut chunked := []u8{}
52 chunked << s2.read(50)
53 chunked << s2.read(80)
54 chunked << s2.read(70)
55
56 assert chunked == all_at_once, 'incremental read produced different output'
57}
58
59fn test_shake128_large_output() {
60 data := 'large output test'.bytes()
61 mut s := new_shake128()
62 s.write(data)
63 // more than one block (168 bytes in shake128)
64 result := s.read(500)
65 assert result.len == 500
66}
67
68fn test_shake_reset() {
69 data := 'reset test'.bytes()
70
71 mut s := new_shake256()
72 s.write(data)
73 first := s.read(32)
74
75 s.reset()
76 s.write(data)
77 second := s.read(32)
78
79 assert first == second, 'reset did not restore initial state'
80}
81