@gsknnft/bigint-buffer 1.2.0

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 ADDED
@@ -0,0 +1,51 @@
1
+ language: node_js
2
+ node_js:
3
+ - node
4
+ - '10'
5
+ sudo : false
6
+ env:
7
+ global:
8
+ - DISPLAY=:99.0
9
+ - secure: SQDlqZZTkrbRW/wYLPgEwFQpkSrs65MiF0JgO+cwSCAG1m0c7EuBTzvttAXzKK+coZXjd2+QJcAOBi6YUP84OdSf31PojIy4/SLuZxuFQa0sGey+8oSDqgMntpSthO9nom0s6u2vB61IVfcRVIUNv5L3J5i5ZIGlKGgC2MIBzsnhx0L//eyh/bLszR27ajCT7RG+pnqpk010eaDtRabOJTtO0C2kbBQoW2SvJIisDsRj5zqVyl03pZJQQAJeseiNlLokucrgitDWENE+tEJ2w1x1yl+901tgU+BIi4iLQTezcSAxnF149U/Nof7LpCba+1Y+PPh1SRpO4100CT6O46qW0O1keVfTmZL/R3ghBM6H0AtV1s48k/XCpHV9CGwxjBHoCN1kKvSO9B1BJuAdlmgobOEdze+X81nilSBhBmYMI1XPLbXqTo2ImfaFeDzveuPJH6+HMCc+8sDlhgLcrOiteAsY8RsI0ngBkutsWWv7QcRq29e4VX2eBy0j1VJZNf0FPAcKpWF8eIaPw7Z0CuWMs2hM0v0ns58jDcOL+1k4kKyJ10OOmANqNX+NZYRJJ05dd/VMrlh37teARI64PFSG6eIoCniW0GtrUcpX4KhqpaRSUDpsKvncXNdNydumMyMQjeQqyHqahvIE2ONVXXELD2T/EV6TpazDR5KSSo0=
10
+ - secure: ZwKAlaNZ8liLknkAfFN+TztrILi8Q06AknHUj0Zd+9PjzD5n6tpzp5g1nE0MEvF9bqkGkwf6vwWJVKcSXWM9J4a2pfoLe9wsVcjq+AkmWnZG0zV26ie3UPoa8d/PezRtxz+4vErvQcOqnESVPJAqeWCt0fH1WvnEcjaY3BYyyDbWwAPamWr/v9c4ZLKxJqJmgtczMCrRahd6Dz+HGGJ9BehhOXtse+rT5qFI6w2SMXaisDzk2twMu0fK1I66G2IsesIKErPigCeEPahsP2yu831+S18YvxJtao7tXpluMBf+RNhD9eI1evUu6IxpF419ANDJ7BpJ8uGfRVzIz9kQIAmcy/1s7g3otT633XngUfNYDisUeLJ39plclOHw+sVeLL49Xa9k+/U0CzlOThNlCj0hY2uJW3Ot9NjvKDjdR/UkgEBCl6nIDAnRQk9xSJ4ws1f9Yp5CgEXcXvu7077i6O8vG7+k08qHDSkxEEwQjB2PwjhcPfg9ieAwafblM3szYwzblL4UpROp/L/XmmzjR/cOVrS93RIpeLqDr2CCMKGd0GoOFtDse7u43SJMIAZ+O5DNtNpqIQ22V13p4P3L2C1RV3cjE55luEYVR1JZ7AzaxD+hZu2zcU2301MuSgFLTKMEu43HV1tPJ6QtCcSf+kH/LK/TPfTLveW23xkkZM0=
11
+ matrix:
12
+ - TEST_SUITE=test:node
13
+ addons:
14
+ chrome: stable
15
+ services:
16
+ - xvfb
17
+ cache:
18
+ directories:
19
+ - $HOME/.npm
20
+ matrix:
21
+ fast_finish: true
22
+ include:
23
+ - os: linux
24
+ node_js: '10'
25
+ env: TEST_SUITE=test:browser
26
+ - os: windows
27
+ node_js: '10'
28
+ env: TEST_SUITE=test:node
29
+ script: npm run $TEST_SUITE
30
+ before_deploy:
31
+ - npm pack
32
+ deploy:
33
+ - provider: releases
34
+ api_key: "$GITHUB_TOKEN"
35
+ file_glob: true
36
+ file: bigint-buffer-*.tgz
37
+ skip_cleanup: true
38
+ on:
39
+ branch: master
40
+ node: '10'
41
+ condition: "$TEST_SUITE = test:node"
42
+ tags: true
43
+ - provider: npm
44
+ api_key: "$NPM_TOKEN"
45
+ skip_cleanup: true
46
+ email: mwei@cs.ucsd.edu
47
+ on:
48
+ branch: master
49
+ node: '10'
50
+ condition: "$TEST_SUITE = test:node"
51
+ tags: true
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/PR_TEMPLATE.md ADDED
@@ -0,0 +1,53 @@
1
+
2
+ ---
3
+
4
+ ## 📑 PR Template (PULL_REQUEST_TEMPLATE.md)
5
+
6
+ ```markdown
7
+ # Pull Request for @diamondprotocol/bigint-buffer
8
+
9
+ Thank you for contributing to **@diamondprotocol/bigint-buffer**! Please fill out the details below.
10
+
11
+ ---
12
+
13
+ ## 📋 What does this PR do?
14
+
15
+ <!--
16
+ - Security fix? (describe vulnerability/CVE)
17
+ - Feature addition? (describe feature, reason, protocol impact)
18
+ - Bug fix? (link to test/issue)
19
+ - CI/test/benchmark improvements?
20
+ - Documentation or README changes?
21
+ -->
22
+
23
+ ---
24
+
25
+ ## 🚦 Security & Breaking Change Checklist
26
+
27
+ - [ ] I have **added or updated tests** for this change.
28
+ - [ ] I have **run all test suites** and verified that everything passes.
29
+ - [ ] If changing buffer size limits or any protocol-relevant logic:
30
+ - [ ] I have documented the reason and added a warning in README.
31
+ - [ ] I have included reference(s) to any relevant CVEs or advisories.
32
+
33
+ ---
34
+
35
+ ## 💬 Additional Notes
36
+
37
+ <!--
38
+ - Did you check compatibility with Solana, Jito, Vera, or protocol X?
39
+ - Is there any API break?
40
+ - Link to related issues, discussions, or upstream PRs if any.
41
+ -->
42
+
43
+ ---
44
+
45
+ ## 🔗 Related Links
46
+
47
+ - [bigint-buffer on npm](https://www.npmjs.com/package/bigint-buffer)
48
+ - [Original repo](https://github.com/no2chem/bigint-buffer)
49
+ - [Current security advisory](https://github.com/advisories/GHSA-3gc7-fjrx-p6mg)
50
+
51
+ ---
52
+
53
+ *Thank you for helping keep supply-chain infra safe and strong!*
package/README.md ADDED
@@ -0,0 +1,149 @@
1
+ # 💪🔢 bigint-buffer: Buffer Utilities for TC39 BigInt Proposal
2
+ [![NPM Package](https://img.shields.io/npm/v/bigint-buffer.svg?style=flat-square)](https://www.npmjs.org/package/bigint-buffer)
3
+ [![Build Status](https://img.shields.io/travis/com/no2chem/bigint-buffer.svg?branch=master&style=flat-square)](https://travis-ci.com/no2chem/bigint-buffer)
4
+ [![Coverage Status](https://img.shields.io/coveralls/no2chem/bigint-buffer.svg?style=flat-square)](https://coveralls.io/r/no2chem/bigint-buffer)
5
+ ![node](https://img.shields.io/node/v/bigint-buffer.svg?style=flat-square)
6
+ [![Maintained Fork](https://img.shields.io/badge/fork-maintained-blue)](https://github.com/sigilnet/bigintbuffer)
7
+
8
+ [bigint-buffer](https://www.npmjs.org/package/bigint-buffer) is a utility converts [TC39 Proposed BigInts](https://github.com/tc39/proposal-bigint) to and from buffers. This utility is necessary because BigInts, as proposed, do not support direct conversion between Buffers (or UInt8Arrays), but rather require conversion from buffers to hexadecimal strings then to BigInts, which is suboptimal. This utility includes N-API bindings, so under node, conversion is performed without generating a hexadecimal string. In the browser, normal string conversion is used.
9
+
10
+ ---
11
+
12
+ ## 🔐 Security Notice: This Module Has Been Reclaimed
13
+
14
+ As of October 2025, `bigint-buffer@1.1.5` is **compromised and flagged by multiple audit tools** due to unresolved vulnerabilities in its native bindings and transitive dependencies. No upstream patch has been published.
15
+
16
+ This fork — `@sigilnet/bigintbuffer@1.2.0` — is a **sovereign override**:
17
+ - ✅ Rebuilt with modern TypeScript and Rollup
18
+ - ✅ Native bindings patched and rebuilt via `node-gyp`
19
+ - ✅ Browser fallback formalized via `"browser"` field
20
+ - ✅ ESM/CJS duality declared via `"exports"`
21
+ - ✅ Peer dependency alignment and audit compliance restored
22
+
23
+ If you're using `bigint-buffer` in a secure or reproducible system, **migrate to `@sigilnet/bigintbuffer`** or override via `pnpm`:
24
+
25
+ ```json
26
+ "pnpm": {
27
+ "overrides": {
28
+ "bigint-buffer": "link:./patched_deps/bigint-buffer"
29
+ }
30
+ }
31
+ ```
32
+
33
+ This fork is maintained by CoreFlame/GSKNNFT as part of the SigilNet ecosystem. It is the only known secure, reproducible implementation of BigInt ↔ Buffer conversion with native fallback.
34
+
35
+ ---
36
+
37
+ # Why use BigInts?
38
+
39
+ BigInts are currently a stage 3 proposal, supported in Node 10 and V8 v6.7. BigInts are primitive arbitrary precision integers, overcoming the limitations of the number type in javascript, which only supports up to 53 bits of precision.
40
+
41
+ In many applications, manipulating 64, 128 or even 256 bit numbers is quite common. For example, database identifiers are often 128 bits, and hashes are often 256 bits (If you're looking for hashing support, try out [bigint-hash](https://github.com/no2chem/bigint-hash)). Before BigInts, manipulating these numbers safely required either allocating a Buffer or UInt8Arrays, which is quite expensive compared to a number, since Buffers are allocated on the heap.
42
+
43
+ BigInts solve this problem by introducing a primitive that can hold
44
+ arbitrary precision integers, reducing memory pressure and allowing
45
+ the runtime to better optimize arithmetic operations. This results in significant performance improvements - 10x-100x for simple equality comparisons (using `===` vs `Buffer.compare()`):
46
+
47
+ ```
48
+ Buffer equality comparison: 11916844±4.23% ops/s 83.91±17.293 ns/op (91 runs)
49
+ bigint equality comparison: 798024851±0.29% ops/s 1.25±0.017 ns/op (91 runs)
50
+ ```
51
+
52
+ Before BigInts, you probably used a library such as the widely used [bn.js](https://www.npmjs.com/package/bn.js).
53
+ bn.js fares a little better than a plain Buffer, but is still 5-10x slower than the bigint:
54
+ ```
55
+ BN equality comparison: 73255774±0.67% ops/s 13.65±0.442 ns/op (89 runs)
56
+ ```
57
+
58
+ bigints are also much better with arithmetic, here are the results compared to BN for multiplying two
59
+ 128-bit integers, yielding a 4x improvement:
60
+ ```
61
+ BN multiply: 4763236±0.49% ops/s 209.94±5.111 ns/op (93 runs)
62
+ bigint multiply: 15268666±0.92% ops/s 65.49±2.938 ns/op (92 runs)
63
+ ```
64
+
65
+ # So what's the problem?
66
+
67
+ BigInts, unfortunately lack an efficient way to be converted back and forth between buffers. When dealing with serialized data or legacy node code, you'll often want to generate a BigInt from a buffer, or convert a BigInt to a Buffer in order to send a BigInt over the wire.
68
+
69
+ Currently, the only method to generate a new BigInt is with the BigInt constructor. Unfortunately, it doesn't support Buffers, though it may in the future:
70
+
71
+ ```
72
+ > BigInt(Buffer.from([1]))
73
+ SyntaxError: Cannot convert to a BigInt
74
+ ```
75
+
76
+ Instead, you need to convert the Buffer to a hexadecimal string of the correct format. For example:
77
+
78
+ ```
79
+ > BigInt(`0x${buf.toString('hex')}`);
80
+ 1n
81
+ ```
82
+
83
+ These conversions are not only quite expensive, but obviate a lot of the performance gains we get from BigInts. For example, on a large buffer, this conversion can take over 100x the time to do a comparison:
84
+ ```
85
+ bigint from hex string from buffer (huge): 1230607±1.02% ops/s 812.61±40.013 ns/op (89 runs)
86
+ ```
87
+
88
+ # And... bigint-buffer helps how?
89
+
90
+ bigint-buffer introduces four functions for conversion between buffers and bigints. A small example follows:
91
+ ```typescript
92
+ import {toBigIntBE, toBigIntLE, toBufferBE, toBufferLE} from 'bigint-buffer';
93
+
94
+ // Get a big endian buffer of the given width
95
+ toBufferBE(0xdeadbeefn, 8);
96
+ // ↪ <Buffer 00 00 00 00 de ad be ef>
97
+
98
+ // Get a little endian buffer of the given width
99
+ toBufferLE(0xdeadbeefn, 8);
100
+ // ↪ <Buffer ef be ad de 00 00 00 00>
101
+
102
+ // Get a BigInt from a buffer in big endian format
103
+ toBigIntBE(Buffer.from('deadbeef', 'hex'));
104
+ // ↪ 3735928559n (0xdeadbeefn)
105
+
106
+ // Get a BigInt from a buffer in little endian format
107
+ toBigIntLE(Buffer.from('deadbeef', 'hex'));
108
+ // ↪ 4022250974n (0xefbeadd0en)
109
+ ```
110
+
111
+ bigint-buffer uses N-API native bindings to perform the conversion efficiently without generating the
112
+ immediate hex strings necessary in pure javascript. This results in a significant performance increase,
113
+ about 2x for small buffer to bigint conversions, and 8x better than BN:
114
+
115
+ ```
116
+ BN to buffer (small): 981703±68.30% ops/s 1018.64±3194.648 ns/op (81 runs)
117
+ bigint from hex string from buffer (small): 2804915±5.00% ops/s 356.52±85.371 ns/op (88 runs)
118
+ LE bigint-buffer ToBigInt (small): 5932704±1.62% ops/s 168.56±12.971 ns/op (87 runs)
119
+ ```
120
+
121
+ And about 3.3x for bigint to buffer conversions, and 17x better than BN:
122
+ ```
123
+ BN to buffer (large): 339437±2.85% ops/s 2946.06±385.504 ns/op (81 runs)
124
+ BE bigint to hex string to buffer (large): 1714292±1.35% ops/s 583.33±37.995 ns/op (90 runs)
125
+ BE bigint-buffer to buffer (large, truncated): 5977218±4.68% ops/s 167.30±37.284 ns/op (87 runs)
126
+ ```
127
+
128
+ You can run the benchmarks by running `npm run benchmark`.
129
+
130
+ # Typescript Support
131
+
132
+ bigint-buffer supplies typescript bindings, but BigInts are still not supported in typescript, though
133
+ a pull request has been opened, so support should be coming soon. If you are using typescript,
134
+ @calebsander has put up a pull request and the instructions in [this post](https://github.com/Microsoft/TypeScript/issues/15096#issuecomment-419654748).
135
+
136
+ # Install
137
+
138
+ Add bigint-buffer to your project with:
139
+
140
+ > `npm install bigint-buffer`
141
+
142
+ # Documentation
143
+
144
+ Basic API documentation can be found [here](https://no2chem.github.io/bigint-buffer/). Note that v1.0.0 changes
145
+ the name of the original functions to meet style guidelines.
146
+
147
+ # Benchmarks
148
+
149
+ Benchmarks can be run by executing `npm run benchmark` from the package directory.
package/binding.gyp ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "targets": [{
3
+ "target_name": "bigint_buffer",
4
+ "sources": [
5
+ "src/bigint-buffer.c"
6
+ ]
7
+ }]
8
+ }
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.isNative = void 0;
5
+ exports.toBigIntLE = toBigIntLE;
6
+ exports.validateBigIntBuffer = validateBigIntBuffer;
7
+ exports.toBigIntBE = toBigIntBE;
8
+ exports.toBufferLE = toBufferLE;
9
+ exports.toBufferBE = toBufferBE;
10
+ exports.isNative = false;
11
+ /**
12
+ * Convert a little-endian buffer into a BigInt.
13
+ * @param buf The little-endian buffer to convert
14
+ * @returns A BigInt with the little-endian representation of buf.
15
+ */
16
+ function toBigIntLE(buf) {
17
+ {
18
+ const reversed = Buffer.from(buf);
19
+ reversed.reverse();
20
+ const hex = reversed.toString('hex');
21
+ if (hex.length === 0) {
22
+ return BigInt(0);
23
+ }
24
+ return BigInt(`0x${hex}`);
25
+ }
26
+ }
27
+ function validateBigIntBuffer() {
28
+ try {
29
+ const test = toBigIntLE(Buffer.from([0x01, 0x00]));
30
+ return test === BigInt(1);
31
+ }
32
+ catch {
33
+ return false;
34
+ }
35
+ }
36
+ /**
37
+ * Convert a big-endian buffer into a BigInt
38
+ * @param buf The big-endian buffer to convert.
39
+ * @returns A BigInt with the big-endian representation of buf.
40
+ */
41
+ function toBigIntBE(buf) {
42
+ {
43
+ const hex = buf.toString('hex');
44
+ if (hex.length === 0) {
45
+ return BigInt(0);
46
+ }
47
+ return BigInt(`0x${hex}`);
48
+ }
49
+ }
50
+ /**
51
+ * Convert a BigInt to a little-endian buffer.
52
+ * @param num The BigInt to convert.
53
+ * @param width The number of bytes that the resulting buffer should be.
54
+ * @returns A little-endian buffer representation of num.
55
+ */
56
+ function toBufferLE(num, width) {
57
+ {
58
+ const hex = num.toString(16);
59
+ const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
60
+ buffer.reverse();
61
+ return buffer;
62
+ }
63
+ }
64
+ /**
65
+ * Convert a BigInt to a big-endian buffer.
66
+ * @param num The BigInt to convert.
67
+ * @param width The number of bytes that the resulting buffer should be.
68
+ * @returns A big-endian buffer representation of num.
69
+ */
70
+ function toBufferBE(num, width) {
71
+ {
72
+ const hex = num.toString(16);
73
+ return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
74
+ }
75
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ export declare let isNative: boolean;
2
+ /**
3
+ * Convert a little-endian buffer into a BigInt.
4
+ * @param buf The little-endian buffer to convert
5
+ * @returns A BigInt with the little-endian representation of buf.
6
+ */
7
+ export declare function toBigIntLE(buf: Buffer): bigint;
8
+ export declare function validateBigIntBuffer(): boolean;
9
+ /**
10
+ * Convert a big-endian buffer into a BigInt
11
+ * @param buf The big-endian buffer to convert.
12
+ * @returns A BigInt with the big-endian representation of buf.
13
+ */
14
+ export declare function toBigIntBE(buf: Buffer): bigint;
15
+ /**
16
+ * Convert a BigInt to a little-endian buffer.
17
+ * @param num The BigInt to convert.
18
+ * @param width The number of bytes that the resulting buffer should be.
19
+ * @returns A little-endian buffer representation of num.
20
+ */
21
+ export declare function toBufferLE(num: bigint, width: number): Buffer;
22
+ /**
23
+ * Convert a BigInt to a big-endian buffer.
24
+ * @param num The BigInt to convert.
25
+ * @param width The number of bytes that the resulting buffer should be.
26
+ * @returns A big-endian buffer representation of num.
27
+ */
28
+ export declare function toBufferBE(num: bigint, width: number): Buffer;
@@ -0,0 +1 @@
1
+ import 'mocha';
package/dist/node.js ADDED
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.isNative = void 0;
5
+ exports.toBigIntLE = toBigIntLE;
6
+ exports.validateBigIntBuffer = validateBigIntBuffer;
7
+ exports.toBigIntBE = toBigIntBE;
8
+ exports.toBufferLE = toBufferLE;
9
+ exports.toBufferBE = toBufferBE;
10
+ let converter;
11
+ exports.isNative = false;
12
+ {
13
+ try {
14
+ converter = require('bindings')('bigint_buffer');
15
+ exports.isNative = !false && converter !== undefined;
16
+ }
17
+ catch (e) {
18
+ console.warn('bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)');
19
+ }
20
+ }
21
+ /**
22
+ * Convert a little-endian buffer into a BigInt.
23
+ * @param buf The little-endian buffer to convert
24
+ * @returns A BigInt with the little-endian representation of buf.
25
+ */
26
+ function toBigIntLE(buf) {
27
+ if (converter === undefined) {
28
+ const reversed = Buffer.from(buf);
29
+ reversed.reverse();
30
+ const hex = reversed.toString('hex');
31
+ if (hex.length === 0) {
32
+ return BigInt(0);
33
+ }
34
+ return BigInt(`0x${hex}`);
35
+ }
36
+ return converter.toBigInt(buf, false);
37
+ }
38
+ function validateBigIntBuffer() {
39
+ try {
40
+ const test = toBigIntLE(Buffer.from([0x01, 0x00]));
41
+ return test === BigInt(1);
42
+ }
43
+ catch {
44
+ return false;
45
+ }
46
+ }
47
+ /**
48
+ * Convert a big-endian buffer into a BigInt
49
+ * @param buf The big-endian buffer to convert.
50
+ * @returns A BigInt with the big-endian representation of buf.
51
+ */
52
+ function toBigIntBE(buf) {
53
+ if (converter === undefined) {
54
+ const hex = buf.toString('hex');
55
+ if (hex.length === 0) {
56
+ return BigInt(0);
57
+ }
58
+ return BigInt(`0x${hex}`);
59
+ }
60
+ return converter.toBigInt(buf, true);
61
+ }
62
+ /**
63
+ * Convert a BigInt to a little-endian buffer.
64
+ * @param num The BigInt to convert.
65
+ * @param width The number of bytes that the resulting buffer should be.
66
+ * @returns A little-endian buffer representation of num.
67
+ */
68
+ function toBufferLE(num, width) {
69
+ if (converter === undefined) {
70
+ const hex = num.toString(16);
71
+ const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
72
+ buffer.reverse();
73
+ return buffer;
74
+ }
75
+ // Allocation is done here, since it is slower using napi in C
76
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
77
+ }
78
+ /**
79
+ * Convert a BigInt to a big-endian buffer.
80
+ * @param num The BigInt to convert.
81
+ * @param width The number of bytes that the resulting buffer should be.
82
+ * @returns A big-endian buffer representation of num.
83
+ */
84
+ function toBufferBE(num, width) {
85
+ if (converter === undefined) {
86
+ const hex = num.toString(16);
87
+ return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
88
+ }
89
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
90
+ }
@@ -0,0 +1,3 @@
1
+ // not used by code, only for documentation generation (since typedoc can't handle BigInt yet)
2
+ type BigInt = number
3
+ declare const BigInt: typeof Number