UNPKG

@isdk/bigint

Version:

The BigInteger class wrapped bn.js and native BitInt

137 lines (135 loc) 4.19 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/bn.ts var bn_exports = {}; __export(bn_exports, { BN: () => BN }); module.exports = __toCommonJS(bn_exports); var import_bn = __toESM(require("bn.js")); import_bn.default.prototype.equal = import_bn.default.prototype.eq; import_bn.default.prototype.isNegative = import_bn.default.prototype.isNeg; import_bn.default.prototype.iinc = function(n = 1) { return this.iaddn(n); }; import_bn.default.prototype.idec = function(n = 1) { return this.isubn(n); }; import_bn.default.prototype.imod = function(b) { const result = this.mod(b); result.copy(this); return this; }; import_bn.default.prototype.iumod = function(b) { const result = this.umod(b); result.copy(this); return this; }; import_bn.default.prototype.inc = function(n = 1) { return this.addn(n); }; import_bn.default.prototype.dec = function(n = 1) { return this.subn(n); }; import_bn.default.prototype.modExp = function(e, n) { const nRed = n.isEven() ? import_bn.default.red(n) : import_bn.default.mont(n); const result = this.toRed(nRed).redPow(e).fromRed(); return result; }; import_bn.default.prototype.modInv = function(n) { if (!this.gcd(n).isOne()) { throw new Error("Inverse does not exist"); } return this.invm(n); }; import_bn.default.prototype.isOne = function() { return this.eqn(1); }; import_bn.default.prototype.ileftShift = function(x) { if (import_bn.default.isBN(x)) { x = x.toNumber(); } this.ishln(x); return this; }; import_bn.default.prototype.leftShift = function(x) { if (import_bn.default.isBN(x)) { x = x.toNumber(); } return this.shln(x); }; import_bn.default.prototype.irightShift = function(x) { if (import_bn.default.isBN(x)) { x = x.toNumber(); } this.ishrn(x); return this; }; import_bn.default.prototype.rightShift = function(x) { if (import_bn.default.isBN(x)) { x = x.toNumber(); } return this.shrn(x); }; import_bn.default.prototype.getBit = function(i) { return this.testn(i) ? 1 : 0; }; import_bn.default.prototype.toUint8Array = function(endian = "be", length) { return this.toArrayLike(Uint8Array, endian, length); }; var BN = class extends import_bn.default { constructor(num) { let base = 10; switch (typeof num) { case "string": num = num.trim(); if (num.startsWith("0x")) { num = num.slice(2); base = 16; } else if (num.startsWith("0o")) { num = num.slice(2); base = 8; } else if (num.startsWith("0b")) { num = num.slice(2); base = 2; } break; case "boolean": num = Number(num); break; default: break; } super(num, base); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BN });