Skip to content

Commit

Permalink
Change name of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
massaru-stark committed Nov 5, 2021
1 parent aefe6fd commit cd9d70f
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Sources/starkbank-ecdsa/Ecdsa.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Ecdsa {
let hashMessage = hashfunc.digest(message)
let numberMessage = BinaryAscii.intFromHex(BinaryAscii.hexFromData(hashMessage as Data))
let curve = privateKey.curve
let randNum = try RandomInteger.between(minimum: BigInt(1), maximum: curve.N)
let randNum = try RandomInteger.between(min: BigInt(1), max: curve.N)
let randomSignPoint = Math.multiply(curve.G, randNum, curve.N, curve.A, curve.P)
let r = randomSignPoint.x.modulus(curve.N)
let s = ((numberMessage + r * privateKey.secret) * (Math.inv(randNum, curve.N))).modulus(curve.N)
Expand Down
14 changes: 7 additions & 7 deletions Sources/starkbank-ecdsa/PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PrivateKey {
public init(curve: CurveFp = secp256k1, secret: BigInt? = nil) throws {
self.curve = curve
if (secret == nil) {
self.secret = try RandomInteger.between(minimum: BigInt(1), maximum: curve.N - BigInt(1))
self.secret = try RandomInteger.between(min: BigInt(1), max: curve.N - BigInt(1))
return
}
self.secret = secret!
Expand Down Expand Up @@ -48,7 +48,7 @@ public class PrivateKey {
public func toDer() -> Data {
let publicKeyString = publicKey().toString(encoded: true)
let hexadecimal = Der.encodeConstructed(
Der.encodeInteger(number: 1),
Der.encodeInteger(1),
Der.encodeOctetString(BinaryAscii.hexFromInt(self.secret)),
Der.encodeOidContainer(
Der.encodeObject(self.curve.oid)),
Expand All @@ -59,10 +59,10 @@ public class PrivateKey {
return BinaryAscii.dataFromHex(hexadecimal)
}

public static func fromDer(_ data: Data) throws -> PrivateKey {
var hexadecimal = BinaryAscii.hexFromData(data)
public static func fromDer(_ string: Data) throws -> PrivateKey {
var hexadecimal = BinaryAscii.hexFromData(string)

let parsed = try Der.parse(hexadecimal: &hexadecimal)[0] as! [Any]
let parsed = try Der.parse(&hexadecimal)[0] as! [Any]
let privateKeyFlag = parsed[0] as! BigInt
let secretHex = parsed[1] as! String
let curveData = (parsed[2] as! [Any])[0] as! [Int]
Expand All @@ -73,7 +73,7 @@ public class PrivateKey {
}

let curve = try getCurveByOid(curveData)
let privateKey = try fromString(secretHex, curve)
let privateKey = try fromString(string: secretHex, curve: curve)

if (privateKey.publicKey().toString(encoded: true) != publicKeyString) {
throw Error.matchError("The public key described inside the private key file doesn't match the actual public key of the pair")
Expand All @@ -85,7 +85,7 @@ public class PrivateKey {
return BinaryAscii.hexFromInt(self.secret)
}

public static func fromString(_ string: String, _ curve: CurveFp = secp256k1) throws -> PrivateKey {
public static func fromString(string: String, curve: CurveFp = secp256k1) throws -> PrivateKey {
return try PrivateKey(curve: curve, secret: BinaryAscii.intFromHex(string))
}
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/starkbank-ecdsa/PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class PublicKey {
return createPem(content: BinaryAscii.base64FromData(der), template: publicKeyPemTemplate)
}

public static func fromPem(_ pem: String) throws -> PublicKey {
let publicKeyPem = try getPemContent(pem: pem, template: publicKeyPemTemplate)
public static func fromPem(_ string: String) throws -> PublicKey {
let publicKeyPem = try getPemContent(pem: string, template: publicKeyPemTemplate)
return try fromDer(BinaryAscii.dataFromBase64(publicKeyPem))
}

Expand All @@ -39,10 +39,10 @@ public class PublicKey {
return BinaryAscii.dataFromHex(hexadecimal)
}

public static func fromDer(_ data: Data) throws -> PublicKey{
var hexadecimal = BinaryAscii.hexFromData(data)
public static func fromDer(_ string: Data) throws -> PublicKey{
var hexadecimal = BinaryAscii.hexFromData(string)

let parsed = try Der.parse(hexadecimal: &hexadecimal)[0] as! [Any]
let parsed = try Der.parse(&hexadecimal)[0] as! [Any]
let publicKeyOid = (parsed[0] as! [Any])[0] as! [Int]
let curveOid = (parsed[0] as! [Any])[1] as! [Int]
var pointString = parsed[1] as! String
Expand All @@ -52,7 +52,7 @@ public class PublicKey {
.replacingOccurrences(of: "{actualOid}", with: publicKeyOid.description))
}
let curve = try getCurveByOid(curveOid)
return try fromString(&pointString, curve)
return try fromString(string: &pointString, curve: curve)
}

public func toString(encoded: Bool = false) -> String {
Expand All @@ -66,7 +66,7 @@ public class PublicKey {
return string
}

public static func fromString(_ string: inout String, _ curve: CurveFp = secp256k1, validatePoint: Bool = true) throws -> PublicKey {
public static func fromString(string: inout String, curve: CurveFp = secp256k1, validatePoint: Bool = true) throws -> PublicKey {
let baseLength = 2 * curve.length()
if (string.count > 2 * baseLength && String(string.prefix(4)) == "0004") {
string = String(string.suffix(string.count - 4))
Expand Down
6 changes: 3 additions & 3 deletions Sources/starkbank-ecdsa/Signature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public class Signature {

public func toString() -> String {
return Der.encodeConstructed(
Der.encodeInteger(number: self.r),
Der.encodeInteger(number: self.s)
Der.encodeInteger(self.r),
Der.encodeInteger(self.s)
)
}

public static func fromString(string: inout String) throws -> Signature {
let parsed = try Der.parse(hexadecimal: &string)[0] as! [Any]
let parsed = try Der.parse(&string)[0] as! [Any]
let r = parsed[0] as! BigInt
let s = parsed[1] as! BigInt
return Signature(r, s)
Expand Down
26 changes: 13 additions & 13 deletions Sources/starkbank-ecdsa/Utils/Binary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import Foundation

class BinaryAscii {

static func intFromHex(_ hex: String) -> BigInt {
return BigInt(hex, radix: 16)!
static func intFromHex(_ hexadecimal: String) -> BigInt {
return BigInt(hexadecimal, radix: 16)!
}

static func hexFromInt(_ int: BigInt) -> String {
var hexadecimal = String(int, radix: 16)
static func hexFromInt(_ number: BigInt) -> String {
var hexadecimal = String(number, radix: 16)
if (hexadecimal.count % 2 == 1) {
hexadecimal = "0" + hexadecimal
}
return hexadecimal
}

static func bitsFromHex(_ hex: String) -> String {
let binary = String(BigInt(hex, radix: 16)!, radix: 2)
return StringHelper.zfill(binary, hex.count * 4)
static func bitsFromHex(_ hexadecimal: String) -> String {
let binary = String(BigInt(hexadecimal, radix: 16)!, radix: 2)
return StringHelper.zfill(binary, hexadecimal.count * 4)
}

static func dataFromBase64(_ base64: String) -> Data {
return Data(base64Encoded: base64)!
static func dataFromBase64(_ base64String: String) -> Data {
return Data(base64Encoded: base64String)!
}

static func base64FromData(_ string: Data) -> String {
Expand All @@ -41,12 +41,12 @@ class BinaryAscii {
return data.map{ String(format:"%02x", $0) }.joined()
}

static func dataFromHex(_ hex: String) -> Data {
var data = Data(capacity: hex.count / 2)
static func dataFromHex(_ hexadecimal: String) -> Data {
var data = Data(capacity: hexadecimal.count / 2)

let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
regex.enumerateMatches(in: hex, range: NSRange(hex.startIndex..., in: hex)) { match, _, _ in
let byteString = (hex as NSString).substring(with: match!.range)
regex.enumerateMatches(in: hexadecimal, range: NSRange(hexadecimal.startIndex..., in: hexadecimal)) { match, _, _ in
let byteString = (hexadecimal as NSString).substring(with: match!.range)
let num = UInt8(byteString, radix: 16)!
data.append(num)
}
Expand Down
44 changes: 22 additions & 22 deletions Sources/starkbank-ecdsa/Utils/Der.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ let hexTagtoType = [

public class Der {

public static func encodeConstructed(_ components: String...) -> String {
return encodeSequence(components.joined(separator: ""))
public static func encodeConstructed(_ encodedValues: String...) -> String {
return encodeSequence(encodedValues.joined(separator: ""))
}

internal static func encodeInteger(number: BigInt) -> String {
internal static func encodeInteger(_ number: BigInt) -> String {
var hexadecimal = BinaryAscii.hexFromInt(abs(number))
if (number < 0) {
let bitCount = 4 * hexadecimal.count
Expand All @@ -69,7 +69,7 @@ public class Der {
}

internal static func encodeObject(_ oid: Array<Int>) -> String {
let hexadecimal = Oid.oidToHex(oid: oid)
let hexadecimal = Oid.oidToHex(oid)
return addPrefix(typeToHexTag.object.rawValue, hexadecimal)
}

Expand Down Expand Up @@ -97,7 +97,7 @@ public class Der {
return String(format: "%@%@%@", tag, generateLengthBytes(hexadecimal: data), data)
}

static func parse(hexadecimal: inout String) throws -> Array<Any> {
static func parse(_ hexadecimal: inout String) throws -> Array<Any> {
if (hexadecimal == "") {
return []
}
Expand All @@ -124,48 +124,48 @@ public class Der {
}

var contentArray: Array<Any> = [content]
let tagData = getTagData(tag: typeByte)
let tagData = getTagData(typeByte)
if (tagData["isConstructed"] as! Bool) {
contentArray = try parse(hexadecimal: &content)
return [contentArray] + (try parse(hexadecimal: &hexadecimal))
contentArray = try parse(&content)
return [contentArray] + (try parse(&hexadecimal))
}

switch tagData["type"]! as! String {
case null:
contentArray = [parseNull(hexadecimal: content)]
contentArray = [parseNull(content)]
case object:
contentArray = [parseOid(hexadecimal: content)]
contentArray = [parseOid(content)]
case utcTime:
contentArray = [parseTime(hexadecimal: content)]
contentArray = [parseTime(content)]
case integer:
contentArray = [parseInteger(hexadecimal: content)]
contentArray = [parseInteger(content)]
case printableString:
contentArray = [parseString(hexadecimal: content)]
contentArray = [parseString(content)]
default:
break
}
return contentArray + (try parse(hexadecimal: &hexadecimal))
return contentArray + (try parse(&hexadecimal))
}

private static func parseOid(hexadecimal: String) -> [Int] {
return Oid.oidFromHex(hexadecimal: hexadecimal)
private static func parseOid(_ hexadecimal: String) -> [Int] {
return Oid.oidFromHex(hexadecimal)
}

private static func parseTime(hexadecimal: String) -> Date {
private static func parseTime(_ hexadecimal: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyMMddHHmmssZ"
return dateFormatter.date(from: parseString(hexadecimal: hexadecimal)) ?? Date()
return dateFormatter.date(from: parseString(hexadecimal)) ?? Date()
}

private static func parseString(hexadecimal: String) -> String {
private static func parseString(_ hexadecimal: String) -> String {
return String(data: BinaryAscii.dataFromHex(hexadecimal), encoding: .utf8)!
}

private static func parseNull(hexadecimal: String) -> String {
private static func parseNull(_ content: String) -> String {
return ""
}

private static func parseInteger(hexadecimal: String) -> BigInt {
private static func parseInteger(_ hexadecimal: String) -> BigInt {
let integer = BinaryAscii.intFromHex(hexadecimal)
let bits = BinaryAscii.bitsFromHex(String(hexadecimal.prefix(1)))
if (String(bits.prefix(1)) == "0") {
Expand Down Expand Up @@ -207,7 +207,7 @@ public class Der {
return BinaryAscii.hexFromInt(lengthLength) + length
}

private static func getTagData(tag: String) -> Dictionary<String, AnyObject> {
private static func getTagData(_ tag: String) -> Dictionary<String, AnyObject> {
let bits = BinaryAscii.bitsFromHex(tag)
let bit8 = bits[bits.index(bits.startIndex, offsetBy: 0)]
let bit7 = bits[bits.index(bits.startIndex, offsetBy: 1)]
Expand Down
12 changes: 6 additions & 6 deletions Sources/starkbank-ecdsa/Utils/Integer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class RandomInteger {
- Parameter maximum: maximum value of the integer
- Returns: integer x in the range: minimum <= x <= maximum
*/
public static func between(minimum: BigInt, maximum: BigInt) throws -> BigInt {
let result = getBytesNeeded(maximum - minimum)
public static func between(min: BigInt, max: BigInt) throws -> BigInt {
let result = getBytesNeeded(max - min)
let bytesNeeded = result.0
let mask = result.1

Expand All @@ -43,13 +43,13 @@ class RandomInteger {

// Taking the randomValue module would increase concentration in a specific region of the range and break the uniform distribution, raising security concerns.
// To avoid this, we retry the method until the value satisfies the range.
if (minimum + randomValue > maximum) {
return try between(minimum: minimum, maximum: maximum)
if (min + randomValue > max) {
return try between(min: min, max: max)
}
return minimum + randomValue
return min + randomValue
}

public static func getBytesNeeded(_ request: BigInt) -> (Int, BigInt) {
internal static func getBytesNeeded(_ request: BigInt) -> (Int, BigInt) {
var range = request
var bitsNeeded = 0
var bytesNeeded = 0
Expand Down
4 changes: 2 additions & 2 deletions Sources/starkbank-ecdsa/Utils/Oid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import BigInt

public class Oid {

public static func oidFromHex(hexadecimal: String) -> [Int] {
public static func oidFromHex(_ hexadecimal: String) -> [Int] {
let firstByte = String(hexadecimal.prefix(2))
var remainingBytes = String(hexadecimal.suffix(hexadecimal.count - 2))
let firstByteInt = BinaryAscii.intFromHex(firstByte)
Expand All @@ -32,7 +32,7 @@ public class Oid {
return oid
}

public static func oidToHex(oid: [Int]) -> String {
public static func oidToHex(_ oid: [Int]) -> String {
var hexadecimal = BinaryAscii.hexFromInt(BigInt(40 * oid[0]) + BigInt(oid[1]))
var byteArray = [Int]()
for var oidInt in oid[2..<oid.count] {
Expand Down
2 changes: 1 addition & 1 deletion Tests/starkbank-ecdsaTests/PrivateKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PrivateKeyTests: XCTestCase {
func testStringConversion() throws {
let privateKey = try PrivateKey()
let string = privateKey.toString()
let privateKey2 = try PrivateKey.fromString(string)
let privateKey2 = try PrivateKey.fromString(string: string)
XCTAssertTrue(privateKey.secret == privateKey2.secret)
XCTAssertTrue(privateKey.curve.A == privateKey2.curve.A &&
privateKey.curve.B == privateKey2.curve.B &&
Expand Down
2 changes: 1 addition & 1 deletion Tests/starkbank-ecdsaTests/PublicKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PublicKeyTests: XCTestCase {
let privateKey = try PrivateKey()
let publicKey1 = privateKey.publicKey()
var string = publicKey1.toString()
let publicKey2 = try PublicKey.fromString(&string)
let publicKey2 = try PublicKey.fromString(string: &string)
XCTAssertTrue(publicKey1.point.x == publicKey2.point.x)
XCTAssertTrue(publicKey1.point.y == publicKey2.point.y)
XCTAssertTrue(publicKey1.curve.A == publicKey2.curve.A &&
Expand Down

0 comments on commit cd9d70f

Please sign in to comment.