Skip to content

Commit

Permalink
Temporarily removed StaticBigInt support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgriebling committed Jul 6, 2023
1 parent a2b11d4 commit 61fa088
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 62 deletions.
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import PackageDescription

let package = Package(
name: "BigInt",
platforms: [
.macOS("13.3"), .iOS("16.4"), .macCatalyst(.v16), .tvOS("16.4"),
.watchOS("9.4")
],
// platforms: [
// .macOS("13.3"), .iOS("16.4"), .macCatalyst(.v16), .tvOS("16.4"),
// .watchOS("9.4")
// ],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand Down
53 changes: 31 additions & 22 deletions Sources/BigInt/BigInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,27 +288,28 @@ public struct BInt: CustomStringConvertible, Comparable, Equatable, Hashable, Co
extension BInt : SignedInteger { }

/// Add support for `StaticBigInt` - 24 Jun 2023 - MG
extension BInt : ExpressibleByIntegerLiteral {
public init(integerLiteral value: StaticBigInt) {
let isNegative = value.signum() < 0
let bitWidth = value.bitWidth
if bitWidth < Int.bitWidth {
self.init(Int(bitPattern: value[0]))
} else {
precondition(value[0].bitWidth == 64, "Requires 64-bit Ints!")
let noOfWords = (bitWidth / 64) + 1 // must be 64-bit system
var words = Limbs()
for index in 0..<noOfWords {
// StaticBigInt words are 2's complement so negative
// values needed to be inverted and have one added
if isNegative { words.append(UInt64(~value[index])) }
else { words.append(UInt64(value[index])) }
}
self.init(words, false)
if isNegative { self += 1; self.negate() }
}
}
}
//@available(macOS 13.3, *)
//extension BInt : ExpressibleByIntegerLiteral {
// public init(integerLiteral value: StaticBigInt) {
// let isNegative = value.signum() < 0
// let bitWidth = value.bitWidth
// if bitWidth < Int.bitWidth {
// self.init(Int(bitPattern: value[0]))
// } else {
// precondition(value[0].bitWidth == 64, "Requires 64-bit Ints!")
// let noOfWords = (bitWidth / 64) + 1 // must be 64-bit system
// var words = Limbs()
// for index in 0..<noOfWords {
// // StaticBigInt words are 2's complement so negative
// // values needed to be inverted and have one added
// if isNegative { words.append(UInt64(~value[index])) }
// else { words.append(UInt64(value[index])) }
// }
// self.init(words, false)
// if isNegative { self += 1; self.negate() }
// }
// }
//}

/// Add support for `ExpressibleByStringLiteral` protocol - 24 Jun 2023 - MG
extension BInt : ExpressibleByStringLiteral {
Expand All @@ -321,7 +322,13 @@ extension BInt : ExpressibleByStringLiteral {
}
}

extension BInt : Numeric { // Also implies AdditiveArithmetic compliance
extension BInt : Numeric {

public init(integerLiteral value: Int) {
self.init(value)
}

// Also implies AdditiveArithmetic compliance
/// Compliance to this protocol enables BInt to use algorithms that
/// have been developed with a Numeric protocol generic constraint.

Expand All @@ -347,6 +354,8 @@ extension BInt : Numeric { // Also implies AdditiveArithmetic compliance
self.init(bwords, false)
if isNegative { self += 1; self.negate() }
}

// public init(_ source: Int) { self.init(exactly: source)! }
}

extension BInt : BinaryInteger {
Expand Down
30 changes: 12 additions & 18 deletions Tests/BigIntTests/ConstructorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,10 @@ class ConstructorTest: XCTestCase {
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(0xfffffffffffffffffffffffffffffffeffffffffffffffff))
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(0xfffffffffffffffffffffffffffffffeffffffffffffffff))
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)!)
}

