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 Hashable #175

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
14 changes: 13 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: Hashable {
private var baseKey: Curve25519.Signing.Curve25519PublicKeyImpl

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

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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of #173 but I was unable to create a diff between just this PR and #173 since my branch is in a fork, otherwise I would have created this PR with the branch curve25519_publickeys_equatable as the target branch, so that we could have seen only the relevant diff.

return lhs.rawRepresentation == rhs.rawRepresentation
}

public func hash(into hasher: inout Hasher) {
return self.withUnsafeBytes { hasher.combine(bytes: $0) }
}
}
}
}
Expand Down
10 changes: 9 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, Hashable {
fileprivate var baseKey: Curve25519PublicKeyImpl

/// Initializes a Curve25519 Key for Key Agreement.
Expand Down Expand Up @@ -54,6 +54,14 @@ 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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of #173 but I was unable to create a diff between just this PR and #173 since my branch is in a fork, otherwise I would have created this PR with the branch curve25519_publickeys_equatable as the target branch, so that we could have seen only the relevant diff.

return lhs.rawRepresentation == rhs.rawRepresentation
}

public func hash(into hasher: inout Hasher) {
self.withUnsafeBytes { hasher.combine(bytes: $0) }
}
}

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

func testCurve25519SigningPublicKeyEquatable() throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of #173 but I was unable to create a diff between just this PR and #173 since my branch is in a fork, otherwise I would have created this PR with the branch curve25519_publickeys_equatable as the target branch, so that we could have seen only the relevant diff.

// 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
)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of #173 but I was unable to create a diff between just this PR and #173 since my branch is in a fork, otherwise I would have created this PR with the branch curve25519_publickeys_equatable as the target branch, so that we could have seen only the relevant diff.


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
)
}
}

func testCurve25519SigningPublicKeyHashable() throws {
let expectedCount = 1000
let set = Set((0..<expectedCount).map { _ in
Curve25519.Signing.PrivateKey().publicKey
})
XCTAssertEqual(set.count, expectedCount)
}

func testCurve25519KeyAgreementPublicKeyHashable() throws {
let expectedCount = 1000
let set = Set((0..<expectedCount).map { _ in
Curve25519.KeyAgreement.PrivateKey().publicKey
})
XCTAssertEqual(set.count, expectedCount)
}
}