|
| 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 | +} |
0 commit comments