github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/1.0.5/uint.js (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  // 
     3  // This file is part of the go-nebulas library.
     4  // 
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  // 
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  // 
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  // 
    18  
    19  /*
    20   * this module must be required after bignumber.js
    21   */
    22  'use strict';
    23  
    24  /*
    25   * ffffffffffffffff 18446744073709551615
    26   * ffffffffffffffffffffffffffffffff 340282366920938463463374607431768211455
    27   * ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 115792089237316195423570985008687907853269984665640564039457584007913129639935
    28   * ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084095
    29   */
    30  const MAX_UINT64 = new BigNumber('ffffffffffffffff', 16);
    31  const MAX_UINT128 = new BigNumber('ffffffffffffffffffffffffffffffff', 16);
    32  const MAX_UINT256 = new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16);
    33  const MAX_UINT512 = new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16);
    34  
    35  const MAX_UINTS = {
    36      64: MAX_UINT64,
    37      128: MAX_UINT128,
    38      256: MAX_UINT256,
    39      512: MAX_UINT512
    40  }
    41  
    42  class Uint {
    43      constructor(n, b, s) {
    44  
    45          Object.defineProperties(this, {
    46              _inner: {
    47                  value: new BigNumber(n, b)
    48              },
    49              _size: {
    50                  value: s
    51              }
    52          });
    53  
    54          this._validate();
    55      }
    56  
    57      _validate() {
    58          // check integer
    59          if (!this._inner.isInteger()) {
    60              throw new Error('[Uint' + this._size + ' Error] not an integer');
    61          }
    62  
    63          // check negative
    64          if (this._inner.isNegative()) {
    65              throw new Error('[Uint' + this._size + ' Error] underflow');
    66          }
    67  
    68          // check overflow
    69          if (this._inner.gt(MAX_UINTS[this._size])) {
    70              throw new Error('[Uint' + this._size + ' Error] overflow');
    71          }
    72      }
    73  
    74      _checkRightOperand(right) {
    75          if (typeof right === 'undefined' || right == null) {
    76              throw new Error('[Uint' + this._size + ' Error] NaN');
    77          }
    78  
    79          if (!right instanceof Uint || this.constructor !== right.constructor) {
    80              throw new Error('[Uint' + this._size + ' Error] incompatible type');
    81          }
    82          right._validate();
    83      }
    84  
    85      div(o) {
    86          this._checkRightOperand(o);
    87          var r = this._inner.divToInt(o._inner);
    88          return new this.constructor(r, null, this._size);
    89      }
    90  
    91      pow(o) {
    92          this._checkRightOperand(o);
    93          var r = this._inner.pow(o._inner);
    94          return new this.constructor(r, null, this._size);
    95      }
    96  
    97      minus(o) {
    98          this._checkRightOperand(o);
    99          var r = this._inner.minus(o._inner);
   100          return new this.constructor(r, null, this._size);
   101      }
   102  
   103      mod(o) {
   104          this._checkRightOperand(o);
   105          var r = this._inner.mod(o._inner);
   106          return new this.constructor(r, null, this._size);
   107      }
   108  
   109      mul(o) {
   110          this._checkRightOperand(o);
   111          var r = this._inner.times(o._inner);
   112          return new this.constructor(r, null, this._size);
   113      }
   114  
   115      plus(o) {
   116          this._checkRightOperand(o);
   117          var r = this._inner.plus(o._inner);
   118          return new this.constructor(r, null, this._size);
   119      }
   120  
   121      cmp(o) {
   122          this._checkRightOperand(o);
   123          return this._inner.comparedTo(o._inner);
   124      }
   125  
   126      isZero() {
   127          return this._inner.isZero();
   128      }
   129  
   130      toString() {
   131          return this._inner.toString.apply(this._inner, Array.prototype.slice.call(arguments));
   132      }
   133  }
   134  
   135  class Uint64 extends Uint {
   136      constructor(n, b) {
   137          super(n, b, 64);
   138      }
   139  
   140      static get MaxValue () {
   141          return new Uint64(MAX_UINTS[64], null, 64);
   142      }
   143  }
   144  
   145  class Uint128 extends Uint {
   146      constructor(n, b) {
   147          super(n, b, 128);
   148      }
   149  
   150      static get MaxValue () {
   151          return new Uint128(MAX_UINTS[128], null, 128);
   152      }
   153  }
   154  
   155  class Uint256 extends Uint {
   156      constructor(n, b) {
   157          super(n, b, 256);
   158      }
   159  
   160      static get MaxValue () {
   161          return new Uint256(MAX_UINTS[256], null, 256);
   162      }
   163  }
   164  class Uint512 extends Uint {
   165      constructor(n, b) {
   166          super(n, b, 512);
   167      }
   168  
   169      static get MaxValue () {
   170          return new Uint512(MAX_UINTS[512], null, 512);
   171      }
   172  }
   173  
   174  module.exports = {
   175      Uint64: Uint64,
   176      Uint128: Uint128,
   177      Uint256: Uint256,
   178      Uint512: Uint512,
   179      isUint: function(o) {
   180          return o instanceof Uint;
   181      }
   182  };