geScalarMult static method

void geScalarMult(
  1. GroupElementP2 r,
  2. List<int> a,
  3. GroupElementP3 gA
)

Implementation

static void geScalarMult(GroupElementP2 r, List<int> a, GroupElementP3 gA) {
  a.asMin32("geScalarMultBase");
  final List<int> e = List<int>.filled(64, 0);
  int carry, carry2, i;
  final List<GroupElementCached> aI =
      GroupElementCached.dsmp; /* 1 * A, 2 * A, ..., 8 * A */
  final GroupElementP1P1 t = GroupElementP1P1();
  final GroupElementP3 u = GroupElementP3();

  carry = 0; /* 0..1 */
  for (i = 0; i < 31; i++) {
    carry += a[i]; /* 0..256 */
    carry2 = (carry + 8) >> 4; /* 0..16 */
    e[2 * i] = carry - (carry2 << 4); /* -8..7 */
    carry = (carry2 + 8) >> 4; /* 0..1 */
    e[2 * i + 1] = carry2 - (carry << 4); /* -8..7 */
  }
  carry += a[31]; /* 0..128 */
  carry2 = (carry + 8) >> 4; /* 0..8 */
  e[62] = carry - (carry2 << 4); /* -8..7 */
  e[63] = carry2; /* 0..8 */

  geP3ToCached(aI[0], gA);
  for (i = 0; i < 7; i++) {
    geAdd(t, gA, aI[i]);
    geP1P1ToP3(u, t);
    geP3ToCached(aI[i + 1], u);
  }

  geP2Zero(r);
  for (i = 63; i >= 0; i--) {
    final int b = e[i];
    final int bnegative = negative(b);
    final int babs = b - (((-bnegative) & b) << 1);
    final GroupElementCached cur = GroupElementCached(),
        minuscur = GroupElementCached();
    geP2Dbl(t, r);
    geP1P1ToP2(r, t);
    geP2Dbl(t, r);
    geP1P1ToP2(r, t);
    geP2Dbl(t, r);
    geP1P1ToP2(r, t);
    geP2Dbl(t, r);
    geP1P1ToP3(u, t);
    geCached0(cur);
    geCachedCmov(cur, aI[0], equal(babs, 1));
    geCachedCmov(cur, aI[1], equal(babs, 2));
    geCachedCmov(cur, aI[2], equal(babs, 3));
    geCachedCmov(cur, aI[3], equal(babs, 4));
    geCachedCmov(cur, aI[4], equal(babs, 5));
    geCachedCmov(cur, aI[5], equal(babs, 6));
    geCachedCmov(cur, aI[6], equal(babs, 7));
    geCachedCmov(cur, aI[7], equal(babs, 8));
    feCopy(minuscur.yPlusX, cur.yMinusX);
    feCopy(minuscur.yMinusX, cur.yPlusX);
    feCopy(minuscur.z, cur.z);
    feNeg(minuscur.t2d, cur.t2d);
    geCachedCmov(cur, minuscur, bnegative);
    geAdd(t, u, cur);
    geP1P1ToP2(r, t);
  }
}