@@ -22,6 +22,7 @@ import TestsUtils
22
22
enum ChaCha20 { }
23
23
24
24
extension ChaCha20 {
25
+ @inline ( never)
25
26
public static func encrypt< Key: Collection , Nonce: Collection , Bytes: MutableCollection > ( bytes: inout Bytes , key: Key , nonce: Nonce , initialCounter: UInt32 = 0 ) where Bytes. Element == UInt8 , Key. Element == UInt8 , Nonce. Element == UInt8 {
26
27
var baseState = ChaChaState ( key: key, nonce: nonce, counter: initialCounter)
27
28
var index = bytes. startIndex
@@ -347,13 +348,28 @@ public let ChaCha = BenchmarkInfo(
347
348
runFunction: run_ChaCha,
348
349
tags: [ . runtime, . cpubench] )
349
350
351
+ @inline ( never)
352
+ func checkResult( _ plaintext: [ UInt8 ] ) {
353
+ CheckResults ( plaintext. first! == 6 && plaintext. last! == 254 )
354
+ var hash : UInt64 = 0
355
+ for byte in plaintext {
356
+ // rotate
357
+ hash = ( hash &<< 8 ) | ( hash &>> ( 64 - 8 ) )
358
+ hash ^= UInt64 ( byte)
359
+ }
360
+ CheckResults ( hash == 0xa1bcdb217d8d14e4 )
361
+ }
350
362
351
363
@inline ( never)
352
364
public func run_ChaCha( _ N: Int ) {
353
- var plaintext = Array ( repeating: UInt8 ( 0 ) , count: 30720 ) // Chosen for CI runtime
354
365
let key = Array ( repeating: UInt8 ( 1 ) , count: 32 )
355
366
let nonce = Array ( repeating: UInt8 ( 2 ) , count: 12 )
356
367
368
+ var checkedtext = Array ( repeating: UInt8 ( 0 ) , count: 1024 )
369
+ ChaCha20 . encrypt ( bytes: & checkedtext, key: key, nonce: nonce)
370
+ checkResult ( checkedtext)
371
+
372
+ var plaintext = Array ( repeating: UInt8 ( 0 ) , count: 30720 ) // Chosen for CI runtime
357
373
for _ in 1 ... N {
358
374
ChaCha20 . encrypt ( bytes: & plaintext, key: key, nonce: nonce)
359
375
blackHole ( plaintext. first!)
0 commit comments