git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 9bb00a66c6590ce780568e10d649be74c66ce1e6

Files: 9bb00a66c6590ce780568e10d649be74c66ce1e6 / deps / u256.js

1432 bytesRaw
1const BN = require('bn.js')
2const ethUtil = require('ethereumjs-util')
3
4module.exports = class U256 {
5 constructor (value) {
6 // bn.js still doesn't support hex prefixes...
7 if ((typeof value === 'string') && ethUtil.isHexPrefixed(value)) {
8 this._value = new BN(ethUtil.stripHexPrefix(value), 16)
9 } else {
10 this._value = new BN(value, 10)
11 }
12 }
13
14 // This assumes Uint8Array in LSB (WASM code)
15 static fromMemory (value) {
16 return new U256(new BN(value, 16, 'be'))
17 }
18
19 // This assumes Uint8Array in LSB (WASM code)
20 toMemory (width) {
21 return this._value.toBuffer('be', width || 32)
22 }
23
24 toString (radix = 10) {
25 if (radix === 16) {
26 return '0x' + this._value.toString(16)
27 }
28 return this._value.toString(radix)
29 }
30
31 toBuffer (width = 32) {
32 if (width <= 0 || width > 32) {
33 throw new Error('Invalid U256 width')
34 }
35 return this._value.toBuffer('be', width)
36 }
37
38 toArray () {
39 return [...this.toBuffer()]
40 }
41
42 isZero () {
43 return this._value.isZero()
44 }
45
46 sub (u256) {
47 return new U256(this._value.sub(u256._value))
48 }
49
50 add (u256) {
51 return new U256(this._value.add(u256._value))
52 }
53
54 mul (u256) {
55 return new U256(this._value.mul(u256._value))
56 }
57
58 div (u256) {
59 return new U256(this._value.div(u256._value))
60 }
61
62 lt (u256) {
63 return this._value.lt(u256._value)
64 }
65
66 gt (u256) {
67 return this._value.gt(u256._value)
68 }
69}
70

Built with git-ssb-web