You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+47-3
Original file line number
Diff line number
Diff line change
@@ -36,15 +36,57 @@ It is worth noting that some Schnorr implementations are incompatible with [BIP3
36
36
37
37
## ECDH
38
38
39
+
This library vendors three different EC Diffie-Hellman (ECDH) key exchange functions:
40
+
1.`ASN1 x9.63` - No hash, return only the `X` coordinate of the point - `sharedSecretFromKeyAgreement -> SharedSecret`
41
+
2.`libsecp256k1` - SHA-256 hash the compressed point - `ecdh -> Data`
42
+
3. Custom - No hash, return point uncompressed - `ecdhPoint -> Data`
43
+
39
44
```swift
40
45
let alice =try K1.PrivateKey.generateNew()
41
46
let bob =try K1.PrivateKey.generateNew()
47
+
```
48
+
49
+
### `ASN1 x9.63` ECDH
50
+
Returning only the `X` coordinate of the point, following [ANSI X9.63][x963] standards, embedded in a [`CryptoKit.SharedSecret`][ckss], which is useful since you can use `CryptoKit` key derivation functions on this SharedSecret, e.g. [`x963DerivedSymmetricKey`](https://developer.apple.com/documentation/cryptokit/sharedsecret/x963derivedsymmetrickey(using:sharedinfo:outputbytecount:)) or [`hkdfDerivedSymmetricKey`](https://developer.apple.com/documentation/cryptokit/sharedsecret/hkdfderivedsymmetrickey(using:salt:sharedinfo:outputbytecount:)).
51
+
52
+
You can retrieve the `X` coordinate as raw data using `withUnsafeBytes` if you need to.
53
+
54
+
```swift
55
+
let ab: CryptoKit.SharedSecret =try alice.sharedSecretFromKeyAgreement(with: bob.publicKey)
56
+
let ba: : CryptoKit.SharedSecret =try bob.sharedSecretFromKeyAgreement(with: alice.publicKey)
57
+
58
+
assert(ab == ba) // pass
59
+
60
+
ab.withUnsafeBytes {
61
+
assert(Data($0).count==32) // pass
62
+
}
63
+
```
64
+
65
+
### `libsecp256k1` ECDH
42
66
43
-
let ab =try alice.sharedSecret(with: bob.publicKey)
44
-
let ba =try bob.sharedSecret(with: alice.publicKey)
45
-
assert(ab == ba, "Alice and Bob should be able to agree on the same secret")
67
+
Using `libsecp256k1` default behaviour, returning a SHA-256 hash of the **compressed** point.
68
+
69
+
```swift
70
+
let ab: Data =try alice.ecdh(with: bob.publicKey)
71
+
let ba: Data =try bob.ecdh(with: alice.publicKey)
72
+
assert(ab == ba) // pass
73
+
74
+
assert(ab.count==32) // pass
75
+
```
76
+
77
+
### Custom ECDH
78
+
79
+
Returns an entire uncompresed EC point, without hashing it. Might be useful if you wanna construct your own cryptographic functions, e.g. some custom ECIES.
80
+
81
+
```swift
82
+
let ab: Data =try alice.ecdhPoint(with: bob.publicKey)
83
+
let ba: Data =try bob.ecdhPoint(with: alice.publicKey)
84
+
assert(ab == ba) // pass
85
+
86
+
assert(ab.count==65) // pass
46
87
```
47
88
89
+
48
90
# Alternatives
49
91
50
92
-[GigaBitcoin/secp256k1.swift](https://github.com/GigaBitcoin/secp256k1.swift) (also using `libsecp256k1`, ⚠️ possibly unsafe, ✅ Schnorr support)
@@ -70,3 +112,5 @@ To clone the dependency [libsecp256k1][lib], using commit [427bc3cdcfbc747780704
0 commit comments