forked from bitcoinjs/coinselect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bn-extensions.js
executable file
·79 lines (65 loc) · 1.41 KB
/
bn-extensions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var BN = require('bignumber.js')
var slice = Array.prototype.slice
var BN_ZERO = new BN(0)
var BN_ONE = new BN(1)
function add () {
var args = slice.call(arguments)
return args.reduce(_add)
}
function _add (a, b) {
if (!BN.isBigNumber(a) || !BN.isBigNumber(b)) return null
return a.plus(b)
}
function sub () {
var args = slice.call(arguments)
return args.reduce(_sub)
}
function _sub (a, b) {
if (!BN.isBigNumber(a) || !BN.isBigNumber(b)) return null
return a.minus(b)
}
function mul () {
var args = slice.call(arguments)
return args.reduce(_mul)
}
function _mul (a, b) {
if (!BN.isBigNumber(a) || !BN.isBigNumber(b)) return null
return a.times(b)
}
function div () {
var args = slice.call(arguments)
return args.reduce(_div)
}
function _div (a, b) {
if (!BN.isBigNumber(a) || !BN.isBigNumber(b)) return null
if (b.isZero()) return null
return a.div(b)
}
function isZero (v) {
if (!BN.isBigNumber(v)) return false
return v.isZero()
}
function eq (a, b) {
if (!BN.isBigNumber(a) || !BN.isBigNumber(b)) return false
return a.eq(b)
}
function lt (a, b) {
if (!BN.isBigNumber(a) || !BN.isBigNumber(b)) return false
return a.lt(b)
}
function gt (a, b) {
if (!BN.isBigNumber(a) || !BN.isBigNumber(b)) return false
return a.gt(b)
}
module.exports = {
mul: mul,
div: div,
add: add,
sub: sub,
isZero: isZero,
eq: eq,
lt: lt,
gt: gt,
BN_ZERO: BN_ZERO,
BN_ONE: BN_ONE
}