@bitcoin-js/tiny-secp256k1-asmjs 2.2.2
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/LICENSE +21 -0
- package/README.md +355 -0
- package/lib/cjs/index.cjs +387 -0
- package/lib/cjs/rand.browser.cjs +20 -0
- package/lib/cjs/rand.cjs +8 -0
- package/lib/cjs/secp256k1.asm.cjs +33218 -0
- package/lib/cjs/validate.cjs +144 -0
- package/lib/cjs/validate_error.cjs +26 -0
- package/lib/cjs/wasm_loader.cjs +7 -0
- package/lib/cjs/wasm_path_cjs.cjs +12 -0
- package/lib/index.d.ts +33 -0
- package/lib/index.js +361 -0
- package/lib/rand.browser.d.ts +1 -0
- package/lib/rand.browser.js +16 -0
- package/lib/rand.d.ts +1 -0
- package/lib/rand.js +4 -0
- package/lib/secp256k1.asm.js +33218 -0
- package/lib/validate.d.ts +25 -0
- package/lib/validate.js +124 -0
- package/lib/validate_error.d.ts +9 -0
- package/lib/validate_error.js +22 -0
- package/lib/wasm_loader.d.ts +41 -0
- package/lib/wasm_loader.js +5 -0
- package/lib/wasm_path.d.ts +1 -0
- package/lib/wasm_path.js +5 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 bitcoinjs contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
# tiny-secp256k1-asmjs
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.org/package/tiny-secp256k1-asmjs)
|
|
4
|
+
|
|
5
|
+
This library is under development, and, like the [secp256k1](https://github.com/bitcoin-core/secp256k1) C library (through [secp256k1-sys](https://github.com/rust-bitcoin/rust-secp256k1/) Rust crate) it depends on, this is a research effort to determine an optimal API for end-users of the bitcoinjs ecosystem.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
### npm
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install tiny-secp256k1-asmjs
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### yarn
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
yarn add tiny-secp256k1-asmjs
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## WebAssembly and Node.js version
|
|
22
|
+
|
|
23
|
+
Previous version of `tiny-secp256k1` implement [C++ addon](https://nodejs.org/api/addons.html) through [NAN (Native Abstractions for Node.js)](https://github.com/nodejs/nan) and [elliptic](https://github.com/indutny/elliptic) as fallback when addon can not be built or in browser-like environement.
|
|
24
|
+
|
|
25
|
+
Current version use Rust crate (which use C library) compiled to [WebAssembly](https://developer.mozilla.org/en-US/docs/WebAssembly). With Wasm same code executed in any environment. Wasm is faster than `elliptic` but slower than node bindings ([results in PR](https://github.com/bitcoinjs/tiny-secp256k1/pull/53#issuecomment-801844450) or you can run own benchmark in `benches` directory).
|
|
26
|
+
|
|
27
|
+
## Building
|
|
28
|
+
|
|
29
|
+
For building locally you need C/C++ toolchain, Rust version >=1.50.0 and `wasm-opt` from [binaryen](https://github.com/WebAssembly/binaryen).
|
|
30
|
+
|
|
31
|
+
[rustup](https://rustup.rs/) is a recommended way to install `Rust`. You also will need `wasm32-unknown-unknown` target.
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
rustup toolchain install stable --target wasm32-unknown-unknown --component clippy --component rustfmt
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
After installing development dependencies with `npm` you can build Wasm:
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
make build-wasm
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
or run tests:
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
make test
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Alternative way is to use [Docker](https://www.docker.com/):
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
% docker build -t tiny-secp256k1 .
|
|
53
|
+
% docker run -it --rm -v `pwd`:/tiny-secp256k1 -w /tiny-secp256k1 tiny-secp256k1
|
|
54
|
+
# make build
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Examples
|
|
58
|
+
|
|
59
|
+
`tiny-secp256k1` includes two examples. First is [simple script for Node.js](examples/random-in-node) which generate random data and print arguments and methods results. Second is [React app](examples/react-app).
|
|
60
|
+
|
|
61
|
+
React app is builded in GitHub Actions on each commit to master branch and uploaded to [gh-pages](https://github.com/bitcoinjs/tiny-secp256k1/tree/gh-pages) branch, which is always available online: https://bitcoinjs.github.io/tiny-secp256k1/
|
|
62
|
+
|
|
63
|
+
## Documentation
|
|
64
|
+
|
|
65
|
+
### isPoint (A)
|
|
66
|
+
|
|
67
|
+
```haskell
|
|
68
|
+
isPoint :: Buffer -> Bool
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Returns `false` if
|
|
72
|
+
|
|
73
|
+
- `A` is not encoded with a sequence tag of `0x02`, `0x03` or `0x04`
|
|
74
|
+
- `A.x` is not in `[1...p - 1]`
|
|
75
|
+
- `A.y` is not in `[1...p - 1]`
|
|
76
|
+
|
|
77
|
+
### isPointCompressed (A)
|
|
78
|
+
|
|
79
|
+
```haskell
|
|
80
|
+
isPointCompressed :: Buffer -> Bool
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Returns `false` if the pubkey is **not** compressed.
|
|
84
|
+
|
|
85
|
+
### isXOnlyPoint (A)
|
|
86
|
+
|
|
87
|
+
```haskell
|
|
88
|
+
isXOnlyPoint :: Buffer -> Bool
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Returns `false` if the pubkey is **not** an xOnlyPubkey.
|
|
92
|
+
|
|
93
|
+
### isPrivate (d)
|
|
94
|
+
|
|
95
|
+
```haskell
|
|
96
|
+
isPrivate :: Buffer -> Bool
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Returns `false` if
|
|
100
|
+
|
|
101
|
+
- `d` is not 256-bit, or
|
|
102
|
+
- `d` is not in `[1..order - 1]`
|
|
103
|
+
|
|
104
|
+
### pointAdd (A, B[, compressed])
|
|
105
|
+
|
|
106
|
+
```haskell
|
|
107
|
+
pointAdd :: Buffer -> Buffer [-> Bool] -> Maybe Buffer
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Returns `null` if result is at infinity.
|
|
111
|
+
|
|
112
|
+
##### Throws:
|
|
113
|
+
|
|
114
|
+
- `Expected Point` if `!isPoint(A)`
|
|
115
|
+
- `Expected Point` if `!isPoint(B)`
|
|
116
|
+
|
|
117
|
+
### pointAddScalar (A, tweak[, compressed])
|
|
118
|
+
|
|
119
|
+
```haskell
|
|
120
|
+
pointAddScalar :: Buffer -> Buffer [-> Bool] -> Maybe Buffer
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Returns `null` if result is at infinity.
|
|
124
|
+
|
|
125
|
+
##### Throws:
|
|
126
|
+
|
|
127
|
+
- `Expected Point` if `!isPoint(A)`
|
|
128
|
+
- `Expected Tweak` if `tweak` is not in `[0...order - 1]`
|
|
129
|
+
|
|
130
|
+
### pointCompress (A, compressed)
|
|
131
|
+
|
|
132
|
+
```haskell
|
|
133
|
+
pointCompress :: Buffer -> Bool -> Buffer
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
##### Throws:
|
|
137
|
+
|
|
138
|
+
- `Expected Point` if `!isPoint(A)`
|
|
139
|
+
|
|
140
|
+
### pointFromScalar (d[, compressed])
|
|
141
|
+
|
|
142
|
+
```haskell
|
|
143
|
+
pointFromScalar :: Buffer [-> Bool] -> Maybe Buffer
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Returns `null` if result is at infinity.
|
|
147
|
+
|
|
148
|
+
##### Throws:
|
|
149
|
+
|
|
150
|
+
- `Expected Private` if `!isPrivate(d)`
|
|
151
|
+
|
|
152
|
+
### xOnlyPointFromScalar (d)
|
|
153
|
+
|
|
154
|
+
```haskell
|
|
155
|
+
xOnlyPointFromScalar :: Buffer -> Buffer
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Returns the xOnlyPubkey for a given private key
|
|
159
|
+
|
|
160
|
+
##### Throws:
|
|
161
|
+
|
|
162
|
+
- `Expected Private` if `!isPrivate(d)`
|
|
163
|
+
|
|
164
|
+
### xOnlyPointFromPoint (p)
|
|
165
|
+
|
|
166
|
+
```haskell
|
|
167
|
+
xOnlyPointFromPoint :: Buffer -> Buffer
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Returns the xOnlyPubkey for a given DER public key
|
|
171
|
+
|
|
172
|
+
##### Throws:
|
|
173
|
+
|
|
174
|
+
- `Expected Point` if `!isPoint(p)`
|
|
175
|
+
|
|
176
|
+
### pointMultiply (A, tweak[, compressed])
|
|
177
|
+
|
|
178
|
+
```haskell
|
|
179
|
+
pointMultiply :: Buffer -> Buffer [-> Bool] -> Maybe Buffer
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Returns `null` if result is at infinity.
|
|
183
|
+
|
|
184
|
+
##### Throws:
|
|
185
|
+
|
|
186
|
+
- `Expected Point` if `!isPoint(A)`
|
|
187
|
+
- `Expected Tweak` if `tweak` is not in `[0...order - 1]`
|
|
188
|
+
|
|
189
|
+
### privateAdd (d, tweak)
|
|
190
|
+
|
|
191
|
+
```haskell
|
|
192
|
+
privateAdd :: Buffer -> Buffer -> Maybe Buffer
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Returns `null` if result is equal to `0`.
|
|
196
|
+
|
|
197
|
+
##### Throws:
|
|
198
|
+
|
|
199
|
+
- `Expected Private` if `!isPrivate(d)`
|
|
200
|
+
- `Expected Tweak` if `tweak` is not in `[0...order - 1]`
|
|
201
|
+
|
|
202
|
+
### privateSub (d, tweak)
|
|
203
|
+
|
|
204
|
+
```haskell
|
|
205
|
+
privateSub :: Buffer -> Buffer -> Maybe Buffer
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Returns `null` if result is equal to `0`.
|
|
209
|
+
|
|
210
|
+
##### Throws:
|
|
211
|
+
|
|
212
|
+
- `Expected Private` if `!isPrivate(d)`
|
|
213
|
+
- `Expected Tweak` if `tweak` is not in `[0...order - 1]`
|
|
214
|
+
|
|
215
|
+
### privateNegate (d)
|
|
216
|
+
|
|
217
|
+
```haskell
|
|
218
|
+
privateNegate :: Buffer -> Buffer
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Returns the negation of d on the order n (`n - d`)
|
|
222
|
+
|
|
223
|
+
##### Throws:
|
|
224
|
+
|
|
225
|
+
- `Expected Private` if `!isPrivate(d)`
|
|
226
|
+
|
|
227
|
+
### xOnlyPointAddTweak (p, tweak)
|
|
228
|
+
|
|
229
|
+
```haskell
|
|
230
|
+
xOnlyPointAddTweak :: Buffer -> Buffer -> { parity: 1 | 0; xOnlyPubkey: Buffer; }
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Returns the tweaked xOnlyPubkey along with the parity bit (number type of 1|0)
|
|
234
|
+
|
|
235
|
+
##### Throws:
|
|
236
|
+
|
|
237
|
+
- `Expected Point` if `!isXOnlyPoint(p)`
|
|
238
|
+
- `Expected Tweak` if `!isXOnlyPoint(tweak)`
|
|
239
|
+
|
|
240
|
+
### xOnlyPointAddTweakCheck (p1, p2, tweak[, tweakParity])
|
|
241
|
+
|
|
242
|
+
```haskell
|
|
243
|
+
xOnlyPointAddTweakCheck :: Buffer -> Buffer -> Buffer [-> 1 | 0] -> Bool
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Checks the tweaked pubkey (p2) against the original pubkey (p1) and tweak.
|
|
247
|
+
This is slightly slower if you include tweakParity, tweakParity will make it
|
|
248
|
+
faster for aggregation later on.
|
|
249
|
+
|
|
250
|
+
##### Throws:
|
|
251
|
+
|
|
252
|
+
- `Expected Point` if `!isXOnlyPoint(p1)`
|
|
253
|
+
- `Expected Point` if `!isXOnlyPoint(p2)`
|
|
254
|
+
- `Expected Tweak` if `!isXOnlyPoint(tweak)`
|
|
255
|
+
- `Expected Parity` if `tweakParity is not 1 or 0`
|
|
256
|
+
|
|
257
|
+
### sign (h, d[, e])
|
|
258
|
+
|
|
259
|
+
```haskell
|
|
260
|
+
sign :: Buffer -> Buffer [-> Buffer] -> Buffer
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Returns normalized signatures, each of (r, s) values are guaranteed to less than `order / 2`.
|
|
264
|
+
Uses RFC6979.
|
|
265
|
+
Adds `e` as Added Entropy to the deterministic k generation.
|
|
266
|
+
|
|
267
|
+
##### Throws:
|
|
268
|
+
|
|
269
|
+
- `Expected Private` if `!isPrivate(d)`
|
|
270
|
+
- `Expected Scalar` if `h` is not 256-bit
|
|
271
|
+
- `Expected Extra Data (32 bytes)` if `e` is not 256-bit
|
|
272
|
+
|
|
273
|
+
### signRecoverable (h, d[, e])
|
|
274
|
+
|
|
275
|
+
```haskell
|
|
276
|
+
signRecoverable :: Buffer -> Buffer [-> Buffer] -> { recoveryId: 0 | 1 | 2 | 3; signature: Buffer; }
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
Returns normalized signatures and recovery Id, each of (r, s) values are guaranteed to less than `order / 2`.
|
|
280
|
+
Uses RFC6979.
|
|
281
|
+
Adds `e` as Added Entropy to the deterministic k generation.
|
|
282
|
+
|
|
283
|
+
##### Throws:
|
|
284
|
+
|
|
285
|
+
- `Expected Private` if `!isPrivate(d)`
|
|
286
|
+
- `Expected Scalar` if `h` is not 256-bit
|
|
287
|
+
- `Expected Extra Data (32 bytes)` if `e` is not 256-bit
|
|
288
|
+
|
|
289
|
+
### signSchnorr (h, d[, e])
|
|
290
|
+
|
|
291
|
+
```haskell
|
|
292
|
+
signSchnorr :: Buffer -> Buffer [-> Buffer] -> Buffer
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
Returns normalized schnorr signature.
|
|
296
|
+
Uses BIP340 nonce generation.
|
|
297
|
+
Adds `e` as Added Entropy.
|
|
298
|
+
|
|
299
|
+
##### Throws:
|
|
300
|
+
|
|
301
|
+
- `Expected Private` if `!isPrivate(d)`
|
|
302
|
+
- `Expected Scalar` if `h` is not 256-bit
|
|
303
|
+
- `Expected Extra Data (32 bytes)` if `e` is not 256-bit
|
|
304
|
+
|
|
305
|
+
### verify (h, Q, signature[, strict = false])
|
|
306
|
+
|
|
307
|
+
```haskell
|
|
308
|
+
verify :: Buffer -> Buffer -> Buffer [-> Bool] -> Bool
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
Returns `false` if any of (r, s) values are equal to `0`, or if the signature is rejected.
|
|
312
|
+
|
|
313
|
+
If `strict` is `true`, valid signatures with any of (r, s) values greater than `order / 2` are rejected.
|
|
314
|
+
|
|
315
|
+
##### Throws:
|
|
316
|
+
|
|
317
|
+
- `Expected Point` if `!isPoint(Q)`
|
|
318
|
+
- `Expected Signature` if `signature` has any (r, s) values not in range `[0...order - 1]`
|
|
319
|
+
- `Expected Scalar` if `h` is not 256-bit
|
|
320
|
+
|
|
321
|
+
### recover (h, signature, recoveryId[, compressed = false])
|
|
322
|
+
|
|
323
|
+
```haskell
|
|
324
|
+
verify :: Buffer -> Buffer -> Number [-> Bool] -> Maybe Buffer
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
Returns the ECDSA public key from a signature if it can be recovered, `null` otherwise.
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
##### Throws:
|
|
331
|
+
|
|
332
|
+
- `Expected Signature` if `signature` has any (r, s) values not in range `(0...order - 1]`
|
|
333
|
+
- `Bad Recovery Id` if `recid & 2 !== 0` and `signature` has any r value not in range `(0...P - N - 1]`
|
|
334
|
+
- `Expected Hash` if `h` is not 256-bit
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
### verifySchnorr (h, Q, signature)
|
|
338
|
+
|
|
339
|
+
```haskell
|
|
340
|
+
verifySchnorr :: Buffer -> Buffer -> Buffer -> Bool
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
Returns `false` if any of (r, s) values are equal to `0`, or if the signature is rejected.
|
|
344
|
+
|
|
345
|
+
##### Throws:
|
|
346
|
+
|
|
347
|
+
- `Expected Point` if `!isPoint(Q)`
|
|
348
|
+
- `Expected Signature` if `signature` has any (r, s) values not in range `[0...order - 1]`
|
|
349
|
+
- `Expected Scalar` if `h` is not 256-bit
|
|
350
|
+
|
|
351
|
+
## Credit
|
|
352
|
+
|
|
353
|
+
This library uses the native library [secp256k1](https://github.com/bitcoin-core/secp256k1) by the bitcoin-core developers through Rust crate [secp256k1-sys](https://crates.io/crates/secp256k1-sys), including derivatives of its tests and test vectors.
|
|
354
|
+
|
|
355
|
+
# LICENSE [MIT](LICENSE)
|