Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Curve25519 PublicKeys conform to Equatable #173

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/Crypto/Keys/EC/Ed25519.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ extension Curve25519 {
}
}

public struct PublicKey {
public struct PublicKey: Equatable {
private var baseKey: Curve25519.Signing.Curve25519PublicKeyImpl

public init<D: ContiguousBytes>(rawRepresentation: D) throws {
Expand All @@ -75,6 +75,10 @@ extension Curve25519 {
var keyBytes: [UInt8] {
return self.baseKey.keyBytes
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.rawRepresentation == rhs.rawRepresentation
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/Crypto/Keys/EC/X25519Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension Curve25519 {
typealias Curve25519PublicKeyImpl = Curve25519.KeyAgreement.OpenSSLCurve25519PublicKeyImpl
#endif

public struct PublicKey: ECPublicKey {
public struct PublicKey: ECPublicKey, Equatable {
fileprivate var baseKey: Curve25519PublicKeyImpl

/// Initializes a Curve25519 Key for Key Agreement.
Expand Down Expand Up @@ -54,6 +54,10 @@ extension Curve25519 {
private func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try self.baseKey.keyBytes.withUnsafeBytes(body)
}

public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.rawRepresentation == rhs.rawRepresentation
}
}

public struct PrivateKey: ECPrivateKey, DiffieHellmanKeyAgreement {
Expand Down
35 changes: 35 additions & 0 deletions Tests/CryptoTests/Signatures/EdDSA/EdDSATests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,39 @@ class EdDSATests: XCTestCase {
// This signature should be invalid
XCTAssertFalse(privateKey.publicKey.isValidSignature(DispatchData.empty, for: DispatchData.empty))
}

func testCurve25519SigningPublicKeyEquatable() throws {
// Equality
let publicKey = Curve25519.Signing.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)

// Inequality

// The probability of this inequality check loop
// accidentally failing is... 1/2^246, i.e. low.
for _ in 0..<1024 {
XCTAssertNotEqual(
publicKey,
Curve25519.Signing.PrivateKey().publicKey
)
}

}

func testCurve25519KeyAgreementPublicKeyEquatable() throws {
// Equality
let publicKey = Curve25519.KeyAgreement.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)

// Inequality

// The probability of this inequality check loop
// accidentally failing is... 1/2^246, i.e. low.
for _ in 0..<1024 {
XCTAssertNotEqual(
publicKey,
Curve25519.KeyAgreement.PrivateKey().publicKey
)
}
}
}