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

Moving PointOnCurve and Scalar32Bytes to separate files. #2

Merged
merged 1 commit into from
Mar 21, 2019
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
8 changes: 8 additions & 0 deletions BitcoinKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@
CFA290702101CDCA001A1BAB /* ScriptTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFA2906F2101CDCA001A1BAB /* ScriptTests.swift */; };
CFA290722102B635001A1BAB /* ScriptMachine.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFA290712102B635001A1BAB /* ScriptMachine.swift */; };
CFA290742102B650001A1BAB /* ScriptMachineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFA290732102B650001A1BAB /* ScriptMachineTests.swift */; };
E6A64306224418EB00CD4BFC /* PointOnCurve.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A64305224418EB00CD4BFC /* PointOnCurve.swift */; };
E6A643082244190400CD4BFC /* Scalar32Bytes.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A643072244190400CD4BFC /* Scalar32Bytes.swift */; };
E6C9FB9D2243FF7A000AAE12 /* PointMultiplicationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6C9FB9C2243FF7A000AAE12 /* PointMultiplicationTests.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -444,6 +446,8 @@
CFA2906F2101CDCA001A1BAB /* ScriptTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptTests.swift; sourceTree = "<group>"; };
CFA290712102B635001A1BAB /* ScriptMachine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptMachine.swift; sourceTree = "<group>"; };
CFA290732102B650001A1BAB /* ScriptMachineTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptMachineTests.swift; sourceTree = "<group>"; };
E6A64305224418EB00CD4BFC /* PointOnCurve.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PointOnCurve.swift; sourceTree = "<group>"; };
E6A643072244190400CD4BFC /* Scalar32Bytes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Scalar32Bytes.swift; sourceTree = "<group>"; };
E6C9FB9C2243FF7A000AAE12 /* PointMultiplicationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PointMultiplicationTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -621,6 +625,8 @@
147494EB201F9E4F006D1CF8 /* Network.swift */,
1463E6B52025E99C0033DAAE /* BlockChain.swift */,
1463E6B32025E9480033DAAE /* BlockStore.swift */,
E6A64305224418EB00CD4BFC /* PointOnCurve.swift */,
E6A643072244190400CD4BFC /* Scalar32Bytes.swift */,
1482B5E12026B0680098B612 /* Mnemonic.swift */,
1482B5E32026F2580098B612 /* WordList.swift */,
14839A7B202F79F900A6CB34 /* PaymentURI.swift */,
Expand Down Expand Up @@ -1035,6 +1041,7 @@
1482B5E42026F2590098B612 /* WordList.swift in Sources */,
14839A93202FE6AC00A6CB34 /* FilterLoadMessage.swift in Sources */,
14839A9D202FE72600A6CB34 /* TransactionOutPoint.swift in Sources */,
E6A643082244190400CD4BFC /* Scalar32Bytes.swift in Sources */,
2933014D214FCE150028946B /* TransactionHistoryProvider.swift in Sources */,
0C1DE155211D787100FE8E43 /* OP_TOTALSTACK.swift in Sources */,
147494E6201F9BF0006D1CF8 /* Encoding.swift in Sources */,
Expand Down Expand Up @@ -1097,6 +1104,7 @@
0C66CD05212542660049DB89 /* OP_AND.swift in Sources */,
0C1DD41021181AF3004BA8A8 /* OP_NUMEQUAL.swift in Sources */,
14839AA9202FE7DD00A6CB34 /* VarString.swift in Sources */,
E6A64306224418EB00CD4BFC /* PointOnCurve.swift in Sources */,
14F37A3E2020A02000D34748 /* SighashType.swift in Sources */,
14839A8F202FE68000A6CB34 /* PongMessage.swift in Sources */,
294DDE41211B31D600B7F645 /* OP_NOTIF.swift in Sources */,
Expand Down
5 changes: 4 additions & 1 deletion BitcoinKit/BitcoinKitPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ NS_ASSUME_NONNULL_BEGIN
@end

@interface _Key : NSObject
+ (NSData *)multiplyECPointX:(NSData *)ecPointX andECPointY:(NSData *)ecPointY withScalar:(NSData *)scalar;
+ (NSData *)computePublicKeyFromPrivateKey:(NSData *)privateKey compression:(BOOL)compression;
+ (NSData *)deriveKey:(NSData *)password salt:(NSData *)salt iterations:(NSInteger)iterations keyLength:(NSInteger)keyLength;

@end

@interface _EllipticCurve : NSObject
+ (NSData *)multiplyECPointX:(NSData *)ecPointX andECPointY:(NSData *)ecPointY withScalar:(NSData *)scalar;
@end

