generateKeyPair method

  1. @override
KeyPair generateKeyPair({
  1. Uint8List? coins,
})
inherited

Implementation

@override
KeyPair generateKeyPair({Uint8List? coins}) {
  final pk = calloc<ffi.Uint8>(_params.publicKeyBytes);
  final sk = calloc<ffi.Uint8>(_params.secretKeyBytes);
  int result;
  ffi.Pointer<ffi.Uint8>? coinsPtr;
  if (coins != null) {
    if (coins.length != randomCoinsSize) {
      throw MLDSACoinsWrongSize();
    }
    coinsPtr = calloc<ffi.Uint8>(randomCoinsSize);
    coinsPtr.asTypedList(randomCoinsSize).setAll(0, coins);
    result = _params.keypairDerand(pk, sk, coinsPtr);
  } else {
    result = _params.keypair(pk, sk);
  }

  if (result != 0) {
    calloc.free(pk);
    calloc.free(sk);
    if (coinsPtr != null) {
      calloc.free(coinsPtr);
    }
    throw MLDSAKeyPairGenerationException(result);
  }

  final pkBytes = Uint8List.fromList(pk.asTypedList(_params.publicKeyBytes));
  final skBytes = Uint8List.fromList(sk.asTypedList(_params.secretKeyBytes));

  calloc.free(pk);
  calloc.free(sk);

  if (coinsPtr != null) {
    calloc.free(coinsPtr);
  }

  return KeyPair(secretKey: skBytes, publicKey: pkBytes);
}