func doTest6(_ n: Int) {
Expand Down Expand Up @@ -178,14 +172,14 @@ class ConstructorTest: XCTestCase {
}

// BInt from a StaticBigInt
func test8a() {
let bigNumber = BInt(1234567890_1234567890_1234567890_1234567890)
let nbigNumber = BInt(-1234567890_1234567890_1234567890_1234567890)
XCTAssertEqual(bigNumber.description,
"1234567890123456789012345678901234567890")
XCTAssertEqual(nbigNumber.description,
"-1234567890123456789012345678901234567890")
}
// func test8a() {
// let bigNumber = BInt(1234567890_1234567890_1234567890_1234567890)
// let nbigNumber = BInt(-1234567890_1234567890_1234567890_1234567890)
// XCTAssertEqual(bigNumber.description,
// "1234567890123456789012345678901234567890")
// XCTAssertEqual(nbigNumber.description,
// "-1234567890123456789012345678901234567890")
// }

let strings = ["0",
"10",
Expand Down
24 changes: 12 additions & 12 deletions Tests/BigIntTests/ExpModTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import XCTest

class ExpModTest: XCTestCase {

let a1 = BInt(43271512091896741409394736939900206368860872098952555939)
let x1 = BInt(42267688843543983783885633938053687583065584419295560555)
let m1 = BInt(65559338391610243479024015552681806795487318988418855174)
let mp1 = BInt(14708028296754883426561209612579760556999800024666797837)
let a1 = BInt("43271512091896741409394736939900206368860872098952555939")
let x1 = BInt("42267688843543983783885633938053687583065584419295560555")
let m1 = BInt("65559338391610243479024015552681806795487318988418855174")
let mp1 = BInt("14708028296754883426561209612579760556999800024666797837")

let a2 = BInt(9879959622001881798420480569351120749752891168795071469741009590796905186183625061410538508653929799901162907503196502223071902180994253404412067954774342232969326053454779870840130810532326017165678692636647404921424922403748460111140358572478743271512091896741409394736939900206368860872098952555939)
let x2 = BInt(4149842346989426807721754542711895351513161856205879655378968612408032038996656528445703209766166328006052965811745650295935314936056815509075542554308409615827636639641110901357012471113482422183741588797481891328204616583065067700486989814417842267688843543983783885633938053687583065584419295560555)
let m2 = BInt(10524302966485349118258764179820205386685991992586369700154893101599927732040662774460446149003080427232451962311367600902738242964142492968383265627950930467854069828393051189273332522792516344807937835132537814794042705435787095095919023768140765559338391610243479024015552681806795487318988418855174)
let mp2 = BInt(1122200972247120546333043544356752277213829455746835691791369400547247327000588079094470008395425401827849174141816902264513461023891163414270218627387835660351885298195540293936217720560009323739879363833688454142584347044964970051959361123182603999640496505409063560777205949747764493609693376510835)
let a2 = BInt("9879959622001881798420480569351120749752891168795071469741009590796905186183625061410538508653929799901162907503196502223071902180994253404412067954774342232969326053454779870840130810532326017165678692636647404921424922403748460111140358572478743271512091896741409394736939900206368860872098952555939")
let x2 = BInt("4149842346989426807721754542711895351513161856205879655378968612408032038996656528445703209766166328006052965811745650295935314936056815509075542554308409615827636639641110901357012471113482422183741588797481891328204616583065067700486989814417842267688843543983783885633938053687583065584419295560555")
let m2 = BInt("10524302966485349118258764179820205386685991992586369700154893101599927732040662774460446149003080427232451962311367600902738242964142492968383265627950930467854069828393051189273332522792516344807937835132537814794042705435787095095919023768140765559338391610243479024015552681806795487318988418855174")
let mp2 = BInt("1122200972247120546333043544356752277213829455746835691791369400547247327000588079094470008395425401827849174141816902264513461023891163414270218627387835660351885298195540293936217720560009323739879363833688454142584347044964970051959361123182603999640496505409063560777205949747764493609693376510835")

let a3 = BInt(2988348162058574136915891421498819466320163312926952423791023078876139)
let x3 = BInt(2351399303373464486466122544523690094744975233415544072992656881240319)
let m3 = BInt(10000000000000000000000000000000000000000)
let mp3 = BInt(1527229998585248450016808958343740453059)
let a3 = BInt("2988348162058574136915891421498819466320163312926952423791023078876139")
let x3 = BInt("2351399303373464486466122544523690094744975233415544072992656881240319")
let m3 = BInt("10000000000000000000000000000000000000000")
let mp3 = BInt("1527229998585248450016808958343740453059")

override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
Expand Down
Loading

0 comments on commit 61fa088

Please sign in to comment.