Skip to content

Commit 8049f0c

Browse files
committed
Created PEM keys from OpenSSL for unit tests
1 parent 50e12e6 commit 8049f0c

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

Sources/zkp/ASN1/Basic ASN1 Types/ObjectIdentifier.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
}
171171

172172
enum AlgorithmIdentifier {
173-
static let idEcPublicKey: ASN1.ASN1ObjectIdentifier = [1, 2, 840, 10045, 2, 1]
173+
static let idEcPublicKey: ASN1.ASN1ObjectIdentifier = [1, 2, 840, 10_045, 2, 1]
174174
}
175175

176176
enum NameAttributes {
@@ -189,8 +189,8 @@
189189
static let countryName: ASN1.ASN1ObjectIdentifier = [2, 5, 4, 6]
190190
static let serialNumber: ASN1.ASN1ObjectIdentifier = [2, 5, 4, 5]
191191
static let pseudonym: ASN1.ASN1ObjectIdentifier = [2, 5, 4, 65]
192-
static let domainComponent: ASN1.ASN1ObjectIdentifier = [0, 9, 2342, 19200300, 100, 1, 25]
193-
static let emailAddress: ASN1.ASN1ObjectIdentifier = [1, 2, 840, 113549, 1, 9, 1]
192+
static let domainComponent: ASN1.ASN1ObjectIdentifier = [0, 9, 2_342, 19_200_300, 100, 1, 25]
193+
static let emailAddress: ASN1.ASN1ObjectIdentifier = [1, 2, 840, 113_549, 1, 9, 1]
194194
}
195195
}
196196

