low-level
Version:
90 lines • 2.71 kB
JavaScript
import { Uint } from "./uint.js";
import { UintUtils } from "./utils.js";
export class FixedUint extends Uint {
static byteLength;
constructor(buffer) {
super(buffer);
}
static create(input) {
return new this(UintUtils.fixBufferByteLength((input instanceof Uint ? input.getRaw() : input), this.byteLength));
}
static alloc(fill) {
return new this(Buffer.alloc(this.byteLength, fill));
}
static empty() {
return this.alloc();
}
static from(input, arg2, arg3) {
let uint;
let buffer;
if (typeof input === "number") {
uint = this.alloc();
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(UintUtils.fixBufferByteLength(buffer, this.byteLength));
}
}
export class Uint8 extends FixedUint {
static byteLength = 1;
}
export class Uint16 extends FixedUint {
static byteLength = 2;
}
export class Uint32 extends FixedUint {
static byteLength = 4;
}
export class AbstractBigUint extends FixedUint {
addNumber(value) {
for (let i = this.buffer.byteLength - 4; i >= 0; i -= 4) {
const sum = this.buffer.readUint32BE(i) + value;
if (sum >= 0) {
this.buffer.writeUint32BE(sum % 4294967296, i);
}
else {
this.buffer.writeUint32BE((sum % 4294967296) + 4294967296, i);
}
value = Math.floor(sum / 4294967296);
}
}
divNumber(value, returnRest) {
let carry = 0;
for (let i = 0; i < this.buffer.byteLength; i += 4) {
const dividend = this.buffer.readUint32BE(i) + carry;
this.buffer.writeUint32BE(Math.floor(dividend / value), i);
carry = (dividend % value) * 4294967296;
}
if (returnRest)
return (carry / 4294967296);
}
toShortUint() {
for (let i = 0; i < this.buffer.byteLength; i++) {
if (this.buffer[i] !== 0) {
return this.slice(i);
}
}
return Uint.from(0);
}
}
export class Uint64 extends AbstractBigUint {
static byteLength = 8;
toBigInt() {
return this.buffer.readBigUint64BE();
}
}
export class Uint96 extends AbstractBigUint {
static byteLength = 12;
}
export class Uint128 extends AbstractBigUint {
static byteLength = 16;
}
export class Uint256 extends AbstractBigUint {
static byteLength = 32;
}
//# sourceMappingURL=fixed-uint.js.map