calculateAgreement method

  1. @override
BigInt calculateAgreement(
  1. ECPublicKey pubKey
)
override

given a public key from a given party calculate the next message in the agreement sequence.

Implementation

@override
BigInt calculateAgreement(ECPublicKey pubKey) {
  var params = key.parameters;
  if (pubKey.parameters?.curve != params?.curve) {
    throw PlatformException('ECDH public key has wrong domain parameters');
  }

  var d = key.d!;

  // Always perform calculations on the exact curve specified by our private key's parameters
  var Q = cleanPoint(params!.curve, pubKey.Q!);
  if (Q == null || Q.isInfinity) {
    throw PlatformException('Infinity is not a valid public key for ECDH');
  }

  var h = (params as ECDomainParametersImpl).h!;

  if (!(h.compareTo(BigInt.one) == 0)) {
    d = (h.modInverse(params.n) * d) % params.n;
    Q = Q * h;
  }

  var P = (Q! * d)!;

  if (P.isInfinity) {
    throw PlatformException(
        'Infinity is not a valid agreement value for ECDH');
  }

  return P.x!.toBigInteger()!;
}