Skip to content

Commit

Permalink
fix type miscasting (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyugit committed Jan 28, 2018
1 parent c3c2671 commit 4ef14cb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/CommonCurves/Secp256k1.swift
Expand Up @@ -40,7 +40,7 @@ public struct Secp256k1: EllipticCurveOverFiniteField {

public static var Generator = Secp256k1(withCoordinates: Parameters.G)
public static var Order: UInt256 = Parameters.N
public static var InverseOrder: (UInt256, UInt256)? = Parameters.InverseN
public static var InverseOrder: (high: UInt256, low: UInt256)? = Parameters.InverseN

public static var a = FFInt(Parameters.a)
public static var b = FFInt(Parameters.b)
Expand Down
8 changes: 6 additions & 2 deletions Sources/FiniteField/FiniteFieldInteger+Default.swift
Expand Up @@ -104,8 +104,12 @@ extension FiniteFieldInteger {

public static func *(lhs: Self, rhs: Self) -> Self {
let (high, low) = lhs.value.multipliedFullWidth(by: rhs.value)
let inv = Self.InverseCharacteristic ?? (0, 0)
let (_, remain) = Self.Characteristic.dividingFullWidth((high, low), withPrecomputedInverse: inv)
let remain: Element
if let inv = Self.InverseCharacteristic {
(_, remain) = Self.Characteristic.dividingFullWidth((high, low), withPrecomputedInverse: inv)
} else {
(_, remain) = Self.Characteristic.dividingFullWidth((high, low))
}
return Self(withValue: remain)
}

Expand Down
4 changes: 4 additions & 0 deletions Tests/ECDSATests.swift
Expand Up @@ -34,6 +34,10 @@ class ECDSATests: XCTestCase {

func testInit() {
let e = ECDSA<MyECFF>()
XCTAssertNil(FFInt7.InverseOrder)
XCTAssertNil(FFInt7.InverseCharacteristic)
XCTAssertNil(MyECFF.InverseOrder)
XCTAssertNil(MyECFF.InverseCharacteristic)
XCTAssertNotNil(e)
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/PerformanceTests.swift
Expand Up @@ -39,7 +39,7 @@ class PerformanceTests: XCTestCase {
let _ = privkey * Secp256k1.Generator
}
}
/*

func testMultiplicationPerf32() {
self.measure {
let privkey = UInt256(1) << 32
Expand All @@ -53,7 +53,7 @@ class PerformanceTests: XCTestCase {
let _ = privkey * Secp256k1.Generator
}
}
*/

// make sure the building process does not slow down UInt256
func testUInt256Multiplication() {
self.measure {
Expand Down
2 changes: 2 additions & 0 deletions Tests/Secp256k1Tests.swift
Expand Up @@ -8,6 +8,8 @@ class Secp256k1Tests: XCTestCase {
func testInit() {
XCTAssertNotNil(Secp256k1())
XCTAssertNotNil(Secp256k1.Generator)
XCTAssertNotNil(Secp256k1.InverseOrder)
XCTAssertNotNil(Secp256k1.InverseCharacteristic)
}

func testPubPoint0() {
Expand Down

0 comments on commit 4ef14cb

Please sign in to comment.