Tests/zkpTests/secp256k1Tests.swift

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ final class secp256k1Tests: XCTestCase {
396396

397397
let set0 = Set(array)
398398

399-
array = [UInt8](repeating: 1, count: Int.random(in: 10...100000))
399+
array = [UInt8](repeating: 1, count: Int.random(in: 10...100_000))
400400

401401
XCTAssertGreaterThan(array.count, 9)
402402

@@ -649,6 +649,75 @@ final class secp256k1Tests: XCTestCase {
649649
XCTAssertEqual(combinedKeyBytes, expectedCombinedKey)
650650
}
651651

652+
func testPrivateKeyPEM() {
653+
let privateKeyString = """
654+
-----BEGIN EC PRIVATE KEY-----
655+
MHQCAQEEIBXwHPDpec6b07GeLbnwetT0dvWzp0nV3MR+4pPKXIc7oAcGBSuBBAAK
656+
oUQDQgAEt2uDn+2GqqYs/fmkBr5+rCQ3oiFSIJMAcjHIrTDS6HEELgguOatmFBOp
657+
2wU4P2TAl/0Ihiq+nMkrAIV69m2W8g==
658+
-----END EC PRIVATE KEY-----
659+
"""
660+
661+
let privateKey = try! secp256k1.Signing.PrivateKey(pemRepresentation: privateKeyString)
662+
let expectedPrivateKey = "15f01cf0e979ce9bd3b19e2db9f07ad4f476f5b3a749d5dcc47ee293ca5c873b"
663+
664+
// Verify the keys matches the expected keys output
665+
XCTAssertEqual(expectedPrivateKey, String(bytes: privateKey.dataRepresentation))
666+
}
667+
668+
func testPublicKeyPEM() {
669+
let publicKeyString = """
670+
-----BEGIN PUBLIC KEY-----
671+
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEt2uDn+2GqqYs/fmkBr5+rCQ3oiFSIJMA
672+
cjHIrTDS6HEELgguOatmFBOp2wU4P2TAl/0Ihiq+nMkrAIV69m2W8g==
673+
-----END PUBLIC KEY-----
674+
"""
675+
676+
let privateKeyBytes = try! "15f01cf0e979ce9bd3b19e2db9f07ad4f476f5b3a749d5dcc47ee293ca5c873b".bytes
677+
let privateKey = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes, format: .uncompressed)
678+
let publicKey = try! secp256k1.Signing.PublicKey(pemRepresentation: publicKeyString)
679+
680+
// Verify the keys matches the expected keys output
681+
XCTAssertEqual(privateKey.publicKey.dataRepresentation, publicKey.dataRepresentation)
682+
}
683+
684+
func testSigningPEM() {
685+
let privateKeyString = """
686+
-----BEGIN EC PRIVATE KEY-----
687+
MHQCAQEEIBXwHPDpec6b07GeLbnwetT0dvWzp0nV3MR+4pPKXIc7oAcGBSuBBAAK
688+
oUQDQgAEt2uDn+2GqqYs/fmkBr5+rCQ3oiFSIJMAcjHIrTDS6HEELgguOatmFBOp
689+
2wU4P2TAl/0Ihiq+nMkrAIV69m2W8g==
690+
-----END EC PRIVATE KEY-----
691+
"""
692+
693+
let expectedDerSignature = "MEQCIC8k5whKPsPg7XtWTInvhGL4iEU6lP6yPdpEXXZ2mOhFAiAZ3Po9tEDV8mQ8LDzwF0nhPmAn9VLYG8bkuY6PKruZNQ=="
694+
let privateKey = try! secp256k1.Signing.PrivateKey(pemRepresentation: privateKeyString)
695+
let messageData = "We're all Satoshi Nakamoto and a bit of Harold Thomas Finney II.".data(using: .utf8)!
696+
697+
let signature = try! privateKey.signature(for: messageData)
698+
699+
// Verify the signature matches the expected output
700+
XCTAssertEqual(expectedDerSignature, try! signature.derRepresentation.base64EncodedString())
701+
}
702+
703+
func testVerifyingPEM() {
704+
let publicKeyString = """
705+
-----BEGIN PUBLIC KEY-----
706+
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEt2uDn+2GqqYs/fmkBr5+rCQ3oiFSIJMA
707+
cjHIrTDS6HEELgguOatmFBOp2wU4P2TAl/0Ihiq+nMkrAIV69m2W8g==
708+
-----END PUBLIC KEY-----
709+
"""
710+
711+
let expectedSignature = "MEQCIEwVxXLE/mwaRzxLvz9VIcMtHaa/Wf1WRxiBJ6NEuWHeAiAQWf2oqqBqEtBABbmwsXqjCJFvsaPt8o+VaOthto1kWQ=="
712+
let expectedDerSignature = Data(base64Encoded: expectedSignature, options: .ignoreUnknownCharacters)!
713+
714+
let messageData = "We're all Satoshi Nakamoto and a bit of Harold Thomas Finney II.".data(using: .utf8)!
715+
let signature = try! secp256k1.Signing.ECDSASignature(derRepresentation: expectedDerSignature)
716+
let publicKey = try! secp256k1.Signing.PublicKey(pemRepresentation: publicKeyString)
717+
718+
XCTAssertTrue(publicKey.isValidSignature(signature, for: SHA256.hash(data: messageData)))
719+
}
720+
652721
static var allTests = [
653722
("testUncompressedKeypairCreation", testUncompressedKeypairCreation),
654723
("testCompressedKeypairCreation", testCompressedKeypairCreation),
@@ -687,6 +756,10 @@ final class secp256k1Tests: XCTestCase {
687756
("testSchnorrNegating", testSchnorrNegating),
688757
("testTaprootDerivation", testTaprootDerivation),
689758
("testPubkeyCombine", testPubkeyCombine),
690-
("testPubkeyCombineBindings", testPubkeyCombineBindings)
759+
("testPubkeyCombineBindings", testPubkeyCombineBindings),
760+
("testPrivateKeyPEM", testPrivateKeyPEM),
761+
("testPublicKeyPEM", testPublicKeyPEM),
762+
("testSigningPEM", testSigningPEM),
763+
("testVerifyingPEM", testVerifyingPEM)
691764
]
692765
}

0 commit comments

Comments
 (0)