toCompressed method

List<int> toCompressed()

Implementation

List<int> toCompressed() {
  // conditional_select(&self.x, &Fp2::zero(), self.infinity)
  final Bls12Fp2 xc = Bls12Fp2.conditionalSelect(
    x,
    Bls12Fp2.zero(),
    infinity,
  );

  // Allocate 96 bytes explicitly
  final res = List<int>.filled(96, 0);

  // Rust:
  // res[0..48]  = x.c1
  // res[48..96] = x.c0
  final c1 = xc.c1.toBytes(); // 48 bytes
  final c0 = xc.c0.toBytes(); // 48 bytes

  for (var i = 0; i < 48; i++) {
    res[i] = c1[i];
    res[i + 48] = c0[i];
  }

  // Set compressed flag (MSB)
  res[0] |= 1 << 7;

  // Set infinity flag (2nd MSB)
  res[0] |= IntUtils.ctSelectInt(0, 1 << 6, infinity);

  // Set lexicographically largest Y flag (3rd MSB),
  // but only if not infinity
  res[0] |= IntUtils.ctSelectInt(
    0,
    1 << 5,
    (!infinity) & y.lexicographicallyLargest(),
  );

  return res;
}