1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright © 2024 kyberlib. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
#[cfg(test)]
mod tests {
use kyberlib::{rng::randombytes, KyberLibError};
use rand_core::OsRng;
#[test]
fn test_randombytes() {
// Test with a buffer of size 32
let mut buffer = [0u8; 32];
let buffer_len = buffer.len();
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes to fill the buffer
let result = randombytes(&mut buffer, buffer_len, &mut rng);
// Check if the result is Ok, indicating successful random byte generation
assert!(result.is_ok());
}
#[test]
fn test_randombytes_success() {
// Test with a buffer of size 32
let mut buffer = [0u8; 32];
let buffer_len = buffer.len();
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes to fill the buffer
let result = randombytes(&mut buffer, buffer_len, &mut rng);
// Check if the result is Ok, indicating successful random byte generation
assert!(result.is_ok());
}
#[test]
fn test_randombytes_small_buffer() {
// Test with a smaller buffer (8 bytes)
let mut buffer = [0u8; 8];
let buffer_len = buffer.len();
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes to fill the buffer
let result = randombytes(&mut buffer, buffer_len, &mut rng);
// Check if the result is Ok, indicating successful random byte generation
assert!(result.is_ok());
// Check that the buffer length is unchanged
assert_eq!(buffer.len(), buffer_len);
}
#[test]
fn test_randombytes_large_buffer() {
// Test with a larger buffer (64 bytes)
let mut buffer = [0u8; 64];
let buffer_len = buffer.len();
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes to fill the buffer
let result = randombytes(&mut buffer, buffer_len, &mut rng);
// Check if the result is Ok, indicating successful random byte generation
assert!(result.is_ok());
// Check that the buffer length is unchanged
assert_eq!(buffer.len(), buffer_len);
}
#[test]
fn test_randombytes_zero_length_buffer() {
let mut buffer = [];
let buffer_len = buffer.len();
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes with a zero-length buffer
let result = randombytes(&mut buffer, buffer_len, &mut rng);
// Check if the result is Ok, indicating no error
assert!(result.is_ok());
// Check that the buffer length is still zero
assert_eq!(buffer.len(), 0);
}
#[test]
fn test_randombytes_randomness() {
// Test with a buffer of size 32
let mut buffer1 = [0u8; 32];
let mut buffer2 = [0u8; 32];
let buffer_len = buffer1.len();
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes to fill the first buffer
let result1 = randombytes(&mut buffer1, buffer_len, &mut rng);
// Call randombytes to fill the second buffer
let result2 = randombytes(&mut buffer2, buffer_len, &mut rng);
// Check if both results are Ok and the buffers are different
assert!(result1.is_ok());
assert!(result2.is_ok());
assert_ne!(&buffer1[..], &buffer2[..]);
}
#[test]
fn test_randombytes_partial_fill() {
// Test filling a partial buffer
let mut buffer = [0u8; 32];
let partial_len = 16;
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes to partially fill the buffer
let result = randombytes(&mut buffer, partial_len, &mut rng);
// Check if the result is Ok, indicating successful random byte generation
assert!(result.is_ok());
// Check that the buffer length is unchanged
assert_eq!(buffer.len(), 32);
// Check that the first 16 bytes are filled with random data
assert_ne!(&buffer[..partial_len], &[0u8; 16]);
}
#[test]
fn test_randombytes_error_handling() {
// Test with a buffer of size 32
let mut buffer = [0u8; 32];
let buffer_len = buffer.len();
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes with a valid length
let result = randombytes(&mut buffer, buffer_len, &mut rng);
// Check if the result is ok
assert!(result.is_ok());
// Call randombytes with an invalid length
let result = randombytes(&mut buffer, buffer_len + 1, &mut rng);
// Check if the result is an error
assert!(matches!(result, Err(KyberLibError::InvalidLength)));
}
#[test]
fn test_randombytes_out_of_bounds() {
// Test with a buffer of size 32
let mut buffer = [0u8; 32];
let buffer_len = buffer.len();
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes with an out-of-bounds length
let result = randombytes(&mut buffer, buffer_len + 1, &mut rng);
// Check if the result is an InvalidLength error
assert!(matches!(result, Err(KyberLibError::InvalidLength)));
}
#[test]
fn test_randombytes_invalid_rng() {
// Test with a buffer of size 32
let mut buffer = [0u8; 32];
let buffer_len = buffer.len();
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes with a valid RNG
let result = randombytes(&mut buffer, buffer_len, &mut rng);
// Check if the result is ok
assert!(result.is_ok());
}
#[test]
fn test_randombytes_invalid_length() {
// Test with a buffer of size 32
let mut buffer = [0u8; 32];
let invalid_len = 33;
// Use OsRng as the RNG
let mut rng = OsRng;
// Call randombytes with an invalid length
let result = randombytes(&mut buffer, invalid_len, &mut rng);
// Check if the result is an InvalidLength error
assert!(matches!(result, Err(KyberLibError::InvalidLength)));
}
}