operator * method

  1. @override
AffinePointt operator *(
  1. BigInt other
)
override

Overrides the multiplication operator (*) to perform point scalar multiplication. It multiplies the AffinePointt instance by a scalar other and returns a new AffinePointt representing the result.

Scalar multiplication is implemented using the double-and-add method, which efficiently computes this * other for positive other. It also handles the case where other is zero or a multiple of the group order.

If other is zero or a multiple of the group order, the result is the point at infinity. For negative scalars, it negates the point before performing multiplication.

Returns a new AffinePointt representing the result of the scalar multiplication.

Implementation

@override
AffinePointt operator *(BigInt other) {
  BigInt leftmostBit(BigInt x) {
    assert(x > BigInt.zero);
    BigInt result = BigInt.one;
    while (result <= x) {
      result = BigInt.from(2) * result;
    }
    return result ~/ BigInt.from(2);
  }

  BigInt e = other;
  if (e == BigInt.zero || (order != null && e % order! == BigInt.zero)) {
    return AffinePointt(curve, BigInt.zero, BigInt.zero);
  }

  if (e < BigInt.zero) {
    return -this * -e;
  }

  e *= BigInt.from(3);
  AffinePointt negativeSelf = AffinePointt(curve, x, -y, order: order);
  BigInt i = leftmostBit(e) ~/ BigInt.from(2);
  AffinePointt result = this;

  while (i > BigInt.one) {
    result = result.doublePoint();
    if ((e & i) != BigInt.zero && (other & i) == BigInt.zero) {
      result = (result + this) as AffinePointt;
    }
    if ((e & i) == BigInt.zero && (other & i) != BigInt.zero) {
      result = (result + negativeSelf) as AffinePointt;
    }
    i = i ~/ BigInt.from(2);
  }

  return result;
}