Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update perfomance of import account #336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Sources/libscrypt/libscrypt"]
path = Sources/libscrypt/libscrypt
url = https://github.com/technion/libscrypt.git
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ let package = Package(
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(name: "secp256k1"),
.target(name: "libscrypt", sources: ["libscrypt/crypto_scrypt-nosse.c",
"libscrypt/sha256.c",
"libscrypt/slowequals.c",
]),
.target(
name: "web3swift",
dependencies: ["BigInt", "secp256k1", "PromiseKit", "Starscream", "CryptoSwift"],
dependencies: ["BigInt", "secp256k1", "PromiseKit", "Starscream", "CryptoSwift", "libscrypt"],
exclude: [
]),
.testTarget(
Expand Down
2 changes: 2 additions & 0 deletions Sources/libscrypt/include/libscrypt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "../libscrypt/libscrypt.h"

4 changes: 4 additions & 0 deletions Sources/libscrypt/include/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module libscrypt [system] {
header "libscrypt.h"
export *
}
1 change: 1 addition & 0 deletions Sources/libscrypt/libscrypt
Submodule libscrypt added at a402f4
4 changes: 1 addition & 3 deletions Sources/web3swift/Convenience/CryptoExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

import Foundation
import CryptoSwift

func toByteArray<T>(_ value: T) -> [UInt8] {
var value = value
Expand All @@ -14,7 +13,6 @@ func toByteArray<T>(_ value: T) -> [UInt8] {

func scrypt (password: String, salt: Data, length: Int, N: Int, R: Int, P: Int) -> Data? {
guard let passwordData = password.data(using: .utf8) else {return nil}
guard let deriver = try? Scrypt(password: passwordData.bytes, salt: salt.bytes, dkLen: length, N: N, r: R, p: P) else {return nil}
guard let result = try? deriver.calculate() else {return nil}
guard let result = try? Scrypt.calculate(password: passwordData.bytes, salt: salt.bytes, dkLen: length, N: N, r: R, p: P) else {return nil}
return Data(result)
}
10 changes: 5 additions & 5 deletions Sources/web3swift/KeystoreManager/BIP32Keystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,14 @@ public class BIP32Keystore: AbstractKeystore {
guard let algo = keystorePars.crypto.kdfparams.prf else {
return nil
}
var hashVariant: HMAC.Variant?;
var hashVariant: PBKDF2.Variant?;
switch algo {
case "hmac-sha256":
hashVariant = HMAC.Variant.sha256
hashVariant = .sha256
case "hmac-sha384":
hashVariant = HMAC.Variant.sha384
hashVariant = .sha384
case "hmac-sha512":
hashVariant = HMAC.Variant.sha512
hashVariant = .sha512
default:
hashVariant = nil
}
Expand All @@ -323,7 +323,7 @@ public class BIP32Keystore: AbstractKeystore {
guard let passData = password.data(using: .utf8) else {
return nil
}
guard let derivedArray = try? PKCS5.PBKDF2(password: passData.bytes, salt: saltData.bytes, iterations: c, keyLength: derivedLen, variant: hashVariant!).calculate() else {
guard let derivedArray = try? PBKDF2.calculate(password: passData.bytes, salt: saltData.bytes, iterations: c, keyLength: derivedLen, variant: hashVariant!) else {
return nil
}
// passwordDerivedKey = Data(bytes:derivedArray)
Expand Down
2 changes: 1 addition & 1 deletion Sources/web3swift/KeystoreManager/BIP39.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public class BIP39 {
guard let mnemData = mnemonics.decomposedStringWithCompatibilityMapping.data(using: .utf8) else {return nil}
let salt = "mnemonic" + password
guard let saltData = salt.decomposedStringWithCompatibilityMapping.data(using: .utf8) else {return nil}
guard let seedArray = try? PKCS5.PBKDF2(password: mnemData.bytes, salt: saltData.bytes, iterations: 2048, keyLength: 64, variant: HMAC.Variant.sha512).calculate() else {return nil}
guard let seedArray = try? PBKDF2.calculate(password: mnemData.bytes, salt: saltData.bytes, iterations: 2048, keyLength: 64, variant: .sha512) else {return nil}
// let seed = Data(bytes:seedArray)
let seed = Data(seedArray)
return seed
Expand Down
10 changes: 5 additions & 5 deletions Sources/web3swift/KeystoreManager/EthereumKeystoreV3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ public class EthereumKeystoreV3: AbstractKeystore {
guard let algo = keystoreParams.crypto.kdfparams.prf else {
return nil
}
var hashVariant: HMAC.Variant?;
var hashVariant: PBKDF2.Variant?;
switch algo {
case "hmac-sha256":
hashVariant = HMAC.Variant.sha256
hashVariant = .sha256
case "hmac-sha384":
hashVariant = HMAC.Variant.sha384
hashVariant = .sha384
case "hmac-sha512":
hashVariant = HMAC.Variant.sha512
hashVariant = .sha512
default:
hashVariant = nil
}
Expand All @@ -199,7 +199,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
guard let passData = password.data(using: .utf8) else {
return nil
}
guard let derivedArray = try? PKCS5.PBKDF2(password: passData.bytes, salt: saltData.bytes, iterations: c, keyLength: derivedLen, variant: hashVariant!).calculate() else {
guard let derivedArray = try? PBKDF2.calculate(password: passData.bytes, salt: saltData.bytes, iterations: c, keyLength: derivedLen, variant: hashVariant!) else {
return nil
}
// passwordDerivedKey = Data(bytes:derivedArray)
Expand Down
57 changes: 57 additions & 0 deletions Sources/web3swift/Utils/Calculations/PBKDF2+Calculate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// PBKDF2+Calculate.swift
// web3swift
//
// Created by Pavel on 23.06.2021.
//

import Foundation
import CommonCrypto

enum PBKDF2 {
enum PBKDF2Error: Error {
case failed
}

enum Variant: Int {
case sha1
case sha256
case sha384
case sha512

var algorithm: Int {
switch self {
case .sha1:
return kCCPRFHmacAlgSHA1
case .sha256:
return kCCPRFHmacAlgSHA256
case .sha384:
return kCCPRFHmacAlgSHA384
case .sha512:
return kCCPRFHmacAlgSHA512
}
}
}

static func calculate(password: Array<UInt8>, salt: Array<UInt8>, iterations: Int = 4096 /* c */, keyLength: Int? = nil /* dkLen */, variant: Variant = .sha256) throws -> Array<UInt8> {

var derivedKey: Array<UInt8> = Array(repeating: 0, count: keyLength ?? 0)

let passwordBytes = password.map { Int8($0) }
let derivationStatus = CCKeyDerivationPBKDF(
CCPBKDFAlgorithm(kCCPBKDF2),
passwordBytes, password.count,
salt, salt.count,
CCPseudoRandomAlgorithm(CCPBKDFAlgorithm(variant.algorithm)),
UInt32(iterations),
&derivedKey,
derivedKey.count)


if derivationStatus != 0 {
throw PBKDF2Error.failed
}

return derivedKey
}
}
60 changes: 60 additions & 0 deletions Sources/web3swift/Utils/Calculations/Scrypt+Calculate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Scrypt+Calculate.swift
// web3swift
//
// Created by Pavel on 23.06.2021.
// Copyright © 2021 Matter Labs. All rights reserved.
//

import Foundation
import libscrypt

enum Scrypt {

enum ScryptError: Error {
case invalidLength
case invalidParameters
case emptySalt
case unknownError(code: Int32)
}

static func calculate(password: Array<UInt8>, salt: Array<UInt8>, dkLen: Int, N: Int, r: Int, p: Int) throws -> [UInt8] {

guard dkLen > 0, UInt64(dkLen) <= 137_438_953_440 else {
throw ScryptError.invalidLength
}
guard r > 0, p > 0, r * p < 1_073_741_824, N.isPowerOfTwo else {
throw ScryptError.invalidParameters
}
var rv = [UInt8](repeating: 0, count: dkLen)
var result: Int32 = -1
try rv.withUnsafeMutableBufferPointer { bufptr in
try password.withUnsafeBufferPointer { passwd in

try salt.withUnsafeBufferPointer { saltptr in
guard !saltptr.isEmpty else {
throw ScryptError.emptySalt
}
result = libscrypt_scrypt(
passwd.baseAddress!, passwd.count,
saltptr.baseAddress!, saltptr.count,
UInt64(N), UInt32(r), UInt32(p),
bufptr.baseAddress!, dkLen
)
}
}
}
guard result == 0 else {
throw ScryptError.unknownError(code: result)
}
return rv
}
}


private extension BinaryInteger {
var isPowerOfTwo: Bool {
(self > 0) && (self & (self - 1) == 0)
}
}

1 change: 1 addition & 0 deletions web3swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Pod::Spec.new do |spec|
spec.dependency 'CryptoSwift', '~> 1.4.0'
spec.dependency 'secp256k1.c', '~> 0.1'
spec.dependency 'PromiseKit', '~> 6.15.3'
spec.dependency 'libscrypt'
end
Loading