neg method

VestaFq neg()

Implementation

VestaFq neg() {
  // Step 1: subtract self from modulus
  var s0 = BigintUtils.sbb(
    VestaFQConst.modulus.limbs[0],
    limbs[0],
    BigInt.zero,
  );
  var d0 = s0[0];
  var borrow = s0[1];

  var s1 = BigintUtils.sbb(VestaFQConst.modulus.limbs[1], limbs[1], borrow);
  var d1 = s1[0];
  borrow = s1[1];

  var s2 = BigintUtils.sbb(VestaFQConst.modulus.limbs[2], limbs[2], borrow);
  var d2 = s2[0];
  borrow = s2[1];

  var s3 = BigintUtils.sbb(VestaFQConst.modulus.limbs[3], limbs[3], borrow);
  var d3 = s3[0];
  // final borrow ignored

  // Step 2: create mask: 0 if self == 0, all ones if self != 0
  bool isZero = limbs.every((x) => x == BigInt.zero);
  BigInt mask = isZero ? BigInt.zero : BigInt.parse('0xFFFFFFFFFFFFFFFF');

  // Step 3: apply mask to each limb
  return VestaFq([d0 & mask, d1 & mask, d2 & mask, d3 & mask]);
}