How to use bn.js

Comprehensive bn.js code examples:

How to use bn.js.apply:

5
6
7
8
9
10
11
12
13

var BN = function BN(n, base) {
  if (!(this instanceof BN)) {
    return new BN(n, base);
  }
  _BN.apply(this, arguments);
};

BN.prototype = _BN.prototype;

How to use bn.js.red:

57
58
59
60
61
62
63
64
65
66
function BaseCurve(type, conf) {
  this.type = type;
  this.p = new BN(conf.p, 16);

  // Use Montgomery, when there is no fast reduction for the prime
  this.red = conf.prime ? BN.red(conf.prime) : BN.mont(this.p);

  // Useful for many curves
  this.zero = new BN(0).toRed(this.red);
  this.one = new BN(1).toRed(this.red);

How to use bn.js.isBN:

80
81
82
83
84
85
86
87
88
89
assert(typeof options.prevBlock === 'string');
assert(typeof options.merkleRoot === 'string');
assert(util.isNumber(options.ts));
assert(util.isNumber(options.bits));
assert(util.isNumber(options.nonce));
assert(!options.chainwork || BN.isBN(options.chainwork));

this.hash = options.hash;
this.version = options.version;
this.prevBlock = options.prevBlock;

How to use bn.js.fromSM:

152
153
154
155
156
157
158
159
160
      if (buf.length <= 1 || (buf[buf.length - 2] & 0x80) === 0) {
        throw new Error('non-minimally encoded script number');
      }
    }
  }
  return BN.fromSM(buf, {
    endian: 'little'
  });
};

How to use bn.js.pad:

77
78
79
80
81
82
83
84
85
86
  if (natlen === opts.size) {
    buf = buf;
  } else if (natlen > opts.size) {
    buf = BN.trim(buf, natlen);
  } else if (natlen < opts.size) {
    buf = BN.pad(buf, natlen, opts.size);
  }
} else {
  hex = this.toString(16, 2);
  buf = new Buffer(hex, 'hex');

How to use bn.js.trim:

75
76
77
78
79
80
81
82
83
84
  buf = new Buffer(hex, 'hex');

  if (natlen === opts.size) {
    buf = buf;
  } else if (natlen > opts.size) {
    buf = BN.trim(buf, natlen);
  } else if (natlen < opts.size) {
    buf = BN.pad(buf, natlen, opts.size);
  }
} else {

How to use bn.js.BN:

196
197
198
199
200
201
202
203
204
205
    var _a = action.params, methodName = _a.methodName, args = _a.args, gas_1 = _a.gas, deposit = _a.deposit;
    console.log('deposit: ', deposit);
    console.log('gas: ', gas_1);
    console.log('args: ', args);
    console.log('methodName: ', methodName);
    return nearTransactions.functionCall(methodName, args, new bn_js_1.BN(gas_1), new bn_js_1.BN(deposit));
}
case "Transfer": {
    var deposit = action.params.deposit;
    return nearTransactions.transfer(new bn_js_1.BN(deposit));

How to use bn.js.mont:

132
133
134
135
136
137
138
139
140
141
}

function DH(prime, generator, malleable) {
  this.setGenerator(generator);
  this.__prime = new BN(prime);
  this._prime = BN.mont(this.__prime);
  this._primeLen = prime.length;
  this._pub = undefined;
  this._priv = undefined;
  this._primeCode = undefined;

How to use bn.js.fromBuffer:

52
53
54
55
56
57
58
59
60
61
  buf = reversebuf(buf);
}

if (buf[0] & 0x80) {
  buf[0] = buf[0] & 0x7f;
  ret = BN.fromBuffer(buf);
  ret.neg().copy(ret);
} else {
  ret = BN.fromBuffer(buf);
}

How to use bn.js.default:

35
36
37
38
39
40
41
42
43
44
    return new PublicKeyLayout(property);
}
exports.publicKeyLayout = publicKeyLayout;
class BNLayout extends buffer_layout_1.Blob {
    decode(b, offset) {
        return new bn_js_1.default(super.decode(b, offset), 10, 'le');
    }
    encode(src, b, offset) {
        return super.encode(src.toArrayLike(Buffer, 'le', this.span), b, offset);
    }

How to use bn.js._prime:

4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
//
// Base reduction engine
//
function Red (m) {
  if (typeof m === 'string') {
    var prime = BN._prime(m);
    this.m = prime.p;
    this.prime = prime;
  } else {
    assert(m.gtn(1), 'modulus must be greater than 1');

How to use bn.js.prototype:

3197
3198
3199
3200
3201
3202
3203
3204
3205
3206

BN.prototype.abs = function abs () {
  return this.clone().iabs();
};

BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) {
  var len = num.length + shift;
  var i;

  this._expand(len);