Debian Codegraph @

third_party/rust/prio/src/vdaf/xof.rs

raw
1 // SPDX-License-Identifier: MPL-2.0
2
3 //! Implementations of XOFs specified in [[draft-irtf-cfrg-vdaf-08]].
4 //!
5 //! [draft-irtf-cfrg-vdaf-08]: https://datatracker.ietf.org/doc/draft-irtf-cfrg-vdaf/08/
6
7 /// Value of the domain separation byte "D" used by XofTurboShake128 when invoking TurboSHAKE128.
8 const XOF_TURBO_SHAKE_128_DOMAIN_SEPARATION: u8 = 1;
9 /// Value of the domain separation byte "D" used by XofFixedKeyAes128 when invoking TurboSHAKE128.
10 #[cfg(all(feature = "crypto-dependencies", feature = "experimental"))]
11 const XOF_FIXED_KEY_AES_128_DOMAIN_SEPARATION: u8 = 2;
12
13 use crate::{
14 field::FieldElement,
15 prng::Prng,
16 vdaf::{CodecError, Decode, Encode},
17 };
18 #[cfg(all(feature = "crypto-dependencies", feature = "experimental"))]
19 use aes::{
20 cipher::{generic_array::GenericArray, BlockEncrypt, KeyInit},
21 Block,
22 };
23 #[cfg(feature = "crypto-dependencies")]
24 use aes::{
25 cipher::{KeyIvInit, StreamCipher},
26 Aes128,
27 };
28 #[cfg(feature = "crypto-dependencies")]
29 use ctr::Ctr64BE;
30 #[cfg(feature = "crypto-dependencies")]
31 use hmac::{Hmac, Mac};
32 use rand_core::{
33 impls::{next_u32_via_fill, next_u64_via_fill},
34 RngCore, SeedableRng,
35 };
36 #[cfg(feature = "crypto-dependencies")]
37 use sha2::Sha256;
38 use sha3::{
39 digest::{ExtendableOutput, Update, XofReader},
40 TurboShake128, TurboShake128Core, TurboShake128Reader,
41 };
42 #[cfg(feature = "crypto-dependencies")]
43 use std::fmt::Formatter;
44 use std::{
45 fmt::Debug,
46 io::{Cursor, Read},
47 };
48 use subtle::{Choice, ConstantTimeEq};
49
50 /// Input of [`Xof`].
51 #[derive(Clone, Debug)]
52 pub struct Seed<const SEED_SIZE: usize>(pub(crate) [u8; SEED_SIZE]);
53
54 impl<const SEED_SIZE: usize> Seed<SEED_SIZE> {
55 /// Generate a uniform random seed.
56 pub fn generate() -> Result<Self, getrandom::Error> {
57 let mut seed = [0; SEED_SIZE];
58 getrandom::getrandom(&mut seed)?;
59 Ok(Self::from_bytes(seed))
60 }
61
62 /// Construct seed from a byte slice.
63 pub(crate) fn from_bytes(seed: [u8; SEED_SIZE]) -> Self {
64 Self(seed)
65 }
66 }
67
68 impl<const SEED_SIZE: usize> AsRef<[u8; SEED_SIZE]> for Seed<SEED_SIZE> {
69 fn as_ref(&self) -> &[u8; SEED_SIZE] {
70 &self.0
71 }
72 }
73
74 impl<const SEED_SIZE: usize> PartialEq for Seed<SEED_SIZE> {
75 fn eq(&self, other: &Self) -> bool {
76 self.ct_eq(other).into()
77 }
78 }
79
80 impl<const SEED_SIZE: usize> Eq for Seed<SEED_SIZE> {}
81
82 impl<const SEED_SIZE: usize> ConstantTimeEq for Seed<SEED_SIZE> {
83 fn ct_eq(&self, other: &Self) -> Choice {
84 self.0.ct_eq(&other.0)
85 }
86 }
87
88 impl<const SEED_SIZE: usize> Encode for Seed<SEED_SIZE> {
89 fn encode(&self, bytes: &mut Vec<u8>) -> Result<(), CodecError> {
90 bytes.extend_from_slice(&self.0[..]);
91 Ok(())
92 }
93
94 fn encoded_len(&self) -> Option<usize> {
95 Some(SEED_SIZE)
96 }
97 }
98
99 impl<const SEED_SIZE: usize> Decode for Seed<SEED_SIZE> {
100 fn decode(bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> {
101 let mut seed = [0; SEED_SIZE];
102 bytes.read_exact(&mut seed)?;
103 Ok(Seed(seed))
104 }
105 }
106
107 /// Trait for deriving a vector of field elements.
108 pub trait IntoFieldVec: RngCore + Sized {
109 /// Generate a finite field vector from the seed stream.
110 fn into_field_vec<F: FieldElement>(self, length: usize) -> Vec<F>;
111 }
112
113 impl<S: RngCore> IntoFieldVec for S {
114 fn into_field_vec<F: FieldElement>(self, length: usize) -> Vec<F> {
115 Prng::from_seed_stream(self).take(length).collect()
116 }
117 }
118
119 /// An extendable output function (XOF) with the interface specified in [[draft-irtf-cfrg-vdaf-08]].
120 ///
121 /// [draft-irtf-cfrg-vdaf-08]: https://datatracker.ietf.org/doc/draft-irtf-cfrg-vdaf/08/
122 pub trait Xof<const SEED_SIZE: usize>: Clone + Debug {
123 /// The type of stream produced by this XOF.
124 type SeedStream: RngCore + Sized;
125
126 /// Construct an instance of [`Xof`] with the given seed.
127 fn init(seed_bytes: &[u8; SEED_SIZE], dst: &[u8]) -> Self;
128
129 /// Update the XOF state by passing in the next fragment of the info string. The final info
130 /// string is assembled from the concatenation of sequence of fragments passed to this method.
131 fn update(&mut self, data: &[u8]);
132
133 /// Finalize the XOF state, producing a seed stream.
134 fn into_seed_stream(self) -> Self::SeedStream;
135
136 /// Finalize the XOF state, producing a seed.
137 fn into_seed(self) -> Seed<SEED_SIZE> {
138 let mut new_seed = [0; SEED_SIZE];
139 let mut seed_stream = self.into_seed_stream();
140 seed_stream.fill_bytes(&mut new_seed);
141 Seed(new_seed)
142 }
143
144 /// Construct a seed stream from the given seed and info string.
145 fn seed_stream(seed: &Seed<SEED_SIZE>, dst: &[u8], binder: &[u8]) -> Self::SeedStream {
146 let mut xof = Self::init(seed.as_ref(), dst);
147 xof.update(binder);
148 xof.into_seed_stream()
149 }
150 }
151
152 /// The key stream produced by AES128 in CTR-mode.
153 #[cfg(feature = "crypto-dependencies")]
154 #[cfg_attr(docsrs, doc(cfg(feature = "crypto-dependencies")))]
155 pub struct SeedStreamAes128(Ctr64BE<Aes128>);
156
157 #[cfg(feature = "crypto-dependencies")]
158 impl SeedStreamAes128 {
159 /// Construct an instance of the seed stream with the given AES key `key` and initialization
160 /// vector `iv`.
161 pub fn new(key: &[u8], iv: &[u8]) -> Self {
162 SeedStreamAes128(<Ctr64BE<Aes128> as KeyIvInit>::new(key.into(), iv.into()))
163 }
164
165 fn fill(&mut self, buf: &mut [u8]) {
166 buf.fill(0);
167 self.0.apply_keystream(buf);
168 }
169 }
170
171 #[cfg(feature = "crypto-dependencies")]
172 impl RngCore for SeedStreamAes128 {
173 fn fill_bytes(&mut self, dest: &mut [u8]) {
174 self.fill(dest);
175 }
176
177 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
178 self.fill(dest);
179 Ok(())
180 }
181
182 fn next_u32(&mut self) -> u32 {
183 next_u32_via_fill(self)
184 }
185
186 fn next_u64(&mut self) -> u64 {
187 next_u64_via_fill(self)
188 }
189 }
190
191 #[cfg(feature = "crypto-dependencies")]
192 impl Debug for SeedStreamAes128 {
193 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
194 // Ctr64BE<Aes128> does not implement Debug, but [`ctr::CtrCore`][1] does, and we get that
195 // with [`cipher::StreamCipherCoreWrapper::get_core`][2].
196 //
197 // [1]: https://docs.rs/ctr/latest/ctr/struct.CtrCore.html
198 // [2]: https://docs.rs/cipher/latest/cipher/struct.StreamCipherCoreWrapper.html
199 self.0.get_core().fmt(f)
200 }
201 }
202
203 /// The XOF based on TurboSHAKE128 as specified in [[draft-irtf-cfrg-vdaf-08]].
204 ///
205 /// [draft-irtf-cfrg-vdaf-08]: https://datatracker.ietf.org/doc/draft-irtf-cfrg-vdaf/08/
206 #[derive(Clone, Debug)]
207 pub struct XofTurboShake128(TurboShake128);
208
209 impl Xof<16> for XofTurboShake128 {
210 type SeedStream = SeedStreamTurboShake128;
211
212 fn init(seed_bytes: &[u8; 16], dst: &[u8]) -> Self {
213 let mut xof = Self(TurboShake128::from_core(TurboShake128Core::new(
214 XOF_TURBO_SHAKE_128_DOMAIN_SEPARATION,
215 )));
216 Update::update(
217 &mut xof.0,
218 &[dst.len().try_into().expect("dst must be at most 255 bytes")],
219 );
220 Update::update(&mut xof.0, dst);
221 Update::update(&mut xof.0, seed_bytes);
222 xof
223 }
224
225 fn update(&mut self, data: &[u8]) {
226 Update::update(&mut self.0, data);
227 }
228
229 fn into_seed_stream(self) -> SeedStreamTurboShake128 {
230 SeedStreamTurboShake128::new(self.0.finalize_xof())
231 }
232 }
233
234 /// The seed stream produced by TurboSHAKE128.
235 pub struct SeedStreamTurboShake128(TurboShake128Reader);
236
237 impl SeedStreamTurboShake128 {
238 pub(crate) fn new(reader: TurboShake128Reader) -> Self {
239 Self(reader)
240 }
241 }
242
243 impl RngCore for SeedStreamTurboShake128 {
244 fn fill_bytes(&mut self, dest: &mut [u8]) {
245 XofReader::read(&mut self.0, dest);
246 }
247
248 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
249 XofReader::read(&mut self.0, dest);
250 Ok(())
251 }
252
253 fn next_u32(&mut self) -> u32 {
254 next_u32_via_fill(self)
255 }
256
257 fn next_u64(&mut self) -> u64 {
258 next_u64_via_fill(self)
259 }
260 }
261
262 /// A `rand`-compatible interface to construct XofTurboShake128 seed streams, with the domain
263 /// separation tag and binder string both fixed as the empty string.
264 impl SeedableRng for SeedStreamTurboShake128 {
265 type Seed = [u8; 16];
266
267 fn from_seed(seed: Self::Seed) -> Self {
268 XofTurboShake128::init(&seed, b"").into_seed_stream()
269 }
270 }
271
272 /// Factory to produce multiple [`XofFixedKeyAes128`] instances with the same fixed key and
273 /// different seeds.
274 #[cfg(all(feature = "crypto-dependencies", feature = "experimental"))]
275 #[cfg_attr(
276 docsrs,
277 doc(cfg(all(feature = "crypto-dependencies", feature = "experimental")))
278 )]
279 pub struct XofFixedKeyAes128Key {
280 cipher: Aes128,
281 }
282
283 #[cfg(all(feature = "crypto-dependencies", feature = "experimental"))]
284 impl XofFixedKeyAes128Key {
285 /// Derive the fixed key from the domain separation tag and binder string.
286 pub fn new(dst: &[u8], binder: &[u8]) -> Self {
287 let mut fixed_key_deriver = TurboShake128::from_core(TurboShake128Core::new(
288 XOF_FIXED_KEY_AES_128_DOMAIN_SEPARATION,
289 ));
290 Update::update(
291 &mut fixed_key_deriver,
292 &[dst.len().try_into().expect("dst must be at most 255 bytes")],
293 );
294 Update::update(&mut fixed_key_deriver, dst);
295 Update::update(&mut fixed_key_deriver, binder);
296 let mut key = GenericArray::from([0; 16]);
297 XofReader::read(&mut fixed_key_deriver.finalize_xof(), key.as_mut());
298 Self {
299 cipher: Aes128::new(&key),
300 }
301 }
302
303 /// Combine a fixed key with a seed to produce a new stream of bytes.
304 pub fn with_seed(&self, seed: &[u8; 16]) -> SeedStreamFixedKeyAes128 {
305 SeedStreamFixedKeyAes128 {
306 cipher: self.cipher.clone(),
307 base_block: (*seed).into(),
308 length_consumed: 0,
309 }
310 }
311 }
312
313 /// XofFixedKeyAes128 as specified in [[draft-irtf-cfrg-vdaf-08]]. This XOF is NOT RECOMMENDED for
314 /// general use; see Section 9 ("Security Considerations") for details.
315 ///
316 /// This XOF combines TurboSHAKE128 and a fixed-key mode of operation for AES-128. The key is
317 /// "fixed" in the sense that it is derived (using TurboSHAKE128) from the domain separation tag and
318 /// binder strings, and depending on the application, these strings can be hard-coded. The seed is
319 /// used to construct each block of input passed to a hash function built from AES-128.
320 ///
321 /// [draft-irtf-cfrg-vdaf-08]: https://datatracker.ietf.org/doc/draft-irtf-cfrg-vdaf/08/
322 #[derive(Clone, Debug)]
323 #[cfg(all(feature = "crypto-dependencies", feature = "experimental"))]
324 #[cfg_attr(
325 docsrs,
326 doc(cfg(all(feature = "crypto-dependencies", feature = "experimental")))
327 )]
328 pub struct XofFixedKeyAes128 {
329 fixed_key_deriver: TurboShake128,
330 base_block: Block,
331 }
332
333 #[cfg(all(feature = "crypto-dependencies", feature = "experimental"))]
334 impl Xof<16> for XofFixedKeyAes128 {
335 type SeedStream = SeedStreamFixedKeyAes128;
336
337 fn init(seed_bytes: &[u8; 16], dst: &[u8]) -> Self {
338 let mut fixed_key_deriver = TurboShake128::from_core(TurboShake128Core::new(2u8));
339 Update::update(
340 &mut fixed_key_deriver,
341 &[dst.len().try_into().expect("dst must be at most 255 bytes")],
342 );
343 Update::update(&mut fixed_key_deriver, dst);
344 Self {
345 fixed_key_deriver,
346 base_block: (*seed_bytes).into(),
347 }
348 }
349
350 fn update(&mut self, data: &[u8]) {
351 Update::update(&mut self.fixed_key_deriver, data);
352 }
353
354 fn into_seed_stream(self) -> SeedStreamFixedKeyAes128 {
355 let mut fixed_key = GenericArray::from([0; 16]);
356 XofReader::read(
357 &mut self.fixed_key_deriver.finalize_xof(),
358 fixed_key.as_mut(),
359 );
360 SeedStreamFixedKeyAes128 {
361 base_block: self.base_block,
362 cipher: Aes128::new(&fixed_key),
363 length_consumed: 0,
364 }
365 }
366 }
367
368 /// Seed stream for [`XofFixedKeyAes128`].
369 #[cfg(all(feature = "crypto-dependencies", feature = "experimental"))]
370 #[cfg_attr(
371 docsrs,
372 doc(cfg(all(feature = "crypto-dependencies", feature = "experimental")))
373 )]
374 pub struct SeedStreamFixedKeyAes128 {
375 cipher: Aes128,
376 base_block: Block,
377 length_consumed: u64,
378 }
379
380 #[cfg(all(feature = "crypto-dependencies", feature = "experimental"))]
381 impl SeedStreamFixedKeyAes128 {
382 fn hash_block(&self, block: &mut Block) {
383 let sigma = Block::from([
384 // hi
385 block[8],
386 block[9],
387 block[10],
388 block[11],
389 block[12],
390 block[13],
391 block[14],
392 block[15],
393 // xor(hi, lo)
394 block[8] ^ block[0],
395 block[9] ^ block[1],
396 block[10] ^ block[2],
397 block[11] ^ block[3],
398 block[12] ^ block[4],
399 block[13] ^ block[5],
400 block[14] ^ block[6],
401 block[15] ^ block[7],
402 ]);
403 self.cipher.encrypt_block_b2b(&sigma, block);
404 for (b, s) in block.iter_mut().zip(sigma.iter()) {
405 *b ^= s;
406 }
407 }
408
409 fn fill(&mut self, buf: &mut [u8]) {
410 let next_length_consumed = self.length_consumed + u64::try_from(buf.len()).unwrap();
411 let mut offset = usize::try_from(self.length_consumed % 16).unwrap();
412 let mut index = 0;
413 let mut block = Block::from([0; 16]);
414
415 // NOTE(cjpatton) We might be able to speed this up by unrolling this loop and encrypting
416 // multiple blocks at the same time via `self.cipher.encrypt_blocks()`.
417 for block_counter in self.length_consumed / 16..(next_length_consumed + 15) / 16 {
418 block.clone_from(&self.base_block);
419 for (b, i) in block.iter_mut().zip(block_counter.to_le_bytes().iter()) {
420 *b ^= i;
421 }
422 self.hash_block(&mut block);
423 let read = std::cmp::min(16 - offset, buf.len() - index);
424 buf[index..index + read].copy_from_slice(&block[offset..offset + read]);
425 offset = 0;
426 index += read;
427 }
428
429 self.length_consumed = next_length_consumed;
430 }
431 }
432
433 #[cfg(all(feature = "crypto-dependencies", feature = "experimental"))]
434 impl RngCore for SeedStreamFixedKeyAes128 {
435 fn fill_bytes(&mut self, dest: &mut [u8]) {
436 self.fill(dest);
437 }
438
439 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
440 self.fill(dest);
441 Ok(())
442 }
443
444 fn next_u32(&mut self) -> u32 {
445 next_u32_via_fill(self)
446 }
447
448 fn next_u64(&mut self) -> u64 {
449 next_u64_via_fill(self)
450 }
451 }
452
453 /// XOF based on HMAC-SHA256 and AES128. This XOF is not part of the VDAF spec.
454 #[cfg(feature = "crypto-dependencies")]
455 #[cfg_attr(docsrs, doc(cfg(feature = "crypto-dependencies")))]
456 #[derive(Clone, Debug)]
457 pub struct XofHmacSha256Aes128(Hmac<Sha256>);
458
459 #[cfg(feature = "crypto-dependencies")]
460 impl Xof<32> for XofHmacSha256Aes128 {
461 type SeedStream = SeedStreamAes128;
462
463 fn init(seed_bytes: &[u8; 32], dst: &[u8]) -> Self {
464 let mut mac = <Hmac<Sha256> as Mac>::new_from_slice(seed_bytes).unwrap();
465 Mac::update(
466 &mut mac,
467 &[dst.len().try_into().expect("dst must be at most 255 bytes")],
468 );
469 Mac::update(&mut mac, dst);
470 Self(mac)
471 }
472
473 fn update(&mut self, data: &[u8]) {
474 Mac::update(&mut self.0, data);
475 }
476
477 fn into_seed_stream(self) -> SeedStreamAes128 {
478 let tag = Mac::finalize(self.0).into_bytes();
479 let (key, iv) = tag.split_at(16);
480 SeedStreamAes128::new(key, iv)
481 }
482 }
483
484 #[cfg(test)]
485 mod tests {
486 use super::*;
487 use crate::{field::Field128, vdaf::equality_comparison_test};
488 use serde::{Deserialize, Serialize};
489 use std::{convert::TryInto, io::Cursor};
490
491 #[derive(Deserialize, Serialize)]
492 struct XofTestVector {
493 #[serde(with = "hex")]
494 seed: Vec<u8>,
495 #[serde(with = "hex")]
496 dst: Vec<u8>,
497 #[serde(with = "hex")]
498 binder: Vec<u8>,
499 length: usize,
500 #[serde(with = "hex")]
501 derived_seed: Vec<u8>,
502 #[serde(with = "hex")]
503 expanded_vec_field128: Vec<u8>,
504 }
505
506 // Test correctness of dervied methods.
507 fn test_xof<P, const SEED_SIZE: usize>()
508 where
509 P: Xof<SEED_SIZE>,
510 {
511 let seed = Seed::generate().unwrap();
512 let dst = b"algorithm and usage";
513 let binder = b"bind to artifact";
514
515 let mut xof = P::init(seed.as_ref(), dst);
516 xof.update(binder);
517
518 let mut want = Seed([0; SEED_SIZE]);
519 xof.clone().into_seed_stream().fill_bytes(&mut want.0[..]);
520 let got = xof.clone().into_seed();
521 assert_eq!(got, want);
522
523 let mut want = [0; 45];
524 xof.clone().into_seed_stream().fill_bytes(&mut want);
525 let mut got = [0; 45];
526 P::seed_stream(&seed, dst, binder).fill_bytes(&mut got);
527 assert_eq!(got, want);
528 }
529
530 #[test]
531 fn xof_turboshake128() {
532 let t: XofTestVector =
533 serde_json::from_str(include_str!("test_vec/08/XofTurboShake128.json")).unwrap();
534 let mut xof = XofTurboShake128::init(&t.seed.try_into().unwrap(), &t.dst);
535 xof.update(&t.binder);
536
537 assert_eq!(
538 xof.clone().into_seed(),
539 Seed(t.derived_seed.try_into().unwrap())
540 );
541
542 let mut bytes = Cursor::new(t.expanded_vec_field128.as_slice());
543 let mut want = Vec::with_capacity(t.length);
544 while (bytes.position() as usize) < t.expanded_vec_field128.len() {
545 want.push(Field128::decode(&mut bytes).unwrap())
546 }
547 let got: Vec<Field128> = xof.clone().into_seed_stream().into_field_vec(t.length);
548 assert_eq!(got, want);
549
550 test_xof::<XofTurboShake128, 16>();
551 }
552
553 #[test]
554 fn xof_hmac_sha256_aes128() {
555 let t: XofTestVector =
556 serde_json::from_str(include_str!("test_vec/XofHmacSha256Aes128.json")).unwrap();
557
558 let mut xof = XofHmacSha256Aes128::init(&t.seed.try_into().unwrap(), &t.dst);
559 xof.update(&t.binder);
560
561 assert_eq!(
562 xof.clone().into_seed(),
563 Seed(t.derived_seed.try_into().unwrap())
564 );
565
566 let mut bytes = Cursor::new(t.expanded_vec_field128.as_slice());
567 let mut want = Vec::with_capacity(t.length);
568 while (bytes.position() as usize) < t.expanded_vec_field128.len() {
569 want.push(Field128::decode(&mut bytes).unwrap())
570 }
571 let got: Vec<Field128> = xof.clone().into_seed_stream().into_field_vec(t.length);
572 assert_eq!(got, want);
573
574 test_xof::<XofHmacSha256Aes128, 32>();
575 }
576
577 #[cfg(feature = "experimental")]
578 #[test]
579 fn xof_fixed_key_aes128() {
580 let t: XofTestVector =
581 serde_json::from_str(include_str!("test_vec/08/XofFixedKeyAes128.json")).unwrap();
582 let mut xof = XofFixedKeyAes128::init(&t.seed.try_into().unwrap(), &t.dst);
583 xof.update(&t.binder);
584
585 assert_eq!(
586 xof.clone().into_seed(),
587 Seed(t.derived_seed.try_into().unwrap())
588 );
589
590 let mut bytes = Cursor::new(t.expanded_vec_field128.as_slice());
591 let mut want = Vec::with_capacity(t.length);
592 while (bytes.position() as usize) < t.expanded_vec_field128.len() {
593 want.push(Field128::decode(&mut bytes).unwrap())
594 }
595 let got: Vec<Field128> = xof.clone().into_seed_stream().into_field_vec(t.length);
596 assert_eq!(got, want);
597
598 test_xof::<XofFixedKeyAes128, 16>();
599 }
600
601 #[cfg(feature = "experimental")]
602 #[test]
603 fn xof_fixed_key_aes128_incomplete_block() {
604 let seed = Seed::generate().unwrap();
605 let mut expected = [0; 32];
606 XofFixedKeyAes128::seed_stream(&seed, b"dst", b"binder").fill(&mut expected);
607
608 for len in 0..=32 {
609 let mut buf = vec![0; len];
610 XofFixedKeyAes128::seed_stream(&seed, b"dst", b"binder").fill(&mut buf);
611 assert_eq!(buf, &expected[..len]);
612 }
613 }
614
615 #[cfg(feature = "experimental")]
616 #[test]
617 fn xof_fixed_key_aes128_alternate_apis() {
618 let dst = b"domain separation tag";
619 let binder = b"AAAAAAAAAAAAAAAAAAAAAAAA";
620 let seed_1 = Seed::generate().unwrap();
621 let seed_2 = Seed::generate().unwrap();
622
623 let mut stream_1_trait_api = XofFixedKeyAes128::seed_stream(&seed_1, dst, binder);
624 let mut output_1_trait_api = [0u8; 32];
625 stream_1_trait_api.fill(&mut output_1_trait_api);
626 let mut stream_2_trait_api = XofFixedKeyAes128::seed_stream(&seed_2, dst, binder);
627 let mut output_2_trait_api = [0u8; 32];
628 stream_2_trait_api.fill(&mut output_2_trait_api);
629
630 let fixed_key = XofFixedKeyAes128Key::new(dst, binder);
631 let mut stream_1_alternate_api = fixed_key.with_seed(seed_1.as_ref());
632 let mut output_1_alternate_api = [0u8; 32];
633 stream_1_alternate_api.fill(&mut output_1_alternate_api);
634 let mut stream_2_alternate_api = fixed_key.with_seed(seed_2.as_ref());
635 let mut output_2_alternate_api = [0u8; 32];
636 stream_2_alternate_api.fill(&mut output_2_alternate_api);
637
638 assert_eq!(output_1_trait_api, output_1_alternate_api);
639 assert_eq!(output_2_trait_api, output_2_alternate_api);
640 }
641
642 #[test]
643 fn seed_equality_test() {
644 equality_comparison_test(&[Seed([1, 2, 3]), Seed([3, 2, 1])])
645 }
646 }