Skip to content

Commit 68e0102

Browse files
Added TomMath as a Swift Package. Lots taken from libtom/libtommath#518
0 parents  commit 68e0102

File tree

8 files changed

+153
-0
lines changed

8 files changed

+153
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/xcuserdata/*

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "vendor"]
2+
path = vendor
3+
url = [email protected]:libtom/libtommath.git

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Headers/module.modulemap

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

Headers/tommath.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "../vendor/tommath.h"

Package.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// swift-tools-version:5.0
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "TomMath",
6+
platforms: [
7+
.macOS(.v10_10), .iOS(.v9), .tvOS(.v9)
8+
],
9+
products: [
10+
.library(
11+
name: "TomMath",
12+
targets: [ "TomMath" ])
13+
],
14+
dependencies: [],
15+
targets: [
16+
.target(name: "TomMath",
17+
path: ".",
18+
exclude: [
19+
"vendor/demo",
20+
"vendor/doc",
21+
"vendor/etc",
22+
"vendor/logs",
23+
"vendor/mtest",
24+
"vendor/Package.swift",
25+
],
26+
sources: [
27+
"vendor"
28+
],
29+
publicHeadersPath: "Headers",
30+
cSettings: [
31+
.unsafeFlags(["-flto=thin"]) // for Dead Code Elimination
32+
]),
33+
.testTarget(name: "TomMathTests",
34+
dependencies: [
35+
"TomMath"
36+
],
37+
path: "Tests")
38+
],
39+
cLanguageStandard: .gnu11,
40+
cxxLanguageStandard: .gnucxx14
41+
)

Tests/TomMathTests.swift

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import XCTest
2+
import TomMath
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 TomMathTests: 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+
}
95+

vendor

Submodule vendor added at f6507b7

0 commit comments

Comments
 (0)