VestaFq.fromBytes constructor

VestaFq.fromBytes(
  1. List<int> bytes
)

Implementation

factory VestaFq.fromBytes(List<int> bytes) {
  if (bytes.length != 32) {
    throw ArgumentException.invalidOperationArguments(
      "VestaFq",
      name: "bytes",
      reason: "Invalid field bytes length.",
      expecteLen: 32,
    );
  }
  // Parse 4 limbs
  final tmpLimbs = List<BigInt>.generate(4, (i) {
    return BigintUtils.fromBytes(
      bytes.sublist(i * 8, (i * 8) + 8),
      byteOrder: Endian.little,
    );
  });

  final tmp = VestaFq(tmpLimbs);

  // Constant-time check: tmp < modulus
  BigInt borrow = BigInt.zero;
  for (int i = 0; i < 4; i++) {
    borrow = tmp.limbs[i] - VestaFQConst.modulus.limbs[i] - borrow;
    borrow = borrow.isNegative ? BigInt.one : BigInt.zero;
  }
  bool isValid = borrow != BigInt.zero;
  if (!isValid) {
    throw ArgumentException.invalidOperationArguments(
      "VestaFq",
      name: "bytes",
      reason: "Invalid field bytes.",
    );
  }

  // Convert to Montgomery form
  return tmp.mul(VestaFq.r2());
}