cmov function

void cmov(
  1. Uint8List r,
  2. Uint8List x,
  3. int len,
  4. int b,
)

Copies the contents of x to r if b is 1, otherwise does nothing. The operation is performed in constant time, without its time varying depending on the data, only on the length.

  • r: destination buffer.
  • x: source buffer.
  • len: amount of bytes to copy.
  • b: control bit (0 or 1).

Implementation

void cmov(Uint8List r, Uint8List x, int len, int b) {
  // If b=1, we need to copy x into r.
  // If b=0, we don't copy anything.
  // Convert b to -b & 0xFF to create a full mask if b=1.
  b = -b & 0xFF;
  for (int i = 0; i < len; i++) {
    r[i] ^= b & (r[i] ^ x[i]);
  }
}