function isCanonical()

in src/ristretto255.js [533:546]


function isCanonical(s) {
  let c = (s[31] & 0x7f) ^ 0x7f;
  /* here c == 0 iff s[31] == 0x7f */
  for (let i = 30; i > 0; i--) {
    c |= s[i] ^ 0xff;
  }
  /* here c == 0 iff s = 0x7f 0xff 0xff ... 0xff 0x** */
  c = ((c | (0 >>> 0)) - 1) >> 8;
  /* here c & 1 == 1 iff s = 0x7f 0xff 0xff ... 0xff 0x** */
  const d = (0xed - 1 - (s[0] | (0 >>> 0))) >> 8;
  /* here d & 1 == 1 iff s[0] >= 0xed */
  /* here (c & d) & 1 == 1 iff 2^255-1 >= s >= 2^255-19 */
  return 1 - ((((c & d) | s[0]) & 1) | ((s[31] & 0xff) >> 7));
}