Skip to content

Commit 9406fcf

Browse files
perf: move ec key gen off the main thread
1 parent 4e6450c commit 9406fcf

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'dart:async';
2020
import 'dart:ffi' show Allocator;
2121
import 'dart:typed_data';
2222
import 'dart:convert' show utf8, base64Url;
23+
import 'dart:isolate';
2324
import 'dart:ffi' as ffi;
2425
import 'dart:math' as math;
2526
import 'package:meta/meta.dart';

lib/src/impl_ffi/impl_ffi.ec_common.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,15 @@ Map<String, dynamic> _exportJwkEcPrivateOrPublicKey(
327327
});
328328
}
329329

330-
KeyPair<_EvpPKey, _EvpPKey> _generateEcKeyPair(
330+
Future<KeyPair<_EvpPKey, _EvpPKey>> _generateEcKeyPair(
331331
EllipticCurve curve,
332-
) {
333-
return _Scope.sync((scope) {
332+
) async {
333+
return _Scope.async((scope) async {
334334
final ecPriv = ssl.EC_KEY_new_by_curve_name(_ecCurveToNID(curve));
335335
_checkOp(ecPriv.address != 0, fallback: 'internal failure to use curve');
336336
scope.defer(() => ssl.EC_KEY_free(ecPriv));
337337

338-
_checkOpIsOne(ssl.EC_KEY_generate_key(ecPriv));
338+
_checkOpIsOne(await _EC_generate_key(ecPriv));
339339

340340
final privKey = _EvpPKey();
341341
_checkOpIsOne(ssl.EVP_PKEY_set1_EC_KEY.invoke(privKey, ecPriv));
@@ -357,3 +357,17 @@ KeyPair<_EvpPKey, _EvpPKey> _generateEcKeyPair(
357357
);
358358
});
359359
}
360+
361+
/// Helper function to run `ssl.EC_KEY_generate_key` in an [Isolate]
362+
/// using [Isolate.run].
363+
///
364+
/// Using this auxiliary function to wrap the call should reduce the risk that
365+
/// unnecessary variables are copied into the closure passed to [Isolate.run].
366+
// ignore: non_constant_identifier_names
367+
Future<int> _EC_generate_key(
368+
ffi.Pointer<EC_KEY> key,
369+
) async =>
370+
await Isolate.run(
371+
() => ssl.EC_KEY_generate_key(key),
372+
debugName: 'EC_KEY_generate_key',
373+
);

lib/src/impl_ffi/impl_ffi.ecdh.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>>
3838
ecdhPrivateKey_generateKey(
3939
EllipticCurve curve,
4040
) async {
41-
final p = _generateEcKeyPair(curve);
41+
final p = await _generateEcKeyPair(curve);
4242
return (
4343
privateKey: _EcdhPrivateKeyImpl(p.privateKey),
4444
publicKey: _EcdhPublicKeyImpl(p.publicKey),

lib/src/impl_ffi/impl_ffi.ecdsa.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Future<KeyPair<EcdsaPrivateKeyImpl, EcdsaPublicKeyImpl>>
5454
ecdsaPrivateKey_generateKey(
5555
EllipticCurve curve,
5656
) async {
57-
final p = _generateEcKeyPair(curve);
57+
final p = await _generateEcKeyPair(curve);
5858
return (
5959
privateKey: _EcdsaPrivateKeyImpl(p.privateKey),
6060
publicKey: _EcdsaPublicKeyImpl(p.publicKey),

0 commit comments

Comments
 (0)