fix: lint
This commit is contained in:
parent
5a5ef737ce
commit
64c0c97973
@ -1272,7 +1272,7 @@ class Utils {
|
|||||||
* @returns {BigInt}
|
* @returns {BigInt}
|
||||||
*/
|
*/
|
||||||
static gcdBigInt(a, b) {
|
static gcdBigInt(a, b) {
|
||||||
return b === 0n ? a : Utils.gcdBigInt(b, a % b);
|
return b === 0n ? a : Utils.gcdBigInt(b, a % b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -22,12 +22,18 @@ const MAX_EXP = (1n << EXP_BITS) - 1n;
|
|||||||
const EXP_BITS_NUM = Number(EXP_BITS);
|
const EXP_BITS_NUM = Number(EXP_BITS);
|
||||||
const MANT_BITS_NUM = Number(MANT_BITS);
|
const MANT_BITS_NUM = Number(MANT_BITS);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
function normaliseInput(input) {
|
function normaliseInput(input) {
|
||||||
if (input === null || input === undefined)
|
if (input === null || input === undefined)
|
||||||
throw new Error("Invalid decimal number");
|
throw new Error("Invalid decimal number");
|
||||||
return String(input).trim();
|
return String(input).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
function safeBigInt(str, context="number") {
|
function safeBigInt(str, context="number") {
|
||||||
try {
|
try {
|
||||||
return BigInt(str);
|
return BigInt(str);
|
||||||
@ -36,22 +42,28 @@ function safeBigInt(str, context="number") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
function validateDecimal(input) {
|
function validateDecimal(input) {
|
||||||
// validates optional-sign base-10 decimal input: integers, decimals with optional digits before or after the decimal point, and scientific notation
|
// validates optional-sign base-10 decimal input: integers, decimals with optional digits before or after the decimal point, and scientific notation
|
||||||
if (!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(input))
|
if (!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(input))
|
||||||
throw new Error("Invalid decimal number");
|
throw new Error("Invalid decimal number");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
function validateBinary64(input) {
|
function validateBinary64(input) {
|
||||||
input = normaliseInput(input);
|
input = normaliseInput(input);
|
||||||
|
|
||||||
if (!/^[01\s]+$/.test(input))
|
if (!/^[01\s]+$/.test(input))
|
||||||
throw new Error("Binary64 must contain only 0 and 1");
|
throw new Error("Binary64 must contain only 0 and 1");
|
||||||
|
|
||||||
if (input.replace(/\s+/g,"").length !== 64)
|
if (input.replace(/\s+/g, "").length !== 64)
|
||||||
throw new Error("Binary64 must be exactly 64 bits");
|
throw new Error("Binary64 must be exactly 64 bits");
|
||||||
|
|
||||||
return input.replace(/\s+/g,"");
|
return input.replace(/\s+/g, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -256,7 +268,7 @@ export function ToIEEE754Float64(input) {
|
|||||||
let D = 1n;
|
let D = 1n;
|
||||||
|
|
||||||
if (fracStr.length > 0) {
|
if (fracStr.length > 0) {
|
||||||
const pow10 = pow10BigInt(BigInt(fracStr.length));
|
const pow10 = pow10BigInt(BigInt(fracStr.length));
|
||||||
N = N * pow10 + safeBigInt(fracStr, "fractional part");
|
N = N * pow10 + safeBigInt(fracStr, "fractional part");
|
||||||
D = pow10;
|
D = pow10;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,21 +37,24 @@ class FromIEEEBinary extends Operation {
|
|||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
if (input === null || input === undefined)
|
if (input === null || input === undefined)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
input = String(input);
|
input = String(input);
|
||||||
|
|
||||||
if (input.trim().length === 0)
|
if (input.trim().length === 0)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return FromIEEE754Float64(input);
|
return FromIEEE754Float64(input);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new OperationError(err.message);
|
throw new OperationError(err.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default FromIEEEBinary;
|
export default FromIEEEBinary;
|
||||||
|
|||||||
@ -35,22 +35,22 @@ class ToIEEEBinary extends Operation {
|
|||||||
* @param {Object[]} args
|
* @param {Object[]} args
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
if (input === null || input === undefined)
|
if (input === null || input === undefined)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
input = String(input);
|
input = String(input);
|
||||||
|
|
||||||
if (input.trim().length === 0)
|
if (input.trim().length === 0)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return ToIEEE754Float64(input);
|
return ToIEEE754Float64(input);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new OperationError(err.message);
|
throw new OperationError(err.message);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,46 +9,46 @@ TestRegister.addApiTests([
|
|||||||
const value = parseBigInt("1", "test value");
|
const value = parseBigInt("1", "test value");
|
||||||
assert.deepStrictEqual(value, BigInt("1"));
|
assert.deepStrictEqual(value, BigInt("1"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: parseBigInt - large decimal", () => {
|
it("BigIntUtils: parseBigInt - large decimal", () => {
|
||||||
const value = parseBigInt("123456789012345678901234567890", "test value");
|
const value = parseBigInt("123456789012345678901234567890", "test value");
|
||||||
assert.deepStrictEqual(value, BigInt("123456789012345678901234567890"));
|
assert.deepStrictEqual(value, BigInt("123456789012345678901234567890"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: parseBigInt - hexadecimal lowercase", () => {
|
it("BigIntUtils: parseBigInt - hexadecimal lowercase", () => {
|
||||||
const value = parseBigInt("0xff", "test value");
|
const value = parseBigInt("0xff", "test value");
|
||||||
assert.deepStrictEqual(value, BigInt("255"));
|
assert.deepStrictEqual(value, BigInt("255"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: parseBigInt - hexadecimal uppercase", () => {
|
it("BigIntUtils: parseBigInt - hexadecimal uppercase", () => {
|
||||||
const value = parseBigInt("0xFF", "test value");
|
const value = parseBigInt("0xFF", "test value");
|
||||||
assert.deepStrictEqual(value, BigInt("255"));
|
assert.deepStrictEqual(value, BigInt("255"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: parseBigInt - large hexadecimal", () => {
|
it("BigIntUtils: parseBigInt - large hexadecimal", () => {
|
||||||
const value = parseBigInt("0x123456789ABCDEF", "test value");
|
const value = parseBigInt("0x123456789ABCDEF", "test value");
|
||||||
assert.deepStrictEqual(value, BigInt("0x123456789ABCDEF"));
|
assert.deepStrictEqual(value, BigInt("0x123456789ABCDEF"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: parseBigInt - whitespace trimming", () => {
|
it("BigIntUtils: parseBigInt - whitespace trimming", () => {
|
||||||
const value = parseBigInt(" 42 ", "test value");
|
const value = parseBigInt(" 42 ", "test value");
|
||||||
assert.deepStrictEqual(value, BigInt("42"));
|
assert.deepStrictEqual(value, BigInt("42"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: parseBigInt - invalid input (text)", () => {
|
it("BigIntUtils: parseBigInt - invalid input (text)", () => {
|
||||||
assert.throws(() => parseBigInt("test", "test value"), {
|
assert.throws(() => parseBigInt("test", "test value"), {
|
||||||
name: "Error",
|
name: "Error",
|
||||||
message: "test value must be decimal or hex (0x...)"
|
message: "test value must be decimal or hex (0x...)"
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: parseBigInt - invalid input (hex without prefix)", () => {
|
it("BigIntUtils: parseBigInt - invalid input (hex without prefix)", () => {
|
||||||
assert.throws(() => parseBigInt("FF", "test value"), {
|
assert.throws(() => parseBigInt("FF", "test value"), {
|
||||||
name: "Error",
|
name: "Error",
|
||||||
message: "test value must be decimal or hex (0x...)"
|
message: "test value must be decimal or hex (0x...)"
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: parseBigInt - invalid input (mixed)", () => {
|
it("BigIntUtils: parseBigInt - invalid input (mixed)", () => {
|
||||||
assert.throws(() => parseBigInt("12abc", "test value"), {
|
assert.throws(() => parseBigInt("12abc", "test value"), {
|
||||||
name: "Error",
|
name: "Error",
|
||||||
@ -65,40 +65,40 @@ TestRegister.addApiTests([
|
|||||||
const bezout2 = BigInt("1");
|
const bezout2 = BigInt("1");
|
||||||
assert.deepStrictEqual(egcd(a, b), [gcd, bezout1, bezout2]);
|
assert.deepStrictEqual(egcd(a, b), [gcd, bezout1, bezout2]);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: egcd - coprime numbers", () => {
|
it("BigIntUtils: egcd - coprime numbers", () => {
|
||||||
const [g, x, y] = egcd(BigInt("3"), BigInt("11"));
|
const [g, x, y] = egcd(BigInt("3"), BigInt("11"));
|
||||||
assert.strictEqual(g, BigInt("1"));
|
assert.strictEqual(g, BigInt("1"));
|
||||||
// Verify Bézout identity: a*x + b*y = gcd
|
// Verify B<EFBFBD>zout identity: a*x + b*y = gcd
|
||||||
assert.strictEqual(BigInt("3") * x + BigInt("11") * y, g);
|
assert.strictEqual(BigInt("3") * x + BigInt("11") * y, g);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: egcd - non-coprime numbers", () => {
|
it("BigIntUtils: egcd - non-coprime numbers", () => {
|
||||||
const [g, x, y] = egcd(BigInt("240"), BigInt("46"));
|
const [g, x, y] = egcd(BigInt("240"), BigInt("46"));
|
||||||
assert.strictEqual(g, BigInt("2"));
|
assert.strictEqual(g, BigInt("2"));
|
||||||
// Verify Bézout identity
|
// Verify B<EFBFBD>zout identity
|
||||||
assert.strictEqual(BigInt("240") * x + BigInt("46") * y, g);
|
assert.strictEqual(BigInt("240") * x + BigInt("46") * y, g);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: egcd - with zero", () => {
|
it("BigIntUtils: egcd - with zero", () => {
|
||||||
const [g, x, y] = egcd(BigInt("17"), BigInt("0"));
|
const [g, x, y] = egcd(BigInt("17"), BigInt("0"));
|
||||||
assert.strictEqual(g, BigInt("17"));
|
assert.strictEqual(g, BigInt("17"));
|
||||||
assert.strictEqual(x, BigInt("1"));
|
assert.strictEqual(x, BigInt("1"));
|
||||||
assert.strictEqual(y, BigInt("0"));
|
assert.strictEqual(y, BigInt("0"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: egcd - identical numbers", () => {
|
it("BigIntUtils: egcd - identical numbers", () => {
|
||||||
const [g, x, y] = egcd(BigInt("42"), BigInt("42"));
|
const [g, x, y] = egcd(BigInt("42"), BigInt("42"));
|
||||||
assert.strictEqual(g, BigInt("42"));
|
assert.strictEqual(g, BigInt("42"));
|
||||||
// Verify Bézout identity
|
// Verify B<EFBFBD>zout identity
|
||||||
assert.strictEqual(BigInt("42") * x + BigInt("42") * y, g);
|
assert.strictEqual(BigInt("42") * x + BigInt("42") * y, g);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: egcd - large numbers", () => {
|
it("BigIntUtils: egcd - large numbers", () => {
|
||||||
const a = BigInt("123456789012345678901234567890");
|
const a = BigInt("123456789012345678901234567890");
|
||||||
const b = BigInt("987654321098765432109876543210");
|
const b = BigInt("987654321098765432109876543210");
|
||||||
const [g, x, y] = egcd(a, b);
|
const [g, x, y] = egcd(a, b);
|
||||||
// Verify Bézout identity
|
// Verify B<EFBFBD>zout identity
|
||||||
assert.strictEqual(a * x + b * y, g);
|
assert.strictEqual(a * x + b * y, g);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ TestRegister.addApiTests([
|
|||||||
const result = modPow(BigInt("2"), BigInt("10"), BigInt("1000"));
|
const result = modPow(BigInt("2"), BigInt("10"), BigInt("1000"));
|
||||||
assert.strictEqual(result, BigInt("24"));
|
assert.strictEqual(result, BigInt("24"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: modPow - RSA-like example", () => {
|
it("BigIntUtils: modPow - RSA-like example", () => {
|
||||||
// Common RSA public exponent
|
// Common RSA public exponent
|
||||||
const base = BigInt("123456789");
|
const base = BigInt("123456789");
|
||||||
@ -119,26 +119,26 @@ TestRegister.addApiTests([
|
|||||||
assert(result < mod);
|
assert(result < mod);
|
||||||
assert(result >= BigInt("0"));
|
assert(result >= BigInt("0"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: modPow - exponent zero", () => {
|
it("BigIntUtils: modPow - exponent zero", () => {
|
||||||
// Any number^0 = 1
|
// Any number^0 = 1
|
||||||
const result = modPow(BigInt("999"), BigInt("0"), BigInt("100"));
|
const result = modPow(BigInt("999"), BigInt("0"), BigInt("100"));
|
||||||
assert.strictEqual(result, BigInt("1"));
|
assert.strictEqual(result, BigInt("1"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: modPow - base zero", () => {
|
it("BigIntUtils: modPow - base zero", () => {
|
||||||
// 0^n = 0
|
// 0^n = 0
|
||||||
const result = modPow(BigInt("0"), BigInt("5"), BigInt("100"));
|
const result = modPow(BigInt("0"), BigInt("5"), BigInt("100"));
|
||||||
assert.strictEqual(result, BigInt("0"));
|
assert.strictEqual(result, BigInt("0"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: modPow - large exponent", () => {
|
it("BigIntUtils: modPow - large exponent", () => {
|
||||||
// Test with very large exponent (efficient algorithm should handle this)
|
// Test with very large exponent (efficient algorithm should handle this)
|
||||||
const result = modPow(BigInt("3"), BigInt("1000000"), BigInt("1000000007"));
|
const result = modPow(BigInt("3"), BigInt("1000000"), BigInt("1000000007"));
|
||||||
assert(result >= BigInt("0"));
|
assert(result >= BigInt("0"));
|
||||||
assert(result < BigInt("1000000007"));
|
assert(result < BigInt("1000000007"));
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("BigIntUtils: modPow - modular inverse verification", () => {
|
it("BigIntUtils: modPow - modular inverse verification", () => {
|
||||||
// If a*x . 1 (mod m), then modPow(a, 1, m) * x . 1 (mod m)
|
// If a*x . 1 (mod m), then modPow(a, 1, m) * x . 1 (mod m)
|
||||||
const a = BigInt("3");
|
const a = BigInt("3");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user