Skip to content

Commit 633b449

Browse files
committed
feat: ensure valid randomness of PrivateKey
1 parent 40f1f66 commit 633b449

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Sources/zkp/secp256k1.swift

+16-14
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public extension secp256k1 {
4040
public static func create(_ context: Context = .none) throws -> OpaquePointer {
4141
var randomBytes = SecureBytes(count: secp256k1.ByteDetails.count).bytes
4242
guard let context = secp256k1_context_create(context.rawValue),
43-
secp256k1_context_randomize(context, &randomBytes).boolValue else {
43+
secp256k1_context_randomize(context, &randomBytes).boolValue
44+
else {
4445
throw secp256k1Error.underlyingCryptoError
4546
}
4647

@@ -151,16 +152,14 @@ extension secp256k1 {
151152

152153
/// Backing initialization that creates a random secp256k1 private key for signing
153154
@usableFromInline init(format: secp256k1.Format = .compressed) throws {
154-
let privateKey = SecureBytes(count: secp256k1.ByteDetails.count)
155-
self.keyParity = 0
156-
self.format = format
157-
self.privateBytes = privateKey
158-
self.publicBytes = try PublicKeyImplementation.generate(bytes: &privateBytes, format: format)
159-
self.xonlyBytes = try XonlyKeyImplementation.generate(
160-
bytes: publicBytes,
161-
keyParity: &keyParity,
162-
format: format
163-
)
155+
for _ in 0 ..< 10 {
156+
let randomBytes = SecureBytes(count: secp256k1.ByteDetails.count)
157+
if let privateKey = try? PrivateKeyImplementation(dataRepresentation: Data(randomBytes), format: format) {
158+
self = privateKey
159+
return
160+
}
161+
}
162+
fatalError("Looped more than 10 times trying to generate a key")
164163
}
165164

166165
/// Backing initialization that creates a secp256k1 private key for signing from a data representation.
@@ -224,7 +223,8 @@ extension secp256k1 {
224223
var bytes = [UInt8](repeating: 0, count: keyLength)
225224

226225
guard secp256k1_ec_pubkey_negate(context, &key).boolValue,
227-
secp256k1_ec_pubkey_serialize(context, &bytes, &keyLength, &key, format.rawValue).boolValue else {
226+
secp256k1_ec_pubkey_serialize(context, &bytes, &keyLength, &key, format.rawValue).boolValue
227+
else {
228228
throw secp256k1Error.underlyingCryptoError
229229
}
230230

@@ -299,7 +299,8 @@ extension secp256k1 {
299299
signature.dataRepresentation.copyToUnsafeMutableBytes(of: &recoverySignature.data)
300300

301301
guard secp256k1_ecdsa_recover(context, &pubKey, &recoverySignature, Array(digest)).boolValue,
302-
secp256k1_ec_pubkey_serialize(context, &pubBytes, &pubKeyLen, &pubKey, format.rawValue).boolValue else {
302+
secp256k1_ec_pubkey_serialize(context, &pubBytes, &pubKeyLen, &pubKey, format.rawValue).boolValue
303+
else {
303304
throw secp256k1Error.underlyingCryptoError
304305
}
305306

@@ -402,7 +403,8 @@ extension secp256k1 {
402403

403404
guard secp256k1_ec_pubkey_parse(context, &pubKey, publicBytes, format.length).boolValue,
404405
secp256k1_xonly_pubkey_from_pubkey(context, &xonlyPubKey, &keyParity, &pubKey).boolValue,
405-
secp256k1_xonly_pubkey_serialize(context, &xonlyBytes, &xonlyPubKey).boolValue else {
406+
secp256k1_xonly_pubkey_serialize(context, &xonlyBytes, &xonlyPubKey).boolValue
407+
else {
406408
throw secp256k1Error.underlyingCryptoError
407409
}
408410

0 commit comments

Comments
 (0)