62 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2025-06-21 23:30:33 +03:00
"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Package constant-time provides functions for performing algorithmically constant-time operations.
*/
/**
* NOTE! Due to the inability to guarantee real constant time evaluation of
* anything in JavaScript VM, this is module is the best effort.
*/
/**
* Returns resultIfOne if subject is 1, or resultIfZero if subject is 0.
*
* Supports only 32-bit integers, so resultIfOne or resultIfZero are not
* integers, they'll be converted to them with bitwise operations.
*/
function select(subject, resultIfOne, resultIfZero) {
return (~(subject - 1) & resultIfOne) | ((subject - 1) & resultIfZero);
}
exports.select = select;
/**
* Returns 1 if a <= b, or 0 if not.
* Arguments must be positive 32-bit integers less than or equal to 2^31 - 1.
*/
function lessOrEqual(a, b) {
return (((a | 0) - (b | 0) - 1) >>> 31) & 1;
}
exports.lessOrEqual = lessOrEqual;
/**
* Returns 1 if a and b are of equal length and their contents
* are equal, or 0 otherwise.
*
* Note that unlike in equal(), zero-length inputs are considered
* the same, so this function will return 1.
*/
function compare(a, b) {
if (a.length !== b.length) {
return 0;
}
var result = 0;
for (var i = 0; i < a.length; i++) {
result |= a[i] ^ b[i];
}
return (1 & ((result - 1) >>> 8));
}
exports.compare = compare;
/**
* Returns true if a and b are of equal non-zero length,
* and their contents are equal, or false otherwise.
*
* Note that unlike in compare() zero-length inputs are considered
* _not_ equal, so this function will return false.
*/
function equal(a, b) {
if (a.length === 0 || b.length === 0) {
return false;
}
return compare(a, b) !== 0;
}
exports.equal = equal;
//# sourceMappingURL=constant-time.js.map