diff --git a/Sources/zkp/Asymmetric.swift b/Sources/zkp/Asymmetric.swift index 4c3a8d3..a0f8fb5 100644 --- a/Sources/zkp/Asymmetric.swift +++ b/Sources/zkp/Asymmetric.swift @@ -33,8 +33,16 @@ public extension secp256k1 { } /// A data representation of the private key. - public var rawRepresentation: Data { - baseKey.rawRepresentation + public var dataRepresentation: Data { + baseKey.dataRepresentation + } + + /// Negates a secret key. + public var negation: PrivateKey { + get throws { + let negatedKey = try baseKey.negation.dataRepresentation + return try Self(dataRepresentation: negatedKey) + } } /// Creates a random secp256k1 private key for signing. @@ -47,11 +55,11 @@ public extension secp256k1 { /// Creates a secp256k1 private key for signing from a data representation. /// - /// - Parameter data: A raw representation of the key. + /// - Parameter data: A data representation of the key. /// - Parameter format: The key format, default is .compressed. /// - Throws: An error if the raw representation does not create a private key for signing. - public init(rawRepresentation data: D, format: secp256k1.Format = .compressed) throws { - self.baseKey = try PrivateKeyImplementation(rawRepresentation: data, format: format) + public init(dataRepresentation data: D, format: secp256k1.Format = .compressed) throws { + self.baseKey = try PrivateKeyImplementation(dataRepresentation: data, format: format) } /// Determines if two private keys are equal. @@ -76,7 +84,12 @@ public extension secp256k1 { } /// A data representation of the public key. - public var rawRepresentation: Data { + public var dataRepresentation: Data { + baseKey.dataRepresentation + } + + /// A raw representation of the public key. + public var rawRepresentation: secp256k1_pubkey { baseKey.rawRepresentation } @@ -92,6 +105,14 @@ public extension secp256k1 { baseKey.format } + /// Negates a public key. + public var negation: PublicKey { + get throws { + let negatedKey = try baseKey.negation + return try Self(dataRepresentation: negatedKey.dataRepresentation, format: negatedKey.format) + } + } + /// Generates a secp256k1 public key. /// /// - Parameter baseKey: Generated secp256k1 public key. @@ -104,19 +125,19 @@ public extension secp256k1 { /// - Parameter xonlyKey: An x-only key object. public init(xonlyKey: XonlyKey) { let key = XonlyKeyImplementation( - rawRepresentation: xonlyKey.bytes, + dataRepresentation: xonlyKey.bytes, keyParity: xonlyKey.parity ? 1 : 0 ) self.baseKey = PublicKeyImplementation(xonlyKey: key) } - /// Generates a secp256k1 public key from a raw representation. + /// Generates a secp256k1 public key from a data representation. /// - /// - Parameter data: A raw representation of the key. + /// - Parameter data: A data representation of the key. /// - Parameter format: The key format. - /// - Throws: An error if the raw representation does not create a public key. - public init(rawRepresentation data: D, format: secp256k1.Format) throws { - self.baseKey = try PublicKeyImplementation(rawRepresentation: data, format: format) + /// - Throws: An error if the data representation does not create a public key. + public init(dataRepresentation data: D, format: secp256k1.Format) throws { + self.baseKey = try PublicKeyImplementation(dataRepresentation: data, format: format) } } @@ -147,10 +168,10 @@ public extension secp256k1 { /// Generates a secp256k1 x-only public key from a raw representation and key parity. /// - /// - Parameter data: A raw representation of the x-only public key. + /// - Parameter data: A data representation of the x-only public key. /// - Parameter keyParity: The key parity as an `Int32`. - public init(rawRepresentation data: D, keyParity: Int32) { - self.baseKey = XonlyKeyImplementation(rawRepresentation: data, keyParity: keyParity) + public init(dataRepresentation data: D, keyParity: Int32) { + self.baseKey = XonlyKeyImplementation(dataRepresentation: data, keyParity: keyParity) } } } diff --git a/Sources/zkp/ECDH.swift b/Sources/zkp/ECDH.swift index a12ec2b..52f1fb0 100644 --- a/Sources/zkp/ECDH.swift +++ b/Sources/zkp/ECDH.swift @@ -24,11 +24,11 @@ public extension secp256k1 { /// Creates a secp256k1 public key for key agreement from a collection of bytes. /// /// - Parameters: - /// - data: A raw representation of the public key as a collection of contiguous bytes. + /// - data: A data representation of the public key as a collection of contiguous bytes. /// - format: The format of the public key object. /// - Throws: An error if the raw representation does not create a public key. - public init(rawRepresentation data: D, format: secp256k1.Format = .compressed) throws { - self.baseKey = try PublicKeyImplementation(rawRepresentation: data, format: format) + public init(dataRepresentation data: D, format: secp256k1.Format = .compressed) throws { + self.baseKey = try PublicKeyImplementation(dataRepresentation: data, format: format) } /// Initializes a secp256k1 public key for key agreement. @@ -46,7 +46,10 @@ public extension secp256k1 { } /// A data representation of the public key. - public var rawRepresentation: Data { baseKey.rawRepresentation } + public var dataRepresentation: Data { baseKey.dataRepresentation } + + /// A raw representation of the public key. + public var rawRepresentation: secp256k1_pubkey { baseKey.rawRepresentation } /// Implementation public key object. var bytes: [UInt8] { baseKey.bytes } @@ -58,7 +61,10 @@ public extension secp256k1 { private let baseKey: XonlyKeyImplementation /// A data representation of the backing x-only public key. - public var rawRepresentation: Data { baseKey.rawRepresentation } + public var dataRepresentation: Data { baseKey.dataRepresentation } + + /// A raw representation of the backing x-only public key. + public var rawRepresentation: secp256k1_xonly_pubkey { baseKey.rawRepresentation } /// A boolean that will be set to true if the point encoded by xonly is the /// negation of the pubkey and set to false otherwise. @@ -91,8 +97,8 @@ public extension secp256k1 { /// - data: A raw representation of the key. /// - format: The format of the secp256k1 key (default is .compressed). /// - Throws: An error is thrown when the raw representation does not create a private key for key agreement. - public init(rawRepresentation data: D, format: secp256k1.Format = .compressed) throws { - self.baseKey = try PrivateKeyImplementation(rawRepresentation: data, format: format) + public init(dataRepresentation data: D, format: secp256k1.Format = .compressed) throws { + self.baseKey = try PrivateKeyImplementation(dataRepresentation: data, format: format) } /// Initializes a secp256k1 private key for key agreement. @@ -108,7 +114,7 @@ public extension secp256k1 { } /// A data representation of the private key. - public var rawRepresentation: Data { baseKey.rawRepresentation } + public var rawRepresentation: Data { baseKey.dataRepresentation } /// A secure bytes representation of the private key. var bytes: SecureBytes { baseKey.key } diff --git a/Sources/zkp/ECDSA.swift b/Sources/zkp/ECDSA.swift index 91fa491..3e0c16b 100644 --- a/Sources/zkp/ECDSA.swift +++ b/Sources/zkp/ECDSA.swift @@ -10,11 +10,11 @@ import Foundation -typealias NISTECDSASignature = RawSignature & DERSignature +typealias NISTECDSASignature = DataSignature & DERSignature -protocol RawSignature { - init(rawRepresentation: D) throws - var rawRepresentation: Data { get } +protocol DataSignature { + init(dataRepresentation: D) throws + var dataRepresentation: Data { get } } protocol DERSignature { @@ -32,20 +32,20 @@ protocol CompactSignature { /// An ECDSA (Elliptic Curve Digital Signature Algorithm) Signature public extension secp256k1.Signing { struct ECDSASignature: ContiguousBytes, NISTECDSASignature, CompactSignature { - /// Returns the raw signature. + /// Returns the data signature. /// The raw signature format for ECDSA is r || s - public var rawRepresentation: Data + public var dataRepresentation: Data /// Initializes ECDSASignature from the raw representation. /// - Parameters: - /// - rawRepresentation: A raw representation of the key as a collection of contiguous bytes. + /// - dataRepresentation: A data representation of the key as a collection of contiguous bytes. /// - Throws: If there is a failure with the dataRepresentation count - public init(rawRepresentation: D) throws { - guard rawRepresentation.count == 4 * secp256k1.CurveDetails.coordinateByteCount else { + public init(dataRepresentation: D) throws { + guard dataRepresentation.count == 4 * secp256k1.CurveDetails.coordinateByteCount else { throw secp256k1Error.incorrectParameterSize } - self.rawRepresentation = Data(rawRepresentation) + self.dataRepresentation = Data(dataRepresentation) } /// Initializes ECDSASignature from the raw representation. @@ -57,7 +57,7 @@ public extension secp256k1.Signing { throw secp256k1Error.incorrectParameterSize } - self.rawRepresentation = dataRepresentation + self.dataRepresentation = dataRepresentation } /// Initializes ECDSASignature from the DER representation. @@ -77,7 +77,7 @@ public extension secp256k1.Signing { throw secp256k1Error.underlyingCryptoError } - self.rawRepresentation = signature.dataValue + self.dataRepresentation = signature.dataValue } /// Initializes ECDSASignature from the Compact representation. @@ -95,7 +95,7 @@ public extension secp256k1.Signing { throw secp256k1Error.underlyingCryptoError } - self.rawRepresentation = signature.dataValue + self.dataRepresentation = signature.dataValue } /// Invokes the given closure with a buffer pointer covering the raw bytes of the digest. @@ -103,7 +103,7 @@ public extension secp256k1.Signing { /// - Throws: If there is a failure with underlying `withUnsafeBytes` /// - Returns: The signature as returned from the body closure. public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { - try rawRepresentation.withUnsafeBytes(body) + try dataRepresentation.withUnsafeBytes(body) } /// Serialize an ECDSA signature in compact (64 byte) format. @@ -116,7 +116,7 @@ public extension secp256k1.Signing { var signature = secp256k1_ecdsa_signature() var compactSignature = [UInt8](repeating: 0, count: compactSignatureLength) - rawRepresentation.copyToUnsafeMutableBytes(of: &signature.data) + dataRepresentation.copyToUnsafeMutableBytes(of: &signature.data) guard secp256k1_ecdsa_signature_serialize_compact( context, @@ -140,7 +140,7 @@ public extension secp256k1.Signing { var derSignatureLength = 80 var derSignature = [UInt8](repeating: 0, count: derSignatureLength) - rawRepresentation.copyToUnsafeMutableBytes(of: &signature.data) + dataRepresentation.copyToUnsafeMutableBytes(of: &signature.data) guard secp256k1_ecdsa_signature_serialize_der( context, @@ -173,7 +173,7 @@ extension secp256k1.Signing.PrivateKey: DigestSigner { context, &signature, Array(digest), - Array(rawRepresentation), + Array(dataRepresentation), nil, nil ).boolValue else { @@ -210,7 +210,7 @@ extension secp256k1.Signing.PublicKey: DigestValidator { var ecdsaSignature = secp256k1_ecdsa_signature() var publicKey = secp256k1_pubkey() - signature.rawRepresentation.copyToUnsafeMutableBytes(of: &ecdsaSignature.data) + signature.dataRepresentation.copyToUnsafeMutableBytes(of: &ecdsaSignature.data) return secp256k1_ec_pubkey_parse(context, &publicKey, bytes, bytes.count).boolValue && secp256k1_ecdsa_verify(context, &ecdsaSignature, Array(digest), &publicKey).boolValue diff --git a/Sources/zkp/Recovery.swift b/Sources/zkp/Recovery.swift index ff7f8d9..088d706 100644 --- a/Sources/zkp/Recovery.swift +++ b/Sources/zkp/Recovery.swift @@ -28,8 +28,8 @@ public extension secp256k1 { } /// A data representation of the private key. - public var rawRepresentation: Data { - baseKey.rawRepresentation + public var dataRepresentation: Data { + baseKey.dataRepresentation } /// Creates a random secp256k1 private key for signing. @@ -42,11 +42,11 @@ public extension secp256k1 { /// Creates a secp256k1 private key for signing from a data representation. /// - /// - Parameter data: A raw representation of the key. + /// - Parameter data: A data representation of the key. /// - Parameter format: The key format, default is .compressed. /// - Throws: An error if the raw representation does not create a private key for signing. - public init(rawRepresentation data: D, format: secp256k1.Format = .compressed) throws { - self.baseKey = try PrivateKeyImplementation(rawRepresentation: data, format: format) + public init(dataRepresentation data: D, format: secp256k1.Format = .compressed) throws { + self.baseKey = try PrivateKeyImplementation(dataRepresentation: data, format: format) } /// Determines if two private keys are equal. @@ -63,7 +63,10 @@ public extension secp256k1 { /// A struct representing a secp256k1 public key for recovery purposes. public struct PublicKey { /// A data representation of the public key. - public var rawRepresentation: Data { baseKey.rawRepresentation } + public var dataRepresentation: Data { baseKey.dataRepresentation } + + /// A raw representation of the public key. + public var rawRepresentation: secp256k1_pubkey { baseKey.rawRepresentation } /// Generated secp256k1 Public Key. private let baseKey: PublicKeyImplementation @@ -115,9 +118,9 @@ public extension secp256k1.Recovery { public let recoveryId: Int32 } - struct ECDSASignature: ContiguousBytes, RawSignature { + struct ECDSASignature: ContiguousBytes, DataSignature { /// Returns the raw signature. - public var rawRepresentation: Data + public var dataRepresentation: Data /// Serialize an ECDSA signature in compact (64 byte) format. /// - Throws: If there is a failure parsing signature @@ -130,7 +133,7 @@ public extension secp256k1.Recovery { var recoverableSignature = secp256k1_ecdsa_recoverable_signature() var compactSignature = [UInt8](repeating: 0, count: compactSignatureLength) - rawRepresentation.copyToUnsafeMutableBytes(of: &recoverableSignature.data) + dataRepresentation.copyToUnsafeMutableBytes(of: &recoverableSignature.data) guard secp256k1_ecdsa_recoverable_signature_serialize_compact( context, @@ -155,7 +158,7 @@ public extension secp256k1.Recovery { var normalizedSignature = secp256k1_ecdsa_signature() var recoverableSignature = secp256k1_ecdsa_recoverable_signature() - rawRepresentation.copyToUnsafeMutableBytes(of: &recoverableSignature.data) + dataRepresentation.copyToUnsafeMutableBytes(of: &recoverableSignature.data) guard secp256k1_ecdsa_recoverable_signature_convert( context, @@ -171,26 +174,26 @@ public extension secp256k1.Recovery { /// Initializes ECDSASignature from the raw representation. /// - Parameters: - /// - rawRepresentation: A raw representation of the key as a collection of contiguous bytes. + /// - dataRepresentation: A data representation of the key as a collection of contiguous bytes. /// - Throws: If there is a failure with the dataRepresentation count - public init(rawRepresentation: D) throws { - guard rawRepresentation.count == 4 * secp256k1.CurveDetails.coordinateByteCount + 1 else { + public init(dataRepresentation: D) throws { + guard dataRepresentation.count == 4 * secp256k1.CurveDetails.coordinateByteCount + 1 else { throw secp256k1Error.incorrectParameterSize } - self.rawRepresentation = Data(rawRepresentation) + self.dataRepresentation = Data(dataRepresentation) } /// Initializes ECDSASignature from the raw representation. /// - Parameters: - /// - rawRepresentation: A raw representation of the key as a collection of contiguous bytes. + /// - dataRepresentation: A data representation of the key as a collection of contiguous bytes. /// - Throws: If there is a failure with the dataRepresentation count internal init(_ dataRepresentation: Data) throws { guard dataRepresentation.count == 4 * secp256k1.CurveDetails.coordinateByteCount + 1 else { throw secp256k1Error.incorrectParameterSize } - self.rawRepresentation = dataRepresentation + self.dataRepresentation = dataRepresentation } /// Initializes ECDSASignature from the Compact representation. @@ -209,7 +212,7 @@ public extension secp256k1.Recovery { throw secp256k1Error.underlyingCryptoError } - self.rawRepresentation = recoverableSignature.dataValue + self.dataRepresentation = recoverableSignature.dataValue } /// Invokes the given closure with a buffer pointer covering the raw bytes of the digest. @@ -217,7 +220,7 @@ public extension secp256k1.Recovery { /// - Throws: If there is a failure with underlying `withUnsafeBytes` /// - Returns: The signature as returned from the body closure. public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { - try rawRepresentation.withUnsafeBytes(body) + try dataRepresentation.withUnsafeBytes(body) } } } @@ -240,7 +243,7 @@ extension secp256k1.Recovery.PrivateKey: DigestSigner { context, &signature, Array(digest), - Array(rawRepresentation), + Array(dataRepresentation), nil, nil ).boolValue else { diff --git a/Sources/zkp/Schnorr.swift b/Sources/zkp/Schnorr.swift index 7fd9c4b..22a39b4 100644 --- a/Sources/zkp/Schnorr.swift +++ b/Sources/zkp/Schnorr.swift @@ -35,11 +35,6 @@ public extension secp256k1.Schnorr { /// Generated secp256k1 Signing Key. private let baseKey: PrivateKeyImplementation - /// Whether strict BIP340 adherence is enabled - public var strict: Bool { - baseKey.strict - } - /// The associated x-only public key for verifying Schnorr signatures. /// /// - Returns: The associated x-only public key. @@ -48,28 +43,32 @@ public extension secp256k1.Schnorr { } /// A data representation of the private key. - public var rawRepresentation: Data { - baseKey.rawRepresentation + public var dataRepresentation: Data { + baseKey.dataRepresentation + } + + /// Negates a secret key. + public var negation: PrivateKey { + get throws { + let negatedKey = try baseKey.negation.dataRepresentation + return try Self(dataRepresentation: negatedKey) + } } /// Creates a random secp256k1 private key for signing. /// /// - Parameter format: The key format, default is .compressed. /// - Throws: An error if the private key cannot be generated. - public init(strict: Bool = true) throws { - self.baseKey = try PrivateKeyImplementation(strict: strict) + public init() throws { + self.baseKey = try PrivateKeyImplementation() } /// Creates a secp256k1 private key for signing from a data representation. /// - /// - Parameter data: A raw representation of the key. - /// - Parameter format: The key format, default is .compressed. - /// - Throws: An error if the raw representation does not create a private key for signing. - public init( - rawRepresentation data: D, - strict: Bool = true - ) throws { - self.baseKey = try PrivateKeyImplementation(rawRepresentation: data, strict: strict) + /// - Parameter data: A data representation of the key. + /// - Throws: An error if the data representation does not create a private key for signing. + public init(dataRepresentation data: D) throws { + self.baseKey = try PrivateKeyImplementation(dataRepresentation: data) } /// Determines if two private keys are equal. @@ -95,12 +94,7 @@ public extension secp256k1.Schnorr { /// Schnorr x-only public key are implicit of the point being even, therefore this will always return `false`.` public var parity: Bool { - baseKey.strict ? false : baseKey.keyParity.boolValue - } - - /// Whether strict BIP340 adherence is enabled - public var strict: Bool { - baseKey.strict + baseKey.keyParity.boolValue } /// Generates a secp256k1 x-only public key. @@ -112,9 +106,9 @@ public extension secp256k1.Schnorr { /// Generates a secp256k1 x-only public key from a raw representation. /// - /// - Parameter data: A raw representation of the x-only public key. - public init(rawRepresentation data: D) { - self.baseKey = XonlyKeyImplementation(rawRepresentation: data, keyParity: 0) + /// - Parameter data: A data representation of the x-only public key. + public init(dataRepresentation data: D) { + self.baseKey = XonlyKeyImplementation(dataRepresentation: data, keyParity: 0) } /// Determines if two x-only keys are equal. @@ -133,20 +127,20 @@ public extension secp256k1.Schnorr { /// A Schnorr (Schnorr Digital Signature Scheme) Signature public extension secp256k1.Schnorr { - struct SchnorrSignature: ContiguousBytes, RawSignature { + struct SchnorrSignature: ContiguousBytes, DataSignature { /// Returns the raw signature in a fixed 64-byte format. - public var rawRepresentation: Data + public var dataRepresentation: Data /// Initializes SchnorrSignature from the raw representation. /// - Parameters: - /// - rawRepresentation: A raw representation of the key as a collection of contiguous bytes. + /// - dataRepresentation: A raw representation of the key as a collection of contiguous bytes. /// - Throws: If there is a failure with the rawRepresentation count - public init(rawRepresentation: D) throws { - guard rawRepresentation.count == secp256k1.Schnorr.signatureByteCount else { + public init(dataRepresentation: D) throws { + guard dataRepresentation.count == secp256k1.Schnorr.signatureByteCount else { throw secp256k1Error.incorrectParameterSize } - self.rawRepresentation = Data(rawRepresentation) + self.dataRepresentation = Data(dataRepresentation) } /// Initializes SchnorrSignature from the raw representation. @@ -158,7 +152,7 @@ public extension secp256k1.Schnorr { throw secp256k1Error.incorrectParameterSize } - self.rawRepresentation = dataRepresentation + self.dataRepresentation = dataRepresentation } /// Invokes the given closure with a buffer pointer covering the raw bytes of the digest. @@ -167,7 +161,7 @@ public extension secp256k1.Schnorr { /// - Throws: If there is a failure with underlying `withUnsafeBytes` /// - Returns: The signature as returned from the body closure. public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { - try rawRepresentation.withUnsafeBytes(body) + try dataRepresentation.withUnsafeBytes(body) } } } @@ -254,7 +248,8 @@ extension secp256k1.Schnorr.PrivateKey: DigestSigner, Signer { /// - Throws: If there is a failure creating the context or signature. public func signature( message: inout [UInt8], - auxiliaryRand: UnsafeMutableRawPointer? + auxiliaryRand: UnsafeMutableRawPointer?, + strict: Bool = true ) throws -> secp256k1.Schnorr.SchnorrSignature { guard strict == false || message.count == secp256k1.Schnorr.xonlyByteCount else { throw secp256k1Error.incorrectParameterSize @@ -266,7 +261,7 @@ extension secp256k1.Schnorr.PrivateKey: DigestSigner, Signer { var signature = [UInt8](repeating: 0, count: secp256k1.Schnorr.signatureByteCount) var extraParams = secp256k1_schnorrsig_extraparams(magic: magic, noncefp: nil, ndata: auxiliaryRand) - guard secp256k1_keypair_create(context, &keypair, Array(rawRepresentation)).boolValue, + guard secp256k1_keypair_create(context, &keypair, Array(dataRepresentation)).boolValue, secp256k1_schnorrsig_sign_custom( context, &signature, @@ -334,7 +329,7 @@ extension secp256k1.Schnorr.XonlyKey: DigestValidator, DataValidator { return secp256k1_xonly_pubkey_parse(context, &pubKey, bytes).boolValue && secp256k1_schnorrsig_verify( context, - signature.rawRepresentation.bytes, + signature.dataRepresentation.bytes, message, message.count, &pubKey diff --git a/Sources/zkp/Tweak.swift b/Sources/zkp/Tweak.swift index d526277..9df0a29 100644 --- a/Sources/zkp/Tweak.swift +++ b/Sources/zkp/Tweak.swift @@ -23,7 +23,7 @@ public extension secp256k1.Signing.PrivateKey { throw secp256k1Error.underlyingCryptoError } - return try Self(rawRepresentation: privateBytes) + return try Self(dataRepresentation: privateBytes) } /// Create a new `PrivateKey` by adding tweak to the secret key. When tweaking x-only keys, @@ -45,7 +45,7 @@ public extension secp256k1.Signing.PrivateKey { throw secp256k1Error.underlyingCryptoError } - return try Self(rawRepresentation: privateBytes) + return try Self(dataRepresentation: privateBytes) } /// Create a new `PrivateKey` by multiplying tweak to the secret key. @@ -60,7 +60,7 @@ public extension secp256k1.Signing.PrivateKey { throw secp256k1Error.underlyingCryptoError } - return try Self(rawRepresentation: privateBytes) + return try Self(dataRepresentation: privateBytes) } } @@ -82,7 +82,7 @@ public extension secp256k1.Signing.PublicKey { throw secp256k1Error.underlyingCryptoError } - return try Self(rawRepresentation: pubKeyBytes, format: format) + return try Self(dataRepresentation: pubKeyBytes, format: format) } /// Create a new `PublicKey` by multiplying tweak to the public key. @@ -102,7 +102,7 @@ public extension secp256k1.Signing.PublicKey { throw secp256k1Error.underlyingCryptoError } - return try Self(rawRepresentation: pubKeyBytes, format: format) + return try Self(dataRepresentation: pubKeyBytes, format: format) } } @@ -128,6 +128,6 @@ public extension secp256k1.Signing.XonlyKey { throw secp256k1Error.underlyingCryptoError } - return Self(rawRepresentation: xonlyBytes, keyParity: keyParity) + return Self(dataRepresentation: xonlyBytes, keyParity: keyParity) } } diff --git a/Sources/zkp/secp256k1.swift b/Sources/zkp/secp256k1.swift index 57f637b..5a8bb78 100644 --- a/Sources/zkp/secp256k1.swift +++ b/Sources/zkp/secp256k1.swift @@ -110,6 +110,11 @@ extension secp256k1 { /// Backing private key object private var privateBytes: SecureBytes + /// Backing secp256k1 private key object + var key: SecureBytes { + privateBytes + } + /// Backing public key object @usableFromInline let publicBytes: [UInt8] @@ -119,9 +124,6 @@ extension secp256k1 { /// Backing public key format @usableFromInline let format: secp256k1.Format - /// Backing strict adherence to BIP340 - @usableFromInline let strict: Bool - /// Backing key parity @usableFromInline var keyParity: Int32 @@ -130,30 +132,34 @@ extension secp256k1 { PublicKeyImplementation(publicBytes, xonly: xonlyBytes, keyParity: keyParity, format: format) } - /// Backing secp256k1 private key object - var key: SecureBytes { - privateBytes + /// Negates a secret key in place. + @usableFromInline var negation: PrivateKeyImplementation { + get throws { + var privateBytes = privateBytes.bytes + guard secp256k1_ec_seckey_negate(secp256k1.Context.rawRepresentation, &privateBytes).boolValue else { + throw secp256k1Error.underlyingCryptoError + } + + return try PrivateKeyImplementation(dataRepresentation: privateBytes, format: format) + } } /// A data representation of the backing private key - @usableFromInline var rawRepresentation: Data { + @usableFromInline var dataRepresentation: Data { Data(privateBytes) } /// Backing initialization that creates a random secp256k1 private key for signing - @usableFromInline init(format: secp256k1.Format = .compressed, strict: Bool = false) throws { + @usableFromInline init(format: secp256k1.Format = .compressed) throws { let privateKey = SecureBytes(count: secp256k1.ByteDetails.count) - // Verify Private Key here self.keyParity = 0 self.format = format - self.strict = strict self.privateBytes = privateKey - self.publicBytes = try PublicKeyImplementation.generate(bytes: &privateBytes, format: format, strict: strict) + self.publicBytes = try PublicKeyImplementation.generate(bytes: &privateBytes, format: format) self.xonlyBytes = try XonlyKeyImplementation.generate( bytes: publicBytes, keyParity: &keyParity, - format: format, - strict: strict + format: format ) } @@ -161,22 +167,19 @@ extension secp256k1 { /// - Parameter data: A raw representation of the key. /// - Throws: An error is thrown when the raw representation does not create a private key for signing. init( - rawRepresentation data: D, - format: secp256k1.Format = .compressed, - strict: Bool = false + dataRepresentation data: D, + format: secp256k1.Format = .compressed ) throws { let privateKey = SecureBytes(bytes: data) // Verify Private Key here self.keyParity = 0 self.format = format - self.strict = strict self.privateBytes = privateKey - self.publicBytes = try PublicKeyImplementation.generate(bytes: &privateBytes, format: format, strict: strict) + self.publicBytes = try PublicKeyImplementation.generate(bytes: &privateBytes, format: format) self.xonlyBytes = try XonlyKeyImplementation.generate( bytes: publicBytes, keyParity: &keyParity, - format: format, - strict: strict + format: format ) } } @@ -195,40 +198,58 @@ extension secp256k1 { /// A key format representation of the backing public key @usableFromInline let format: secp256k1.Format - /// Backing strict adherence to BIP340 - @usableFromInline let strict: Bool - /// Backing implementation for a public key object @usableFromInline var xonly: XonlyKeyImplementation { - XonlyKeyImplementation(xonlyBytes, keyParity: keyParity, strict: strict) + XonlyKeyImplementation(xonlyBytes, keyParity: keyParity) } /// A data representation of the backing public key - @usableFromInline var rawRepresentation: Data { + @usableFromInline var dataRepresentation: Data { Data(bytes) } - /// Backing initialization that generates a secp256k1 public key from only a raw representation and key format. + /// A raw representation of the backing public key + @usableFromInline var rawRepresentation: secp256k1_pubkey { + var pubKey = secp256k1_pubkey() + dataRepresentation.copyToUnsafeMutableBytes(of: &pubKey.data) + return pubKey + } + + /// Negates a public key in place. + @usableFromInline var negation: PublicKeyImplementation { + get throws { + let context = secp256k1.Context.rawRepresentation + var key = rawRepresentation + var keyLength = format.length + var bytes = [UInt8](repeating: 0, count: keyLength) + + guard secp256k1_ec_pubkey_negate(context, &key).boolValue, + secp256k1_ec_pubkey_serialize(context, &bytes, &keyLength, &key, format.rawValue).boolValue else { + throw secp256k1Error.underlyingCryptoError + } + + return try PublicKeyImplementation(dataRepresentation: bytes, format: format) + } + } + + /// Backing initialization that generates a secp256k1 public key from only a data representation and key format. /// - Parameters: - /// - data: A raw representation of the public key. + /// - data: A data representation of the public key. /// - format: an enum that represents the format of the public key @usableFromInline init( - rawRepresentation data: D, - format: secp256k1.Format, - strict: Bool = false + dataRepresentation data: D, + format: secp256k1.Format ) throws { var keyParity = Int32() self.xonlyBytes = try XonlyKeyImplementation.generate( bytes: data.bytes, keyParity: &keyParity, - format: format, - strict: strict + format: format ) self.bytes = data.bytes self.format = format - self.strict = strict self.keyParity = keyParity } @@ -238,12 +259,10 @@ extension secp256k1 { _ bytes: [UInt8], xonly: [UInt8], keyParity: Int32, - format: secp256k1.Format, - strict: Bool = false + format: secp256k1.Format ) { self.bytes = bytes self.format = format - self.strict = strict self.xonlyBytes = xonly self.keyParity = keyParity } @@ -254,7 +273,6 @@ extension secp256k1 { let yCoord: [UInt8] = xonlyKey.keyParity.boolValue ? [3] : [2] self.format = .compressed - self.strict = xonlyKey.strict self.xonlyBytes = xonlyKey.bytes self.keyParity = xonlyKey.keyParity self.bytes = yCoord + xonlyKey.bytes @@ -278,7 +296,7 @@ extension secp256k1 { var pubBytes = [UInt8](repeating: 0, count: pubKeyLen) var recoverySignature = secp256k1_ecdsa_recoverable_signature() - signature.rawRepresentation.copyToUnsafeMutableBytes(of: &recoverySignature.data) + signature.dataRepresentation.copyToUnsafeMutableBytes(of: &recoverySignature.data) guard secp256k1_ecdsa_recover(context, &pubKey, &recoverySignature, Array(digest)).boolValue, secp256k1_ec_pubkey_serialize(context, &pubBytes, &pubKeyLen, &pubKey, format.rawValue).boolValue else { @@ -288,13 +306,11 @@ extension secp256k1 { self.xonlyBytes = try XonlyKeyImplementation.generate( bytes: pubBytes, keyParity: &keyParity, - format: format, - strict: false + format: format ) self.keyParity = keyParity self.format = format - self.strict = false self.bytes = pubBytes } @@ -304,8 +320,7 @@ extension secp256k1 { /// - Throws: An error is thrown when the bytes does not create a public key. static func generate( bytes privateBytes: inout SecureBytes, - format: secp256k1.Format, - strict: Bool + format: secp256k1.Format ) throws -> [UInt8] { guard privateBytes.count == secp256k1.ByteDetails.count else { throw secp256k1Error.incorrectKeySize @@ -313,27 +328,13 @@ extension secp256k1 { let context = secp256k1.Context.rawRepresentation var pubKeyLen = format.length - var keyParity = Int32() var pubKey = secp256k1_pubkey() - var keypair = secp256k1_keypair() - var xonly = secp256k1_xonly_pubkey() - var pubBytes = [UInt8](repeating: 0, count: format.length) - var secBytes = privateBytes.bytes - - // Verify seckey and get key parity for BIP340 - guard secp256k1_keypair_create(context, &keypair, privateBytes.bytes).boolValue, - secp256k1_keypair_pub(context, &pubKey, &keypair).boolValue, - secp256k1_keypair_xonly_pub(context, &xonly, &keyParity, &keypair).boolValue else { - throw secp256k1Error.underlyingCryptoError - } - - // For BIP340, negate the private key when the Y coordinate is odd - if strict, keyParity.boolValue, - secp256k1_ec_seckey_negate(context, &secBytes).boolValue { - privateBytes = SecureBytes(bytes: secBytes) - } + var pubBytes = [UInt8](repeating: 0, count: pubKeyLen) - guard secp256k1_ec_pubkey_serialize(context, &pubBytes, &pubKeyLen, &pubKey, format.rawValue).boolValue else { + guard secp256k1_ec_seckey_verify(context, privateBytes.bytes).boolValue, + secp256k1_ec_pubkey_create(context, &pubKey, privateBytes.bytes).boolValue, + secp256k1_ec_pubkey_serialize(context, &pubBytes, &pubKeyLen, &pubKey, format.rawValue).boolValue + else { throw secp256k1Error.underlyingCryptoError } @@ -347,38 +348,38 @@ extension secp256k1 { @usableFromInline let bytes: [UInt8] /// A data representation of the backing x-only public key - @usableFromInline var rawRepresentation: Data { + @usableFromInline var dataRepresentation: Data { Data(bytes) } + /// A raw representation of the backing x-only public key + @usableFromInline var rawRepresentation: secp256k1_xonly_pubkey { + var xonlyKey = secp256k1_xonly_pubkey() + dataRepresentation.copyToUnsafeMutableBytes(of: &xonlyKey.data) + return xonlyKey + } + /// Backing key parity object @usableFromInline let keyParity: Int32 - /// Backing strict adherence to BIP340 - @usableFromInline let strict: Bool - /// Backing initialization that generates a x-only public key from a raw representation. - /// - Parameter data: A raw representation of the key. + /// - Parameter data: A data representation of the key. @usableFromInline init( - rawRepresentation data: D, - keyParity: Int32, - strict: Bool = false + dataRepresentation data: D, + keyParity: Int32 ) { self.bytes = data.bytes self.keyParity = keyParity - self.strict = strict } /// Backing initialization that sets the public key from a x-only public key object. /// - Parameter bytes: a x-only public key in byte form @usableFromInline init( _ bytes: [UInt8], - keyParity: Int32, - strict: Bool = false + keyParity: Int32 ) { self.bytes = bytes self.keyParity = keyParity - self.strict = strict } /// Create a x-only public key from bytes representation. @@ -388,8 +389,7 @@ extension secp256k1 { static func generate( bytes publicBytes: [UInt8], keyParity: inout Int32, - format: secp256k1.Format, - strict: Bool = false + format: secp256k1.Format ) throws -> [UInt8] { guard publicBytes.count == format.length else { throw secp256k1Error.incorrectKeySize diff --git a/Tests/zkpTests/secp256k1Tests.swift b/Tests/zkpTests/secp256k1Tests.swift index 3f93734..223c8d6 100644 --- a/Tests/zkpTests/secp256k1Tests.swift +++ b/Tests/zkpTests/secp256k1Tests.swift @@ -144,11 +144,11 @@ final class secp256k1Tests: XCTestCase { let expectedPrivateKey = "7da12cc39bb4189ac72d34fc2225df5cf36aaacdcac7e5a43963299bc8d888ed" let expectedPublicKey = "023521df7b94248ffdf0d37f738a4792cc3932b6b1b89ef71cddde8251383b26e7" let privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes) // Verify the keys matches the expected keys output - XCTAssertEqual(expectedPrivateKey, String(bytes: privateKey.rawRepresentation)) - XCTAssertEqual(expectedPublicKey, String(bytes: privateKey.publicKey.rawRepresentation)) + XCTAssertEqual(expectedPrivateKey, String(bytes: privateKey.dataRepresentation)) + XCTAssertEqual(expectedPublicKey, String(bytes: privateKey.publicKey.dataRepresentation)) } /// SHA256 test @@ -179,13 +179,13 @@ final class secp256k1Tests: XCTestCase { let expectedSignature = "rPnhleCU8vQOthm5h4gX/5UbmxH6w3zw1ykAmLvvtXT4YGKBoiMaP8eBBF8upN8IaTYmO7+o0Vyhf+cODD1uVg==" let expectedPrivateKey = "5f6d5afecc677d66fb3d41eee7a8ad8195659ceff588edaf416a9a17daf38fdd" let privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes) let messageData = "We're all Satoshi Nakamoto and a bit of Harold Thomas Finney II.".data(using: .utf8)! let signature = try! privateKey.signature(for: messageData) // Verify the signature matches the expected output - XCTAssertEqual(expectedSignature, signature.rawRepresentation.base64EncodedString()) + XCTAssertEqual(expectedSignature, signature.dataRepresentation.base64EncodedString()) XCTAssertEqual(expectedDerSignature, try! signature.derRepresentation.base64EncodedString()) } @@ -195,18 +195,18 @@ final class secp256k1Tests: XCTestCase { let expectedSignature = "rPnhleCU8vQOthm5h4gX/5UbmxH6w3zw1ykAmLvvtXT4YGKBoiMaP8eBBF8upN8IaTYmO7+o0Vyhf+cODD1uVg==" let expectedPrivateKey = "5f6d5afecc677d66fb3d41eee7a8ad8195659ceff588edaf416a9a17daf38fdd" let privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey = try! secp256k1.Recovery.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Recovery.PrivateKey(dataRepresentation: privateKeyBytes) let messageData = "We're all Satoshi Nakamoto and a bit of Harold Thomas Finney II.".data(using: .utf8)! let recoverySignature = try! privateKey.signature(for: messageData) // Verify the recovery signature matches the expected output - XCTAssertEqual(expectedRecoverySignature, recoverySignature.rawRepresentation.base64EncodedString()) + XCTAssertEqual(expectedRecoverySignature, recoverySignature.dataRepresentation.base64EncodedString()) let signature = try! recoverySignature.normalize // Verify the signature matches the expected output - XCTAssertEqual(expectedSignature, signature.rawRepresentation.base64EncodedString()) + XCTAssertEqual(expectedSignature, signature.dataRepresentation.base64EncodedString()) XCTAssertEqual(expectedDerSignature, try! signature.derRepresentation.base64EncodedString()) } @@ -214,18 +214,18 @@ final class secp256k1Tests: XCTestCase { let expectedRecoverySignature = "rPnhleCU8vQOthm5h4gX/5UbmxH6w3zw1ykAmLvvtXT4YGKBoiMaP8eBBF8upN8IaTYmO7+o0Vyhf+cODD1uVgE=" let expectedPrivateKey = "5f6d5afecc677d66fb3d41eee7a8ad8195659ceff588edaf416a9a17daf38fdd" let privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey = try! secp256k1.Recovery.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Recovery.PrivateKey(dataRepresentation: privateKeyBytes) let messageData = "We're all Satoshi Nakamoto and a bit of Harold Thomas Finney II.".data(using: .utf8)! let recoverySignature = try! privateKey.signature(for: messageData) // Verify the recovery signature matches the expected output - XCTAssertEqual(expectedRecoverySignature, recoverySignature.rawRepresentation.base64EncodedString()) + XCTAssertEqual(expectedRecoverySignature, recoverySignature.dataRepresentation.base64EncodedString()) let publicKey = try! secp256k1.Recovery.PublicKey(messageData, signature: recoverySignature) // Verify the recovered public key matches the expected public key - XCTAssertEqual(publicKey.rawRepresentation, privateKey.publicKey.rawRepresentation) + XCTAssertEqual(publicKey.dataRepresentation, privateKey.publicKey.dataRepresentation) } func testSchnorrSigning() { @@ -233,7 +233,7 @@ final class secp256k1Tests: XCTestCase { let expectedSignature = "e907831f80848d1069a5371b402410364bdf1c5f8307b0084c55f1ce2dca821525f66a4a85ea8b71e482a74f382d2ce5ebeee8fdb2172f477df4900d310536c0" let expectedPrivateKey = "0000000000000000000000000000000000000000000000000000000000000003" let privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey = try! secp256k1.Schnorr.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Schnorr.PrivateKey(dataRepresentation: privateKeyBytes) var messageDigest = try! "0000000000000000000000000000000000000000000000000000000000000000".bytes var auxRand = try! "0000000000000000000000000000000000000000000000000000000000000000".bytes @@ -241,14 +241,14 @@ final class secp256k1Tests: XCTestCase { let signature = try! privateKey.signature(message: &messageDigest, auxiliaryRand: &auxRand) // Verify the signature matches the expected output - XCTAssertEqual(expectedSignature, String(bytes: Array(signature.rawRepresentation))) - XCTAssertEqual(expectedDerSignature, signature.rawRepresentation.base64EncodedString()) + XCTAssertEqual(expectedSignature, String(bytes: Array(signature.dataRepresentation))) + XCTAssertEqual(expectedDerSignature, signature.dataRepresentation.base64EncodedString()) } func testVerifying() { let expectedPrivateKey = "5f6d5afecc677d66fb3d41eee7a8ad8195659ceff588edaf416a9a17daf38fdd" let privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes) let messageData = "We're all Satoshi Nakamoto and a bit of Harold Thomas Finney II.".data(using: .utf8)! let signature = try! privateKey.signature(for: messageData) @@ -261,15 +261,15 @@ final class secp256k1Tests: XCTestCase { let expectedPrivateKey = "4894b8087f428971b55ff96e16f7127340138bc84e7973821a224cad02055975" let expectedSignature = "ad57c21d383ef8ac799adfd469a221c40ef9f09563a16682b9ab1edc46c33d6d6a1d719761d269e87ab971e0ffafc1618a4666a4f9aef4abddc3ea9fc0cd5b12" let privateKeyBytes = try! expectedPrivateKey.bytes - let throwKey = try! secp256k1.Schnorr.PrivateKey(rawRepresentation: privateKeyBytes) - let privateKey = try! secp256k1.Schnorr.PrivateKey(rawRepresentation: privateKeyBytes, strict: false) + let throwKey = try! secp256k1.Schnorr.PrivateKey(dataRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Schnorr.PrivateKey(dataRepresentation: privateKeyBytes) var messageDigest = "We're all Satoshi Nakamoto and a bit of Harold Thomas Finney II.".data(using: .utf8)!.bytes var auxRand = try! "f50c8c99e39a82f125fa83186b5f2483f39fb0fb56269c755689313a177be6ea".bytes - let signature = try! privateKey.signature(message: &messageDigest, auxiliaryRand: &auxRand) + let signature = try! privateKey.signature(message: &messageDigest, auxiliaryRand: &auxRand, strict: false) // Test the verification of the signature output - XCTAssertEqual(expectedSignature, String(bytes: signature.rawRepresentation.bytes)) + XCTAssertEqual(expectedSignature, String(bytes: signature.dataRepresentation.bytes)) XCTAssertTrue(privateKey.xonly.isValid(signature, for: &messageDigest)) XCTAssertThrowsError(try throwKey.signature(message: &messageDigest, auxiliaryRand: &auxRand)) } @@ -277,7 +277,7 @@ final class secp256k1Tests: XCTestCase { func testSchnorrVerifying() { let expectedPrivateKey = "0000000000000000000000000000000000000000000000000000000000000003" let privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey = try! secp256k1.Schnorr.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Schnorr.PrivateKey(dataRepresentation: privateKeyBytes) var messageDigest = try! "0000000000000000000000000000000000000000000000000000000000000000".bytes var auxRand = try! "0000000000000000000000000000000000000000000000000000000000000000".bytes @@ -291,7 +291,7 @@ final class secp256k1Tests: XCTestCase { let expectedDerSignature = Data(base64Encoded: "MEQCIHS177uYACnX8HzD+hGbG5X/F4iHuRm2DvTylOCV4fmsAiBWbj0MDud/oVzRqL87JjZpCN+kLl8Egcc/GiOigWJg+A==", options: .ignoreUnknownCharacters)! let expectedPrivateKey = "5f6d5afecc677d66fb3d41eee7a8ad8195659ceff588edaf416a9a17daf38fdd" let privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes) let messageData = "We're all Satoshi Nakamoto and a bit of Harold Thomas Finney II.".data(using: .utf8)! let signature = try! secp256k1.Signing.ECDSASignature(derRepresentation: expectedDerSignature) @@ -308,22 +308,22 @@ final class secp256k1Tests: XCTestCase { let privateKey = try! secp256k1.Signing.PrivateKey() XCTAssertEqual(privateKey.publicKey.format, .compressed) - XCTAssertEqual(privateKey.publicKey.rawRepresentation.count, secp256k1.Format.compressed.length) + XCTAssertEqual(privateKey.publicKey.dataRepresentation.count, secp256k1.Format.compressed.length) } func testUncompressedPublicKey() { let privateKey = try! secp256k1.Signing.PrivateKey(format: .uncompressed) XCTAssertEqual(privateKey.publicKey.format, .uncompressed) - XCTAssertEqual(privateKey.publicKey.rawRepresentation.count, secp256k1.Format.uncompressed.length) + XCTAssertEqual(privateKey.publicKey.dataRepresentation.count, secp256k1.Format.uncompressed.length) } func testUncompressedPublicKeyWithKey() { let privateBytes = try! "703d3b63e84421e59f9359f8b27c25365df9d85b6b1566e3168412fa599c12f4".bytes - let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateBytes, format: .uncompressed) + let privateKey = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateBytes, format: .uncompressed) XCTAssertEqual(privateKey.publicKey.format, .uncompressed) - XCTAssertEqual(privateKey.publicKey.rawRepresentation.count, secp256k1.Format.uncompressed.length) + XCTAssertEqual(privateKey.publicKey.dataRepresentation.count, secp256k1.Format.uncompressed.length) let expectedPublicKeyString = """ 04c9c68596824505dd6cd1993a16452b4b1a13bacde56f80e9049fd03850cce137c1fa4acb7bef7edcc04f4fa29e071ea17e34fa07fa5d87b5ebf6340df6558498 @@ -333,13 +333,13 @@ final class secp256k1Tests: XCTestCase { let expectedPublicKey = try! expectedPublicKeyString.bytes // Verify the generated public key matches the expected public key - XCTAssertEqual(expectedPublicKey, privateKey.publicKey.rawRepresentation.bytes) - XCTAssertEqual(expectedPublicKeyString, String(bytes: privateKey.publicKey.rawRepresentation.bytes)) + XCTAssertEqual(expectedPublicKey, privateKey.publicKey.bytes) + XCTAssertEqual(expectedPublicKeyString, String(bytes: privateKey.publicKey.bytes)) } func testInvalidRawSignature() { XCTAssertThrowsError( - try secp256k1.Signing.ECDSASignature(rawRepresentation: Data()), + try secp256k1.Signing.ECDSASignature(dataRepresentation: Data()), "Thrown Error", { error in XCTAssertEqual(error as? secp256k1Error, secp256k1Error.incorrectParameterSize) } @@ -366,7 +366,7 @@ final class secp256k1Tests: XCTestCase { let privateKeyBytes = try! expectedPrivateKey.bytes XCTAssertThrowsError( - try secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes), + try secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes), "Thrown Error", { error in XCTAssertEqual(error as? secp256k1Error, secp256k1Error.incorrectKeySize) } @@ -376,15 +376,15 @@ final class secp256k1Tests: XCTestCase { func testKeypairSafeCompare() { let expectedPrivateKey = "7da12cc39bb4189ac72d34fc2225df5cf36aaacdcac7e5a43963299bc8d888ed" var privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey0 = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes) - let privateKey1 = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey0 = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes) + let privateKey1 = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes) // Verify the keys match XCTAssertEqual(privateKey0, privateKey1) let expectedFailingPrivateKey = "7da12cc39bb4189ac72d34fc2225df5cf36aaacdcac7e5a43963299bc8d888dd" privateKeyBytes = try! expectedFailingPrivateKey.bytes - let privateKey2 = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey2 = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes) XCTAssertNotEqual(privateKey0, privateKey2) } @@ -414,15 +414,15 @@ final class secp256k1Tests: XCTestCase { let expectedPublicKey = "023521df7b94248ffdf0d37f738a4792cc3932b6b1b89ef71cddde8251383b26e7" let expectedTweakedPrivateKey = "5f0da318c6e02f653a789950e55756ade9f194e1ec228d7f368de1bd821322b6" let privateKeyBytes = try! expectedPrivateKey.bytes - let privateKey = try! secp256k1.Signing.PrivateKey(rawRepresentation: privateKeyBytes) + let privateKey = try! secp256k1.Signing.PrivateKey(dataRepresentation: privateKeyBytes) let tweak = SHA256.hash(data: expectedPrivateKey.data(using: .utf8)!) // tweak the private key let tweakedPrivateKey = try! privateKey.add(xonly: Array(tweak)) // Verify the keys matches the expected keys output - XCTAssertEqual(String(bytes: tweakedPrivateKey.rawRepresentation), expectedTweakedPrivateKey) - XCTAssertEqual(expectedPublicKey, String(bytes: privateKey.publicKey.rawRepresentation)) + XCTAssertEqual(String(bytes: tweakedPrivateKey.dataRepresentation), expectedTweakedPrivateKey) + XCTAssertEqual(expectedPublicKey, String(bytes: privateKey.publicKey.dataRepresentation)) } func testKeyAgreement() { @@ -432,8 +432,8 @@ final class secp256k1Tests: XCTestCase { let privateBytes1 = try! privateString1.bytes let privateBytes2 = try! privateString2.bytes - let privateKey1 = try! secp256k1.KeyAgreement.PrivateKey(rawRepresentation: privateBytes1) - let privateKey2 = try! secp256k1.KeyAgreement.PrivateKey(rawRepresentation: privateBytes2) + let privateKey1 = try! secp256k1.KeyAgreement.PrivateKey(dataRepresentation: privateBytes1) + let privateKey2 = try! secp256k1.KeyAgreement.PrivateKey(dataRepresentation: privateBytes2) let sharedSecret1 = try! privateKey1.sharedSecretFromKeyAgreement(with: privateKey2.publicKey) let sharedSecret2 = try! privateKey2.sharedSecretFromKeyAgreement(with: privateKey1.publicKey) @@ -445,27 +445,27 @@ final class secp256k1Tests: XCTestCase { let privateSign1 = try! secp256k1.Signing.PrivateKey() let privateSign2 = try! secp256k1.Signing.PrivateKey() - let privateKey1 = try! secp256k1.KeyAgreement.PrivateKey(rawRepresentation: privateSign1.rawRepresentation) - let privateKey2 = try! secp256k1.KeyAgreement.PrivateKey(rawRepresentation: privateSign2.rawRepresentation) + let privateKey1 = try! secp256k1.KeyAgreement.PrivateKey(dataRepresentation: privateSign1.dataRepresentation) + let privateKey2 = try! secp256k1.KeyAgreement.PrivateKey(dataRepresentation: privateSign2.dataRepresentation) - let publicKey1 = try! secp256k1.KeyAgreement.PublicKey(rawRepresentation: privateKey1.publicKey.rawRepresentation) + let publicKey1 = try! secp256k1.KeyAgreement.PublicKey(dataRepresentation: privateKey1.publicKey.dataRepresentation) let sharedSecret1 = try! privateKey1.sharedSecretFromKeyAgreement(with: privateKey2.publicKey) let sharedSecret2 = try! privateKey2.sharedSecretFromKeyAgreement(with: publicKey1) XCTAssertEqual(sharedSecret1.bytes, sharedSecret2.bytes) - let sharedSecretSign1 = try! secp256k1.Signing.PrivateKey(rawRepresentation: sharedSecret1.bytes) - let sharedSecretSign2 = try! secp256k1.Signing.PrivateKey(rawRepresentation: sharedSecret2.bytes) + let sharedSecretSign1 = try! secp256k1.Signing.PrivateKey(dataRepresentation: sharedSecret1.bytes) + let sharedSecretSign2 = try! secp256k1.Signing.PrivateKey(dataRepresentation: sharedSecret2.bytes) let privateTweak1 = try! sharedSecretSign1.add(xonly: privateSign1.publicKey.xonly.bytes) let publicTweak2 = try! sharedSecretSign2.publicKey.add(privateSign1.publicKey.xonly.bytes) let xonlyTweak2 = try! sharedSecretSign2.publicKey.xonly.add(privateSign1.publicKey.xonly.bytes) if sharedSecretSign2.publicKey.xonly.parity { - XCTAssertNotEqual(privateTweak1.publicKey.rawRepresentation, publicTweak2.rawRepresentation) + XCTAssertNotEqual(privateTweak1.publicKey.dataRepresentation, publicTweak2.dataRepresentation) } else { - XCTAssertEqual(privateTweak1.publicKey.rawRepresentation, publicTweak2.rawRepresentation) + XCTAssertEqual(privateTweak1.publicKey.dataRepresentation, publicTweak2.dataRepresentation) } XCTAssertEqual(privateTweak1.publicKey.xonly.bytes, xonlyTweak2.bytes) @@ -475,7 +475,7 @@ final class secp256k1Tests: XCTestCase { let privateKey = try! secp256k1.Signing.PrivateKey() let publicKey = secp256k1.Signing.PublicKey(xonlyKey: privateKey.publicKey.xonly) - XCTAssertEqual(privateKey.publicKey.rawRepresentation, publicKey.rawRepresentation) + XCTAssertEqual(privateKey.publicKey.dataRepresentation, publicKey.dataRepresentation) } func testTapscript() { @@ -490,7 +490,7 @@ final class secp256k1Tests: XCTestCase { let array = withUnsafeBytes(of: &value) { Array($0).prefix(numberOfBytes) } let aliceBytes = try! "2bd806c97f0e00af1a1fc3328fa763a9269723c8db8fac4f93af71db186d6e90".bytes - let alice = try! secp256k1.Signing.PrivateKey(rawRepresentation: aliceBytes) + let alice = try! secp256k1.Signing.PrivateKey(dataRepresentation: aliceBytes) let aliceScript = Data([UInt8(array.count)] + array) + OP_CHECKSEQUENCEVERIFY + OP_DROP + @@ -506,7 +506,7 @@ final class secp256k1Tests: XCTestCase { XCTAssertEqual(String(bytes: Array(aliceLeafHash).bytes), aliceExpectedLeafHash) let bobBytes = try! "81b637d8fcd2c6da6359e6963113a1170de795e4b725b84d1e0b4cfd9ec58ce9".bytes - let bob = try! secp256k1.Signing.PrivateKey(rawRepresentation: bobBytes) + let bob = try! secp256k1.Signing.PrivateKey(dataRepresentation: bobBytes) let preimageBytes = try! "6c60f404f8167a38fc70eaf8aa17ac351023bef86bcb9d1086a19afe95bd5333".bytes let bobScript = OP_SHA256 + Data([UInt8(preimageBytes.count)] + preimageBytes.bytes) + @@ -551,19 +551,13 @@ final class secp256k1Tests: XCTestCase { let privateBytes = try! "56baa476b36a5b1548279f5bf57b82db39e594aee7912cde30977b8e80e6edca".bytes let negatedBytes = try! "a9455b894c95a4eab7d860a40a847d2380c94837c7b7735d8f3ae2fe4f4f5377".bytes - let privateKey = try! secp256k1.Schnorr.PrivateKey(rawRepresentation: privateBytes) - let negatedKey = try! secp256k1.Schnorr.PrivateKey(rawRepresentation: negatedBytes) - let notStrictKey = try! secp256k1.Schnorr.PrivateKey(rawRepresentation: privateBytes, strict: false) + let privateKey = try! secp256k1.Schnorr.PrivateKey(dataRepresentation: privateBytes) + let negatedKey = try! secp256k1.Schnorr.PrivateKey(dataRepresentation: negatedBytes).negation XCTAssertEqual(privateKey, negatedKey) - XCTAssertEqual(privateKey.rawRepresentation, negatedKey.rawRepresentation) + XCTAssertEqual(privateKey.dataRepresentation, negatedKey.dataRepresentation) XCTAssertEqual(privateKey.xonly, negatedKey.xonly) XCTAssertEqual(privateKey.xonly.bytes, negatedKey.xonly.bytes) - - XCTAssertNotEqual(privateKey, notStrictKey) - XCTAssertNotEqual(privateKey.rawRepresentation, notStrictKey.rawRepresentation) - XCTAssertEqual(privateKey.xonly, notStrictKey.xonly) - XCTAssertEqual(privateKey.xonly.bytes, notStrictKey.xonly.bytes) } static var allTests = [