Skip to content

Commit f692782

Browse files
authored
Merge pull request #516 from cntrump/pr_spm_support
Add Swift Package Manager support
2 parents 8355b88 + 9b6d7d5 commit f692782

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

Package.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// swift-tools-version:5.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "tommath",
8+
platforms: [
9+
.macOS(.v10_10), .iOS(.v9), .tvOS(.v9)
10+
],
11+
products: [
12+
// Products define the executables and libraries a package produces, and make them visible to other packages.
13+
.library(
14+
name: "libtommath",
15+
targets: ["libtommath"])
16+
],
17+
dependencies: [
18+
// Dependencies declare other packages that this package depends on.
19+
// .package(url: /* package url */, from: "1.0.0"),
20+
],
21+
targets: [
22+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
23+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
24+
.target(
25+
name: "libtommath",
26+
path: ".",
27+
exclude: ["demo", "doc", "etc", "logs", "mtest"],
28+
sources: ["."],
29+
publicHeadersPath: "modulemap",
30+
cSettings: [
31+
.headerSearchPath("."),
32+
.unsafeFlags(["-flto=thin"]) // for Dead Code Elimination
33+
])
34+
],
35+
cLanguageStandard: .gnu11,
36+
cxxLanguageStandard: .gnucxx14
37+
)

demo/tommath_tests.swift

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import XCTest
2+
import libtommath
3+
4+
/* ---> Basic Manipulations <--- */
5+
6+
extension mp_int {
7+
var isZero: Bool { used == 0 }
8+
var isNeg: Bool { sign == MP_NEG }
9+
var isEven: Bool { used == 0 || (dp[0] & 1) == 0 }
10+
var isOdd: Bool { !isEven }
11+
}
12+
13+
func mp_get_u32(_ a: UnsafePointer<mp_int>) -> UInt32 {
14+
return UInt32(bitPattern: mp_get_i32(a))
15+
}
16+
17+
class LibTommathTests: XCTestCase {
18+
19+
override func setUpWithError() throws {
20+
// Put setup code here. This method is called before the invocation of each test method in the class.
21+
}
22+
23+
override func tearDownWithError() throws {
24+
// Put teardown code here. This method is called after the invocation of each test method in the class.
25+
}
26+
27+
func testTrivialStuff() throws {
28+
var a = mp_int()
29+
var b = mp_int()
30+
var c = mp_int()
31+
var d = mp_int()
32+
33+
XCTAssertEqual(mp_init(&a), MP_OKAY)
34+
XCTAssertEqual(mp_init(&b), MP_OKAY)
35+
XCTAssertEqual(mp_init(&c), MP_OKAY)
36+
XCTAssertEqual(mp_init(&d), MP_OKAY)
37+
38+
defer {
39+
mp_clear(&a)
40+
mp_clear(&b)
41+
mp_clear(&c)
42+
mp_clear(&d)
43+
}
44+
45+
XCTAssert(mp_error_to_string(MP_OKAY) != nil)
46+
47+
/* a: 0->5 */
48+
mp_set(&a, 5)
49+
/* a: 5-> b: -5 */
50+
XCTAssertEqual(mp_neg(&a, &b), MP_OKAY)
51+
XCTAssertEqual(mp_cmp(&a, &b), MP_GT)
52+
XCTAssertEqual(mp_cmp(&b, &a), MP_LT)
53+
XCTAssertTrue(b.isNeg)
54+
/* a: 5-> a: -5 */
55+
var t = a // Fix compiler error: Overlapping accesses to 'a', but modification requires exclusive access; consider copying to a local variable
56+
XCTAssertEqual(mp_neg(&t, &a), MP_OKAY)
57+
XCTAssertEqual(mp_cmp(&b, &a), MP_EQ)
58+
XCTAssertTrue(a.isNeg)
59+
/* a: -5-> b: 5 */
60+
XCTAssertEqual(mp_abs(&a, &b), MP_OKAY)
61+
XCTAssertTrue(!b.isNeg)
62+
/* a: -5-> b: -4 */
63+
XCTAssertEqual(mp_add_d(&a, 1, &b), MP_OKAY)
64+
XCTAssertTrue(b.isNeg)
65+
XCTAssertEqual(mp_get_i32(&b), -4)
66+
XCTAssertEqual(mp_get_u32(&b), UInt32(bitPattern: -4))
67+
XCTAssertEqual(mp_get_mag_u32(&b), 4)
68+
/* a: -5-> b: 1 */
69+
XCTAssertEqual(mp_add_d(&a, 6, &b), MP_OKAY)
70+
XCTAssertEqual(mp_get_u32(&b), 1)
71+
/* a: -5-> a: 1 */
72+
t = a
73+
XCTAssertEqual(mp_add_d(&t, 6, &a), MP_OKAY)
74+
XCTAssertEqual(mp_get_u32(&a), 1)
75+
mp_zero(&a);
76+
/* a: 0-> a: 6 */
77+
t = a
78+
XCTAssertEqual(mp_add_d(&t, 6, &a), MP_OKAY)
79+
XCTAssertEqual(mp_get_u32(&a), 6)
80+
81+
mp_set(&a, 42)
82+
mp_set(&b, 1)
83+
t = b
84+
XCTAssertEqual(mp_neg(&t, &b), MP_OKAY)
85+
mp_set(&c, 1)
86+
XCTAssertEqual(mp_exptmod(&a, &b, &c, &d), MP_OKAY)
87+
88+
mp_set(&c, 7)
89+
/* same here */
90+
XCTAssertTrue(mp_exptmod(&a, &b, &c, &d) != MP_OKAY)
91+
92+
XCTAssertTrue(a.isEven != a.isOdd)
93+
}
94+
}

modulemap/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module libtommath [extern_c] {
2+
header "../tommath.h"
3+
export *
4+
}

0 commit comments

Comments
 (0)