@@ -396,7 +396,7 @@ final class secp256k1Tests: XCTestCase {
396
396
397
397
let set0 = Set ( array)
398
398
399
- array = [ UInt8] ( repeating: 1 , count: Int . random ( in: 10 ... 100000 ) )
399
+ array = [ UInt8] ( repeating: 1 , count: Int . random ( in: 10 ... 100_000 ) )
400
400
401
401
XCTAssertGreaterThan ( array. count, 9 )
402
402
@@ -649,6 +649,75 @@ final class secp256k1Tests: XCTestCase {
649
649
XCTAssertEqual ( combinedKeyBytes, expectedCombinedKey)
650
650
}
651
651
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
+
652
721
static var allTests = [
653
722
( " testUncompressedKeypairCreation " , testUncompressedKeypairCreation) ,
654
723
( " testCompressedKeypairCreation " , testCompressedKeypairCreation) ,
@@ -687,6 +756,10 @@ final class secp256k1Tests: XCTestCase {
687
756
( " testSchnorrNegating " , testSchnorrNegating) ,
688
757
( " testTaprootDerivation " , testTaprootDerivation) ,
689
758
( " testPubkeyCombine " , testPubkeyCombine) ,
690
- ( " testPubkeyCombineBindings " , testPubkeyCombineBindings)
759
+ ( " testPubkeyCombineBindings " , testPubkeyCombineBindings) ,
760
+ ( " testPrivateKeyPEM " , testPrivateKeyPEM) ,
761
+ ( " testPublicKeyPEM " , testPublicKeyPEM) ,
762
+ ( " testSigningPEM " , testSigningPEM) ,
763
+ ( " testVerifyingPEM " , testVerifyingPEM)
691
764
]
692
765
}
0 commit comments