X25519Keypair constructor

X25519Keypair({
  1. required List<int> privateKey,
  2. required List<int> publicKey,
  3. bool validatePublicKey = false,
})

Public constructor with validation of key lengths and public key canonicality.

Implementation

factory X25519Keypair(
    {required List<int> privateKey,
    required List<int> publicKey,
    bool validatePublicKey = false}) {
  if (privateKey.length != X25519KeyConst.privateKeyLength) {
    throw CryptoException('invalid private key bytes length');
  }
  if (publicKey.length != X25519KeyConst.publickKeyLength) {
    throw CryptoException('invalid public key bytes length');
  }
  final u = BigintUtils.fromBytes(publicKey, byteOrder: Endian.little);
  if (u >= X25519KeyConst.p) {
    throw CryptoException('uBytes is not a canonical field element');
  }
  if (validatePublicKey) {
    final key = X25519.scalarMultBase(privateKey);
    if (!BytesUtils.bytesEqual(key.publicKey, publicKey)) {
      throw CryptoException('invalid public.');
    }
  }
  return X25519Keypair._(privateKey: privateKey, publicKey: publicKey);
}