From c6e4e52754e512debe257d4d010f228eb5747e87 Mon Sep 17 00:00:00 2001 From: Leif Ibsen Date: Tue, 17 Sep 2019 16:09:30 +0200 Subject: [PATCH] 1.0.0 --- .gitignore | 4 + Package.swift | 28 + README.md | 35 + Sources/BigInt/BigInt.swift | 1617 +++++ Sources/BigInt/BitSieve.swift | 149 + Sources/BigInt/ExpMod.swift | 262 + Sources/BigInt/Karatsuba.swift | 76 + Sources/BigInt/Limbs.swift | 709 ++ Sources/BigInt/ToomCook.swift | 119 + Tests/BigIntTests/AdditionTest.swift | 80 + Tests/BigIntTests/BitTest.swift | 84 + Tests/BigIntTests/ConstructorTest.swift | 117 + Tests/BigIntTests/DivModTest.swift | 74 + Tests/BigIntTests/ExpModTest.swift | 71 + Tests/BigIntTests/GcdTest.swift | 61 + Tests/BigIntTests/KaratsubaTest.swift | 42 + Tests/BigIntTests/LimbTest.swift | 33 + Tests/BigIntTests/ModInverseTest.swift | 40 + Tests/BigIntTests/MulSquareTest.swift | 50 + Tests/BigIntTests/MultiplicationTest.swift | 89 + Tests/BigIntTests/PerformanceTest.swift | 97 + Tests/BigIntTests/PrimeTest.swift | 47 + Tests/BigIntTests/RootTest.swift | 45 + Tests/BigIntTests/ShiftTest.swift | 33 + Tests/BigIntTests/SubtractionTest.swift | 58 + Tests/BigIntTests/ToByteArrayTest.swift | 40 + Tests/BigIntTests/ToStringTest.swift | 48 + Tests/BigIntTests/ToomCookTest.swift | 41 + Tests/BigIntTests/XCTestManifests.swift | 9 + Tests/LinuxMain.swift | 7 + docs/Structs.html | 113 + docs/Structs/BInt.html | 5734 +++++++++++++++++ docs/Typealiases.html | 187 + docs/badge.svg | 28 + docs/css/highlight.css | 200 + docs/css/jazzy.css | 343 + docs/docsets/.docset/Contents/Info.plist | 20 + .../Contents/Resources/Documents/Structs.html | 113 + .../Resources/Documents/Structs/BInt.html | 5734 +++++++++++++++++ .../Resources/Documents/Typealiases.html | 187 + .../Resources/Documents/css/highlight.css | 200 + .../Resources/Documents/css/jazzy.css | 343 + .../Resources/Documents/img/carat.png | Bin 0 -> 274 bytes .../Contents/Resources/Documents/img/dash.png | Bin 0 -> 1338 bytes .../Contents/Resources/Documents/img/gh.png | Bin 0 -> 1571 bytes .../Contents/Resources/Documents/index.html | 103 + .../Contents/Resources/Documents/js/jazzy.js | 59 + .../Resources/Documents/js/jquery.min.js | 2 + .../Contents/Resources/Documents/search.json | 1 + .../.docset/Contents/Resources/docSet.dsidx | Bin 0 -> 28672 bytes docs/docsets/.tgz | Bin 0 -> 53847 bytes docs/img/carat.png | Bin 0 -> 274 bytes docs/img/dash.png | Bin 0 -> 1338 bytes docs/img/gh.png | Bin 0 -> 1571 bytes docs/index.html | 103 + docs/js/jazzy.js | 59 + docs/js/jquery.min.js | 2 + docs/search.json | 1 + docs/undocumented.json | 6 + 59 files changed, 17703 insertions(+) create mode 100644 .gitignore create mode 100644 Package.swift create mode 100644 README.md create mode 100755 Sources/BigInt/BigInt.swift create mode 100755 Sources/BigInt/BitSieve.swift create mode 100755 Sources/BigInt/ExpMod.swift create mode 100755 Sources/BigInt/Karatsuba.swift create mode 100755 Sources/BigInt/Limbs.swift create mode 100755 Sources/BigInt/ToomCook.swift create mode 100755 Tests/BigIntTests/AdditionTest.swift create mode 100755 Tests/BigIntTests/BitTest.swift create mode 100755 Tests/BigIntTests/ConstructorTest.swift create mode 100755 Tests/BigIntTests/DivModTest.swift create mode 100755 Tests/BigIntTests/ExpModTest.swift create mode 100755 Tests/BigIntTests/GcdTest.swift create mode 100755 Tests/BigIntTests/KaratsubaTest.swift create mode 100755 Tests/BigIntTests/LimbTest.swift create mode 100755 Tests/BigIntTests/ModInverseTest.swift create mode 100755 Tests/BigIntTests/MulSquareTest.swift create mode 100755 Tests/BigIntTests/MultiplicationTest.swift create mode 100755 Tests/BigIntTests/PerformanceTest.swift create mode 100755 Tests/BigIntTests/PrimeTest.swift create mode 100644 Tests/BigIntTests/RootTest.swift create mode 100755 Tests/BigIntTests/ShiftTest.swift create mode 100755 Tests/BigIntTests/SubtractionTest.swift create mode 100644 Tests/BigIntTests/ToByteArrayTest.swift create mode 100755 Tests/BigIntTests/ToStringTest.swift create mode 100755 Tests/BigIntTests/ToomCookTest.swift create mode 100755 Tests/BigIntTests/XCTestManifests.swift create mode 100644 Tests/LinuxMain.swift create mode 100644 docs/Structs.html create mode 100644 docs/Structs/BInt.html create mode 100644 docs/Typealiases.html create mode 100644 docs/badge.svg create mode 100644 docs/css/highlight.css create mode 100644 docs/css/jazzy.css create mode 100644 docs/docsets/.docset/Contents/Info.plist create mode 100644 docs/docsets/.docset/Contents/Resources/Documents/Structs.html create mode 100644 docs/docsets/.docset/Contents/Resources/Documents/Structs/BInt.html create mode 100644 docs/docsets/.docset/Contents/Resources/Documents/Typealiases.html create mode 100644 docs/docsets/.docset/Contents/Resources/Documents/css/highlight.css create mode 100644 docs/docsets/.docset/Contents/Resources/Documents/css/jazzy.css create mode 100755 docs/docsets/.docset/Contents/Resources/Documents/img/carat.png create mode 100755 docs/docsets/.docset/Contents/Resources/Documents/img/dash.png create mode 100755 docs/docsets/.docset/Contents/Resources/Documents/img/gh.png create mode 100644 docs/docsets/.docset/Contents/Resources/Documents/index.html create mode 100755 docs/docsets/.docset/Contents/Resources/Documents/js/jazzy.js create mode 100644 docs/docsets/.docset/Contents/Resources/Documents/js/jquery.min.js create mode 100644 docs/docsets/.docset/Contents/Resources/Documents/search.json create mode 100644 docs/docsets/.docset/Contents/Resources/docSet.dsidx create mode 100644 docs/docsets/.tgz create mode 100755 docs/img/carat.png create mode 100755 docs/img/dash.png create mode 100755 docs/img/gh.png create mode 100644 docs/index.html create mode 100755 docs/js/jazzy.js create mode 100644 docs/js/jquery.min.js create mode 100644 docs/search.json create mode 100644 docs/undocumented.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..02c0875 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..465560a --- /dev/null +++ b/Package.swift @@ -0,0 +1,28 @@ +// swift-tools-version:5.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "BigInt", + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "BigInt", + targets: ["BigInt"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages which this package depends on. + .target( + name: "BigInt", + dependencies: []), + .testTarget( + name: "BigIntTests", + dependencies: ["BigInt"]), + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f0ce93 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +

Description

+ +The BigInt package provides arbitrary-precision integer arithmetic in Swift. +It contains functionality comparable to that of the Java BigInteger class. + +BigInt requires Swift 5.0. + + +

Usage

+Using Swift Package Manager, in your projects Package.swift file add a dependency like
+``` +dependencies: [ +// Dependencies declare other packages that this package depends on. +.package(url: "https://github.com/leif-ibsen/BigInt", from: "1.0.0"), +], +``` + +

References

+ +Algorithms from the following books have been used in the implementation. +There are references in the source code where appropriate. + + + + +

Acknowledgement

+ +The BitSieve class used in the implementation is a translation to Swift of the corresponding class from Java BigInteger. +The GCD algorithm and the Karatsuba and ToomCook multiplication algorithms are modelled after the corresponding algorithms in Java BigInteger. + diff --git a/Sources/BigInt/BigInt.swift b/Sources/BigInt/BigInt.swift new file mode 100755 index 0000000..1e34959 --- /dev/null +++ b/Sources/BigInt/BigInt.swift @@ -0,0 +1,1617 @@ +// +// BigInt.swift +// BigInt +// +// Created by Leif Ibsen on 24/12/2018. +// Copyright © 2018 Leif Ibsen. All rights reserved. +// + +/// Unsigned 8 bit value +public typealias Byte = UInt8 +/// Array of unsigned 8 bit values +public typealias Bytes = [UInt8] + +/// Unsigned 64 bit value +public typealias Limb = UInt64 +/// Array of unsigned 64 bit values +public typealias Limbs = [UInt64] + +precedencegroup ExponentiationPrecedence { + associativity: left + higherThan: MultiplicationPrecedence + lowerThan: BitwiseShiftPrecedence +} + +infix operator ** : ExponentiationPrecedence + +/// A signed integer of unbounded size. +/// A BInt value is represented with magnitude and sign. +/// The magnitude is an array of unsigned 64 bit integers (a.k.a. Limbs). +/// The sign is a boolean value, *true* means value < 0, *false* means value >= 0 +/// The representation is little-endian, least significant Limb has index 0. +/// The representation is minimal, there is no leading zero Limbs. +/// The exception is that the value 0 is represented as a single 64 bit zero Limb and sign *false* +public struct BInt: CustomStringConvertible, Equatable, Hashable { + + static let digits: [Character] = [ + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", + "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] + + + // MARK: - Constants + + /// BInt(0) + public static let ZERO = BInt(0) + /// BInt(1) + public static let ONE = BInt(1) + /// BInt(2) + public static let TWO = BInt(2) + /// BInt(3) + public static let THREE = BInt(3) + /// BInt(10) + public static let TEN = BInt(10) + + + // MARK: - Initializers + + /// Constructs a BInt from magnitude and sign + /// + /// - Parameters: + /// - magnitude: magnitude of value + /// - isNegative: *true* means negative value, *false* means 0 or positive value, default is *false* + public init(_ magnitude: Limbs, _ isNegative : Bool = false) { + self.magnitude = magnitude + self.magnitude.normalize() + self.isNegative = isNegative + if self.isZero { + self.isNegative = false + } + } + + /// Constructs a BInt from an Int value + /// + /// - Parameter x: Int value + public init(_ x: Int) { + if x == Int.min { + self.init([0x8000000000000000], true) + } else if x < 0 { + self.init([Limb(-x)], true) + } else { + self.init([Limb(x)], false) + } + } + + /// Constructs a BInt from a String value and radix + /// + /// - Parameters: + /// - x: String value to be converted + /// - radix: Radix of x, from 2 to 36 inclusive, default is 10 + /// + /// Examples: + /// * BInt("90abcdef", radix = 16) + /// * BInt("111110010", radix = 2) + /// * BInt("1cdefghijk44", radix = 26) + public init?(_ x: String, radix: Int = 10) { + if radix < 2 || radix > 36 { + return nil + } + var number = x + var sign: Bool + if x.hasPrefix("-") { + sign = true + number.remove(at: number.startIndex) + } else { + sign = false + } + var magnitude = [Limb(0)] + var base = [Limb(1)] + let lrdx = [Limb(radix)] + for s in number.reversed() { + if let digit = BInt.digits.firstIndex(of: s) { + let d = digit < 36 ? digit : digit - 26 + if d >= radix { + return nil + } + magnitude.add(base.times([Limb(d)])) + base.multiply(lrdx) + } else { + return nil + } + } + self.init(magnitude, sign) + } + + /// Constructs a random BInt with a specified number of bits + /// + /// - Precondition: bitWidth is positive + /// - Parameter bitWidth: Number of bits + public init(bitWidth: Int) { + precondition(bitWidth > 0, "Bitwidth must be positive") + let (q, r) = bitWidth.quotientAndRemainder(dividingBy: 64) + var limbs = Limbs(repeating: 0, count: r == 0 ? q : q + 1) + BInt.randomLimbs(&limbs) + if r > 0 { + limbs[limbs.count - 1] <<= 64 - r + limbs[limbs.count - 1] >>= 64 - r + } + self.init(limbs) + } + + /// Constructs a BInt from a big-endian magnitude byte array representation + /// + /// - Precondition: Byte array is not empty + /// - Parameter x: Magnitude big-endian byte array + /// + /// Examples: + /// * The byte array [1, 0, 0] represents BInt value 65536 + /// * The byte array [128, 0] represents BInt value 32768 + /// * The byte array [255, 127] represents BInt value 65407 + public init(magnitude x: Bytes) { + precondition(!x.isEmpty, "Empty byte array") + var bb = x + if bb[0] > 127 { + bb.insert(0, at: 0) + } + self.init(signed: bb) + } + + /// Constructs a BInt from a big-endian 2's complement byte array representation + /// + /// - Precondition: Byte array is not empty + /// - Parameter x: 2's complement big-endian byte array + /// + /// Examples: + /// * The byte array [1, 0, 0] represents BInt value 65536 + /// * The byte array [128, 0] represents BInt value -32768 + /// * The byte array [255, 127] represents BInt value -129 + public init(signed x: Bytes) { + precondition(!x.isEmpty, "Empty byte array") + self.isNegative = x[0] > 127 + var bb = x + if self.isNegative { + while bb.count > 1 && bb[0] == 255 { + bb.remove(at: 0) + } + } else { + while bb.count > 1 && bb[0] == 0 { + bb.remove(at: 0) + } + } + if self.isNegative { + var carry = true + var bbi = bb.count + for _ in 0 ..< bb.count { + bbi -= 1 + bb[bbi] = ~bb[bbi] + if carry { + if bb[bbi] == 255 { + bb[bbi] = 0 + } else { + bb[bbi] += 1 + carry = false + } + } + } + if carry { + bb.insert(1, at: 0) + } + } + let chunks = bb.count / 8 + let remaining = bb.count - chunks * 8 + self.magnitude = Limbs(repeating: 0, count: chunks + (remaining == 0 ? 0 : 1)) + var bi = 0 + var li = self.magnitude.count + if remaining > 0 { + li -= 1 + } + for _ in 0 ..< remaining { + self.magnitude[li] <<= 8 + self.magnitude[li] |= Limb(bb[bi]) + bi += 1 + } + for _ in 0 ..< chunks { + li -= 1 + for _ in 0 ..< 8 { + self.magnitude[li] <<= 8 + self.magnitude[li] |= Limb(bb[bi]) + bi += 1 + } + } + } + + + // MARK: Stored properties + + /// The sign, *true* if *self* < 0, *false* otherwise + public internal(set) var isNegative: Bool + + /// The magnitude limb array + public internal(set) var magnitude: Limbs + + + // MARK: Computed properties + + /// The number of bits in the magnitude of *self*. 0 if *self* = 0 + public var bitWidth: Int { + return self.magnitude.bitWidth + } + + /// Base 10 string value of *self* + public var description: String { + return self.asString() + } + + /// Is *true* if *self* is even, *false* if *self* is odd + public var isEven: Bool { + return self.magnitude[0] & 1 == 0 + } + + /// Is *false* if *self* = 0, *true* otherwise + public var isNotZero: Bool { + return self.magnitude.count > 1 || self.magnitude[0] > 0 + } + + /// Is *true* if *self* is odd, *false* if *self* is even + public var isOdd: Bool { + return self.magnitude[0] & 1 == 1 + } + + /// Is *true* if *self* = 1, *false* otherwise + public var isOne: Bool { + return self.magnitude.count == 1 && self.magnitude[0] == 1 && !self.isNegative + } + + /// Is *true* if *self* > 0, *false* otherwise + public var isPositive: Bool { + return !self.isNegative && self.isNotZero + } + + /// Is *true* if *self* = 0, *false* otherwise + public var isZero: Bool { + return self.magnitude.count == 1 && self.magnitude[0] == 0 + } + + /// The number of leading zero bits in the magnitude of *self*. 0 if *self* = 0 + public var leadingZeroBitCount: Int { + return self.isZero ? 0 : self.magnitude.last!.leadingZeroBitCount + } + + /// Is 0 if *self* = 0, 1 if *self* > 0, and -1 if *self* < 0 + public var signum: Int { + return self.isZero ? 0 : (self.isNegative ? -1 : 1) + } + + /// The number of trailing zero bits in the magnitude of *self*. 0 if *self* = 0 + public var trailingZeroBitCount: Int { + return self.magnitude.trailingZeroBitCount() + } + + mutating func setSign(_ sign: Bool) { + self.isNegative = self.isZero ? false : sign + } + + + // MARK: Conversion functions to Int, String, and Bytes + + /// *self* as an Int + /// + /// - Returns: *self* as an Int or *nil* if *self* is not representable as an Int + public func asInt() -> Int? { + if self.magnitude.count > 1 { + return nil + } + let mag0 = self.magnitude[0] + if self.isNegative { + return mag0 > 0x8000000000000000 ? nil : (mag0 == 0x8000000000000000 ? Int.min : -Int(mag0)) + } else { + return mag0 < 0x8000000000000000 ? Int(mag0) : nil + } + } + + static let zeros = "000000000000000000000000000000000000000000000000000000000000000" + static let limbDigits = [0, 0, + 63, 40, 31, 27, 24, 22, 21, 20, 19, 18, 17, 17, 16, 16, 15, 15, 15, + 15, 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 12] + static let limbRadix = [BInt(0), BInt(0), + BInt([0x8000000000000000]), + BInt([0xa8b8b452291fe821]), + BInt([0x4000000000000000]), + BInt([0x6765c793fa10079d]), + BInt([0x41c21cb8e1000000]), + BInt([0x3642798750226111]), + BInt([0x8000000000000000]), + BInt([0xa8b8b452291fe821]), + BInt([0x8ac7230489e80000]), + BInt([0x4d28cb56c33fa539]), + BInt([0x1eca170c00000000]), + BInt([0x780c7372621bd74d]), + BInt([0x1e39a5057d810000]), + BInt([0x5b27ac993df97701]), + BInt([0x1000000000000000]), + BInt([0x27b95e997e21d9f1]), + BInt([0x5da0e1e53c5c8000]), + BInt([0xd2ae3299c1c4aedb]), + BInt([0x16bcc41e90000000]), + BInt([0x2d04b7fdd9c0ef49]), + BInt([0x5658597bcaa24000]), + BInt([0xa0e2073737609371]), + BInt([0xc29e98000000000]), + BInt([0x14adf4b7320334b9]), + BInt([0x226ed36478bfa000]), + BInt([0x383d9170b85ff80b]), + BInt([0x5a3c23e39c000000]), + BInt([0x8e65137388122bcd]), + BInt([0xdd41bb36d259e000]), + BInt([0xaee5720ee830681]), + BInt([0x1000000000000000]), + BInt([0x172588ad4f5f0981]), + BInt([0x211e44f7d02c1000]), + BInt([0x2ee56725f06e5c71]), + BInt([0x41c21cb8e1000000])] + + /// Byte array representation of magnitude value + /// + /// - Returns: Minimal big-endian magnitude byte array representation + /// + /// Examples: + /// * BInt(1).asMagnitudeBytes() = [1] + /// * BInt(-1).asMagnitudeBytes() = [1] + public func asMagnitudeBytes() -> Bytes { + var bb = (self.isNegative ? -self : self).asSignedBytes() + while bb.count > 1 && bb[0] == 0 { + bb.remove(at: 0) + } + return bb + } + + /// Byte array representation of 2's complement value + /// + /// - Returns: Minimal big-endian 2's complement byte array representation + /// + /// Examples: + /// * BInt(1).asSignedBytes() = [1] + /// * BInt(-1).asSignedBytes() = [255] + public func asSignedBytes() -> Bytes { + var xl = self.magnitude + if self.isNegative { + var carry = true + for i in 0 ..< xl.count { + xl[i] = ~xl[i] + if carry { + if xl[i] == Limb.max { + xl[i] = 0 + } else { + xl[i] += 1 + carry = false + } + } + } + } + var bb = Bytes(repeating: 0, count: xl.count * 8) + var bbi = bb.count + for i in 0 ..< xl.count { + var l = xl[i] + for _ in 0 ..< 8 { + bbi -= 1 + bb[bbi] = Byte(l & 0xff) + l >>= 8 + } + } + if self.isNegative { + if bb[0] < 128 { + bb.insert(255, at: 0) + } + while bb.count > 1 && bb[0] == 255 && bb[1] > 127 { + bb.remove(at: 0) + } + } else { + if bb[0] > 127 { + bb.insert(0, at: 0) + } + while bb.count > 1 && bb[0] == 0 && bb[1] < 128 { + bb.remove(at: 0) + } + } + return bb + } + + /// *self* as a String with a given radix + /// + /// - Precondition: Radix between 2 and 36 inclusive + /// - Parameters: + /// - radix: Radix from 2 to 36 inclusive + /// - uppercase: *true* to use uppercase letters, *false* to use lowercase letters, default is *false* + /// - Returns: *self* as a String in the given radix + public func asString(radix: Int = 10, uppercase: Bool = false) -> String { + precondition(radix >= 2 && radix <= 36, "Wrong radix \(radix)") + if self.isZero { + return "0" + } + let d = BInt.limbRadix[radix] + var digitGroups = [String]() + var tmp = BInt(self.magnitude) + while tmp.isNotZero { + let (q, r) = tmp.quotientAndRemainder(dividingBy: d) + digitGroups.append(r.isZero ? "0" : String(r.magnitude[0], radix: radix, uppercase: uppercase)) + tmp = q + } + var result = self.isNegative ? "-" : "" + result += digitGroups.last! + for i in (0 ..< digitGroups.count - 1).reversed() { + let leadingZeros = BInt.limbDigits[radix] - digitGroups[i].count + result += BInt.zeros.prefix(leadingZeros) + result += digitGroups[i] + } + return result + } + + static func toSignedBytesPair(_ x: BInt, _ y: BInt) -> (bx: Bytes, by: Bytes) { + var bx = x.asSignedBytes() + var by = y.asSignedBytes() + let x0 = bx[0] > Byte(0x7f) ? Byte(0xff) : 0 + let y0 = by[0] > Byte(0x7f) ? Byte(0xff) : 0 + while bx.count < by.count { + bx.insert(x0, at: 0) + } + while by.count < bx.count { + by.insert(y0, at: 0) + } + return (bx, by) + } + + + // MARK: Bit operation functions + + /// Bitwise **and** operator + /// + /// - Parameters: + /// - x: First value + /// - y: Second value + /// - Returns: BInt(signed: bx & by) where + /// - bx = x.asSignedBytes() + /// - by = y.asSignedBytes() + public static func &(x: BInt, y: BInt) -> BInt { + var (bx, by) = BInt.toSignedBytesPair(x, y) + for i in 0 ..< bx.count { + bx[i] &= by[i] + } + return BInt(signed: bx) + } + + /// x = x & y + /// + /// - Parameters: + /// - x: Left hand parameter + /// - y: Right hand parameter + public static func &=(x: inout BInt, y: BInt) { + x = x & y + } + + /// Bitwise **or** operator + /// + /// - Parameters: + /// - x: First value + /// - y: Second value + /// - Returns: BInt(signed: bx | by) where + /// - bx = x.asSignedBytes() + /// - by = y.asSignedBytes() + public static func |(x: BInt, y: BInt) -> BInt { + var (bx, by) = BInt.toSignedBytesPair(x, y) + for i in 0 ..< bx.count { + bx[i] |= by[i] + } + return BInt(signed: bx) + } + + /// x = x | y + /// + /// - Parameters: + /// - x: Left hand parameter + /// - y: Right hand parameter + public static func |=(x: inout BInt, y: BInt) { + x = x | y + } + + /// Bitwise **xor** operator + /// + /// - Parameters: + /// - x: First value + /// - y: Second value + /// - Returns: BInt(signed: bx ^ by) where + /// - bx = x.asSignedBytes() + /// - by = y.asSignedBytes() + public static func ^(x: BInt, y: BInt) -> BInt { + var (bx, by) = BInt.toSignedBytesPair(x, y) + for i in 0 ..< bx.count { + bx[i] ^= by[i] + } + return BInt(signed: bx) + } + + /// x = x ^ y + /// + /// - Parameters: + /// - x: Left hand parameter + /// - y: Right hand parameter + public static func ^=(x: inout BInt, y: BInt) { + x = x ^ y + } + + /// Bitwise **not** operator + /// + /// - Parameter x: BInt value + /// - Returns: -x - 1 + public static prefix func ~(x: BInt) -> BInt { + return -x - 1 + } + + /// Clear a specified bit + /// + /// - Parameter n: Bit number + public mutating func clearBit(_ n: Int) { + self.magnitude.setBitAt(n, to: false) + } + + /// Invert a specified bit + /// + /// - Parameter n: Bit number + public mutating func flipBit(_ n: Int) { + self.magnitude.setBitAt(n, to: !self.magnitude.getBitAt(n)) + } + + /// Set a specified bit + /// + /// - Parameter n: Bit number + public mutating func setBit(_ n: Int) { + self.magnitude.setBitAt(n, to: true) + } + + /// Test a specified bit + /// + /// - Parameter n: Bit number + /// - Returns: *true* if bit is set, *false* otherwise + public func testBit(_ n: Int) -> Bool { + return self.magnitude.getBitAt(n) + } + + + // MARK: Addition functions + + /// Prefix plus + /// + /// - Parameter x: BInt value + /// - Returns: x + public prefix static func +(x: BInt) -> BInt { + return x + } + + /// Addition + /// + /// - Parameters: + /// - x: First addend + /// - y: Second addend + /// - Returns: x + y + public static func +(x: BInt, y: BInt) -> BInt { + var sum = x + sum += y + return sum + } + + /// Addition + /// + /// - Parameters: + /// - x: First addend + /// - y: Second addend + /// - Returns: x + y + public static func +(x: Int, y: BInt) -> BInt { + var sum = y + sum += x + return sum + } + + /// Addition + /// + /// - Parameters: + /// - x: First addend + /// - y: Second addend + /// - Returns: x + y + public static func +(x: BInt, y: Int) -> BInt { + var sum = x + sum += y + return sum + } + + /// x = x + y + /// + /// - Parameters: + /// - x: Left hand addend + /// - y: Right hand addend + public static func +=(x: inout BInt, y: BInt) { + if x.isNegative == y.isNegative { + x.magnitude.add(y.magnitude) + } else { + let cmp = x.magnitude.difference(y.magnitude) + if cmp < 0 { + x.isNegative = !x.isNegative + } else if cmp == 0 { + x.isNegative = false + } + } + } + + /// x = x + y + /// + /// - Parameters: + /// - x: Left hand addend + /// - y: Right hand addend + public static func +=(x: inout BInt, y: Int) { + let absy = Limb(Swift.abs(y)) + if (y < 0 && x.isNegative) || (y >= 0 && !x.isNegative) { + x.magnitude.add(absy) + } else { + let cmp = x.magnitude.compare(absy) + x.magnitude.difference(absy) + if cmp < 0 { + x.isNegative = !x.isNegative + } else if cmp == 0 { + x.isNegative = false + } + } + } + + + // MARK: Negation functions + + /// Negates *self* + public mutating func negate() { + if self.isNotZero { + self.isNegative = !self.isNegative + } + } + + /// Negation + /// + /// - Parameter x: Operand + /// - Returns: -x + public static prefix func -(x: BInt) -> BInt { + var y = x + y.negate() + return y + } + + + // MARK: Subtraction functions + + /// Subtraction + /// + /// - Parameters: + /// - x: Minuend + /// - y: Subtrahend + /// - Returns: x - y + public static func -(x: BInt, y: BInt) -> BInt { + var diff = x + diff -= y + return diff + } + + /// Subtraction + /// + /// - Parameters: + /// - x: Minuend + /// - y: Subtrahend + /// - Returns: x - y + public static func -(x: Int, y: BInt) -> BInt { + var diff = y + diff -= x + return -diff + } + + /// Subtraction + /// + /// - Parameters: + /// - x: Minuend + /// - y: Subtrahend + /// - Returns: x - y + public static func -(x: BInt, y: Int) -> BInt { + var diff = x + diff -= y + return diff + } + + /// x = x - y + /// + /// - Parameters: + /// - x: Left hand minuend + /// - y: Right hand subtrahend + public static func -=(x: inout BInt, y: BInt) { + if x.isNegative == y.isNegative { + let cmp = x.magnitude.difference(y.magnitude) + if cmp < 0 { + x.isNegative = !x.isNegative + } else if cmp == 0 { + x.isNegative = false + } + } else { + x.magnitude.add(y.magnitude) + } + } + + /// x = x - y + /// + /// - Parameters: + /// - x: Left hand minuend + /// - y: Right hand subtrahend + public static func -=(x: inout BInt, y: Int) { + let absy = Limb(Swift.abs(y)) + if (y < 0 && !x.isNegative) || (y >= 0 && x.isNegative) { + x.magnitude.add(absy) + } else { + let cmp = x.magnitude.compare(absy) + x.magnitude.difference(absy) + if cmp < 0 { + x.isNegative = !x.isNegative + } else if cmp == 0 { + x.isNegative = false + } + } + } + + + // MARK: Multiplication functions + + /// Multiplication + /// + /// - Parameters: + /// - x: Multiplier + /// - y: Multiplicand + /// - Returns: x * y + public static func *(x: BInt, y: BInt) -> BInt { + var prod = x + prod *= y + return prod + } + + /// Multiplication + /// + /// - Parameters: + /// - x: Multiplier + /// - y: Multiplicand + /// - Returns: x * y + public static func *(x: Int, y: BInt) -> BInt { + var prod = y + prod *= x + return prod + } + + /// Multiplication + /// + /// - Parameters: + /// - x: Multiplier + /// - y: Multiplicand + /// - Returns: x * y + public static func *(x: BInt, y: Int) -> BInt { + var prod = x + prod *= y + return prod + } + + /// x = x * y + /// + /// - Parameters: + /// - x: Left hand multiplier + /// - y: Right hand multiplicand + public static func *=(x: inout BInt, y: BInt) { + x.magnitude.multiply(y.magnitude) + x.setSign(x.isNegative != y.isNegative) + } + + /// x = x * y + /// + /// - Parameters: + /// - x: Left hand multiplier + /// - y: Right hand multiplicand + public static func *=(x: inout BInt, y: Int) { + if y > 0 { + x.magnitude.multiply(Limb(y)) + } else if y < 0 { + x.magnitude.multiply(Limb(-y)) + x.setSign(!x.isNegative) + } else { + x = BInt.ZERO + } + } + + + // MARK: Division functions + + /// Division + /// + /// - Precondition: Divisor is not zero + /// - Parameter x: Divisor + /// - Returns: Quotient and remainder of *self* / x + public func quotientAndRemainder(dividingBy x: BInt) -> (quotient: BInt, remainder: BInt) { + var quotient = BInt.ZERO + var remainder = BInt.ZERO + self.quotientAndRemainder(dividingBy: x, "ient, &remainder) + return (quotient, remainder) + } + + /// Division + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Divisor + /// - quotient: Set to the quotient of *self* / x + /// - remainder: Set to the remainder of *self* / x + public func quotientAndRemainder(dividingBy x: BInt, _ quotient: inout BInt, _ remainder: inout BInt) { + self.magnitude.divMod(x.magnitude, "ient.magnitude, &remainder.magnitude) + quotient.setSign(self.isNegative != x.isNegative) + remainder.setSign(self.isNegative) + } + + /// Division + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Dividend + /// - y: Divisor + /// - Returns: x / y + public static func /(x: BInt, y: BInt) -> BInt { + return x.quotientAndRemainder(dividingBy: y).quotient + } + + /// Division + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Dividend + /// - y: Divisor + /// - Returns: x / y + public static func /(x: Int, y: BInt) -> BInt { + return BInt(x) / y + } + + /// Division + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Dividend + /// - y: Divisor + /// - Returns: x / y + public static func /(x: BInt, y: Int) -> BInt { + return x / BInt(y) + } + + /// x = x / y + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Left hand dividend + /// - y: Right hand divisor + public static func /=(x: inout BInt, y: BInt) { + x = x / y + } + + /// x = x / y + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Left hand dividend + /// - y: Right hand divisor + public static func /=(x: inout BInt, y: Int) { + x = x / y + } + + + // MARK: Remainder and modulus functions + + /// Remainder + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Dividend + /// - y: Divisor + /// - Returns: x % y + public static func %(x: BInt, y: BInt) -> BInt { + return x.quotientAndRemainder(dividingBy: y).remainder + } + + /// Remainder + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Dividend + /// - y: Divisor + /// - Returns: x % y + public static func %(x: Int, y: BInt) -> BInt { + return BInt(x) % y + } + + /// Remainder + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Dividend + /// - y: Divisor + /// - Returns: x % y + public static func %(x: BInt, y: Int) -> BInt { + return x % BInt(y) + } + + /// x = x % y + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Dividend + /// - y: Divisor + public static func %=(x: inout BInt,y: BInt) { + x = x % y + } + + /// x = x % y + /// + /// - Precondition: Divisor is not zero + /// - Parameters: + /// - x: Dividend + /// - y: Divisor + public static func %=(x: inout BInt, y: Int) { + x = x % y + } + + /// Modulus + /// + /// - Precondition: Divisor is not zero + /// - Parameter x: Divisor + /// - Returns: *self* *mod* x, a non-negative value + public func mod(_ x: BInt) -> BInt { + let r = self % x + if x.isNegative { + return r.isNegative ? r - x : r + } else { + return r.isNegative ? r + x : r + } + } + + /* + * Prime Numbers - algorithm 2.1.4 + * + * Return self modinverse m + */ + /// Inverse modulus + /// + /// - Precondition: *self* and modulus are coprime + /// - Parameter m: Modulus + /// - Returns: If *self* and m are coprime, x such that (*self* * x) mod m = 1 + public func modInverse(_ m: BInt) -> BInt { + var a = BInt.ONE + var b = BInt.ZERO + var g = self.mod(m) + var u = BInt.ZERO + var v = BInt.ONE + var w = m + while w.isPositive { + let q = g / w + (a, b, g, u, v, w) = (u, v, w, a - q * u, b - q * v, g - q * w) + } + precondition(g.isOne, "Modulus and self are not coprime") + return a.isNegative ? a + m : a + } + + + // MARK: Exponentiation functions + + /// Exponentiation + /// + /// - Precondition: Exponent is non-negative + /// - Parameters: + /// - a: Operand + /// - x: Non-negative exponent + /// - Returns: a^x + public static func **(a: BInt, x: Int) -> BInt { + precondition(x >= 0, "Exponent must be non-negative") + return x == 2 ? BInt(a.magnitude.squared()) : BInt(a.magnitude.raisedTo(x), a.isNegative && (x & 1 == 1)) + } + + /* + * Return (self ** x) mod m + * + * Use Barrett reduction algorithm for x.bitWidth < 512, else use Montgomery reduction algorithm + */ + /// Modular exponentiation + /// + /// - Precondition: Modulus is positive + /// - Parameters: + /// - x: The exponent + /// - m: The modulus, a positive number + /// - Returns: (*self*^x) mod m for positive x, ((*self*^-x) mod m) modInverse m for negative x + public func expMod(_ x: BInt, _ m: BInt) -> BInt { + precondition(m.isPositive, "Modulus must be positive") + if m.isOne { + return BInt.ZERO + } + let exponent = x.isNegative ? -x : x + var result: BInt + if exponent.magnitude.count < 9 { + result = BarrettModulus(self, m).expMod(exponent) + } else { + let trailing = m.trailingZeroBitCount + let pow2Modulus = BInt.ONE << trailing + let oddModulus = m >> trailing + if pow2Modulus.isOne { + result = OddModulus(self, oddModulus).expMod(exponent) + } else if oddModulus.isOne { + result = Pow2Modulus(self, pow2Modulus).expMod(exponent) + } else { + let pow2Mod = Pow2Modulus(self, pow2Modulus) + let oddMod = OddModulus(self, oddModulus) + let a1 = oddMod.expMod(exponent) + let a2 = pow2Mod.expMod(exponent) + let y1 = pow2Modulus.modInverse(oddModulus) + let y2 = oddModulus.modInverse(pow2Modulus) + result = (a1 * pow2Modulus * y1 + a2 * oddModulus * y2).mod(m) + } + } + if x.isNegative { + result = result.modInverse(m) + } + if self.isNegative { + return x.isEven || result.isZero ? result : m - result + } else { + return result + } + } + + + // MARK: Comparison functions + + /// Equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x = y, *false* otherwise + public static func ==(x: BInt, y: BInt) -> Bool { + return x.magnitude == y.magnitude && x.isNegative == y.isNegative + } + + /// Equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x = y, *false* otherwise + public static func ==(x: BInt, y: Int) -> Bool { + return x == BInt(y) + } + + /// Equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x = y, *false* otherwise + public static func ==(x: Int, y: BInt) -> Bool { + return BInt(x) == y + } + + /// Not equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x != y, *false* otherwise + public static func !=(x: BInt, y: BInt) -> Bool { + return x.magnitude != y.magnitude || x.isNegative != y.isNegative + } + + /// Not equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x != y, *false* otherwise + public static func !=(x: BInt, y: Int) -> Bool { + return x != BInt(y) + } + + /// Not equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x != y, *false* otherwise + public static func !=(x: Int, y: BInt) -> Bool { + return BInt(x) != y + } + + /// Less then + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x < y, *false* otherwise + public static func <(x: BInt, y: BInt) -> Bool { + if x.isNegative && y.isNegative { + return y.magnitude.lessThan(x.magnitude) + } else if !x.isNegative && y.isNegative { + return false + } else if x.isNegative && !y.isNegative { + return true + } else { + return x.magnitude.lessThan(y.magnitude) + } + } + + /// Less than + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x < y, *false* otherwise + public static func <(x: BInt, y: Int) -> Bool { + return x < BInt(y) + } + + /// Less than + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x < y, *false* otherwise + public static func <(x: Int, y: BInt) -> Bool { + return BInt(x) < y + } + + /// Greater than + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x > y, *false* otherwise + public static func >(x: BInt, y: BInt) -> Bool { + return y < x + } + + /// Greater than + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x > y, *false* otherwise + public static func >(x: Int, y: BInt) -> Bool { + return y < x + } + + /// Greater than + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x > y, *false* otherwise + public static func >(x: BInt, y: Int) -> Bool { + return y < x + } + + /// Less than or equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x <= y, *false* otherwise + public static func <=(x: BInt, y: BInt) -> Bool { + return !(y < x) + } + + /// Less than or equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x <= y, *false* otherwise + public static func <=(x: Int, y: BInt) -> Bool { + return !(y < x) + } + + /// Less than or equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x <= y, *false* otherwise + public static func <=(x: BInt, y: Int) -> Bool { + return !(y < x) + } + + /// Greater than or equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x >= y, *false* otherwise + public static func >=(x: BInt, y: BInt) -> Bool { + return !(x < y) + } + + /// Greater than or equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x >= y, *false* otherwise + public static func >=(x: Int, y: BInt) -> Bool { + return !(x < y) + } + + /// Greater than or equal + /// + /// - Parameters: + /// - x: First operand + /// - y: Second operand + /// - Returns: *true* if x >= y, *false* otherwise + public static func >=(x: BInt, y: Int) -> Bool { + return !(x < y) + } + + + // MARK: Shift functions + + /// Logical left shift + /// + /// - Parameters: + /// - x: Operand + /// - n: Shift count + /// - Returns: + /// - BInt(x.magnitude << n, x.isNegative) if n > 0 + /// - BInt(x.magnitude >> -n, x.isNegative) if n < 0 + /// - x if n = 0 + public static func <<(x: BInt, n: Int) -> BInt { + if n < 0 { + return x >> -n + } + return BInt(n == 1 ? x.magnitude.shifted1Left() : x.magnitude.shiftedLeft(n), x.isNegative) + } + + /// x = x << n + /// + /// - Parameters: + /// - x: Operand + /// - n: Shift count + public static func <<=(x: inout BInt, n: Int) { + if n < 0 { + x.magnitude.shiftRight(-n) + } else if n == 1 { + x.magnitude.shift1Left() + } else { + x.magnitude.shiftLeft(n) + } + } + + /// Logical right shift + /// + /// - Parameters: + /// - x: Operand + /// - n: Shift count + /// - Returns: + /// - BInt(x.magnitude >> n, x.isNegative) if n > 0 + /// - BInt(x.magnitude << -n, x.isNegative) if n < 0 + /// - x if n = 0 + public static func >>(x: BInt, n: Int) -> BInt { + if n < 0 { + return x << -n + } + return BInt(n == 1 ? x.magnitude.shifted1Right() : x.magnitude.shiftedRight(n), x.isNegative) + } + + /// x = x >> n + /// + /// - Parameters: + /// - x: Operand + /// - n: Shift count + public static func >>=(x: inout BInt, n: Int) { + if n < 0 { + x.magnitude.shiftLeft(-n) + } else if n == 1 { + x.magnitude.shift1Right() + } else { + x.magnitude.shiftRight(n) + } + if x.isZero { + x.isNegative = false + } + } + + + // MARK: Prime number functions + + static internal var random = SystemRandomNumberGenerator() + + static internal func randomBytes(_ bytes: inout Bytes) { + for i in 0 ..< bytes.count { + bytes[i] = BInt.random.next() + } + } + + static internal func randomLimbs(_ limbs: inout Limbs) { + for i in 0 ..< limbs.count { + limbs[i] = BInt.random.next() + } + } + + // Small prime product + static let SPP = BInt(3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 * 31 * 37 * 41) + + static func smallPrime(_ bitLength: Int) -> BInt { + let multiple8 = bitLength & 0x7 == 0 + let length = multiple8 ? (bitLength + 7) >> 3 + 1 : (bitLength + 7) >> 3 + var bytes = Bytes(repeating: 0, count: length) + let highBit = Byte(1 << ((bitLength + 7) & 0x7)) // High bit of high int + let highMask = Byte((Int(highBit) << 1) - 1) // Bits to keep in high int + + while true { + BInt.randomBytes(&bytes) + if multiple8 { + bytes[0] = 0 + bytes[1] = (bytes[1] & highMask) | highBit + } else { + bytes[0] = (bytes[0] & highMask) | highBit + } + let x = BInt(signed: bytes) + if bitLength > 6 { + let r = x % SPP + if r % 3 == 0 || r % 5 == 0 || r % 7 == 0 || r % 11 == 0 || r % 13 == 0 || r % 17 == 0 || + r % 19 == 0 || r % 23 == 0 || r % 29 == 0 || r % 31 == 0 || r % 37 == 0 || r % 41 == 0 { + continue + } + } + if x.isProbablyPrime() { + return x + } + } + } + + static func largePrime(_ bitLength: Int, _ p: Int) -> BInt { + var x = BInt(bitWidth: bitLength) + x.setBit(bitLength - 1) + x.clearBit(0) + var bs = BitSieve(x, p) + var candidate = bs.retrieve() + while candidate == nil || candidate!.bitWidth != bitLength { + x += BInt(2 * bs.length) + if x.bitWidth != bitLength { + x = BInt(bitWidth: bitLength) + x.setBit(bitLength - 1) + } + x.clearBit(0) + bs = BitSieve(x, p) + candidate = bs.retrieve() + } + return candidate! + } + + /// Checks whether *self* is prime using the Miller-Rabin algorithm + /// + /// - Parameter p: If *true* is returned, *self* is prime with probability > 1-1/2^p + /// - Returns: *true* if *self* is probably prime, *false* if *self* is definitely not prime + public func isProbablyPrime(_ p: Int = 30) -> Bool { + if self < 2 { + return false + } + var rounds: Int + if self.bitWidth < 100 { + rounds = 50 + } else if self.bitWidth < 256 { + rounds = 27 + } else if bitWidth < 512 { + rounds = 15 + } else if bitWidth < 768 { + rounds = 8 + } else if bitWidth < 1024 { + rounds = 4 + } else { + rounds = 2 + } + rounds = min((p + 1) / 2, rounds) + let s1 = self - 1 + for _ in 0 ..< rounds { + if !self.pass(s1.randomLessThan() + 1) { + return false + } + } + return true + } + + func pass(_ a: BInt) -> Bool { + let s_1 = self - 1 + let k = s_1.trailingZeroBitCount + let m = s_1 >> k + var x = a.expMod(m, self) + if x == 1 { + return true + } + if k > 0 { + for _ in 0 ..< k - 1 { + if x == s_1 { + return true + } + x = (x * x) % self + } + } + return x == s_1 + } + + /// A probable prime number with a given bitwidth + /// + /// - Parameters: + /// - bitWidth: The bitWidth + /// - p: The returned number is prime with probability > 1-1/2^p, default value is 30 + /// - Returns: A prime number with the specified bitwidth and probability + public static func probablePrime(_ bitWidth: Int, _ p: Int = 30) -> BInt { + return bitWidth < 100 ? smallPrime(bitWidth) : largePrime(bitWidth, p) + } + + + // MARK: Miscellaneous functions + + /// Absolute value + /// + /// - Returns: The absolute value of *self* + public func abs() -> BInt { + return BInt(self.magnitude) + } + + /// Greatest common divisor + /// + /// - Parameter x: Operand + /// - Returns: Greatest common divisor of magnitude of *self* and magnitude of x + public func gcd(_ x: BInt) -> BInt { + return BInt(self.magnitude.gcd(x.magnitude)) + } + + /* + * Prime Numbers - algorithm 2.3.5 + */ + /// Jacobi symbol + /// + /// - Parameters: + /// - m: An integer value + /// - Returns: The Jacobi symbol of *self* and m, or 0 if it does not exist + public func jacobiSymbol(_ m: BInt) -> Int { + var m1 = m + var a = self % m1 + var t = 1 + while a.isNotZero { + var x: BInt + while a.isEven { + a >>= 1 + x = m1 % 8 + if x == 3 || x == 5 { + t = -t + } + } + x = a + a = m1 + m1 = x + if a % 4 == 3 && m1 % 4 == 3 { + t = -t + } + a %= m1 + } + return m1 == 1 ? t : 0 + } + + /// Random value + /// + /// - Precondition: *self* is positive + /// - Returns: A random value < absolute value of *self* + public func randomLessThan() -> BInt { + precondition(self.isPositive, "Must be positive") + return BInt(bitWidth: self.bitWidth) % self + } + + /* + * Prime Numbers - exercise 4.11 + */ + /// n'th root of a non-negative number + /// + /// - Precondition: + /// - *self* is non-negative + /// - n is positive + /// - Parameter n: The root + /// - Returns: Largest x such that x^n <= *self* + public func root(_ n: Int) -> BInt { + precondition(!self.isNegative, "\(n)'th root of negative number") + precondition(n > 0, "non-positive root") + if self.isZero { + return BInt.ZERO + } + let bn = BInt(n) + let bn1 = bn - 1 + var x = BInt.ONE << (self.bitWidth / n + 1) + while true { + let xx = x ** (n - 1) + let y = (self / xx + x * bn1) / bn + if y >= x { + return x + } + x = y + } + } + + /* + * Prime Numbers - algorithm 9.2.11 + */ + /// Square root of a non-negative number + /// + /// - Precondition: *self* is non-negative + /// - Returns: Largest x such that x^2 <= *self* + public func sqrt() -> BInt { + precondition(!self.isNegative, "Square root of negative number") + if self.isZero { + return BInt.ZERO + } + var x = BInt.ONE << (self.bitWidth / 2 + 1) + while true { + let y = (self / x + x) >> 1 + if y >= x { + return x + } + x = y + } + } + + /* + * Prime Numbers - algorithm 2.3.8 + */ + /// Square root modulo a prime number + /// + /// - Parameter p: An odd prime number + /// - Returns: x, such that x^2 mod p = *self*, or *nil* if no such x exists + public func sqrtMod(_ p: BInt) -> BInt? { + let A = self % p + switch (p % 8).asInt() { + case 3, 7: + return A.expMod((p + 1) >> 2, p) + + case 5: + var x = A.expMod((p + 3) >> 3, p) + if (x * x) % p != A % p { + x = x * BInt.TWO.expMod((p - 1) >> 2, p) % p + } + return x + + case 1: + let p_1 = p - 1 + var d: BInt? + let p_3 = p - 3 + while true { + d = p_3.randomLessThan() + 2 + if d!.jacobiSymbol(p) == -1 { + break + } + } + var s = 0 + var t = p_1 + while t.isEven { + s += 1 + t >>= 1 + } + let A1 = A.expMod(t, p) + let D = d!.expMod(t, p) + var m = BInt.ZERO + var exp = BInt.TWO ** (s - 1) + for i in 0 ..< s { + let DD = (D ** m.asInt()!) * A1 + if DD.expMod(exp, p) == p_1 { + m.setBit(i) + } + exp >>= 1 + } + return (A.expMod((t + 1) >> 1, p) * D.expMod(m >> 1, p)) % p + + default: + return nil + } + } + +} diff --git a/Sources/BigInt/BitSieve.swift b/Sources/BigInt/BitSieve.swift new file mode 100755 index 0000000..3cd2b33 --- /dev/null +++ b/Sources/BigInt/BitSieve.swift @@ -0,0 +1,149 @@ +// +// BitSieve.swift +// BigInt +// +// Created by Leif Ibsen on 15/11/2018. +// Copyright © 2018 Leif Ibsen. All rights reserved. +// + +/* + * BitSieve class from Java BigInteger translated to Swift + */ +class BitSieve { + + var bits: [UInt64] + let length: Int + let base: BInt + let prob: Int + + static let smallSieve = BitSieve() + + init() { + self.length = 150 * 64 + self.bits = [UInt64](repeating: 0, count: ((length - 1) >> 6) + 1) + self.base = BInt.ZERO + self.prob = 0 + + // Mark 1 as composite + set(0) + var nextIndex = 1 + var nextPrime = 3 + var more = true + + // Find primes and remove their multiples from sieve + repeat { + sieveSingle(self.length, nextIndex + nextPrime, nextPrime) + (nextIndex, more) = sieveSearch(self.length, nextIndex + 1) + nextPrime = nextIndex << 1 + 1 + } while more && (nextPrime < self.length) + } + + /** + * Construct a bit sieve of searchLen bits used for finding prime number + * candidates. The new sieve begins at the specified base, which must + * be even. + */ + init(_ base: BInt, _ prob: Int) { + /* + * Candidates are indicated by clear bits in the sieve. As a candidates + * nonprimality is calculated, a bit is set in the sieve to eliminate + * it. To reduce storage space and increase efficiency, no even numbers + * are represented in the sieve (each bit in the sieve represents an + * odd number). + */ + self.base = base + self.length = 16 * base.bitWidth + self.bits = [UInt64](repeating: 0, count: ((self.length - 1) >> 6) + 1) + self.prob = prob + var start = 0 + + var (step, more) = BitSieve.smallSieve.sieveSearch(BitSieve.smallSieve.length, start) + var convertedStep = step << 1 + 1 + + // Construct the large sieve at an even offset specified by base + repeat { + // Calculate base mod convertedStep + let (_, r) = self.base.magnitude.divMod1(UInt64(convertedStep)) + start = Int(r) + + // Take each multiple of step out of sieve + start = convertedStep - start + if start & 1 == 0 { + start += convertedStep + } + sieveSingle(self.length, (start - 1) >> 1, convertedStep) + + // Find next prime from small sieve + (step, more) = BitSieve.smallSieve.sieveSearch(BitSieve.smallSieve.length, step + 1) + convertedStep = step << 1 + 1 + } while more + } + + /** + * Get the value of the bit at the specified index. + */ + func get(_ bitIndex: Int) -> Bool { + return (self.bits[bitIndex >> 6] & Limbs.UMasks[bitIndex & 0x3f]) != 0 + } + + /** + * Set the bit at the specified index. + */ + func set(_ bitIndex: Int) { + self.bits[bitIndex >> 6] |= Limbs.UMasks[bitIndex & 0x3f] + } + + /** + * This method returns the index of the first clear bit in the search + * array that occurs at or after start. It will not search past the + * specified limit. It returns false if there is no such clear bit. + */ + func sieveSearch(_ limit: Int, _ start: Int) -> (Int, Bool) { + if start >= limit { + return (0, false) + } + var index = start + repeat { + if !get(index) { + return (index, true) + } + index += 1 + } while index < limit - 1 + return (0, false) + } + + /** + * Sieve a single set of multiples out of the sieve. Begin to remove + * multiples of the specified step starting at the specified start index, + * up to the specified limit. + */ + func sieveSingle(_ limit: Int, _ start: Int, _ step: Int) { + var x = start + while x < limit { + set(x) + x += step + } + } + + /** + * Test probable primes in the sieve and return successful candidates. + */ + func retrieve() -> BInt? { + // Examine the sieve one word at a time to find possible primes + var offset = 1 + for i in 0 ..< bits.count { + let nextWord = ~bits[i] + for j in 0 ..< 64 { + if nextWord & Limbs.UMasks[j] != 0 { + let candidate = self.base + offset + if candidate.isProbablyPrime(prob) { + return candidate + } + } + offset += 2 + } + } + return nil + } + +} diff --git a/Sources/BigInt/ExpMod.swift b/Sources/BigInt/ExpMod.swift new file mode 100755 index 0000000..e9b1b79 --- /dev/null +++ b/Sources/BigInt/ExpMod.swift @@ -0,0 +1,262 @@ +// +// ExpMod.swift +// BigInt +// +// Created by Leif Ibsen on 17/02/2019. +// Copyright © 2019 Leif Ibsen. All rights reserved. +// + +extension BInt { + + /* + * Montgomery helper class for (a ** x) mod m computation, from + * + * Montgomery Multiplication + * By Henry S. Warren, Jr. + * July 2012 + */ + class OddModulus { + + let modulus: Limbs + let base: Limbs + let Rsize: Int + var Rinv: Limbs = [1] + var mprime: Limbs = [0] + + init(_ a: BInt, _ modulus: BInt) { + self.modulus = modulus.magnitude + self.Rsize = self.modulus.count + self.base = a.magnitude.divMod(self.modulus).remainder + + // Compute Rinv and mprime such that R * Rinv - modulus * mprime = 1 + + for _ in 0 ..< Rsize * 64 { + if self.Rinv[0] & 1 == 0 { + self.Rinv.shift1Right() + self.mprime.shift1Right() + } else { + self.Rinv.add(self.modulus) + self.Rinv.shift1Right() + self.mprime.shift1Right() + self.mprime.setBitAt(Rsize * 64 - 1, to: true) + } + } + } + + func toMspace(_ x: Limbs) -> Limbs { + return (x.shiftedLeft(self.Rsize * 64)).divMod(self.modulus).remainder + } + + func fromMspace(_ x: Limbs) -> Limbs { + return (x.times(self.Rinv)).divMod(self.modulus).remainder + } + + func moduloR(_ x: inout Limbs) { + if x.count > self.Rsize { + x.removeLast(x.count - self.Rsize) + } + x.normalize() + } + + func divideR(_ x: inout Limbs) { + if x.count > self.Rsize { + x.removeFirst(self.Rsize) + } else { + x = [0] + } + } + + func reduce(_ t: inout Limbs) { + var u = t + moduloR(&u) + u.multiply(self.mprime) + moduloR(&u) + u.multiply(self.modulus) + u.add(t) + divideR(&u) + if !u.lessThan(self.modulus) { + u.difference(self.modulus) + } + t = u + } + + // Handbook of Applied Cryptography - sliding window algorithm 14.85 + func expMod(_ x: BInt) -> BInt { + let bw = x.bitWidth + let k = bw < 200 ? 4 : (bw < 700 ? 5 : 6) + var g = Array(repeating: Limbs(repeating: 0, count: 0), count: 1 << k) + g[0] = toMspace(self.base) + var g2 = g[0] + g2.square() + reduce(&g2) + for i in 1 ..< g.count { + g[i] = g[i - 1].times(g2) + reduce(&g[i]) + } + var result = toMspace([1]) + var i = x.bitWidth - 1 + while i >= 0 { + if x.testBit(i) { + var l = max(0, i - k + 1) + while !x.testBit(l) { + l += 1 + } + for _ in 0 ..< i - l + 1 { + result.square() + reduce(&result) + } + var ndx = 0 + for j in (l ... i).reversed() { + ndx <<= 1 + if x.testBit(j) { + ndx += 1 + } + } + ndx >>= 1 + result.multiply(g[ndx]) + reduce(&result) + i = l - 1 + } else { + result.square() + reduce(&result) + i -= 1 + } + } + return BInt(fromMspace(result)) + } + } + + /* + * Helper class for (a ** x) mod m computation for m a power of 2 + */ + class Pow2Modulus { + + var limbCount = 0 + var mask: Limb = 0 + var base: Limbs + + init(_ a: BInt, _ modulus: BInt) { + let trailing = modulus.trailingZeroBitCount + self.limbCount = trailing / Limb.bitWidth + if trailing % Limb.bitWidth != 0 { + self.limbCount += 1 + } + for _ in 0 ..< trailing % Limb.bitWidth { + self.mask = (self.mask << 1) + 1 + } + self.base = a.magnitude + reduce(&self.base) + } + + func reduce(_ t: inout Limbs) { + let k = t.count - self.limbCount + if k > 0 { + t.removeLast(k) + } + if t.count == self.limbCount { + t[t.count - 1] &= self.mask + } + t.normalize() + } + + func expMod(_ x: BInt) -> BInt { + var exponent = x.magnitude + var result: Limbs = [1] + while true { + if exponent[0] & 1 == 1 { + result.multiply(self.base) + reduce(&result) + } + exponent.shift1Right() + if exponent.equalTo(0) { + break + } + self.base.square() + reduce(&self.base) + } + return BInt(result) + } + + } + + /* + * Helper class for (a ** x) mod m computation for small exponents + */ + class BarrettModulus { + + var base: Limbs + let modulus: Limbs + let u: Limbs + let k1: Int + let km1: Int + + init(_ a: BInt, _ modulus: BInt) { + self.modulus = modulus.magnitude + self.k1 = self.modulus.count + 1 + self.km1 = self.modulus.count - 1 + self.base = a.magnitude + var uu = Limbs(repeating: 0, count: 2 * self.modulus.count + 1) + uu[2 * self.modulus.count] = 1 + self.u = uu.divMod(self.modulus).quotient + self.base = self.base.divMod(self.modulus).remainder + } + + func moduloK1(_ x: inout Limbs) { + if x.count > self.k1 { + x.removeLast(x.count - self.k1) + } + x.normalize() + } + + func divideK(_ k: Int, _ x: inout Limbs) { + if x.count > k { + x.removeFirst(k) + } else { + x = [0] + } + } + + // Handbook of Applied Cryptography - Barrett reduction algorithm 14.42 + func reduce(_ t: inout Limbs) { + if self.modulus.compare(t) > 0 { + return + } + var q = t + divideK(self.km1, &q) + q.multiply(self.u) + divideK(self.k1, &q) + moduloK1(&t) + q.multiply(self.modulus) + moduloK1(&q) + if t.compare(q) < 0 { + var bk = Limbs(repeating: 0, count: self.k1) + bk[self.k1 - 1] = 1 + t.add(bk) + } + t.difference(q) + while t.compare(self.modulus) >= 0 { + t.difference(self.modulus) + } + } + + func expMod(_ x: BInt) -> BInt { + var exponent = x.magnitude + var result: Limbs = [1] + while true { + if exponent[0] & 1 == 1 { + result.multiply(self.base) + reduce(&result) + } + exponent.shift1Right() + if exponent.equalTo(0) { + break + } + self.base.square() + reduce(&self.base) + } + return BInt(result) + } + + } + +} diff --git a/Sources/BigInt/Karatsuba.swift b/Sources/BigInt/Karatsuba.swift new file mode 100755 index 0000000..47a2aec --- /dev/null +++ b/Sources/BigInt/Karatsuba.swift @@ -0,0 +1,76 @@ +// +// Karatsuba.swift +// BigInt +// +// Created by Leif Ibsen on 14/02/2019. +// Copyright © 2019 Leif Ibsen. All rights reserved. +// + +/* + * Karatsuba multiplication modelled after the Karatsuba algorithm in Java BigInteger + */ +extension Array where Element == Limb { + + func getUpper(_ k: Int) -> Limbs { + var w: Limbs + if self.count <= k { + w = [0] + } else { + w = Limbs(repeating: 0, count: self.count - k) + for i in 0 ..< w.count { + w[i] = self[k + i] + } + } + return w + } + + func getLower(_ k: Int) -> Limbs { + var w = Limbs(repeating: 0, count: k < self.count ? k : self.count) + for i in 0 ..< w.count { + w[i] = self[i] + } + return w + } + + func karatsubaTimes(_ x: Limbs) -> Limbs { + let k = (Swift.max(self.count, x.count) + 1) >> 1 + var x0 = x.getLower(k) + let x1 = x.getUpper(k) + var s0 = self.getLower(k) + let s1 = self.getUpper(k) + var w0 = x0 + w0.multiply(s0) + var w1 = x1 + w1.multiply(s1) + let xcmp = x0.difference(x1) + let scmp = s0.difference(s1) + x0.multiply(s0) + w0.add(w0, k) + w0.add(w1, k << 1) + w0.add(w1, k) + if xcmp * scmp < 0 { + w0.add(x0, k) + } else { + w0.subtract(x0, k) + } + return w0 + } + + func karatsubaSquared() -> Limbs { + let k = (self.count + 1) >> 1 + var s0 = self.getLower(k) + let s1 = self.getUpper(k) + var w0 = s0 + w0.square() + var w1 = s1 + w1.square() + s0.difference(s1) + s0.square() + w0.add(w0, k) + w0.add(w1, k << 1) + w0.add(w1, k) + w0.subtract(s0, k) + return w0 + } + +} diff --git a/Sources/BigInt/Limbs.swift b/Sources/BigInt/Limbs.swift new file mode 100755 index 0000000..2ac9847 --- /dev/null +++ b/Sources/BigInt/Limbs.swift @@ -0,0 +1,709 @@ +// +// Limbs.swift +// BigInt +// +// Created by Leif Ibsen on 24/12/2018. +// Copyright © 2018 Leif Ibsen. All rights reserved. +// + +extension Array where Element == Limb { + + static let UMasks: Limbs = [ + 0x1,0x2,0x4,0x8, + 0x10,0x20,0x40,0x80, + 0x100,0x200,0x400,0x800, + 0x1000,0x2000,0x4000,0x8000, + 0x10000,0x20000,0x40000,0x80000, + 0x100000,0x200000,0x400000,0x800000, + 0x1000000,0x2000000,0x4000000,0x8000000, + 0x10000000,0x20000000,0x40000000,0x80000000, + 0x100000000,0x200000000,0x400000000,0x800000000, + 0x1000000000,0x2000000000,0x4000000000,0x8000000000, + 0x10000000000,0x20000000000,0x40000000000,0x80000000000, + 0x100000000000,0x200000000000,0x400000000000,0x800000000000, + 0x1000000000000,0x2000000000000,0x4000000000000,0x8000000000000, + 0x10000000000000,0x20000000000000,0x40000000000000,0x80000000000000, + 0x100000000000000,0x200000000000000,0x400000000000000,0x800000000000000, + 0x1000000000000000,0x2000000000000000,0x4000000000000000,0x8000000000000000] + + mutating func normalize() { + let sc = self.count + if sc == 0 { + self.append(0) + } else if sc > 1 { + var i = sc - 1 + while self[i] == 0 && i > 0 { + i -= 1 + } + self.removeSubrange(i + 1 ..< sc) + } + } + + mutating func ensureSize(_ size: Int) { + self.reserveCapacity(size) + while self.count < size { + self.append(0) + } + } + + func trailingZeroBitCount() -> Int { + if self == [0] { + return 0 + } + var i = 0 + while self[i] == 0 { + i += 1 + } + return i * 64 + self[i].trailingZeroBitCount + } + + /* + * Bit operations + */ + + var bitWidth: Int { + var lastBits = 0 + var last = self.last! + while last != 0 { + last >>= 1 + lastBits += 1 + } + return (self.count - 1) * 64 + lastBits + } + + func getBitAt(_ i: Int) -> Bool { + let limbIndex = i >> 6 + if limbIndex >= self.count { + return false + } + return (self[limbIndex] & Limbs.UMasks[i & 0x3f]) != 0 + } + + mutating func setBitAt(_ i: Int, to bit: Bool) { + let limbIndex = i >> 6 + if limbIndex >= self.count && !bit { + return + } + self.ensureSize(limbIndex + 1) + if bit { + self[limbIndex] |= Limbs.UMasks[i & 0x3f] + } else { + self[limbIndex] &= ~Limbs.UMasks[i & 0x3f] + } + } + + /* + * Comparing + */ + + // return -1 if self < x, +1 if self > x, and 0 if self = x + func compare(_ x: Limbs) -> Int { + let scount = self.count + let xcount = x.count + if scount < xcount { + return -1 + } else if scount > xcount { + return 1 + } + var i = xcount - 1 + while i >= 0 { + if self[i] < x[i] { + return -1 + } else if self[i] > x[i] { + return 1 + } + i -= 1 + } + return 0 + } + + // return -1 if self < x, +1 if self > x, and 0 if self = x + func compare(_ x: Limb) -> Int { + if self.count > 1 { + return 1 + } + return self[0] == x ? 0 : (self[0] < x ? -1 : 1) + } + + // return self < x + func lessThan(_ x: Limbs) -> Bool { + return self.compare(x) < 0 + } + + // return self > x + func greaterThan(_ x: Limbs) -> Bool { + return self.compare(x) > 0 + } + + // return self = x + func equalTo(_ x: Limb) -> Bool { + return self.count == 1 && self[0] == x + } + + /* + * Shifting + */ + + // self = self << 1 + mutating func shift1Left() { + var b = self[0] & 0x8000000000000000 != 0 + self[0] <<= 1 + for i in 1 ..< self.count { + let b1 = self[i] & 0x8000000000000000 != 0 + self[i] <<= 1 + if b { + self[i] |= 1 + } + b = b1 + } + if b { + self.append(1) + } + } + + // self = self << shifts + mutating func shiftLeft(_ shifts: Int) { + let limbShifts = shifts >> 6 + let bitShifts = shifts & 0x3f + if bitShifts > 0 { + let b = self[self.count - 1] >> (64 - bitShifts) + for i in (0 ..< self.count).reversed() { + self[i] <<= bitShifts + if i > 0 { + self[i] |= self[i - 1] >> (64 - bitShifts) + } + } + self.append(b) + } + for _ in 0 ..< limbShifts { + self.insert(0, at: 0) + } + self.normalize() + } + + // return self << 1 + func shifted1Left() -> Limbs { + var res = self + res.shift1Left() + return res + } + + // return self << shifts + func shiftedLeft(_ shifts: Int) -> Limbs { + var res = self + res.shiftLeft(shifts) + return res + } + + // self = self >> 1 + mutating func shift1Right() { + for i in 0 ..< self.count { + if i > 0 && self[i] & 1 == 1 { + self[i - 1] |= 0x8000000000000000 + } + self[i] >>= 1 + } + self.normalize() + } + + // self = self >> shifts + mutating func shiftRight(_ shifts: Int) { + let limbShifts = Swift.min(shifts >> 6, self.count) + self.removeFirst(limbShifts) + let bitShifts = shifts & 0x3f + if bitShifts > 0 { + for i in 0 ..< self.count { + if i > 0 { + self[i - 1] |= self[i] << (64 - bitShifts) + } + self[i] >>= bitShifts + } + } + self.normalize() + } + + // return self >> 1 + func shifted1Right() -> Limbs { + var res = self + res.shift1Right() + return res + } + + // return self >> shifts + func shiftedRight(_ shifts: Int) -> Limbs { + var res = self + res.shiftRight(shifts) + return res + } + + /* + * Addition + */ + + // self[offset ..< self.count] = self[offset ..< self.count] + x + mutating func add(_ x: Limbs, _ offset: Int = 0, _ uselastcarry: Bool = true) { + if x.equalTo(0) { + return + } + self.ensureSize(x.count + offset) + var carry = false + for i in 0 ..< x.count { + let io = i + offset + if carry { + self[io] = self[io] &+ 1 + if self[io] == 0 { + self[io] = x[i] + // carry still lives + } else { + (self[io], carry) = self[io].addingReportingOverflow(x[i]) + } + } else { + (self[io], carry) = self[io].addingReportingOverflow(x[i]) + } + } + var i = x.count + offset + while carry && i < self.count { + self[i] = self[i] &+ 1 + carry = self[i] == 0 + i += 1 + } + if carry && uselastcarry { + self.append(1) + } + } + + // self = self + [x] + mutating func add(_ x: Limb) { + var carry: Bool + (self[0], carry) = self[0].addingReportingOverflow(x) + var i = 1 + while carry && i < self.count { + self[i] = self[i] &+ 1 + carry = self[i] == 0 + i += 1 + } + if carry { + self.append(1) + } + } + + /* + * Subtraction + */ + + // self[offset ..< self.count] = self[offset ..< self.count] - x, return borrow + mutating func subtract(_ x: Limbs, _ offset: Int) -> Bool { + self.ensureSize(x.count + offset) + var borrow = false + for i in 0 ..< x.count { + let io = i + offset + if borrow { + if self[io] == 0 { + self[io] = 0xffffffffffffffff - x[i] + // borrow still lives + } else { + self[io] -= 1 + (self[io], borrow) = self[io].subtractingReportingOverflow(x[i]) + } + } else { + (self[io], borrow) = self[io].subtractingReportingOverflow(x[i]) + } + } + var i = x.count + offset + while borrow && i < self.count { + self[i] = self[i] &- 1 + borrow = self[i] == 0xffffffffffffffff + i += 1 + } + return borrow + } + + // self = abs(self - x), return self.compare(x) + mutating func difference(_ x: Limbs) -> Int { + var xx = x + let cmp = self.compare(xx) + if cmp < 0 { + swap(&self, &xx) + } + var borrow = false + for i in 0 ..< xx.count { + if borrow { + if self[i] == 0 { + self[i] = 0xffffffffffffffff - xx[i] + // borrow still lives + } else { + self[i] -= 1 + (self[i], borrow) = self[i].subtractingReportingOverflow(xx[i]) + } + } else { + (self[i], borrow) = self[i].subtractingReportingOverflow(xx[i]) + } + } + var i = xx.count + while borrow && i < self.count { + self[i] = self[i] &- 1 + borrow = self[i] == 0xffffffffffffffff + i += 1 + } + self.normalize() + return cmp + } + + // self = abs(self - [x]), return self.compare([x]) + mutating func difference(_ x: Limb) -> Int { + var xx = [x] + let cmp = self.compare(xx) + if cmp < 0 { + swap(&self, &xx) + } + var borrow: Bool + (self[0], borrow) = self[0].subtractingReportingOverflow(xx[0]) + var i = 1 + while borrow && i < self.count { + self[i] = self[i] &- 1 + borrow = self[i] == 0xffffffffffffffff + i += 1 + } + self.normalize() + return cmp + } + + /* + * Multiplication + */ + + // Threshold for Karatsuba multplication + static let KA_THR = 100 + // Threshold for Toom Cook multplication + static let TC_THR = 200 + + // self = self * x + // Seminumerical Algorithms chapter 4.3.1 - algorithm M + mutating func multiply(_ x: Limbs) { + let m = self.count + var w: Limbs + if m > Limbs.KA_THR && x.count > Limbs.KA_THR { + if m > Limbs.TC_THR || x.count > Limbs.TC_THR { + w = self.toomCookTimes(x) + } else { + w = self.karatsubaTimes(x) + } + } else { + let n = x.count + w = Limbs(repeating: 0, count: m + n) + var carry: Limb + var ovfl1, ovfl2: Bool + for i in 0 ..< m { + carry = 0 + for j in 0 ..< n { + let (hi, lo) = self[i].multipliedFullWidth(by: x[j]) + (w[i + j], ovfl1) = w[i + j].addingReportingOverflow(lo) + (w[i + j], ovfl2) = w[i + j].addingReportingOverflow(carry) + carry = hi &+ ((ovfl1 ? 1 : 0) + (ovfl2 ? 1 : 0)) + } + w[i + n] = carry + } + } + w.normalize() + self = w + } + + // self = self * x + mutating func multiply(_ x: Limb) { + let m = self.count + var w = Limbs(repeating: 0, count: m + 1) + var carry: Limb + var ovfl1, ovfl2: Bool + for i in 0 ..< m { + carry = 0 + let (hi, lo) = self[i].multipliedFullWidth(by: x) + (w[i], ovfl1) = w[i].addingReportingOverflow(lo) + (w[i], ovfl2) = w[i].addingReportingOverflow(carry) + carry = hi &+ ((ovfl1 ? 1 : 0) + (ovfl2 ? 1 : 0)) + w[i + 1] = carry + } + w.normalize() + self = w + } + + func times(_ x: Limbs) -> Limbs { + var w = self + w.multiply(x) + return w + } + + // self = self * self + mutating func square() { + let m = self.count + var w: Limbs + if m > Limbs.TC_THR { + w = self.toomCookSquared() + } else if m > Limbs.KA_THR { + w = self.karatsubaSquared() + } else { + w = Limbs(repeating: 0, count: m << 1) + // Compute diagonal elements and divide by 2 + (w[1], w[0]) = self[0].multipliedFullWidth(by: self[0]) + w[0] >>= 1 + if w[1] & 1 == 1 { + w[0] |= 0x8000000000000000 + } + w[1] >>= 1 + var j = 2 + for i in 1 ..< m { + (w[j + 1], w[j]) = self[i].multipliedFullWidth(by: self[i]) + if w[j] & 1 == 1 { + w[j - 1] |= 0x8000000000000000 + } + w[j] >>= 1 + if w[j + 1] & 1 == 1 { + w[j] |= 0x8000000000000000 + } + w[j + 1] >>= 1 + j += 2 + } + // Add off-diagonal products + var carry: Limb + var ovfl1, ovfl2: Bool + for i in 0 ..< m { + carry = 0 + for j in i + 1 ..< m { + let (hi, lo) = self[i].multipliedFullWidth(by: self[j]) + (w[i + j], ovfl1) = w[i + j].addingReportingOverflow(lo) + (w[i + j], ovfl2) = w[i + j].addingReportingOverflow(carry) + carry = hi &+ ((ovfl1 ? 1 : 0) + (ovfl2 ? 1 : 0)) + } + (w[i + m], ovfl1) = w[i + m].addingReportingOverflow(carry) + if ovfl1 { + w[i + m + 1] += 1 + } + } + // Multiply by 2 and adjust last bit + w.shift1Left() + w[0] |= (self[0] & 1) + } + w.normalize() + self = w + } + + func squared() -> Limbs { + var w = self + w.square() + return w + } + + /* + * Division and modulus + */ + + // Hacker's Delight - algorithm 9.2 + // (hi || lo) / d => (q, r) + static func div128(_ hi: Limb, _ lo: Limb, _ d: Limb) -> (q: Limb, r: Limb) { + precondition(d > 0, "Division by zero") + var hi = hi + var lo = lo + for _ in 0 ..< 64 { + let t: Limb = hi & 0x8000000000000000 == 0 ? 0 : 0xffffffffffffffff + hi = (hi << 1) | (lo >> 63) + lo <<= 1 + if (hi | t) >= d { + hi = hi &- d + lo = lo &+ 1 + } + } + return (lo, hi) + } + + // Seminumerical Algorithms chapter 4.3.1 - exercise 16 + func divMod1(_ v: Limb) -> (quotient: Limbs, remainder: Limb) { + if self.equalTo(0) { + return ([0], 0) + } + var w = Limbs(repeating: 0, count: self.count) + var r = Limb(0) + for j in (0 ..< self.count).reversed() { + (w[j], r) = Limbs.div128(r, self[j], v) + } + w.normalize() + return (w, r) + } + + func divMod(_ v: Limbs) -> (quotient: Limbs, remainder: Limbs) { + var quotient = Limbs() + var remainder = Limbs() + self.divMod(v, "ient, &remainder) + return (quotient, remainder) + } + + // Seminumerical Algorithms chapter 4.3.1 - algorithm D + func divMod(_ v: Limbs, _ quotient: inout Limbs, _ remainder: inout Limbs) { + if self.lessThan(v) { + quotient = [0] + remainder = self + } else if v.count == 1 { + let (q, r) = self.divMod1(v[0]) + quotient = q + remainder = [r] + } else { + remainder = self + var v = v + let n = v.count + let m = remainder.count + let d = v[n - 1].leadingZeroBitCount + v.shiftLeft(d) + remainder.shiftLeft(d) + remainder.append(0) + var qhat = Limb(0) + var rhat = Limb(0) + var k = m - n + quotient = Limbs(repeating: 0, count: k + 1) + var ovfl: Bool + repeat { + if v[n - 1] == remainder[k + n] { + qhat = 0xffffffffffffffff + (rhat, ovfl) = remainder[k + n].addingReportingOverflow(remainder[k + n - 1]) + } else { + (qhat, rhat) = Limbs.div128(remainder[k + n], remainder[k + n - 1], v[n - 1]) + ovfl = false + } + while !ovfl { + let (hi, lo) = qhat.multipliedFullWidth(by: v[n - 2]) + if hi < rhat || (hi == rhat && lo <= remainder[k + n - 2]) { + break + } + qhat -= 1 + (rhat, ovfl) = rhat.addingReportingOverflow(v[n - 1]) + } + if qhat != 0 { + let borrow = remainder.subtract(v.times([qhat]), k) + if borrow { + qhat -= 1 + remainder.add(v, k, false) + } + } + quotient[k] = qhat + k -= 1 + } while k >= 0 + remainder.shiftRight(d) + quotient.normalize() + } + } + + /* + * gcd algorithms modelled after the gcd algorithms in Java BigInteger + */ + func gcd(_ x: Limbs) -> Limbs { + if self == [0] { + return x + } + var u = self + var v = x + while v != [0] { + if v.count == u.count { + return u.binaryGcd(v) + } + let r = u.divMod(v).remainder + u = v + v = r + } + return u + } + + func binaryGcd(_ x: Limbs) -> Limbs { + if self == [0] { + return x + } + var u = self + var v = x + let k = Swift.min(u.trailingZeroBitCount(), v.trailingZeroBitCount()) + u.shiftRight(k) + v.shiftRight(k) + var t: Limbs + var tSign: Bool + if u[0] & 1 == 1 { + t = v + tSign = true + } else { + t = u + tSign = false + } + while t != [0] { + t.shiftRight(t.trailingZeroBitCount()) + if tSign { + v = t + } else { + u = t + } + if u.count == 1 && v.count == 1 { + return Limbs.binaryGcd(u[0], v[0]).shiftedLeft(k) + } + let cmp = u.compare(v) + if cmp < 0 { + v.difference(u) + t = v + tSign = true + } else if cmp > 0 { + u.difference(v) + t = u + tSign = false + } else { + break + } + } + return u.shiftedLeft(k) + } + + /* + * gcd of single Limb values + */ + static func binaryGcd(_ a: Limb, _ b: Limb) -> Limbs { + if b == 0 { + return [a] + } + if a == 0 { + return [b] + } + var a = a + var b = b + let atz = a.trailingZeroBitCount + let btz = b.trailingZeroBitCount + a >>= atz + b >>= btz + let t = Swift.min(atz, btz) + while a != b { + if a > b { + a -= b + a >>= a.trailingZeroBitCount + } else { + b -= a + b >>= b.trailingZeroBitCount + } + } + return [a << t] + } + + /* + * Exponentiation + */ + + func raisedTo(_ x: Int) -> Limbs { + if x == 0 { + return [1] + } + if x == 1 { + return self + } + var base = self + var exponent = x + var y: Limbs = [1] + while exponent > 1 { + if exponent & 1 != 0 { + y.multiply(base) + } + base.square() + exponent >>= 1 + } + return base.times(y) + } +} + diff --git a/Sources/BigInt/ToomCook.swift b/Sources/BigInt/ToomCook.swift new file mode 100755 index 0000000..0fb9521 --- /dev/null +++ b/Sources/BigInt/ToomCook.swift @@ -0,0 +1,119 @@ +// +// ToomCook.swift +// BigInt +// +// Created by Leif Ibsen on 07/02/2019. +// Copyright © 2019 Leif Ibsen. All rights reserved. +// + +/* + * ToomCook multiplication modelled after the ToomCook algorithm in Java BigInteger + */ +extension Array where Element == Limb { + + static func divideBy3(_ d: BInt) -> BInt { + var quotient = Limbs(repeating: 0, count: d.magnitude.count) + var remainder = Limb(0) + for i in 0 ..< quotient.count { + let w = remainder > d.magnitude[i] ? remainder - d.magnitude[i] : d.magnitude[i] - remainder + let x = w &* 0xaaaaaaaaaaaaaaab + quotient[i] = x + remainder = x < 0x5555555555555556 ? 0 : (x < 0xaaaaaaaaaaaaaaab ? 1 : 2) + } + return BInt(quotient, d.isNegative) + } + + func getSlice(_ n: Int, _ k: Int) -> BInt { + var w: Limbs + if n * k >= self.count { + w = [] + } else if (n + 1) * k < self.count { + w = Limbs(repeating: 0, count: k) + } else { + w = Limbs(repeating: 0, count: self.count - n * k) + } + for i in 0 ..< w.count { + w[i] = self[n * k + i] + } + return BInt(w) + } + + func toomCookTimes(_ x: Limbs) -> Limbs { + let k = (Swift.max(self.count, x.count) + 2) / 3 + let x0 = x.getSlice(0, k) + let x1 = x.getSlice(1, k) + let x2 = x.getSlice(2, k) + let s0 = self.getSlice(0, k) + let s1 = self.getSlice(1, k) + let s2 = self.getSlice(2, k) + + let pp = x0 + x2 + let p1 = pp + x1 + let pm1 = pp - x1 + let pm2 = ((pm1 + x2) << 1) - x0 + let qq = s0 + s2 + let q1 = qq + s1 + let qm1 = qq - s1 + let qm2 = ((qm1 + s2) << 1) - s0 + + var r0 = x0 * s0 + let r1 = p1 * q1 + let rm1 = pm1 * qm1 + let rm2 = pm2 * qm2 + let rinf = x2 * s2 + var rr3 = Limbs.divideBy3(rm2 - r1) + var rr1 = (r1 - rm1) >> 1 + var rr2 = rm1 - r0 + + rr3 = (rr2 - rr3) >> 1 + (rinf << 1) + rr2 += rr1 + rr2 -= rinf + rr1 -= rr3 + + var offset = k << 2 + r0.magnitude.add(rinf.magnitude, offset) + offset -= k + r0.magnitude.add(rr3.magnitude, offset) + offset -= k + r0.magnitude.add(rr2.magnitude, offset) + r0.magnitude.add(rr1.magnitude, k) + return r0.magnitude + } + + func toomCookSquared() -> Limbs { + let k = (self.count + 2) / 3 + let s0 = self.getSlice(0, k) + let s1 = self.getSlice(1, k) + let s2 = self.getSlice(2, k) + + let qq = s0 + s2 + let q1 = qq + s1 + let qm1 = qq - s1 + let qm2 = ((qm1 + s2) << 1) - s0 + + var r0 = s0 ** 2 + let r1 = q1 ** 2 + let rm1 = qm1 ** 2 + let rm2 = qm2 ** 2 + let rinf = s2 ** 2 + + var rr3 = Limbs.divideBy3(rm2 - r1) + var rr1 = (r1 - rm1) >> 1 + var rr2 = rm1 - r0 + + rr3 = (rr2 - rr3) >> 1 + (rinf << 1) + rr2 += rr1 + rr2 -= rinf + rr1 -= rr3 + + var offset = k << 2 + r0.magnitude.add(rinf.magnitude, offset) + offset -= k + r0.magnitude.add(rr3.magnitude, offset) + offset -= k + r0.magnitude.add(rr2.magnitude, offset) + r0.magnitude.add(rr1.magnitude, k) + return r0.magnitude + } + +} diff --git a/Tests/BigIntTests/AdditionTest.swift b/Tests/BigIntTests/AdditionTest.swift new file mode 100755 index 0000000..0a0558c --- /dev/null +++ b/Tests/BigIntTests/AdditionTest.swift @@ -0,0 +1,80 @@ +// +// AdditionTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 17/01/2019. +// + +import XCTest + +class AdditionTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + XCTAssertEqual(BInt(7) + BInt(4), BInt(11)) + XCTAssertEqual(BInt(7) + BInt(-4), BInt(3)) + XCTAssertEqual(BInt(-7) + BInt(4), BInt(-3)) + XCTAssertEqual(BInt(-7) + BInt(-4), BInt(-11)) + XCTAssertEqual(BInt(-7) + BInt(0), BInt(-7)) + XCTAssertEqual(BInt(7) + BInt(0), BInt(7)) + XCTAssertEqual(BInt(7) + 4, BInt(11)) + XCTAssertEqual(BInt(7) + (-4), BInt(3)) + XCTAssertEqual(BInt(-7) + 4, BInt(-3)) + XCTAssertEqual(BInt(-7) + (-4), BInt(-11)) + XCTAssertEqual(BInt(-7) + 0, BInt(-7)) + XCTAssertEqual(BInt(7) + 0, BInt(7)) + XCTAssertEqual(7 + BInt(4), BInt(11)) + XCTAssertEqual(7 + BInt(-4), BInt(3)) + XCTAssertEqual((-7) + BInt(4), BInt(-3)) + XCTAssertEqual((-7) + BInt(-4), BInt(-11)) + XCTAssertEqual((-7) + BInt(0), BInt(-7)) + XCTAssertEqual(7 + BInt(0), BInt(7)) + } + + func test2() { + var x1 = BInt(7) + x1 += BInt(4) + XCTAssertEqual(x1, BInt(11)) + var x2 = BInt(7) + x2 += BInt(-4) + XCTAssertEqual(x2, BInt(3)) + var x3 = BInt(-7) + x3 += BInt(4) + XCTAssertEqual(x3, BInt(-3)) + var x4 = BInt(-7) + x4 += BInt(-4) + XCTAssertEqual(x4, BInt(-11)) + } + + func test3() { + for i in 0 ..< 1000 { + let a = i % 2 == 0 ? BInt(bitWidth: 1000) : -BInt(bitWidth: 1000) + let b = BInt(bitWidth: 800) + let n = BInt(bitWidth: 500) + let ab = a + b + let an = a * n + XCTAssertEqual(ab - a, b) + XCTAssertEqual(ab - b, a) + XCTAssertEqual(an - a, a * (n - 1)) + } + } + + func test4() { + for i in 0 ..< 1000 { + let a = i % 2 == 0 ? BInt(bitWidth: 1000) : -BInt(bitWidth: 1000) + var b = a + for _ in 0 ..< 100 { + b += a + } + XCTAssertEqual(b, a * 101) + } + } + +} diff --git a/Tests/BigIntTests/BitTest.swift b/Tests/BigIntTests/BitTest.swift new file mode 100755 index 0000000..0593eda --- /dev/null +++ b/Tests/BigIntTests/BitTest.swift @@ -0,0 +1,84 @@ +// +// BitTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 31/12/2018. +// + +import XCTest + +class BitTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func doTest(_ x1: BInt) { + var x2 = x1 + x2.negate() + XCTAssertEqual(x1, -x2) + } + + func test1() { + doTest(BInt(0)) + doTest(BInt(1)) + doTest(BInt(-1)) + doTest(BInt(bitWidth: 200)) + } + + func test2() { + let a = BInt(bitWidth: 300) + let b = BInt(bitWidth: 300) + let c = BInt(bitWidth: 300) + XCTAssertEqual(b ^ b ^ b, b) + XCTAssertEqual(a & (b | c), (a & b) | (a & c)) + } + + func test3() { + let a = BInt(bitWidth: 300) + var b = a + for i in 0 ..< 300 { + b.flipBit(i) + } + for i in 0 ..< 300 { + XCTAssertEqual(a.testBit(i), !b.testBit(i)) + } + } + + func test4() { + let a = BInt(bitWidth: 300) + XCTAssertEqual(a | BInt.ZERO, a) + XCTAssertEqual(a & BInt.ZERO, BInt.ZERO) + XCTAssertEqual(a & ~BInt.ZERO, a) + XCTAssertEqual(a ^ BInt.ZERO, a) + XCTAssertEqual(a ^ ~BInt.ZERO, ~a) + } + + func test5() { + let b3 = BInt(3) + let bm3 = BInt(-3) + let b7 = BInt(7) + let bm7 = BInt(-7) + XCTAssertEqual(b3 & b7, BInt(3)) + XCTAssertEqual(b3 & bm7, BInt(1)) + XCTAssertEqual(bm3 & b7, BInt(5)) + XCTAssertEqual(bm3 & bm7, BInt(-7)) + XCTAssertEqual(b3 | b7, BInt(7)) + XCTAssertEqual(b3 | bm7, BInt(-5)) + XCTAssertEqual(bm3 | b7, BInt(-1)) + XCTAssertEqual(bm3 | bm7, BInt(-3)) + XCTAssertEqual(b3 ^ b7, BInt(4)) + XCTAssertEqual(b3 ^ bm7, BInt(-6)) + XCTAssertEqual(bm3 ^ b7, BInt(-6)) + XCTAssertEqual(bm3 ^ bm7, BInt(4)) + XCTAssertEqual(~b3, BInt(-4)) + XCTAssertEqual(~bm3, BInt(2)) + XCTAssertEqual(~b7, BInt(-8)) + XCTAssertEqual(~bm7, BInt(6)) + } + +} diff --git a/Tests/BigIntTests/ConstructorTest.swift b/Tests/BigIntTests/ConstructorTest.swift new file mode 100755 index 0000000..568cff4 --- /dev/null +++ b/Tests/BigIntTests/ConstructorTest.swift @@ -0,0 +1,117 @@ +// +// ConstructorTest.swift +// XBigIntegerTests +// +// Created by Leif Ibsen on 11/12/2018. +// + +import XCTest + +class ConstructorTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + let x1 = BInt("123", radix: 10)! + XCTAssertEqual(x1.magnitude.count, 1) + XCTAssertFalse((x1.isNegative)) + let x2 = BInt("-0", radix: 10)! + XCTAssertEqual(x2.magnitude.count, 1) + XCTAssertFalse((x2.isNegative)) + let b: Bytes = [0, 0, 0, 123] + let x3 = BInt(signed: b) + XCTAssertEqual(x1, x3) + let x4 = BInt(magnitude: b) + XCTAssertEqual(x1, x4) + let x5 = BInt(bitWidth: 100) + XCTAssertTrue(x5.bitWidth <= 100) + let x6 = BInt("-123")! + XCTAssertEqual(x6.magnitude[0], 123) + let x71 = BInt(bitWidth: 100) + let x72 = BInt(signed: x71.asSignedBytes()) + XCTAssertEqual(x71, x72) + let x8 = BInt.ONE << 317 + XCTAssertEqual(x8.trailingZeroBitCount, 317) + let x9 = BInt(1) + XCTAssertFalse(x9.isEven) + XCTAssertTrue(x9.isOdd) + XCTAssertTrue(x9.isPositive) + XCTAssertFalse(x9.isNegative) + XCTAssertFalse(x9.isZero) + XCTAssertTrue(x9.isNotZero) + let x10 = BInt(-1) + XCTAssertFalse(x10.isEven) + XCTAssertTrue(x10.isOdd) + XCTAssertFalse(x10.isPositive) + XCTAssertTrue(x10.isNegative) + XCTAssertFalse(x10.isZero) + XCTAssertTrue(x10.isNotZero) + let x11 = BInt(0) + XCTAssertTrue(x11.isEven) + XCTAssertFalse(x11.isOdd) + XCTAssertFalse(x11.isPositive) + XCTAssertFalse(x11.isNegative) + XCTAssertTrue(x11.isZero) + XCTAssertFalse(x11.isNotZero) + let x12 = BInt(bitWidth: 1) + XCTAssertTrue(x12 == BInt.ONE || x12 == BInt.ZERO) + let x13 = BInt("12345670", radix: 8) + XCTAssertEqual(x13, BInt("2739128")) + let x14 = BInt("12345678", radix: 8) + XCTAssertEqual(x14, nil) + } + + func test2() { + let x0 = BInt(0) + let x1 = BInt(1) + let xm1 = BInt(-1) + XCTAssertEqual(x0.magnitude.count, 1) + XCTAssertEqual(x1.magnitude.count, 1) + XCTAssertEqual(xm1.magnitude.count, 1) + XCTAssertFalse(x0.isNegative) + XCTAssertFalse(x1.isNegative) + XCTAssertTrue(xm1.isNegative) + XCTAssertTrue(x0.isZero) + XCTAssertTrue(x1.isPositive) + XCTAssertTrue(xm1.isNegative) + } + + func test3() { + let x0 = BInt([0x8000000000000000], true) + XCTAssertEqual(x0.asInt(), Int.min) + let x1 = BInt(signed: [0]) + XCTAssertEqual(x1, BInt.ZERO) + let x2 = BInt(signed: [0, 255]) + XCTAssertEqual(x2, BInt(255)) + let x3 = BInt(signed: [255]) + XCTAssertEqual(x3, BInt(-1)) + let x4 = BInt(signed: [255, 0]) + XCTAssertEqual(x4, BInt(-256)) + let x5 = BInt(signed: [255, 255]) + XCTAssertEqual(x5, BInt(-1)) + } + + func test4() { + let x1 = BInt(magnitude: [0]) + XCTAssertEqual(x1, BInt.ZERO) + let x2 = BInt(magnitude: [0, 255]) + XCTAssertEqual(x2, BInt(255)) + let x3 = BInt(magnitude: [255]) + XCTAssertEqual(x3, BInt(255)) + let x4 = BInt(magnitude: [255, 0]) + XCTAssertEqual(x4, BInt(65280)) + let x5 = BInt(magnitude: [255, 255]) + XCTAssertEqual(x5, BInt(65535)) + let x6 = BInt(magnitude: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) + XCTAssertEqual(x6, BInt("fffffffffffffffffffffffffffffffeffffffffffffffff", radix: 16)!) + let x7 = BInt(signed: [0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]) + XCTAssertEqual(x7, BInt("fffffffffffffffffffffffffffffffeffffffffffffffff", radix: 16)!) + } + +} diff --git a/Tests/BigIntTests/DivModTest.swift b/Tests/BigIntTests/DivModTest.swift new file mode 100755 index 0000000..b92a738 --- /dev/null +++ b/Tests/BigIntTests/DivModTest.swift @@ -0,0 +1,74 @@ +// +// DivModTest.swift +// XBigIntegerTests +// +// Created by Leif Ibsen on 11/12/2018. +// + +import XCTest + +class DivModTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func doTest1(_ bw1: Int, _ bw2: Int) { + let x1 = BInt(bitWidth: bw1) + let x2 = BInt(bitWidth: bw2) + BInt.ONE + doTest2(x1, x2) + } + + func doTest2(_ x1: BInt, _ x2: BInt) { + let r1 = x1 % x2 + let q1 = x1 / x2 + XCTAssertEqual(x1, x2 * q1 + r1) + let (q2, r2) = x1.quotientAndRemainder(dividingBy: x2) + XCTAssertEqual(q1, q2) + XCTAssertEqual(r1, r2) + var q3 = BInt.ZERO + var r3 = BInt.ZERO + x1.quotientAndRemainder(dividingBy: x2, &q3, &r3) + XCTAssertEqual(q1, q3) + XCTAssertEqual(r1, r3) + } + + func test1() { + doTest1(30, 20) + doTest1(30, 120) + doTest1(130, 20) + doTest1(130, 120) + } + + func test2() { + XCTAssertEqual(BInt(7) % BInt(4), BInt(3)) + XCTAssertEqual(BInt(-7) % BInt(4), BInt(-3)) + XCTAssertEqual(BInt(7) % BInt(-4), BInt(3)) + XCTAssertEqual(BInt(-7) % BInt(-4), BInt(-3)) + XCTAssertEqual(BInt(7).mod(BInt(4)), BInt(3)) + XCTAssertEqual(BInt(-7).mod(BInt(4)), BInt(1)) + XCTAssertEqual(BInt(7).mod(BInt(-4)), BInt(3)) + XCTAssertEqual(BInt(-7).mod(BInt(-4)), BInt(1)) + doTest2(BInt(7), BInt(4)) + doTest2(BInt(-7), BInt(4)) + doTest2(BInt(7), BInt(-4)) + doTest2(BInt(-7), BInt(-4)) + doTest2(BInt(Limbs(repeating: UInt64.max, count: 50)), BInt(Limbs(repeating: UInt64.max, count: 35))) + } + + func test3() { + XCTAssertEqual(BInt(0) / BInt(7), BInt.ZERO) + XCTAssertEqual(-BInt(0) / BInt(7), BInt.ZERO) + XCTAssertEqual(BInt(0) / BInt(-7), BInt.ZERO) + XCTAssertEqual(-BInt(0) / BInt(-7), BInt.ZERO) + XCTAssertEqual(BInt(7) % BInt(7), BInt.ZERO) + XCTAssertEqual(BInt(-7) % BInt(7), BInt.ZERO) + XCTAssertEqual(BInt(7) % BInt(-7), BInt.ZERO) + XCTAssertEqual(BInt(-7) % BInt(-7), BInt.ZERO) + } + +} diff --git a/Tests/BigIntTests/ExpModTest.swift b/Tests/BigIntTests/ExpModTest.swift new file mode 100755 index 0000000..28409de --- /dev/null +++ b/Tests/BigIntTests/ExpModTest.swift @@ -0,0 +1,71 @@ +// +// ExpModTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 04/02/2019. +// + +import XCTest + +class ExpModTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + XCTAssertEqual(BInt(3).expMod(BInt(7), BInt(1)), BInt(0)) + XCTAssertEqual(BInt(2).expMod(BInt(10), BInt(1000)), BInt(24)) + } + + func test2() { + let a1 = BInt("43271512091896741409394736939900206368860872098952555939")! + let x1 = BInt("42267688843543983783885633938053687583065584419295560555")! + let m1 = BInt("65559338391610243479024015552681806795487318988418855174")! + let mp1 = BInt("14708028296754883426561209612579760556999800024666797837")! + XCTAssertEqual(a1.expMod(x1, m1), mp1) + XCTAssertEqual(a1.expMod(-x1, m1), mp1.modInverse(m1)) + XCTAssertEqual((-a1).expMod(x1, m1), m1 - mp1) + XCTAssertEqual((-a1).expMod(-x1, m1), (m1 - mp1).modInverse(m1)) + let a2 = BInt("9879959622001881798420480569351120749752891168795071469741009590796905186183625061410538508653929799901162907503196502223071902180994253404412067954774342232969326053454779870840130810532326017165678692636647404921424922403748460111140358572478743271512091896741409394736939900206368860872098952555939")! + let x2 = BInt("4149842346989426807721754542711895351513161856205879655378968612408032038996656528445703209766166328006052965811745650295935314936056815509075542554308409615827636639641110901357012471113482422183741588797481891328204616583065067700486989814417842267688843543983783885633938053687583065584419295560555")! + let m2 = BInt("10524302966485349118258764179820205386685991992586369700154893101599927732040662774460446149003080427232451962311367600902738242964142492968383265627950930467854069828393051189273332522792516344807937835132537814794042705435787095095919023768140765559338391610243479024015552681806795487318988418855174")! + let mp2 = BInt("1122200972247120546333043544356752277213829455746835691791369400547247327000588079094470008395425401827849174141816902264513461023891163414270218627387835660351885298195540293936217720560009323739879363833688454142584347044964970051959361123182603999640496505409063560777205949747764493609693376510835")! + XCTAssertEqual(a2.expMod(x2, m2), mp2) + XCTAssertEqual(a2.expMod(-x2, m2), mp2.modInverse(m2)) + XCTAssertEqual((-a2).expMod(x2, m2), m2 - mp2) + XCTAssertEqual((-a2).expMod(-x2, m2), (m2 - mp2).modInverse(m2)) + let a3 = BInt("2988348162058574136915891421498819466320163312926952423791023078876139")! + let x3 = BInt("2351399303373464486466122544523690094744975233415544072992656881240319")! + let m3 = BInt("10000000000000000000000000000000000000000")! + let mp3 = BInt("1527229998585248450016808958343740453059")! + XCTAssertEqual(a3.expMod(x3, m3), mp3) + XCTAssertEqual(a3.expMod(-x3, m3), mp3.modInverse(m3)) + XCTAssertEqual((-a3).expMod(x3, m3), m3 - mp3) + XCTAssertEqual((-a3).expMod(-x3, m3), (m3 - mp3).modInverse(m3)) + } + + func test3() { + XCTAssertEqual(BInt(-2).expMod(BInt(0), BInt(11)), BInt(1)) + XCTAssertEqual(BInt(2).expMod(BInt(0), BInt(11)), BInt(1)) + XCTAssertEqual(BInt(1).expMod(BInt(2), BInt(11)), BInt(1)) + XCTAssertEqual(BInt(1).expMod(BInt(-2), BInt(11)), BInt(1)) + XCTAssertEqual(BInt(-2).expMod(BInt(-3), BInt(11)), BInt(4)) + XCTAssertEqual(BInt(-11).expMod(BInt(2), BInt(11)), BInt(0)) + XCTAssertEqual(BInt(2).expMod(BInt(5), BInt(11)), BInt(10)) + XCTAssertEqual(BInt(2).expMod(BInt(-5), BInt(11)), BInt(10)) + XCTAssertEqual(BInt(2).expMod(BInt(4), BInt(11)), BInt(5)) + XCTAssertEqual(BInt(2).expMod(BInt(-4), BInt(11)), BInt(9)) + XCTAssertEqual(BInt(-2).expMod(BInt(5), BInt(11)), BInt(1)) + XCTAssertEqual(BInt(-2).expMod(BInt(-5), BInt(11)), BInt(1)) + XCTAssertEqual(BInt(-2).expMod(BInt(4), BInt(11)), BInt(5)) + XCTAssertEqual(BInt(-2).expMod(BInt(-4), BInt(11)), BInt(9)) + XCTAssertEqual(BInt(0).expMod(BInt(5), BInt(11)), BInt(0)) + XCTAssertEqual(BInt(0).expMod(BInt(4), BInt(11)), BInt(0)) + } + +} diff --git a/Tests/BigIntTests/GcdTest.swift b/Tests/BigIntTests/GcdTest.swift new file mode 100755 index 0000000..84f1237 --- /dev/null +++ b/Tests/BigIntTests/GcdTest.swift @@ -0,0 +1,61 @@ +// +// gcdTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 15/01/2019. +// + +import XCTest + +class GcdTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + XCTAssertEqual(BInt.ZERO.gcd(BInt.ZERO), BInt.ZERO) + XCTAssertEqual(BInt.ONE.gcd(BInt.ZERO), BInt.ONE) + XCTAssertEqual(BInt.ZERO.gcd(BInt.ONE), BInt.ONE) + let x = BInt(bitWidth: 100) + XCTAssertEqual(x.gcd(BInt.ONE), BInt.ONE) + XCTAssertEqual(x.gcd(BInt.ZERO), x) + XCTAssertEqual(x.gcd(x), x) + XCTAssertEqual(BInt(4).gcd(BInt(272)), BInt(4)) + } + + func test2() { + var i = 1 + for _ in 0 ..< 3 { + i *= 10 + let x = BInt(bitWidth: i) + let y = BInt(bitWidth: 2 * i) + let gcd = x.gcd(y) + let (qx, rx) = x.quotientAndRemainder(dividingBy: gcd) + let (qy, ry) = y.quotientAndRemainder(dividingBy: gcd) + XCTAssertEqual(rx, BInt.ZERO) + XCTAssertEqual(ry, BInt.ZERO) + XCTAssertEqual(qx.gcd(qy), BInt.ONE) + XCTAssertEqual(qy.gcd(qx), BInt.ONE) + } + } + + func test3() { + for _ in 0 ..< 1000 { + let x1 = BInt(bitWidth: 10) + XCTAssertEqual(x1.gcd(x1), x1) + XCTAssertEqual(x1.gcd(x1 + 1), BInt.ONE) + let x2 = BInt(bitWidth: 60) + XCTAssertEqual(x2.gcd(x2), x2) + XCTAssertEqual(x2.gcd(x2 + 1), BInt.ONE) + let x3 = BInt(bitWidth: 1000) + XCTAssertEqual(x3.gcd(x3), x3) + XCTAssertEqual(x3.gcd(x3 + 1), BInt.ONE) + } + } + +} diff --git a/Tests/BigIntTests/KaratsubaTest.swift b/Tests/BigIntTests/KaratsubaTest.swift new file mode 100755 index 0000000..87b5ef3 --- /dev/null +++ b/Tests/BigIntTests/KaratsubaTest.swift @@ -0,0 +1,42 @@ +// +// KaratsubaTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 15/02/2019. +// + +import XCTest + +class KaratsubaTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + for _ in 0 ..< 10 { + let a = BInt(bitWidth: (Limbs.KA_THR + Limbs.TC_THR) / 2 * 64) + let b = BInt(bitWidth: (Limbs.KA_THR + Limbs.TC_THR) / 2 * 64) + let p = a * b + let (q, r) = p.quotientAndRemainder(dividingBy: a) + XCTAssertEqual(q, b) + XCTAssertEqual(r, BInt.ZERO) + } + } + + func test2() { + for _ in 0 ..< 10 { + let a = BInt(bitWidth: (Limbs.KA_THR + Limbs.TC_THR) / 2 * 64) + let p = a ** 2 + let (q, r) = p.quotientAndRemainder(dividingBy: a) + XCTAssertEqual(q, a) + XCTAssertEqual(r, BInt.ZERO) + } + } + + +} diff --git a/Tests/BigIntTests/LimbTest.swift b/Tests/BigIntTests/LimbTest.swift new file mode 100755 index 0000000..3f8d7c8 --- /dev/null +++ b/Tests/BigIntTests/LimbTest.swift @@ -0,0 +1,33 @@ +// +// LimbTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 24/12/2018. +// + +import XCTest + +class LimbTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + var x1: Limbs = [0, 0, 0] + XCTAssertEqual(x1.count, 3) + x1.ensureSize(5) + XCTAssertEqual(x1.count, 5) + x1.normalize() + XCTAssertEqual(x1.count, 1) + x1 = [0, 0, 1] + XCTAssertEqual(x1.bitWidth, 129) + x1.setBitAt(1, to: true) + XCTAssertEqual(BInt(x1), (BInt(1) << 128) + 2) + } + +} diff --git a/Tests/BigIntTests/ModInverseTest.swift b/Tests/BigIntTests/ModInverseTest.swift new file mode 100755 index 0000000..1316c66 --- /dev/null +++ b/Tests/BigIntTests/ModInverseTest.swift @@ -0,0 +1,40 @@ +// +// ModInverseTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 02/01/2019. +// + +import XCTest + +class ModInverseTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func doTest(_ n: Int, _ m: Int) { + let p1 = BInt.probablePrime(m) + var x1 = BInt(bitWidth: n) + if x1 == 0 { + x1 = BInt(1) + } + if x1 % p1 == BInt.ONE { + XCTAssert((x1 * x1.modInverse(p1)).mod(p1) == BInt.ONE) + } + } + + func test1() { + doTest(3, 4) + doTest(30, 40) + doTest(300, 400) + doTest(4, 3) + doTest(40, 30) + doTest(400, 300) + } + +} diff --git a/Tests/BigIntTests/MulSquareTest.swift b/Tests/BigIntTests/MulSquareTest.swift new file mode 100755 index 0000000..1650ca2 --- /dev/null +++ b/Tests/BigIntTests/MulSquareTest.swift @@ -0,0 +1,50 @@ +// +// MulSquareTest.swift +// BigIntegerTests +// +// Created by Leif Ibsen on 19/12/2018. +// + +import XCTest + +class MulSquareTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + for i in 1 ... 10 { + let a1 = (BInt(1) << (i * 10)).randomLessThan() + let a2 = (BInt(1) << (i * 100)).randomLessThan() + let a3 = (BInt(1) << (i * 1000)).randomLessThan() + let a4 = (BInt(1) << (i * 10000)).randomLessThan() + let b1 = -a1 + let b2 = -a2 + let b3 = -a3 + let b4 = -a4 + XCTAssertEqual(a1 ** 2, a1 * a1) + XCTAssertEqual(a2 ** 2, a2 * a2) + XCTAssertEqual(a3 ** 2, a3 * a3) + XCTAssertEqual(a4 ** 2, a4 * a4) + XCTAssertEqual(b1 ** 2, b1 * b1) + XCTAssertEqual(b2 ** 2, b2 * b2) + XCTAssertEqual(b3 ** 2, b3 * b3) + XCTAssertEqual(b4 ** 2, b4 * b4) + } + } + + func test2() { + var b1 = BInt(1) + let a1 = BInt(bitWidth: 100) + for i in 0 ..< 10 { + XCTAssertEqual(a1 ** i, b1) + b1 *= a1 + } + } + +} diff --git a/Tests/BigIntTests/MultiplicationTest.swift b/Tests/BigIntTests/MultiplicationTest.swift new file mode 100755 index 0000000..e90da5c --- /dev/null +++ b/Tests/BigIntTests/MultiplicationTest.swift @@ -0,0 +1,89 @@ +// +// MultiplicationTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 19/01/2019. +// + +import XCTest + +class MultiplicationTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func doTest1(_ i: Int) { + let a = BInt(bitWidth: 200 * i) + let b = BInt(bitWidth: 100 * i) + let c = a * b + let (q, r) = c.quotientAndRemainder(dividingBy: a) + XCTAssertEqual(r, BInt(0)) + XCTAssertEqual(q, b) + let (q1, r1) = c.quotientAndRemainder(dividingBy: b) + XCTAssertEqual(r1, BInt(0)) + XCTAssertEqual(q1, a) + var am = a.magnitude + am.square() + XCTAssertEqual(a * a, BInt(am)) + } + + func test1() { + for i in 1 ..< 10 { + doTest1(i) + } + doTest1(1000) + } + + func test2() { + XCTAssertEqual(BInt.ONE * BInt.ZERO, BInt.ZERO) + XCTAssertEqual(BInt(-1) * BInt.ZERO, BInt.ZERO) + XCTAssertEqual(BInt.ZERO * BInt.ONE, BInt.ZERO) + XCTAssertEqual(BInt.ZERO * BInt(-1), BInt.ZERO) + XCTAssertEqual(BInt.ONE * 0, BInt.ZERO) + XCTAssertEqual(BInt(-1) * 0, BInt.ZERO) + XCTAssertEqual(0 * BInt.ONE, BInt.ZERO) + XCTAssertEqual(0 * BInt(-1), BInt.ZERO) + } + + func test3() { + XCTAssertEqual(BInt(7) * BInt(4), BInt(28)) + XCTAssertEqual(BInt(7) * BInt(-4), BInt(-28)) + XCTAssertEqual(BInt(-7) * BInt(4), BInt(-28)) + XCTAssertEqual(BInt(-7) * BInt(-4), BInt(28)) + XCTAssertEqual(BInt(-7) * BInt(0), BInt(0)) + XCTAssertEqual(BInt(7) * BInt(0), BInt(0)) + XCTAssertEqual(BInt(7) * 4, BInt(28)) + XCTAssertEqual(BInt(7) * (-4), BInt(-28)) + XCTAssertEqual(BInt(-7) * 4, BInt(-28)) + XCTAssertEqual(BInt(-7) * (-4), BInt(28)) + XCTAssertEqual(BInt(-7) * 0, BInt(0)) + XCTAssertEqual(BInt(7) * 0, BInt(0)) + XCTAssertEqual(7 * BInt(4), BInt(28)) + XCTAssertEqual(7 * BInt(-4), BInt(-28)) + XCTAssertEqual((-7) * BInt(4), BInt(-28)) + XCTAssertEqual((-7) * BInt(-4), BInt(28)) + XCTAssertEqual((-7) * BInt(0), BInt(0)) + XCTAssertEqual(7 * BInt(0), BInt(0)) + } + + func test5() { + var x1 = BInt(7) + x1 *= BInt(4) + XCTAssertEqual(x1, BInt(28)) + var x2 = BInt(7) + x2 *= BInt(-4) + XCTAssertEqual(x2, BInt(-28)) + var x3 = BInt(-7) + x3 *= BInt(4) + XCTAssertEqual(x3, BInt(-28)) + var x4 = BInt(-7) + x4 *= BInt(-4) + XCTAssertEqual(x4, BInt(28)) + } + +} diff --git a/Tests/BigIntTests/PerformanceTest.swift b/Tests/BigIntTests/PerformanceTest.swift new file mode 100755 index 0000000..b464a63 --- /dev/null +++ b/Tests/BigIntTests/PerformanceTest.swift @@ -0,0 +1,97 @@ +// +// PerformanceTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 17/01/2019. +// + +import XCTest + +class PerformanceTest: XCTestCase { + + let a1000 = BInt("3187705437890850041662973758105262878153514794996698172406519277876060364087986868049465132757493318066301987043192958841748826350731448419937544810921786918975580180410200630645469411588934094075222404396990984350815153163569041641732160380739556436955287671287935796642478260435292021117614349253825")! + let b1000 = BInt("9159373012373110951130589007821321098436345855865428979299172149373720601254669552044211236974571462005332583657082428026625366060511329189733296464187785766230514564038057370938741745651937465362625449921195096442684523511715110908407508139315000469851121118117438147266381183636498494901233452870695")! + let c2000 = BInt("1190583332681083129323588684910845359379915367459759242106618067261956856381281184752008892106576666833853411939711280970145570546868549934865719229538926506588929417873149597614787608112658086250354719939407543740242931571462165384138560315454455247539461818779966171917173966217706187439870264672508450210272481951994459523586160979759782950984370978171111340529321052541588344733968902238813379990628157732181128074253104347868153860527298911917508606081710893794973605227829729403843750412766366804402629686458092685235454222856584200220355212623917637542398554907364450159627359316156463617143173")! + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + + func testAddition() { + let N = 100000 + var x = BInt.ZERO + self.measure { + for _ in 0 ..< N { + x = a1000 + b1000 + } + } + } + + func testSubtraction() { + let N = 100000 + var x = BInt.ZERO + self.measure { + for _ in 0 ..< N { + x = a1000 - b1000 + } + } + } + + func testMultiplication() { + let N = 1000 + var x = BInt.ZERO + self.measure { + for _ in 0 ..< N { + x = a1000 * b1000 + } + } + } + + func testSquaring() { + let N = 1000 + var a = Limbs(repeating: 0, count: 0) + self.measure { + for _ in 0 ..< N { + a = a1000.magnitude + a.square() + } + } + } + + func testDivision() { + let N = 1000 + var q = BInt.ZERO + var r = BInt.ZERO + self.measure { + for _ in 0 ..< N { + (q, r) = c2000.quotientAndRemainder(dividingBy: a1000) + } + } + } + + func testGcd() { + let N = 10 + var x = BInt.ZERO + self.measure { + for _ in 0 ..< N { + x = a1000.gcd(b1000) + } + } + } + + func testExpMod() { + let N = 10 + var x = BInt.ZERO + self.measure { + for _ in 0 ..< N { + x = a1000.expMod(b1000, c2000) + } + } + } + +} diff --git a/Tests/BigIntTests/PrimeTest.swift b/Tests/BigIntTests/PrimeTest.swift new file mode 100755 index 0000000..d031054 --- /dev/null +++ b/Tests/BigIntTests/PrimeTest.swift @@ -0,0 +1,47 @@ +// +// PrimeTest.swift +// BigIntegerTests +// +// Created by Leif Ibsen on 15/12/2018. +// + +import XCTest + +class PrimeTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func doMersenne(_ x: Int) { + XCTAssertTrue((BInt(1) << x - 1).isProbablyPrime()) + XCTAssertTrue(!(BInt(1) << x + 1).isProbablyPrime()) + } + func test1() { + XCTAssertTrue(BInt(100).isProbablyPrime(0)) + XCTAssertTrue(!BInt(100).isProbablyPrime()) + for i in 1 ... 5 { + XCTAssertTrue(BInt.probablePrime(100 * i).isProbablyPrime()) + } + } + + func test2() { + doMersenne(3) + doMersenne(5) + doMersenne(7) + doMersenne(13) + doMersenne(17) + doMersenne(19) + doMersenne(31) + doMersenne(61) + doMersenne(89) + doMersenne(107) + doMersenne(127) + doMersenne(521) + } + +} diff --git a/Tests/BigIntTests/RootTest.swift b/Tests/BigIntTests/RootTest.swift new file mode 100644 index 0000000..834e290 --- /dev/null +++ b/Tests/BigIntTests/RootTest.swift @@ -0,0 +1,45 @@ +// +// RootTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 11/09/2019. +// + +import XCTest + +class RootTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + for _ in 0 ..< 1000 { + let x = BInt(bitWidth: 1000) + let y = x.sqrt() + XCTAssert(y * y <= x) + XCTAssert((y + 1) * (y + 1) > x) + for n in 1 ... 10 { + let y = x.root(n) + XCTAssert(y ** n <= x) + XCTAssert((y + 1) ** n > x) + } + } + } + + func test2() { + for _ in 0 ..< 100 { + let p = BInt.probablePrime(100) + for _ in 0 ..< 100 { + let x = BInt(bitWidth: 300) + let s = x.sqrtMod(p) + XCTAssert(x.jacobiSymbol(p) != 1 || (s! ** 2).mod(p) == x.mod(p)) + } + } + } + +} diff --git a/Tests/BigIntTests/ShiftTest.swift b/Tests/BigIntTests/ShiftTest.swift new file mode 100755 index 0000000..ce444bb --- /dev/null +++ b/Tests/BigIntTests/ShiftTest.swift @@ -0,0 +1,33 @@ +// +// ShiftTest.swift +// XBigIntegerTests +// +// Created by Leif Ibsen on 11/12/2018. +// + +import XCTest + +class ShiftTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + let x1 = BInt(bitWidth: 200) + XCTAssertEqual(x1, (x1 << 3) >> 3) + XCTAssertEqual(x1 << -3, x1 >> 3) + XCTAssertEqual(x1 << 3, x1 >> -3) + let x2 = BInt("1234567890")! + XCTAssertEqual(x2, (x2 << 200) / (BInt.ONE << 200)) + var x3 = x2 + x3 <<= 1 + XCTAssertEqual(x3, BInt("2469135780")!) + } + + +} diff --git a/Tests/BigIntTests/SubtractionTest.swift b/Tests/BigIntTests/SubtractionTest.swift new file mode 100755 index 0000000..5e55174 --- /dev/null +++ b/Tests/BigIntTests/SubtractionTest.swift @@ -0,0 +1,58 @@ +// +// SubtractionTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 19/01/2019. +// + +import XCTest + +class SubtractionTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + XCTAssertEqual(BInt(7) - BInt(4), BInt(3)) + XCTAssertEqual(BInt(7) - BInt(-4), BInt(11)) + XCTAssertEqual(BInt(-7) - BInt(4), BInt(-11)) + XCTAssertEqual(BInt(-7) - BInt(-4), BInt(-3)) + XCTAssertEqual(BInt(-7) - BInt(0), BInt(-7)) + XCTAssertEqual(BInt(7) - BInt(0), BInt(7)) + XCTAssertEqual(BInt(7) - 4, BInt(3)) + XCTAssertEqual(BInt(7) - (-4), BInt(11)) + XCTAssertEqual(BInt(-7) - 4, BInt(-11)) + XCTAssertEqual(BInt(-7) - (-4), BInt(-3)) + XCTAssertEqual(BInt(-7) - 0, BInt(-7)) + XCTAssertEqual(BInt(7) - 0, BInt(7)) + XCTAssertEqual(7 - BInt(4), BInt(3)) + XCTAssertEqual(7 - BInt(-4), BInt(11)) + XCTAssertEqual((-7) - BInt(4), BInt(-11)) + XCTAssertEqual((-7) - BInt(-4), BInt(-3)) + XCTAssertEqual((-7) - BInt(0), BInt(-7)) + XCTAssertEqual(7 - BInt(0), BInt(7)) + } + + func test2() { + var x1 = BInt(7) + x1 -= BInt(4) + XCTAssertEqual(x1, BInt(3)) + var x2 = BInt(7) + x2 -= BInt(-4) + XCTAssertEqual(x2, BInt(11)) + var x3 = BInt(-7) + x3 -= BInt(4) + XCTAssertEqual(x3, BInt(-11)) + var x4 = BInt(-7) + x4 -= BInt(-4) + XCTAssertEqual(x4, BInt(-3)) + + } + + +} diff --git a/Tests/BigIntTests/ToByteArrayTest.swift b/Tests/BigIntTests/ToByteArrayTest.swift new file mode 100644 index 0000000..69935f3 --- /dev/null +++ b/Tests/BigIntTests/ToByteArrayTest.swift @@ -0,0 +1,40 @@ +// +// ToByteArrayTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 05/09/2019. +// + +import XCTest + +class ToByteArrayTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + for _ in 0 ..< 10000 { + let x = BInt(bitWidth: 1000) + let x1 = BInt(magnitude: x.asMagnitudeBytes()) + XCTAssertEqual(x, x1) + let x2 = BInt(signed: x.asSignedBytes()) + XCTAssertEqual(x, x2) + } + } + + func test2() { + for _ in 0 ..< 10000 { + let x = -BInt(bitWidth: 1000) + let x1 = -BInt(magnitude: x.asMagnitudeBytes()) + XCTAssertEqual(x, x1) + let x2 = BInt(signed: x.asSignedBytes()) + XCTAssertEqual(x, x2) + } + } + +} diff --git a/Tests/BigIntTests/ToStringTest.swift b/Tests/BigIntTests/ToStringTest.swift new file mode 100755 index 0000000..f591a9a --- /dev/null +++ b/Tests/BigIntTests/ToStringTest.swift @@ -0,0 +1,48 @@ +// +// ToStringTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 04/02/2019. +// + +import XCTest + +class ToStringTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func testToString1() { + var x = BInt(1) + var s = "1" + for _ in 0 ..< 100 { + x *= 10 + s += "0" + XCTAssertEqual(s, x.asString(radix: 10)) + } + } + + func testToString2() { + let x: Limbs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + let b = BInt(x, false) + for r in 2 ... 36 { + let b1 = BInt(b.asString(radix: r), radix: r) + XCTAssertEqual(b, b1) + } + } + + func testToString3() { + let x: Limbs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + let b = BInt(x, true) + for r in 2 ... 36 { + let b1 = BInt(b.asString(radix: r), radix: r) + XCTAssertEqual(b, b1) + } + } + +} diff --git a/Tests/BigIntTests/ToomCookTest.swift b/Tests/BigIntTests/ToomCookTest.swift new file mode 100755 index 0000000..cc95034 --- /dev/null +++ b/Tests/BigIntTests/ToomCookTest.swift @@ -0,0 +1,41 @@ +// +// ToomCookTest.swift +// BigIntTests +// +// Created by Leif Ibsen on 09/02/2019. +// + +import XCTest + +class ToomCookTest: XCTestCase { + + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test1() { + for _ in 0 ..< 10 { + let a = BInt(bitWidth: Limbs.TC_THR * 5 * 64) + let b = BInt(bitWidth: Limbs.TC_THR * 2 * 64) + let p = a * b + let (q, r) = p.quotientAndRemainder(dividingBy: a) + XCTAssertEqual(q, b) + XCTAssertEqual(r, BInt.ZERO) + } + } + + func test2() { + for _ in 0 ..< 10 { + let a = BInt(bitWidth: Limbs.TC_THR * 10 * 64) + let p = a ** 2 + let (q, r) = p.quotientAndRemainder(dividingBy: a) + XCTAssertEqual(q, a) + XCTAssertEqual(r, BInt.ZERO) + } + } + +} diff --git a/Tests/BigIntTests/XCTestManifests.swift b/Tests/BigIntTests/XCTestManifests.swift new file mode 100755 index 0000000..ab63890 --- /dev/null +++ b/Tests/BigIntTests/XCTestManifests.swift @@ -0,0 +1,9 @@ +import XCTest + +#if !os(macOS) +public func allTests() -> [XCTestCaseEntry] { + return [ + testCase(BigIntegerTests.allTests), + ] +} +#endif \ No newline at end of file diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 0000000..ff9f8c0 --- /dev/null +++ b/Tests/LinuxMain.swift @@ -0,0 +1,7 @@ +import XCTest + +import BigIntTests + +var tests = [XCTestCaseEntry]() +tests += BigIntTests.allTests() +XCTMain(tests) diff --git a/docs/Structs.html b/docs/Structs.html new file mode 100644 index 0000000..97523ff --- /dev/null +++ b/docs/Structs.html @@ -0,0 +1,113 @@ + + + + Structures Reference + + + + + + + + + + +
+
+

Docs (100% documented)

+
+
+
+ +
+
+ +
+
+
+

Structures

+

The following structures are available globally.

+ +
+
+
+
    +
  • +
    + + + + BInt + +
    +
    +
    +
    +
    +
    +

    A signed integer of unbounded size. +A BInt value is represented with magnitude and sign. +The magnitude is an array of unsigned 64 bit integers (a.k.a. Limbs). +The sign is a boolean value, true means value < 0, false means value >= 0 +The representation is little-endian, least significant Limb has index 0. +The representation is minimal, there is no leading zero Limbs. +The exception is that the value 0 is represented as a single 64 bit zero Limb and sign false

    + + See more +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public struct BInt : CustomStringConvertible, Equatable, Hashable
    + +
    +
    +
    +
    +
  • +
+
+
+
+ +
+
+ + + diff --git a/docs/Structs/BInt.html b/docs/Structs/BInt.html new file mode 100644 index 0000000..e12deab --- /dev/null +++ b/docs/Structs/BInt.html @@ -0,0 +1,5734 @@ + + + + BInt Structure Reference + + + + + + + + + + +
+
+

Docs (100% documented)

+
+
+
+ +
+
+ +
+
+
+

BInt

+
+
+
public struct BInt : CustomStringConvertible, Equatable, Hashable
+ +
+
+

A signed integer of unbounded size. +A BInt value is represented with magnitude and sign. +The magnitude is an array of unsigned 64 bit integers (a.k.a. Limbs). +The sign is a boolean value, true means value < 0, false means value >= 0 +The representation is little-endian, least significant Limb has index 0. +The representation is minimal, there is no leading zero Limbs. +The exception is that the value 0 is represented as a single 64 bit zero Limb and sign false

+ +
+
+
+ +
    +
  • +
    + + + + ZERO + +
    +
    +
    +
    +
    +
    +

    BInt(0)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let ZERO: BInt
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + ONE + +
    +
    +
    +
    +
    +
    +

    BInt(1)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let ONE: BInt
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + TWO + +
    +
    +
    +
    +
    +
    +

    BInt(2)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let TWO: BInt
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + THREE + +
    +
    +
    +
    +
    +
    +

    BInt(3)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let THREE: BInt
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + TEN + +
    +
    +
    +
    +
    +
    +

    BInt(10)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let TEN: BInt
    + +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + init(_:_:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from magnitude and sign

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(_ magnitude: Limbs, _ isNegative: Bool = false)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + magnitude + + +
    +

    magnitude of value

    +
    +
    + + isNegative + + +
    +

    true means negative value, false means 0 or positive value, default is false

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(_:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from an Int value

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(_ x: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Int value

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(_:radix:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from a String value and radix

    + +

    Examples:

    + +
      +
    • BInt(90abcdef, radix = 16)
    • +
    • BInt(111110010, radix = 2)
    • +
    • BInt(1cdefghijk44, radix = 26)
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init?(_ x: String, radix: Int = 10)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    String value to be converted

    +
    +
    + + radix + + +
    +

    Radix of x, from 2 to 36 inclusive, default is 10

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(bitWidth:) + +
    +
    +
    +
    +
    +
    +

    Constructs a random BInt with a specified number of bits

    +
    +

    Precondition

    + bitWidth is positive + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(bitWidth: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + bitWidth + + +
    +

    Number of bits

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(magnitude:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from a big-endian magnitude byte array representation

    +
    +

    Precondition

    + Byte array is not empty + +
    + +

    Examples:

    + +
      +
    • The byte array [1, 0, 0] represents BInt value 65536
    • +
    • The byte array [128, 0] represents BInt value 32768
    • +
    • The byte array [255, 127] represents BInt value 65407
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(magnitude x: Bytes)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Magnitude big-endian byte array

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(signed:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from a big-endian 2’s complement byte array representation

    +
    +

    Precondition

    + Byte array is not empty + +
    + +

    Examples:

    + +
      +
    • The byte array [1, 0, 0] represents BInt value 65536
    • +
    • The byte array [128, 0] represents BInt value -32768
    • +
    • The byte array [255, 127] represents BInt value -129
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(signed x: Bytes)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    2’s complement big-endian byte array

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + isNegative + +
    +
    +
    +
    +
    +
    +

    The sign, true if self < 0, false otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public internal(set) var isNegative: Bool
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + magnitude + +
    +
    +
    +
    +
    +
    +

    The magnitude limb array

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public internal(set) var magnitude: Limbs
    + +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + bitWidth + +
    +
    +
    +
    +
    +
    +

    The number of bits in the magnitude of self. 0 if self = 0

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var bitWidth: Int { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + description + +
    +
    +
    +
    +
    +
    +

    Base 10 string value of self

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var description: String { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isEven + +
    +
    +
    +
    +
    +
    +

    Is true if self is even, false if self is odd

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isEven: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isNotZero + +
    +
    +
    +
    +
    +
    +

    Is false if self = 0, true otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isNotZero: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isOdd + +
    +
    +
    +
    +
    +
    +

    Is true if self is odd, false if self is even

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isOdd: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isOne + +
    +
    +
    +
    +
    +
    +

    Is true if self = 1, false otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isOne: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isPositive + +
    +
    +
    +
    +
    +
    +

    Is true if self > 0, false otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isPositive: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isZero + +
    +
    +
    +
    +
    +
    +

    Is true if self = 0, false otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isZero: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + leadingZeroBitCount + +
    +
    +
    +
    +
    +
    +

    The number of leading zero bits in the magnitude of self. 0 if self = 0

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var leadingZeroBitCount: Int { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + signum + +
    +
    +
    +
    +
    +
    +

    Is 0 if self = 0, 1 if self > 0, and -1 if self < 0

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var signum: Int { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + trailingZeroBitCount + +
    +
    +
    +
    +
    +
    +

    The number of trailing zero bits in the magnitude of self. 0 if self = 0

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var trailingZeroBitCount: Int { get }
    + +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + asInt() + +
    +
    +
    +
    +
    +
    +

    self as an Int

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func asInt() -> Int?
    + +
    +
    +
    +

    Return Value

    +

    self as an Int or nil if self is not representable as an Int

    +
    +
    +
    +
  • +
  • +
    + + + + asMagnitudeBytes() + +
    +
    +
    +
    +
    +
    +

    Byte array representation of magnitude value

    + +

    Examples:

    + +
      +
    • BInt(1).asMagnitudeBytes() = [1]
    • +
    • BInt(-1).asMagnitudeBytes() = [1]
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func asMagnitudeBytes() -> Bytes
    + +
    +
    +
    +

    Return Value

    +

    Minimal big-endian magnitude byte array representation

    +
    +
    +
    +
  • +
  • +
    + + + + asSignedBytes() + +
    +
    +
    +
    +
    +
    +

    Byte array representation of 2’s complement value

    + +

    Examples:

    + +
      +
    • BInt(1).asSignedBytes() = [1]
    • +
    • BInt(-1).asSignedBytes() = [255]
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func asSignedBytes() -> Bytes
    + +
    +
    +
    +

    Return Value

    +

    Minimal big-endian 2’s complement byte array representation

    +
    +
    +
    +
  • +
  • + +
    +
    +
    +
    +
    +

    self as a String with a given radix

    +
    +

    Precondition

    + Radix between 2 and 36 inclusive + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func asString(radix: Int = 10, uppercase: Bool = false) -> String
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + radix + + +
    +

    Radix from 2 to 36 inclusive

    +
    +
    + + uppercase + + +
    +

    true to use uppercase letters, false to use lowercase letters, default is false

    +
    +
    +
    +
    +

    Return Value

    +

    self as a String in the given radix

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + &(_:_:) + +
    +
    +
    +
    +
    +
    +

    Bitwise and operator

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func & (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First value

    +
    +
    + + y + + +
    +

    Second value

    +
    +
    +
    +
    +

    Return Value

    +

    BInt(signed: bx & by) where + +

      +
    • bx = x.asSignedBytes()
    • +
    • by = y.asSignedBytes()
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + &=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x & y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func &= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand parameter

    +
    +
    + + y + + +
    +

    Right hand parameter

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + |(_:_:) + +
    +
    +
    +
    +
    +
    +

    Bitwise or operator

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func | (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First value

    +
    +
    + + y + + +
    +

    Second value

    +
    +
    +
    +
    +

    Return Value

    +

    BInt(signed: bx | by) where + +

      +
    • bx = x.asSignedBytes()
    • +
    • by = y.asSignedBytes()
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + |=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x | y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func |= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand parameter

    +
    +
    + + y + + +
    +

    Right hand parameter

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + ^(_:_:) + +
    +
    +
    +
    +
    +
    +

    Bitwise xor operator

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func ^ (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First value

    +
    +
    + + y + + +
    +

    Second value

    +
    +
    +
    +
    +

    Return Value

    +

    BInt(signed: bx ^ by) where + +

      +
    • bx = x.asSignedBytes()
    • +
    • by = y.asSignedBytes()
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + ^=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x ^ y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func ^= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand parameter

    +
    +
    + + y + + +
    +

    Right hand parameter

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + ~(_:) + +
    +
    +
    +
    +
    +
    +

    Bitwise not operator

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public prefix static func ~ (x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    BInt value

    +
    +
    +
    +
    +

    Return Value

    +

    -x - 1

    +
    +
    +
    +
  • +
  • +
    + + + + clearBit(_:) + +
    +
    +
    +
    +
    +
    +

    Clear a specified bit

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public mutating func clearBit(_ n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    Bit number

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + flipBit(_:) + +
    +
    +
    +
    +
    +
    +

    Invert a specified bit

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public mutating func flipBit(_ n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    Bit number

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + setBit(_:) + +
    +
    +
    +
    +
    +
    +

    Set a specified bit

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public mutating func setBit(_ n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    Bit number

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + testBit(_:) + +
    +
    +
    +
    +
    +
    +

    Test a specified bit

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func testBit(_ n: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    Bit number

    +
    +
    +
    +
    +

    Return Value

    +

    true if bit is set, false otherwise

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + +(_:) + +
    +
    +
    +
    +
    +
    +

    Prefix plus

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public prefix static func + (x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    BInt value

    +
    +
    +
    +
    +

    Return Value

    +

    x

    +
    +
    +
    +
  • +
  • +
    + + + + +(_:_:) + +
    +
    +
    +
    +
    +
    +

    Addition

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func + (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First addend

    +
    +
    + + y + + +
    +

    Second addend

    +
    +
    +
    +
    +

    Return Value

    +

    x + y

    +
    +
    +
    +
  • +
  • +
    + + + + +(_:_:) + +
    +
    +
    +
    +
    +
    +

    Addition

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func + (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First addend

    +
    +
    + + y + + +
    +

    Second addend

    +
    +
    +
    +
    +

    Return Value

    +

    x + y

    +
    +
    +
    +
  • +
  • +
    + + + + +(_:_:) + +
    +
    +
    +
    +
    +
    +

    Addition

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func + (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First addend

    +
    +
    + + y + + +
    +

    Second addend

    +
    +
    +
    +
    +

    Return Value

    +

    x + y

    +
    +
    +
    +
  • +
  • +
    + + + + +=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x + y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func += (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand addend

    +
    +
    + + y + + +
    +

    Right hand addend

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + +=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x + y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func += (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand addend

    +
    +
    + + y + + +
    +

    Right hand addend

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + negate() + +
    +
    +
    +
    +
    +
    +

    Negates self

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public mutating func negate()
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + -(_:) + +
    +
    +
    +
    +
    +
    +

    Negation

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public prefix static func - (x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    +
    +
    +

    Return Value

    +

    -x

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + -(_:_:) + +
    +
    +
    +
    +
    +
    +

    Subtraction

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func - (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Minuend

    +
    +
    + + y + + +
    +

    Subtrahend

    +
    +
    +
    +
    +

    Return Value

    +

    x - y

    +
    +
    +
    +
  • +
  • +
    + + + + -(_:_:) + +
    +
    +
    +
    +
    +
    +

    Subtraction

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func - (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Minuend

    +
    +
    + + y + + +
    +

    Subtrahend

    +
    +
    +
    +
    +

    Return Value

    +

    x - y

    +
    +
    +
    +
  • +
  • +
    + + + + -(_:_:) + +
    +
    +
    +
    +
    +
    +

    Subtraction

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func - (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Minuend

    +
    +
    + + y + + +
    +

    Subtrahend

    +
    +
    +
    +
    +

    Return Value

    +

    x - y

    +
    +
    +
    +
  • +
  • +
    + + + + -=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x - y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func -= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand minuend

    +
    +
    + + y + + +
    +

    Right hand subtrahend

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + -=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x - y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func -= (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand minuend

    +
    +
    + + y + + +
    +

    Right hand subtrahend

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + *(_:_:) + +
    +
    +
    +
    +
    +
    +

    Multiplication

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func * (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Multiplier

    +
    +
    + + y + + +
    +

    Multiplicand

    +
    +
    +
    +
    +

    Return Value

    +

    x * y

    +
    +
    +
    +
  • +
  • +
    + + + + *(_:_:) + +
    +
    +
    +
    +
    +
    +

    Multiplication

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func * (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Multiplier

    +
    +
    + + y + + +
    +

    Multiplicand

    +
    +
    +
    +
    +

    Return Value

    +

    x * y

    +
    +
    +
    +
  • +
  • +
    + + + + *(_:_:) + +
    +
    +
    +
    +
    +
    +

    Multiplication

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func * (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Multiplier

    +
    +
    + + y + + +
    +

    Multiplicand

    +
    +
    +
    +
    +

    Return Value

    +

    x * y

    +
    +
    +
    +
  • +
  • +
    + + + + *=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x * y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func *= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand multiplier

    +
    +
    + + y + + +
    +

    Right hand multiplicand

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + *=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x * y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func *= (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand multiplier

    +
    +
    + + y + + +
    +

    Right hand multiplicand

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • + +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func quotientAndRemainder(dividingBy x: BInt) -> (quotient: BInt, remainder: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    Quotient and remainder of self / x

    +
    +
    +
    +
  • +
  • + +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func quotientAndRemainder(dividingBy x: BInt, _ quotient: inout BInt, _ remainder: inout BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + + + + + +
    + + x + + +
    +

    Divisor

    +
    +
    + + quotient + + +
    +

    Set to the quotient of self / x

    +
    +
    + + remainder + + +
    +

    Set to the remainder of self / x

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + /(_:_:) + +
    +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func / (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x / y

    +
    +
    +
    +
  • +
  • +
    + + + + /(_:_:) + +
    +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func / (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x / y

    +
    +
    +
    +
  • +
  • +
    + + + + /(_:_:) + +
    +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func / (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x / y

    +
    +
    +
    +
  • +
  • +
    + + + + /=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x / y

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func /= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand dividend

    +
    +
    + + y + + +
    +

    Right hand divisor

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + /=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x / y

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func /= (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand dividend

    +
    +
    + + y + + +
    +

    Right hand divisor

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + %(_:_:) + +
    +
    +
    +
    +
    +
    +

    Remainder

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func % (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x % y

    +
    +
    +
    +
  • +
  • +
    + + + + %(_:_:) + +
    +
    +
    +
    +
    +
    +

    Remainder

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func % (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x % y

    +
    +
    +
    +
  • +
  • +
    + + + + %(_:_:) + +
    +
    +
    +
    +
    +
    +

    Remainder

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func % (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x % y

    +
    +
    +
    +
  • +
  • +
    + + + + %=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x % y

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func %= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + %=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x % y

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func %= (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + mod(_:) + +
    +
    +
    +
    +
    +
    +

    Modulus

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func mod(_ x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    self mod x, a non-negative value

    +
    +
    +
    +
  • +
  • +
    + + + + modInverse(_:) + +
    +
    +
    +
    +
    +
    +

    Inverse modulus

    +
    +

    Precondition

    + self and modulus are coprime + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func modInverse(_ m: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + m + + +
    +

    Modulus

    +
    +
    +
    +
    +

    Return Value

    +

    If self and m are coprime, x such that (self * x) mod m = 1

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + **(_:_:) + +
    +
    +
    +
    +
    +
    +

    Exponentiation

    +
    +

    Precondition

    + Exponent is non-negative + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func ** (a: BInt, x: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + a + + +
    +

    Operand

    +
    +
    + + x + + +
    +

    Non-negative exponent

    +
    +
    +
    +
    +

    Return Value

    +

    a^x

    +
    +
    +
    +
  • +
  • +
    + + + + expMod(_:_:) + +
    +
    +
    +
    +
    +
    +

    Modular exponentiation

    +
    +

    Precondition

    + Modulus is positive + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func expMod(_ x: BInt, _ m: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    The exponent

    +
    +
    + + m + + +
    +

    The modulus, a positive number

    +
    +
    +
    +
    +

    Return Value

    +

    (self^x) mod m for positive x, ((self^-x) mod m) modInverse m for negative x

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + ==(_:_:) + +
    +
    +
    +
    +
    +
    +

    Equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func == (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x = y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + ==(_:_:) + +
    +
    +
    +
    +
    +
    +

    Equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func == (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x = y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + ==(_:_:) + +
    +
    +
    +
    +
    +
    +

    Equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func == (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x = y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + !=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Not equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func != (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x != y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + !=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Not equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func != (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x != y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + !=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Not equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func != (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x != y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less then

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func < (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x < y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func < (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x < y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func < (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x < y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func > (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x > y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func > (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x > y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func > (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x > y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func <= (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x <= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func <= (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x <= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func <= (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x <= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >= (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x >= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >= (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x >= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >= (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x >= y, false otherwise

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + <<(_:_:) + +
    +
    +
    +
    +
    +
    +

    Logical left shift

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func << (x: BInt, n: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    + + n + + +
    +

    Shift count

    +
    +
    +
    +
    +

    Return Value

    +

      +
    • BInt(x.magnitude << n, x.isNegative) if n > 0
    • +
    • BInt(x.magnitude >> -n, x.isNegative) if n < 0
    • +
    • x if n = 0
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + <<=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x << n

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func <<= (x: inout BInt, n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    + + n + + +
    +

    Shift count

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + >>(_:_:) + +
    +
    +
    +
    +
    +
    +

    Logical right shift

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >> (x: BInt, n: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    + + n + + +
    +

    Shift count

    +
    +
    +
    +
    +

    Return Value

    +

      +
    • BInt(x.magnitude >> n, x.isNegative) if n > 0
    • +
    • BInt(x.magnitude << -n, x.isNegative) if n < 0
    • +
    • x if n = 0
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + >>=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x >> n

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >>= (x: inout BInt, n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    + + n + + +
    +

    Shift count

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + isProbablyPrime(_:) + +
    +
    +
    +
    +
    +
    +

    Checks whether self is prime using the Miller-Rabin algorithm

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func isProbablyPrime(_ p: Int = 30) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + p + + +
    +

    If true is returned, self is prime with probability > 1-½^p

    +
    +
    +
    +
    +

    Return Value

    +

    true if self is probably prime, false if self is definitely not prime

    +
    +
    +
    +
  • +
  • +
    + + + + probablePrime(_:_:) + +
    +
    +
    +
    +
    +
    +

    A probable prime number with a given bitwidth

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func probablePrime(_ bitWidth: Int, _ p: Int = 30) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + bitWidth + + +
    +

    The bitWidth

    +
    +
    + + p + + +
    +

    The returned number is prime with probability > 1-½^p, default value is 30

    +
    +
    +
    +
    +

    Return Value

    +

    A prime number with the specified bitwidth and probability

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + abs() + +
    +
    +
    +
    +
    +
    +

    Absolute value

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func abs() -> BInt
    + +
    +
    +
    +

    Return Value

    +

    The absolute value of self

    +
    +
    +
    +
  • +
  • +
    + + + + gcd(_:) + +
    +
    +
    +
    +
    +
    +

    Greatest common divisor

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func gcd(_ x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    +
    +
    +

    Return Value

    +

    Greatest common divisor of magnitude of self and magnitude of x

    +
    +
    +
    +
  • +
  • +
    + + + + jacobiSymbol(_:) + +
    +
    +
    +
    +
    +
    +

    Jacobi symbol

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func jacobiSymbol(_ m: BInt) -> Int
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + m + + +
    +

    An integer value

    +
    +
    +
    +
    +

    Return Value

    +

    The Jacobi symbol of self and m, or 0 if it does not exist

    +
    +
    +
    +
  • +
  • +
    + + + + randomLessThan() + +
    +
    +
    +
    +
    +
    +

    Random value

    +
    +

    Precondition

    + self is positive + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func randomLessThan() -> BInt
    + +
    +
    +
    +

    Return Value

    +

    A random value < absolute value of self

    +
    +
    +
    +
  • +
  • +
    + + + + root(_:) + +
    +
    +
    +
    +
    +
    +

    n'th root of a non-negative number

    +
    +

    Precondition

    +
      +
    • self is non-negative
    • +
    • n is positive
    • +
    + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func root(_ n: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    The root

    +
    +
    +
    +
    +

    Return Value

    +

    Largest x such that x^n <= self

    +
    +
    +
    +
  • +
  • +
    + + + + sqrt() + +
    +
    +
    +
    +
    +
    +

    Square root of a non-negative number

    +
    +

    Precondition

    + self is non-negative + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func sqrt() -> BInt
    + +
    +
    +
    +

    Return Value

    +

    Largest x such that x^2 <= self

    +
    +
    +
    +
  • +
  • +
    + + + + sqrtMod(_:) + +
    +
    +
    +
    +
    +
    +

    Square root modulo a prime number

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func sqrtMod(_ p: BInt) -> BInt?
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + p + + +
    +

    An odd prime number

    +
    +
    +
    +
    +

    Return Value

    +

    x, such that x^2 mod p = self, or nil if no such x exists

    +
    +
    +
    +
  • +
+
+
+
+ +
+
+ + + diff --git a/docs/Typealiases.html b/docs/Typealiases.html new file mode 100644 index 0000000..2a06fbe --- /dev/null +++ b/docs/Typealiases.html @@ -0,0 +1,187 @@ + + + + Type Aliases Reference + + + + + + + + + + +
+
+

Docs (100% documented)

+
+
+
+ +
+
+ +
+
+
+

Type Aliases

+

The following type aliases are available globally.

+ +
+
+
+
    +
  • +
    + + + + Byte + +
    +
    +
    +
    +
    +
    +

    Unsigned 8 bit value

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public typealias Byte = UInt8
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + Bytes + +
    +
    +
    +
    +
    +
    +

    Array of unsigned 8 bit values

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public typealias Bytes = [UInt8]
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + Limb + +
    +
    +
    +
    +
    +
    +

    Unsigned 64 bit value

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public typealias Limb = UInt64
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + Limbs + +
    +
    +
    +
    +
    +
    +

    Array of unsigned 64 bit values

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public typealias Limbs = [UInt64]
    + +
    +
    +
    +
    +
  • +
+
+
+
+ +
+
+ + + diff --git a/docs/badge.svg b/docs/badge.svg new file mode 100644 index 0000000..a096fec --- /dev/null +++ b/docs/badge.svg @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + documentation + + + documentation + + + 100% + + + 100% + + + diff --git a/docs/css/highlight.css b/docs/css/highlight.css new file mode 100644 index 0000000..d0db0e1 --- /dev/null +++ b/docs/css/highlight.css @@ -0,0 +1,200 @@ +/* Credit to https://gist.github.com/wataru420/2048287 */ +.highlight { + /* Comment */ + /* Error */ + /* Keyword */ + /* Operator */ + /* Comment.Multiline */ + /* Comment.Preproc */ + /* Comment.Single */ + /* Comment.Special */ + /* Generic.Deleted */ + /* Generic.Deleted.Specific */ + /* Generic.Emph */ + /* Generic.Error */ + /* Generic.Heading */ + /* Generic.Inserted */ + /* Generic.Inserted.Specific */ + /* Generic.Output */ + /* Generic.Prompt */ + /* Generic.Strong */ + /* Generic.Subheading */ + /* Generic.Traceback */ + /* Keyword.Constant */ + /* Keyword.Declaration */ + /* Keyword.Pseudo */ + /* Keyword.Reserved */ + /* Keyword.Type */ + /* Literal.Number */ + /* Literal.String */ + /* Name.Attribute */ + /* Name.Builtin */ + /* Name.Class */ + /* Name.Constant */ + /* Name.Entity */ + /* Name.Exception */ + /* Name.Function */ + /* Name.Namespace */ + /* Name.Tag */ + /* Name.Variable */ + /* Operator.Word */ + /* Text.Whitespace */ + /* Literal.Number.Float */ + /* Literal.Number.Hex */ + /* Literal.Number.Integer */ + /* Literal.Number.Oct */ + /* Literal.String.Backtick */ + /* Literal.String.Char */ + /* Literal.String.Doc */ + /* Literal.String.Double */ + /* Literal.String.Escape */ + /* Literal.String.Heredoc */ + /* Literal.String.Interpol */ + /* Literal.String.Other */ + /* Literal.String.Regex */ + /* Literal.String.Single */ + /* Literal.String.Symbol */ + /* Name.Builtin.Pseudo */ + /* Name.Variable.Class */ + /* Name.Variable.Global */ + /* Name.Variable.Instance */ + /* Literal.Number.Integer.Long */ } + .highlight .c { + color: #999988; + font-style: italic; } + .highlight .err { + color: #a61717; + background-color: #e3d2d2; } + .highlight .k { + color: #000000; + font-weight: bold; } + .highlight .o { + color: #000000; + font-weight: bold; } + .highlight .cm { + color: #999988; + font-style: italic; } + .highlight .cp { + color: #999999; + font-weight: bold; } + .highlight .c1 { + color: #999988; + font-style: italic; } + .highlight .cs { + color: #999999; + font-weight: bold; + font-style: italic; } + .highlight .gd { + color: #000000; + background-color: #ffdddd; } + .highlight .gd .x { + color: #000000; + background-color: #ffaaaa; } + .highlight .ge { + color: #000000; + font-style: italic; } + .highlight .gr { + color: #aa0000; } + .highlight .gh { + color: #999999; } + .highlight .gi { + color: #000000; + background-color: #ddffdd; } + .highlight .gi .x { + color: #000000; + background-color: #aaffaa; } + .highlight .go { + color: #888888; } + .highlight .gp { + color: #555555; } + .highlight .gs { + font-weight: bold; } + .highlight .gu { + color: #aaaaaa; } + .highlight .gt { + color: #aa0000; } + .highlight .kc { + color: #000000; + font-weight: bold; } + .highlight .kd { + color: #000000; + font-weight: bold; } + .highlight .kp { + color: #000000; + font-weight: bold; } + .highlight .kr { + color: #000000; + font-weight: bold; } + .highlight .kt { + color: #445588; } + .highlight .m { + color: #009999; } + .highlight .s { + color: #d14; } + .highlight .na { + color: #008080; } + .highlight .nb { + color: #0086B3; } + .highlight .nc { + color: #445588; + font-weight: bold; } + .highlight .no { + color: #008080; } + .highlight .ni { + color: #800080; } + .highlight .ne { + color: #990000; + font-weight: bold; } + .highlight .nf { + color: #990000; } + .highlight .nn { + color: #555555; } + .highlight .nt { + color: #000080; } + .highlight .nv { + color: #008080; } + .highlight .ow { + color: #000000; + font-weight: bold; } + .highlight .w { + color: #bbbbbb; } + .highlight .mf { + color: #009999; } + .highlight .mh { + color: #009999; } + .highlight .mi { + color: #009999; } + .highlight .mo { + color: #009999; } + .highlight .sb { + color: #d14; } + .highlight .sc { + color: #d14; } + .highlight .sd { + color: #d14; } + .highlight .s2 { + color: #d14; } + .highlight .se { + color: #d14; } + .highlight .sh { + color: #d14; } + .highlight .si { + color: #d14; } + .highlight .sx { + color: #d14; } + .highlight .sr { + color: #009926; } + .highlight .s1 { + color: #d14; } + .highlight .ss { + color: #990073; } + .highlight .bp { + color: #999999; } + .highlight .vc { + color: #008080; } + .highlight .vg { + color: #008080; } + .highlight .vi { + color: #008080; } + .highlight .il { + color: #009999; } diff --git a/docs/css/jazzy.css b/docs/css/jazzy.css new file mode 100644 index 0000000..0541d71 --- /dev/null +++ b/docs/css/jazzy.css @@ -0,0 +1,343 @@ +html, body, div, span, h1, h3, h4, p, a, code, em, img, ul, li, table, tbody, tr, td { + background: transparent; + border: 0; + margin: 0; + outline: 0; + padding: 0; + vertical-align: baseline; } + +body { + background-color: #f2f2f2; + font-family: Helvetica, freesans, Arial, sans-serif; + font-size: 14px; + -webkit-font-smoothing: subpixel-antialiased; + word-wrap: break-word; } + +h1, h2, h3 { + margin-top: 0.8em; + margin-bottom: 0.3em; + font-weight: 100; + color: black; } + +h1 { + font-size: 2.5em; } + +h2 { + font-size: 2em; + border-bottom: 1px solid #e2e2e2; } + +h4 { + font-size: 13px; + line-height: 1.5; + margin-top: 21px; } + +h5 { + font-size: 1.1em; } + +h6 { + font-size: 1.1em; + color: #777; } + +.section-name { + color: gray; + display: block; + font-family: Helvetica; + font-size: 22px; + font-weight: 100; + margin-bottom: 15px; } + +pre, code { + font: 0.95em Menlo, monospace; + color: #777; + word-wrap: normal; } + +p code, li code { + background-color: #eee; + padding: 2px 4px; + border-radius: 4px; } + +a { + color: #0088cc; + text-decoration: none; } + +ul { + padding-left: 15px; } + +li { + line-height: 1.8em; } + +img { + max-width: 100%; } + +blockquote { + margin-left: 0; + padding: 0 10px; + border-left: 4px solid #ccc; } + +.content-wrapper { + margin: 0 auto; + width: 980px; } + +header { + font-size: 0.85em; + line-height: 26px; + background-color: #414141; + position: fixed; + width: 100%; + z-index: 1; } + header img { + padding-right: 6px; + vertical-align: -4px; + height: 16px; } + header a { + color: #fff; } + header p { + float: left; + color: #999; } + header .header-right { + float: right; + margin-left: 16px; } + +#breadcrumbs { + background-color: #f2f2f2; + height: 27px; + padding-top: 17px; + position: fixed; + width: 100%; + z-index: 1; + margin-top: 26px; } + #breadcrumbs #carat { + height: 10px; + margin: 0 5px; } + +.sidebar { + background-color: #f9f9f9; + border: 1px solid #e2e2e2; + overflow-y: auto; + overflow-x: hidden; + position: fixed; + top: 70px; + bottom: 0; + width: 230px; + word-wrap: normal; } + +.nav-groups { + list-style-type: none; + background: #fff; + padding-left: 0; } + +.nav-group-name { + border-bottom: 1px solid #e2e2e2; + font-size: 1.1em; + font-weight: 100; + padding: 15px 0 15px 20px; } + .nav-group-name > a { + color: #333; } + +.nav-group-tasks { + margin-top: 5px; } + +.nav-group-task { + font-size: 0.9em; + list-style-type: none; + white-space: nowrap; } + .nav-group-task a { + color: #888; } + +.main-content { + background-color: #fff; + border: 1px solid #e2e2e2; + margin-left: 246px; + position: absolute; + overflow: hidden; + padding-bottom: 20px; + top: 70px; + width: 734px; } + .main-content p, .main-content a, .main-content code, .main-content em, .main-content ul, .main-content table, .main-content blockquote { + margin-bottom: 1em; } + .main-content p { + line-height: 1.8em; } + .main-content section .section:first-child { + margin-top: 0; + padding-top: 0; } + .main-content section .task-group-section .task-group:first-of-type { + padding-top: 10px; } + .main-content section .task-group-section .task-group:first-of-type .section-name { + padding-top: 15px; } + .main-content section .heading:before { + content: ""; + display: block; + padding-top: 70px; + margin: -70px 0 0; } + +.section { + padding: 0 25px; } + +.highlight { + background-color: #eee; + padding: 10px 12px; + border: 1px solid #e2e2e2; + border-radius: 4px; + overflow-x: auto; } + +.declaration .highlight { + overflow-x: initial; + padding: 0 40px 40px 0; + margin-bottom: -25px; + background-color: transparent; + border: none; } + +.section-name { + margin: 0; + margin-left: 18px; } + +.task-group-section { + padding-left: 6px; + border-top: 1px solid #e2e2e2; } + +.task-group { + padding-top: 0px; } + +.task-name-container a[name]:before { + content: ""; + display: block; + padding-top: 70px; + margin: -70px 0 0; } + +.item { + padding-top: 8px; + width: 100%; + list-style-type: none; } + .item a[name]:before { + content: ""; + display: block; + padding-top: 70px; + margin: -70px 0 0; } + .item code { + background-color: transparent; + padding: 0; } + .item .token, .item .direct-link { + padding-left: 3px; + margin-left: 15px; + font-size: 11.9px; + transition: all 300ms; } + .item .token-open { + margin-left: 0px; } + .item .discouraged { + text-decoration: line-through; } + .item .declaration-note { + font-size: .85em; + color: gray; + font-style: italic; } + +.pointer-container { + border-bottom: 1px solid #e2e2e2; + left: -23px; + padding-bottom: 13px; + position: relative; + width: 110%; } + +.pointer { + background: #f9f9f9; + border-left: 1px solid #e2e2e2; + border-top: 1px solid #e2e2e2; + height: 12px; + left: 21px; + top: -7px; + -webkit-transform: rotate(45deg); + -moz-transform: rotate(45deg); + -o-transform: rotate(45deg); + transform: rotate(45deg); + position: absolute; + width: 12px; } + +.height-container { + display: none; + left: -25px; + padding: 0 25px; + position: relative; + width: 100%; + overflow: hidden; } + .height-container .section { + background: #f9f9f9; + border-bottom: 1px solid #e2e2e2; + left: -25px; + position: relative; + width: 100%; + padding-top: 10px; + padding-bottom: 5px; } + +.aside, .language { + padding: 6px 12px; + margin: 12px 0; + border-left: 5px solid #dddddd; + overflow-y: hidden; } + .aside .aside-title, .language .aside-title { + font-size: 9px; + letter-spacing: 2px; + text-transform: uppercase; + padding-bottom: 0; + margin: 0; + color: #aaa; + -webkit-user-select: none; } + .aside p:last-child, .language p:last-child { + margin-bottom: 0; } + +.language { + border-left: 5px solid #cde9f4; } + .language .aside-title { + color: #4b8afb; } + +.aside-warning, .aside-deprecated, .aside-unavailable { + border-left: 5px solid #ff6666; } + .aside-warning .aside-title, .aside-deprecated .aside-title, .aside-unavailable .aside-title { + color: #ff0000; } + +.graybox { + border-collapse: collapse; + width: 100%; } + .graybox p { + margin: 0; + word-break: break-word; + min-width: 50px; } + .graybox td { + border: 1px solid #e2e2e2; + padding: 5px 25px 5px 10px; + vertical-align: middle; } + .graybox tr td:first-of-type { + text-align: right; + padding: 7px; + vertical-align: top; + word-break: normal; + width: 40px; } + +.slightly-smaller { + font-size: 0.9em; } + +#footer { + position: relative; + top: 10px; + bottom: 0px; + margin-left: 25px; } + #footer p { + margin: 0; + color: #aaa; + font-size: 0.8em; } + +html.dash header, html.dash #breadcrumbs, html.dash .sidebar { + display: none; } +html.dash .main-content { + width: 980px; + margin-left: 0; + border: none; + width: 100%; + top: 0; + padding-bottom: 0; } +html.dash .height-container { + display: block; } +html.dash .item .token { + margin-left: 0; } +html.dash .content-wrapper { + width: auto; } +html.dash #footer { + position: static; } diff --git a/docs/docsets/.docset/Contents/Info.plist b/docs/docsets/.docset/Contents/Info.plist new file mode 100644 index 0000000..61863ec --- /dev/null +++ b/docs/docsets/.docset/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleIdentifier + com.jazzy. + CFBundleName + + DocSetPlatformFamily + + isDashDocset + + dashIndexFilePath + index.html + isJavaScriptEnabled + + DashDocSetFamily + dashtoc + + diff --git a/docs/docsets/.docset/Contents/Resources/Documents/Structs.html b/docs/docsets/.docset/Contents/Resources/Documents/Structs.html new file mode 100644 index 0000000..97523ff --- /dev/null +++ b/docs/docsets/.docset/Contents/Resources/Documents/Structs.html @@ -0,0 +1,113 @@ + + + + Structures Reference + + + + + + + + + + +
+
+

Docs (100% documented)

+
+
+
+ +
+
+ +
+
+
+

Structures

+

The following structures are available globally.

+ +
+
+
+
    +
  • +
    + + + + BInt + +
    +
    +
    +
    +
    +
    +

    A signed integer of unbounded size. +A BInt value is represented with magnitude and sign. +The magnitude is an array of unsigned 64 bit integers (a.k.a. Limbs). +The sign is a boolean value, true means value < 0, false means value >= 0 +The representation is little-endian, least significant Limb has index 0. +The representation is minimal, there is no leading zero Limbs. +The exception is that the value 0 is represented as a single 64 bit zero Limb and sign false

    + + See more +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public struct BInt : CustomStringConvertible, Equatable, Hashable
    + +
    +
    +
    +
    +
  • +
+
+
+
+ +
+
+ + + diff --git a/docs/docsets/.docset/Contents/Resources/Documents/Structs/BInt.html b/docs/docsets/.docset/Contents/Resources/Documents/Structs/BInt.html new file mode 100644 index 0000000..e12deab --- /dev/null +++ b/docs/docsets/.docset/Contents/Resources/Documents/Structs/BInt.html @@ -0,0 +1,5734 @@ + + + + BInt Structure Reference + + + + + + + + + + +
+
+

Docs (100% documented)

+
+
+
+ +
+
+ +
+
+
+

BInt

+
+
+
public struct BInt : CustomStringConvertible, Equatable, Hashable
+ +
+
+

A signed integer of unbounded size. +A BInt value is represented with magnitude and sign. +The magnitude is an array of unsigned 64 bit integers (a.k.a. Limbs). +The sign is a boolean value, true means value < 0, false means value >= 0 +The representation is little-endian, least significant Limb has index 0. +The representation is minimal, there is no leading zero Limbs. +The exception is that the value 0 is represented as a single 64 bit zero Limb and sign false

+ +
+
+
+ +
    +
  • +
    + + + + ZERO + +
    +
    +
    +
    +
    +
    +

    BInt(0)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let ZERO: BInt
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + ONE + +
    +
    +
    +
    +
    +
    +

    BInt(1)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let ONE: BInt
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + TWO + +
    +
    +
    +
    +
    +
    +

    BInt(2)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let TWO: BInt
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + THREE + +
    +
    +
    +
    +
    +
    +

    BInt(3)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let THREE: BInt
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + TEN + +
    +
    +
    +
    +
    +
    +

    BInt(10)

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static let TEN: BInt
    + +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + init(_:_:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from magnitude and sign

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(_ magnitude: Limbs, _ isNegative: Bool = false)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + magnitude + + +
    +

    magnitude of value

    +
    +
    + + isNegative + + +
    +

    true means negative value, false means 0 or positive value, default is false

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(_:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from an Int value

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(_ x: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Int value

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(_:radix:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from a String value and radix

    + +

    Examples:

    + +
      +
    • BInt(90abcdef, radix = 16)
    • +
    • BInt(111110010, radix = 2)
    • +
    • BInt(1cdefghijk44, radix = 26)
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init?(_ x: String, radix: Int = 10)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    String value to be converted

    +
    +
    + + radix + + +
    +

    Radix of x, from 2 to 36 inclusive, default is 10

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(bitWidth:) + +
    +
    +
    +
    +
    +
    +

    Constructs a random BInt with a specified number of bits

    +
    +

    Precondition

    + bitWidth is positive + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(bitWidth: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + bitWidth + + +
    +

    Number of bits

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(magnitude:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from a big-endian magnitude byte array representation

    +
    +

    Precondition

    + Byte array is not empty + +
    + +

    Examples:

    + +
      +
    • The byte array [1, 0, 0] represents BInt value 65536
    • +
    • The byte array [128, 0] represents BInt value 32768
    • +
    • The byte array [255, 127] represents BInt value 65407
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(magnitude x: Bytes)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Magnitude big-endian byte array

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + init(signed:) + +
    +
    +
    +
    +
    +
    +

    Constructs a BInt from a big-endian 2’s complement byte array representation

    +
    +

    Precondition

    + Byte array is not empty + +
    + +

    Examples:

    + +
      +
    • The byte array [1, 0, 0] represents BInt value 65536
    • +
    • The byte array [128, 0] represents BInt value -32768
    • +
    • The byte array [255, 127] represents BInt value -129
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public init(signed x: Bytes)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    2’s complement big-endian byte array

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + isNegative + +
    +
    +
    +
    +
    +
    +

    The sign, true if self < 0, false otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public internal(set) var isNegative: Bool
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + magnitude + +
    +
    +
    +
    +
    +
    +

    The magnitude limb array

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public internal(set) var magnitude: Limbs
    + +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + bitWidth + +
    +
    +
    +
    +
    +
    +

    The number of bits in the magnitude of self. 0 if self = 0

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var bitWidth: Int { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + description + +
    +
    +
    +
    +
    +
    +

    Base 10 string value of self

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var description: String { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isEven + +
    +
    +
    +
    +
    +
    +

    Is true if self is even, false if self is odd

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isEven: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isNotZero + +
    +
    +
    +
    +
    +
    +

    Is false if self = 0, true otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isNotZero: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isOdd + +
    +
    +
    +
    +
    +
    +

    Is true if self is odd, false if self is even

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isOdd: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isOne + +
    +
    +
    +
    +
    +
    +

    Is true if self = 1, false otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isOne: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isPositive + +
    +
    +
    +
    +
    +
    +

    Is true if self > 0, false otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isPositive: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + isZero + +
    +
    +
    +
    +
    +
    +

    Is true if self = 0, false otherwise

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var isZero: Bool { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + leadingZeroBitCount + +
    +
    +
    +
    +
    +
    +

    The number of leading zero bits in the magnitude of self. 0 if self = 0

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var leadingZeroBitCount: Int { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + signum + +
    +
    +
    +
    +
    +
    +

    Is 0 if self = 0, 1 if self > 0, and -1 if self < 0

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var signum: Int { get }
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + trailingZeroBitCount + +
    +
    +
    +
    +
    +
    +

    The number of trailing zero bits in the magnitude of self. 0 if self = 0

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public var trailingZeroBitCount: Int { get }
    + +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + asInt() + +
    +
    +
    +
    +
    +
    +

    self as an Int

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func asInt() -> Int?
    + +
    +
    +
    +

    Return Value

    +

    self as an Int or nil if self is not representable as an Int

    +
    +
    +
    +
  • +
  • +
    + + + + asMagnitudeBytes() + +
    +
    +
    +
    +
    +
    +

    Byte array representation of magnitude value

    + +

    Examples:

    + +
      +
    • BInt(1).asMagnitudeBytes() = [1]
    • +
    • BInt(-1).asMagnitudeBytes() = [1]
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func asMagnitudeBytes() -> Bytes
    + +
    +
    +
    +

    Return Value

    +

    Minimal big-endian magnitude byte array representation

    +
    +
    +
    +
  • +
  • +
    + + + + asSignedBytes() + +
    +
    +
    +
    +
    +
    +

    Byte array representation of 2’s complement value

    + +

    Examples:

    + +
      +
    • BInt(1).asSignedBytes() = [1]
    • +
    • BInt(-1).asSignedBytes() = [255]
    • +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func asSignedBytes() -> Bytes
    + +
    +
    +
    +

    Return Value

    +

    Minimal big-endian 2’s complement byte array representation

    +
    +
    +
    +
  • +
  • + +
    +
    +
    +
    +
    +

    self as a String with a given radix

    +
    +

    Precondition

    + Radix between 2 and 36 inclusive + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func asString(radix: Int = 10, uppercase: Bool = false) -> String
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + radix + + +
    +

    Radix from 2 to 36 inclusive

    +
    +
    + + uppercase + + +
    +

    true to use uppercase letters, false to use lowercase letters, default is false

    +
    +
    +
    +
    +

    Return Value

    +

    self as a String in the given radix

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + &(_:_:) + +
    +
    +
    +
    +
    +
    +

    Bitwise and operator

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func & (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First value

    +
    +
    + + y + + +
    +

    Second value

    +
    +
    +
    +
    +

    Return Value

    +

    BInt(signed: bx & by) where + +

      +
    • bx = x.asSignedBytes()
    • +
    • by = y.asSignedBytes()
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + &=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x & y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func &= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand parameter

    +
    +
    + + y + + +
    +

    Right hand parameter

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + |(_:_:) + +
    +
    +
    +
    +
    +
    +

    Bitwise or operator

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func | (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First value

    +
    +
    + + y + + +
    +

    Second value

    +
    +
    +
    +
    +

    Return Value

    +

    BInt(signed: bx | by) where + +

      +
    • bx = x.asSignedBytes()
    • +
    • by = y.asSignedBytes()
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + |=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x | y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func |= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand parameter

    +
    +
    + + y + + +
    +

    Right hand parameter

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + ^(_:_:) + +
    +
    +
    +
    +
    +
    +

    Bitwise xor operator

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func ^ (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First value

    +
    +
    + + y + + +
    +

    Second value

    +
    +
    +
    +
    +

    Return Value

    +

    BInt(signed: bx ^ by) where + +

      +
    • bx = x.asSignedBytes()
    • +
    • by = y.asSignedBytes()
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + ^=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x ^ y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func ^= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand parameter

    +
    +
    + + y + + +
    +

    Right hand parameter

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + ~(_:) + +
    +
    +
    +
    +
    +
    +

    Bitwise not operator

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public prefix static func ~ (x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    BInt value

    +
    +
    +
    +
    +

    Return Value

    +

    -x - 1

    +
    +
    +
    +
  • +
  • +
    + + + + clearBit(_:) + +
    +
    +
    +
    +
    +
    +

    Clear a specified bit

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public mutating func clearBit(_ n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    Bit number

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + flipBit(_:) + +
    +
    +
    +
    +
    +
    +

    Invert a specified bit

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public mutating func flipBit(_ n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    Bit number

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + setBit(_:) + +
    +
    +
    +
    +
    +
    +

    Set a specified bit

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public mutating func setBit(_ n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    Bit number

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + testBit(_:) + +
    +
    +
    +
    +
    +
    +

    Test a specified bit

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func testBit(_ n: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    Bit number

    +
    +
    +
    +
    +

    Return Value

    +

    true if bit is set, false otherwise

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + +(_:) + +
    +
    +
    +
    +
    +
    +

    Prefix plus

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public prefix static func + (x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    BInt value

    +
    +
    +
    +
    +

    Return Value

    +

    x

    +
    +
    +
    +
  • +
  • +
    + + + + +(_:_:) + +
    +
    +
    +
    +
    +
    +

    Addition

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func + (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First addend

    +
    +
    + + y + + +
    +

    Second addend

    +
    +
    +
    +
    +

    Return Value

    +

    x + y

    +
    +
    +
    +
  • +
  • +
    + + + + +(_:_:) + +
    +
    +
    +
    +
    +
    +

    Addition

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func + (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First addend

    +
    +
    + + y + + +
    +

    Second addend

    +
    +
    +
    +
    +

    Return Value

    +

    x + y

    +
    +
    +
    +
  • +
  • +
    + + + + +(_:_:) + +
    +
    +
    +
    +
    +
    +

    Addition

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func + (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First addend

    +
    +
    + + y + + +
    +

    Second addend

    +
    +
    +
    +
    +

    Return Value

    +

    x + y

    +
    +
    +
    +
  • +
  • +
    + + + + +=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x + y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func += (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand addend

    +
    +
    + + y + + +
    +

    Right hand addend

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + +=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x + y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func += (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand addend

    +
    +
    + + y + + +
    +

    Right hand addend

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + negate() + +
    +
    +
    +
    +
    +
    +

    Negates self

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public mutating func negate()
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + -(_:) + +
    +
    +
    +
    +
    +
    +

    Negation

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public prefix static func - (x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    +
    +
    +

    Return Value

    +

    -x

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + -(_:_:) + +
    +
    +
    +
    +
    +
    +

    Subtraction

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func - (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Minuend

    +
    +
    + + y + + +
    +

    Subtrahend

    +
    +
    +
    +
    +

    Return Value

    +

    x - y

    +
    +
    +
    +
  • +
  • +
    + + + + -(_:_:) + +
    +
    +
    +
    +
    +
    +

    Subtraction

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func - (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Minuend

    +
    +
    + + y + + +
    +

    Subtrahend

    +
    +
    +
    +
    +

    Return Value

    +

    x - y

    +
    +
    +
    +
  • +
  • +
    + + + + -(_:_:) + +
    +
    +
    +
    +
    +
    +

    Subtraction

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func - (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Minuend

    +
    +
    + + y + + +
    +

    Subtrahend

    +
    +
    +
    +
    +

    Return Value

    +

    x - y

    +
    +
    +
    +
  • +
  • +
    + + + + -=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x - y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func -= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand minuend

    +
    +
    + + y + + +
    +

    Right hand subtrahend

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + -=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x - y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func -= (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand minuend

    +
    +
    + + y + + +
    +

    Right hand subtrahend

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + *(_:_:) + +
    +
    +
    +
    +
    +
    +

    Multiplication

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func * (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Multiplier

    +
    +
    + + y + + +
    +

    Multiplicand

    +
    +
    +
    +
    +

    Return Value

    +

    x * y

    +
    +
    +
    +
  • +
  • +
    + + + + *(_:_:) + +
    +
    +
    +
    +
    +
    +

    Multiplication

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func * (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Multiplier

    +
    +
    + + y + + +
    +

    Multiplicand

    +
    +
    +
    +
    +

    Return Value

    +

    x * y

    +
    +
    +
    +
  • +
  • +
    + + + + *(_:_:) + +
    +
    +
    +
    +
    +
    +

    Multiplication

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func * (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Multiplier

    +
    +
    + + y + + +
    +

    Multiplicand

    +
    +
    +
    +
    +

    Return Value

    +

    x * y

    +
    +
    +
    +
  • +
  • +
    + + + + *=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x * y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func *= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand multiplier

    +
    +
    + + y + + +
    +

    Right hand multiplicand

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + *=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x * y

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func *= (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand multiplier

    +
    +
    + + y + + +
    +

    Right hand multiplicand

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • + +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func quotientAndRemainder(dividingBy x: BInt) -> (quotient: BInt, remainder: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    Quotient and remainder of self / x

    +
    +
    +
    +
  • +
  • + +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func quotientAndRemainder(dividingBy x: BInt, _ quotient: inout BInt, _ remainder: inout BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + + + + + +
    + + x + + +
    +

    Divisor

    +
    +
    + + quotient + + +
    +

    Set to the quotient of self / x

    +
    +
    + + remainder + + +
    +

    Set to the remainder of self / x

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + /(_:_:) + +
    +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func / (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x / y

    +
    +
    +
    +
  • +
  • +
    + + + + /(_:_:) + +
    +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func / (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x / y

    +
    +
    +
    +
  • +
  • +
    + + + + /(_:_:) + +
    +
    +
    +
    +
    +
    +

    Division

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func / (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x / y

    +
    +
    +
    +
  • +
  • +
    + + + + /=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x / y

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func /= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand dividend

    +
    +
    + + y + + +
    +

    Right hand divisor

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + /=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x / y

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func /= (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Left hand dividend

    +
    +
    + + y + + +
    +

    Right hand divisor

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + %(_:_:) + +
    +
    +
    +
    +
    +
    +

    Remainder

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func % (x: BInt, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x % y

    +
    +
    +
    +
  • +
  • +
    + + + + %(_:_:) + +
    +
    +
    +
    +
    +
    +

    Remainder

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func % (x: Int, y: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x % y

    +
    +
    +
    +
  • +
  • +
    + + + + %(_:_:) + +
    +
    +
    +
    +
    +
    +

    Remainder

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func % (x: BInt, y: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    x % y

    +
    +
    +
    +
  • +
  • +
    + + + + %=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x % y

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func %= (x: inout BInt, y: BInt)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + %=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x % y

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func %= (x: inout BInt, y: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Dividend

    +
    +
    + + y + + +
    +

    Divisor

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + mod(_:) + +
    +
    +
    +
    +
    +
    +

    Modulus

    +
    +

    Precondition

    + Divisor is not zero + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func mod(_ x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Divisor

    +
    +
    +
    +
    +

    Return Value

    +

    self mod x, a non-negative value

    +
    +
    +
    +
  • +
  • +
    + + + + modInverse(_:) + +
    +
    +
    +
    +
    +
    +

    Inverse modulus

    +
    +

    Precondition

    + self and modulus are coprime + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func modInverse(_ m: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + m + + +
    +

    Modulus

    +
    +
    +
    +
    +

    Return Value

    +

    If self and m are coprime, x such that (self * x) mod m = 1

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + **(_:_:) + +
    +
    +
    +
    +
    +
    +

    Exponentiation

    +
    +

    Precondition

    + Exponent is non-negative + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func ** (a: BInt, x: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + a + + +
    +

    Operand

    +
    +
    + + x + + +
    +

    Non-negative exponent

    +
    +
    +
    +
    +

    Return Value

    +

    a^x

    +
    +
    +
    +
  • +
  • +
    + + + + expMod(_:_:) + +
    +
    +
    +
    +
    +
    +

    Modular exponentiation

    +
    +

    Precondition

    + Modulus is positive + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func expMod(_ x: BInt, _ m: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    The exponent

    +
    +
    + + m + + +
    +

    The modulus, a positive number

    +
    +
    +
    +
    +

    Return Value

    +

    (self^x) mod m for positive x, ((self^-x) mod m) modInverse m for negative x

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + ==(_:_:) + +
    +
    +
    +
    +
    +
    +

    Equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func == (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x = y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + ==(_:_:) + +
    +
    +
    +
    +
    +
    +

    Equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func == (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x = y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + ==(_:_:) + +
    +
    +
    +
    +
    +
    +

    Equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func == (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x = y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + !=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Not equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func != (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x != y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + !=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Not equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func != (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x != y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + !=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Not equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func != (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x != y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less then

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func < (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x < y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func < (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x < y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func < (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x < y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func > (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x > y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func > (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x > y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func > (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x > y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func <= (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x <= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func <= (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x <= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + <=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Less than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func <= (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x <= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >= (x: BInt, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x >= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >= (x: Int, y: BInt) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x >= y, false otherwise

    +
    +
    +
    +
  • +
  • +
    + + + + >=(_:_:) + +
    +
    +
    +
    +
    +
    +

    Greater than or equal

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >= (x: BInt, y: Int) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    First operand

    +
    +
    + + y + + +
    +

    Second operand

    +
    +
    +
    +
    +

    Return Value

    +

    true if x >= y, false otherwise

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + <<(_:_:) + +
    +
    +
    +
    +
    +
    +

    Logical left shift

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func << (x: BInt, n: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    + + n + + +
    +

    Shift count

    +
    +
    +
    +
    +

    Return Value

    +

      +
    • BInt(x.magnitude << n, x.isNegative) if n > 0
    • +
    • BInt(x.magnitude >> -n, x.isNegative) if n < 0
    • +
    • x if n = 0
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + <<=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x << n

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func <<= (x: inout BInt, n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    + + n + + +
    +

    Shift count

    +
    +
    +
    +
    +
    +
  • +
  • +
    + + + + >>(_:_:) + +
    +
    +
    +
    +
    +
    +

    Logical right shift

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >> (x: BInt, n: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    + + n + + +
    +

    Shift count

    +
    +
    +
    +
    +

    Return Value

    +

      +
    • BInt(x.magnitude >> n, x.isNegative) if n > 0
    • +
    • BInt(x.magnitude << -n, x.isNegative) if n < 0
    • +
    • x if n = 0
    • +

    +
    +
    +
    +
  • +
  • +
    + + + + >>=(_:_:) + +
    +
    +
    +
    +
    +
    +

    x = x >> n

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func >>= (x: inout BInt, n: Int)
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    + + n + + +
    +

    Shift count

    +
    +
    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + isProbablyPrime(_:) + +
    +
    +
    +
    +
    +
    +

    Checks whether self is prime using the Miller-Rabin algorithm

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func isProbablyPrime(_ p: Int = 30) -> Bool
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + p + + +
    +

    If true is returned, self is prime with probability > 1-½^p

    +
    +
    +
    +
    +

    Return Value

    +

    true if self is probably prime, false if self is definitely not prime

    +
    +
    +
    +
  • +
  • +
    + + + + probablePrime(_:_:) + +
    +
    +
    +
    +
    +
    +

    A probable prime number with a given bitwidth

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public static func probablePrime(_ bitWidth: Int, _ p: Int = 30) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + + + + + +
    + + bitWidth + + +
    +

    The bitWidth

    +
    +
    + + p + + +
    +

    The returned number is prime with probability > 1-½^p, default value is 30

    +
    +
    +
    +
    +

    Return Value

    +

    A prime number with the specified bitwidth and probability

    +
    +
    +
    +
  • +
+
+
+ +
    +
  • +
    + + + + abs() + +
    +
    +
    +
    +
    +
    +

    Absolute value

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func abs() -> BInt
    + +
    +
    +
    +

    Return Value

    +

    The absolute value of self

    +
    +
    +
    +
  • +
  • +
    + + + + gcd(_:) + +
    +
    +
    +
    +
    +
    +

    Greatest common divisor

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func gcd(_ x: BInt) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + x + + +
    +

    Operand

    +
    +
    +
    +
    +

    Return Value

    +

    Greatest common divisor of magnitude of self and magnitude of x

    +
    +
    +
    +
  • +
  • +
    + + + + jacobiSymbol(_:) + +
    +
    +
    +
    +
    +
    +

    Jacobi symbol

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func jacobiSymbol(_ m: BInt) -> Int
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + m + + +
    +

    An integer value

    +
    +
    +
    +
    +

    Return Value

    +

    The Jacobi symbol of self and m, or 0 if it does not exist

    +
    +
    +
    +
  • +
  • +
    + + + + randomLessThan() + +
    +
    +
    +
    +
    +
    +

    Random value

    +
    +

    Precondition

    + self is positive + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func randomLessThan() -> BInt
    + +
    +
    +
    +

    Return Value

    +

    A random value < absolute value of self

    +
    +
    +
    +
  • +
  • +
    + + + + root(_:) + +
    +
    +
    +
    +
    +
    +

    n'th root of a non-negative number

    +
    +

    Precondition

    +
      +
    • self is non-negative
    • +
    • n is positive
    • +
    + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func root(_ n: Int) -> BInt
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + n + + +
    +

    The root

    +
    +
    +
    +
    +

    Return Value

    +

    Largest x such that x^n <= self

    +
    +
    +
    +
  • +
  • +
    + + + + sqrt() + +
    +
    +
    +
    +
    +
    +

    Square root of a non-negative number

    +
    +

    Precondition

    + self is non-negative + +
    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func sqrt() -> BInt
    + +
    +
    +
    +

    Return Value

    +

    Largest x such that x^2 <= self

    +
    +
    +
    +
  • +
  • +
    + + + + sqrtMod(_:) + +
    +
    +
    +
    +
    +
    +

    Square root modulo a prime number

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public func sqrtMod(_ p: BInt) -> BInt?
    + +
    +
    +
    +

    Parameters

    + + + + + + + +
    + + p + + +
    +

    An odd prime number

    +
    +
    +
    +
    +

    Return Value

    +

    x, such that x^2 mod p = self, or nil if no such x exists

    +
    +
    +
    +
  • +
+
+
+
+ +
+
+ + + diff --git a/docs/docsets/.docset/Contents/Resources/Documents/Typealiases.html b/docs/docsets/.docset/Contents/Resources/Documents/Typealiases.html new file mode 100644 index 0000000..2a06fbe --- /dev/null +++ b/docs/docsets/.docset/Contents/Resources/Documents/Typealiases.html @@ -0,0 +1,187 @@ + + + + Type Aliases Reference + + + + + + + + + + +
+
+

Docs (100% documented)

+
+
+
+ +
+
+ +
+
+
+

Type Aliases

+

The following type aliases are available globally.

+ +
+
+
+
    +
  • +
    + + + + Byte + +
    +
    +
    +
    +
    +
    +

    Unsigned 8 bit value

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public typealias Byte = UInt8
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + Bytes + +
    +
    +
    +
    +
    +
    +

    Array of unsigned 8 bit values

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public typealias Bytes = [UInt8]
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + Limb + +
    +
    +
    +
    +
    +
    +

    Unsigned 64 bit value

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public typealias Limb = UInt64
    + +
    +
    +
    +
    +
  • +
  • +
    + + + + Limbs + +
    +
    +
    +
    +
    +
    +

    Array of unsigned 64 bit values

    + +
    +
    +

    Declaration

    +
    +

    Swift

    +
    public typealias Limbs = [UInt64]
    + +
    +
    +
    +
    +
  • +
+
+
+
+ +
+
+ + + diff --git a/docs/docsets/.docset/Contents/Resources/Documents/css/highlight.css b/docs/docsets/.docset/Contents/Resources/Documents/css/highlight.css new file mode 100644 index 0000000..d0db0e1 --- /dev/null +++ b/docs/docsets/.docset/Contents/Resources/Documents/css/highlight.css @@ -0,0 +1,200 @@ +/* Credit to https://gist.github.com/wataru420/2048287 */ +.highlight { + /* Comment */ + /* Error */ + /* Keyword */ + /* Operator */ + /* Comment.Multiline */ + /* Comment.Preproc */ + /* Comment.Single */ + /* Comment.Special */ + /* Generic.Deleted */ + /* Generic.Deleted.Specific */ + /* Generic.Emph */ + /* Generic.Error */ + /* Generic.Heading */ + /* Generic.Inserted */ + /* Generic.Inserted.Specific */ + /* Generic.Output */ + /* Generic.Prompt */ + /* Generic.Strong */ + /* Generic.Subheading */ + /* Generic.Traceback */ + /* Keyword.Constant */ + /* Keyword.Declaration */ + /* Keyword.Pseudo */ + /* Keyword.Reserved */ + /* Keyword.Type */ + /* Literal.Number */ + /* Literal.String */ + /* Name.Attribute */ + /* Name.Builtin */ + /* Name.Class */ + /* Name.Constant */ + /* Name.Entity */ + /* Name.Exception */ + /* Name.Function */ + /* Name.Namespace */ + /* Name.Tag */ + /* Name.Variable */ + /* Operator.Word */ + /* Text.Whitespace */ + /* Literal.Number.Float */ + /* Literal.Number.Hex */ + /* Literal.Number.Integer */ + /* Literal.Number.Oct */ + /* Literal.String.Backtick */ + /* Literal.String.Char */ + /* Literal.String.Doc */ + /* Literal.String.Double */ + /* Literal.String.Escape */ + /* Literal.String.Heredoc */ + /* Literal.String.Interpol */ + /* Literal.String.Other */ + /* Literal.String.Regex */ + /* Literal.String.Single */ + /* Literal.String.Symbol */ + /* Name.Builtin.Pseudo */ + /* Name.Variable.Class */ + /* Name.Variable.Global */ + /* Name.Variable.Instance */ + /* Literal.Number.Integer.Long */ } + .highlight .c { + color: #999988; + font-style: italic; } + .highlight .err { + color: #a61717; + background-color: #e3d2d2; } + .highlight .k { + color: #000000; + font-weight: bold; } + .highlight .o { + color: #000000; + font-weight: bold; } + .highlight .cm { + color: #999988; + font-style: italic; } + .highlight .cp { + color: #999999; + font-weight: bold; } + .highlight .c1 { + color: #999988; + font-style: italic; } + .highlight .cs { + color: #999999; + font-weight: bold; + font-style: italic; } + .highlight .gd { + color: #000000; + background-color: #ffdddd; } + .highlight .gd .x { + color: #000000; + background-color: #ffaaaa; } + .highlight .ge { + color: #000000; + font-style: italic; } + .highlight .gr { + color: #aa0000; } + .highlight .gh { + color: #999999; } + .highlight .gi { + color: #000000; + background-color: #ddffdd; } + .highlight .gi .x { + color: #000000; + background-color: #aaffaa; } + .highlight .go { + color: #888888; } + .highlight .gp { + color: #555555; } + .highlight .gs { + font-weight: bold; } + .highlight .gu { + color: #aaaaaa; } + .highlight .gt { + color: #aa0000; } + .highlight .kc { + color: #000000; + font-weight: bold; } + .highlight .kd { + color: #000000; + font-weight: bold; } + .highlight .kp { + color: #000000; + font-weight: bold; } + .highlight .kr { + color: #000000; + font-weight: bold; } + .highlight .kt { + color: #445588; } + .highlight .m { + color: #009999; } + .highlight .s { + color: #d14; } + .highlight .na { + color: #008080; } + .highlight .nb { + color: #0086B3; } + .highlight .nc { + color: #445588; + font-weight: bold; } + .highlight .no { + color: #008080; } + .highlight .ni { + color: #800080; } + .highlight .ne { + color: #990000; + font-weight: bold; } + .highlight .nf { + color: #990000; } + .highlight .nn { + color: #555555; } + .highlight .nt { + color: #000080; } + .highlight .nv { + color: #008080; } + .highlight .ow { + color: #000000; + font-weight: bold; } + .highlight .w { + color: #bbbbbb; } + .highlight .mf { + color: #009999; } + .highlight .mh { + color: #009999; } + .highlight .mi { + color: #009999; } + .highlight .mo { + color: #009999; } + .highlight .sb { + color: #d14; } + .highlight .sc { + color: #d14; } + .highlight .sd { + color: #d14; } + .highlight .s2 { + color: #d14; } + .highlight .se { + color: #d14; } + .highlight .sh { + color: #d14; } + .highlight .si { + color: #d14; } + .highlight .sx { + color: #d14; } + .highlight .sr { + color: #009926; } + .highlight .s1 { + color: #d14; } + .highlight .ss { + color: #990073; } + .highlight .bp { + color: #999999; } + .highlight .vc { + color: #008080; } + .highlight .vg { + color: #008080; } + .highlight .vi { + color: #008080; } + .highlight .il { + color: #009999; } diff --git a/docs/docsets/.docset/Contents/Resources/Documents/css/jazzy.css b/docs/docsets/.docset/Contents/Resources/Documents/css/jazzy.css new file mode 100644 index 0000000..0541d71 --- /dev/null +++ b/docs/docsets/.docset/Contents/Resources/Documents/css/jazzy.css @@ -0,0 +1,343 @@ +html, body, div, span, h1, h3, h4, p, a, code, em, img, ul, li, table, tbody, tr, td { + background: transparent; + border: 0; + margin: 0; + outline: 0; + padding: 0; + vertical-align: baseline; } + +body { + background-color: #f2f2f2; + font-family: Helvetica, freesans, Arial, sans-serif; + font-size: 14px; + -webkit-font-smoothing: subpixel-antialiased; + word-wrap: break-word; } + +h1, h2, h3 { + margin-top: 0.8em; + margin-bottom: 0.3em; + font-weight: 100; + color: black; } + +h1 { + font-size: 2.5em; } + +h2 { + font-size: 2em; + border-bottom: 1px solid #e2e2e2; } + +h4 { + font-size: 13px; + line-height: 1.5; + margin-top: 21px; } + +h5 { + font-size: 1.1em; } + +h6 { + font-size: 1.1em; + color: #777; } + +.section-name { + color: gray; + display: block; + font-family: Helvetica; + font-size: 22px; + font-weight: 100; + margin-bottom: 15px; } + +pre, code { + font: 0.95em Menlo, monospace; + color: #777; + word-wrap: normal; } + +p code, li code { + background-color: #eee; + padding: 2px 4px; + border-radius: 4px; } + +a { + color: #0088cc; + text-decoration: none; } + +ul { + padding-left: 15px; } + +li { + line-height: 1.8em; } + +img { + max-width: 100%; } + +blockquote { + margin-left: 0; + padding: 0 10px; + border-left: 4px solid #ccc; } + +.content-wrapper { + margin: 0 auto; + width: 980px; } + +header { + font-size: 0.85em; + line-height: 26px; + background-color: #414141; + position: fixed; + width: 100%; + z-index: 1; } + header img { + padding-right: 6px; + vertical-align: -4px; + height: 16px; } + header a { + color: #fff; } + header p { + float: left; + color: #999; } + header .header-right { + float: right; + margin-left: 16px; } + +#breadcrumbs { + background-color: #f2f2f2; + height: 27px; + padding-top: 17px; + position: fixed; + width: 100%; + z-index: 1; + margin-top: 26px; } + #breadcrumbs #carat { + height: 10px; + margin: 0 5px; } + +.sidebar { + background-color: #f9f9f9; + border: 1px solid #e2e2e2; + overflow-y: auto; + overflow-x: hidden; + position: fixed; + top: 70px; + bottom: 0; + width: 230px; + word-wrap: normal; } + +.nav-groups { + list-style-type: none; + background: #fff; + padding-left: 0; } + +.nav-group-name { + border-bottom: 1px solid #e2e2e2; + font-size: 1.1em; + font-weight: 100; + padding: 15px 0 15px 20px; } + .nav-group-name > a { + color: #333; } + +.nav-group-tasks { + margin-top: 5px; } + +.nav-group-task { + font-size: 0.9em; + list-style-type: none; + white-space: nowrap; } + .nav-group-task a { + color: #888; } + +.main-content { + background-color: #fff; + border: 1px solid #e2e2e2; + margin-left: 246px; + position: absolute; + overflow: hidden; + padding-bottom: 20px; + top: 70px; + width: 734px; } + .main-content p, .main-content a, .main-content code, .main-content em, .main-content ul, .main-content table, .main-content blockquote { + margin-bottom: 1em; } + .main-content p { + line-height: 1.8em; } + .main-content section .section:first-child { + margin-top: 0; + padding-top: 0; } + .main-content section .task-group-section .task-group:first-of-type { + padding-top: 10px; } + .main-content section .task-group-section .task-group:first-of-type .section-name { + padding-top: 15px; } + .main-content section .heading:before { + content: ""; + display: block; + padding-top: 70px; + margin: -70px 0 0; } + +.section { + padding: 0 25px; } + +.highlight { + background-color: #eee; + padding: 10px 12px; + border: 1px solid #e2e2e2; + border-radius: 4px; + overflow-x: auto; } + +.declaration .highlight { + overflow-x: initial; + padding: 0 40px 40px 0; + margin-bottom: -25px; + background-color: transparent; + border: none; } + +.section-name { + margin: 0; + margin-left: 18px; } + +.task-group-section { + padding-left: 6px; + border-top: 1px solid #e2e2e2; } + +.task-group { + padding-top: 0px; } + +.task-name-container a[name]:before { + content: ""; + display: block; + padding-top: 70px; + margin: -70px 0 0; } + +.item { + padding-top: 8px; + width: 100%; + list-style-type: none; } + .item a[name]:before { + content: ""; + display: block; + padding-top: 70px; + margin: -70px 0 0; } + .item code { + background-color: transparent; + padding: 0; } + .item .token, .item .direct-link { + padding-left: 3px; + margin-left: 15px; + font-size: 11.9px; + transition: all 300ms; } + .item .token-open { + margin-left: 0px; } + .item .discouraged { + text-decoration: line-through; } + .item .declaration-note { + font-size: .85em; + color: gray; + font-style: italic; } + +.pointer-container { + border-bottom: 1px solid #e2e2e2; + left: -23px; + padding-bottom: 13px; + position: relative; + width: 110%; } + +.pointer { + background: #f9f9f9; + border-left: 1px solid #e2e2e2; + border-top: 1px solid #e2e2e2; + height: 12px; + left: 21px; + top: -7px; + -webkit-transform: rotate(45deg); + -moz-transform: rotate(45deg); + -o-transform: rotate(45deg); + transform: rotate(45deg); + position: absolute; + width: 12px; } + +.height-container { + display: none; + left: -25px; + padding: 0 25px; + position: relative; + width: 100%; + overflow: hidden; } + .height-container .section { + background: #f9f9f9; + border-bottom: 1px solid #e2e2e2; + left: -25px; + position: relative; + width: 100%; + padding-top: 10px; + padding-bottom: 5px; } + +.aside, .language { + padding: 6px 12px; + margin: 12px 0; + border-left: 5px solid #dddddd; + overflow-y: hidden; } + .aside .aside-title, .language .aside-title { + font-size: 9px; + letter-spacing: 2px; + text-transform: uppercase; + padding-bottom: 0; + margin: 0; + color: #aaa; + -webkit-user-select: none; } + .aside p:last-child, .language p:last-child { + margin-bottom: 0; } + +.language { + border-left: 5px solid #cde9f4; } + .language .aside-title { + color: #4b8afb; } + +.aside-warning, .aside-deprecated, .aside-unavailable { + border-left: 5px solid #ff6666; } + .aside-warning .aside-title, .aside-deprecated .aside-title, .aside-unavailable .aside-title { + color: #ff0000; } + +.graybox { + border-collapse: collapse; + width: 100%; } + .graybox p { + margin: 0; + word-break: break-word; + min-width: 50px; } + .graybox td { + border: 1px solid #e2e2e2; + padding: 5px 25px 5px 10px; + vertical-align: middle; } + .graybox tr td:first-of-type { + text-align: right; + padding: 7px; + vertical-align: top; + word-break: normal; + width: 40px; } + +.slightly-smaller { + font-size: 0.9em; } + +#footer { + position: relative; + top: 10px; + bottom: 0px; + margin-left: 25px; } + #footer p { + margin: 0; + color: #aaa; + font-size: 0.8em; } + +html.dash header, html.dash #breadcrumbs, html.dash .sidebar { + display: none; } +html.dash .main-content { + width: 980px; + margin-left: 0; + border: none; + width: 100%; + top: 0; + padding-bottom: 0; } +html.dash .height-container { + display: block; } +html.dash .item .token { + margin-left: 0; } +html.dash .content-wrapper { + width: auto; } +html.dash #footer { + position: static; } diff --git a/docs/docsets/.docset/Contents/Resources/Documents/img/carat.png b/docs/docsets/.docset/Contents/Resources/Documents/img/carat.png new file mode 100755 index 0000000000000000000000000000000000000000..29d2f7fd4955fca6bc6fb740e0373a2c358c398e GIT binary patch literal 274 zcmeAS@N?(olHy`uVBq!ia0vp^0zfRo!3HEV4DF?Wlw^r(L`iUdT1k0gQ7VIDN`6wR zf@f}GdTLN=VoGJ<$y6JlqAi{-jv*Ddl5#RKJQ5NTUZgiPI4RUKGIKU?u8L&ndhX1t za+0CMVUnT(Gnb}ei=c~x==tMH^F1_tBocXwcoSWoO-SZY-o>!8%^=Bms)(~h;m_U( zXNixk28L}0LS5-jKyq@#2gyS|J&f#pGCLkTc<@2s1dqeyqJ*Rc0tSIETAgmODY;(s z2y|Mcp&2}7rpBprBBB~1qM1`N+}4SoxYVPqsXi&l`rxZp{(w0iSy$Nv5*Vy!RapG^ S^0y4=eg;ohKbLh*2~7a!Pg}VF literal 0 HcmV?d00001 diff --git a/docs/docsets/.docset/Contents/Resources/Documents/img/dash.png b/docs/docsets/.docset/Contents/Resources/Documents/img/dash.png new file mode 100755 index 0000000000000000000000000000000000000000..6f694c7a012b417908da3687a0a39aa182e91c74 GIT binary patch literal 1338 zcmaJ>U2NM_6t){^r>#wcfL0VSTvuX@)$vd4#5N6WVkc|1rR}naMb)(7I5(};#!el# zbtCASsp?W-qE8zSJoFVdA%-T$WL8RI_B? zd+t5o`T5Q{p6=<|U$?VqCxRe#u}(PwSIl{LRKstfSbPYV7pzFiI$~t4QN;vEC}X4n z7RxDpAOV!j*w8ni4MAK3S~6v&;)g`l$axh<$7|>E5RD*h?RH*K2Y`j8L7%1v@%vZi za7@bt@uOUvisvQJuXPqpaHQCkREqd6M>0WG?6AwXR*T65ziuw$&~q$MS$o zfPyh>s<0l}mI@eh_hd(oB8*1tHZ@ojWl%QM;T+Jdm>k66jW?rZ#Atx!qns4-g&E4v z(=;FQ%W^avW?3J{L@2IeV>_(Ca)Lk1vm70uX*$9Rewm8!AxRF0BcZTNSFka?U@5u^ zDtpMY2lVtCmQm<8@|YxHuf`Qs(;a!QQ=g4=WngL}AQLr> z9JWrdsBIHKHXF!fSydodRsaOc@jgNkSU^x9kY&;UP<}3pZ{joC5f_Tevd>4eG~;)Y z=eZ~qp=5#aaUn*E3OES^BApKTU&mCAU>iEyt^S9?)&v0^j*SWDqjRZr20>6rTPSJ& zlzz0f);`}+^~w}lP1PK7Ew3f7ot#*uJ@>1Yo3J0TdsRKpA+*n9JnDXDrM~YvF`;uS|vAh|-QdmRf4AqG=`U z#v1n_Lxg8;&z#YCU2K`_W{-A zUf_|V)B9U(WZ~PP>)O(JZ|Vc-*qP&Q{c~BE~6izDPQq)#Nu*KOf(n^(VHY9;fiINM65``pc+9*v(mL$bwfCjbc%v9V{8r9iX|O%>Nr%pLD2qT{mty}c=LVleeamv znz3SOSm@kP8jThvOOq(56Yzh*fz(booe!uZij=BJC6+_lbvQ~B8nA2>kXdv_RDtRY z`5QXWWEySCe6vbTs^#f?J!WC*{1~RgVx!nJTJjQyO{dRANgx|FnymtGbD9%JmCh9^y)##j7{Dcqfn*1ta$rG89pJF6w-S7Z037$rr|y0;1Onp_ zGFJdT6Q!1C0AdVB0WOmpuV=AgAQ550Tn+-mivTtYPJmz*#75#_n9oV%!#rSOfmAfy zki%C~=fTp1{O#BLpJ|0jj#m6#|LRWit-vq3PE1z9ZqyvET4sX$-Icqy7t z<=aq5ff86AuBZBu6EjJsYWM0uejufWFTwPA7Su}0Bm$7KFb!q{Um_8~A{LUG#1l(l zSehUda@kU8LIRg9fkk2tZ;~ss5~R+mM<==F7hLHpxqLB>>PQS%Vc7b~?q!%T5+h8Q z4G=4Nzyi5WZ?^gkasJ{?Xhm`JC#WG6$1K2jb@=9&D3EgD#3UhGh#*21rJjulVXjCF zvp76q62jt0zzMG5C7DlfMgPl%C^3+~wf|}Lq=}jz|MmIcQjh1Ok6NjD$Em^Iv26D> z8tt_TnM9~^Tt8mflRGPOrrX|HtT3gG4LEuuk{g2Rn}QgJIa?gZo))!!=o_l9bvD%A zZ`aHajl8#~u?!4f7F#*b*->A=R2L)6!>saz?h>#wTXT-I(XmQ zx{84skS>k=i~i`(6k4C7;Zpfx%dCPVjPayMf8pugtGM=~s=Id1l#8MZJ1-73wV#Q3 zR3>v3%}jbQs1f_Z0xo;%=LILlA+nTpKI4ha%xWW}uqHrNao~&T4AY6m`P$_n-6h*g zhoX+e4n%~gl_lhe#s+AMb7d{5WzvYTa%6Q~si@@4{;s(0zU|H&P3fE+t{7X`S#Cj@ zC#vd}^4pcBD*77Ny5=j$h8EL2_t$O38$SQiJ6fPjJMimypr~MB2(&P0aI|h}$64<0 z>_~duqNjaT=DM^6+N{&B_lED;F2wrl?!4Lk*2((x!fmrcsw+=cI^qttuZ9C}-m~5E z-ryYVpL%^xR#&(0YI5hz<(}F7-p)?FPcyJO-zVO>%9ZDXJH8pnY;GJYFDQ>vd#j_* zRrd}L(r=!g+1#nQwsO?kpS`Qq8`NxE+Zy{gf7*_7J*U2V_|NpLo{iasj7VCg_V9&| ShohtYzipXxh2)4xTk + + + Reference + + + + + + + + + +
+
+

Docs (100% documented)

+
+
+
+ +
+
+ +
+
+
+ +

Description

+ +

The BigInt package provides arbitrary-precision integer arithmetic in Swift. +It contains functionality comparable to that of the Java BigInteger class.

+ +

BigInt requires Swift 5.0.

+ +

Usage

+ +

Using Swift Package Manager, in your projects Package.swift file add a dependency like

+
dependencies: [
+// Dependencies declare other packages that this package depends on.
+.package(url: "https://github.com/leif-ibsen/BigInt", from: "1.0.0"),
+],
+
+ +

References

+ +

Algorithms from the following books have been used in the implementation. +There are references in the source code where appropriate.

+ +
    +
  • Donald E. Knuth: Seminumerical Algorithms. Addison-Wesley 1971
  • +
  • Crandall and Pomerance: Prime Numbers - A Computational Perspective. Second Edition, Springer 2005
  • +
  • Henry S. Warren, Jr: Hacker’s Delight. Second Edition, Addison-Wesley 2013
  • +
  • Menezes, Oorschot, Vanstone: Handbook of Applied Cryptography. CRC Press 1996
  • +
+ +

Acknowledgement

+ +

The BitSieve class used in the implementation is a translation to Swift of the corresponding class from Java BigInteger. +The GCD algorithm and the Karatsuba and ToomCook multiplication algorithms are modelled after the corresponding algorithms in Java BigInteger.

+ +
+
+ +
+
+ + + diff --git a/docs/docsets/.docset/Contents/Resources/Documents/js/jazzy.js b/docs/docsets/.docset/Contents/Resources/Documents/js/jazzy.js new file mode 100755 index 0000000..c31dc05 --- /dev/null +++ b/docs/docsets/.docset/Contents/Resources/Documents/js/jazzy.js @@ -0,0 +1,59 @@ +window.jazzy = {'docset': false} +if (typeof window.dash != 'undefined') { + document.documentElement.className += ' dash' + window.jazzy.docset = true +} +if (navigator.userAgent.match(/xcode/i)) { + document.documentElement.className += ' xcode' + window.jazzy.docset = true +} + +function toggleItem($link, $content) { + var animationDuration = 300; + $link.toggleClass('token-open'); + $content.slideToggle(animationDuration); +} + +function itemLinkToContent($link) { + return $link.parent().parent().next(); +} + +// On doc load + hash-change, open any targetted item +function openCurrentItemIfClosed() { + if (window.jazzy.docset) { + return; + } + var $link = $(`.token[href="${location.hash}"]`); + $content = itemLinkToContent($link); + if ($content.is(':hidden')) { + toggleItem($link, $content); + } +} + +$(openCurrentItemIfClosed); +$(window).on('hashchange', openCurrentItemIfClosed); + +// On item link ('token') click, toggle its discussion +$('.token').on('click', function(event) { + if (window.jazzy.docset) { + return; + } + var $link = $(this); + toggleItem($link, itemLinkToContent($link)); + + // Keeps the document from jumping to the hash. + var href = $link.attr('href'); + if (history.pushState) { + history.pushState({}, '', href); + } else { + location.hash = href; + } + event.preventDefault(); +}); + +// Clicks on links to the current, closed, item need to open the item +$("a:not('.token')").on('click', function() { + if (location == this.href) { + openCurrentItemIfClosed(); + } +}); diff --git a/docs/docsets/.docset/Contents/Resources/Documents/js/jquery.min.js b/docs/docsets/.docset/Contents/Resources/Documents/js/jquery.min.js new file mode 100644 index 0000000..4d9b3a2 --- /dev/null +++ b/docs/docsets/.docset/Contents/Resources/Documents/js/jquery.min.js @@ -0,0 +1,2 @@ +/*! jQuery v3.3.1 | (c) JS Foundation and other contributors | jquery.org/license */ +!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(e,t){"use strict";var n=[],r=e.document,i=Object.getPrototypeOf,o=n.slice,a=n.concat,s=n.push,u=n.indexOf,l={},c=l.toString,f=l.hasOwnProperty,p=f.toString,d=p.call(Object),h={},g=function e(t){return"function"==typeof t&&"number"!=typeof t.nodeType},y=function e(t){return null!=t&&t===t.window},v={type:!0,src:!0,noModule:!0};function m(e,t,n){var i,o=(t=t||r).createElement("script");if(o.text=e,n)for(i in v)n[i]&&(o[i]=n[i]);t.head.appendChild(o).parentNode.removeChild(o)}function x(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?l[c.call(e)]||"object":typeof e}var b="3.3.1",w=function(e,t){return new w.fn.init(e,t)},T=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;w.fn=w.prototype={jquery:"3.3.1",constructor:w,length:0,toArray:function(){return o.call(this)},get:function(e){return null==e?o.call(this):e<0?this[e+this.length]:this[e]},pushStack:function(e){var t=w.merge(this.constructor(),e);return t.prevObject=this,t},each:function(e){return w.each(this,e)},map:function(e){return this.pushStack(w.map(this,function(t,n){return e.call(t,n,t)}))},slice:function(){return this.pushStack(o.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(e){var t=this.length,n=+e+(e<0?t:0);return this.pushStack(n>=0&&n0&&t-1 in e)}var E=function(e){var t,n,r,i,o,a,s,u,l,c,f,p,d,h,g,y,v,m,x,b="sizzle"+1*new Date,w=e.document,T=0,C=0,E=ae(),k=ae(),S=ae(),D=function(e,t){return e===t&&(f=!0),0},N={}.hasOwnProperty,A=[],j=A.pop,q=A.push,L=A.push,H=A.slice,O=function(e,t){for(var n=0,r=e.length;n+~]|"+M+")"+M+"*"),z=new RegExp("="+M+"*([^\\]'\"]*?)"+M+"*\\]","g"),X=new RegExp(W),U=new RegExp("^"+R+"$"),V={ID:new RegExp("^#("+R+")"),CLASS:new RegExp("^\\.("+R+")"),TAG:new RegExp("^("+R+"|[*])"),ATTR:new RegExp("^"+I),PSEUDO:new RegExp("^"+W),CHILD:new RegExp("^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\("+M+"*(even|odd|(([+-]|)(\\d*)n|)"+M+"*(?:([+-]|)"+M+"*(\\d+)|))"+M+"*\\)|)","i"),bool:new RegExp("^(?:"+P+")$","i"),needsContext:new RegExp("^"+M+"*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("+M+"*((?:-\\d)?\\d*)"+M+"*\\)|)(?=[^-]|$)","i")},G=/^(?:input|select|textarea|button)$/i,Y=/^h\d$/i,Q=/^[^{]+\{\s*\[native \w/,J=/^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,K=/[+~]/,Z=new RegExp("\\\\([\\da-f]{1,6}"+M+"?|("+M+")|.)","ig"),ee=function(e,t,n){var r="0x"+t-65536;return r!==r||n?t:r<0?String.fromCharCode(r+65536):String.fromCharCode(r>>10|55296,1023&r|56320)},te=/([\0-\x1f\x7f]|^-?\d)|^-$|[^\0-\x1f\x7f-\uFFFF\w-]/g,ne=function(e,t){return t?"\0"===e?"\ufffd":e.slice(0,-1)+"\\"+e.charCodeAt(e.length-1).toString(16)+" ":"\\"+e},re=function(){p()},ie=me(function(e){return!0===e.disabled&&("form"in e||"label"in e)},{dir:"parentNode",next:"legend"});try{L.apply(A=H.call(w.childNodes),w.childNodes),A[w.childNodes.length].nodeType}catch(e){L={apply:A.length?function(e,t){q.apply(e,H.call(t))}:function(e,t){var n=e.length,r=0;while(e[n++]=t[r++]);e.length=n-1}}}function oe(e,t,r,i){var o,s,l,c,f,h,v,m=t&&t.ownerDocument,T=t?t.nodeType:9;if(r=r||[],"string"!=typeof e||!e||1!==T&&9!==T&&11!==T)return r;if(!i&&((t?t.ownerDocument||t:w)!==d&&p(t),t=t||d,g)){if(11!==T&&(f=J.exec(e)))if(o=f[1]){if(9===T){if(!(l=t.getElementById(o)))return r;if(l.id===o)return r.push(l),r}else if(m&&(l=m.getElementById(o))&&x(t,l)&&l.id===o)return r.push(l),r}else{if(f[2])return L.apply(r,t.getElementsByTagName(e)),r;if((o=f[3])&&n.getElementsByClassName&&t.getElementsByClassName)return L.apply(r,t.getElementsByClassName(o)),r}if(n.qsa&&!S[e+" "]&&(!y||!y.test(e))){if(1!==T)m=t,v=e;else if("object"!==t.nodeName.toLowerCase()){(c=t.getAttribute("id"))?c=c.replace(te,ne):t.setAttribute("id",c=b),s=(h=a(e)).length;while(s--)h[s]="#"+c+" "+ve(h[s]);v=h.join(","),m=K.test(e)&&ge(t.parentNode)||t}if(v)try{return L.apply(r,m.querySelectorAll(v)),r}catch(e){}finally{c===b&&t.removeAttribute("id")}}}return u(e.replace(B,"$1"),t,r,i)}function ae(){var e=[];function t(n,i){return e.push(n+" ")>r.cacheLength&&delete t[e.shift()],t[n+" "]=i}return t}function se(e){return e[b]=!0,e}function ue(e){var t=d.createElement("fieldset");try{return!!e(t)}catch(e){return!1}finally{t.parentNode&&t.parentNode.removeChild(t),t=null}}function le(e,t){var n=e.split("|"),i=n.length;while(i--)r.attrHandle[n[i]]=t}function ce(e,t){var n=t&&e,r=n&&1===e.nodeType&&1===t.nodeType&&e.sourceIndex-t.sourceIndex;if(r)return r;if(n)while(n=n.nextSibling)if(n===t)return-1;return e?1:-1}function fe(e){return function(t){return"input"===t.nodeName.toLowerCase()&&t.type===e}}function pe(e){return function(t){var n=t.nodeName.toLowerCase();return("input"===n||"button"===n)&&t.type===e}}function de(e){return function(t){return"form"in t?t.parentNode&&!1===t.disabled?"label"in t?"label"in t.parentNode?t.parentNode.disabled===e:t.disabled===e:t.isDisabled===e||t.isDisabled!==!e&&ie(t)===e:t.disabled===e:"label"in t&&t.disabled===e}}function he(e){return se(function(t){return t=+t,se(function(n,r){var i,o=e([],n.length,t),a=o.length;while(a--)n[i=o[a]]&&(n[i]=!(r[i]=n[i]))})})}function ge(e){return e&&"undefined"!=typeof e.getElementsByTagName&&e}n=oe.support={},o=oe.isXML=function(e){var t=e&&(e.ownerDocument||e).documentElement;return!!t&&"HTML"!==t.nodeName},p=oe.setDocument=function(e){var t,i,a=e?e.ownerDocument||e:w;return a!==d&&9===a.nodeType&&a.documentElement?(d=a,h=d.documentElement,g=!o(d),w!==d&&(i=d.defaultView)&&i.top!==i&&(i.addEventListener?i.addEventListener("unload",re,!1):i.attachEvent&&i.attachEvent("onunload",re)),n.attributes=ue(function(e){return e.className="i",!e.getAttribute("className")}),n.getElementsByTagName=ue(function(e){return e.appendChild(d.createComment("")),!e.getElementsByTagName("*").length}),n.getElementsByClassName=Q.test(d.getElementsByClassName),n.getById=ue(function(e){return h.appendChild(e).id=b,!d.getElementsByName||!d.getElementsByName(b).length}),n.getById?(r.filter.ID=function(e){var t=e.replace(Z,ee);return function(e){return e.getAttribute("id")===t}},r.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&g){var n=t.getElementById(e);return n?[n]:[]}}):(r.filter.ID=function(e){var t=e.replace(Z,ee);return function(e){var n="undefined"!=typeof e.getAttributeNode&&e.getAttributeNode("id");return n&&n.value===t}},r.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&g){var n,r,i,o=t.getElementById(e);if(o){if((n=o.getAttributeNode("id"))&&n.value===e)return[o];i=t.getElementsByName(e),r=0;while(o=i[r++])if((n=o.getAttributeNode("id"))&&n.value===e)return[o]}return[]}}),r.find.TAG=n.getElementsByTagName?function(e,t){return"undefined"!=typeof t.getElementsByTagName?t.getElementsByTagName(e):n.qsa?t.querySelectorAll(e):void 0}:function(e,t){var n,r=[],i=0,o=t.getElementsByTagName(e);if("*"===e){while(n=o[i++])1===n.nodeType&&r.push(n);return r}return o},r.find.CLASS=n.getElementsByClassName&&function(e,t){if("undefined"!=typeof t.getElementsByClassName&&g)return t.getElementsByClassName(e)},v=[],y=[],(n.qsa=Q.test(d.querySelectorAll))&&(ue(function(e){h.appendChild(e).innerHTML="",e.querySelectorAll("[msallowcapture^='']").length&&y.push("[*^$]="+M+"*(?:''|\"\")"),e.querySelectorAll("[selected]").length||y.push("\\["+M+"*(?:value|"+P+")"),e.querySelectorAll("[id~="+b+"-]").length||y.push("~="),e.querySelectorAll(":checked").length||y.push(":checked"),e.querySelectorAll("a#"+b+"+*").length||y.push(".#.+[+~]")}),ue(function(e){e.innerHTML="";var t=d.createElement("input");t.setAttribute("type","hidden"),e.appendChild(t).setAttribute("name","D"),e.querySelectorAll("[name=d]").length&&y.push("name"+M+"*[*^$|!~]?="),2!==e.querySelectorAll(":enabled").length&&y.push(":enabled",":disabled"),h.appendChild(e).disabled=!0,2!==e.querySelectorAll(":disabled").length&&y.push(":enabled",":disabled"),e.querySelectorAll("*,:x"),y.push(",.*:")})),(n.matchesSelector=Q.test(m=h.matches||h.webkitMatchesSelector||h.mozMatchesSelector||h.oMatchesSelector||h.msMatchesSelector))&&ue(function(e){n.disconnectedMatch=m.call(e,"*"),m.call(e,"[s!='']:x"),v.push("!=",W)}),y=y.length&&new RegExp(y.join("|")),v=v.length&&new RegExp(v.join("|")),t=Q.test(h.compareDocumentPosition),x=t||Q.test(h.contains)?function(e,t){var n=9===e.nodeType?e.documentElement:e,r=t&&t.parentNode;return e===r||!(!r||1!==r.nodeType||!(n.contains?n.contains(r):e.compareDocumentPosition&&16&e.compareDocumentPosition(r)))}:function(e,t){if(t)while(t=t.parentNode)if(t===e)return!0;return!1},D=t?function(e,t){if(e===t)return f=!0,0;var r=!e.compareDocumentPosition-!t.compareDocumentPosition;return r||(1&(r=(e.ownerDocument||e)===(t.ownerDocument||t)?e.compareDocumentPosition(t):1)||!n.sortDetached&&t.compareDocumentPosition(e)===r?e===d||e.ownerDocument===w&&x(w,e)?-1:t===d||t.ownerDocument===w&&x(w,t)?1:c?O(c,e)-O(c,t):0:4&r?-1:1)}:function(e,t){if(e===t)return f=!0,0;var n,r=0,i=e.parentNode,o=t.parentNode,a=[e],s=[t];if(!i||!o)return e===d?-1:t===d?1:i?-1:o?1:c?O(c,e)-O(c,t):0;if(i===o)return ce(e,t);n=e;while(n=n.parentNode)a.unshift(n);n=t;while(n=n.parentNode)s.unshift(n);while(a[r]===s[r])r++;return r?ce(a[r],s[r]):a[r]===w?-1:s[r]===w?1:0},d):d},oe.matches=function(e,t){return oe(e,null,null,t)},oe.matchesSelector=function(e,t){if((e.ownerDocument||e)!==d&&p(e),t=t.replace(z,"='$1']"),n.matchesSelector&&g&&!S[t+" "]&&(!v||!v.test(t))&&(!y||!y.test(t)))try{var r=m.call(e,t);if(r||n.disconnectedMatch||e.document&&11!==e.document.nodeType)return r}catch(e){}return oe(t,d,null,[e]).length>0},oe.contains=function(e,t){return(e.ownerDocument||e)!==d&&p(e),x(e,t)},oe.attr=function(e,t){(e.ownerDocument||e)!==d&&p(e);var i=r.attrHandle[t.toLowerCase()],o=i&&N.call(r.attrHandle,t.toLowerCase())?i(e,t,!g):void 0;return void 0!==o?o:n.attributes||!g?e.getAttribute(t):(o=e.getAttributeNode(t))&&o.specified?o.value:null},oe.escape=function(e){return(e+"").replace(te,ne)},oe.error=function(e){throw new Error("Syntax error, unrecognized expression: "+e)},oe.uniqueSort=function(e){var t,r=[],i=0,o=0;if(f=!n.detectDuplicates,c=!n.sortStable&&e.slice(0),e.sort(D),f){while(t=e[o++])t===e[o]&&(i=r.push(o));while(i--)e.splice(r[i],1)}return c=null,e},i=oe.getText=function(e){var t,n="",r=0,o=e.nodeType;if(o){if(1===o||9===o||11===o){if("string"==typeof e.textContent)return e.textContent;for(e=e.firstChild;e;e=e.nextSibling)n+=i(e)}else if(3===o||4===o)return e.nodeValue}else while(t=e[r++])n+=i(t);return n},(r=oe.selectors={cacheLength:50,createPseudo:se,match:V,attrHandle:{},find:{},relative:{">":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(e){return e[1]=e[1].replace(Z,ee),e[3]=(e[3]||e[4]||e[5]||"").replace(Z,ee),"~="===e[2]&&(e[3]=" "+e[3]+" "),e.slice(0,4)},CHILD:function(e){return e[1]=e[1].toLowerCase(),"nth"===e[1].slice(0,3)?(e[3]||oe.error(e[0]),e[4]=+(e[4]?e[5]+(e[6]||1):2*("even"===e[3]||"odd"===e[3])),e[5]=+(e[7]+e[8]||"odd"===e[3])):e[3]&&oe.error(e[0]),e},PSEUDO:function(e){var t,n=!e[6]&&e[2];return V.CHILD.test(e[0])?null:(e[3]?e[2]=e[4]||e[5]||"":n&&X.test(n)&&(t=a(n,!0))&&(t=n.indexOf(")",n.length-t)-n.length)&&(e[0]=e[0].slice(0,t),e[2]=n.slice(0,t)),e.slice(0,3))}},filter:{TAG:function(e){var t=e.replace(Z,ee).toLowerCase();return"*"===e?function(){return!0}:function(e){return e.nodeName&&e.nodeName.toLowerCase()===t}},CLASS:function(e){var t=E[e+" "];return t||(t=new RegExp("(^|"+M+")"+e+"("+M+"|$)"))&&E(e,function(e){return t.test("string"==typeof e.className&&e.className||"undefined"!=typeof e.getAttribute&&e.getAttribute("class")||"")})},ATTR:function(e,t,n){return function(r){var i=oe.attr(r,e);return null==i?"!="===t:!t||(i+="","="===t?i===n:"!="===t?i!==n:"^="===t?n&&0===i.indexOf(n):"*="===t?n&&i.indexOf(n)>-1:"$="===t?n&&i.slice(-n.length)===n:"~="===t?(" "+i.replace($," ")+" ").indexOf(n)>-1:"|="===t&&(i===n||i.slice(0,n.length+1)===n+"-"))}},CHILD:function(e,t,n,r,i){var o="nth"!==e.slice(0,3),a="last"!==e.slice(-4),s="of-type"===t;return 1===r&&0===i?function(e){return!!e.parentNode}:function(t,n,u){var l,c,f,p,d,h,g=o!==a?"nextSibling":"previousSibling",y=t.parentNode,v=s&&t.nodeName.toLowerCase(),m=!u&&!s,x=!1;if(y){if(o){while(g){p=t;while(p=p[g])if(s?p.nodeName.toLowerCase()===v:1===p.nodeType)return!1;h=g="only"===e&&!h&&"nextSibling"}return!0}if(h=[a?y.firstChild:y.lastChild],a&&m){x=(d=(l=(c=(f=(p=y)[b]||(p[b]={}))[p.uniqueID]||(f[p.uniqueID]={}))[e]||[])[0]===T&&l[1])&&l[2],p=d&&y.childNodes[d];while(p=++d&&p&&p[g]||(x=d=0)||h.pop())if(1===p.nodeType&&++x&&p===t){c[e]=[T,d,x];break}}else if(m&&(x=d=(l=(c=(f=(p=t)[b]||(p[b]={}))[p.uniqueID]||(f[p.uniqueID]={}))[e]||[])[0]===T&&l[1]),!1===x)while(p=++d&&p&&p[g]||(x=d=0)||h.pop())if((s?p.nodeName.toLowerCase()===v:1===p.nodeType)&&++x&&(m&&((c=(f=p[b]||(p[b]={}))[p.uniqueID]||(f[p.uniqueID]={}))[e]=[T,x]),p===t))break;return(x-=i)===r||x%r==0&&x/r>=0}}},PSEUDO:function(e,t){var n,i=r.pseudos[e]||r.setFilters[e.toLowerCase()]||oe.error("unsupported pseudo: "+e);return i[b]?i(t):i.length>1?(n=[e,e,"",t],r.setFilters.hasOwnProperty(e.toLowerCase())?se(function(e,n){var r,o=i(e,t),a=o.length;while(a--)e[r=O(e,o[a])]=!(n[r]=o[a])}):function(e){return i(e,0,n)}):i}},pseudos:{not:se(function(e){var t=[],n=[],r=s(e.replace(B,"$1"));return r[b]?se(function(e,t,n,i){var o,a=r(e,null,i,[]),s=e.length;while(s--)(o=a[s])&&(e[s]=!(t[s]=o))}):function(e,i,o){return t[0]=e,r(t,null,o,n),t[0]=null,!n.pop()}}),has:se(function(e){return function(t){return oe(e,t).length>0}}),contains:se(function(e){return e=e.replace(Z,ee),function(t){return(t.textContent||t.innerText||i(t)).indexOf(e)>-1}}),lang:se(function(e){return U.test(e||"")||oe.error("unsupported lang: "+e),e=e.replace(Z,ee).toLowerCase(),function(t){var n;do{if(n=g?t.lang:t.getAttribute("xml:lang")||t.getAttribute("lang"))return(n=n.toLowerCase())===e||0===n.indexOf(e+"-")}while((t=t.parentNode)&&1===t.nodeType);return!1}}),target:function(t){var n=e.location&&e.location.hash;return n&&n.slice(1)===t.id},root:function(e){return e===h},focus:function(e){return e===d.activeElement&&(!d.hasFocus||d.hasFocus())&&!!(e.type||e.href||~e.tabIndex)},enabled:de(!1),disabled:de(!0),checked:function(e){var t=e.nodeName.toLowerCase();return"input"===t&&!!e.checked||"option"===t&&!!e.selected},selected:function(e){return e.parentNode&&e.parentNode.selectedIndex,!0===e.selected},empty:function(e){for(e=e.firstChild;e;e=e.nextSibling)if(e.nodeType<6)return!1;return!0},parent:function(e){return!r.pseudos.empty(e)},header:function(e){return Y.test(e.nodeName)},input:function(e){return G.test(e.nodeName)},button:function(e){var t=e.nodeName.toLowerCase();return"input"===t&&"button"===e.type||"button"===t},text:function(e){var t;return"input"===e.nodeName.toLowerCase()&&"text"===e.type&&(null==(t=e.getAttribute("type"))||"text"===t.toLowerCase())},first:he(function(){return[0]}),last:he(function(e,t){return[t-1]}),eq:he(function(e,t,n){return[n<0?n+t:n]}),even:he(function(e,t){for(var n=0;n=0;)e.push(r);return e}),gt:he(function(e,t,n){for(var r=n<0?n+t:n;++r1?function(t,n,r){var i=e.length;while(i--)if(!e[i](t,n,r))return!1;return!0}:e[0]}function be(e,t,n){for(var r=0,i=t.length;r-1&&(o[l]=!(a[l]=f))}}else v=we(v===a?v.splice(h,v.length):v),i?i(null,a,v,u):L.apply(a,v)})}function Ce(e){for(var t,n,i,o=e.length,a=r.relative[e[0].type],s=a||r.relative[" "],u=a?1:0,c=me(function(e){return e===t},s,!0),f=me(function(e){return O(t,e)>-1},s,!0),p=[function(e,n,r){var i=!a&&(r||n!==l)||((t=n).nodeType?c(e,n,r):f(e,n,r));return t=null,i}];u1&&xe(p),u>1&&ve(e.slice(0,u-1).concat({value:" "===e[u-2].type?"*":""})).replace(B,"$1"),n,u0,i=e.length>0,o=function(o,a,s,u,c){var f,h,y,v=0,m="0",x=o&&[],b=[],w=l,C=o||i&&r.find.TAG("*",c),E=T+=null==w?1:Math.random()||.1,k=C.length;for(c&&(l=a===d||a||c);m!==k&&null!=(f=C[m]);m++){if(i&&f){h=0,a||f.ownerDocument===d||(p(f),s=!g);while(y=e[h++])if(y(f,a||d,s)){u.push(f);break}c&&(T=E)}n&&((f=!y&&f)&&v--,o&&x.push(f))}if(v+=m,n&&m!==v){h=0;while(y=t[h++])y(x,b,a,s);if(o){if(v>0)while(m--)x[m]||b[m]||(b[m]=j.call(u));b=we(b)}L.apply(u,b),c&&!o&&b.length>0&&v+t.length>1&&oe.uniqueSort(u)}return c&&(T=E,l=w),x};return n?se(o):o}return s=oe.compile=function(e,t){var n,r=[],i=[],o=S[e+" "];if(!o){t||(t=a(e)),n=t.length;while(n--)(o=Ce(t[n]))[b]?r.push(o):i.push(o);(o=S(e,Ee(i,r))).selector=e}return o},u=oe.select=function(e,t,n,i){var o,u,l,c,f,p="function"==typeof e&&e,d=!i&&a(e=p.selector||e);if(n=n||[],1===d.length){if((u=d[0]=d[0].slice(0)).length>2&&"ID"===(l=u[0]).type&&9===t.nodeType&&g&&r.relative[u[1].type]){if(!(t=(r.find.ID(l.matches[0].replace(Z,ee),t)||[])[0]))return n;p&&(t=t.parentNode),e=e.slice(u.shift().value.length)}o=V.needsContext.test(e)?0:u.length;while(o--){if(l=u[o],r.relative[c=l.type])break;if((f=r.find[c])&&(i=f(l.matches[0].replace(Z,ee),K.test(u[0].type)&&ge(t.parentNode)||t))){if(u.splice(o,1),!(e=i.length&&ve(u)))return L.apply(n,i),n;break}}}return(p||s(e,d))(i,t,!g,n,!t||K.test(e)&&ge(t.parentNode)||t),n},n.sortStable=b.split("").sort(D).join("")===b,n.detectDuplicates=!!f,p(),n.sortDetached=ue(function(e){return 1&e.compareDocumentPosition(d.createElement("fieldset"))}),ue(function(e){return e.innerHTML="","#"===e.firstChild.getAttribute("href")})||le("type|href|height|width",function(e,t,n){if(!n)return e.getAttribute(t,"type"===t.toLowerCase()?1:2)}),n.attributes&&ue(function(e){return e.innerHTML="",e.firstChild.setAttribute("value",""),""===e.firstChild.getAttribute("value")})||le("value",function(e,t,n){if(!n&&"input"===e.nodeName.toLowerCase())return e.defaultValue}),ue(function(e){return null==e.getAttribute("disabled")})||le(P,function(e,t,n){var r;if(!n)return!0===e[t]?t.toLowerCase():(r=e.getAttributeNode(t))&&r.specified?r.value:null}),oe}(e);w.find=E,w.expr=E.selectors,w.expr[":"]=w.expr.pseudos,w.uniqueSort=w.unique=E.uniqueSort,w.text=E.getText,w.isXMLDoc=E.isXML,w.contains=E.contains,w.escapeSelector=E.escape;var k=function(e,t,n){var r=[],i=void 0!==n;while((e=e[t])&&9!==e.nodeType)if(1===e.nodeType){if(i&&w(e).is(n))break;r.push(e)}return r},S=function(e,t){for(var n=[];e;e=e.nextSibling)1===e.nodeType&&e!==t&&n.push(e);return n},D=w.expr.match.needsContext;function N(e,t){return e.nodeName&&e.nodeName.toLowerCase()===t.toLowerCase()}var A=/^<([a-z][^\/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i;function j(e,t,n){return g(t)?w.grep(e,function(e,r){return!!t.call(e,r,e)!==n}):t.nodeType?w.grep(e,function(e){return e===t!==n}):"string"!=typeof t?w.grep(e,function(e){return u.call(t,e)>-1!==n}):w.filter(t,e,n)}w.filter=function(e,t,n){var r=t[0];return n&&(e=":not("+e+")"),1===t.length&&1===r.nodeType?w.find.matchesSelector(r,e)?[r]:[]:w.find.matches(e,w.grep(t,function(e){return 1===e.nodeType}))},w.fn.extend({find:function(e){var t,n,r=this.length,i=this;if("string"!=typeof e)return this.pushStack(w(e).filter(function(){for(t=0;t1?w.uniqueSort(n):n},filter:function(e){return this.pushStack(j(this,e||[],!1))},not:function(e){return this.pushStack(j(this,e||[],!0))},is:function(e){return!!j(this,"string"==typeof e&&D.test(e)?w(e):e||[],!1).length}});var q,L=/^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/;(w.fn.init=function(e,t,n){var i,o;if(!e)return this;if(n=n||q,"string"==typeof e){if(!(i="<"===e[0]&&">"===e[e.length-1]&&e.length>=3?[null,e,null]:L.exec(e))||!i[1]&&t)return!t||t.jquery?(t||n).find(e):this.constructor(t).find(e);if(i[1]){if(t=t instanceof w?t[0]:t,w.merge(this,w.parseHTML(i[1],t&&t.nodeType?t.ownerDocument||t:r,!0)),A.test(i[1])&&w.isPlainObject(t))for(i in t)g(this[i])?this[i](t[i]):this.attr(i,t[i]);return this}return(o=r.getElementById(i[2]))&&(this[0]=o,this.length=1),this}return e.nodeType?(this[0]=e,this.length=1,this):g(e)?void 0!==n.ready?n.ready(e):e(w):w.makeArray(e,this)}).prototype=w.fn,q=w(r);var H=/^(?:parents|prev(?:Until|All))/,O={children:!0,contents:!0,next:!0,prev:!0};w.fn.extend({has:function(e){var t=w(e,this),n=t.length;return this.filter(function(){for(var e=0;e-1:1===n.nodeType&&w.find.matchesSelector(n,e))){o.push(n);break}return this.pushStack(o.length>1?w.uniqueSort(o):o)},index:function(e){return e?"string"==typeof e?u.call(w(e),this[0]):u.call(this,e.jquery?e[0]:e):this[0]&&this[0].parentNode?this.first().prevAll().length:-1},add:function(e,t){return this.pushStack(w.uniqueSort(w.merge(this.get(),w(e,t))))},addBack:function(e){return this.add(null==e?this.prevObject:this.prevObject.filter(e))}});function P(e,t){while((e=e[t])&&1!==e.nodeType);return e}w.each({parent:function(e){var t=e.parentNode;return t&&11!==t.nodeType?t:null},parents:function(e){return k(e,"parentNode")},parentsUntil:function(e,t,n){return k(e,"parentNode",n)},next:function(e){return P(e,"nextSibling")},prev:function(e){return P(e,"previousSibling")},nextAll:function(e){return k(e,"nextSibling")},prevAll:function(e){return k(e,"previousSibling")},nextUntil:function(e,t,n){return k(e,"nextSibling",n)},prevUntil:function(e,t,n){return k(e,"previousSibling",n)},siblings:function(e){return S((e.parentNode||{}).firstChild,e)},children:function(e){return S(e.firstChild)},contents:function(e){return N(e,"iframe")?e.contentDocument:(N(e,"template")&&(e=e.content||e),w.merge([],e.childNodes))}},function(e,t){w.fn[e]=function(n,r){var i=w.map(this,t,n);return"Until"!==e.slice(-5)&&(r=n),r&&"string"==typeof r&&(i=w.filter(r,i)),this.length>1&&(O[e]||w.uniqueSort(i),H.test(e)&&i.reverse()),this.pushStack(i)}});var M=/[^\x20\t\r\n\f]+/g;function R(e){var t={};return w.each(e.match(M)||[],function(e,n){t[n]=!0}),t}w.Callbacks=function(e){e="string"==typeof e?R(e):w.extend({},e);var t,n,r,i,o=[],a=[],s=-1,u=function(){for(i=i||e.once,r=t=!0;a.length;s=-1){n=a.shift();while(++s-1)o.splice(n,1),n<=s&&s--}),this},has:function(e){return e?w.inArray(e,o)>-1:o.length>0},empty:function(){return o&&(o=[]),this},disable:function(){return i=a=[],o=n="",this},disabled:function(){return!o},lock:function(){return i=a=[],n||t||(o=n=""),this},locked:function(){return!!i},fireWith:function(e,n){return i||(n=[e,(n=n||[]).slice?n.slice():n],a.push(n),t||u()),this},fire:function(){return l.fireWith(this,arguments),this},fired:function(){return!!r}};return l};function I(e){return e}function W(e){throw e}function $(e,t,n,r){var i;try{e&&g(i=e.promise)?i.call(e).done(t).fail(n):e&&g(i=e.then)?i.call(e,t,n):t.apply(void 0,[e].slice(r))}catch(e){n.apply(void 0,[e])}}w.extend({Deferred:function(t){var n=[["notify","progress",w.Callbacks("memory"),w.Callbacks("memory"),2],["resolve","done",w.Callbacks("once memory"),w.Callbacks("once memory"),0,"resolved"],["reject","fail",w.Callbacks("once memory"),w.Callbacks("once memory"),1,"rejected"]],r="pending",i={state:function(){return r},always:function(){return o.done(arguments).fail(arguments),this},"catch":function(e){return i.then(null,e)},pipe:function(){var e=arguments;return w.Deferred(function(t){w.each(n,function(n,r){var i=g(e[r[4]])&&e[r[4]];o[r[1]](function(){var e=i&&i.apply(this,arguments);e&&g(e.promise)?e.promise().progress(t.notify).done(t.resolve).fail(t.reject):t[r[0]+"With"](this,i?[e]:arguments)})}),e=null}).promise()},then:function(t,r,i){var o=0;function a(t,n,r,i){return function(){var s=this,u=arguments,l=function(){var e,l;if(!(t=o&&(r!==W&&(s=void 0,u=[e]),n.rejectWith(s,u))}};t?c():(w.Deferred.getStackHook&&(c.stackTrace=w.Deferred.getStackHook()),e.setTimeout(c))}}return w.Deferred(function(e){n[0][3].add(a(0,e,g(i)?i:I,e.notifyWith)),n[1][3].add(a(0,e,g(t)?t:I)),n[2][3].add(a(0,e,g(r)?r:W))}).promise()},promise:function(e){return null!=e?w.extend(e,i):i}},o={};return w.each(n,function(e,t){var a=t[2],s=t[5];i[t[1]]=a.add,s&&a.add(function(){r=s},n[3-e][2].disable,n[3-e][3].disable,n[0][2].lock,n[0][3].lock),a.add(t[3].fire),o[t[0]]=function(){return o[t[0]+"With"](this===o?void 0:this,arguments),this},o[t[0]+"With"]=a.fireWith}),i.promise(o),t&&t.call(o,o),o},when:function(e){var t=arguments.length,n=t,r=Array(n),i=o.call(arguments),a=w.Deferred(),s=function(e){return function(n){r[e]=this,i[e]=arguments.length>1?o.call(arguments):n,--t||a.resolveWith(r,i)}};if(t<=1&&($(e,a.done(s(n)).resolve,a.reject,!t),"pending"===a.state()||g(i[n]&&i[n].then)))return a.then();while(n--)$(i[n],s(n),a.reject);return a.promise()}});var B=/^(Eval|Internal|Range|Reference|Syntax|Type|URI)Error$/;w.Deferred.exceptionHook=function(t,n){e.console&&e.console.warn&&t&&B.test(t.name)&&e.console.warn("jQuery.Deferred exception: "+t.message,t.stack,n)},w.readyException=function(t){e.setTimeout(function(){throw t})};var F=w.Deferred();w.fn.ready=function(e){return F.then(e)["catch"](function(e){w.readyException(e)}),this},w.extend({isReady:!1,readyWait:1,ready:function(e){(!0===e?--w.readyWait:w.isReady)||(w.isReady=!0,!0!==e&&--w.readyWait>0||F.resolveWith(r,[w]))}}),w.ready.then=F.then;function _(){r.removeEventListener("DOMContentLoaded",_),e.removeEventListener("load",_),w.ready()}"complete"===r.readyState||"loading"!==r.readyState&&!r.documentElement.doScroll?e.setTimeout(w.ready):(r.addEventListener("DOMContentLoaded",_),e.addEventListener("load",_));var z=function(e,t,n,r,i,o,a){var s=0,u=e.length,l=null==n;if("object"===x(n)){i=!0;for(s in n)z(e,t,s,n[s],!0,o,a)}else if(void 0!==r&&(i=!0,g(r)||(a=!0),l&&(a?(t.call(e,r),t=null):(l=t,t=function(e,t,n){return l.call(w(e),n)})),t))for(;s1,null,!0)},removeData:function(e){return this.each(function(){K.remove(this,e)})}}),w.extend({queue:function(e,t,n){var r;if(e)return t=(t||"fx")+"queue",r=J.get(e,t),n&&(!r||Array.isArray(n)?r=J.access(e,t,w.makeArray(n)):r.push(n)),r||[]},dequeue:function(e,t){t=t||"fx";var n=w.queue(e,t),r=n.length,i=n.shift(),o=w._queueHooks(e,t),a=function(){w.dequeue(e,t)};"inprogress"===i&&(i=n.shift(),r--),i&&("fx"===t&&n.unshift("inprogress"),delete o.stop,i.call(e,a,o)),!r&&o&&o.empty.fire()},_queueHooks:function(e,t){var n=t+"queueHooks";return J.get(e,n)||J.access(e,n,{empty:w.Callbacks("once memory").add(function(){J.remove(e,[t+"queue",n])})})}}),w.fn.extend({queue:function(e,t){var n=2;return"string"!=typeof e&&(t=e,e="fx",n--),arguments.length\x20\t\r\n\f]+)/i,he=/^$|^module$|\/(?:java|ecma)script/i,ge={option:[1,""],thead:[1,"","
"],col:[2,"","
"],tr:[2,"","
"],td:[3,"","
"],_default:[0,"",""]};ge.optgroup=ge.option,ge.tbody=ge.tfoot=ge.colgroup=ge.caption=ge.thead,ge.th=ge.td;function ye(e,t){var n;return n="undefined"!=typeof e.getElementsByTagName?e.getElementsByTagName(t||"*"):"undefined"!=typeof e.querySelectorAll?e.querySelectorAll(t||"*"):[],void 0===t||t&&N(e,t)?w.merge([e],n):n}function ve(e,t){for(var n=0,r=e.length;n-1)i&&i.push(o);else if(l=w.contains(o.ownerDocument,o),a=ye(f.appendChild(o),"script"),l&&ve(a),n){c=0;while(o=a[c++])he.test(o.type||"")&&n.push(o)}return f}!function(){var e=r.createDocumentFragment().appendChild(r.createElement("div")),t=r.createElement("input");t.setAttribute("type","radio"),t.setAttribute("checked","checked"),t.setAttribute("name","t"),e.appendChild(t),h.checkClone=e.cloneNode(!0).cloneNode(!0).lastChild.checked,e.innerHTML="",h.noCloneChecked=!!e.cloneNode(!0).lastChild.defaultValue}();var be=r.documentElement,we=/^key/,Te=/^(?:mouse|pointer|contextmenu|drag|drop)|click/,Ce=/^([^.]*)(?:\.(.+)|)/;function Ee(){return!0}function ke(){return!1}function Se(){try{return r.activeElement}catch(e){}}function De(e,t,n,r,i,o){var a,s;if("object"==typeof t){"string"!=typeof n&&(r=r||n,n=void 0);for(s in t)De(e,s,n,r,t[s],o);return e}if(null==r&&null==i?(i=n,r=n=void 0):null==i&&("string"==typeof n?(i=r,r=void 0):(i=r,r=n,n=void 0)),!1===i)i=ke;else if(!i)return e;return 1===o&&(a=i,(i=function(e){return w().off(e),a.apply(this,arguments)}).guid=a.guid||(a.guid=w.guid++)),e.each(function(){w.event.add(this,t,i,r,n)})}w.event={global:{},add:function(e,t,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,y=J.get(e);if(y){n.handler&&(n=(o=n).handler,i=o.selector),i&&w.find.matchesSelector(be,i),n.guid||(n.guid=w.guid++),(u=y.events)||(u=y.events={}),(a=y.handle)||(a=y.handle=function(t){return"undefined"!=typeof w&&w.event.triggered!==t.type?w.event.dispatch.apply(e,arguments):void 0}),l=(t=(t||"").match(M)||[""]).length;while(l--)d=g=(s=Ce.exec(t[l])||[])[1],h=(s[2]||"").split(".").sort(),d&&(f=w.event.special[d]||{},d=(i?f.delegateType:f.bindType)||d,f=w.event.special[d]||{},c=w.extend({type:d,origType:g,data:r,handler:n,guid:n.guid,selector:i,needsContext:i&&w.expr.match.needsContext.test(i),namespace:h.join(".")},o),(p=u[d])||((p=u[d]=[]).delegateCount=0,f.setup&&!1!==f.setup.call(e,r,h,a)||e.addEventListener&&e.addEventListener(d,a)),f.add&&(f.add.call(e,c),c.handler.guid||(c.handler.guid=n.guid)),i?p.splice(p.delegateCount++,0,c):p.push(c),w.event.global[d]=!0)}},remove:function(e,t,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,y=J.hasData(e)&&J.get(e);if(y&&(u=y.events)){l=(t=(t||"").match(M)||[""]).length;while(l--)if(s=Ce.exec(t[l])||[],d=g=s[1],h=(s[2]||"").split(".").sort(),d){f=w.event.special[d]||{},p=u[d=(r?f.delegateType:f.bindType)||d]||[],s=s[2]&&new RegExp("(^|\\.)"+h.join("\\.(?:.*\\.|)")+"(\\.|$)"),a=o=p.length;while(o--)c=p[o],!i&&g!==c.origType||n&&n.guid!==c.guid||s&&!s.test(c.namespace)||r&&r!==c.selector&&("**"!==r||!c.selector)||(p.splice(o,1),c.selector&&p.delegateCount--,f.remove&&f.remove.call(e,c));a&&!p.length&&(f.teardown&&!1!==f.teardown.call(e,h,y.handle)||w.removeEvent(e,d,y.handle),delete u[d])}else for(d in u)w.event.remove(e,d+t[l],n,r,!0);w.isEmptyObject(u)&&J.remove(e,"handle events")}},dispatch:function(e){var t=w.event.fix(e),n,r,i,o,a,s,u=new Array(arguments.length),l=(J.get(this,"events")||{})[t.type]||[],c=w.event.special[t.type]||{};for(u[0]=t,n=1;n=1))for(;l!==this;l=l.parentNode||this)if(1===l.nodeType&&("click"!==e.type||!0!==l.disabled)){for(o=[],a={},n=0;n-1:w.find(i,this,null,[l]).length),a[i]&&o.push(r);o.length&&s.push({elem:l,handlers:o})}return l=this,u\x20\t\r\n\f]*)[^>]*)\/>/gi,Ae=/\s*$/g;function Le(e,t){return N(e,"table")&&N(11!==t.nodeType?t:t.firstChild,"tr")?w(e).children("tbody")[0]||e:e}function He(e){return e.type=(null!==e.getAttribute("type"))+"/"+e.type,e}function Oe(e){return"true/"===(e.type||"").slice(0,5)?e.type=e.type.slice(5):e.removeAttribute("type"),e}function Pe(e,t){var n,r,i,o,a,s,u,l;if(1===t.nodeType){if(J.hasData(e)&&(o=J.access(e),a=J.set(t,o),l=o.events)){delete a.handle,a.events={};for(i in l)for(n=0,r=l[i].length;n1&&"string"==typeof y&&!h.checkClone&&je.test(y))return e.each(function(i){var o=e.eq(i);v&&(t[0]=y.call(this,i,o.html())),Re(o,t,n,r)});if(p&&(i=xe(t,e[0].ownerDocument,!1,e,r),o=i.firstChild,1===i.childNodes.length&&(i=o),o||r)){for(u=(s=w.map(ye(i,"script"),He)).length;f")},clone:function(e,t,n){var r,i,o,a,s=e.cloneNode(!0),u=w.contains(e.ownerDocument,e);if(!(h.noCloneChecked||1!==e.nodeType&&11!==e.nodeType||w.isXMLDoc(e)))for(a=ye(s),r=0,i=(o=ye(e)).length;r0&&ve(a,!u&&ye(e,"script")),s},cleanData:function(e){for(var t,n,r,i=w.event.special,o=0;void 0!==(n=e[o]);o++)if(Y(n)){if(t=n[J.expando]){if(t.events)for(r in t.events)i[r]?w.event.remove(n,r):w.removeEvent(n,r,t.handle);n[J.expando]=void 0}n[K.expando]&&(n[K.expando]=void 0)}}}),w.fn.extend({detach:function(e){return Ie(this,e,!0)},remove:function(e){return Ie(this,e)},text:function(e){return z(this,function(e){return void 0===e?w.text(this):this.empty().each(function(){1!==this.nodeType&&11!==this.nodeType&&9!==this.nodeType||(this.textContent=e)})},null,e,arguments.length)},append:function(){return Re(this,arguments,function(e){1!==this.nodeType&&11!==this.nodeType&&9!==this.nodeType||Le(this,e).appendChild(e)})},prepend:function(){return Re(this,arguments,function(e){if(1===this.nodeType||11===this.nodeType||9===this.nodeType){var t=Le(this,e);t.insertBefore(e,t.firstChild)}})},before:function(){return Re(this,arguments,function(e){this.parentNode&&this.parentNode.insertBefore(e,this)})},after:function(){return Re(this,arguments,function(e){this.parentNode&&this.parentNode.insertBefore(e,this.nextSibling)})},empty:function(){for(var e,t=0;null!=(e=this[t]);t++)1===e.nodeType&&(w.cleanData(ye(e,!1)),e.textContent="");return this},clone:function(e,t){return e=null!=e&&e,t=null==t?e:t,this.map(function(){return w.clone(this,e,t)})},html:function(e){return z(this,function(e){var t=this[0]||{},n=0,r=this.length;if(void 0===e&&1===t.nodeType)return t.innerHTML;if("string"==typeof e&&!Ae.test(e)&&!ge[(de.exec(e)||["",""])[1].toLowerCase()]){e=w.htmlPrefilter(e);try{for(;n=0&&(u+=Math.max(0,Math.ceil(e["offset"+t[0].toUpperCase()+t.slice(1)]-o-u-s-.5))),u}function et(e,t,n){var r=$e(e),i=Fe(e,t,r),o="border-box"===w.css(e,"boxSizing",!1,r),a=o;if(We.test(i)){if(!n)return i;i="auto"}return a=a&&(h.boxSizingReliable()||i===e.style[t]),("auto"===i||!parseFloat(i)&&"inline"===w.css(e,"display",!1,r))&&(i=e["offset"+t[0].toUpperCase()+t.slice(1)],a=!0),(i=parseFloat(i)||0)+Ze(e,t,n||(o?"border":"content"),a,r,i)+"px"}w.extend({cssHooks:{opacity:{get:function(e,t){if(t){var n=Fe(e,"opacity");return""===n?"1":n}}}},cssNumber:{animationIterationCount:!0,columnCount:!0,fillOpacity:!0,flexGrow:!0,flexShrink:!0,fontWeight:!0,lineHeight:!0,opacity:!0,order:!0,orphans:!0,widows:!0,zIndex:!0,zoom:!0},cssProps:{},style:function(e,t,n,r){if(e&&3!==e.nodeType&&8!==e.nodeType&&e.style){var i,o,a,s=G(t),u=Xe.test(t),l=e.style;if(u||(t=Je(s)),a=w.cssHooks[t]||w.cssHooks[s],void 0===n)return a&&"get"in a&&void 0!==(i=a.get(e,!1,r))?i:l[t];"string"==(o=typeof n)&&(i=ie.exec(n))&&i[1]&&(n=ue(e,t,i),o="number"),null!=n&&n===n&&("number"===o&&(n+=i&&i[3]||(w.cssNumber[s]?"":"px")),h.clearCloneStyle||""!==n||0!==t.indexOf("background")||(l[t]="inherit"),a&&"set"in a&&void 0===(n=a.set(e,n,r))||(u?l.setProperty(t,n):l[t]=n))}},css:function(e,t,n,r){var i,o,a,s=G(t);return Xe.test(t)||(t=Je(s)),(a=w.cssHooks[t]||w.cssHooks[s])&&"get"in a&&(i=a.get(e,!0,n)),void 0===i&&(i=Fe(e,t,r)),"normal"===i&&t in Ve&&(i=Ve[t]),""===n||n?(o=parseFloat(i),!0===n||isFinite(o)?o||0:i):i}}),w.each(["height","width"],function(e,t){w.cssHooks[t]={get:function(e,n,r){if(n)return!ze.test(w.css(e,"display"))||e.getClientRects().length&&e.getBoundingClientRect().width?et(e,t,r):se(e,Ue,function(){return et(e,t,r)})},set:function(e,n,r){var i,o=$e(e),a="border-box"===w.css(e,"boxSizing",!1,o),s=r&&Ze(e,t,r,a,o);return a&&h.scrollboxSize()===o.position&&(s-=Math.ceil(e["offset"+t[0].toUpperCase()+t.slice(1)]-parseFloat(o[t])-Ze(e,t,"border",!1,o)-.5)),s&&(i=ie.exec(n))&&"px"!==(i[3]||"px")&&(e.style[t]=n,n=w.css(e,t)),Ke(e,n,s)}}}),w.cssHooks.marginLeft=_e(h.reliableMarginLeft,function(e,t){if(t)return(parseFloat(Fe(e,"marginLeft"))||e.getBoundingClientRect().left-se(e,{marginLeft:0},function(){return e.getBoundingClientRect().left}))+"px"}),w.each({margin:"",padding:"",border:"Width"},function(e,t){w.cssHooks[e+t]={expand:function(n){for(var r=0,i={},o="string"==typeof n?n.split(" "):[n];r<4;r++)i[e+oe[r]+t]=o[r]||o[r-2]||o[0];return i}},"margin"!==e&&(w.cssHooks[e+t].set=Ke)}),w.fn.extend({css:function(e,t){return z(this,function(e,t,n){var r,i,o={},a=0;if(Array.isArray(t)){for(r=$e(e),i=t.length;a1)}});function tt(e,t,n,r,i){return new tt.prototype.init(e,t,n,r,i)}w.Tween=tt,tt.prototype={constructor:tt,init:function(e,t,n,r,i,o){this.elem=e,this.prop=n,this.easing=i||w.easing._default,this.options=t,this.start=this.now=this.cur(),this.end=r,this.unit=o||(w.cssNumber[n]?"":"px")},cur:function(){var e=tt.propHooks[this.prop];return e&&e.get?e.get(this):tt.propHooks._default.get(this)},run:function(e){var t,n=tt.propHooks[this.prop];return this.options.duration?this.pos=t=w.easing[this.easing](e,this.options.duration*e,0,1,this.options.duration):this.pos=t=e,this.now=(this.end-this.start)*t+this.start,this.options.step&&this.options.step.call(this.elem,this.now,this),n&&n.set?n.set(this):tt.propHooks._default.set(this),this}},tt.prototype.init.prototype=tt.prototype,tt.propHooks={_default:{get:function(e){var t;return 1!==e.elem.nodeType||null!=e.elem[e.prop]&&null==e.elem.style[e.prop]?e.elem[e.prop]:(t=w.css(e.elem,e.prop,""))&&"auto"!==t?t:0},set:function(e){w.fx.step[e.prop]?w.fx.step[e.prop](e):1!==e.elem.nodeType||null==e.elem.style[w.cssProps[e.prop]]&&!w.cssHooks[e.prop]?e.elem[e.prop]=e.now:w.style(e.elem,e.prop,e.now+e.unit)}}},tt.propHooks.scrollTop=tt.propHooks.scrollLeft={set:function(e){e.elem.nodeType&&e.elem.parentNode&&(e.elem[e.prop]=e.now)}},w.easing={linear:function(e){return e},swing:function(e){return.5-Math.cos(e*Math.PI)/2},_default:"swing"},w.fx=tt.prototype.init,w.fx.step={};var nt,rt,it=/^(?:toggle|show|hide)$/,ot=/queueHooks$/;function at(){rt&&(!1===r.hidden&&e.requestAnimationFrame?e.requestAnimationFrame(at):e.setTimeout(at,w.fx.interval),w.fx.tick())}function st(){return e.setTimeout(function(){nt=void 0}),nt=Date.now()}function ut(e,t){var n,r=0,i={height:e};for(t=t?1:0;r<4;r+=2-t)i["margin"+(n=oe[r])]=i["padding"+n]=e;return t&&(i.opacity=i.width=e),i}function lt(e,t,n){for(var r,i=(pt.tweeners[t]||[]).concat(pt.tweeners["*"]),o=0,a=i.length;o1)},removeAttr:function(e){return this.each(function(){w.removeAttr(this,e)})}}),w.extend({attr:function(e,t,n){var r,i,o=e.nodeType;if(3!==o&&8!==o&&2!==o)return"undefined"==typeof e.getAttribute?w.prop(e,t,n):(1===o&&w.isXMLDoc(e)||(i=w.attrHooks[t.toLowerCase()]||(w.expr.match.bool.test(t)?dt:void 0)),void 0!==n?null===n?void w.removeAttr(e,t):i&&"set"in i&&void 0!==(r=i.set(e,n,t))?r:(e.setAttribute(t,n+""),n):i&&"get"in i&&null!==(r=i.get(e,t))?r:null==(r=w.find.attr(e,t))?void 0:r)},attrHooks:{type:{set:function(e,t){if(!h.radioValue&&"radio"===t&&N(e,"input")){var n=e.value;return e.setAttribute("type",t),n&&(e.value=n),t}}}},removeAttr:function(e,t){var n,r=0,i=t&&t.match(M);if(i&&1===e.nodeType)while(n=i[r++])e.removeAttribute(n)}}),dt={set:function(e,t,n){return!1===t?w.removeAttr(e,n):e.setAttribute(n,n),n}},w.each(w.expr.match.bool.source.match(/\w+/g),function(e,t){var n=ht[t]||w.find.attr;ht[t]=function(e,t,r){var i,o,a=t.toLowerCase();return r||(o=ht[a],ht[a]=i,i=null!=n(e,t,r)?a:null,ht[a]=o),i}});var gt=/^(?:input|select|textarea|button)$/i,yt=/^(?:a|area)$/i;w.fn.extend({prop:function(e,t){return z(this,w.prop,e,t,arguments.length>1)},removeProp:function(e){return this.each(function(){delete this[w.propFix[e]||e]})}}),w.extend({prop:function(e,t,n){var r,i,o=e.nodeType;if(3!==o&&8!==o&&2!==o)return 1===o&&w.isXMLDoc(e)||(t=w.propFix[t]||t,i=w.propHooks[t]),void 0!==n?i&&"set"in i&&void 0!==(r=i.set(e,n,t))?r:e[t]=n:i&&"get"in i&&null!==(r=i.get(e,t))?r:e[t]},propHooks:{tabIndex:{get:function(e){var t=w.find.attr(e,"tabindex");return t?parseInt(t,10):gt.test(e.nodeName)||yt.test(e.nodeName)&&e.href?0:-1}}},propFix:{"for":"htmlFor","class":"className"}}),h.optSelected||(w.propHooks.selected={get:function(e){var t=e.parentNode;return t&&t.parentNode&&t.parentNode.selectedIndex,null},set:function(e){var t=e.parentNode;t&&(t.selectedIndex,t.parentNode&&t.parentNode.selectedIndex)}}),w.each(["tabIndex","readOnly","maxLength","cellSpacing","cellPadding","rowSpan","colSpan","useMap","frameBorder","contentEditable"],function(){w.propFix[this.toLowerCase()]=this});function vt(e){return(e.match(M)||[]).join(" ")}function mt(e){return e.getAttribute&&e.getAttribute("class")||""}function xt(e){return Array.isArray(e)?e:"string"==typeof e?e.match(M)||[]:[]}w.fn.extend({addClass:function(e){var t,n,r,i,o,a,s,u=0;if(g(e))return this.each(function(t){w(this).addClass(e.call(this,t,mt(this)))});if((t=xt(e)).length)while(n=this[u++])if(i=mt(n),r=1===n.nodeType&&" "+vt(i)+" "){a=0;while(o=t[a++])r.indexOf(" "+o+" ")<0&&(r+=o+" ");i!==(s=vt(r))&&n.setAttribute("class",s)}return this},removeClass:function(e){var t,n,r,i,o,a,s,u=0;if(g(e))return this.each(function(t){w(this).removeClass(e.call(this,t,mt(this)))});if(!arguments.length)return this.attr("class","");if((t=xt(e)).length)while(n=this[u++])if(i=mt(n),r=1===n.nodeType&&" "+vt(i)+" "){a=0;while(o=t[a++])while(r.indexOf(" "+o+" ")>-1)r=r.replace(" "+o+" "," ");i!==(s=vt(r))&&n.setAttribute("class",s)}return this},toggleClass:function(e,t){var n=typeof e,r="string"===n||Array.isArray(e);return"boolean"==typeof t&&r?t?this.addClass(e):this.removeClass(e):g(e)?this.each(function(n){w(this).toggleClass(e.call(this,n,mt(this),t),t)}):this.each(function(){var t,i,o,a;if(r){i=0,o=w(this),a=xt(e);while(t=a[i++])o.hasClass(t)?o.removeClass(t):o.addClass(t)}else void 0!==e&&"boolean"!==n||((t=mt(this))&&J.set(this,"__className__",t),this.setAttribute&&this.setAttribute("class",t||!1===e?"":J.get(this,"__className__")||""))})},hasClass:function(e){var t,n,r=0;t=" "+e+" ";while(n=this[r++])if(1===n.nodeType&&(" "+vt(mt(n))+" ").indexOf(t)>-1)return!0;return!1}});var bt=/\r/g;w.fn.extend({val:function(e){var t,n,r,i=this[0];{if(arguments.length)return r=g(e),this.each(function(n){var i;1===this.nodeType&&(null==(i=r?e.call(this,n,w(this).val()):e)?i="":"number"==typeof i?i+="":Array.isArray(i)&&(i=w.map(i,function(e){return null==e?"":e+""})),(t=w.valHooks[this.type]||w.valHooks[this.nodeName.toLowerCase()])&&"set"in t&&void 0!==t.set(this,i,"value")||(this.value=i))});if(i)return(t=w.valHooks[i.type]||w.valHooks[i.nodeName.toLowerCase()])&&"get"in t&&void 0!==(n=t.get(i,"value"))?n:"string"==typeof(n=i.value)?n.replace(bt,""):null==n?"":n}}}),w.extend({valHooks:{option:{get:function(e){var t=w.find.attr(e,"value");return null!=t?t:vt(w.text(e))}},select:{get:function(e){var t,n,r,i=e.options,o=e.selectedIndex,a="select-one"===e.type,s=a?null:[],u=a?o+1:i.length;for(r=o<0?u:a?o:0;r-1)&&(n=!0);return n||(e.selectedIndex=-1),o}}}}),w.each(["radio","checkbox"],function(){w.valHooks[this]={set:function(e,t){if(Array.isArray(t))return e.checked=w.inArray(w(e).val(),t)>-1}},h.checkOn||(w.valHooks[this].get=function(e){return null===e.getAttribute("value")?"on":e.value})}),h.focusin="onfocusin"in e;var wt=/^(?:focusinfocus|focusoutblur)$/,Tt=function(e){e.stopPropagation()};w.extend(w.event,{trigger:function(t,n,i,o){var a,s,u,l,c,p,d,h,v=[i||r],m=f.call(t,"type")?t.type:t,x=f.call(t,"namespace")?t.namespace.split("."):[];if(s=h=u=i=i||r,3!==i.nodeType&&8!==i.nodeType&&!wt.test(m+w.event.triggered)&&(m.indexOf(".")>-1&&(m=(x=m.split(".")).shift(),x.sort()),c=m.indexOf(":")<0&&"on"+m,t=t[w.expando]?t:new w.Event(m,"object"==typeof t&&t),t.isTrigger=o?2:3,t.namespace=x.join("."),t.rnamespace=t.namespace?new RegExp("(^|\\.)"+x.join("\\.(?:.*\\.|)")+"(\\.|$)"):null,t.result=void 0,t.target||(t.target=i),n=null==n?[t]:w.makeArray(n,[t]),d=w.event.special[m]||{},o||!d.trigger||!1!==d.trigger.apply(i,n))){if(!o&&!d.noBubble&&!y(i)){for(l=d.delegateType||m,wt.test(l+m)||(s=s.parentNode);s;s=s.parentNode)v.push(s),u=s;u===(i.ownerDocument||r)&&v.push(u.defaultView||u.parentWindow||e)}a=0;while((s=v[a++])&&!t.isPropagationStopped())h=s,t.type=a>1?l:d.bindType||m,(p=(J.get(s,"events")||{})[t.type]&&J.get(s,"handle"))&&p.apply(s,n),(p=c&&s[c])&&p.apply&&Y(s)&&(t.result=p.apply(s,n),!1===t.result&&t.preventDefault());return t.type=m,o||t.isDefaultPrevented()||d._default&&!1!==d._default.apply(v.pop(),n)||!Y(i)||c&&g(i[m])&&!y(i)&&((u=i[c])&&(i[c]=null),w.event.triggered=m,t.isPropagationStopped()&&h.addEventListener(m,Tt),i[m](),t.isPropagationStopped()&&h.removeEventListener(m,Tt),w.event.triggered=void 0,u&&(i[c]=u)),t.result}},simulate:function(e,t,n){var r=w.extend(new w.Event,n,{type:e,isSimulated:!0});w.event.trigger(r,null,t)}}),w.fn.extend({trigger:function(e,t){return this.each(function(){w.event.trigger(e,t,this)})},triggerHandler:function(e,t){var n=this[0];if(n)return w.event.trigger(e,t,n,!0)}}),h.focusin||w.each({focus:"focusin",blur:"focusout"},function(e,t){var n=function(e){w.event.simulate(t,e.target,w.event.fix(e))};w.event.special[t]={setup:function(){var r=this.ownerDocument||this,i=J.access(r,t);i||r.addEventListener(e,n,!0),J.access(r,t,(i||0)+1)},teardown:function(){var r=this.ownerDocument||this,i=J.access(r,t)-1;i?J.access(r,t,i):(r.removeEventListener(e,n,!0),J.remove(r,t))}}});var Ct=e.location,Et=Date.now(),kt=/\?/;w.parseXML=function(t){var n;if(!t||"string"!=typeof t)return null;try{n=(new e.DOMParser).parseFromString(t,"text/xml")}catch(e){n=void 0}return n&&!n.getElementsByTagName("parsererror").length||w.error("Invalid XML: "+t),n};var St=/\[\]$/,Dt=/\r?\n/g,Nt=/^(?:submit|button|image|reset|file)$/i,At=/^(?:input|select|textarea|keygen)/i;function jt(e,t,n,r){var i;if(Array.isArray(t))w.each(t,function(t,i){n||St.test(e)?r(e,i):jt(e+"["+("object"==typeof i&&null!=i?t:"")+"]",i,n,r)});else if(n||"object"!==x(t))r(e,t);else for(i in t)jt(e+"["+i+"]",t[i],n,r)}w.param=function(e,t){var n,r=[],i=function(e,t){var n=g(t)?t():t;r[r.length]=encodeURIComponent(e)+"="+encodeURIComponent(null==n?"":n)};if(Array.isArray(e)||e.jquery&&!w.isPlainObject(e))w.each(e,function(){i(this.name,this.value)});else for(n in e)jt(n,e[n],t,i);return r.join("&")},w.fn.extend({serialize:function(){return w.param(this.serializeArray())},serializeArray:function(){return this.map(function(){var e=w.prop(this,"elements");return e?w.makeArray(e):this}).filter(function(){var e=this.type;return this.name&&!w(this).is(":disabled")&&At.test(this.nodeName)&&!Nt.test(e)&&(this.checked||!pe.test(e))}).map(function(e,t){var n=w(this).val();return null==n?null:Array.isArray(n)?w.map(n,function(e){return{name:t.name,value:e.replace(Dt,"\r\n")}}):{name:t.name,value:n.replace(Dt,"\r\n")}}).get()}});var qt=/%20/g,Lt=/#.*$/,Ht=/([?&])_=[^&]*/,Ot=/^(.*?):[ \t]*([^\r\n]*)$/gm,Pt=/^(?:about|app|app-storage|.+-extension|file|res|widget):$/,Mt=/^(?:GET|HEAD)$/,Rt=/^\/\//,It={},Wt={},$t="*/".concat("*"),Bt=r.createElement("a");Bt.href=Ct.href;function Ft(e){return function(t,n){"string"!=typeof t&&(n=t,t="*");var r,i=0,o=t.toLowerCase().match(M)||[];if(g(n))while(r=o[i++])"+"===r[0]?(r=r.slice(1)||"*",(e[r]=e[r]||[]).unshift(n)):(e[r]=e[r]||[]).push(n)}}function _t(e,t,n,r){var i={},o=e===Wt;function a(s){var u;return i[s]=!0,w.each(e[s]||[],function(e,s){var l=s(t,n,r);return"string"!=typeof l||o||i[l]?o?!(u=l):void 0:(t.dataTypes.unshift(l),a(l),!1)}),u}return a(t.dataTypes[0])||!i["*"]&&a("*")}function zt(e,t){var n,r,i=w.ajaxSettings.flatOptions||{};for(n in t)void 0!==t[n]&&((i[n]?e:r||(r={}))[n]=t[n]);return r&&w.extend(!0,e,r),e}function Xt(e,t,n){var r,i,o,a,s=e.contents,u=e.dataTypes;while("*"===u[0])u.shift(),void 0===r&&(r=e.mimeType||t.getResponseHeader("Content-Type"));if(r)for(i in s)if(s[i]&&s[i].test(r)){u.unshift(i);break}if(u[0]in n)o=u[0];else{for(i in n){if(!u[0]||e.converters[i+" "+u[0]]){o=i;break}a||(a=i)}o=o||a}if(o)return o!==u[0]&&u.unshift(o),n[o]}function Ut(e,t,n,r){var i,o,a,s,u,l={},c=e.dataTypes.slice();if(c[1])for(a in e.converters)l[a.toLowerCase()]=e.converters[a];o=c.shift();while(o)if(e.responseFields[o]&&(n[e.responseFields[o]]=t),!u&&r&&e.dataFilter&&(t=e.dataFilter(t,e.dataType)),u=o,o=c.shift())if("*"===o)o=u;else if("*"!==u&&u!==o){if(!(a=l[u+" "+o]||l["* "+o]))for(i in l)if((s=i.split(" "))[1]===o&&(a=l[u+" "+s[0]]||l["* "+s[0]])){!0===a?a=l[i]:!0!==l[i]&&(o=s[0],c.unshift(s[1]));break}if(!0!==a)if(a&&e["throws"])t=a(t);else try{t=a(t)}catch(e){return{state:"parsererror",error:a?e:"No conversion from "+u+" to "+o}}}return{state:"success",data:t}}w.extend({active:0,lastModified:{},etag:{},ajaxSettings:{url:Ct.href,type:"GET",isLocal:Pt.test(Ct.protocol),global:!0,processData:!0,async:!0,contentType:"application/x-www-form-urlencoded; charset=UTF-8",accepts:{"*":$t,text:"text/plain",html:"text/html",xml:"application/xml, text/xml",json:"application/json, text/javascript"},contents:{xml:/\bxml\b/,html:/\bhtml/,json:/\bjson\b/},responseFields:{xml:"responseXML",text:"responseText",json:"responseJSON"},converters:{"* text":String,"text html":!0,"text json":JSON.parse,"text xml":w.parseXML},flatOptions:{url:!0,context:!0}},ajaxSetup:function(e,t){return t?zt(zt(e,w.ajaxSettings),t):zt(w.ajaxSettings,e)},ajaxPrefilter:Ft(It),ajaxTransport:Ft(Wt),ajax:function(t,n){"object"==typeof t&&(n=t,t=void 0),n=n||{};var i,o,a,s,u,l,c,f,p,d,h=w.ajaxSetup({},n),g=h.context||h,y=h.context&&(g.nodeType||g.jquery)?w(g):w.event,v=w.Deferred(),m=w.Callbacks("once memory"),x=h.statusCode||{},b={},T={},C="canceled",E={readyState:0,getResponseHeader:function(e){var t;if(c){if(!s){s={};while(t=Ot.exec(a))s[t[1].toLowerCase()]=t[2]}t=s[e.toLowerCase()]}return null==t?null:t},getAllResponseHeaders:function(){return c?a:null},setRequestHeader:function(e,t){return null==c&&(e=T[e.toLowerCase()]=T[e.toLowerCase()]||e,b[e]=t),this},overrideMimeType:function(e){return null==c&&(h.mimeType=e),this},statusCode:function(e){var t;if(e)if(c)E.always(e[E.status]);else for(t in e)x[t]=[x[t],e[t]];return this},abort:function(e){var t=e||C;return i&&i.abort(t),k(0,t),this}};if(v.promise(E),h.url=((t||h.url||Ct.href)+"").replace(Rt,Ct.protocol+"//"),h.type=n.method||n.type||h.method||h.type,h.dataTypes=(h.dataType||"*").toLowerCase().match(M)||[""],null==h.crossDomain){l=r.createElement("a");try{l.href=h.url,l.href=l.href,h.crossDomain=Bt.protocol+"//"+Bt.host!=l.protocol+"//"+l.host}catch(e){h.crossDomain=!0}}if(h.data&&h.processData&&"string"!=typeof h.data&&(h.data=w.param(h.data,h.traditional)),_t(It,h,n,E),c)return E;(f=w.event&&h.global)&&0==w.active++&&w.event.trigger("ajaxStart"),h.type=h.type.toUpperCase(),h.hasContent=!Mt.test(h.type),o=h.url.replace(Lt,""),h.hasContent?h.data&&h.processData&&0===(h.contentType||"").indexOf("application/x-www-form-urlencoded")&&(h.data=h.data.replace(qt,"+")):(d=h.url.slice(o.length),h.data&&(h.processData||"string"==typeof h.data)&&(o+=(kt.test(o)?"&":"?")+h.data,delete h.data),!1===h.cache&&(o=o.replace(Ht,"$1"),d=(kt.test(o)?"&":"?")+"_="+Et+++d),h.url=o+d),h.ifModified&&(w.lastModified[o]&&E.setRequestHeader("If-Modified-Since",w.lastModified[o]),w.etag[o]&&E.setRequestHeader("If-None-Match",w.etag[o])),(h.data&&h.hasContent&&!1!==h.contentType||n.contentType)&&E.setRequestHeader("Content-Type",h.contentType),E.setRequestHeader("Accept",h.dataTypes[0]&&h.accepts[h.dataTypes[0]]?h.accepts[h.dataTypes[0]]+("*"!==h.dataTypes[0]?", "+$t+"; q=0.01":""):h.accepts["*"]);for(p in h.headers)E.setRequestHeader(p,h.headers[p]);if(h.beforeSend&&(!1===h.beforeSend.call(g,E,h)||c))return E.abort();if(C="abort",m.add(h.complete),E.done(h.success),E.fail(h.error),i=_t(Wt,h,n,E)){if(E.readyState=1,f&&y.trigger("ajaxSend",[E,h]),c)return E;h.async&&h.timeout>0&&(u=e.setTimeout(function(){E.abort("timeout")},h.timeout));try{c=!1,i.send(b,k)}catch(e){if(c)throw e;k(-1,e)}}else k(-1,"No Transport");function k(t,n,r,s){var l,p,d,b,T,C=n;c||(c=!0,u&&e.clearTimeout(u),i=void 0,a=s||"",E.readyState=t>0?4:0,l=t>=200&&t<300||304===t,r&&(b=Xt(h,E,r)),b=Ut(h,b,E,l),l?(h.ifModified&&((T=E.getResponseHeader("Last-Modified"))&&(w.lastModified[o]=T),(T=E.getResponseHeader("etag"))&&(w.etag[o]=T)),204===t||"HEAD"===h.type?C="nocontent":304===t?C="notmodified":(C=b.state,p=b.data,l=!(d=b.error))):(d=C,!t&&C||(C="error",t<0&&(t=0))),E.status=t,E.statusText=(n||C)+"",l?v.resolveWith(g,[p,C,E]):v.rejectWith(g,[E,C,d]),E.statusCode(x),x=void 0,f&&y.trigger(l?"ajaxSuccess":"ajaxError",[E,h,l?p:d]),m.fireWith(g,[E,C]),f&&(y.trigger("ajaxComplete",[E,h]),--w.active||w.event.trigger("ajaxStop")))}return E},getJSON:function(e,t,n){return w.get(e,t,n,"json")},getScript:function(e,t){return w.get(e,void 0,t,"script")}}),w.each(["get","post"],function(e,t){w[t]=function(e,n,r,i){return g(n)&&(i=i||r,r=n,n=void 0),w.ajax(w.extend({url:e,type:t,dataType:i,data:n,success:r},w.isPlainObject(e)&&e))}}),w._evalUrl=function(e){return w.ajax({url:e,type:"GET",dataType:"script",cache:!0,async:!1,global:!1,"throws":!0})},w.fn.extend({wrapAll:function(e){var t;return this[0]&&(g(e)&&(e=e.call(this[0])),t=w(e,this[0].ownerDocument).eq(0).clone(!0),this[0].parentNode&&t.insertBefore(this[0]),t.map(function(){var e=this;while(e.firstElementChild)e=e.firstElementChild;return e}).append(this)),this},wrapInner:function(e){return g(e)?this.each(function(t){w(this).wrapInner(e.call(this,t))}):this.each(function(){var t=w(this),n=t.contents();n.length?n.wrapAll(e):t.append(e)})},wrap:function(e){var t=g(e);return this.each(function(n){w(this).wrapAll(t?e.call(this,n):e)})},unwrap:function(e){return this.parent(e).not("body").each(function(){w(this).replaceWith(this.childNodes)}),this}}),w.expr.pseudos.hidden=function(e){return!w.expr.pseudos.visible(e)},w.expr.pseudos.visible=function(e){return!!(e.offsetWidth||e.offsetHeight||e.getClientRects().length)},w.ajaxSettings.xhr=function(){try{return new e.XMLHttpRequest}catch(e){}};var Vt={0:200,1223:204},Gt=w.ajaxSettings.xhr();h.cors=!!Gt&&"withCredentials"in Gt,h.ajax=Gt=!!Gt,w.ajaxTransport(function(t){var n,r;if(h.cors||Gt&&!t.crossDomain)return{send:function(i,o){var a,s=t.xhr();if(s.open(t.type,t.url,t.async,t.username,t.password),t.xhrFields)for(a in t.xhrFields)s[a]=t.xhrFields[a];t.mimeType&&s.overrideMimeType&&s.overrideMimeType(t.mimeType),t.crossDomain||i["X-Requested-With"]||(i["X-Requested-With"]="XMLHttpRequest");for(a in i)s.setRequestHeader(a,i[a]);n=function(e){return function(){n&&(n=r=s.onload=s.onerror=s.onabort=s.ontimeout=s.onreadystatechange=null,"abort"===e?s.abort():"error"===e?"number"!=typeof s.status?o(0,"error"):o(s.status,s.statusText):o(Vt[s.status]||s.status,s.statusText,"text"!==(s.responseType||"text")||"string"!=typeof s.responseText?{binary:s.response}:{text:s.responseText},s.getAllResponseHeaders()))}},s.onload=n(),r=s.onerror=s.ontimeout=n("error"),void 0!==s.onabort?s.onabort=r:s.onreadystatechange=function(){4===s.readyState&&e.setTimeout(function(){n&&r()})},n=n("abort");try{s.send(t.hasContent&&t.data||null)}catch(e){if(n)throw e}},abort:function(){n&&n()}}}),w.ajaxPrefilter(function(e){e.crossDomain&&(e.contents.script=!1)}),w.ajaxSetup({accepts:{script:"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"},contents:{script:/\b(?:java|ecma)script\b/},converters:{"text script":function(e){return w.globalEval(e),e}}}),w.ajaxPrefilter("script",function(e){void 0===e.cache&&(e.cache=!1),e.crossDomain&&(e.type="GET")}),w.ajaxTransport("script",function(e){if(e.crossDomain){var t,n;return{send:function(i,o){t=w(" + + + + + +
+
+

Docs (100% documented)

+
+
+
+ +
+
+ +
+
+
+ +

Description

+ +

The BigInt package provides arbitrary-precision integer arithmetic in Swift. +It contains functionality comparable to that of the Java BigInteger class.

+ +

BigInt requires Swift 5.0.

+ +

Usage

+ +

Using Swift Package Manager, in your projects Package.swift file add a dependency like

+
dependencies: [
+// Dependencies declare other packages that this package depends on.
+.package(url: "https://github.com/leif-ibsen/BigInt", from: "1.0.0"),
+],
+
+ +

References

+ +

Algorithms from the following books have been used in the implementation. +There are references in the source code where appropriate.

+ +
    +
  • Donald E. Knuth: Seminumerical Algorithms. Addison-Wesley 1971
  • +
  • Crandall and Pomerance: Prime Numbers - A Computational Perspective. Second Edition, Springer 2005
  • +
  • Henry S. Warren, Jr: Hacker’s Delight. Second Edition, Addison-Wesley 2013
  • +
  • Menezes, Oorschot, Vanstone: Handbook of Applied Cryptography. CRC Press 1996
  • +
+ +

Acknowledgement

+ +

The BitSieve class used in the implementation is a translation to Swift of the corresponding class from Java BigInteger. +The GCD algorithm and the Karatsuba and ToomCook multiplication algorithms are modelled after the corresponding algorithms in Java BigInteger.

+ +
+
+ +
+
+ + + diff --git a/docs/js/jazzy.js b/docs/js/jazzy.js new file mode 100755 index 0000000..c31dc05 --- /dev/null +++ b/docs/js/jazzy.js @@ -0,0 +1,59 @@ +window.jazzy = {'docset': false} +if (typeof window.dash != 'undefined') { + document.documentElement.className += ' dash' + window.jazzy.docset = true +} +if (navigator.userAgent.match(/xcode/i)) { + document.documentElement.className += ' xcode' + window.jazzy.docset = true +} + +function toggleItem($link, $content) { + var animationDuration = 300; + $link.toggleClass('token-open'); + $content.slideToggle(animationDuration); +} + +function itemLinkToContent($link) { + return $link.parent().parent().next(); +} + +// On doc load + hash-change, open any targetted item +function openCurrentItemIfClosed() { + if (window.jazzy.docset) { + return; + } + var $link = $(`.token[href="${location.hash}"]`); + $content = itemLinkToContent($link); + if ($content.is(':hidden')) { + toggleItem($link, $content); + } +} + +$(openCurrentItemIfClosed); +$(window).on('hashchange', openCurrentItemIfClosed); + +// On item link ('token') click, toggle its discussion +$('.token').on('click', function(event) { + if (window.jazzy.docset) { + return; + } + var $link = $(this); + toggleItem($link, itemLinkToContent($link)); + + // Keeps the document from jumping to the hash. + var href = $link.attr('href'); + if (history.pushState) { + history.pushState({}, '', href); + } else { + location.hash = href; + } + event.preventDefault(); +}); + +// Clicks on links to the current, closed, item need to open the item +$("a:not('.token')").on('click', function() { + if (location == this.href) { + openCurrentItemIfClosed(); + } +}); diff --git a/docs/js/jquery.min.js b/docs/js/jquery.min.js new file mode 100644 index 0000000..4d9b3a2 --- /dev/null +++ b/docs/js/jquery.min.js @@ -0,0 +1,2 @@ +/*! jQuery v3.3.1 | (c) JS Foundation and other contributors | jquery.org/license */ +!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(e,t){"use strict";var n=[],r=e.document,i=Object.getPrototypeOf,o=n.slice,a=n.concat,s=n.push,u=n.indexOf,l={},c=l.toString,f=l.hasOwnProperty,p=f.toString,d=p.call(Object),h={},g=function e(t){return"function"==typeof t&&"number"!=typeof t.nodeType},y=function e(t){return null!=t&&t===t.window},v={type:!0,src:!0,noModule:!0};function m(e,t,n){var i,o=(t=t||r).createElement("script");if(o.text=e,n)for(i in v)n[i]&&(o[i]=n[i]);t.head.appendChild(o).parentNode.removeChild(o)}function x(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?l[c.call(e)]||"object":typeof e}var b="3.3.1",w=function(e,t){return new w.fn.init(e,t)},T=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;w.fn=w.prototype={jquery:"3.3.1",constructor:w,length:0,toArray:function(){return o.call(this)},get:function(e){return null==e?o.call(this):e<0?this[e+this.length]:this[e]},pushStack:function(e){var t=w.merge(this.constructor(),e);return t.prevObject=this,t},each:function(e){return w.each(this,e)},map:function(e){return this.pushStack(w.map(this,function(t,n){return e.call(t,n,t)}))},slice:function(){return this.pushStack(o.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(e){var t=this.length,n=+e+(e<0?t:0);return this.pushStack(n>=0&&n0&&t-1 in e)}var E=function(e){var t,n,r,i,o,a,s,u,l,c,f,p,d,h,g,y,v,m,x,b="sizzle"+1*new Date,w=e.document,T=0,C=0,E=ae(),k=ae(),S=ae(),D=function(e,t){return e===t&&(f=!0),0},N={}.hasOwnProperty,A=[],j=A.pop,q=A.push,L=A.push,H=A.slice,O=function(e,t){for(var n=0,r=e.length;n+~]|"+M+")"+M+"*"),z=new RegExp("="+M+"*([^\\]'\"]*?)"+M+"*\\]","g"),X=new RegExp(W),U=new RegExp("^"+R+"$"),V={ID:new RegExp("^#("+R+")"),CLASS:new RegExp("^\\.("+R+")"),TAG:new RegExp("^("+R+"|[*])"),ATTR:new RegExp("^"+I),PSEUDO:new RegExp("^"+W),CHILD:new RegExp("^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\("+M+"*(even|odd|(([+-]|)(\\d*)n|)"+M+"*(?:([+-]|)"+M+"*(\\d+)|))"+M+"*\\)|)","i"),bool:new RegExp("^(?:"+P+")$","i"),needsContext:new RegExp("^"+M+"*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("+M+"*((?:-\\d)?\\d*)"+M+"*\\)|)(?=[^-]|$)","i")},G=/^(?:input|select|textarea|button)$/i,Y=/^h\d$/i,Q=/^[^{]+\{\s*\[native \w/,J=/^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,K=/[+~]/,Z=new RegExp("\\\\([\\da-f]{1,6}"+M+"?|("+M+")|.)","ig"),ee=function(e,t,n){var r="0x"+t-65536;return r!==r||n?t:r<0?String.fromCharCode(r+65536):String.fromCharCode(r>>10|55296,1023&r|56320)},te=/([\0-\x1f\x7f]|^-?\d)|^-$|[^\0-\x1f\x7f-\uFFFF\w-]/g,ne=function(e,t){return t?"\0"===e?"\ufffd":e.slice(0,-1)+"\\"+e.charCodeAt(e.length-1).toString(16)+" ":"\\"+e},re=function(){p()},ie=me(function(e){return!0===e.disabled&&("form"in e||"label"in e)},{dir:"parentNode",next:"legend"});try{L.apply(A=H.call(w.childNodes),w.childNodes),A[w.childNodes.length].nodeType}catch(e){L={apply:A.length?function(e,t){q.apply(e,H.call(t))}:function(e,t){var n=e.length,r=0;while(e[n++]=t[r++]);e.length=n-1}}}function oe(e,t,r,i){var o,s,l,c,f,h,v,m=t&&t.ownerDocument,T=t?t.nodeType:9;if(r=r||[],"string"!=typeof e||!e||1!==T&&9!==T&&11!==T)return r;if(!i&&((t?t.ownerDocument||t:w)!==d&&p(t),t=t||d,g)){if(11!==T&&(f=J.exec(e)))if(o=f[1]){if(9===T){if(!(l=t.getElementById(o)))return r;if(l.id===o)return r.push(l),r}else if(m&&(l=m.getElementById(o))&&x(t,l)&&l.id===o)return r.push(l),r}else{if(f[2])return L.apply(r,t.getElementsByTagName(e)),r;if((o=f[3])&&n.getElementsByClassName&&t.getElementsByClassName)return L.apply(r,t.getElementsByClassName(o)),r}if(n.qsa&&!S[e+" "]&&(!y||!y.test(e))){if(1!==T)m=t,v=e;else if("object"!==t.nodeName.toLowerCase()){(c=t.getAttribute("id"))?c=c.replace(te,ne):t.setAttribute("id",c=b),s=(h=a(e)).length;while(s--)h[s]="#"+c+" "+ve(h[s]);v=h.join(","),m=K.test(e)&&ge(t.parentNode)||t}if(v)try{return L.apply(r,m.querySelectorAll(v)),r}catch(e){}finally{c===b&&t.removeAttribute("id")}}}return u(e.replace(B,"$1"),t,r,i)}function ae(){var e=[];function t(n,i){return e.push(n+" ")>r.cacheLength&&delete t[e.shift()],t[n+" "]=i}return t}function se(e){return e[b]=!0,e}function ue(e){var t=d.createElement("fieldset");try{return!!e(t)}catch(e){return!1}finally{t.parentNode&&t.parentNode.removeChild(t),t=null}}function le(e,t){var n=e.split("|"),i=n.length;while(i--)r.attrHandle[n[i]]=t}function ce(e,t){var n=t&&e,r=n&&1===e.nodeType&&1===t.nodeType&&e.sourceIndex-t.sourceIndex;if(r)return r;if(n)while(n=n.nextSibling)if(n===t)return-1;return e?1:-1}function fe(e){return function(t){return"input"===t.nodeName.toLowerCase()&&t.type===e}}function pe(e){return function(t){var n=t.nodeName.toLowerCase();return("input"===n||"button"===n)&&t.type===e}}function de(e){return function(t){return"form"in t?t.parentNode&&!1===t.disabled?"label"in t?"label"in t.parentNode?t.parentNode.disabled===e:t.disabled===e:t.isDisabled===e||t.isDisabled!==!e&&ie(t)===e:t.disabled===e:"label"in t&&t.disabled===e}}function he(e){return se(function(t){return t=+t,se(function(n,r){var i,o=e([],n.length,t),a=o.length;while(a--)n[i=o[a]]&&(n[i]=!(r[i]=n[i]))})})}function ge(e){return e&&"undefined"!=typeof e.getElementsByTagName&&e}n=oe.support={},o=oe.isXML=function(e){var t=e&&(e.ownerDocument||e).documentElement;return!!t&&"HTML"!==t.nodeName},p=oe.setDocument=function(e){var t,i,a=e?e.ownerDocument||e:w;return a!==d&&9===a.nodeType&&a.documentElement?(d=a,h=d.documentElement,g=!o(d),w!==d&&(i=d.defaultView)&&i.top!==i&&(i.addEventListener?i.addEventListener("unload",re,!1):i.attachEvent&&i.attachEvent("onunload",re)),n.attributes=ue(function(e){return e.className="i",!e.getAttribute("className")}),n.getElementsByTagName=ue(function(e){return e.appendChild(d.createComment("")),!e.getElementsByTagName("*").length}),n.getElementsByClassName=Q.test(d.getElementsByClassName),n.getById=ue(function(e){return h.appendChild(e).id=b,!d.getElementsByName||!d.getElementsByName(b).length}),n.getById?(r.filter.ID=function(e){var t=e.replace(Z,ee);return function(e){return e.getAttribute("id")===t}},r.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&g){var n=t.getElementById(e);return n?[n]:[]}}):(r.filter.ID=function(e){var t=e.replace(Z,ee);return function(e){var n="undefined"!=typeof e.getAttributeNode&&e.getAttributeNode("id");return n&&n.value===t}},r.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&g){var n,r,i,o=t.getElementById(e);if(o){if((n=o.getAttributeNode("id"))&&n.value===e)return[o];i=t.getElementsByName(e),r=0;while(o=i[r++])if((n=o.getAttributeNode("id"))&&n.value===e)return[o]}return[]}}),r.find.TAG=n.getElementsByTagName?function(e,t){return"undefined"!=typeof t.getElementsByTagName?t.getElementsByTagName(e):n.qsa?t.querySelectorAll(e):void 0}:function(e,t){var n,r=[],i=0,o=t.getElementsByTagName(e);if("*"===e){while(n=o[i++])1===n.nodeType&&r.push(n);return r}return o},r.find.CLASS=n.getElementsByClassName&&function(e,t){if("undefined"!=typeof t.getElementsByClassName&&g)return t.getElementsByClassName(e)},v=[],y=[],(n.qsa=Q.test(d.querySelectorAll))&&(ue(function(e){h.appendChild(e).innerHTML="",e.querySelectorAll("[msallowcapture^='']").length&&y.push("[*^$]="+M+"*(?:''|\"\")"),e.querySelectorAll("[selected]").length||y.push("\\["+M+"*(?:value|"+P+")"),e.querySelectorAll("[id~="+b+"-]").length||y.push("~="),e.querySelectorAll(":checked").length||y.push(":checked"),e.querySelectorAll("a#"+b+"+*").length||y.push(".#.+[+~]")}),ue(function(e){e.innerHTML="";var t=d.createElement("input");t.setAttribute("type","hidden"),e.appendChild(t).setAttribute("name","D"),e.querySelectorAll("[name=d]").length&&y.push("name"+M+"*[*^$|!~]?="),2!==e.querySelectorAll(":enabled").length&&y.push(":enabled",":disabled"),h.appendChild(e).disabled=!0,2!==e.querySelectorAll(":disabled").length&&y.push(":enabled",":disabled"),e.querySelectorAll("*,:x"),y.push(",.*:")})),(n.matchesSelector=Q.test(m=h.matches||h.webkitMatchesSelector||h.mozMatchesSelector||h.oMatchesSelector||h.msMatchesSelector))&&ue(function(e){n.disconnectedMatch=m.call(e,"*"),m.call(e,"[s!='']:x"),v.push("!=",W)}),y=y.length&&new RegExp(y.join("|")),v=v.length&&new RegExp(v.join("|")),t=Q.test(h.compareDocumentPosition),x=t||Q.test(h.contains)?function(e,t){var n=9===e.nodeType?e.documentElement:e,r=t&&t.parentNode;return e===r||!(!r||1!==r.nodeType||!(n.contains?n.contains(r):e.compareDocumentPosition&&16&e.compareDocumentPosition(r)))}:function(e,t){if(t)while(t=t.parentNode)if(t===e)return!0;return!1},D=t?function(e,t){if(e===t)return f=!0,0;var r=!e.compareDocumentPosition-!t.compareDocumentPosition;return r||(1&(r=(e.ownerDocument||e)===(t.ownerDocument||t)?e.compareDocumentPosition(t):1)||!n.sortDetached&&t.compareDocumentPosition(e)===r?e===d||e.ownerDocument===w&&x(w,e)?-1:t===d||t.ownerDocument===w&&x(w,t)?1:c?O(c,e)-O(c,t):0:4&r?-1:1)}:function(e,t){if(e===t)return f=!0,0;var n,r=0,i=e.parentNode,o=t.parentNode,a=[e],s=[t];if(!i||!o)return e===d?-1:t===d?1:i?-1:o?1:c?O(c,e)-O(c,t):0;if(i===o)return ce(e,t);n=e;while(n=n.parentNode)a.unshift(n);n=t;while(n=n.parentNode)s.unshift(n);while(a[r]===s[r])r++;return r?ce(a[r],s[r]):a[r]===w?-1:s[r]===w?1:0},d):d},oe.matches=function(e,t){return oe(e,null,null,t)},oe.matchesSelector=function(e,t){if((e.ownerDocument||e)!==d&&p(e),t=t.replace(z,"='$1']"),n.matchesSelector&&g&&!S[t+" "]&&(!v||!v.test(t))&&(!y||!y.test(t)))try{var r=m.call(e,t);if(r||n.disconnectedMatch||e.document&&11!==e.document.nodeType)return r}catch(e){}return oe(t,d,null,[e]).length>0},oe.contains=function(e,t){return(e.ownerDocument||e)!==d&&p(e),x(e,t)},oe.attr=function(e,t){(e.ownerDocument||e)!==d&&p(e);var i=r.attrHandle[t.toLowerCase()],o=i&&N.call(r.attrHandle,t.toLowerCase())?i(e,t,!g):void 0;return void 0!==o?o:n.attributes||!g?e.getAttribute(t):(o=e.getAttributeNode(t))&&o.specified?o.value:null},oe.escape=function(e){return(e+"").replace(te,ne)},oe.error=function(e){throw new Error("Syntax error, unrecognized expression: "+e)},oe.uniqueSort=function(e){var t,r=[],i=0,o=0;if(f=!n.detectDuplicates,c=!n.sortStable&&e.slice(0),e.sort(D),f){while(t=e[o++])t===e[o]&&(i=r.push(o));while(i--)e.splice(r[i],1)}return c=null,e},i=oe.getText=function(e){var t,n="",r=0,o=e.nodeType;if(o){if(1===o||9===o||11===o){if("string"==typeof e.textContent)return e.textContent;for(e=e.firstChild;e;e=e.nextSibling)n+=i(e)}else if(3===o||4===o)return e.nodeValue}else while(t=e[r++])n+=i(t);return n},(r=oe.selectors={cacheLength:50,createPseudo:se,match:V,attrHandle:{},find:{},relative:{">":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(e){return e[1]=e[1].replace(Z,ee),e[3]=(e[3]||e[4]||e[5]||"").replace(Z,ee),"~="===e[2]&&(e[3]=" "+e[3]+" "),e.slice(0,4)},CHILD:function(e){return e[1]=e[1].toLowerCase(),"nth"===e[1].slice(0,3)?(e[3]||oe.error(e[0]),e[4]=+(e[4]?e[5]+(e[6]||1):2*("even"===e[3]||"odd"===e[3])),e[5]=+(e[7]+e[8]||"odd"===e[3])):e[3]&&oe.error(e[0]),e},PSEUDO:function(e){var t,n=!e[6]&&e[2];return V.CHILD.test(e[0])?null:(e[3]?e[2]=e[4]||e[5]||"":n&&X.test(n)&&(t=a(n,!0))&&(t=n.indexOf(")",n.length-t)-n.length)&&(e[0]=e[0].slice(0,t),e[2]=n.slice(0,t)),e.slice(0,3))}},filter:{TAG:function(e){var t=e.replace(Z,ee).toLowerCase();return"*"===e?function(){return!0}:function(e){return e.nodeName&&e.nodeName.toLowerCase()===t}},CLASS:function(e){var t=E[e+" "];return t||(t=new RegExp("(^|"+M+")"+e+"("+M+"|$)"))&&E(e,function(e){return t.test("string"==typeof e.className&&e.className||"undefined"!=typeof e.getAttribute&&e.getAttribute("class")||"")})},ATTR:function(e,t,n){return function(r){var i=oe.attr(r,e);return null==i?"!="===t:!t||(i+="","="===t?i===n:"!="===t?i!==n:"^="===t?n&&0===i.indexOf(n):"*="===t?n&&i.indexOf(n)>-1:"$="===t?n&&i.slice(-n.length)===n:"~="===t?(" "+i.replace($," ")+" ").indexOf(n)>-1:"|="===t&&(i===n||i.slice(0,n.length+1)===n+"-"))}},CHILD:function(e,t,n,r,i){var o="nth"!==e.slice(0,3),a="last"!==e.slice(-4),s="of-type"===t;return 1===r&&0===i?function(e){return!!e.parentNode}:function(t,n,u){var l,c,f,p,d,h,g=o!==a?"nextSibling":"previousSibling",y=t.parentNode,v=s&&t.nodeName.toLowerCase(),m=!u&&!s,x=!1;if(y){if(o){while(g){p=t;while(p=p[g])if(s?p.nodeName.toLowerCase()===v:1===p.nodeType)return!1;h=g="only"===e&&!h&&"nextSibling"}return!0}if(h=[a?y.firstChild:y.lastChild],a&&m){x=(d=(l=(c=(f=(p=y)[b]||(p[b]={}))[p.uniqueID]||(f[p.uniqueID]={}))[e]||[])[0]===T&&l[1])&&l[2],p=d&&y.childNodes[d];while(p=++d&&p&&p[g]||(x=d=0)||h.pop())if(1===p.nodeType&&++x&&p===t){c[e]=[T,d,x];break}}else if(m&&(x=d=(l=(c=(f=(p=t)[b]||(p[b]={}))[p.uniqueID]||(f[p.uniqueID]={}))[e]||[])[0]===T&&l[1]),!1===x)while(p=++d&&p&&p[g]||(x=d=0)||h.pop())if((s?p.nodeName.toLowerCase()===v:1===p.nodeType)&&++x&&(m&&((c=(f=p[b]||(p[b]={}))[p.uniqueID]||(f[p.uniqueID]={}))[e]=[T,x]),p===t))break;return(x-=i)===r||x%r==0&&x/r>=0}}},PSEUDO:function(e,t){var n,i=r.pseudos[e]||r.setFilters[e.toLowerCase()]||oe.error("unsupported pseudo: "+e);return i[b]?i(t):i.length>1?(n=[e,e,"",t],r.setFilters.hasOwnProperty(e.toLowerCase())?se(function(e,n){var r,o=i(e,t),a=o.length;while(a--)e[r=O(e,o[a])]=!(n[r]=o[a])}):function(e){return i(e,0,n)}):i}},pseudos:{not:se(function(e){var t=[],n=[],r=s(e.replace(B,"$1"));return r[b]?se(function(e,t,n,i){var o,a=r(e,null,i,[]),s=e.length;while(s--)(o=a[s])&&(e[s]=!(t[s]=o))}):function(e,i,o){return t[0]=e,r(t,null,o,n),t[0]=null,!n.pop()}}),has:se(function(e){return function(t){return oe(e,t).length>0}}),contains:se(function(e){return e=e.replace(Z,ee),function(t){return(t.textContent||t.innerText||i(t)).indexOf(e)>-1}}),lang:se(function(e){return U.test(e||"")||oe.error("unsupported lang: "+e),e=e.replace(Z,ee).toLowerCase(),function(t){var n;do{if(n=g?t.lang:t.getAttribute("xml:lang")||t.getAttribute("lang"))return(n=n.toLowerCase())===e||0===n.indexOf(e+"-")}while((t=t.parentNode)&&1===t.nodeType);return!1}}),target:function(t){var n=e.location&&e.location.hash;return n&&n.slice(1)===t.id},root:function(e){return e===h},focus:function(e){return e===d.activeElement&&(!d.hasFocus||d.hasFocus())&&!!(e.type||e.href||~e.tabIndex)},enabled:de(!1),disabled:de(!0),checked:function(e){var t=e.nodeName.toLowerCase();return"input"===t&&!!e.checked||"option"===t&&!!e.selected},selected:function(e){return e.parentNode&&e.parentNode.selectedIndex,!0===e.selected},empty:function(e){for(e=e.firstChild;e;e=e.nextSibling)if(e.nodeType<6)return!1;return!0},parent:function(e){return!r.pseudos.empty(e)},header:function(e){return Y.test(e.nodeName)},input:function(e){return G.test(e.nodeName)},button:function(e){var t=e.nodeName.toLowerCase();return"input"===t&&"button"===e.type||"button"===t},text:function(e){var t;return"input"===e.nodeName.toLowerCase()&&"text"===e.type&&(null==(t=e.getAttribute("type"))||"text"===t.toLowerCase())},first:he(function(){return[0]}),last:he(function(e,t){return[t-1]}),eq:he(function(e,t,n){return[n<0?n+t:n]}),even:he(function(e,t){for(var n=0;n=0;)e.push(r);return e}),gt:he(function(e,t,n){for(var r=n<0?n+t:n;++r1?function(t,n,r){var i=e.length;while(i--)if(!e[i](t,n,r))return!1;return!0}:e[0]}function be(e,t,n){for(var r=0,i=t.length;r-1&&(o[l]=!(a[l]=f))}}else v=we(v===a?v.splice(h,v.length):v),i?i(null,a,v,u):L.apply(a,v)})}function Ce(e){for(var t,n,i,o=e.length,a=r.relative[e[0].type],s=a||r.relative[" "],u=a?1:0,c=me(function(e){return e===t},s,!0),f=me(function(e){return O(t,e)>-1},s,!0),p=[function(e,n,r){var i=!a&&(r||n!==l)||((t=n).nodeType?c(e,n,r):f(e,n,r));return t=null,i}];u1&&xe(p),u>1&&ve(e.slice(0,u-1).concat({value:" "===e[u-2].type?"*":""})).replace(B,"$1"),n,u0,i=e.length>0,o=function(o,a,s,u,c){var f,h,y,v=0,m="0",x=o&&[],b=[],w=l,C=o||i&&r.find.TAG("*",c),E=T+=null==w?1:Math.random()||.1,k=C.length;for(c&&(l=a===d||a||c);m!==k&&null!=(f=C[m]);m++){if(i&&f){h=0,a||f.ownerDocument===d||(p(f),s=!g);while(y=e[h++])if(y(f,a||d,s)){u.push(f);break}c&&(T=E)}n&&((f=!y&&f)&&v--,o&&x.push(f))}if(v+=m,n&&m!==v){h=0;while(y=t[h++])y(x,b,a,s);if(o){if(v>0)while(m--)x[m]||b[m]||(b[m]=j.call(u));b=we(b)}L.apply(u,b),c&&!o&&b.length>0&&v+t.length>1&&oe.uniqueSort(u)}return c&&(T=E,l=w),x};return n?se(o):o}return s=oe.compile=function(e,t){var n,r=[],i=[],o=S[e+" "];if(!o){t||(t=a(e)),n=t.length;while(n--)(o=Ce(t[n]))[b]?r.push(o):i.push(o);(o=S(e,Ee(i,r))).selector=e}return o},u=oe.select=function(e,t,n,i){var o,u,l,c,f,p="function"==typeof e&&e,d=!i&&a(e=p.selector||e);if(n=n||[],1===d.length){if((u=d[0]=d[0].slice(0)).length>2&&"ID"===(l=u[0]).type&&9===t.nodeType&&g&&r.relative[u[1].type]){if(!(t=(r.find.ID(l.matches[0].replace(Z,ee),t)||[])[0]))return n;p&&(t=t.parentNode),e=e.slice(u.shift().value.length)}o=V.needsContext.test(e)?0:u.length;while(o--){if(l=u[o],r.relative[c=l.type])break;if((f=r.find[c])&&(i=f(l.matches[0].replace(Z,ee),K.test(u[0].type)&&ge(t.parentNode)||t))){if(u.splice(o,1),!(e=i.length&&ve(u)))return L.apply(n,i),n;break}}}return(p||s(e,d))(i,t,!g,n,!t||K.test(e)&&ge(t.parentNode)||t),n},n.sortStable=b.split("").sort(D).join("")===b,n.detectDuplicates=!!f,p(),n.sortDetached=ue(function(e){return 1&e.compareDocumentPosition(d.createElement("fieldset"))}),ue(function(e){return e.innerHTML="","#"===e.firstChild.getAttribute("href")})||le("type|href|height|width",function(e,t,n){if(!n)return e.getAttribute(t,"type"===t.toLowerCase()?1:2)}),n.attributes&&ue(function(e){return e.innerHTML="",e.firstChild.setAttribute("value",""),""===e.firstChild.getAttribute("value")})||le("value",function(e,t,n){if(!n&&"input"===e.nodeName.toLowerCase())return e.defaultValue}),ue(function(e){return null==e.getAttribute("disabled")})||le(P,function(e,t,n){var r;if(!n)return!0===e[t]?t.toLowerCase():(r=e.getAttributeNode(t))&&r.specified?r.value:null}),oe}(e);w.find=E,w.expr=E.selectors,w.expr[":"]=w.expr.pseudos,w.uniqueSort=w.unique=E.uniqueSort,w.text=E.getText,w.isXMLDoc=E.isXML,w.contains=E.contains,w.escapeSelector=E.escape;var k=function(e,t,n){var r=[],i=void 0!==n;while((e=e[t])&&9!==e.nodeType)if(1===e.nodeType){if(i&&w(e).is(n))break;r.push(e)}return r},S=function(e,t){for(var n=[];e;e=e.nextSibling)1===e.nodeType&&e!==t&&n.push(e);return n},D=w.expr.match.needsContext;function N(e,t){return e.nodeName&&e.nodeName.toLowerCase()===t.toLowerCase()}var A=/^<([a-z][^\/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i;function j(e,t,n){return g(t)?w.grep(e,function(e,r){return!!t.call(e,r,e)!==n}):t.nodeType?w.grep(e,function(e){return e===t!==n}):"string"!=typeof t?w.grep(e,function(e){return u.call(t,e)>-1!==n}):w.filter(t,e,n)}w.filter=function(e,t,n){var r=t[0];return n&&(e=":not("+e+")"),1===t.length&&1===r.nodeType?w.find.matchesSelector(r,e)?[r]:[]:w.find.matches(e,w.grep(t,function(e){return 1===e.nodeType}))},w.fn.extend({find:function(e){var t,n,r=this.length,i=this;if("string"!=typeof e)return this.pushStack(w(e).filter(function(){for(t=0;t1?w.uniqueSort(n):n},filter:function(e){return this.pushStack(j(this,e||[],!1))},not:function(e){return this.pushStack(j(this,e||[],!0))},is:function(e){return!!j(this,"string"==typeof e&&D.test(e)?w(e):e||[],!1).length}});var q,L=/^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/;(w.fn.init=function(e,t,n){var i,o;if(!e)return this;if(n=n||q,"string"==typeof e){if(!(i="<"===e[0]&&">"===e[e.length-1]&&e.length>=3?[null,e,null]:L.exec(e))||!i[1]&&t)return!t||t.jquery?(t||n).find(e):this.constructor(t).find(e);if(i[1]){if(t=t instanceof w?t[0]:t,w.merge(this,w.parseHTML(i[1],t&&t.nodeType?t.ownerDocument||t:r,!0)),A.test(i[1])&&w.isPlainObject(t))for(i in t)g(this[i])?this[i](t[i]):this.attr(i,t[i]);return this}return(o=r.getElementById(i[2]))&&(this[0]=o,this.length=1),this}return e.nodeType?(this[0]=e,this.length=1,this):g(e)?void 0!==n.ready?n.ready(e):e(w):w.makeArray(e,this)}).prototype=w.fn,q=w(r);var H=/^(?:parents|prev(?:Until|All))/,O={children:!0,contents:!0,next:!0,prev:!0};w.fn.extend({has:function(e){var t=w(e,this),n=t.length;return this.filter(function(){for(var e=0;e-1:1===n.nodeType&&w.find.matchesSelector(n,e))){o.push(n);break}return this.pushStack(o.length>1?w.uniqueSort(o):o)},index:function(e){return e?"string"==typeof e?u.call(w(e),this[0]):u.call(this,e.jquery?e[0]:e):this[0]&&this[0].parentNode?this.first().prevAll().length:-1},add:function(e,t){return this.pushStack(w.uniqueSort(w.merge(this.get(),w(e,t))))},addBack:function(e){return this.add(null==e?this.prevObject:this.prevObject.filter(e))}});function P(e,t){while((e=e[t])&&1!==e.nodeType);return e}w.each({parent:function(e){var t=e.parentNode;return t&&11!==t.nodeType?t:null},parents:function(e){return k(e,"parentNode")},parentsUntil:function(e,t,n){return k(e,"parentNode",n)},next:function(e){return P(e,"nextSibling")},prev:function(e){return P(e,"previousSibling")},nextAll:function(e){return k(e,"nextSibling")},prevAll:function(e){return k(e,"previousSibling")},nextUntil:function(e,t,n){return k(e,"nextSibling",n)},prevUntil:function(e,t,n){return k(e,"previousSibling",n)},siblings:function(e){return S((e.parentNode||{}).firstChild,e)},children:function(e){return S(e.firstChild)},contents:function(e){return N(e,"iframe")?e.contentDocument:(N(e,"template")&&(e=e.content||e),w.merge([],e.childNodes))}},function(e,t){w.fn[e]=function(n,r){var i=w.map(this,t,n);return"Until"!==e.slice(-5)&&(r=n),r&&"string"==typeof r&&(i=w.filter(r,i)),this.length>1&&(O[e]||w.uniqueSort(i),H.test(e)&&i.reverse()),this.pushStack(i)}});var M=/[^\x20\t\r\n\f]+/g;function R(e){var t={};return w.each(e.match(M)||[],function(e,n){t[n]=!0}),t}w.Callbacks=function(e){e="string"==typeof e?R(e):w.extend({},e);var t,n,r,i,o=[],a=[],s=-1,u=function(){for(i=i||e.once,r=t=!0;a.length;s=-1){n=a.shift();while(++s-1)o.splice(n,1),n<=s&&s--}),this},has:function(e){return e?w.inArray(e,o)>-1:o.length>0},empty:function(){return o&&(o=[]),this},disable:function(){return i=a=[],o=n="",this},disabled:function(){return!o},lock:function(){return i=a=[],n||t||(o=n=""),this},locked:function(){return!!i},fireWith:function(e,n){return i||(n=[e,(n=n||[]).slice?n.slice():n],a.push(n),t||u()),this},fire:function(){return l.fireWith(this,arguments),this},fired:function(){return!!r}};return l};function I(e){return e}function W(e){throw e}function $(e,t,n,r){var i;try{e&&g(i=e.promise)?i.call(e).done(t).fail(n):e&&g(i=e.then)?i.call(e,t,n):t.apply(void 0,[e].slice(r))}catch(e){n.apply(void 0,[e])}}w.extend({Deferred:function(t){var n=[["notify","progress",w.Callbacks("memory"),w.Callbacks("memory"),2],["resolve","done",w.Callbacks("once memory"),w.Callbacks("once memory"),0,"resolved"],["reject","fail",w.Callbacks("once memory"),w.Callbacks("once memory"),1,"rejected"]],r="pending",i={state:function(){return r},always:function(){return o.done(arguments).fail(arguments),this},"catch":function(e){return i.then(null,e)},pipe:function(){var e=arguments;return w.Deferred(function(t){w.each(n,function(n,r){var i=g(e[r[4]])&&e[r[4]];o[r[1]](function(){var e=i&&i.apply(this,arguments);e&&g(e.promise)?e.promise().progress(t.notify).done(t.resolve).fail(t.reject):t[r[0]+"With"](this,i?[e]:arguments)})}),e=null}).promise()},then:function(t,r,i){var o=0;function a(t,n,r,i){return function(){var s=this,u=arguments,l=function(){var e,l;if(!(t=o&&(r!==W&&(s=void 0,u=[e]),n.rejectWith(s,u))}};t?c():(w.Deferred.getStackHook&&(c.stackTrace=w.Deferred.getStackHook()),e.setTimeout(c))}}return w.Deferred(function(e){n[0][3].add(a(0,e,g(i)?i:I,e.notifyWith)),n[1][3].add(a(0,e,g(t)?t:I)),n[2][3].add(a(0,e,g(r)?r:W))}).promise()},promise:function(e){return null!=e?w.extend(e,i):i}},o={};return w.each(n,function(e,t){var a=t[2],s=t[5];i[t[1]]=a.add,s&&a.add(function(){r=s},n[3-e][2].disable,n[3-e][3].disable,n[0][2].lock,n[0][3].lock),a.add(t[3].fire),o[t[0]]=function(){return o[t[0]+"With"](this===o?void 0:this,arguments),this},o[t[0]+"With"]=a.fireWith}),i.promise(o),t&&t.call(o,o),o},when:function(e){var t=arguments.length,n=t,r=Array(n),i=o.call(arguments),a=w.Deferred(),s=function(e){return function(n){r[e]=this,i[e]=arguments.length>1?o.call(arguments):n,--t||a.resolveWith(r,i)}};if(t<=1&&($(e,a.done(s(n)).resolve,a.reject,!t),"pending"===a.state()||g(i[n]&&i[n].then)))return a.then();while(n--)$(i[n],s(n),a.reject);return a.promise()}});var B=/^(Eval|Internal|Range|Reference|Syntax|Type|URI)Error$/;w.Deferred.exceptionHook=function(t,n){e.console&&e.console.warn&&t&&B.test(t.name)&&e.console.warn("jQuery.Deferred exception: "+t.message,t.stack,n)},w.readyException=function(t){e.setTimeout(function(){throw t})};var F=w.Deferred();w.fn.ready=function(e){return F.then(e)["catch"](function(e){w.readyException(e)}),this},w.extend({isReady:!1,readyWait:1,ready:function(e){(!0===e?--w.readyWait:w.isReady)||(w.isReady=!0,!0!==e&&--w.readyWait>0||F.resolveWith(r,[w]))}}),w.ready.then=F.then;function _(){r.removeEventListener("DOMContentLoaded",_),e.removeEventListener("load",_),w.ready()}"complete"===r.readyState||"loading"!==r.readyState&&!r.documentElement.doScroll?e.setTimeout(w.ready):(r.addEventListener("DOMContentLoaded",_),e.addEventListener("load",_));var z=function(e,t,n,r,i,o,a){var s=0,u=e.length,l=null==n;if("object"===x(n)){i=!0;for(s in n)z(e,t,s,n[s],!0,o,a)}else if(void 0!==r&&(i=!0,g(r)||(a=!0),l&&(a?(t.call(e,r),t=null):(l=t,t=function(e,t,n){return l.call(w(e),n)})),t))for(;s1,null,!0)},removeData:function(e){return this.each(function(){K.remove(this,e)})}}),w.extend({queue:function(e,t,n){var r;if(e)return t=(t||"fx")+"queue",r=J.get(e,t),n&&(!r||Array.isArray(n)?r=J.access(e,t,w.makeArray(n)):r.push(n)),r||[]},dequeue:function(e,t){t=t||"fx";var n=w.queue(e,t),r=n.length,i=n.shift(),o=w._queueHooks(e,t),a=function(){w.dequeue(e,t)};"inprogress"===i&&(i=n.shift(),r--),i&&("fx"===t&&n.unshift("inprogress"),delete o.stop,i.call(e,a,o)),!r&&o&&o.empty.fire()},_queueHooks:function(e,t){var n=t+"queueHooks";return J.get(e,n)||J.access(e,n,{empty:w.Callbacks("once memory").add(function(){J.remove(e,[t+"queue",n])})})}}),w.fn.extend({queue:function(e,t){var n=2;return"string"!=typeof e&&(t=e,e="fx",n--),arguments.length\x20\t\r\n\f]+)/i,he=/^$|^module$|\/(?:java|ecma)script/i,ge={option:[1,""],thead:[1,"","
"],col:[2,"","
"],tr:[2,"","
"],td:[3,"","
"],_default:[0,"",""]};ge.optgroup=ge.option,ge.tbody=ge.tfoot=ge.colgroup=ge.caption=ge.thead,ge.th=ge.td;function ye(e,t){var n;return n="undefined"!=typeof e.getElementsByTagName?e.getElementsByTagName(t||"*"):"undefined"!=typeof e.querySelectorAll?e.querySelectorAll(t||"*"):[],void 0===t||t&&N(e,t)?w.merge([e],n):n}function ve(e,t){for(var n=0,r=e.length;n-1)i&&i.push(o);else if(l=w.contains(o.ownerDocument,o),a=ye(f.appendChild(o),"script"),l&&ve(a),n){c=0;while(o=a[c++])he.test(o.type||"")&&n.push(o)}return f}!function(){var e=r.createDocumentFragment().appendChild(r.createElement("div")),t=r.createElement("input");t.setAttribute("type","radio"),t.setAttribute("checked","checked"),t.setAttribute("name","t"),e.appendChild(t),h.checkClone=e.cloneNode(!0).cloneNode(!0).lastChild.checked,e.innerHTML="",h.noCloneChecked=!!e.cloneNode(!0).lastChild.defaultValue}();var be=r.documentElement,we=/^key/,Te=/^(?:mouse|pointer|contextmenu|drag|drop)|click/,Ce=/^([^.]*)(?:\.(.+)|)/;function Ee(){return!0}function ke(){return!1}function Se(){try{return r.activeElement}catch(e){}}function De(e,t,n,r,i,o){var a,s;if("object"==typeof t){"string"!=typeof n&&(r=r||n,n=void 0);for(s in t)De(e,s,n,r,t[s],o);return e}if(null==r&&null==i?(i=n,r=n=void 0):null==i&&("string"==typeof n?(i=r,r=void 0):(i=r,r=n,n=void 0)),!1===i)i=ke;else if(!i)return e;return 1===o&&(a=i,(i=function(e){return w().off(e),a.apply(this,arguments)}).guid=a.guid||(a.guid=w.guid++)),e.each(function(){w.event.add(this,t,i,r,n)})}w.event={global:{},add:function(e,t,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,y=J.get(e);if(y){n.handler&&(n=(o=n).handler,i=o.selector),i&&w.find.matchesSelector(be,i),n.guid||(n.guid=w.guid++),(u=y.events)||(u=y.events={}),(a=y.handle)||(a=y.handle=function(t){return"undefined"!=typeof w&&w.event.triggered!==t.type?w.event.dispatch.apply(e,arguments):void 0}),l=(t=(t||"").match(M)||[""]).length;while(l--)d=g=(s=Ce.exec(t[l])||[])[1],h=(s[2]||"").split(".").sort(),d&&(f=w.event.special[d]||{},d=(i?f.delegateType:f.bindType)||d,f=w.event.special[d]||{},c=w.extend({type:d,origType:g,data:r,handler:n,guid:n.guid,selector:i,needsContext:i&&w.expr.match.needsContext.test(i),namespace:h.join(".")},o),(p=u[d])||((p=u[d]=[]).delegateCount=0,f.setup&&!1!==f.setup.call(e,r,h,a)||e.addEventListener&&e.addEventListener(d,a)),f.add&&(f.add.call(e,c),c.handler.guid||(c.handler.guid=n.guid)),i?p.splice(p.delegateCount++,0,c):p.push(c),w.event.global[d]=!0)}},remove:function(e,t,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,y=J.hasData(e)&&J.get(e);if(y&&(u=y.events)){l=(t=(t||"").match(M)||[""]).length;while(l--)if(s=Ce.exec(t[l])||[],d=g=s[1],h=(s[2]||"").split(".").sort(),d){f=w.event.special[d]||{},p=u[d=(r?f.delegateType:f.bindType)||d]||[],s=s[2]&&new RegExp("(^|\\.)"+h.join("\\.(?:.*\\.|)")+"(\\.|$)"),a=o=p.length;while(o--)c=p[o],!i&&g!==c.origType||n&&n.guid!==c.guid||s&&!s.test(c.namespace)||r&&r!==c.selector&&("**"!==r||!c.selector)||(p.splice(o,1),c.selector&&p.delegateCount--,f.remove&&f.remove.call(e,c));a&&!p.length&&(f.teardown&&!1!==f.teardown.call(e,h,y.handle)||w.removeEvent(e,d,y.handle),delete u[d])}else for(d in u)w.event.remove(e,d+t[l],n,r,!0);w.isEmptyObject(u)&&J.remove(e,"handle events")}},dispatch:function(e){var t=w.event.fix(e),n,r,i,o,a,s,u=new Array(arguments.length),l=(J.get(this,"events")||{})[t.type]||[],c=w.event.special[t.type]||{};for(u[0]=t,n=1;n=1))for(;l!==this;l=l.parentNode||this)if(1===l.nodeType&&("click"!==e.type||!0!==l.disabled)){for(o=[],a={},n=0;n-1:w.find(i,this,null,[l]).length),a[i]&&o.push(r);o.length&&s.push({elem:l,handlers:o})}return l=this,u\x20\t\r\n\f]*)[^>]*)\/>/gi,Ae=/\s*$/g;function Le(e,t){return N(e,"table")&&N(11!==t.nodeType?t:t.firstChild,"tr")?w(e).children("tbody")[0]||e:e}function He(e){return e.type=(null!==e.getAttribute("type"))+"/"+e.type,e}function Oe(e){return"true/"===(e.type||"").slice(0,5)?e.type=e.type.slice(5):e.removeAttribute("type"),e}function Pe(e,t){var n,r,i,o,a,s,u,l;if(1===t.nodeType){if(J.hasData(e)&&(o=J.access(e),a=J.set(t,o),l=o.events)){delete a.handle,a.events={};for(i in l)for(n=0,r=l[i].length;n1&&"string"==typeof y&&!h.checkClone&&je.test(y))return e.each(function(i){var o=e.eq(i);v&&(t[0]=y.call(this,i,o.html())),Re(o,t,n,r)});if(p&&(i=xe(t,e[0].ownerDocument,!1,e,r),o=i.firstChild,1===i.childNodes.length&&(i=o),o||r)){for(u=(s=w.map(ye(i,"script"),He)).length;f")},clone:function(e,t,n){var r,i,o,a,s=e.cloneNode(!0),u=w.contains(e.ownerDocument,e);if(!(h.noCloneChecked||1!==e.nodeType&&11!==e.nodeType||w.isXMLDoc(e)))for(a=ye(s),r=0,i=(o=ye(e)).length;r0&&ve(a,!u&&ye(e,"script")),s},cleanData:function(e){for(var t,n,r,i=w.event.special,o=0;void 0!==(n=e[o]);o++)if(Y(n)){if(t=n[J.expando]){if(t.events)for(r in t.events)i[r]?w.event.remove(n,r):w.removeEvent(n,r,t.handle);n[J.expando]=void 0}n[K.expando]&&(n[K.expando]=void 0)}}}),w.fn.extend({detach:function(e){return Ie(this,e,!0)},remove:function(e){return Ie(this,e)},text:function(e){return z(this,function(e){return void 0===e?w.text(this):this.empty().each(function(){1!==this.nodeType&&11!==this.nodeType&&9!==this.nodeType||(this.textContent=e)})},null,e,arguments.length)},append:function(){return Re(this,arguments,function(e){1!==this.nodeType&&11!==this.nodeType&&9!==this.nodeType||Le(this,e).appendChild(e)})},prepend:function(){return Re(this,arguments,function(e){if(1===this.nodeType||11===this.nodeType||9===this.nodeType){var t=Le(this,e);t.insertBefore(e,t.firstChild)}})},before:function(){return Re(this,arguments,function(e){this.parentNode&&this.parentNode.insertBefore(e,this)})},after:function(){return Re(this,arguments,function(e){this.parentNode&&this.parentNode.insertBefore(e,this.nextSibling)})},empty:function(){for(var e,t=0;null!=(e=this[t]);t++)1===e.nodeType&&(w.cleanData(ye(e,!1)),e.textContent="");return this},clone:function(e,t){return e=null!=e&&e,t=null==t?e:t,this.map(function(){return w.clone(this,e,t)})},html:function(e){return z(this,function(e){var t=this[0]||{},n=0,r=this.length;if(void 0===e&&1===t.nodeType)return t.innerHTML;if("string"==typeof e&&!Ae.test(e)&&!ge[(de.exec(e)||["",""])[1].toLowerCase()]){e=w.htmlPrefilter(e);try{for(;n=0&&(u+=Math.max(0,Math.ceil(e["offset"+t[0].toUpperCase()+t.slice(1)]-o-u-s-.5))),u}function et(e,t,n){var r=$e(e),i=Fe(e,t,r),o="border-box"===w.css(e,"boxSizing",!1,r),a=o;if(We.test(i)){if(!n)return i;i="auto"}return a=a&&(h.boxSizingReliable()||i===e.style[t]),("auto"===i||!parseFloat(i)&&"inline"===w.css(e,"display",!1,r))&&(i=e["offset"+t[0].toUpperCase()+t.slice(1)],a=!0),(i=parseFloat(i)||0)+Ze(e,t,n||(o?"border":"content"),a,r,i)+"px"}w.extend({cssHooks:{opacity:{get:function(e,t){if(t){var n=Fe(e,"opacity");return""===n?"1":n}}}},cssNumber:{animationIterationCount:!0,columnCount:!0,fillOpacity:!0,flexGrow:!0,flexShrink:!0,fontWeight:!0,lineHeight:!0,opacity:!0,order:!0,orphans:!0,widows:!0,zIndex:!0,zoom:!0},cssProps:{},style:function(e,t,n,r){if(e&&3!==e.nodeType&&8!==e.nodeType&&e.style){var i,o,a,s=G(t),u=Xe.test(t),l=e.style;if(u||(t=Je(s)),a=w.cssHooks[t]||w.cssHooks[s],void 0===n)return a&&"get"in a&&void 0!==(i=a.get(e,!1,r))?i:l[t];"string"==(o=typeof n)&&(i=ie.exec(n))&&i[1]&&(n=ue(e,t,i),o="number"),null!=n&&n===n&&("number"===o&&(n+=i&&i[3]||(w.cssNumber[s]?"":"px")),h.clearCloneStyle||""!==n||0!==t.indexOf("background")||(l[t]="inherit"),a&&"set"in a&&void 0===(n=a.set(e,n,r))||(u?l.setProperty(t,n):l[t]=n))}},css:function(e,t,n,r){var i,o,a,s=G(t);return Xe.test(t)||(t=Je(s)),(a=w.cssHooks[t]||w.cssHooks[s])&&"get"in a&&(i=a.get(e,!0,n)),void 0===i&&(i=Fe(e,t,r)),"normal"===i&&t in Ve&&(i=Ve[t]),""===n||n?(o=parseFloat(i),!0===n||isFinite(o)?o||0:i):i}}),w.each(["height","width"],function(e,t){w.cssHooks[t]={get:function(e,n,r){if(n)return!ze.test(w.css(e,"display"))||e.getClientRects().length&&e.getBoundingClientRect().width?et(e,t,r):se(e,Ue,function(){return et(e,t,r)})},set:function(e,n,r){var i,o=$e(e),a="border-box"===w.css(e,"boxSizing",!1,o),s=r&&Ze(e,t,r,a,o);return a&&h.scrollboxSize()===o.position&&(s-=Math.ceil(e["offset"+t[0].toUpperCase()+t.slice(1)]-parseFloat(o[t])-Ze(e,t,"border",!1,o)-.5)),s&&(i=ie.exec(n))&&"px"!==(i[3]||"px")&&(e.style[t]=n,n=w.css(e,t)),Ke(e,n,s)}}}),w.cssHooks.marginLeft=_e(h.reliableMarginLeft,function(e,t){if(t)return(parseFloat(Fe(e,"marginLeft"))||e.getBoundingClientRect().left-se(e,{marginLeft:0},function(){return e.getBoundingClientRect().left}))+"px"}),w.each({margin:"",padding:"",border:"Width"},function(e,t){w.cssHooks[e+t]={expand:function(n){for(var r=0,i={},o="string"==typeof n?n.split(" "):[n];r<4;r++)i[e+oe[r]+t]=o[r]||o[r-2]||o[0];return i}},"margin"!==e&&(w.cssHooks[e+t].set=Ke)}),w.fn.extend({css:function(e,t){return z(this,function(e,t,n){var r,i,o={},a=0;if(Array.isArray(t)){for(r=$e(e),i=t.length;a1)}});function tt(e,t,n,r,i){return new tt.prototype.init(e,t,n,r,i)}w.Tween=tt,tt.prototype={constructor:tt,init:function(e,t,n,r,i,o){this.elem=e,this.prop=n,this.easing=i||w.easing._default,this.options=t,this.start=this.now=this.cur(),this.end=r,this.unit=o||(w.cssNumber[n]?"":"px")},cur:function(){var e=tt.propHooks[this.prop];return e&&e.get?e.get(this):tt.propHooks._default.get(this)},run:function(e){var t,n=tt.propHooks[this.prop];return this.options.duration?this.pos=t=w.easing[this.easing](e,this.options.duration*e,0,1,this.options.duration):this.pos=t=e,this.now=(this.end-this.start)*t+this.start,this.options.step&&this.options.step.call(this.elem,this.now,this),n&&n.set?n.set(this):tt.propHooks._default.set(this),this}},tt.prototype.init.prototype=tt.prototype,tt.propHooks={_default:{get:function(e){var t;return 1!==e.elem.nodeType||null!=e.elem[e.prop]&&null==e.elem.style[e.prop]?e.elem[e.prop]:(t=w.css(e.elem,e.prop,""))&&"auto"!==t?t:0},set:function(e){w.fx.step[e.prop]?w.fx.step[e.prop](e):1!==e.elem.nodeType||null==e.elem.style[w.cssProps[e.prop]]&&!w.cssHooks[e.prop]?e.elem[e.prop]=e.now:w.style(e.elem,e.prop,e.now+e.unit)}}},tt.propHooks.scrollTop=tt.propHooks.scrollLeft={set:function(e){e.elem.nodeType&&e.elem.parentNode&&(e.elem[e.prop]=e.now)}},w.easing={linear:function(e){return e},swing:function(e){return.5-Math.cos(e*Math.PI)/2},_default:"swing"},w.fx=tt.prototype.init,w.fx.step={};var nt,rt,it=/^(?:toggle|show|hide)$/,ot=/queueHooks$/;function at(){rt&&(!1===r.hidden&&e.requestAnimationFrame?e.requestAnimationFrame(at):e.setTimeout(at,w.fx.interval),w.fx.tick())}function st(){return e.setTimeout(function(){nt=void 0}),nt=Date.now()}function ut(e,t){var n,r=0,i={height:e};for(t=t?1:0;r<4;r+=2-t)i["margin"+(n=oe[r])]=i["padding"+n]=e;return t&&(i.opacity=i.width=e),i}function lt(e,t,n){for(var r,i=(pt.tweeners[t]||[]).concat(pt.tweeners["*"]),o=0,a=i.length;o1)},removeAttr:function(e){return this.each(function(){w.removeAttr(this,e)})}}),w.extend({attr:function(e,t,n){var r,i,o=e.nodeType;if(3!==o&&8!==o&&2!==o)return"undefined"==typeof e.getAttribute?w.prop(e,t,n):(1===o&&w.isXMLDoc(e)||(i=w.attrHooks[t.toLowerCase()]||(w.expr.match.bool.test(t)?dt:void 0)),void 0!==n?null===n?void w.removeAttr(e,t):i&&"set"in i&&void 0!==(r=i.set(e,n,t))?r:(e.setAttribute(t,n+""),n):i&&"get"in i&&null!==(r=i.get(e,t))?r:null==(r=w.find.attr(e,t))?void 0:r)},attrHooks:{type:{set:function(e,t){if(!h.radioValue&&"radio"===t&&N(e,"input")){var n=e.value;return e.setAttribute("type",t),n&&(e.value=n),t}}}},removeAttr:function(e,t){var n,r=0,i=t&&t.match(M);if(i&&1===e.nodeType)while(n=i[r++])e.removeAttribute(n)}}),dt={set:function(e,t,n){return!1===t?w.removeAttr(e,n):e.setAttribute(n,n),n}},w.each(w.expr.match.bool.source.match(/\w+/g),function(e,t){var n=ht[t]||w.find.attr;ht[t]=function(e,t,r){var i,o,a=t.toLowerCase();return r||(o=ht[a],ht[a]=i,i=null!=n(e,t,r)?a:null,ht[a]=o),i}});var gt=/^(?:input|select|textarea|button)$/i,yt=/^(?:a|area)$/i;w.fn.extend({prop:function(e,t){return z(this,w.prop,e,t,arguments.length>1)},removeProp:function(e){return this.each(function(){delete this[w.propFix[e]||e]})}}),w.extend({prop:function(e,t,n){var r,i,o=e.nodeType;if(3!==o&&8!==o&&2!==o)return 1===o&&w.isXMLDoc(e)||(t=w.propFix[t]||t,i=w.propHooks[t]),void 0!==n?i&&"set"in i&&void 0!==(r=i.set(e,n,t))?r:e[t]=n:i&&"get"in i&&null!==(r=i.get(e,t))?r:e[t]},propHooks:{tabIndex:{get:function(e){var t=w.find.attr(e,"tabindex");return t?parseInt(t,10):gt.test(e.nodeName)||yt.test(e.nodeName)&&e.href?0:-1}}},propFix:{"for":"htmlFor","class":"className"}}),h.optSelected||(w.propHooks.selected={get:function(e){var t=e.parentNode;return t&&t.parentNode&&t.parentNode.selectedIndex,null},set:function(e){var t=e.parentNode;t&&(t.selectedIndex,t.parentNode&&t.parentNode.selectedIndex)}}),w.each(["tabIndex","readOnly","maxLength","cellSpacing","cellPadding","rowSpan","colSpan","useMap","frameBorder","contentEditable"],function(){w.propFix[this.toLowerCase()]=this});function vt(e){return(e.match(M)||[]).join(" ")}function mt(e){return e.getAttribute&&e.getAttribute("class")||""}function xt(e){return Array.isArray(e)?e:"string"==typeof e?e.match(M)||[]:[]}w.fn.extend({addClass:function(e){var t,n,r,i,o,a,s,u=0;if(g(e))return this.each(function(t){w(this).addClass(e.call(this,t,mt(this)))});if((t=xt(e)).length)while(n=this[u++])if(i=mt(n),r=1===n.nodeType&&" "+vt(i)+" "){a=0;while(o=t[a++])r.indexOf(" "+o+" ")<0&&(r+=o+" ");i!==(s=vt(r))&&n.setAttribute("class",s)}return this},removeClass:function(e){var t,n,r,i,o,a,s,u=0;if(g(e))return this.each(function(t){w(this).removeClass(e.call(this,t,mt(this)))});if(!arguments.length)return this.attr("class","");if((t=xt(e)).length)while(n=this[u++])if(i=mt(n),r=1===n.nodeType&&" "+vt(i)+" "){a=0;while(o=t[a++])while(r.indexOf(" "+o+" ")>-1)r=r.replace(" "+o+" "," ");i!==(s=vt(r))&&n.setAttribute("class",s)}return this},toggleClass:function(e,t){var n=typeof e,r="string"===n||Array.isArray(e);return"boolean"==typeof t&&r?t?this.addClass(e):this.removeClass(e):g(e)?this.each(function(n){w(this).toggleClass(e.call(this,n,mt(this),t),t)}):this.each(function(){var t,i,o,a;if(r){i=0,o=w(this),a=xt(e);while(t=a[i++])o.hasClass(t)?o.removeClass(t):o.addClass(t)}else void 0!==e&&"boolean"!==n||((t=mt(this))&&J.set(this,"__className__",t),this.setAttribute&&this.setAttribute("class",t||!1===e?"":J.get(this,"__className__")||""))})},hasClass:function(e){var t,n,r=0;t=" "+e+" ";while(n=this[r++])if(1===n.nodeType&&(" "+vt(mt(n))+" ").indexOf(t)>-1)return!0;return!1}});var bt=/\r/g;w.fn.extend({val:function(e){var t,n,r,i=this[0];{if(arguments.length)return r=g(e),this.each(function(n){var i;1===this.nodeType&&(null==(i=r?e.call(this,n,w(this).val()):e)?i="":"number"==typeof i?i+="":Array.isArray(i)&&(i=w.map(i,function(e){return null==e?"":e+""})),(t=w.valHooks[this.type]||w.valHooks[this.nodeName.toLowerCase()])&&"set"in t&&void 0!==t.set(this,i,"value")||(this.value=i))});if(i)return(t=w.valHooks[i.type]||w.valHooks[i.nodeName.toLowerCase()])&&"get"in t&&void 0!==(n=t.get(i,"value"))?n:"string"==typeof(n=i.value)?n.replace(bt,""):null==n?"":n}}}),w.extend({valHooks:{option:{get:function(e){var t=w.find.attr(e,"value");return null!=t?t:vt(w.text(e))}},select:{get:function(e){var t,n,r,i=e.options,o=e.selectedIndex,a="select-one"===e.type,s=a?null:[],u=a?o+1:i.length;for(r=o<0?u:a?o:0;r-1)&&(n=!0);return n||(e.selectedIndex=-1),o}}}}),w.each(["radio","checkbox"],function(){w.valHooks[this]={set:function(e,t){if(Array.isArray(t))return e.checked=w.inArray(w(e).val(),t)>-1}},h.checkOn||(w.valHooks[this].get=function(e){return null===e.getAttribute("value")?"on":e.value})}),h.focusin="onfocusin"in e;var wt=/^(?:focusinfocus|focusoutblur)$/,Tt=function(e){e.stopPropagation()};w.extend(w.event,{trigger:function(t,n,i,o){var a,s,u,l,c,p,d,h,v=[i||r],m=f.call(t,"type")?t.type:t,x=f.call(t,"namespace")?t.namespace.split("."):[];if(s=h=u=i=i||r,3!==i.nodeType&&8!==i.nodeType&&!wt.test(m+w.event.triggered)&&(m.indexOf(".")>-1&&(m=(x=m.split(".")).shift(),x.sort()),c=m.indexOf(":")<0&&"on"+m,t=t[w.expando]?t:new w.Event(m,"object"==typeof t&&t),t.isTrigger=o?2:3,t.namespace=x.join("."),t.rnamespace=t.namespace?new RegExp("(^|\\.)"+x.join("\\.(?:.*\\.|)")+"(\\.|$)"):null,t.result=void 0,t.target||(t.target=i),n=null==n?[t]:w.makeArray(n,[t]),d=w.event.special[m]||{},o||!d.trigger||!1!==d.trigger.apply(i,n))){if(!o&&!d.noBubble&&!y(i)){for(l=d.delegateType||m,wt.test(l+m)||(s=s.parentNode);s;s=s.parentNode)v.push(s),u=s;u===(i.ownerDocument||r)&&v.push(u.defaultView||u.parentWindow||e)}a=0;while((s=v[a++])&&!t.isPropagationStopped())h=s,t.type=a>1?l:d.bindType||m,(p=(J.get(s,"events")||{})[t.type]&&J.get(s,"handle"))&&p.apply(s,n),(p=c&&s[c])&&p.apply&&Y(s)&&(t.result=p.apply(s,n),!1===t.result&&t.preventDefault());return t.type=m,o||t.isDefaultPrevented()||d._default&&!1!==d._default.apply(v.pop(),n)||!Y(i)||c&&g(i[m])&&!y(i)&&((u=i[c])&&(i[c]=null),w.event.triggered=m,t.isPropagationStopped()&&h.addEventListener(m,Tt),i[m](),t.isPropagationStopped()&&h.removeEventListener(m,Tt),w.event.triggered=void 0,u&&(i[c]=u)),t.result}},simulate:function(e,t,n){var r=w.extend(new w.Event,n,{type:e,isSimulated:!0});w.event.trigger(r,null,t)}}),w.fn.extend({trigger:function(e,t){return this.each(function(){w.event.trigger(e,t,this)})},triggerHandler:function(e,t){var n=this[0];if(n)return w.event.trigger(e,t,n,!0)}}),h.focusin||w.each({focus:"focusin",blur:"focusout"},function(e,t){var n=function(e){w.event.simulate(t,e.target,w.event.fix(e))};w.event.special[t]={setup:function(){var r=this.ownerDocument||this,i=J.access(r,t);i||r.addEventListener(e,n,!0),J.access(r,t,(i||0)+1)},teardown:function(){var r=this.ownerDocument||this,i=J.access(r,t)-1;i?J.access(r,t,i):(r.removeEventListener(e,n,!0),J.remove(r,t))}}});var Ct=e.location,Et=Date.now(),kt=/\?/;w.parseXML=function(t){var n;if(!t||"string"!=typeof t)return null;try{n=(new e.DOMParser).parseFromString(t,"text/xml")}catch(e){n=void 0}return n&&!n.getElementsByTagName("parsererror").length||w.error("Invalid XML: "+t),n};var St=/\[\]$/,Dt=/\r?\n/g,Nt=/^(?:submit|button|image|reset|file)$/i,At=/^(?:input|select|textarea|keygen)/i;function jt(e,t,n,r){var i;if(Array.isArray(t))w.each(t,function(t,i){n||St.test(e)?r(e,i):jt(e+"["+("object"==typeof i&&null!=i?t:"")+"]",i,n,r)});else if(n||"object"!==x(t))r(e,t);else for(i in t)jt(e+"["+i+"]",t[i],n,r)}w.param=function(e,t){var n,r=[],i=function(e,t){var n=g(t)?t():t;r[r.length]=encodeURIComponent(e)+"="+encodeURIComponent(null==n?"":n)};if(Array.isArray(e)||e.jquery&&!w.isPlainObject(e))w.each(e,function(){i(this.name,this.value)});else for(n in e)jt(n,e[n],t,i);return r.join("&")},w.fn.extend({serialize:function(){return w.param(this.serializeArray())},serializeArray:function(){return this.map(function(){var e=w.prop(this,"elements");return e?w.makeArray(e):this}).filter(function(){var e=this.type;return this.name&&!w(this).is(":disabled")&&At.test(this.nodeName)&&!Nt.test(e)&&(this.checked||!pe.test(e))}).map(function(e,t){var n=w(this).val();return null==n?null:Array.isArray(n)?w.map(n,function(e){return{name:t.name,value:e.replace(Dt,"\r\n")}}):{name:t.name,value:n.replace(Dt,"\r\n")}}).get()}});var qt=/%20/g,Lt=/#.*$/,Ht=/([?&])_=[^&]*/,Ot=/^(.*?):[ \t]*([^\r\n]*)$/gm,Pt=/^(?:about|app|app-storage|.+-extension|file|res|widget):$/,Mt=/^(?:GET|HEAD)$/,Rt=/^\/\//,It={},Wt={},$t="*/".concat("*"),Bt=r.createElement("a");Bt.href=Ct.href;function Ft(e){return function(t,n){"string"!=typeof t&&(n=t,t="*");var r,i=0,o=t.toLowerCase().match(M)||[];if(g(n))while(r=o[i++])"+"===r[0]?(r=r.slice(1)||"*",(e[r]=e[r]||[]).unshift(n)):(e[r]=e[r]||[]).push(n)}}function _t(e,t,n,r){var i={},o=e===Wt;function a(s){var u;return i[s]=!0,w.each(e[s]||[],function(e,s){var l=s(t,n,r);return"string"!=typeof l||o||i[l]?o?!(u=l):void 0:(t.dataTypes.unshift(l),a(l),!1)}),u}return a(t.dataTypes[0])||!i["*"]&&a("*")}function zt(e,t){var n,r,i=w.ajaxSettings.flatOptions||{};for(n in t)void 0!==t[n]&&((i[n]?e:r||(r={}))[n]=t[n]);return r&&w.extend(!0,e,r),e}function Xt(e,t,n){var r,i,o,a,s=e.contents,u=e.dataTypes;while("*"===u[0])u.shift(),void 0===r&&(r=e.mimeType||t.getResponseHeader("Content-Type"));if(r)for(i in s)if(s[i]&&s[i].test(r)){u.unshift(i);break}if(u[0]in n)o=u[0];else{for(i in n){if(!u[0]||e.converters[i+" "+u[0]]){o=i;break}a||(a=i)}o=o||a}if(o)return o!==u[0]&&u.unshift(o),n[o]}function Ut(e,t,n,r){var i,o,a,s,u,l={},c=e.dataTypes.slice();if(c[1])for(a in e.converters)l[a.toLowerCase()]=e.converters[a];o=c.shift();while(o)if(e.responseFields[o]&&(n[e.responseFields[o]]=t),!u&&r&&e.dataFilter&&(t=e.dataFilter(t,e.dataType)),u=o,o=c.shift())if("*"===o)o=u;else if("*"!==u&&u!==o){if(!(a=l[u+" "+o]||l["* "+o]))for(i in l)if((s=i.split(" "))[1]===o&&(a=l[u+" "+s[0]]||l["* "+s[0]])){!0===a?a=l[i]:!0!==l[i]&&(o=s[0],c.unshift(s[1]));break}if(!0!==a)if(a&&e["throws"])t=a(t);else try{t=a(t)}catch(e){return{state:"parsererror",error:a?e:"No conversion from "+u+" to "+o}}}return{state:"success",data:t}}w.extend({active:0,lastModified:{},etag:{},ajaxSettings:{url:Ct.href,type:"GET",isLocal:Pt.test(Ct.protocol),global:!0,processData:!0,async:!0,contentType:"application/x-www-form-urlencoded; charset=UTF-8",accepts:{"*":$t,text:"text/plain",html:"text/html",xml:"application/xml, text/xml",json:"application/json, text/javascript"},contents:{xml:/\bxml\b/,html:/\bhtml/,json:/\bjson\b/},responseFields:{xml:"responseXML",text:"responseText",json:"responseJSON"},converters:{"* text":String,"text html":!0,"text json":JSON.parse,"text xml":w.parseXML},flatOptions:{url:!0,context:!0}},ajaxSetup:function(e,t){return t?zt(zt(e,w.ajaxSettings),t):zt(w.ajaxSettings,e)},ajaxPrefilter:Ft(It),ajaxTransport:Ft(Wt),ajax:function(t,n){"object"==typeof t&&(n=t,t=void 0),n=n||{};var i,o,a,s,u,l,c,f,p,d,h=w.ajaxSetup({},n),g=h.context||h,y=h.context&&(g.nodeType||g.jquery)?w(g):w.event,v=w.Deferred(),m=w.Callbacks("once memory"),x=h.statusCode||{},b={},T={},C="canceled",E={readyState:0,getResponseHeader:function(e){var t;if(c){if(!s){s={};while(t=Ot.exec(a))s[t[1].toLowerCase()]=t[2]}t=s[e.toLowerCase()]}return null==t?null:t},getAllResponseHeaders:function(){return c?a:null},setRequestHeader:function(e,t){return null==c&&(e=T[e.toLowerCase()]=T[e.toLowerCase()]||e,b[e]=t),this},overrideMimeType:function(e){return null==c&&(h.mimeType=e),this},statusCode:function(e){var t;if(e)if(c)E.always(e[E.status]);else for(t in e)x[t]=[x[t],e[t]];return this},abort:function(e){var t=e||C;return i&&i.abort(t),k(0,t),this}};if(v.promise(E),h.url=((t||h.url||Ct.href)+"").replace(Rt,Ct.protocol+"//"),h.type=n.method||n.type||h.method||h.type,h.dataTypes=(h.dataType||"*").toLowerCase().match(M)||[""],null==h.crossDomain){l=r.createElement("a");try{l.href=h.url,l.href=l.href,h.crossDomain=Bt.protocol+"//"+Bt.host!=l.protocol+"//"+l.host}catch(e){h.crossDomain=!0}}if(h.data&&h.processData&&"string"!=typeof h.data&&(h.data=w.param(h.data,h.traditional)),_t(It,h,n,E),c)return E;(f=w.event&&h.global)&&0==w.active++&&w.event.trigger("ajaxStart"),h.type=h.type.toUpperCase(),h.hasContent=!Mt.test(h.type),o=h.url.replace(Lt,""),h.hasContent?h.data&&h.processData&&0===(h.contentType||"").indexOf("application/x-www-form-urlencoded")&&(h.data=h.data.replace(qt,"+")):(d=h.url.slice(o.length),h.data&&(h.processData||"string"==typeof h.data)&&(o+=(kt.test(o)?"&":"?")+h.data,delete h.data),!1===h.cache&&(o=o.replace(Ht,"$1"),d=(kt.test(o)?"&":"?")+"_="+Et+++d),h.url=o+d),h.ifModified&&(w.lastModified[o]&&E.setRequestHeader("If-Modified-Since",w.lastModified[o]),w.etag[o]&&E.setRequestHeader("If-None-Match",w.etag[o])),(h.data&&h.hasContent&&!1!==h.contentType||n.contentType)&&E.setRequestHeader("Content-Type",h.contentType),E.setRequestHeader("Accept",h.dataTypes[0]&&h.accepts[h.dataTypes[0]]?h.accepts[h.dataTypes[0]]+("*"!==h.dataTypes[0]?", "+$t+"; q=0.01":""):h.accepts["*"]);for(p in h.headers)E.setRequestHeader(p,h.headers[p]);if(h.beforeSend&&(!1===h.beforeSend.call(g,E,h)||c))return E.abort();if(C="abort",m.add(h.complete),E.done(h.success),E.fail(h.error),i=_t(Wt,h,n,E)){if(E.readyState=1,f&&y.trigger("ajaxSend",[E,h]),c)return E;h.async&&h.timeout>0&&(u=e.setTimeout(function(){E.abort("timeout")},h.timeout));try{c=!1,i.send(b,k)}catch(e){if(c)throw e;k(-1,e)}}else k(-1,"No Transport");function k(t,n,r,s){var l,p,d,b,T,C=n;c||(c=!0,u&&e.clearTimeout(u),i=void 0,a=s||"",E.readyState=t>0?4:0,l=t>=200&&t<300||304===t,r&&(b=Xt(h,E,r)),b=Ut(h,b,E,l),l?(h.ifModified&&((T=E.getResponseHeader("Last-Modified"))&&(w.lastModified[o]=T),(T=E.getResponseHeader("etag"))&&(w.etag[o]=T)),204===t||"HEAD"===h.type?C="nocontent":304===t?C="notmodified":(C=b.state,p=b.data,l=!(d=b.error))):(d=C,!t&&C||(C="error",t<0&&(t=0))),E.status=t,E.statusText=(n||C)+"",l?v.resolveWith(g,[p,C,E]):v.rejectWith(g,[E,C,d]),E.statusCode(x),x=void 0,f&&y.trigger(l?"ajaxSuccess":"ajaxError",[E,h,l?p:d]),m.fireWith(g,[E,C]),f&&(y.trigger("ajaxComplete",[E,h]),--w.active||w.event.trigger("ajaxStop")))}return E},getJSON:function(e,t,n){return w.get(e,t,n,"json")},getScript:function(e,t){return w.get(e,void 0,t,"script")}}),w.each(["get","post"],function(e,t){w[t]=function(e,n,r,i){return g(n)&&(i=i||r,r=n,n=void 0),w.ajax(w.extend({url:e,type:t,dataType:i,data:n,success:r},w.isPlainObject(e)&&e))}}),w._evalUrl=function(e){return w.ajax({url:e,type:"GET",dataType:"script",cache:!0,async:!1,global:!1,"throws":!0})},w.fn.extend({wrapAll:function(e){var t;return this[0]&&(g(e)&&(e=e.call(this[0])),t=w(e,this[0].ownerDocument).eq(0).clone(!0),this[0].parentNode&&t.insertBefore(this[0]),t.map(function(){var e=this;while(e.firstElementChild)e=e.firstElementChild;return e}).append(this)),this},wrapInner:function(e){return g(e)?this.each(function(t){w(this).wrapInner(e.call(this,t))}):this.each(function(){var t=w(this),n=t.contents();n.length?n.wrapAll(e):t.append(e)})},wrap:function(e){var t=g(e);return this.each(function(n){w(this).wrapAll(t?e.call(this,n):e)})},unwrap:function(e){return this.parent(e).not("body").each(function(){w(this).replaceWith(this.childNodes)}),this}}),w.expr.pseudos.hidden=function(e){return!w.expr.pseudos.visible(e)},w.expr.pseudos.visible=function(e){return!!(e.offsetWidth||e.offsetHeight||e.getClientRects().length)},w.ajaxSettings.xhr=function(){try{return new e.XMLHttpRequest}catch(e){}};var Vt={0:200,1223:204},Gt=w.ajaxSettings.xhr();h.cors=!!Gt&&"withCredentials"in Gt,h.ajax=Gt=!!Gt,w.ajaxTransport(function(t){var n,r;if(h.cors||Gt&&!t.crossDomain)return{send:function(i,o){var a,s=t.xhr();if(s.open(t.type,t.url,t.async,t.username,t.password),t.xhrFields)for(a in t.xhrFields)s[a]=t.xhrFields[a];t.mimeType&&s.overrideMimeType&&s.overrideMimeType(t.mimeType),t.crossDomain||i["X-Requested-With"]||(i["X-Requested-With"]="XMLHttpRequest");for(a in i)s.setRequestHeader(a,i[a]);n=function(e){return function(){n&&(n=r=s.onload=s.onerror=s.onabort=s.ontimeout=s.onreadystatechange=null,"abort"===e?s.abort():"error"===e?"number"!=typeof s.status?o(0,"error"):o(s.status,s.statusText):o(Vt[s.status]||s.status,s.statusText,"text"!==(s.responseType||"text")||"string"!=typeof s.responseText?{binary:s.response}:{text:s.responseText},s.getAllResponseHeaders()))}},s.onload=n(),r=s.onerror=s.ontimeout=n("error"),void 0!==s.onabort?s.onabort=r:s.onreadystatechange=function(){4===s.readyState&&e.setTimeout(function(){n&&r()})},n=n("abort");try{s.send(t.hasContent&&t.data||null)}catch(e){if(n)throw e}},abort:function(){n&&n()}}}),w.ajaxPrefilter(function(e){e.crossDomain&&(e.contents.script=!1)}),w.ajaxSetup({accepts:{script:"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"},contents:{script:/\b(?:java|ecma)script\b/},converters:{"text script":function(e){return w.globalEval(e),e}}}),w.ajaxPrefilter("script",function(e){void 0===e.cache&&(e.cache=!1),e.crossDomain&&(e.type="GET")}),w.ajaxTransport("script",function(e){if(e.crossDomain){var t,n;return{send:function(i,o){t=w("