neg method

JubJubFq neg()

Implementation

JubJubFq neg() {
  // Step 1: Subtract this from modulus
  var sbbRes = BigintUtils.sbb(
    JubJubFqConst.modulus.limbs[0],
    limbs[0],
    BigInt.zero,
  );
  BigInt d0 = sbbRes[0];
  BigInt borrow = sbbRes[1];

  sbbRes = BigintUtils.sbb(JubJubFqConst.modulus.limbs[1], limbs[1], borrow);
  BigInt d1 = sbbRes[0];
  borrow = sbbRes[1];

  sbbRes = BigintUtils.sbb(JubJubFqConst.modulus.limbs[2], limbs[2], borrow);
  BigInt d2 = sbbRes[0];
  borrow = sbbRes[1];

  sbbRes = BigintUtils.sbb(JubJubFqConst.modulus.limbs[3], limbs[3], borrow);
  BigInt d3 = sbbRes[0];
  // final borrow ignored

  // Step 2: Compute mask
  BigInt zeroCheck = limbs[0] | limbs[1] | limbs[2] | limbs[3];
  // If zeroCheck == 0, mask = 0; else mask = 0xffff... (64-bit max)
  BigInt mask = zeroCheck == BigInt.zero ? BigInt.zero : BinaryOps.maskBig64;

  // Step 3: Apply mask
  return JubJubFq([d0 & mask, d1 & mask, d2 & mask, d3 & mask]);
}