@gsknnft/bigint-buffer 1.3.2 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.travis.yml +10 -10
- package/README.md +123 -126
- package/WHY_BIGINT.md +127 -0
- package/benchmark.md +38 -0
- package/dist/index.cjs +1 -205
- package/dist/index.js +347 -202
- package/dist/index.umd.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/bigint-buffer.test.d.ts +1 -0
- package/dist/types/conversion/index.d.ts +1 -0
- package/dist/types/conversion/vite.config.d.ts +2 -0
- package/dist/types/conversion/vitest.config.d.ts +2 -0
- package/dist/types/dist/dist/conversion/conversion/index.d.ts +1 -0
- package/dist/{index.d.ts → types/dist/dist/index.d.ts} +2 -0
- package/dist/types/index.bench.d.ts +1 -0
- package/dist/types/index.d.ts +80 -0
- package/dist/types/index.spec.d.ts +1 -0
- package/eslint.config.ts +12 -0
- package/package.json +56 -37
- package/pnpm-workspace.yaml +14 -0
- package/rollup.cjs.config.js +8 -3
- package/rollup.conversion.cjs.config.js +13 -0
- package/rollup.conversion.esm.config.js +24 -0
- package/rollup.esm.config.js +12 -3
- package/tsconfig.tsbuildinfo +1 -0
- package/vite.config.ts +44 -0
- package/vitest.config.ts +20 -0
- package/dist/node.js +0 -94
- package/src/bigint-buffer.c +0 -203
- package/src/index.bench.ts +0 -207
- package/src/index.spec.ts +0 -444
- package/src/index.ts +0 -210
- package/tsconfig.json +0 -20
package/src/index.bench.ts
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import * as benchmark from 'benchmark';
|
|
3
|
-
|
|
4
|
-
import {toBigIntBE, toBigIntLE, toBufferBE, toBufferLE} from './index';
|
|
5
|
-
|
|
6
|
-
const BN = require('bn.js');
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// This file contains the benchmark test suite. It includes the benchmark and
|
|
10
|
-
// some lightweight boilerplate code for running benchmark.js. To
|
|
11
|
-
// run the benchmarks, execute `npm run benchmark` from the package directory.
|
|
12
|
-
const suite = new benchmark.Suite();
|
|
13
|
-
|
|
14
|
-
interface BenchmarkRun {
|
|
15
|
-
name: string;
|
|
16
|
-
hz: number;
|
|
17
|
-
stats: benchmark.Stats;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Tests the performance of a no-op.
|
|
21
|
-
suite.add('no-op', () => {});
|
|
22
|
-
|
|
23
|
-
// Test small strings (unaligned)
|
|
24
|
-
const smallHex = 'deadbeef';
|
|
25
|
-
const smallString = `0x${smallHex}`;
|
|
26
|
-
const smallBuf: Buffer = Buffer.from(smallHex, 'hex');
|
|
27
|
-
suite.add('bigint from hex string (small)', () => {
|
|
28
|
-
return BigInt(smallString);
|
|
29
|
-
});
|
|
30
|
-
suite.add('bigint from hex string from buffer (small)', () => {
|
|
31
|
-
return BigInt(`0x${smallBuf.toString('hex')}`);
|
|
32
|
-
});
|
|
33
|
-
suite.add('BN from hex string from buffer (small)', () => {
|
|
34
|
-
return new BN(smallBuf.toString('hex'), 16);
|
|
35
|
-
});
|
|
36
|
-
suite.add('LE bigint-buffer ToBigInt (small)', () => {
|
|
37
|
-
return toBigIntLE(smallBuf);
|
|
38
|
-
});
|
|
39
|
-
suite.add('BE bigint-buffer ToBigInt (small)', () => {
|
|
40
|
-
return toBigIntBE(smallBuf);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
// Test mid strings (aligned)
|
|
44
|
-
const midHex = 'badc0ffee0ddf00d';
|
|
45
|
-
const midString = `0x${midHex}`;
|
|
46
|
-
const midBuf: Buffer = Buffer.from(midHex, 'hex');
|
|
47
|
-
suite.add('bigint from hex string (mid, aligned)', () => {
|
|
48
|
-
return BigInt(midString);
|
|
49
|
-
});
|
|
50
|
-
suite.add('bigint from hex string from buffer (mid, aligned)', () => {
|
|
51
|
-
return BigInt(`0x${midBuf.toString('hex')}`);
|
|
52
|
-
});
|
|
53
|
-
suite.add('BN from hex string from buffer (mid, aligned)', () => {
|
|
54
|
-
return new BN(midBuf.toString('hex'), 16);
|
|
55
|
-
});
|
|
56
|
-
suite.add('LE bigint-buffer ToBigInt (mid, aligned)', () => {
|
|
57
|
-
return toBigIntLE(midBuf);
|
|
58
|
-
});
|
|
59
|
-
suite.add('BE bigint-buffer ToBigInt (mid, aligned)', () => {
|
|
60
|
-
return toBigIntBE(midBuf);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// Test huge strings
|
|
64
|
-
const hugeHex =
|
|
65
|
-
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d';
|
|
66
|
-
const hugeString = `0x${hugeHex}`;
|
|
67
|
-
const hugeBuf: Buffer = Buffer.from(hugeHex, 'hex');
|
|
68
|
-
suite.add('bigint from hex string (huge)', () => {
|
|
69
|
-
return BigInt(hugeString);
|
|
70
|
-
});
|
|
71
|
-
suite.add('bigint from hex string from buffer (huge)', () => {
|
|
72
|
-
return BigInt(`0x${hugeBuf.toString('hex')}`);
|
|
73
|
-
});
|
|
74
|
-
suite.add('BN from hex string from buffer (huge)', () => {
|
|
75
|
-
return new BN(hugeBuf.toString('hex'), 16);
|
|
76
|
-
});
|
|
77
|
-
suite.add('LE bigint-buffer ToBigInt (huge)', () => {
|
|
78
|
-
return toBigIntLE(hugeBuf);
|
|
79
|
-
});
|
|
80
|
-
suite.add('BE bigint-buffer ToBigInt (huge)', () => {
|
|
81
|
-
return toBigIntBE(hugeBuf);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
const bigIntToBufferWithStringBE = (int: bigint, width: number): Buffer => {
|
|
85
|
-
const hex = int.toString(16);
|
|
86
|
-
return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const bigIntToBufferWithStringLE = (int: bigint, width: number): Buffer => {
|
|
90
|
-
const hex = int.toString(16);
|
|
91
|
-
const buffer =
|
|
92
|
-
Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
93
|
-
buffer.reverse();
|
|
94
|
-
return buffer;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// Test small toBuffer
|
|
98
|
-
const smallValue = 12345678n;
|
|
99
|
-
suite.add('LE bigint to hex string to buffer (small)', () => {
|
|
100
|
-
return bigIntToBufferWithStringLE(smallValue, 8);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
suite.add('BE bigint to hex string to buffer (small)', () => {
|
|
104
|
-
return bigIntToBufferWithStringBE(smallValue, 8);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
const bnSmallValue = new BN('12345678', 10);
|
|
108
|
-
suite.add('BN to buffer (small)', () => {
|
|
109
|
-
return bnSmallValue.toBuffer(8);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
suite.add('LE bigint-buffer to buffer (small)', () => {
|
|
113
|
-
return toBufferLE(smallValue, 8);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
suite.add('BE bigint-buffer to buffer (small)', () => {
|
|
117
|
-
return toBufferBE(smallValue, 8);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// Test large toBuffer
|
|
122
|
-
const largeValue =
|
|
123
|
-
0xbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dn;
|
|
124
|
-
suite.add('LE bigint to hex string to buffer (large)', () => {
|
|
125
|
-
return bigIntToBufferWithStringLE(largeValue, 24);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
suite.add('BE bigint to hex string to buffer (large)', () => {
|
|
129
|
-
return bigIntToBufferWithStringBE(largeValue, 24);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
const bnLargeValue = new BN(
|
|
133
|
-
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d',
|
|
134
|
-
16);
|
|
135
|
-
suite.add('BN to buffer (large)', () => {
|
|
136
|
-
return bnLargeValue.toBuffer(24);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
suite.add('LE bigint-buffer to buffer (large)', () => {
|
|
140
|
-
return toBufferLE(largeValue, 24);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
suite.add('BE bigint-buffer to buffer (large)', () => {
|
|
144
|
-
return toBufferBE(largeValue, 24);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
suite.add('LE bigint to hex string to buffer (large)', () => {
|
|
148
|
-
return bigIntToBufferWithStringLE(largeValue, 8);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
suite.add('BE bigint to hex string to buffer (large)', () => {
|
|
152
|
-
return bigIntToBufferWithStringBE(largeValue, 8);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
suite.add('LE bigint-buffer to buffer (large, truncated)', () => {
|
|
156
|
-
return toBufferLE(largeValue, 8);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
suite.add('BE bigint-buffer to buffer (large, truncated)', () => {
|
|
160
|
-
return toBufferBE(largeValue, 8);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
const b1 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
164
|
-
const b2 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
165
|
-
const bn1 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
166
|
-
const bn2 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
|
167
|
-
const n1 = 0x0123456789ABCDEF0123456789ABCDEFn;
|
|
168
|
-
const n2 = 0x0123456789ABCDEF0123456789ABCDEFn;
|
|
169
|
-
suite.add('Buffer equality comparison', () => {
|
|
170
|
-
return b1.compare(b2) === 0;
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
suite.add('BN equality comparison', () => {
|
|
174
|
-
return bn1.eq(bn2);
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
suite.add('bigint equality comparison', () => {
|
|
178
|
-
return n1 === n2;
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
suite.add('BN multiply', () => {
|
|
182
|
-
return bn1.mul(bn2);
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
suite.add('bigint multiply', () => {
|
|
186
|
-
return n1 * n2;
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
//#endregion
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
// Reporter for each benchmark
|
|
193
|
-
suite.on('cycle', (event: benchmark.Event) => {
|
|
194
|
-
const benchmarkRun: BenchmarkRun = event.target as BenchmarkRun;
|
|
195
|
-
const stats = benchmarkRun.stats as benchmark.Stats;
|
|
196
|
-
const meanInNanos = (stats.mean * 1000000000).toFixed(2);
|
|
197
|
-
const stdDevInNanos = (stats.deviation * 1000000000).toFixed(3);
|
|
198
|
-
const runs = stats.sample.length;
|
|
199
|
-
const ops = benchmarkRun.hz.toFixed(benchmarkRun.hz < 100 ? 2 : 0);
|
|
200
|
-
const err = stats.rme.toFixed(2);
|
|
201
|
-
|
|
202
|
-
console.log(`${benchmarkRun.name}: ${ops}±${err}% ops/s ${meanInNanos}±${
|
|
203
|
-
stdDevInNanos} ns/op (${runs} run${runs === 0 ? '' : 's'})`);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
// Runs the test suite
|
|
207
|
-
suite.run();
|
package/src/index.spec.ts
DELETED
|
@@ -1,444 +0,0 @@
|
|
|
1
|
-
import 'mocha';
|
|
2
|
-
declare var process: {browser: boolean;};
|
|
3
|
-
|
|
4
|
-
import * as chai from 'chai';
|
|
5
|
-
import * as path from 'path';
|
|
6
|
-
|
|
7
|
-
const lib = process.browser ?
|
|
8
|
-
require('../../dist/index.js') :
|
|
9
|
-
require(path.join(__dirname, '../../dist/index.cjs'));
|
|
10
|
-
const toBigIntBE = lib.toBigIntBE;
|
|
11
|
-
const toBigIntLE = lib.toBigIntLE;
|
|
12
|
-
const toBufferBE = lib.toBufferBE;
|
|
13
|
-
const toBufferLE = lib.toBufferLE;
|
|
14
|
-
|
|
15
|
-
// Needed for should.not.be.undefined.
|
|
16
|
-
/* tslint:disable:no-unused-expression */
|
|
17
|
-
|
|
18
|
-
chai.should();
|
|
19
|
-
|
|
20
|
-
const assertEquals = (n0: bigint, n1: bigint) => {
|
|
21
|
-
chai.expect(n0.toString(16)).to.equal(n1.toString(16));
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
describe('Try buffer conversion (little endian)', () => {
|
|
25
|
-
it('0 should equal 0n', () => {
|
|
26
|
-
assertEquals(toBigIntLE(Buffer.from([0])), BigInt('0'));
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('1 should equal 1n', async () => {
|
|
30
|
-
assertEquals(toBigIntLE(Buffer.from([1])), BigInt('1'));
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('0xdead should equal 0xdeadn', async () => {
|
|
34
|
-
assertEquals(toBigIntLE(Buffer.from([0xad, 0xde])), BigInt(`0xdead`));
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it('0xdeadbeef should equal 0xdeadbeefn', async () => {
|
|
38
|
-
assertEquals(
|
|
39
|
-
toBigIntLE(Buffer.from([0xef, 0xbe, 0xad, 0xde])),
|
|
40
|
-
BigInt(`0xdeadbeef`));
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('0xbadc0ffee0 should equal 0xbadc0ffee0n', async () => {
|
|
44
|
-
assertEquals(
|
|
45
|
-
toBigIntLE(Buffer.from([0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
|
|
46
|
-
BigInt(`0xbadc0ffee0`));
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('0xbadc0ffee0dd should equal 0xbadc0ffee0ddn', async () => {
|
|
50
|
-
assertEquals(
|
|
51
|
-
toBigIntLE(Buffer.from([0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
|
|
52
|
-
BigInt(`0xbadc0ffee0dd`));
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('0xbadc0ffee0ddf0 should equal 0xbadc0ffee0ddf0n', async () => {
|
|
56
|
-
assertEquals(
|
|
57
|
-
toBigIntLE(Buffer.from([0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
|
|
58
|
-
BigInt(`0xbadc0ffee0ddf0`));
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn', async () => {
|
|
62
|
-
assertEquals(
|
|
63
|
-
toBigIntLE(
|
|
64
|
-
Buffer.from([0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
|
|
65
|
-
BigInt(`0xbadc0ffee0ddf00d`));
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
|
|
69
|
-
async () => {
|
|
70
|
-
assertEquals(
|
|
71
|
-
toBigIntLE(Buffer.from([
|
|
72
|
-
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc,
|
|
73
|
-
0xba
|
|
74
|
-
])),
|
|
75
|
-
BigInt(`0xbadc0ffee0ddf00ddeadbeef`));
|
|
76
|
-
});
|
|
77
|
-
it('0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefn',
|
|
78
|
-
async () => {
|
|
79
|
-
assertEquals(
|
|
80
|
-
toBigIntLE(Buffer.from([
|
|
81
|
-
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0,
|
|
82
|
-
0xfe, 0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde,
|
|
83
|
-
0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
|
|
84
|
-
])),
|
|
85
|
-
BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`));
|
|
86
|
-
});
|
|
87
|
-
it('0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbeef should equal 0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbeefn',
|
|
88
|
-
async () => {
|
|
89
|
-
assertEquals(
|
|
90
|
-
toBigIntLE(Buffer.from([
|
|
91
|
-
0xef, 0xbe, 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd,
|
|
92
|
-
0xe0, 0xfe, 0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde,
|
|
93
|
-
0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
|
|
94
|
-
])),
|
|
95
|
-
BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbeef`));
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe('Try buffer conversion (big endian)', () => {
|
|
100
|
-
it('0 should equal 0n', () => {
|
|
101
|
-
assertEquals(toBigIntBE(Buffer.from([0])), BigInt(`0`));
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('1 should equal 1n', async () => {
|
|
105
|
-
assertEquals(toBigIntBE(Buffer.from([1])), BigInt(`1`));
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('0xdead should equal 0xdeadn', async () => {
|
|
109
|
-
assertEquals(toBigIntBE(Buffer.from([0xde, 0xad])), BigInt(`0xdead`));
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('0xdeadbeef should equal 0xdeadbeefn', async () => {
|
|
113
|
-
assertEquals(
|
|
114
|
-
toBigIntBE(Buffer.from([0xde, 0xad, 0xbe, 0xef])),
|
|
115
|
-
BigInt(`0xdeadbeef`));
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('0xbadc0ffee0 should equal 0xbadc0ffee0n', async () => {
|
|
119
|
-
assertEquals(
|
|
120
|
-
toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0])),
|
|
121
|
-
BigInt(`0xbadc0ffee0`));
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('0xbadc0ffee0dd should equal 0xbadc0ffee0ddn', async () => {
|
|
125
|
-
assertEquals(
|
|
126
|
-
toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd])),
|
|
127
|
-
BigInt(`0xbadc0ffee0dd`));
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('0xbadc0ffee0ddf0 should equal 0xbadc0ffee0ddf0n', async () => {
|
|
131
|
-
assertEquals(
|
|
132
|
-
toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0])),
|
|
133
|
-
BigInt(`0xbadc0ffee0ddf0`));
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn', async () => {
|
|
137
|
-
assertEquals(
|
|
138
|
-
toBigIntBE(
|
|
139
|
-
Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d])),
|
|
140
|
-
BigInt(`0xbadc0ffee0ddf00d`));
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
|
|
144
|
-
async () => {
|
|
145
|
-
assertEquals(
|
|
146
|
-
toBigIntBE(Buffer.from([
|
|
147
|
-
0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe,
|
|
148
|
-
0xef
|
|
149
|
-
])),
|
|
150
|
-
BigInt(`0xbadc0ffee0ddf00ddeadbeef`));
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it('long value should equal long val', async () => {
|
|
154
|
-
assertEquals(
|
|
155
|
-
toBigIntBE(Buffer.from(
|
|
156
|
-
'badc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef',
|
|
157
|
-
'hex')),
|
|
158
|
-
BigInt(
|
|
159
|
-
`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`));
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('other long value should equal long val', async () => {
|
|
163
|
-
assertEquals(
|
|
164
|
-
toBigIntBE(Buffer.from(
|
|
165
|
-
'd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544',
|
|
166
|
-
'hex')),
|
|
167
|
-
BigInt(
|
|
168
|
-
`0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544`));
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
describe('Try bigint conversion (little endian)', () => {
|
|
173
|
-
it('0 should equal 0n', () => {
|
|
174
|
-
toBufferLE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
|
|
175
|
-
0, 0, 0, 0, 0, 0, 0, 0
|
|
176
|
-
]));
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it('1 should equal 1n', async () => {
|
|
180
|
-
toBufferLE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
|
|
181
|
-
1, 0, 0, 0, 0, 0, 0, 0
|
|
182
|
-
]));
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
it('1 should equal 1n (32 byte)', async () => {
|
|
187
|
-
toBufferLE(BigInt(`1`), 32)
|
|
188
|
-
.should.deep.equal(Buffer.from(
|
|
189
|
-
'0100000000000000000000000000000000000000000000000000000000000000',
|
|
190
|
-
'hex'));
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it('0xdead should equal 0xdeadn (6 byte)', async () => {
|
|
194
|
-
toBufferLE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
|
|
195
|
-
0xad, 0xde, 0, 0, 0, 0
|
|
196
|
-
]));
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
|
|
200
|
-
toBufferLE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
|
|
201
|
-
0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0
|
|
202
|
-
]));
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
|
|
206
|
-
async () => {
|
|
207
|
-
toBufferLE(BigInt(`0xbadc0ffee0ddf00d`), 8)
|
|
208
|
-
.should.deep.equal(
|
|
209
|
-
Buffer.from([0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba]));
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
|
|
213
|
-
async () => {
|
|
214
|
-
toBufferLE(BigInt(`0xbadc0ffee0ddf00ddeadbeef`), 12)
|
|
215
|
-
.should.deep.equal(Buffer.from([
|
|
216
|
-
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc,
|
|
217
|
-
0xba
|
|
218
|
-
]));
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it('long value should equal long val', async () => {
|
|
222
|
-
toBufferLE(BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`), 24)
|
|
223
|
-
.should.deep.equal(Buffer.from([
|
|
224
|
-
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0,
|
|
225
|
-
0xfe, 0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde,
|
|
226
|
-
0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
|
|
227
|
-
]));
|
|
228
|
-
});
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
describe('Try bigint conversion (big endian)', () => {
|
|
233
|
-
it('0 should equal 0n', () => {
|
|
234
|
-
toBufferBE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
|
|
235
|
-
0, 0, 0, 0, 0, 0, 0, 0
|
|
236
|
-
]));
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
it('1 should equal 1n', async () => {
|
|
240
|
-
toBufferBE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
|
|
241
|
-
0, 0, 0, 0, 0, 0, 0, 1
|
|
242
|
-
]));
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it('1 should equal 1n (32 byte)', async () => {
|
|
246
|
-
toBufferBE(BigInt(`1`), 32)
|
|
247
|
-
.should.deep.equal(Buffer.from(
|
|
248
|
-
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
249
|
-
'hex'));
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it('0xdead should equal 0xdeadn (6 byte)', async () => {
|
|
253
|
-
toBufferBE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
|
|
254
|
-
0, 0, 0, 0, 0xde, 0xad
|
|
255
|
-
]));
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
|
|
259
|
-
toBufferBE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
|
|
260
|
-
0, 0, 0, 0, 0, 0xde, 0xad, 0xbe, 0xef
|
|
261
|
-
]));
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
|
|
265
|
-
async () => {
|
|
266
|
-
toBufferBE(BigInt(`0xbadc0ffee0ddf00d`), 8)
|
|
267
|
-
.should.deep.equal(
|
|
268
|
-
Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d]));
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
|
|
272
|
-
async () => {
|
|
273
|
-
toBufferBE(BigInt(`0xbadc0ffee0ddf00ddeadbeef`), 12)
|
|
274
|
-
.should.deep.equal(Buffer.from([
|
|
275
|
-
0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe,
|
|
276
|
-
0xef
|
|
277
|
-
]));
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
it('long value should equal long val', async () => {
|
|
281
|
-
toBufferBE(BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`), 24)
|
|
282
|
-
.should.deep.equal(Buffer.from([
|
|
283
|
-
0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d,
|
|
284
|
-
0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe,
|
|
285
|
-
0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef
|
|
286
|
-
]));
|
|
287
|
-
});
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
describe('Conversion Utilities - bigintToBuf and bufToBigint', () => {
|
|
291
|
-
const bigintToBuf = lib.bigintToBuf;
|
|
292
|
-
const bufToBigint = lib.bufToBigint;
|
|
293
|
-
|
|
294
|
-
it('should convert 0 to buffer', () => {
|
|
295
|
-
const buf = bigintToBuf(BigInt(0));
|
|
296
|
-
buf.should.deep.equal(Buffer.from([0]));
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
it('should convert small number to buffer', () => {
|
|
300
|
-
const buf = bigintToBuf(BigInt(123456789));
|
|
301
|
-
assertEquals(bufToBigint(buf), BigInt(123456789));
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
it('should round-trip convert medium numbers', () => {
|
|
305
|
-
const original = BigInt('0xdeadbeef');
|
|
306
|
-
const buf = bigintToBuf(original);
|
|
307
|
-
const result = bufToBigint(buf);
|
|
308
|
-
assertEquals(result, original);
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
it('should round-trip convert large numbers', () => {
|
|
312
|
-
const original = BigInt('0xbadc0ffee0ddf00ddeadbeef');
|
|
313
|
-
const buf = bigintToBuf(original);
|
|
314
|
-
const result = bufToBigint(buf);
|
|
315
|
-
assertEquals(result, original);
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
it('should throw error for negative bigint', () => {
|
|
319
|
-
chai.expect(() => bigintToBuf(BigInt(-1))).to.throw('negative bigint');
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
describe('Conversion Utilities - bigintToHex and hexToBigint', () => {
|
|
324
|
-
const bigintToHex = lib.bigintToHex;
|
|
325
|
-
const hexToBigint = lib.hexToBigint;
|
|
326
|
-
|
|
327
|
-
it('should convert 0 to hex', () => {
|
|
328
|
-
bigintToHex(BigInt(0)).should.equal('00');
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
it('should convert small number to hex', () => {
|
|
332
|
-
bigintToHex(BigInt(255)).should.equal('ff');
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
it('should convert number to even-length hex', () => {
|
|
336
|
-
bigintToHex(BigInt(15)).should.equal('0f');
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
it('should convert hex string to bigint', () => {
|
|
340
|
-
assertEquals(hexToBigint('deadbeef'), BigInt('0xdeadbeef'));
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
it('should handle hex string with 0x prefix', () => {
|
|
344
|
-
assertEquals(hexToBigint('0xdeadbeef'), BigInt('0xdeadbeef'));
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
it('should round-trip convert', () => {
|
|
348
|
-
const original = BigInt('0xbadc0ffee0ddf00ddeadbeef');
|
|
349
|
-
const hex = bigintToHex(original);
|
|
350
|
-
const result = hexToBigint(hex);
|
|
351
|
-
assertEquals(result, original);
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
it('should handle empty string', () => {
|
|
355
|
-
assertEquals(hexToBigint(''), BigInt(0));
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
it('should throw error for negative bigint', () => {
|
|
359
|
-
chai.expect(() => bigintToHex(BigInt(-1))).to.throw('negative bigint');
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
describe('Conversion Utilities - bigintToText and textToBigint', () => {
|
|
364
|
-
const bigintToText = lib.bigintToText;
|
|
365
|
-
const textToBigint = lib.textToBigint;
|
|
366
|
-
|
|
367
|
-
it('should convert 0 to text', () => {
|
|
368
|
-
bigintToText(BigInt(0)).should.equal('0');
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
it('should convert positive number to text', () => {
|
|
372
|
-
bigintToText(BigInt(123456789)).should.equal('123456789');
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
it('should convert large number to text', () => {
|
|
376
|
-
bigintToText(BigInt('0xdeadbeef')).should.equal('3735928559');
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
it('should convert text to bigint', () => {
|
|
380
|
-
assertEquals(textToBigint('123456789'), BigInt(123456789));
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
it('should round-trip convert', () => {
|
|
384
|
-
const original = BigInt('9876543210123456789');
|
|
385
|
-
const text = bigintToText(original);
|
|
386
|
-
const result = textToBigint(text);
|
|
387
|
-
assertEquals(result, original);
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
it('should throw error for empty string', () => {
|
|
391
|
-
chai.expect(() => textToBigint('')).to.throw('cannot be empty');
|
|
392
|
-
});
|
|
393
|
-
|
|
394
|
-
it('should throw error for invalid decimal string', () => {
|
|
395
|
-
chai.expect(() => textToBigint('abc')).to.throw('invalid decimal');
|
|
396
|
-
});
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
describe('Conversion Utilities - bigintToBase64 and base64ToBigint', () => {
|
|
400
|
-
const bigintToBase64 = lib.bigintToBase64;
|
|
401
|
-
const base64ToBigint = lib.base64ToBigint;
|
|
402
|
-
|
|
403
|
-
it('should convert 0 to base64', () => {
|
|
404
|
-
const b64 = bigintToBase64(BigInt(0));
|
|
405
|
-
b64.should.be.a('string');
|
|
406
|
-
assertEquals(base64ToBigint(b64), BigInt(0));
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
it('should convert small number to base64', () => {
|
|
410
|
-
const original = BigInt(123456789);
|
|
411
|
-
const b64 = bigintToBase64(original);
|
|
412
|
-
b64.should.equal('B1vNFQ==');
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
it('should convert base64 to bigint', () => {
|
|
416
|
-
assertEquals(base64ToBigint('B1vNFQ=='), BigInt(123456789));
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
it('should round-trip convert medium numbers', () => {
|
|
420
|
-
const original = BigInt('0xdeadbeef');
|
|
421
|
-
const b64 = bigintToBase64(original);
|
|
422
|
-
const result = base64ToBigint(b64);
|
|
423
|
-
assertEquals(result, original);
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
it('should round-trip convert large numbers', () => {
|
|
427
|
-
const original = BigInt('0xbadc0ffee0ddf00ddeadbeef');
|
|
428
|
-
const b64 = bigintToBase64(original);
|
|
429
|
-
const result = base64ToBigint(b64);
|
|
430
|
-
assertEquals(result, original);
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
it('should throw error for negative bigint', () => {
|
|
434
|
-
chai.expect(() => bigintToBase64(BigInt(-1))).to.throw('negative bigint');
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
it('should throw error for empty base64 string', () => {
|
|
438
|
-
chai.expect(() => base64ToBigint('')).to.throw('cannot be empty');
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
it('should throw error for invalid base64 string', () => {
|
|
442
|
-
chai.expect(() => base64ToBigint('invalid!@#')).to.throw('invalid base64');
|
|
443
|
-
});
|
|
444
|
-
});
|