From 1c03ebe071a9a4cb8dfff0e1f49636bac804fe7a Mon Sep 17 00:00:00 2001 From: Arash Ranjbaran Date: Sun, 15 Jan 2017 15:52:29 +0330 Subject: [PATCH] Swift 3 --- .../BigInteger.xcodeproj/project.pbxproj | 6 + Source/BigInteger.swift | 171 +++++++++--------- Source/Operators.swift | 28 +-- Source/Utils.swift | 19 +- Tests/BigIntegerTests.swift | 97 +++++----- 5 files changed, 165 insertions(+), 156 deletions(-) diff --git a/BigInteger/BigInteger.xcodeproj/project.pbxproj b/BigInteger/BigInteger.xcodeproj/project.pbxproj index 6754d87..f32b074 100644 --- a/BigInteger/BigInteger.xcodeproj/project.pbxproj +++ b/BigInteger/BigInteger.xcodeproj/project.pbxproj @@ -307,9 +307,11 @@ TargetAttributes = { 1846BA901A136C8800ABC529 = { CreatedOnToolsVersion = 6.1; + LastSwiftMigration = 0820; }; 1846BA9B1A136C8800ABC529 = { CreatedOnToolsVersion = 6.1; + LastSwiftMigration = 0820; }; 1846BBBC1A13765900ABC529 = { CreatedOnToolsVersion = 6.1; @@ -563,6 +565,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -583,6 +586,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -602,6 +606,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -616,6 +621,7 @@ INFOPLIST_FILE = Tests/iOS/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/Source/BigInteger.swift b/Source/BigInteger.swift index c283cc4..f13ba8e 100644 --- a/Source/BigInteger.swift +++ b/Source/BigInteger.swift @@ -14,37 +14,34 @@ extension mp_int { } } -public class BigInteger : NSObject, IntegerLiteralConvertible { +public class BigInteger : ExpressibleByIntegerLiteral { var value = mp_int() // MARK: - Init / Deinit - init(var value: mp_int) { + init(_ value: mp_int) { + var value = value mp_init_copy(&self.value, &value) } - public override init() { + public init() { mp_init_set(&self.value, 0) } - public init?(var valueAsString: String, radix: Int) { + public init?(_ valueAsString: String, radix: Int = 10) { + var valueAsString = valueAsString assert(radix >= 2 && radix <= 36, "Only radix from 2 to 36 are supported") - super.init() - valueAsString = valueAsString.uppercaseString + valueAsString = valueAsString.uppercased() if !(Utils.canParseBigIntegerFromString(valueAsString, withRadix: radix)) || valueAsString.isEmpty { return nil } - - let cString = valueAsString.cStringUsingEncoding(NSUTF8StringEncoding)! + + let cString = valueAsString.cString(using: String.Encoding.utf8)! mp_read_radix(&self.value, cString, Int32(radix)) } - - public convenience init?(_ valueAsString: String) { - self.init(valueAsString: valueAsString, radix: 10) - } - + public convenience init(_ int: Int) { let bigInteger = BigInteger("\(int)")! self.init(bigInteger) @@ -55,73 +52,72 @@ public class BigInteger : NSObject, IntegerLiteralConvertible { } public convenience init(_ bigInteger: BigInteger) { - self.init(value: bigInteger.value) + self.init(bigInteger.value) } - + public required init(coder aDecoder: NSCoder) { - super.init() - - let alloc = aDecoder.decodeInt32ForKey(Keys.alloc) + + let alloc = aDecoder.decodeInt32(forKey: Keys.alloc) mp_init_size(&self.value, alloc) self.value.alloc = alloc - self.value.used = aDecoder.decodeInt32ForKey(Keys.used) - self.value.sign = aDecoder.decodeInt32ForKey(Keys.sign) - let data = aDecoder.decodeObjectForKey(Keys.dp) as NSData + self.value.used = aDecoder.decodeInt32(forKey: Keys.used) + self.value.sign = aDecoder.decodeInt32(forKey: Keys.sign) + let data = aDecoder.decodeObject(forKey: Keys.dp) as! NSData - var buffer = [mp_digit](count: Int(self.value.alloc), repeatedValue: 0) + var buffer = [mp_digit](repeating: 0, count: Int(self.value.alloc)) data.getBytes(&buffer) - - for var i = 0; i < Int(self.value.alloc); ++i { + + for i in 0 ..< Int(self.value.alloc) { self.value.dp[i] = buffer[i] } } public func duplicate() -> BigInteger { - return BigInteger(value: self.value) + return BigInteger(self.value) } - + deinit { mp_clear(&self.value); } // MARK: - Operations - public func add(bigInteger: BigInteger) -> BigInteger { + public func add(_ bigInteger: BigInteger) -> BigInteger { var sum = BigInteger() mp_add(&self.value, &bigInteger.value, &sum.value); return sum } - public func add(primitiveInt: Int) -> BigInteger { + public func add(_ primitiveInt: Int) -> BigInteger { return add(BigInteger(primitiveInt)) } - public func subtract(bigInteger: BigInteger) -> BigInteger { + public func subtract(_ bigInteger: BigInteger) -> BigInteger { var difference = BigInteger() mp_sub(&self.value, &bigInteger.value, &difference.value); return difference } - public func subtract(primitiveInt: Int) -> BigInteger { + public func subtract(_ primitiveInt: Int) -> BigInteger { return subtract(BigInteger(primitiveInt)) } - public func multiplyBy(bigInteger: BigInteger) -> BigInteger { + public func multiplyBy(_ bigInteger: BigInteger) -> BigInteger { var product = BigInteger() mp_mul(&self.value, &bigInteger.value, &product.value); return product } - public func multiplyBy(primitiveInt: Int) -> BigInteger { + public func multiplyBy(_ primitiveInt: Int) -> BigInteger { return multiplyBy(BigInteger(primitiveInt)) } public typealias QuotientAndReminder = (quotient: BigInteger, reminder: BigInteger) - public func divideAndRemainder(bigInteger: BigInteger) -> QuotientAndReminder? { + public func divideAndRemainder(_ bigInteger: BigInteger) -> QuotientAndReminder? { var quotient = BigInteger() var remainder = BigInteger() @@ -138,23 +134,23 @@ public class BigInteger : NSObject, IntegerLiteralConvertible { return divideAndRemainder(BigInteger(primitiveInt)) } - public func divideBy(bigInteger: BigInteger) -> BigInteger? { + public func divideBy(_ bigInteger: BigInteger) -> BigInteger? { return divideAndRemainder(bigInteger)?.quotient } - public func divideBy(primitiveInt: Int) -> BigInteger? { + public func divideBy(_ primitiveInt: Int) -> BigInteger? { return divideBy(BigInteger(primitiveInt)) } - public func reminder(bigInteger: BigInteger) -> BigInteger? { + public func reminder(_ bigInteger: BigInteger) -> BigInteger? { return divideAndRemainder(bigInteger)?.reminder } - public func reminder(primitiveInt: Int) -> BigInteger? { + public func reminder(_ primitiveInt: Int) -> BigInteger? { return reminder(BigInteger(primitiveInt)) } - public func pow(exponent: mp_digit) -> BigInteger { + public func pow(_ exponent: mp_digit) -> BigInteger { var power = BigInteger() mp_expt_d(&self.value, exponent, &power.value); @@ -174,55 +170,62 @@ public class BigInteger : NSObject, IntegerLiteralConvertible { return absolute } + + public func exptmod(power: BigInteger, modulus: BigInteger) -> BigInteger { + var result = BigInteger() + mp_exptmod(&self.value, &power.value, &modulus.value, &result.value) + + return result + } - public func bitwiseXor(bigInteger: BigInteger) -> BigInteger { + public func bitwiseXor(_ bigInteger: BigInteger) -> BigInteger { var xor = BigInteger() mp_xor(&self.value, &bigInteger.value, &xor.value); return xor } - public func bitwiseXor(primitiveInt: Int) -> BigInteger { + public func bitwiseXor(_ primitiveInt: Int) -> BigInteger { return bitwiseXor(BigInteger(primitiveInt)) } - public func bitwiseOr(bigInteger: BigInteger) -> BigInteger { + public func bitwiseOr(_ bigInteger: BigInteger) -> BigInteger { var or = BigInteger() mp_or(&self.value, &bigInteger.value, &or.value); return or } - public func bitwiseOr(primitiveInt: Int) -> BigInteger { + public func bitwiseOr(_ primitiveInt: Int) -> BigInteger { return bitwiseOr(BigInteger(primitiveInt)) } - public func bitwiseAnd(bigInteger: BigInteger) -> BigInteger { + public func bitwiseAnd(_ bigInteger: BigInteger) -> BigInteger { var and = BigInteger() mp_and(&self.value, &bigInteger.value, &and.value); return and } - public func bitwiseAnd(primitiveInt: Int) -> BigInteger { + public func bitwiseAnd(_ primitiveInt: Int) -> BigInteger { return bitwiseAnd(BigInteger(primitiveInt)) } - public func shiftLeft(placesToShift: Int32) -> BigInteger { + public func shiftLeft(_ placesToShift: Int32) -> BigInteger { var leftShifted = BigInteger() mp_mul_2d(&self.value, placesToShift, &leftShifted.value) return leftShifted } - public func shiftRight(placesToShift: Int32) -> BigInteger { + public func shiftRight(_ placesToShift: Int32) -> BigInteger { var rightShifted = BigInteger() mp_div_2d(&self.value, placesToShift, &rightShifted.value, nil) return rightShifted } - public func gcd(bigInteger: BigInteger) -> BigInteger? { + public func gcd(_ bigInteger: BigInteger) -> BigInteger? { var gcd = BigInteger() let result = mp_gcd(&self.value, &bigInteger.value, &gcd.value) @@ -233,20 +236,20 @@ public class BigInteger : NSObject, IntegerLiteralConvertible { return gcd } - public func gcd(primitiveInt: Int) -> BigInteger? { + public func gcd(_ primitiveInt: Int) -> BigInteger? { return gcd(BigInteger(primitiveInt)) } - public func compare(bigInteger: BigInteger) -> NSComparisonResult { + public func compare(_ bigInteger: BigInteger) -> ComparisonResult { let comparisonResult = mp_cmp(&self.value, &bigInteger.value) - + switch comparisonResult { case MP_GT: - return .OrderedAscending + return .orderedAscending case MP_LT: - return .OrderedDescending + return .orderedDescending default: - return .OrderedSame + return .orderedSame } } @@ -260,34 +263,34 @@ public class BigInteger : NSObject, IntegerLiteralConvertible { return asString(radix: 10) } - public func asString(#radix: Int) -> String { + public func asString(radix: Int) -> String { assert(radix >= 2 && radix <= 36, "Only radix from 2 to 36 are supported") var stringLength = Int32() mp_radix_size(&self.value, Int32(radix), &stringLength) - var cString = [Int8](count: Int(stringLength), repeatedValue: 0) + var cString = [Int8](repeating: 0, count: Int(stringLength)) mp_toradix(&self.value, &cString, Int32(radix)) - - return NSString(UTF8String: cString)! + + return NSString(utf8String: cString) as! String } - + public var bytesCount: Int { return Int(mp_unsigned_bin_size(&self.value)) } - public var byteArray: [Byte] { - var buffer = [Byte](count: bytesCount, repeatedValue: 0) + public var byteArray: [UInt8] { + var buffer = [UInt8](repeating: 0, count: bytesCount) mp_to_signed_bin(&self.value, &buffer) - + return buffer } } // MARK: - Printable -extension BigInteger: Printable { - public override var description: String { +extension BigInteger: CustomStringConvertible { + public var description: String { return asString } } @@ -295,7 +298,7 @@ extension BigInteger: Printable { // MARK: - Hashable extension BigInteger: Hashable { - public override var hashValue: Int { + public var hashValue: Int { return asString.hashValue } } @@ -303,32 +306,32 @@ extension BigInteger: Hashable { // MARK: - NSCoding extension BigInteger: NSCoding { + public func encode(with aCoder: NSCoder) { + mp_clamp(&self.value) + + let data = NSData( + bytes: self.value.dp, + length: Int(self.value.alloc) * MemoryLayout.size + ) + + aCoder.encode(data, forKey: Keys.dp) + aCoder.encode(self.value.alloc, forKey: Keys.alloc) + aCoder.encode(self.value.used, forKey: Keys.used) + aCoder.encode(self.value.sign, forKey: Keys.sign) + } + struct Keys { static let dp = "dp" static let alloc = "alloc" static let used = "used" static let sign = "sign" } - - public func encodeWithCoder(aCoder: NSCoder) { - mp_clamp(&self.value) - - let data = NSData( - bytes: self.value.dp, - length: Int(self.value.alloc) * sizeof(mp_digit) - ) - - aCoder.encodeObject(data, forKey: Keys.dp) - aCoder.encodeInt32(self.value.alloc, forKey: Keys.alloc) - aCoder.encodeInt32(self.value.used, forKey: Keys.used) - aCoder.encodeInt32(self.value.sign, forKey: Keys.sign) - } } // MARK: - NSCopying extension BigInteger: NSCopying { - public func copyWithZone(zone: NSZone) -> AnyObject { + public func copy(with zone: NSZone? = nil) -> Any { return self.duplicate() } } @@ -338,21 +341,21 @@ extension BigInteger: NSCopying { extension BigInteger: Comparable, Equatable {} public func == (lhs: BigInteger, rhs: BigInteger) -> Bool { - return (lhs.compare(rhs) == .OrderedSame) + return (lhs.compare(rhs) == .orderedSame) } public func <= (lhs: BigInteger, rhs: BigInteger) -> Bool { - return (lhs.compare(rhs) != .OrderedAscending) + return (lhs.compare(rhs) != .orderedAscending) } public func >= (lhs: BigInteger, rhs: BigInteger) -> Bool { - return (lhs.compare(rhs) != .OrderedDescending) + return (lhs.compare(rhs) != .orderedDescending) } public func > (lhs: BigInteger, rhs: BigInteger) -> Bool { - return (lhs.compare(rhs) == .OrderedAscending) + return (lhs.compare(rhs) == .orderedAscending) } public func < (lhs: BigInteger, rhs: BigInteger) -> Bool { - return (lhs.compare(rhs) == .OrderedDescending) + return (lhs.compare(rhs) == .orderedDescending) } diff --git a/Source/Operators.swift b/Source/Operators.swift index 3d6769c..b8adefe 100644 --- a/Source/Operators.swift +++ b/Source/Operators.swift @@ -62,11 +62,11 @@ public func + (lhs: Int, rhs: BigInteger) -> BigInteger { return rhs.add(lhs) } -public func += (inout lhs: BigInteger, rhs: BigInteger) { +public func += (lhs: inout BigInteger, rhs: BigInteger) { lhs = lhs.add(rhs) } -public func += (inout lhs: BigInteger, rhs: Int) { +public func += (lhs: inout BigInteger, rhs: Int) { lhs = lhs.add(rhs) } @@ -84,11 +84,11 @@ public func - (lhs: Int, rhs: BigInteger) -> BigInteger { return rhs.negate().add(lhs) } -public func -= (inout lhs: BigInteger, rhs: BigInteger) { +public func -= (lhs: inout BigInteger, rhs: BigInteger) { lhs = lhs.subtract(rhs) } -public func -= (inout lhs: BigInteger, rhs: Int) { +public func -= (lhs: inout BigInteger, rhs: Int) { lhs = lhs.subtract(rhs) } @@ -106,11 +106,11 @@ public func * (lhs: Int, rhs: BigInteger) -> BigInteger { return rhs.multiplyBy(lhs) } -public func *= (inout lhs: BigInteger, rhs: BigInteger) { +public func *= (lhs: inout BigInteger, rhs: BigInteger) { lhs = lhs.multiplyBy(rhs) } -public func *= (inout lhs: BigInteger, rhs: Int) { +public func *= (lhs: inout BigInteger, rhs: Int) { lhs = lhs.multiplyBy(rhs) } @@ -154,11 +154,11 @@ public func ^ (lhs: Int, rhs: BigInteger) -> BigInteger { return rhs.bitwiseXor(lhs) } -public func ^= (inout lhs: BigInteger, rhs: BigInteger) { +public func ^= (lhs: inout BigInteger, rhs: BigInteger) { lhs = lhs.bitwiseXor(rhs) } -public func ^= (inout lhs: BigInteger, rhs: Int) { +public func ^= (lhs: inout BigInteger, rhs: Int) { lhs = lhs.bitwiseXor(rhs) } @@ -176,11 +176,11 @@ public func | (lhs: Int, rhs: BigInteger) -> BigInteger { return rhs.bitwiseOr(lhs) } -public func |= (inout lhs: BigInteger, rhs: BigInteger) { +public func |= (lhs: inout BigInteger, rhs: BigInteger) { lhs = lhs.bitwiseOr(rhs) } -public func |= (inout lhs: BigInteger, rhs: Int) { +public func |= (lhs: inout BigInteger, rhs: Int) { lhs = lhs.bitwiseOr(rhs) } @@ -198,11 +198,11 @@ public func & (lhs: Int, rhs: BigInteger) -> BigInteger { return rhs.bitwiseAnd(lhs) } -public func &= (inout lhs: BigInteger, rhs: BigInteger) { +public func &= (lhs: inout BigInteger, rhs: BigInteger) { lhs = lhs.bitwiseAnd(rhs) } -public func &= (inout lhs: BigInteger, rhs: Int) { +public func &= (lhs: inout BigInteger, rhs: Int) { lhs = lhs.bitwiseAnd(rhs) } @@ -212,7 +212,7 @@ public func << (lhs: BigInteger, rhs: Int32) -> BigInteger { return lhs.shiftLeft(rhs) } -public func <<= (inout lhs: BigInteger, rhs: Int32) { +public func <<= (lhs: inout BigInteger, rhs: Int32) { lhs = lhs.shiftLeft(rhs) } @@ -222,6 +222,6 @@ public func >> (lhs: BigInteger, rhs: Int32) -> BigInteger { return lhs.shiftRight(rhs) } -public func >>= (inout lhs: BigInteger, rhs: Int32) { +public func >>= (lhs: inout BigInteger, rhs: Int32) { lhs = lhs.shiftRight(rhs) } diff --git a/Source/Utils.swift b/Source/Utils.swift index d3c65c3..d30e08d 100644 --- a/Source/Utils.swift +++ b/Source/Utils.swift @@ -10,14 +10,17 @@ import Foundation struct Utils { static let base36Digits: String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" - static func canParseBigIntegerFromString(var string: String, withRadix radix: Int) -> Bool { + static func canParseBigIntegerFromString(_ string: String, withRadix radix: Int) -> Bool { + var string = string if string.hasPrefix("-") { - string.removeAtIndex(string.startIndex) + string.remove(at: string.startIndex) } - - let stringOfDigits = base36Digits.substringToIndex(advance(base36Digits.startIndex, radix)) - let characterSet = NSCharacterSet(charactersInString: stringOfDigits).invertedSet - - return (string.rangeOfCharacterFromSet(characterSet) == nil) + + let index = base36Digits.index(base36Digits.startIndex, offsetBy: radix) + let stringOfDigits = base36Digits.substring(to: index) + + let characterSet = NSCharacterSet(charactersIn: stringOfDigits).inverted + + return (string.rangeOfCharacter(from: characterSet) == nil) } -} \ No newline at end of file +} diff --git a/Tests/BigIntegerTests.swift b/Tests/BigIntegerTests.swift index 17749a4..a217946 100644 --- a/Tests/BigIntegerTests.swift +++ b/Tests/BigIntegerTests.swift @@ -12,13 +12,13 @@ import BigInteger class BigIntegerTests: XCTestCase { func testParse() { - println(BigInteger("123456789")!) - println(BigInteger(123456789)) + print(BigInteger("123456789")!) + print(BigInteger(123456789)) XCTAssert(BigInteger("123456789")! == BigInteger(123456789)) - var int1 = BigInteger(valueAsString: "FFFFFFFFFFFFFFFFFFFF", radix: 16) - var int2 = BigInteger(valueAsString: "11111111111111111111111111111111111111111111111111111111111111111111111111111111", radix: 2) + var int1 = BigInteger("FFFFFFFFFFFFFFFFFFFF", radix: 16) + var int2 = BigInteger("11111111111111111111111111111111111111111111111111111111111111111111111111111111", radix: 2) XCTAssert(int1 == int2) XCTAssertNil(BigInteger("--143524")) @@ -30,8 +30,8 @@ class BigIntegerTests: XCTestCase { } func testCompare() { - XCTAssert(BigInteger("1234567891999999999999999999999999") > BigInteger("1234567890999999999999999999999999")) - XCTAssert(BigInteger("1234567891999999999999999999999999") >= BigInteger("1234567890999999999999999999999999")) + XCTAssert(BigInteger("1234567891999999999999999999999999")! > BigInteger("1234567890999999999999999999999999")!) + XCTAssert(BigInteger("1234567891999999999999999999999999")! >= BigInteger("1234567890999999999999999999999999")!) } func testAdd() { @@ -49,17 +49,17 @@ class BigIntegerTests: XCTestCase { XCTAssert(int1 - int2 == int3) - int1 = BigInteger(valueAsString: "19b880f625283f859509e0e0f8fb3ac655", radix: 16)! - int2 = BigInteger(valueAsString: "10832cc998a0cefaea5d2971aa3456b685c3dbf036ed6105a3a2763a06a02ed8195dedac77cfab0f77", radix: 16)! - int3 = BigInteger(valueAsString: "-10832cc998a0cefaea5d2971aa3456b685c3dbf036ed610589e9f543e177ef5284540ccb7ed4704922", radix: 16)! + int1 = BigInteger("19b880f625283f859509e0e0f8fb3ac655", radix: 16)! + int2 = BigInteger("10832cc998a0cefaea5d2971aa3456b685c3dbf036ed6105a3a2763a06a02ed8195dedac77cfab0f77", radix: 16)! + int3 = BigInteger("-10832cc998a0cefaea5d2971aa3456b685c3dbf036ed610589e9f543e177ef5284540ccb7ed4704922", radix: 16)! XCTAssert(int1 - int2 == int3) } func testMultiply() { - var int1 = BigInteger(valueAsString: "163277057711765636560747447031217015017170155667415437655316770777137", radix: 8)! - var int2 = BigInteger(valueAsString: "7237215676720565020372546341703475553736747716372011567267761457600467467522634067670020467677527433525", radix: 8)! - var int3 = BigInteger(valueAsString: "1513401757356351454234764232211641316736616642643211617703006736112752224764721721321731200254174752010533741212752401377330537217165256452340012502354275047547302416757213", radix: 8)! + var int1 = BigInteger("163277057711765636560747447031217015017170155667415437655316770777137", radix: 8)! + var int2 = BigInteger("7237215676720565020372546341703475553736747716372011567267761457600467467522634067670020467677527433525", radix: 8)! + var int3 = BigInteger("1513401757356351454234764232211641316736616642643211617703006736112752224764721721321731200254174752010533741212752401377330537217165256452340012502354275047547302416757213", radix: 8)! XCTAssert(int1 * int2 == int3) } @@ -73,91 +73,88 @@ class BigIntegerTests: XCTestCase { } func testReminder() { - var int1 = BigInteger(valueAsString: "11ff84a0f3ef22b03e755bf7c119a69cb03aa09d13257bbb803ec8bb97ab1b09918fd21caccc226f4", radix: 16)! - var int2 = BigInteger(valueAsString: "121774a4a1", radix: 16)! - var int3 = BigInteger(valueAsString: "468b8fdbd", radix: 16)! + var int1 = BigInteger("11ff84a0f3ef22b03e755bf7c119a69cb03aa09d13257bbb803ec8bb97ab1b09918fd21caccc226f4", radix: 16)! + var int2 = BigInteger("121774a4a1", radix: 16)! + var int3 = BigInteger("468b8fdbd", radix: 16)! XCTAssert((int1 % int2)! == int3) } func testPower() { - var int1 = BigInteger(valueAsString: "352064660", radix: 8)! - var int2 = BigInteger(valueAsString: "11445440503765037264345614414201317562305075366370315205160001743510253761621003506311727224756206163364574662673072724535122730763362312735042210663525222433233235631510321502655772565647576737044053522616402745400000000000000000000000000000000000000", radix: 8)! + var int1 = BigInteger("352064660", radix: 8)! + var int2 = BigInteger("11445440503765037264345614414201317562305075366370315205160001743510253761621003506311727224756206163364574662673072724535122730763362312735042210663525222433233235631510321502655772565647576737044053522616402745400000000000000000000000000000000000000", radix: 8)! XCTAssert(int1.pow(29) == int2) } func testNegate() { - var int1 = BigInteger(valueAsString: "1234112389471982374123470172341324013217238471238947189375182937518923", radix: 10)! - var int2 = BigInteger(valueAsString: "-1234112389471982374123470172341324013217238471238947189375182937518923", radix: 10)! + var int1 = BigInteger("1234112389471982374123470172341324013217238471238947189375182937518923", radix: 10)! + var int2 = BigInteger("-1234112389471982374123470172341324013217238471238947189375182937518923", radix: 10)! XCTAssert(-int1 == int2) } func testXor() { - var int1 = BigInteger(valueAsString: "88fd2e", radix: 16)! - var int2 = BigInteger(valueAsString: "7f761ae982e3b55fe8", radix: 16)! - var int3 = BigInteger(valueAsString: "7f761ae982e33da2c6", radix: 16)! + var int1 = BigInteger("88fd2e", radix: 16)! + var int2 = BigInteger("7f761ae982e3b55fe8", radix: 16)! + var int3 = BigInteger("7f761ae982e33da2c6", radix: 16)! XCTAssert((int1 ^ int2) == int3) } func testOr() { - var int1 = BigInteger(valueAsString: "65634cc946ef5841549c020fa92f64", radix: 16)! - var int2 = BigInteger(valueAsString: "5dfa79fc90760ce1c50970b2ac6d23bd004df92040f72c8bebec", radix: 16)! - var int3 = BigInteger(valueAsString: "5dfa79fc90760ce1c50970f7ef6debffef5df974dcf72fabefec", radix: 16)! + var int1 = BigInteger("65634cc946ef5841549c020fa92f64", radix: 16)! + var int2 = BigInteger("5dfa79fc90760ce1c50970b2ac6d23bd004df92040f72c8bebec", radix: 16)! + var int3 = BigInteger("5dfa79fc90760ce1c50970f7ef6debffef5df974dcf72fabefec", radix: 16)! XCTAssert((int1 | int2) == int3) } func testAnd() { - var int1 = BigInteger(valueAsString: "13cb8fad8b3623ce10ee5b27ec7d77c03963109c872846", radix: 16)! - var int2 = BigInteger(valueAsString: "11d40ad4a69abd11e229e0e8b4fa71eb33db3", radix: 16)! - var int3 = BigInteger(valueAsString: "114008c00680b010c2016000943001c832802", radix: 16)! + var int1 = BigInteger("13cb8fad8b3623ce10ee5b27ec7d77c03963109c872846", radix: 16)! + var int2 = BigInteger("11d40ad4a69abd11e229e0e8b4fa71eb33db3", radix: 16)! + var int3 = BigInteger("114008c00680b010c2016000943001c832802", radix: 16)! XCTAssert((int1 & int2) == int3) } func testShiftLeft() { - var int1 = BigInteger(valueAsString: "8de59c88e581df0860ab", radix: 16)! - var int2 = BigInteger(valueAsString: "8de59c88e581df0860ab000000000000000000000", radix: 16)! + var int1 = BigInteger("8de59c88e581df0860ab", radix: 16)! + var int2 = BigInteger("8de59c88e581df0860ab000000000000000000000", radix: 16)! XCTAssert((int1 << 84) == int2) } func testShiftRight() { - var int1 = BigInteger(valueAsString: "3e0af7b136ad56a06a10f45bf", radix: 16)! - var int2 = BigInteger(valueAsString: "7c15ef626d5aad40d421", radix: 16)! - + var int1 = BigInteger("3e0af7b136ad56a06a10f45bf", radix: 16)! + var int2 = BigInteger("7c15ef626d5aad40d421", radix: 16)! + XCTAssert((int1 >> 19) == int2) } func testGCD() { - var int1 = BigInteger(valueAsString: "18f253d71e5309209bbaa2e9012ce7d", radix: 16)! - var int2 = BigInteger(valueAsString: "1bbdc63eb4dbb8cdea4eead7316f4e62ab8b85be4ae4832119a8f", radix: 16)! - var int3 = BigInteger(valueAsString: "11", radix: 16)! + var int1 = BigInteger("18f253d71e5309209bbaa2e9012ce7d", radix: 16)! + var int2 = BigInteger("1bbdc63eb4dbb8cdea4eead7316f4e62ab8b85be4ae4832119a8f", radix: 16)! + var int3 = BigInteger("11", radix: 16)! XCTAssert(int1.gcd(int2) == int3) } func testAbs() { - var int1 = BigInteger(valueAsString: "-1bbdc63eb4dbb8cdea4eead7316f4e62ab8b85be4ae4832119a8ffdfdfdf", radix: 16)! - var int2 = BigInteger(valueAsString: "1bbdc63eb4dbb8cdea4eead7316f4e62ab8b85be4ae4832119a8ffdfdfdf", radix: 16)! + var int1 = BigInteger("-1bbdc63eb4dbb8cdea4eead7316f4e62ab8b85be4ae4832119a8ffdfdfdf", radix: 16)! + var int2 = BigInteger("1bbdc63eb4dbb8cdea4eead7316f4e62ab8b85be4ae4832119a8ffdfdfdf", radix: 16)! XCTAssert(int1.abs() == int2) XCTAssert(int2.abs() == int2) } - - func testAchiving() { - let filename = "archive" - let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String - let filePath = documentsPath.stringByAppendingPathComponent(filename) - - let int1 = BigInteger("-123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523123471238940713294701327508917230516230561320512352315021305012395091032950923520395013258623185465463545681428354162345612435416523")! - NSKeyedArchiver.archiveRootObject(int1, toFile: filePath) - - let int2 = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as BigInteger - - XCTAssert(int1 == int2) + + func testExptmod() { + let int1 = BigInteger("-3")! + let power = BigInteger("3")! + let modulus = BigInteger("2")! + let int2 = BigInteger("1")! + + print("bytes: \(int1.byteArray)") + XCTAssert(int1.exptmod(power: power, modulus: modulus) == int2) } }