-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from yenom/Sajjon/expose-ec-methods
Adding EC point multiplication method
- Loading branch information
Showing
9 changed files
with
382 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// PointOnCurve.swift | ||
// BitcoinKit | ||
// | ||
// Created by Alexander Cyon on 2019-03-21. | ||
// Copyright © 2019 BitcoinKit developers. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
#if BitcoinKitXcode | ||
import BitcoinKit.Private | ||
#else | ||
import BitcoinKitPrivate | ||
#endif | ||
|
||
public struct PointOnCurve { | ||
|
||
private static let byteForUncompressed: UInt8 = 0x04 | ||
public let x: Scalar32Bytes | ||
public let y: Scalar32Bytes | ||
|
||
public init(x: Scalar32Bytes, y: Scalar32Bytes) { | ||
self.x = x | ||
self.y = y | ||
} | ||
|
||
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 extension PointOnCurve { | ||
enum Error: Swift.Error { | ||
case multiplicationResultedInTooFewBytes(expected: Int, butGot: Int) | ||
case expectedUncompressedPoint | ||
case publicKeyContainsTooFewBytes(expected: Int, butGot: Int) | ||
} | ||
|
||
static func decodePointFromPublicKey(_ publicKey: PublicKey) throws -> PointOnCurve { | ||
let data: Data | ||
if publicKey.isCompressed { | ||
data = _EllipticCurve.decodePointOnCurve(forCompressedPublicKey: publicKey.data) | ||
} else { | ||
data = publicKey.data | ||
} | ||
return try PointOnCurve.decodePointFrom(xAndYPrefixedWithCompressionType: data) | ||
} | ||
|
||
private static func decodePointFrom(xAndYPrefixedWithCompressionType data: Data) throws -> PointOnCurve { | ||
var xAndY = data | ||
guard xAndY[0] == PointOnCurve.byteForUncompressed else { | ||
throw Error.expectedUncompressedPoint | ||
} | ||
xAndY = Data(xAndY.dropFirst()) | ||
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(scalar: Scalar32Bytes) throws -> PointOnCurve { | ||
let xAndY = _EllipticCurve.multiplyECPointX(x.data, andECPointY: y.data, withScalar: scalar.data) | ||
return try PointOnCurve.decodePointFrom(xAndYPrefixedWithCompressionType: xAndY) | ||
} | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// 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 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.tooManyBytes(expectedCount: Scalar32Bytes.expectedByteCount, butGot: byteCount) | ||
} | ||
self.data = data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.