Skip to content

Commit 2f688d4

Browse files
committed
Convert test vectors to u128 arrays
1 parent 0c53ff5 commit 2f688d4

File tree

1 file changed

+57
-137
lines changed

1 file changed

+57
-137
lines changed

src/rngs/std.rs

Lines changed: 57 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl CryptoRng for StdRng {}
101101
#[cfg(test)]
102102
mod test {
103103
use crate::rngs::StdRng;
104-
use crate::{RngCore, SeedableRng};
104+
use crate::{Rng, RngCore, SeedableRng};
105105

106106
#[test]
107107
fn test_stdrng_construction() {
@@ -125,40 +125,26 @@ mod test {
125125

126126
#[test]
127127
fn test_chacha_true_values_1() {
128-
// Source: Test Vectors for the Stream Cipher ChaCha
128+
// Source: Strombergson 2013, Test Vectors for the Stream Cipher ChaCha
129129
// draft-strombergson-chacha-test-vectors-01
130130
// https://datatracker.ietf.org/doc/html/draft-strombergson-chacha-test-vectors-01
131+
// Converted to LE u128 form (four u128 to one block).
131132
// TC: all zero key and IV, rounds 12, 256-bit key
132133

133134
let seed = [0u8; 32];
134135
let mut rng = StdRng::from_seed(seed);
135136

136-
let mut results = [0u8; 64];
137-
rng.fill_bytes(&mut results);
138-
#[rustfmt::skip]
137+
let mut results = [0u128; 8];
138+
rng.fill(&mut results);
139139
let expected = [
140-
0x9b, 0xf4, 0x9a, 0x6a, 0x07, 0x55, 0xf9, 0x53,
141-
0x81, 0x1f, 0xce, 0x12, 0x5f, 0x26, 0x83, 0xd5,
142-
0x04, 0x29, 0xc3, 0xbb, 0x49, 0xe0, 0x74, 0x14,
143-
0x7e, 0x00, 0x89, 0xa5, 0x2e, 0xae, 0x15, 0x5f,
144-
0x05, 0x64, 0xf8, 0x79, 0xd2, 0x7a, 0xe3, 0xc0,
145-
0x2c, 0xe8, 0x28, 0x34, 0xac, 0xfa, 0x8c, 0x79,
146-
0x3a, 0x62, 0x9f, 0x2c, 0xa0, 0xde, 0x69, 0x19,
147-
0x61, 0x0b, 0xe8, 0x2f, 0x41, 0x13, 0x26, 0xbe,
148-
];
149-
assert_eq!(results, expected);
150-
151-
rng.fill_bytes(&mut results);
152-
#[rustfmt::skip]
153-
let expected = [
154-
0x0b, 0xd5, 0x88, 0x41, 0x20, 0x3e, 0x74, 0xfe,
155-
0x86, 0xfc, 0x71, 0x33, 0x8c, 0xe0, 0x17, 0x3d,
156-
0xc6, 0x28, 0xeb, 0xb7, 0x19, 0xbd, 0xcb, 0xcc,
157-
0x15, 0x15, 0x85, 0x21, 0x4c, 0xc0, 0x89, 0xb4,
158-
0x42, 0x25, 0x8d, 0xcd, 0xa1, 0x4c, 0xf1, 0x11,
159-
0xc6, 0x02, 0xb8, 0x97, 0x1b, 0x8c, 0xc8, 0x43,
160-
0xe9, 0x1e, 0x46, 0xca, 0x90, 0x51, 0x51, 0xc0,
161-
0x27, 0x44, 0xa6, 0xb0, 0x17, 0xe6, 0x93, 0x16,
140+
0xd583265f12ce1f8153f955076a9af49b,
141+
0x5f15ae2ea589007e1474e049bbc32904,
142+
0x798cfaac3428e82cc0e37ad279f86405,
143+
0xbe2613412fe80b611969dea02c9f623a,
144+
0x3d17e08c3371fc86fe743e204188d50b,
145+
0xb489c04c21851515cccbbd19b7eb28c6,
146+
0x43c88c1b97b802c611f14ca1cd8d2542,
147+
0x1693e617b0a64427c0515190ca461ee9,
162148
];
163149
assert_eq!(results, expected);
164150

@@ -167,41 +153,24 @@ mod test {
167153

168154
#[test]
169155
fn test_chacha_true_values_2() {
170-
// Source: Test Vectors for the Stream Cipher ChaCha
171-
// draft-strombergson-chacha-test-vectors-01
172-
// https://datatracker.ietf.org/doc/html/draft-strombergson-chacha-test-vectors-01
156+
// Source: Strombergson 2013, Test Vectors for the Stream Cipher ChaCha
173157
// TC2: single bit set in key, all zero IV, rounds 12, 256-bit key
174158

175159
let mut seed = [0u8; 32];
176160
seed[0] = 1;
177161
let mut rng = StdRng::from_seed(seed);
178162

179-
let mut results = [0u8; 64];
180-
rng.fill_bytes(&mut results);
181-
#[rustfmt::skip]
182-
let expected = [
183-
0x12, 0x05, 0x6e, 0x59, 0x5d, 0x56, 0xb0, 0xf6,
184-
0xee, 0xf0, 0x90, 0xf0, 0xcd, 0x25, 0xa2, 0x09,
185-
0x49, 0x24, 0x8c, 0x27, 0x90, 0x52, 0x5d, 0x0f,
186-
0x93, 0x02, 0x18, 0xff, 0x0b, 0x4d, 0xdd, 0x10,
187-
0xa6, 0x00, 0x22, 0x39, 0xd9, 0xa4, 0x54, 0xe2,
188-
0x9e, 0x10, 0x7a, 0x7d, 0x06, 0xfe, 0xfd, 0xfe,
189-
0xf0, 0x21, 0x0f, 0xeb, 0xa0, 0x44, 0xf9, 0xf2,
190-
0x9b, 0x17, 0x72, 0xc9, 0x60, 0xdc, 0x29, 0xc0,
191-
];
192-
assert_eq!(results, expected);
193-
194-
rng.fill_bytes(&mut results);
195-
#[rustfmt::skip]
163+
let mut results = [0u128; 8];
164+
rng.fill(&mut results);
196165
let expected = [
197-
0x0c, 0x73, 0x66, 0xc5, 0xcb, 0xc6, 0x04, 0x24,
198-
0x0e, 0x66, 0x5e, 0xb0, 0x2a, 0x69, 0x37, 0x2a,
199-
0x7a, 0xf9, 0x79, 0xb2, 0x6f, 0xbb, 0x78, 0x09,
200-
0x2a, 0xc7, 0xc4, 0xb8, 0x80, 0x29, 0xa7, 0xc8,
201-
0x54, 0x51, 0x3b, 0xc2, 0x17, 0xbb, 0xfc, 0x7d,
202-
0x90, 0x43, 0x2e, 0x30, 0x8e, 0xba, 0x15, 0xaf,
203-
0xc6, 0x5a, 0xeb, 0x48, 0xef, 0x10, 0x0d, 0x56,
204-
0x01, 0xe6, 0xaf, 0xba, 0x25, 0x71, 0x17, 0xa9,
166+
0x9a225cdf090f0eef6b0565d596e0512,
167+
0x10dd4d0bff1802930f5d5290278c2449,
168+
0xfefdfe067d7a109ee254a4d9392200a6,
169+
0xc029dc60c972179bf2f944a0eb0f21f0,
170+
0x2a37692ab05e660e2404c6cbc566730c,
171+
0xc8a72980b8c4c72a0978bb6fb279f97a,
172+
0xaf15ba8e302e43907dfcbb17c23b5154,
173+
0xa9177125baafe601560d10ef48eb5ac6,
205174
];
206175
assert_eq!(results, expected);
207176

@@ -210,41 +179,24 @@ mod test {
210179

211180
#[test]
212181
fn test_chacha_true_values_3() {
213-
// Source: Test Vectors for the Stream Cipher ChaCha
214-
// draft-strombergson-chacha-test-vectors-01
215-
// https://datatracker.ietf.org/doc/html/draft-strombergson-chacha-test-vectors-01
182+
// Source: Strombergson 2013, Test Vectors for the Stream Cipher ChaCha
216183
// TC3: all zero key, single bit set in IV, rounds 12, 256-bit key
217184

218185
let seed = [0u8; 32];
219186
let mut rng = StdRng::from_seed(seed);
220187
rng.0.set_stream(1);
221188

222-
let mut results = [0u8; 64];
223-
rng.fill_bytes(&mut results);
224-
#[rustfmt::skip]
225-
let expected = [
226-
0x64, 0xb8, 0xbd, 0xf8, 0x7b, 0x82, 0x8c, 0x4b,
227-
0x6d, 0xba, 0xf7, 0xef, 0x69, 0x8d, 0xe0, 0x3d,
228-
0xf8, 0xb3, 0x3f, 0x63, 0x57, 0x14, 0x41, 0x8f,
229-
0x98, 0x36, 0xad, 0xe5, 0x9b, 0xe1, 0x29, 0x69,
230-
0x46, 0xc9, 0x53, 0xa0, 0xf3, 0x8e, 0xcf, 0xfc,
231-
0x9e, 0xcb, 0x98, 0xe8, 0x1d, 0x5d, 0x99, 0xa5,
232-
0xed, 0xfc, 0x8f, 0x9a, 0x0a, 0x45, 0xb9, 0xe4,
233-
0x1e, 0xf3, 0xb3, 0x1f, 0x02, 0x8f, 0x1d, 0x0f,
234-
];
235-
assert_eq!(results, expected);
236-
237-
rng.fill_bytes(&mut results);
238-
#[rustfmt::skip]
189+
let mut results = [0u128; 8];
190+
rng.fill(&mut results);
239191
let expected = [
240-
0x55, 0x9d, 0xb4, 0xa7, 0xf2, 0x22, 0xc4, 0x42,
241-
0xfe, 0x23, 0xb9, 0xa2, 0x59, 0x6a, 0x88, 0x28,
242-
0x51, 0x22, 0xee, 0x4f, 0x13, 0x63, 0x89, 0x6e,
243-
0xa7, 0x7c, 0xa1, 0x50, 0x91, 0x2a, 0xc7, 0x23,
244-
0xbf, 0xf0, 0x4b, 0x02, 0x6a, 0x2f, 0x80, 0x7e,
245-
0x03, 0xb2, 0x9c, 0x02, 0x07, 0x7d, 0x7b, 0x06,
246-
0xfc, 0x1a, 0xb9, 0x82, 0x7c, 0x13, 0xc8, 0x01,
247-
0x3a, 0x6d, 0x83, 0xbd, 0x3b, 0x52, 0xa2, 0x6f,
192+
0x3de08d69eff7ba6d4b8c827bf8bdb864,
193+
0x6929e19be5ad36988f411457633fb3f8,
194+
0xa5995d1de898cb9efccf8ef3a053c946,
195+
0xf1d8f021fb3f31ee4b9450a9a8ffced,
196+
0x28886a59a2b923fe42c422f2a7b49d55,
197+
0x23c72a9150a17ca76e8963134fee2251,
198+
0x67b7d07029cb2037e802f6a024bf0bf,
199+
0x6fa2523bbd836d3a01c8137c82b91afc,
248200
];
249201
assert_eq!(results, expected);
250202

@@ -253,9 +205,7 @@ mod test {
253205

254206
#[test]
255207
fn test_chacha_true_values_8() {
256-
// Source: Test Vectors for the Stream Cipher ChaCha
257-
// draft-strombergson-chacha-test-vectors-01
258-
// https://datatracker.ietf.org/doc/html/draft-strombergson-chacha-test-vectors-01
208+
// Source: Strombergson 2013, Test Vectors for the Stream Cipher ChaCha
259209
// TC8: key: 'All your base are belong to us!', IV: IETF2013, rounds 12, 256-bit key
260210

261211
#[rustfmt::skip]
@@ -269,32 +219,17 @@ mod test {
269219
let mut rng = StdRng::from_seed(seed);
270220
rng.0.set_stream(u64::from_le_bytes(iv));
271221

272-
let mut results = [0u8; 64];
273-
rng.fill_bytes(&mut results);
274-
#[rustfmt::skip]
275-
let expected = [
276-
0x14, 0x82, 0x07, 0x27, 0x84, 0xbc, 0x6d, 0x06,
277-
0xb4, 0xe7, 0x3b, 0xdc, 0x11, 0x8b, 0xc0, 0x10,
278-
0x3c, 0x79, 0x76, 0x78, 0x6c, 0xa9, 0x18, 0xe0,
279-
0x69, 0x86, 0xaa, 0x25, 0x1f, 0x7e, 0x9c, 0xc1,
280-
0xb2, 0x74, 0x9a, 0x0a, 0x16, 0xee, 0x83, 0xb4,
281-
0x24, 0x2d, 0x2e, 0x99, 0xb0, 0x8d, 0x7c, 0x20,
282-
0x09, 0x2b, 0x80, 0xbc, 0x46, 0x6c, 0x87, 0x28,
283-
0x3b, 0x61, 0xb1, 0xb3, 0x9d, 0x0f, 0xfb, 0xab,
284-
];
285-
assert_eq!(results, expected);
286-
287-
rng.fill_bytes(&mut results);
288-
#[rustfmt::skip]
222+
let mut results = [0u128; 8];
223+
rng.fill(&mut results);
289224
let expected = [
290-
0xd9, 0x4b, 0x11, 0x6b, 0xc1, 0xeb, 0xdb, 0x32,
291-
0x9b, 0x9e, 0x4f, 0x62, 0x0d, 0xb6, 0x95, 0x54,
292-
0x4a, 0x8e, 0x3d, 0x9b, 0x68, 0x47, 0x3d, 0x0c,
293-
0x97, 0x5a, 0x46, 0xad, 0x96, 0x6e, 0xd6, 0x31,
294-
0xe4, 0x2a, 0xff, 0x53, 0x0a, 0xd5, 0xea, 0xc7,
295-
0xd8, 0x04, 0x7a, 0xdf, 0xa1, 0xe5, 0x11, 0x3c,
296-
0x91, 0xf3, 0xe3, 0xb8, 0x83, 0xf1, 0xd1, 0x89,
297-
0xac, 0x1c, 0x8f, 0xe0, 0x7b, 0xa5, 0xa4, 0x2b,
225+
0x10c08b11dc3be7b4066dbc8427078214,
226+
0xc19c7e1f25aa8669e018a96c7876793c,
227+
0x207c8db0992e2d24b483ee160a9a74b2,
228+
0xabfb0f9db3b1613b28876c46bc802b09,
229+
0x5495b60d624f9e9b32dbebc16b114bd9,
230+
0x31d66e96ad465a970c3d47689b3d8e4a,
231+
0x3c11e5a1df7a04d8c7ead50a53ff2ae4,
232+
0x2ba4a57be08f1cac89d1f183b8e3f391,
298233
];
299234
assert_eq!(results, expected);
300235

@@ -303,7 +238,7 @@ mod test {
303238

304239
#[test]
305240
fn test_chacha_counter() {
306-
// Source: none
241+
// Source: rand_chacha implementation
307242
// Test: all zero key and IV, block set to u32::MAX, rounds 12, 256-bit key
308243

309244
let seed = [0u8; 32];
@@ -312,32 +247,17 @@ mod test {
312247
let words_per_block = 16;
313248
rng.0.set_word_pos((block as u128) * words_per_block);
314249

315-
let mut results = [0u8; 64];
316-
rng.fill_bytes(&mut results);
317-
#[rustfmt::skip]
318-
let expected = [
319-
0xd7, 0xa6, 0xaf, 0x50, 0xf1, 0xc9, 0x2a, 0x29,
320-
0x48, 0x42, 0x52, 0xbb, 0xfc, 0xe2, 0x06, 0xf1,
321-
0x7d, 0x01, 0xdd, 0x13, 0x95, 0x30, 0xa3, 0x83,
322-
0x0a, 0xb5, 0x83, 0xc1, 0xf6, 0x2e, 0x03, 0x12,
323-
0x82, 0x93, 0x61, 0xa1, 0x9a, 0x8a, 0x95, 0x6c,
324-
0xed, 0xea, 0x38, 0x04, 0x30, 0xff, 0x93, 0x2c,
325-
0xd0, 0x52, 0xdb, 0x5e, 0x94, 0x77, 0x83, 0x50,
326-
0x58, 0xb8, 0x0a, 0x27, 0x24, 0x06, 0xfc, 0x74,
327-
];
328-
assert_eq!(results, expected);
329-
330-
rng.fill_bytes(&mut results);
331-
#[rustfmt::skip]
250+
let mut results = [0u128; 8];
251+
rng.fill(&mut results);
332252
let expected = [
333-
0xcc, 0x7b, 0x53, 0xdc, 0x11, 0x89, 0x4d, 0x26,
334-
0x24, 0x05, 0x81, 0xb8, 0xa8, 0xf4, 0xf4, 0xe5,
335-
0xaf, 0x40, 0x67, 0x05, 0x80, 0x12, 0x23, 0xb1,
336-
0x3f, 0x82, 0x1f, 0xdc, 0xcb, 0xa6, 0xa6, 0x18,
337-
0x8a, 0x63, 0xf8, 0xd3, 0xdc, 0x83, 0xcc, 0xbc,
338-
0xed, 0x45, 0x1f, 0x4b, 0xa4, 0xe0, 0xda, 0xab,
339-
0x22, 0x8a, 0xbb, 0x0d, 0x74, 0x39, 0xcc, 0x67,
340-
0xe5, 0x0d, 0xf7, 0x12, 0x9f, 0x64, 0x6b, 0xad,
253+
0xf106e2fcbb524248292ac9f150afa6d7,
254+
0x12032ef6c183b50a83a3309513dd017d,
255+
0x2c93ff300438eaed6c958a9aa1619382,
256+
0x74fc0624270ab858508377945edb52d0,
257+
0xe5f4f4a8b8810524264d8911dc537bcc,
258+
0x18a6a6cbdc1f823fb1231280056740af,
259+
0xabdae0a44b1f45edbccc83dcd3f8638a,
260+
0xad6b649f12f70de567cc39740dbb8a22,
341261
];
342262
assert_eq!(results, expected);
343263

0 commit comments

Comments
 (0)