-
Notifications
You must be signed in to change notification settings - Fork 172
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 PublicKey
s conform to Hashable
#175
Open
Sajjon
wants to merge
5
commits into
apple:main
Choose a base branch
from
Sajjon:curve25519_publickeys_hashable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66e9400
Make Curve25519 PublicKeys Equatable
Sajjon ed6f754
Merge branch 'main' into curve25519_publickeys_equatable
Sajjon cd98129
Update tests
Sajjon f581064
Make Curve25519 PublicKey's conform to Hashable
Sajjon 5498bbc
Merge branch 'main' into curve25519_publickeys_hashable
Sajjon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return lhs.rawRepresentation == rhs.rawRepresentation | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
self.withUnsafeBytes { hasher.combine(bytes: $0) } | ||
} | ||
} | ||
|
||
public struct PrivateKey: ECPrivateKey, DiffieHellmanKeyAgreement { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,4 +75,54 @@ class EdDSATests: XCTestCase { | |
// This signature should be invalid | ||
XCTAssertFalse(privateKey.publicKey.isValidSignature(DispatchData.empty, for: DispatchData.empty)) | ||
} | ||
|
||
func testCurve25519SigningPublicKeyEquatable() throws { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// 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 | ||
) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.