Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions Tests/CryptoTests/Signatures/EdDSA/EdDSATests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,38 @@ class EdDSATests: XCTestCase {
// This signature should be invalid
XCTAssertFalse(privateKey.publicKey.isValidSignature(DispatchData.empty, for: DispatchData.empty))
}

func testCurve25519SigningPublicKeyEquatable() throws {
// Inequality

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

// Equality
let publicKey = Curve25519.Signing.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)
}

func testCurve25519KeyAgreementPublicKeyEquatable() throws {
// Inequality

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

// Equality
let publicKey = Curve25519.KeyAgreement.PrivateKey().publicKey
XCTAssertEqual(publicKey, publicKey)
}
}