@@ -69,22 +69,18 @@ public extension secp256k1.Signing.PublicKey {
6969 /// - format: the format of the tweaked `PublicKey` object
7070 /// - Returns: tweaked `PublicKey` object
7171 func add( _ tweak: [ UInt8 ] , format: secp256k1 . Format = . compressed) throws -> Self {
72+ let context = secp256k1. Context. raw
7273 var pubKey = secp256k1_pubkey ( )
7374 var pubKeyLen = format. length
7475 var pubKeyBytes = [ UInt8] ( repeating: 0 , count: pubKeyLen)
75- var xonlyKey = secp256k1_xonly_pubkey ( )
76- var xonlyBytes = [ UInt8] ( repeating: 0 , count: secp256k1. Schnorr. xonlyByteCount)
77- var keyParity = Int32 ( )
7876
79- guard secp256k1_ec_pubkey_parse ( secp256k1. Context. raw, & pubKey, bytes, pubKeyLen) . boolValue,
80- secp256k1_ec_pubkey_tweak_add ( secp256k1. Context. raw, & pubKey, tweak) . boolValue,
81- secp256k1_ec_pubkey_serialize ( secp256k1. Context. raw, & pubKeyBytes, & pubKeyLen, & pubKey, format. rawValue) . boolValue,
82- secp256k1_xonly_pubkey_from_pubkey ( secp256k1. Context. raw, & xonlyKey, & keyParity, & pubKey) . boolValue,
83- secp256k1_xonly_pubkey_serialize ( secp256k1. Context. raw, & xonlyBytes, & xonlyKey) . boolValue else {
77+ guard secp256k1_ec_pubkey_parse ( context, & pubKey, bytes, pubKeyLen) . boolValue,
78+ secp256k1_ec_pubkey_tweak_add ( context, & pubKey, tweak) . boolValue,
79+ secp256k1_ec_pubkey_serialize ( context, & pubKeyBytes, & pubKeyLen, & pubKey, format. rawValue) . boolValue else {
8480 throw secp256k1Error. underlyingCryptoError
8581 }
8682
87- return Self ( rawRepresentation: pubKeyBytes, xonly : xonlyBytes , keyParity : keyParity , format: format)
83+ return try Self ( rawRepresentation: pubKeyBytes, format: format)
8884 }
8985
9086 /// Create a new `PublicKey` by multiplying tweak to the public key.
@@ -93,22 +89,18 @@ public extension secp256k1.Signing.PublicKey {
9389 /// - format: the format of the tweaked `PublicKey` object
9490 /// - Returns: tweaked `PublicKey` object
9591 func multiply( _ tweak: [ UInt8 ] , format: secp256k1 . Format = . compressed) throws -> Self {
92+ let context = secp256k1. Context. raw
9693 var pubKey = secp256k1_pubkey ( )
9794 var pubKeyLen = format. length
9895 var pubKeyBytes = [ UInt8] ( repeating: 0 , count: pubKeyLen)
99- var xonlyKey = secp256k1_xonly_pubkey ( )
100- var xonlyBytes = [ UInt8] ( repeating: 0 , count: secp256k1. Schnorr. xonlyByteCount)
101- var keyParity = Int32 ( )
10296
103- guard secp256k1_ec_pubkey_parse ( secp256k1. Context. raw, & pubKey, bytes, pubKeyLen) . boolValue,
104- secp256k1_ec_pubkey_tweak_mul ( secp256k1. Context. raw, & pubKey, tweak) . boolValue,
105- secp256k1_ec_pubkey_serialize ( secp256k1. Context. raw, & pubKeyBytes, & pubKeyLen, & pubKey, format. rawValue) . boolValue,
106- secp256k1_xonly_pubkey_from_pubkey ( secp256k1. Context. raw, & xonlyKey, & keyParity, & pubKey) . boolValue,
107- secp256k1_xonly_pubkey_serialize ( secp256k1. Context. raw, & xonlyBytes, & xonlyKey) . boolValue else {
97+ guard secp256k1_ec_pubkey_parse ( context, & pubKey, bytes, pubKeyLen) . boolValue,
98+ secp256k1_ec_pubkey_tweak_mul ( context, & pubKey, tweak) . boolValue,
99+ secp256k1_ec_pubkey_serialize ( context, & pubKeyBytes, & pubKeyLen, & pubKey, format. rawValue) . boolValue else {
108100 throw secp256k1Error. underlyingCryptoError
109101 }
110102
111- return Self ( rawRepresentation: pubKeyBytes, xonly : xonlyBytes , keyParity : keyParity , format: format)
103+ return try Self ( rawRepresentation: pubKeyBytes, format: format)
112104 }
113105}
114106
@@ -119,17 +111,18 @@ public extension secp256k1.Signing.XonlyKey {
119111 /// - format: the format of the tweaked `XonlyKey` object
120112 /// - Returns: tweaked `PublicKey` object
121113 func add( _ tweak: [ UInt8 ] ) throws -> Self {
114+ let context = secp256k1. Context. raw
122115 var pubKey = secp256k1_pubkey ( )
123116 var inXonlyPubKey = secp256k1_xonly_pubkey ( )
124117 var outXonlyPubKey = secp256k1_xonly_pubkey ( )
125118 var xonlyBytes = [ UInt8] ( repeating: 0 , count: secp256k1. Schnorr. xonlyByteCount)
126119 var keyParity = Int32 ( )
127120
128- guard secp256k1_xonly_pubkey_parse ( secp256k1 . Context . raw , & inXonlyPubKey, bytes) . boolValue,
129- secp256k1_xonly_pubkey_tweak_add ( secp256k1 . Context . raw , & pubKey, & inXonlyPubKey, tweak) . boolValue,
130- secp256k1_xonly_pubkey_from_pubkey ( secp256k1 . Context . raw , & outXonlyPubKey, & keyParity, & pubKey) . boolValue,
131- secp256k1_xonly_pubkey_serialize ( secp256k1 . Context . raw , & xonlyBytes, & outXonlyPubKey) . boolValue,
132- secp256k1_xonly_pubkey_tweak_add_check ( secp256k1 . Context . raw , & xonlyBytes, keyParity, & inXonlyPubKey, tweak) . boolValue else {
121+ guard secp256k1_xonly_pubkey_parse ( context , & inXonlyPubKey, bytes) . boolValue,
122+ secp256k1_xonly_pubkey_tweak_add ( context , & pubKey, & inXonlyPubKey, tweak) . boolValue,
123+ secp256k1_xonly_pubkey_from_pubkey ( context , & outXonlyPubKey, & keyParity, & pubKey) . boolValue,
124+ secp256k1_xonly_pubkey_serialize ( context , & xonlyBytes, & outXonlyPubKey) . boolValue,
125+ secp256k1_xonly_pubkey_tweak_add_check ( context , & xonlyBytes, keyParity, & inXonlyPubKey, tweak) . boolValue else {
133126 throw secp256k1Error. underlyingCryptoError
134127 }
135128
0 commit comments