From 0a64100ba7abe732f338caf9396c0609db439c00 Mon Sep 17 00:00:00 2001 From: nixberg <310825+nixberg@users.noreply.github.com> Date: Sat, 11 May 2024 13:50:10 +0200 Subject: [PATCH] Update BinaryEncoding.swift --- Sources/ChaCha8Rand/BinaryEncoding.swift | 34 +++++++++++------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/Sources/ChaCha8Rand/BinaryEncoding.swift b/Sources/ChaCha8Rand/BinaryEncoding.swift index c4ac252..a21ca49 100644 --- a/Sources/ChaCha8Rand/BinaryEncoding.swift +++ b/Sources/ChaCha8Rand/BinaryEncoding.swift @@ -23,18 +23,15 @@ extension ChaCha8Rand { endianess: .big ).quotientAndRemainder(dividingBy: 32) - let seed = ContiguousArray(unsafeUninitializedCapacity: 8) { seed, count in - seed.initialize(repeating: 0) - var offset = 16 - for index in seed.indices { - seed[index] = buffer.loadUnaligned( - fromByteOffset: offset, - as: UInt32.self, - endianess: .little - ) - offset &+= 4 - } - count = 8 + var seed = ContiguousArray() + seed.reserveCapacity(8) + + for offset in stride(from: 16, to: 48, by: 4) { + seed.append(buffer.loadUnaligned( + fromByteOffset: offset, + as: UInt32.self, + endianess: .little + )) } self.init(seed: seed, counter: 4 * UInt32(counter), index: Int(index)) @@ -43,15 +40,16 @@ extension ChaCha8Rand { public func encode(into buffer: UnsafeMutableRawBufferPointer) { precondition(buffer.count == 48, "TODO") - buffer.storeBytes(of: Self.header, endianess: .little, toByteOffset: 00) + buffer.storeBytes(of: Self.header, endianess: .little) - let used = UInt64(counter / 4) * 32 + UInt64(index) - buffer.storeBytes(of: used, endianess: .big, toByteOffset: 08) + buffer.storeBytes( + of: UInt64(counter / 4) * 32 + UInt64(index), + endianess: .big, + toByteOffset: 08 + ) - var offset = 16 - for word in seed { + for (word, offset) in zip(seed, stride(from: 16, to: 48, by: 4)) { buffer.storeBytes(of: word, endianess: .little, toByteOffset: offset) - offset &+= 4 } }