low-level
Version:
271 lines • 8.44 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Uint = void 0;
const utils_js_1 = require("./utils.js");
class Uint {
buffer;
constructor(input) {
this.buffer = input instanceof Uint ? input.getRaw() : input;
}
static create(input) {
return new this(input);
}
static alloc(length, fill) {
return new this(Buffer.alloc(length, fill));
}
static empty() {
return this.alloc(this.byteLength || 0);
}
static from(input, arg2, arg3) {
let uint;
let buffer;
if (typeof input === "number") {
uint = this.alloc(arg2 || Math.ceil(input.toString(16).length / 2));
uint.iadd(input);
return uint;
}
else if (typeof input === "string" && arg2 === undefined) {
buffer = Buffer.from(input, "hex");
}
else {
buffer = Buffer.from(input, arg2, arg3);
}
return new this(buffer);
}
/** @deprecated Use {@link Uint.from}(string) instead */
static fromHex(hex) {
return this.from(hex, "hex");
}
static concat(list, totalLength) {
return new this(Buffer.concat(list.map((item) => {
return item instanceof Uint ? (item.getRaw()) : item;
}), totalLength));
}
static isUint(input) {
return input instanceof this;
}
clone() {
return this.constructor.from(this.buffer);
}
toHex() {
return this.buffer.toString("hex");
}
toString(encoding) {
return this.buffer.toString(encoding);
}
valueOf() {
return this.toInt();
}
/**
* Supports only a number up to (2^48)-1
* For bigger numbers use {@link Uint.toBigInt}
*/
toInt() {
if (this.buffer.byteLength > 6) {
return this.buffer.readUintBE(this.buffer.byteLength - 6, 6);
}
return this.buffer.readUintBE(0, this.buffer.byteLength);
}
toBigInt() {
return BigInt("0x" + this.toHex());
}
getRaw() {
return this.buffer;
}
getAB() {
return this.buffer.buffer.slice(this.buffer.byteOffset, this.buffer.byteOffset + this.buffer.byteLength);
}
getLen(enc) {
return enc === "uint" ? Uint.from(this.buffer.byteLength) : this.buffer.byteLength;
}
get(offset, length = 1) {
return new Uint(this.buffer.subarray(offset, offset + length));
}
set(list, offset) {
this.buffer.set((list instanceof Uint ? list.getRaw() : list), offset);
}
appendData(data) {
return Uint.concat([this.buffer, data]);
}
nci_slice(CLS, start, end) {
return new CLS(this.buffer.subarray(start, end));
}
nci_split(CLS, afterBytes) {
const list = [];
for (let i = 0; i < this.buffer.byteLength; i += afterBytes) {
list.push(this.nci_slice(CLS, i, i + afterBytes));
}
return list;
}
slice(start, end) { return this.nci_slice(Uint, start, end); }
subarray(start, end) { return this.slice(start, end); }
split(afterBytes) { return this.nci_split(Uint, afterBytes); }
iadd(value) {
if (typeof value === "object") {
return this.addUint(value);
}
return this.addNumber(value);
}
add(value) {
const clone = this.clone();
clone.iadd(value);
return clone;
}
isub(value) {
if (typeof value === "object") {
return this.subUint(value);
}
return this.addNumber(value * -1);
}
sub(value) {
const clone = this.clone();
clone.isub(value);
return clone;
}
imul(value) {
if (typeof value === "object") {
return this.mulUint(value);
}
return this.mulNumber(value);
}
mul(value) {
const clone = this.clone();
clone.imul(value);
return clone;
}
idiv(value, returnRest = false) {
if (typeof value === "object") {
return this.divUint(value, returnRest);
}
return this.divNumber(value, returnRest);
}
div(value) {
const clone = this.clone();
clone.idiv(value);
return clone;
}
mod(value) {
const clone = this.clone();
return clone.idiv(value, true);
}
gt(value) {
return this.compare(value) === 1;
}
gte(value) {
return this.compare(value) !== -1;
}
lt(value) {
return this.compare(value) === -1;
}
lte(value) {
return this.compare(value) !== 1;
}
eq(value) {
return this.compare(value) === 0;
}
/**
* @deprecated Use {@link Uint.eq} with ! operator instead
*/
eqn(value) {
return this.compare(value) !== 0;
}
addUint(value) {
if (this.buffer.byteLength !== value.buffer.byteLength) {
value = utils_js_1.UintUtils.fixUintByteLength(this.constructor, value, this.buffer.byteLength);
}
let carry = 0;
for (let i = this.buffer.byteLength - 1; i >= 0; i--) {
const sum = this.buffer[i] + value.buffer[i] + carry;
this.buffer[i] = sum % 256;
carry = Math.floor(sum / 256);
}
}
addNumber(value) {
for (let i = this.buffer.byteLength - 1; i >= 0; i--) {
const sum = this.buffer[i] + value;
if (sum >= 0) {
this.buffer[i] = sum % 256;
}
else {
this.buffer[i] = (sum % 256) + 256;
}
value = Math.floor(sum / 256);
}
}
subUint(value) {
if (this.buffer.byteLength !== value.buffer.byteLength) {
value = utils_js_1.UintUtils.fixUintByteLength(this.constructor, value, this.buffer.byteLength);
}
let carry = 0;
for (let i = this.buffer.byteLength - 1; i >= 0; i--) {
const sum = this.buffer[i] - value.buffer[i] + carry;
if (sum >= 0) {
this.buffer[i] = sum % 256;
}
else {
this.buffer[i] = (sum % 256) + 256;
}
carry = Math.floor(sum / 256);
}
}
mulUint(value) {
if (this.buffer.byteLength !== value.buffer.byteLength) {
value = utils_js_1.UintUtils.fixUintByteLength(this.constructor, value, this.buffer.byteLength);
}
let carry = 0;
for (let i = 0; i < this.buffer.byteLength; i++) {
const product = this.buffer[i] * value.buffer[i] + carry;
this.buffer[i] = product % 256;
carry = Math.floor(product / 256);
}
}
mulNumber(value) {
let carry = 0;
for (let i = this.buffer.byteLength - 1; i >= 0; i--) {
const product = this.buffer[i] * value + carry;
this.buffer[i] = product % 256;
carry = Math.floor(product / 256);
}
}
divUint(value, returnRest) {
return this.divNumber(value.toInt(), returnRest);
}
divNumber(value, returnRest) {
let carry = 0;
for (let i = 0; i < this.buffer.byteLength; i++) {
const dividend = this.buffer[i] + carry;
this.buffer[i] = Math.floor(dividend / value);
carry = (dividend % value) * 256;
}
if (returnRest)
return (carry / 256);
}
compare(value) {
if (typeof value === "number") {
value = Uint.from(value, this.buffer.byteLength);
}
else if (this.buffer.byteLength !== value.buffer.byteLength) {
value = utils_js_1.UintUtils.fixUintByteLength(Uint, value, this.buffer.byteLength);
}
return this.buffer.compare(value.buffer);
}
get [Symbol.toStringTag]() {
return this.constructor.name;
}
[Symbol.toPrimitive](hint) {
switch (hint) {
case "string": return this.toHex();
case "number": return this.toInt();
default: return this.toBigInt();
}
}
[Symbol.for('nodejs.util.inspect.custom')]() {
let output = "<" + this.constructor.name + " ";
for (let i = 0; i < this.buffer.byteLength; i++) {
output += this.buffer[i].toString(16).padStart(2, '0') + ' ';
}
return output.trim() + ">";
}
}
exports.Uint = Uint;
//# sourceMappingURL=uint.js.map