Explorar o código

Meaningless performance improvements

Marcin Krzyżanowski %!s(int64=10) %!d(string=hai) anos
pai
achega
54045529cf
Modificáronse 4 ficheiros con 28 adicións e 15 borrados
  1. 14 12
      CryptoSwift/AES.swift
  2. 2 2
      CryptoSwift/PKCS7.swift
  3. 11 0
      CryptoSwift/Utils.swift
  4. 1 1
      CryptoSwiftTests/AESTests.swift

+ 14 - 12
CryptoSwift/AES.swift

@@ -143,8 +143,8 @@ public class AES {
             assert(false, "AES 128-bit block exceeded!");
             return nil
         }
-        
-        let blocks = finalBytes.chunks(AES.blockSize)
+
+        let blocks = finalBytes.chunks(AES.blockSize) // 0.34
         return blockMode.encryptBlocks(blocks, iv: self.iv, cipherOperation: encryptBlock)
     }
     
@@ -256,9 +256,9 @@ public class AES {
             }
         }
         
-        var tmp = [UInt8](count: 4, repeatedValue: 0)
+        var tmp:[UInt8]
         for (var i = variant.Nk; i < variant.Nb * (variant.Nr + 1); i++) {
-            tmp = tmp.map({ val in 0});
+            tmp = [UInt8](count: 4, repeatedValue: 0)
             
             for wordIdx in 0..<4 {
                 tmp[wordIdx] = w[4*(i-1)+wordIdx]
@@ -342,7 +342,7 @@ extension AES {
     }
     
     public func matrixMultiplyPolys(matrix:[[UInt8]], _ array:[UInt8]) -> [UInt8] {
-        var returnArray:[UInt8] = array.map({ _ in return 0 })
+        var returnArray:[UInt8] = [UInt8](count: array.count, repeatedValue: 0)
         for (i, row) in enumerate(matrix) {
             for (j, boxVal) in enumerate(row) {
                 returnArray[i] = multiplyPolys(boxVal, array[j]) ^ returnArray[i]
@@ -352,11 +352,14 @@ extension AES {
     }
 
     public func addRoundKey(state:[[UInt8]], _ expandedKeyW:[UInt8], _ round:Int) -> [[UInt8]] {
-        var newState = state.map({ val -> [UInt8] in return val.map { _ in return 0 } })
+        var newState = [[UInt8]](count: state.count, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
+        let idxRow = 4*variant.Nb*round
         for c in 0..<variant.Nb {
-            for i in 0..<4 {
-                newState[i][c] = state[i][c] ^ expandedKeyW[(4*variant.Nb*round)+(variant.Nb*c)+i]
-            }
+            let idxCol = variant.Nb*c
+            newState[0][c] = state[0][c] ^ expandedKeyW[idxRow+idxCol+0]
+            newState[1][c] = state[1][c] ^ expandedKeyW[idxRow+idxCol+1]
+            newState[2][c] = state[2][c] ^ expandedKeyW[idxRow+idxCol+2]
+            newState[3][c] = state[3][c] ^ expandedKeyW[idxRow+idxCol+3]
         }
         return newState
     }
@@ -366,7 +369,8 @@ extension AES {
         var state = state
         var colBox:[[UInt8]] = [[2,3,1,1],[1,2,3,1],[1,1,2,3],[3,1,1,2]]
         
-        var rowMajorState = state.map({ val -> [UInt8] in return val.map { _ in return 0 } }) // zeroing
+        var rowMajorState = [[UInt8]](count: state.count, repeatedValue: [UInt8](count: state.first!.count, repeatedValue: 0)) //state.map({ val -> [UInt8] in return val.map { _ in return 0 } }) // zeroing
+        var newRowMajorState = rowMajorState
         
         for i in 0..<state.count {
             for j in 0..<state[0].count {
@@ -374,8 +378,6 @@ extension AES {
             }
         }
         
-        var newRowMajorState = state.map({ val -> [UInt8] in return val.map { _ in return 0 } })
-        
         for (i, row) in enumerate(rowMajorState) {
             newRowMajorState[i] = matrixMultiplyPolys(colBox, row)
         }

+ 2 - 2
CryptoSwift/PKCS7.swift

@@ -23,12 +23,12 @@ public struct PKCS7: Padding {
         if (padding == 0) {
             // If the original data is a multiple of N bytes, then an extra block of bytes with value N is added.
             for i in 0..<blockSize {
-                withPadding += [UInt8(blockSize)]
+                withPadding.extend([UInt8(blockSize)])
             }
         } else {
             // The value of each added byte is the number of bytes that are added
             for i in 0..<padding {
-                withPadding += [UInt8(padding)]
+                withPadding.extend([UInt8(padding)])
             }
         }
         return withPadding

+ 11 - 0
CryptoSwift/Utils.swift

@@ -48,4 +48,15 @@ func xor(a: [UInt8], b:[UInt8]) -> [UInt8] {
         xored[i] = a[i] ^ b[i]
     }
     return xored
+}
+
+func perf(text: String, closure: () -> ()) {
+    let measurementStart = NSDate();
+    
+    closure()
+    
+    let measurementStop = NSDate();
+    let executionTime = measurementStop.timeIntervalSinceDate(measurementStart)
+    
+    println("\(text) \(executionTime)");
 }

+ 1 - 1
CryptoSwiftTests/AESTests.swift

@@ -181,7 +181,7 @@ class AESTests: XCTestCase {
         let message = [UInt8](count: 1024 * 1024, repeatedValue: 7)
         self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, forBlock: { () -> Void in
             self.startMeasuring()
-            let encrypted = AES(key: key, iv: iv, blockMode: .ECB)?.encrypt(message, padding: PKCS7())
+            let encrypted = AES(key: key, iv: iv, blockMode: .CBC)?.encrypt(message, padding: PKCS7())
             self.stopMeasuring()
         })
     }