@interface _HDKey : NSObject

@property (nonatomic, readonly, nullable) NSData *privateKey;
Expand Down
17 changes: 8 additions & 9 deletions BitcoinKit/BitcoinKitPrivate.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ + (NSData *)hmacsha512:(NSData *)data key:(NSData *)key {

@end

@implementation _Key


@implementation _EllipticCurve
+ (NSData *)multiplyECPointX:(NSData *)ecPointX andECPointY:(NSData *)ecPointY withScalar:(NSData *)scalar {
BN_CTX *ctx = BN_CTX_new();
const EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
Expand All @@ -79,7 +77,6 @@ + (NSData *)multiplyECPointX:(NSData *)ecPointX andECPointY:(NSData *)ecPointY w
BN_bin2bn(scalar.bytes, (int)scalar.length, multiplication_factor);

BIGNUM *point_x = BN_new();

BN_bin2bn(ecPointX.bytes, (int)ecPointX.length, point_x);

BIGNUM *point_y = BN_new();
Expand All @@ -89,26 +86,28 @@ + (NSData *)multiplyECPointX:(NSData *)ecPointX andECPointY:(NSData *)ecPointY w
EC_POINT_set_affine_coordinates_GFp(group, point, point_x, point_y, ctx);

EC_POINT *point_result_of_ec_multiplication = EC_POINT_new(group);

EC_POINT_mul(group, point_result_of_ec_multiplication, nil, point, multiplication_factor, ctx);

NSMutableData *newPointXAndYPrefixedWithByte = [NSMutableData dataWithLength:65];
BIGNUM *new_point_x_and_y_as_single_bn = BN_new();
EC_POINT_point2bn(group, point_result_of_ec_multiplication, POINT_CONVERSION_UNCOMPRESSED, new_point_x_and_y_as_single_bn, ctx);
BN_bn2bin(new_point_x_and_y_as_single_bn, newPointXAndYPrefixedWithByte.mutableBytes);
BN_free(new_point_x_and_y_as_single_bn);

BN_free(new_point_x_and_y_as_single_bn);
EC_POINT_free(point_result_of_ec_multiplication);
EC_POINT_free(point);
BN_free(multiplication_factor);
BN_free(point_x);
BN_free(point_y);
BN_CTX_free(ctx);
NSData* newPointXAndYDataConcatenated = [newPointXAndYPrefixedWithByte subdataWithRange:NSMakeRange(1, 64)];

return newPointXAndYDataConcatenated;
return [newPointXAndYPrefixedWithByte subdataWithRange:NSMakeRange(1, 64)];
}

@end

@implementation _Key

+ (NSData *)computePublicKeyFromPrivateKey:(NSData *)privateKey compression:(BOOL)compression {
BN_CTX *ctx = BN_CTX_new();
EC_KEY *key = EC_KEY_new_by_curve_name(NID_secp256k1);
Expand Down
62 changes: 1 addition & 61 deletions Sources/BitcoinKit/Core/Keys/PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,66 +30,6 @@ import BitcoinKit.Private
import BitcoinKitPrivate
#endif

public struct PointOnCurve {

public struct Scalar32Bytes {
// swiftlint:disable:next nesting
public enum Error: Swift.Error {
case tooFewBytes(expectedCount: Int, butGot: Int)
case tooManyBytes(expectedCount: Int, butGot: Int)
}
public static let expectedByteCount = 32
public let data: Data
public init(data: Data) throws {
let byteCount = data.count
if byteCount < Scalar32Bytes.expectedByteCount {
throw Error.tooFewBytes(expectedCount: Scalar32Bytes.expectedByteCount, butGot: byteCount)
}
if byteCount > Scalar32Bytes.expectedByteCount {
throw Error.tooManyBytes(expectedCount: Scalar32Bytes.expectedByteCount, butGot: byteCount)
}
self.data = data
}
}

public enum Error: Swift.Error {
case multiplicationResultedInTooFewBytes(expected: Int, butGot: Int)
}

public let x: Scalar32Bytes
public let y: Scalar32Bytes
public init(x: Scalar32Bytes, y: Scalar32Bytes) {
self.x = x
self.y = y
}

public init(x xData: Data, y yData: Data) throws {
let x = try Scalar32Bytes(data: xData)
let y = try Scalar32Bytes(data: yData)
self.init(x: x, y: y)
}

public func multiplyBy(scalar: Scalar32Bytes) throws -> PointOnCurve {
let xAndY = _Key.multiplyECPointX(x.data, andECPointY: y.data, withScalar: scalar.data)
let expectedByteCount = Scalar32Bytes.expectedByteCount * 2
guard xAndY.count == expectedByteCount else {
throw Error.multiplicationResultedInTooFewBytes(expected: expectedByteCount, butGot: xAndY.count)
}
let resultX = xAndY.prefix(Scalar32Bytes.expectedByteCount)
let resultY = xAndY.suffix(Scalar32Bytes.expectedByteCount)
return try PointOnCurve(x: resultX, y: resultY)
}

public func multiplyBy(privateKey: PrivateKey) throws -> PointOnCurve {
return try multiplyBy(scalar: privateKey.data)
}

public func multiplyBy(scalar scalarData: Data) throws -> PointOnCurve {
let scalar = try Scalar32Bytes(data: scalarData)
return try multiplyBy(scalar: scalar)
}
}

public struct PrivateKey {
@available(*, deprecated, renamed: "data")
public var raw: Data { return data }
Expand Down Expand Up @@ -184,7 +124,7 @@ public struct PrivateKey {

public func publicKeyPoint() throws -> PointOnCurve {
let xAndY: Data = _Key.computePublicKey(fromPrivateKey: data, compression: false)
let expectedLengthOfScalar = PointOnCurve.Scalar32Bytes.expectedByteCount
let expectedLengthOfScalar = Scalar32Bytes.expectedByteCount
let expectedLengthOfKey = expectedLengthOfScalar * 2
guard xAndY.count == expectedLengthOfKey else {
fatalError("expected length of key is \(expectedLengthOfKey) bytes, but got: \(xAndY.count)")
Expand Down
53 changes: 53 additions & 0 deletions Sources/BitcoinKit/Core/PointOnCurve.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// PointOnCurve.swift
// BitcoinKit
//
// Created by Alexander Cyon on 2019-03-21.
// Copyright © 2019 BitcoinKit developers. All rights reserved.
//

import Foundation

public struct PointOnCurve {

public let x: Scalar32Bytes
public let y: Scalar32Bytes

public init(x: Scalar32Bytes, y: Scalar32Bytes) {
self.x = x
self.y = y
}
}

public extension PointOnCurve {

public enum Error: Swift.Error {
case multiplicationResultedInTooFewBytes(expected: Int, butGot: Int)
}

init(x xData: Data, y yData: Data) throws {
let x = try Scalar32Bytes(data: xData)
let y = try Scalar32Bytes(data: yData)
self.init(x: x, y: y)
}

func multiplyBy(scalar: Scalar32Bytes) throws -> PointOnCurve {
let xAndY = _EllipticCurve.multiplyECPointX(x.data, andECPointY: y.data, withScalar: scalar.data)
let expectedByteCount = Scalar32Bytes.expectedByteCount * 2
guard xAndY.count == expectedByteCount else {
throw Error.multiplicationResultedInTooFewBytes(expected: expectedByteCount, butGot: xAndY.count)
}
let resultX = xAndY.prefix(Scalar32Bytes.expectedByteCount)
let resultY = xAndY.suffix(Scalar32Bytes.expectedByteCount)
return try PointOnCurve(x: resultX, y: resultY)
}

func multiplyBy(privateKey: PrivateKey) throws -> PointOnCurve {
return try multiplyBy(scalar: privateKey.data)
}

func multiplyBy(scalar scalarData: Data) throws -> PointOnCurve {
let scalar = try Scalar32Bytes(data: scalarData)
return try multiplyBy(scalar: scalar)
}
}
28 changes: 28 additions & 0 deletions Sources/BitcoinKit/Core/Scalar32Bytes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Scalar32Bytes.swift
// BitcoinKit
//
// Created by Alexander Cyon on 2019-03-21.
// Copyright © 2019 BitcoinKit developers. All rights reserved.
//

import Foundation

public struct Scalar32Bytes {
public enum Error: Swift.Error {
case tooFewBytes(expectedCount: Int, butGot: Int)
case tooManyBytes(expectedCount: Int, butGot: Int)
}
public static let expectedByteCount = 32
public let data: Data
public init(data: Data) throws {
let byteCount = data.count
if byteCount < Scalar32Bytes.expectedByteCount {
throw Error.tooFewBytes(expectedCount: Scalar32Bytes.expectedByteCount, butGot: byteCount)
}
if byteCount > Scalar32Bytes.expectedByteCount {
throw Error.tooManyBytes(expectedCount: Scalar32Bytes.expectedByteCount, butGot: byteCount)
}
self.data = data
}
}