From 3e1e2dedcbf7d2328a16ad70b539e2a73ffca276 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 3 May 2019 13:17:27 +1000 Subject: [PATCH] Use UInt64 instead of [UInt8] where possible. This ensures that we work with intuitive numbers rather than unintuitive arrays. We also might get some compiler optimisations (which are minimal but still) --- LokiKit/LokiKit/ProofOfWork.swift | 38 ++++++++++++++----------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/LokiKit/LokiKit/ProofOfWork.swift b/LokiKit/LokiKit/ProofOfWork.swift index 3c08ca014..58a6f9109 100644 --- a/LokiKit/LokiKit/ProofOfWork.swift +++ b/LokiKit/LokiKit/ProofOfWork.swift @@ -5,25 +5,21 @@ private extension UInt64 { init(_ decimal: Decimal) { self.init(truncating: decimal as NSDecimalNumber) } + + // Convert a UInt8 array to a UInt64 + init(_ bytes: [UInt8]) { + precondition(bytes.count <= MemoryLayout.size) + var value: UInt64 = 0 + for byte in bytes { + value <<= 8 + value |= UInt64(byte) + } + self.init(value) + } } // UInt8 Array specific stuff we need private extension Array where Element == UInt8 { - - // Convert a UInt64 into an array of size 8 - init(_ uint64: UInt64) { - let array = stride(from: 0, to: 64, by: 8).reversed().map { - UInt8(uint64 >> $0 & 0x000000FF) - } - self.init(array) - } - - static func > (lhs: [UInt8], rhs: [UInt8]) -> Bool { - guard lhs.count == rhs.count else { return false } - guard let (lhsElement, rhsElement) = zip(lhs, rhs).first(where: { $0 != $1 }) else { return false } - return lhsElement > rhsElement - } - /// Increment the UInt8 array by a given amount /// /// - Parameter amount: The amount to increment by @@ -91,8 +87,8 @@ public enum ProofOfWork { let payload = config.payload let target = calcTarget(ttl: config.ttl, payloadLength: payload.count, nonceTrials: nonceTrialCount) - // Start with most the max value we can - var trialValue = [UInt8](repeating: UInt8.max, count: nonceLength) + // Start with the max value + var trialValue = UInt64.max let initialHash = payload.sha512() var nonce = [UInt8](repeating: 0, count: nonceLength) @@ -103,14 +99,15 @@ public enum ProofOfWork { // This is different to the bitmessage pow // resultHash = hash(nonce + hash(data)) ==> hash(nonce + initialHash) let resultHash = (nonce + initialHash).sha512() - trialValue = Array(resultHash[0..<8]) + let trialValueArray = Array(resultHash[0..<8]) + trialValue = UInt64(trialValueArray) } return nonce.toBase64() } /// Calculate the UInt8 target we need to reach - private static func calcTarget(ttl: Int, payloadLength: Int, nonceTrials: Int) -> [UInt8] { + private static func calcTarget(ttl: Int, payloadLength: Int, nonceTrials: Int) -> UInt64 { let two16 = UInt64(pow(2, 16) - 1) let two64 = UInt64(pow(2, 64) - 1) @@ -125,8 +122,7 @@ public enum ProofOfWork { let innerFrac = ttlMult / two16 let lenPlusInnerFrac = totalLength + innerFrac let denominator = UInt64(nonceTrials) * lenPlusInnerFrac - let targetNum = two64 / denominator - return [UInt8](targetNum) + return two64 / denominator } }