Skip to content

ECDH: Handler with customizable output length #426

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion Sources/zkp/ECDH.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,19 @@ extension secp256k1.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement {
/// - Parameters:
/// - publicKeyShare: The public key to perform the ECDH with.
/// - handler: Closure for customizing a hash function; Defaults to nil.
/// - outputLength: Length of the shared secret output; Defaults to 32.
/// - data: Additional data for the hash function; Defaults to nil.
/// - Returns: Returns a shared secret.
/// - Throws: An error occurred while computing the shared secret.
public func sharedSecretFromKeyAgreement(
with publicKeyShare: secp256k1.KeyAgreement.PublicKey,
handler: HashFunctionType? = nil,
outputLength: Int = 32,
data: UnsafeMutableRawPointer? = nil
) throws -> SharedSecret {
let context = secp256k1.Context.rawRepresentation
var publicKey = secp256k1_pubkey()
var sharedSecret = [UInt8](repeating: 0, count: 32)
var sharedSecret = [UInt8](repeating: 0, count: outputLength)

guard secp256k1_ec_pubkey_parse(context, &publicKey, publicKeyShare.bytes, publicKeyShare.bytes.count).boolValue,
secp256k1_ecdh(context, &sharedSecret, &publicKey, baseKey.key.bytes, handler, data).boolValue else {
Expand Down
33 changes: 33 additions & 0 deletions Tests/zkpTests/secp256k1Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,39 @@ final class secp256k1Tests: XCTestCase {

XCTAssertEqual(secp256k1_ec_pubkey_create(context, &point, s_one), 1)
XCTAssertEqual(secp256k1_ecdh(context, &res, &point, s_one, nil, nil), 1)

// Test with handler and data.
var data = try! [UInt8](hexString: "abcdef")
let copyx : secp256k1.KeyAgreement.PrivateKey.HashFunctionType = {
(out, x, y, data) -> Int32 in
guard let out = out, let x = x else {
return 0;
}
out.initialize(from: x, count: 32)
return 1
}
XCTAssertEqual(secp256k1_ecdh(context, &res, &point, s_one, copyx, &data), 1)
}

func testECDH() {
let privateKey = try! secp256k1.KeyAgreement.PrivateKey()
let publicKey = try! secp256k1.KeyAgreement.PrivateKey().publicKey

// Test without optional parameters.
XCTAssertNoThrow(try privateKey.sharedSecretFromKeyAgreement(with: publicKey))

// Test with handler and outputLength and data.
var data = try! [UInt8](hexString: "abcdef")
let copyxy : secp256k1.KeyAgreement.PrivateKey.HashFunctionType = {
(out, x, y, data) -> Int32 in
guard let out = out, let x = x, let y = y else {
return 0;
}
out.initialize(from: x, count: 32)
out.advanced(by: 32).initialize(from: y, count: 32)
return 1
}
XCTAssertNoThrow(try privateKey.sharedSecretFromKeyAgreement(with: publicKey, handler: copyxy, outputLength: 64, data: &data))
}

func testExtraKeysBindings() {
Expand Down