multiply static method

EllipticCurvePoint multiply(
  1. EllipticCurvePoint ellipticCurvePoint,
  2. BigInt n
)

Multiplies an elliptic curve point with another number usind the efficient double and add scheme. NOTE: This is not secure against side-channel-attacks. THIS IS NOT CONSIDERED A PROBLEM IN APPLICATIONS, as the realistic attack vector for side-channel-attacks are embedded chips (like on EC cards or credit cards). But we develop web apps and smartphone apps; For a side-channel-attack an attacker needs physical access to the hardware while generating a key. For an EC card, this is a realistic attack vector. For normal smartphone usage and generating shard keys on adding new contacts, a side-channel-attack is an entirely hypothetical scenario. If you need security against side-channel-attacks, change the if statement so that both clauses consume the same amount of CPU cycles and energy in any case.

Implementation

static EllipticCurvePoint multiply(
    EllipticCurvePoint ellipticCurvePoint, BigInt n) {
  BigInt doneAdditions = BigInt.zero;
  EllipticCurvePoint result = ellipticCurvePoint;
  while (doneAdditions < n) {
    if ((n - doneAdditions).isEven) {
      result = add(result, result);
      doneAdditions += ((n - doneAdditions) ~/ BigInt.two);
    } else {
      result = add(result, ellipticCurvePoint);
      doneAdditions += BigInt.one;
    }
  }
  return result;
}