-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbn-extensions.js
126 lines (105 loc) · 2.08 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const BN = require('bn.js')
const slice = Array.prototype.slice
const BN_ZERO = new BN(0)
const BN_ONE = new BN(1)
function add () {
const args = slice.call(arguments)
return args.reduce(_add)
}
function _add (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return null
return a.add(b)
}
function sub () {
const args = slice.call(arguments)
return args.reduce(_sub)
}
function _sub (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return null
return a.sub(b)
}
function mul () {
const args = slice.call(arguments)
return args.reduce(_mul)
}
function _mul (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return null
return a.mul(b)
}
function div () {
const args = slice.call(arguments)
return args.reduce(_div)
}
function _div (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return null
if (b.isZero()) return null
return a.div(b)
}
function isZero (v) {
if (!BN.isBN(v)) return false
return v.isZero()
}
function eq (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return false
return a.eq(b)
}
function lt (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return false
return a.lt(b)
}
function lte (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return false
return a.lte(b)
}
function gt (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return false
return a.gt(b)
}
function mod (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return null
return a.mod(b)
}
function or (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return false
return a.or(b)
}
function and (a, b) {
if (!BN.isBN(a) || !BN.isBN(b)) return false
return a.and(b)
}
function shln (a, b) {
if (!BN.isBN(a) || BN.isBN(b)) return false
return a.shln(b)
}
function shrn (a, b) {
if (!BN.isBN(a) || BN.isBN(b)) return false
return a.shrn(b)
}
function neg (a) {
if (!BN.isBN(a)) return false
return a.neg()
}
function maskn (a) {
if (!BN.isBN(a)) return false
return a.maskn()
}
module.exports = {
mul: mul,
div: div,
mod: mod,
add: add,
sub: sub,
isZero: isZero,
eq: eq,
lt: lt,
lte: lte,
gt: gt,
or: or,
and: and,
shln: shln,
shrn: shrn,
neg: neg,
maskn: maskn,
BN_ZERO: BN_ZERO,
BN_ONE: BN_ONE
}