|
1 | 1 | import XCTest |
2 | 2 | @testable import secp256k1 |
| 3 | +@testable import secp256k1_utils |
3 | 4 |
|
4 | 5 | final class secp256k1Tests: XCTestCase { |
5 | | - /// Basic Keypair test |
6 | | - func testKeypairCreation() { |
| 6 | + /// Uncompressed Keypair test |
| 7 | + func testUncompressedKeypairCreation() { |
7 | 8 | // Initialize context |
8 | 9 | let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))! |
9 | 10 |
|
10 | 11 | // Destory context after execution |
11 | | - defer { |
12 | | - secp256k1_context_destroy(context) |
13 | | - } |
| 12 | + defer { secp256k1_context_destroy(context) } |
14 | 13 |
|
15 | 14 | // Setup private and public key variables |
16 | 15 | var pubkeyLen = 65 |
17 | 16 | var cPubkey = secp256k1_pubkey() |
18 | | - var pubkey = [UInt8](repeating: 0, count: pubkeyLen) |
19 | | - let privkey: [UInt8] = [0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,0,0,1,0,0,1,1,0,0,1,0,0,32,0] |
| 17 | + var publicKey = [UInt8](repeating: 0, count: pubkeyLen) |
| 18 | + |
| 19 | + let privateKey = try! Array(hexString: "14E4A74438858920D8A35FB2D88677580B6A2EE9BE4E711AE34EC6B396D87B5C".lowercased()) |
| 20 | + |
| 21 | + // Verify the context and keys are setup correctly |
| 22 | + XCTAssertEqual(secp256k1_context_randomize(context, privateKey), 1) |
| 23 | + XCTAssertEqual(secp256k1_ec_pubkey_create(context, &cPubkey, privateKey), 1) |
| 24 | + XCTAssertEqual(secp256k1_ec_pubkey_serialize(context, &publicKey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_UNCOMPRESSED)), 1) |
20 | 25 |
|
| 26 | + let hexString = """ |
| 27 | + 04734B3511150A60FC8CAC329CD5FF804555728740F2F2E98BC4242135EF5D5E4E6C4918116B0866F50C46614F3015D8667FBFB058471D662A642B8EA2C9C78E8A |
| 28 | + """ |
| 29 | + |
| 30 | + // Define the expected public key |
| 31 | + let expectedPublicKey = try! Array(hexString: hexString.lowercased()) |
| 32 | + |
| 33 | + // Verify the generated public key matches the expected public key |
| 34 | + XCTAssertEqual(expectedPublicKey, publicKey) |
| 35 | + } |
| 36 | + |
| 37 | + /// Compressed Keypair test |
| 38 | + func testCompressedKeypairCreation() { |
| 39 | + // Initialize context |
| 40 | + let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY))! |
| 41 | + |
| 42 | + // Destory context after execution |
| 43 | + defer { secp256k1_context_destroy(context) } |
| 44 | + |
| 45 | + // Setup private and public key variables |
| 46 | + var pubkeyLen = 33 |
| 47 | + var cPubkey = secp256k1_pubkey() |
| 48 | + var publicKey = [UInt8](repeating: 0, count: pubkeyLen) |
| 49 | + |
| 50 | + let privateKey = try! Array(hexString: "B035FCFC6ABF660856C5F3A6F9AC51FCA897BB4E76AD9ACA3EFD40DA6B9C864B".lowercased()) |
| 51 | + |
21 | 52 | // Verify the context and keys are setup correctly |
22 | | - XCTAssertEqual(secp256k1_context_randomize(context, privkey), 1) |
23 | | - XCTAssertEqual(secp256k1_ec_pubkey_create(context, &cPubkey, privkey), 1) |
24 | | - XCTAssertEqual(secp256k1_ec_pubkey_serialize(context, &pubkey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_UNCOMPRESSED)), 1) |
| 53 | + XCTAssertEqual(secp256k1_context_randomize(context, privateKey), 1) |
| 54 | + XCTAssertEqual(secp256k1_ec_pubkey_create(context, &cPubkey, privateKey), 1) |
| 55 | + XCTAssertEqual(secp256k1_ec_pubkey_serialize(context, &publicKey, &pubkeyLen, &cPubkey, UInt32(SECP256K1_EC_COMPRESSED)), 1) |
25 | 56 |
|
26 | 57 | // Define the expected public key |
27 | | - let expectedPublicKey: [UInt8] = [ |
28 | | - 4,96,104, 212, 128, 165, 213, |
29 | | - 207, 134, 132, 22, 247, 38, |
30 | | - 114, 82, 108, 77, 43, 6, 56, |
31 | | - 80, 113, 12, 11, 119, 7, 240, |
32 | | - 188, 73, 170, 44, 202, 33, 225, |
33 | | - 30, 248, 53, 138, 34, 22, 100, |
34 | | - 96, 31, 76, 64, 125, 71, 127, |
35 | | - 62, 155, 108, 243, 17, 222, 97, |
36 | | - 234, 75, 247, 187, 83, 151, 206, |
37 | | - 27, 38, 228 |
38 | | - ] |
| 58 | + let expectedPublicKey = try! Array(hexString: "02EA724B70B48B61FB87E4310871A48C65BF38BF3FDFEFE73C2B90F8F32F9C1794".lowercased()) |
39 | 59 |
|
40 | 60 | // Verify the generated public key matches the expected public key |
41 | | - XCTAssertEqual(expectedPublicKey, pubkey) |
| 61 | + XCTAssertEqual(expectedPublicKey, publicKey) |
42 | 62 | } |
43 | 63 |
|
44 | 64 | static var allTests = [ |
45 | | - ("testKeypairCreation", testKeypairCreation), |
| 65 | + ("testUncompressedKeypairCreation", testUncompressedKeypairCreation), |
| 66 | + ("testCompressedKeypairCreation", testCompressedKeypairCreation), |
46 | 67 | ] |
47 | 68 | } |
0 commit comments