@@ -87,16 +87,18 @@ impl<M> From<[u8; CHACHA_SEED_LEN]> for ChaCha<M> {
87
87
}
88
88
}
89
89
90
- impl < M : Machine > ChaCha < M > {
90
+ #[ cfg( test) ]
91
+ impl < M > ChaCha < M > {
91
92
/// Utility for setting all bytes when testing.
92
- #[ cfg( test) ]
93
- fn broadcast < const VALUE : u64 > ( & mut self ) {
93
+ pub fn broadcast < const VALUE : u64 > ( & mut self ) {
94
94
self . row_b . u64x2 = [ VALUE , VALUE ] ;
95
95
self . row_c . u64x2 = [ VALUE , VALUE ] ;
96
96
// Tests always expect the counter to start at 0.
97
97
self . row_d . u64x2 = [ 0 , VALUE ] ;
98
98
}
99
+ }
99
100
101
+ impl < M : Machine > ChaCha < M > {
100
102
/// Computes 4 blocks of chacha and fills `buf` with the output.
101
103
///
102
104
/// This is the inline boundary. Everything beneath this should be
@@ -134,9 +136,9 @@ mod tests {
134
136
135
137
#[ test]
136
138
fn correct_constant ( ) {
137
- const EXPECTED : & [ u8 ; 16 ] = b"expand 32-byte k" ;
138
- const ACTUAL : [ u8 ; 16 ] = unsafe { transmute ( ROW_A ) } ;
139
- assert ! ( ACTUAL == * EXPECTED ) ;
139
+ const EXPECTED : [ u8 ; 16 ] = * b"expand 32-byte k" ;
140
+ const ACTUAL : [ u8 ; 16 ] = unsafe { ROW_A . u8x16 } ;
141
+ assert ! ( ACTUAL == EXPECTED ) ;
140
142
}
141
143
142
144
#[ cfg( target_feature = "neon" ) ]
@@ -379,26 +381,12 @@ mod tests {
379
381
assert_blocks_match ( & buf, & KEYSTREAM_BLOCK_0 , & KEYSTREAM_BLOCK_1 ) ;
380
382
}
381
383
382
- /// We're only able to retrieve chacha output in blocks of 4, but we
384
+ /// We always compute chacha output in blocks of 4, but we
383
385
/// only test the first 2 blocks, discarding the rest.
384
- ///
385
- /// Only checking the first 2 is just as good as checking many more,
386
- /// since if our implementation were even slightly incorrect the output
387
- /// would diverge almost instantly. Even more so because we test against
388
- /// multiple keystreams.
389
- fn assert_blocks_match ( buf : & [ u64 ] , block_0 : & [ u8 ] , block_1 : & [ u8 ] ) {
390
- // Sanity checks
391
- assert ! ( buf. len( ) == BUF_LEN ) ;
392
- assert ! ( block_0. len( ) == 64 ) ;
393
- assert ! ( block_1. len( ) == 64 ) ;
394
-
395
- // Reinterpret &[u64] as &[u8]
396
- let buf = unsafe {
397
- let data = buf. as_ptr ( ) . cast :: < u8 > ( ) ;
398
- let len = buf. len ( ) * size_of :: < u64 > ( ) ;
399
- core:: slice:: from_raw_parts ( data, len)
400
- } ;
401
- // Compare chacha output against expected results
386
+ #[ inline]
387
+ fn assert_blocks_match ( buf : & [ u64 ; BUF_LEN ] , block_0 : & [ u8 ] , block_1 : & [ u8 ] ) {
388
+ const LEN : usize = size_of :: < [ u64 ; BUF_LEN ] > ( ) ;
389
+ let buf: & [ u8 ; LEN ] = unsafe { transmute ( buf) } ;
402
390
assert ! ( buf[ ..64 ] == * block_0) ;
403
391
assert ! ( buf[ 64 ..128 ] == * block_1) ;
404
392
}
0 commit comments