git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: d40898a1c0e4f37c6b28da6da6646db86398ebcb

Files: d40898a1c0e4f37c6b28da6da6646db86398ebcb / u256.js

1170 bytesRaw
1const BN = require('bn.js')
2const ethUtil = require('ethereumjs-util')
3
4module.exports = class U256 {
5 constructor (value) {
6 // This is the case when data is copied from WASM
7 if (value instanceof Uint8Array) {
8 this._value = new BN(value, 16, 'le')
9 } else if ((typeof value === 'string') && ethUtil.isHexPrefixed(value)) {
10 this._value = new BN(ethUtil.stripHexPrefix(value), 16)
11 } else {
12 this._value = new BN(value, 10)
13 }
14 }
15
16 toString (radix = 10) {
17 if (radix === 16) {
18 return '0x' + this._value.toString(16)
19 }
20 return this._value.toString(radix)
21 }
22
23 toBuffer (width) {
24 if (width <= 0 || width > 32) {
25 throw new Error('Invalid U256 width')
26 }
27 return this._value.toBuffer('le', width || 32)
28 }
29
30 sub (u256) {
31 return new U256(this._value.sub(u256._value))
32 }
33
34 add (u256) {
35 return new U256(this._value.add(u256._value))
36 }
37
38 mul (u256) {
39 return new U256(this._value.mul(u256._value))
40 }
41
42 div (u256) {
43 return new U256(this._value.div(u256._value))
44 }
45
46 lt (u256) {
47 return this._value.lt(u256._value)
48 }
49
50 gt (u256) {
51 return this._value.gt(u256._value)
52 }
53}
54

Built with git-ssb-web