Skip to content

Commit

Permalink
Adding Negation APIs (#360)
Browse files Browse the repository at this point in the history
Adding negation APIs
  • Loading branch information
csjones authored May 14, 2023
1 parent 0e6d4fa commit a0dd0d0
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 235 deletions.
51 changes: 36 additions & 15 deletions Sources/zkp/Asymmetric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<D: ContiguousBytes>(rawRepresentation data: D, format: secp256k1.Format = .compressed) throws {
self.baseKey = try PrivateKeyImplementation(rawRepresentation: data, format: format)
public init<D: ContiguousBytes>(dataRepresentation data: D, format: secp256k1.Format = .compressed) throws {
self.baseKey = try PrivateKeyImplementation(dataRepresentation: data, format: format)
}

/// Determines if two private keys are equal.
Expand All @@ -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
}

Expand All @@ -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.
Expand All @@ -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<D: ContiguousBytes>(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<D: ContiguousBytes>(dataRepresentation data: D, format: secp256k1.Format) throws {
self.baseKey = try PublicKeyImplementation(dataRepresentation: data, format: format)
}
}

Expand Down Expand Up @@ -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<D: ContiguousBytes>(rawRepresentation data: D, keyParity: Int32) {
self.baseKey = XonlyKeyImplementation(rawRepresentation: data, keyParity: keyParity)
public init<D: ContiguousBytes>(dataRepresentation data: D, keyParity: Int32) {
self.baseKey = XonlyKeyImplementation(dataRepresentation: data, keyParity: keyParity)
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions Sources/zkp/ECDH.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<D: ContiguousBytes>(rawRepresentation data: D, format: secp256k1.Format = .compressed) throws {
self.baseKey = try PublicKeyImplementation(rawRepresentation: data, format: format)
public init<D: ContiguousBytes>(dataRepresentation data: D, format: secp256k1.Format = .compressed) throws {
self.baseKey = try PublicKeyImplementation(dataRepresentation: data, format: format)
}

/// Initializes a secp256k1 public key for key agreement.
Expand All @@ -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 }
Expand All @@ -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.
Expand Down Expand Up @@ -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<D: ContiguousBytes>(rawRepresentation data: D, format: secp256k1.Format = .compressed) throws {
self.baseKey = try PrivateKeyImplementation(rawRepresentation: data, format: format)
public init<D: ContiguousBytes>(dataRepresentation data: D, format: secp256k1.Format = .compressed) throws {
self.baseKey = try PrivateKeyImplementation(dataRepresentation: data, format: format)
}

/// Initializes a secp256k1 private key for key agreement.
Expand All @@ -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 }
Expand Down
36 changes: 18 additions & 18 deletions Sources/zkp/ECDSA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

import Foundation

typealias NISTECDSASignature = RawSignature & DERSignature
typealias NISTECDSASignature = DataSignature & DERSignature

protocol RawSignature {
init<D: DataProtocol>(rawRepresentation: D) throws
var rawRepresentation: Data { get }
protocol DataSignature {
init<D: DataProtocol>(dataRepresentation: D) throws
var dataRepresentation: Data { get }
}

protocol DERSignature {
Expand All @@ -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<D: DataProtocol>(rawRepresentation: D) throws {
guard rawRepresentation.count == 4 * secp256k1.CurveDetails.coordinateByteCount else {
public init<D: DataProtocol>(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.
Expand All @@ -57,7 +57,7 @@ public extension secp256k1.Signing {
throw secp256k1Error.incorrectParameterSize
}

self.rawRepresentation = dataRepresentation
self.dataRepresentation = dataRepresentation
}

/// Initializes ECDSASignature from the DER representation.
Expand All @@ -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.
Expand All @@ -95,15 +95,15 @@ 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.
/// - Parameter body: A closure that takes a raw buffer pointer to the bytes of the digest and returns the digest.
/// - Throws: If there is a failure with underlying `withUnsafeBytes`
/// - Returns: The signature as returned from the body closure.
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
try rawRepresentation.withUnsafeBytes(body)
try dataRepresentation.withUnsafeBytes(body)
}

/// Serialize an ECDSA signature in compact (64 byte) format.
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -173,7 +173,7 @@ extension secp256k1.Signing.PrivateKey: DigestSigner {
context,
&signature,
Array(digest),
Array(rawRepresentation),
Array(dataRepresentation),
nil,
nil
).boolValue else {
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit a0dd0d0

Please sign in to comment.