Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouke committed Aug 28, 2016
0 parents commit f127fd6
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (C) 2016 Bouke Haarsma

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import PackageDescription

let package = Package(
name: "Bignum",
dependencies: [
.Package(url: "https://github.com/Bouke/COpenSSL.git", majorVersion: 1),
]
)
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Swift big number library
========================

A big number library for Swift, backed by OpenSSL. Not all methods are currently provided, but can be added if needed.

let result = Bignum("2") + Bignum("3") * Bignum("4") / Bignum("2")
assert(result == Bignum("8"))
116 changes: 116 additions & 0 deletions Sources/Bignum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import COpenSSL
import Foundation

public class Bignum {
internal let ctx: UnsafeMutablePointer<BIGNUM>

init() {
ctx = BN_new()
}

init(ctx: UnsafeMutablePointer<BIGNUM>) {
self.ctx = ctx
}

public init(_ dec: String) {
var ctx: UnsafeMutablePointer<BIGNUM>? = nil
BN_dec2bn(&ctx, dec)
self.ctx = ctx!
}

public init(hex: String) {
var ctx: UnsafeMutablePointer<BIGNUM>? = nil
BN_hex2bn(&ctx, hex)
self.ctx = ctx!
}

public convenience init(data: Data) {
self.init()
_ = data.withUnsafeBytes { pData in
BN_bin2bn(pData, Int32(data.count), ctx)
}
}

deinit {
BN_free(ctx)
}

public var data: Data {
var data = Data(count: Int((BN_num_bits(ctx) + 7) / 8))
_ = data.withUnsafeMutableBytes { pData in
BN_bn2bin(ctx, pData)
}
return data
}

public var dec: String {
return String(validatingUTF8: BN_bn2dec(ctx))!
}

public var hex: String {
return String(validatingUTF8: BN_bn2hex(ctx))!
}
}

extension Bignum: CustomStringConvertible {
public var description: String {
return dec
}
}

extension Bignum: Comparable {
public static func == (lhs: Bignum, rhs: Bignum) -> Bool {
return BN_cmp(lhs.ctx, rhs.ctx) == 0
}

public static func < (lhs: Bignum, rhs: Bignum) -> Bool {
return BN_cmp(lhs.ctx, rhs.ctx) == -1
}
}

internal let ctx = BN_CTX_new()

func operation(_ block: (_ result: Bignum) -> Int32) -> Bignum {
let result = Bignum()
precondition(block(result) == 1)
return result
}

/// Returns: (a ** b) % N
public func mod_exp(_ a: Bignum, _ p: Bignum, _ m: Bignum) -> Bignum {
return operation {
BN_mod_exp($0.ctx, a.ctx, p.ctx, m.ctx, ctx)
}
}

public func * (lhs: Bignum, rhs: Bignum) -> Bignum {
return operation {
BN_mul($0.ctx, lhs.ctx, rhs.ctx, ctx)
}
}

public func + (lhs: Bignum, rhs: Bignum) -> Bignum {
return operation {
BN_add($0.ctx, lhs.ctx, rhs.ctx)
}
}

public func - (lhs: Bignum, rhs: Bignum) -> Bignum {
return operation {
BN_sub($0.ctx, lhs.ctx, rhs.ctx)
}
}

/// Returns lhs / rhs, rounded to zero.
public func / (lhs: Bignum, rhs: Bignum) -> Bignum {
return operation {
BN_div($0.ctx, nil, lhs.ctx, rhs.ctx, ctx)
}
}

/// Returns: (a + b) % N
public func mod_add(_ a: Bignum, _ b: Bignum, _ m: Bignum) -> Bignum {
return operation {
BN_mod_add($0.ctx, a.ctx, b.ctx, m.ctx, ctx)
}
}
45 changes: 45 additions & 0 deletions Tests/BignumTests/BignumTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import XCTest
@testable import Bignum

class BignumTests: XCTestCase {
func testAddition() {
XCTAssertEqual(Bignum("1") + Bignum("2"), Bignum("3"))
}

func testSubtraction() {
XCTAssertEqual(Bignum("5") - Bignum("3"), Bignum("2"))
}

func testMultiplication() {
XCTAssertEqual(Bignum("2") * Bignum("3"), Bignum("6"))
}

func testDivision() {
XCTAssertEqual(Bignum("10") / Bignum("5"), Bignum("2"))
}

func testModExp() {
XCTAssertEqual(mod_exp(Bignum("5"), Bignum("8"), Bignum("13")), Bignum("1"))
}

func testModAdd() {
XCTAssertEqual(mod_exp(Bignum("5"), Bignum("7"), Bignum("13")), Bignum("8"))
}

func testReadme() {
let result = Bignum("2") + Bignum("3") * Bignum("4") / Bignum("2")
assert(result == Bignum("8"))
}

static var allTests : [(String, (BignumTests) -> () throws -> Void)] {
return [
("testAddition", testAddition),
("testSubtraction", testSubtraction),
("testMultiplication", testMultiplication),
("testDivision", testDivision),
("testModExp", testModExp),
("testModAdd", testModAdd),
("testReadme", testReadme)
]
}
}
6 changes: 6 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import XCTest
@testable import BignumTests

XCTMain([
testCase(BignumTests.allTests),
])

0 comments on commit f127fd6

Please sign in to comment.