lexicographicallyLargest method

  1. @override
bool lexicographicallyLargest()
override

Returns whether or not this element is strictly lexicographically larger than its negation.

Implementation

@override
bool lexicographicallyLargest() {
  // Reduce Montgomery representation
  final tmp = Bls12Fp.montgomeryReduce(
    limbs[0],
    limbs[1],
    limbs[2],
    limbs[3],
    limbs[4],
    limbs[5],
    BigInt.zero,
    BigInt.zero,
    BigInt.zero,
    BigInt.zero,
    BigInt.zero,
    BigInt.zero,
  );

  BigInt borrow = BigInt.zero;

  // Subtract ((p - 1) / 2) + 1 using sbb
  final result0 = BigintUtils.sbb(
    tmp.limbs[0],
    BigInt.parse('0xdcff7fffffffd556'),
    borrow,
  );
  borrow = result0[1];

  final result1 = BigintUtils.sbb(
    tmp.limbs[1],
    BigInt.parse('0x0f55ffff58a9ffff'),
    borrow,
  );
  borrow = result1[1];

  final result2 = BigintUtils.sbb(
    tmp.limbs[2],
    BigInt.parse('0xb39869507b587b12'),
    borrow,
  );
  borrow = result2[1];

  final result3 = BigintUtils.sbb(
    tmp.limbs[3],
    BigInt.parse('0xb23ba5c279c2895f'),
    borrow,
  );
  borrow = result3[1];

  final result4 = BigintUtils.sbb(
    tmp.limbs[4],
    BigInt.parse('0x258dd3db21a5d66b'),
    borrow,
  );
  borrow = result4[1];

  final result5 = BigintUtils.sbb(
    tmp.limbs[5],
    BigInt.parse('0x0d0088f51cbff34d'),
    borrow,
  );
  borrow = result5[1];

  // If borrow = 0, element is lexicographically largest
  return borrow == BigInt.zero;
}