From 65b6ff69fd980a3f5ea8a595193c169022c57a7f Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Thu, 25 Nov 2021 13:37:24 +0800 Subject: [PATCH 1/4] Add Swift Package Manager support --- Package.swift | 36 ++++++++++++++++++++++++++++++++++++ modulemap/module.modulemap | 4 ++++ 2 files changed, 40 insertions(+) create mode 100644 Package.swift create mode 100644 modulemap/module.modulemap diff --git a/Package.swift b/Package.swift new file mode 100644 index 00000000..738bb7fd --- /dev/null +++ b/Package.swift @@ -0,0 +1,36 @@ +// 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: "tommath", + platforms: [ + .macOS(.v10_10), .iOS(.v9), .tvOS(.v9) + ], + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "libtommath", + targets: ["libtommath"]) + ], + 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 this package depends on. + .target( + name: "libtommath", + path: ".", + exclude: ["demo", "doc", "etc", "logs", "mtest"], + sources: ["."], + publicHeadersPath: "modulemap", + cSettings: [ + .headerSearchPath(".") + ]) + ], + cLanguageStandard: .gnu11, + cxxLanguageStandard: .gnucxx14 +) diff --git a/modulemap/module.modulemap b/modulemap/module.modulemap new file mode 100644 index 00000000..7280be7e --- /dev/null +++ b/modulemap/module.modulemap @@ -0,0 +1,4 @@ +module libtommath [extern_c] { + header "../tommath.h" + export * +} From 8422235d2f7dddfebd3f5b5123030aebcc7a7479 Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Thu, 25 Nov 2021 13:58:21 +0800 Subject: [PATCH 2/4] Add ThinLTO flags --- Package.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 738bb7fd..60a7d1ec 100644 --- a/Package.swift +++ b/Package.swift @@ -28,7 +28,8 @@ let package = Package( sources: ["."], publicHeadersPath: "modulemap", cSettings: [ - .headerSearchPath(".") + .headerSearchPath("."), + .unsafeFlags(["-flto=thin"]) ]) ], cLanguageStandard: .gnu11, From 3d282f2ff2d7d7c13306a33a69571799cf342ed0 Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Thu, 25 Nov 2021 14:12:24 +0800 Subject: [PATCH 3/4] Add comment for ThinLTO flags --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 60a7d1ec..1ed4dd3f 100644 --- a/Package.swift +++ b/Package.swift @@ -29,7 +29,7 @@ let package = Package( publicHeadersPath: "modulemap", cSettings: [ .headerSearchPath("."), - .unsafeFlags(["-flto=thin"]) + .unsafeFlags(["-flto=thin"]) // for Dead Code Elimination ]) ], cLanguageStandard: .gnu11, From 9b6d7d5cac1af3217599e9fbd23fc83a9e536f76 Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Wed, 1 Dec 2021 22:10:29 +0800 Subject: [PATCH 4/4] Add Swift Test case for libtommath --- demo/tommath_tests.swift | 94 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 demo/tommath_tests.swift diff --git a/demo/tommath_tests.swift b/demo/tommath_tests.swift new file mode 100644 index 00000000..fd254da3 --- /dev/null +++ b/demo/tommath_tests.swift @@ -0,0 +1,94 @@ +import XCTest +import libtommath + +/* ---> Basic Manipulations <--- */ + +extension mp_int { + var isZero: Bool { used == 0 } + var isNeg: Bool { sign == MP_NEG } + var isEven: Bool { used == 0 || (dp[0] & 1) == 0 } + var isOdd: Bool { !isEven } +} + +func mp_get_u32(_ a: UnsafePointer) -> UInt32 { + return UInt32(bitPattern: mp_get_i32(a)) +} + +class LibTommathTests: XCTestCase { + + override func setUpWithError() throws { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDownWithError() throws { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func testTrivialStuff() throws { + var a = mp_int() + var b = mp_int() + var c = mp_int() + var d = mp_int() + + XCTAssertEqual(mp_init(&a), MP_OKAY) + XCTAssertEqual(mp_init(&b), MP_OKAY) + XCTAssertEqual(mp_init(&c), MP_OKAY) + XCTAssertEqual(mp_init(&d), MP_OKAY) + + defer { + mp_clear(&a) + mp_clear(&b) + mp_clear(&c) + mp_clear(&d) + } + + XCTAssert(mp_error_to_string(MP_OKAY) != nil) + + /* a: 0->5 */ + mp_set(&a, 5) + /* a: 5-> b: -5 */ + XCTAssertEqual(mp_neg(&a, &b), MP_OKAY) + XCTAssertEqual(mp_cmp(&a, &b), MP_GT) + XCTAssertEqual(mp_cmp(&b, &a), MP_LT) + XCTAssertTrue(b.isNeg) + /* a: 5-> a: -5 */ + var t = a // Fix compiler error: Overlapping accesses to 'a', but modification requires exclusive access; consider copying to a local variable + XCTAssertEqual(mp_neg(&t, &a), MP_OKAY) + XCTAssertEqual(mp_cmp(&b, &a), MP_EQ) + XCTAssertTrue(a.isNeg) + /* a: -5-> b: 5 */ + XCTAssertEqual(mp_abs(&a, &b), MP_OKAY) + XCTAssertTrue(!b.isNeg) + /* a: -5-> b: -4 */ + XCTAssertEqual(mp_add_d(&a, 1, &b), MP_OKAY) + XCTAssertTrue(b.isNeg) + XCTAssertEqual(mp_get_i32(&b), -4) + XCTAssertEqual(mp_get_u32(&b), UInt32(bitPattern: -4)) + XCTAssertEqual(mp_get_mag_u32(&b), 4) + /* a: -5-> b: 1 */ + XCTAssertEqual(mp_add_d(&a, 6, &b), MP_OKAY) + XCTAssertEqual(mp_get_u32(&b), 1) + /* a: -5-> a: 1 */ + t = a + XCTAssertEqual(mp_add_d(&t, 6, &a), MP_OKAY) + XCTAssertEqual(mp_get_u32(&a), 1) + mp_zero(&a); + /* a: 0-> a: 6 */ + t = a + XCTAssertEqual(mp_add_d(&t, 6, &a), MP_OKAY) + XCTAssertEqual(mp_get_u32(&a), 6) + + mp_set(&a, 42) + mp_set(&b, 1) + t = b + XCTAssertEqual(mp_neg(&t, &b), MP_OKAY) + mp_set(&c, 1) + XCTAssertEqual(mp_exptmod(&a, &b, &c, &d), MP_OKAY) + + mp_set(&c, 7) + /* same here */ + XCTAssertTrue(mp_exptmod(&a, &b, &c, &d) != MP_OKAY) + + XCTAssertTrue(a.isEven != a.isOdd) + } +}