@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
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare const PRIVATE_KEY_SIZE = 32;
|
|
2
|
+
export declare const PUBLIC_KEY_COMPRESSED_SIZE = 33;
|
|
3
|
+
export declare const PUBLIC_KEY_UNCOMPRESSED_SIZE = 65;
|
|
4
|
+
export declare const X_ONLY_PUBLIC_KEY_SIZE = 32;
|
|
5
|
+
export declare const TWEAK_SIZE = 32;
|
|
6
|
+
export declare const HASH_SIZE = 32;
|
|
7
|
+
export declare const EXTRA_DATA_SIZE = 32;
|
|
8
|
+
export declare const SIGNATURE_SIZE = 64;
|
|
9
|
+
export declare function isZero(x: Uint8Array): boolean;
|
|
10
|
+
export declare function isPrivate(x: Uint8Array): boolean;
|
|
11
|
+
export declare function isPoint(p: Uint8Array): boolean;
|
|
12
|
+
export declare function isXOnlyPoint(p: Uint8Array): boolean;
|
|
13
|
+
export declare function isDERPoint(p: Uint8Array): boolean;
|
|
14
|
+
export declare function isPointCompressed(p: Uint8Array): boolean;
|
|
15
|
+
export declare function validateParity(p: 1 | 0): void;
|
|
16
|
+
export declare function validatePrivate(d: Uint8Array): void;
|
|
17
|
+
export declare function validatePoint(p: Uint8Array): void;
|
|
18
|
+
export declare function validateXOnlyPoint(p: Uint8Array): void;
|
|
19
|
+
export declare function validateTweak(tweak: Uint8Array): void;
|
|
20
|
+
export declare function validateHash(h: Uint8Array): void;
|
|
21
|
+
export declare function validateExtraData(e?: Uint8Array): void;
|
|
22
|
+
export declare function validateSignature(signature: Uint8Array): void;
|
|
23
|
+
export declare function validateSignatureCustom(validatorFn: () => boolean): void;
|
|
24
|
+
export declare function validateSignatureNonzeroRS(signature: Uint8Array): void;
|
|
25
|
+
export declare function validateSigrPMinusN(signature: Uint8Array): void;
|
package/lib/validate.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { ERROR_BAD_PRIVATE, ERROR_BAD_POINT, ERROR_BAD_TWEAK, throwError, ERROR_BAD_HASH, ERROR_BAD_EXTRA_DATA, ERROR_BAD_SIGNATURE, ERROR_BAD_PARITY, ERROR_BAD_RECOVERY_ID, } from "./validate_error.js";
|
|
2
|
+
export const PRIVATE_KEY_SIZE = 32;
|
|
3
|
+
export const PUBLIC_KEY_COMPRESSED_SIZE = 33;
|
|
4
|
+
export const PUBLIC_KEY_UNCOMPRESSED_SIZE = 65;
|
|
5
|
+
export const X_ONLY_PUBLIC_KEY_SIZE = 32;
|
|
6
|
+
export const TWEAK_SIZE = 32;
|
|
7
|
+
export const HASH_SIZE = 32;
|
|
8
|
+
export const EXTRA_DATA_SIZE = 32;
|
|
9
|
+
export const SIGNATURE_SIZE = 64;
|
|
10
|
+
const BN32_ZERO = new Uint8Array(32);
|
|
11
|
+
const BN32_N = new Uint8Array([
|
|
12
|
+
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
13
|
+
254, 186, 174, 220, 230, 175, 72, 160, 59, 191, 210, 94, 140, 208, 54, 65, 65,
|
|
14
|
+
]);
|
|
15
|
+
// Difference between field and order
|
|
16
|
+
const BN32_P_MINUS_N = new Uint8Array([
|
|
17
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 69, 81, 35, 25, 80, 183, 95,
|
|
18
|
+
196, 64, 45, 161, 114, 47, 201, 186, 238,
|
|
19
|
+
]);
|
|
20
|
+
function isUint8Array(value) {
|
|
21
|
+
return value instanceof Uint8Array;
|
|
22
|
+
}
|
|
23
|
+
function cmpBN32(data1, data2) {
|
|
24
|
+
for (let i = 0; i < 32; ++i) {
|
|
25
|
+
if (data1[i] !== data2[i]) {
|
|
26
|
+
return data1[i] < data2[i] ? -1 : 1;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
export function isZero(x) {
|
|
32
|
+
return cmpBN32(x, BN32_ZERO) === 0;
|
|
33
|
+
}
|
|
34
|
+
export function isPrivate(x) {
|
|
35
|
+
return (isUint8Array(x) &&
|
|
36
|
+
x.length === PRIVATE_KEY_SIZE &&
|
|
37
|
+
cmpBN32(x, BN32_ZERO) > 0 &&
|
|
38
|
+
cmpBN32(x, BN32_N) < 0);
|
|
39
|
+
}
|
|
40
|
+
export function isPoint(p) {
|
|
41
|
+
return (isUint8Array(p) &&
|
|
42
|
+
(p.length === PUBLIC_KEY_COMPRESSED_SIZE ||
|
|
43
|
+
p.length === PUBLIC_KEY_UNCOMPRESSED_SIZE ||
|
|
44
|
+
p.length === X_ONLY_PUBLIC_KEY_SIZE));
|
|
45
|
+
}
|
|
46
|
+
export function isXOnlyPoint(p) {
|
|
47
|
+
return isUint8Array(p) && p.length === X_ONLY_PUBLIC_KEY_SIZE;
|
|
48
|
+
}
|
|
49
|
+
export function isDERPoint(p) {
|
|
50
|
+
return (isUint8Array(p) &&
|
|
51
|
+
(p.length === PUBLIC_KEY_COMPRESSED_SIZE ||
|
|
52
|
+
p.length === PUBLIC_KEY_UNCOMPRESSED_SIZE));
|
|
53
|
+
}
|
|
54
|
+
export function isPointCompressed(p) {
|
|
55
|
+
return isUint8Array(p) && p.length === PUBLIC_KEY_COMPRESSED_SIZE;
|
|
56
|
+
}
|
|
57
|
+
function isTweak(tweak) {
|
|
58
|
+
return (isUint8Array(tweak) &&
|
|
59
|
+
tweak.length === TWEAK_SIZE &&
|
|
60
|
+
cmpBN32(tweak, BN32_N) < 0);
|
|
61
|
+
}
|
|
62
|
+
function isHash(h) {
|
|
63
|
+
return isUint8Array(h) && h.length === HASH_SIZE;
|
|
64
|
+
}
|
|
65
|
+
function isExtraData(e) {
|
|
66
|
+
return e === undefined || (isUint8Array(e) && e.length === EXTRA_DATA_SIZE);
|
|
67
|
+
}
|
|
68
|
+
function isSignature(signature) {
|
|
69
|
+
return (isUint8Array(signature) &&
|
|
70
|
+
signature.length === 64 &&
|
|
71
|
+
cmpBN32(signature.subarray(0, 32), BN32_N) < 0 &&
|
|
72
|
+
cmpBN32(signature.subarray(32, 64), BN32_N) < 0);
|
|
73
|
+
}
|
|
74
|
+
function isSigrLessThanPMinusN(signature) {
|
|
75
|
+
return (isUint8Array(signature) &&
|
|
76
|
+
signature.length === 64 &&
|
|
77
|
+
cmpBN32(signature.subarray(0, 32), BN32_P_MINUS_N) < 0);
|
|
78
|
+
}
|
|
79
|
+
export function validateParity(p) {
|
|
80
|
+
if (p !== 0 && p !== 1)
|
|
81
|
+
throwError(ERROR_BAD_PARITY);
|
|
82
|
+
}
|
|
83
|
+
export function validatePrivate(d) {
|
|
84
|
+
if (!isPrivate(d))
|
|
85
|
+
throwError(ERROR_BAD_PRIVATE);
|
|
86
|
+
}
|
|
87
|
+
export function validatePoint(p) {
|
|
88
|
+
if (!isPoint(p))
|
|
89
|
+
throwError(ERROR_BAD_POINT);
|
|
90
|
+
}
|
|
91
|
+
export function validateXOnlyPoint(p) {
|
|
92
|
+
if (!isXOnlyPoint(p))
|
|
93
|
+
throwError(ERROR_BAD_POINT);
|
|
94
|
+
}
|
|
95
|
+
export function validateTweak(tweak) {
|
|
96
|
+
if (!isTweak(tweak))
|
|
97
|
+
throwError(ERROR_BAD_TWEAK);
|
|
98
|
+
}
|
|
99
|
+
export function validateHash(h) {
|
|
100
|
+
if (!isHash(h))
|
|
101
|
+
throwError(ERROR_BAD_HASH);
|
|
102
|
+
}
|
|
103
|
+
export function validateExtraData(e) {
|
|
104
|
+
if (!isExtraData(e))
|
|
105
|
+
throwError(ERROR_BAD_EXTRA_DATA);
|
|
106
|
+
}
|
|
107
|
+
export function validateSignature(signature) {
|
|
108
|
+
if (!isSignature(signature))
|
|
109
|
+
throwError(ERROR_BAD_SIGNATURE);
|
|
110
|
+
}
|
|
111
|
+
export function validateSignatureCustom(validatorFn) {
|
|
112
|
+
if (!validatorFn())
|
|
113
|
+
throwError(ERROR_BAD_SIGNATURE);
|
|
114
|
+
}
|
|
115
|
+
export function validateSignatureNonzeroRS(signature) {
|
|
116
|
+
if (isZero(signature.subarray(0, 32)))
|
|
117
|
+
throwError(ERROR_BAD_SIGNATURE);
|
|
118
|
+
if (isZero(signature.subarray(32, 64)))
|
|
119
|
+
throwError(ERROR_BAD_SIGNATURE);
|
|
120
|
+
}
|
|
121
|
+
export function validateSigrPMinusN(signature) {
|
|
122
|
+
if (!isSigrLessThanPMinusN(signature))
|
|
123
|
+
throwError(ERROR_BAD_RECOVERY_ID);
|
|
124
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const ERROR_BAD_PRIVATE = 0;
|
|
2
|
+
export declare const ERROR_BAD_POINT = 1;
|
|
3
|
+
export declare const ERROR_BAD_TWEAK = 2;
|
|
4
|
+
export declare const ERROR_BAD_HASH = 3;
|
|
5
|
+
export declare const ERROR_BAD_SIGNATURE = 4;
|
|
6
|
+
export declare const ERROR_BAD_EXTRA_DATA = 5;
|
|
7
|
+
export declare const ERROR_BAD_PARITY = 6;
|
|
8
|
+
export declare const ERROR_BAD_RECOVERY_ID = 7;
|
|
9
|
+
export declare function throwError(errcode: number): never;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const ERROR_BAD_PRIVATE = 0;
|
|
2
|
+
export const ERROR_BAD_POINT = 1;
|
|
3
|
+
export const ERROR_BAD_TWEAK = 2;
|
|
4
|
+
export const ERROR_BAD_HASH = 3;
|
|
5
|
+
export const ERROR_BAD_SIGNATURE = 4;
|
|
6
|
+
export const ERROR_BAD_EXTRA_DATA = 5;
|
|
7
|
+
export const ERROR_BAD_PARITY = 6;
|
|
8
|
+
export const ERROR_BAD_RECOVERY_ID = 7;
|
|
9
|
+
const ERRORS_MESSAGES = {
|
|
10
|
+
[ERROR_BAD_PRIVATE.toString()]: "Expected Private",
|
|
11
|
+
[ERROR_BAD_POINT.toString()]: "Expected Point",
|
|
12
|
+
[ERROR_BAD_TWEAK.toString()]: "Expected Tweak",
|
|
13
|
+
[ERROR_BAD_HASH.toString()]: "Expected Hash",
|
|
14
|
+
[ERROR_BAD_SIGNATURE.toString()]: "Expected Signature",
|
|
15
|
+
[ERROR_BAD_EXTRA_DATA.toString()]: "Expected Extra Data (32 bytes)",
|
|
16
|
+
[ERROR_BAD_PARITY.toString()]: "Expected Parity (1 | 0)",
|
|
17
|
+
[ERROR_BAD_RECOVERY_ID.toString()]: "Bad Recovery Id",
|
|
18
|
+
};
|
|
19
|
+
export function throwError(errcode) {
|
|
20
|
+
const message = ERRORS_MESSAGES[errcode.toString()] || `Unknow error code: ${errcode}`;
|
|
21
|
+
throw new TypeError(message);
|
|
22
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
interface WebAssemblyMemory {
|
|
2
|
+
buffer: Uint8Array;
|
|
3
|
+
}
|
|
4
|
+
interface WebAssemblyGlobal {
|
|
5
|
+
value: number;
|
|
6
|
+
}
|
|
7
|
+
declare type RecoveryIdType = 0 | 1 | 2 | 3;
|
|
8
|
+
interface Secp256k1WASM {
|
|
9
|
+
memory: WebAssemblyMemory;
|
|
10
|
+
PRIVATE_INPUT: WebAssemblyGlobal;
|
|
11
|
+
PUBLIC_KEY_INPUT: WebAssemblyGlobal;
|
|
12
|
+
PUBLIC_KEY_INPUT2: WebAssemblyGlobal;
|
|
13
|
+
X_ONLY_PUBLIC_KEY_INPUT: WebAssemblyGlobal;
|
|
14
|
+
X_ONLY_PUBLIC_KEY_INPUT2: WebAssemblyGlobal;
|
|
15
|
+
TWEAK_INPUT: WebAssemblyGlobal;
|
|
16
|
+
HASH_INPUT: WebAssemblyGlobal;
|
|
17
|
+
EXTRA_DATA_INPUT: WebAssemblyGlobal;
|
|
18
|
+
SIGNATURE_INPUT: WebAssemblyGlobal;
|
|
19
|
+
initializeContext: () => void;
|
|
20
|
+
isPoint: (p: number) => number;
|
|
21
|
+
pointAdd: (pA: number, pB: number, outputlen: number) => number;
|
|
22
|
+
pointAddScalar: (p: number, outputlen: number) => number;
|
|
23
|
+
pointCompress: (p: number, outputlen: number) => number;
|
|
24
|
+
pointFromScalar: (outputlen: number) => number;
|
|
25
|
+
xOnlyPointFromScalar: () => number;
|
|
26
|
+
xOnlyPointFromPoint: (inputLen: number) => number;
|
|
27
|
+
xOnlyPointAddTweak: () => 1 | 0 | -1;
|
|
28
|
+
xOnlyPointAddTweakCheck: (parity: number) => number;
|
|
29
|
+
pointMultiply: (p: number, outputlen: number) => number;
|
|
30
|
+
privateAdd: () => number;
|
|
31
|
+
privateSub: () => number;
|
|
32
|
+
privateNegate: () => void;
|
|
33
|
+
sign: (e: number) => void;
|
|
34
|
+
signRecoverable: (e: number) => 0 | 1 | 2 | 3;
|
|
35
|
+
signSchnorr: (e: number) => void;
|
|
36
|
+
verify: (Q: number, strict: number) => number;
|
|
37
|
+
verifySchnorr: () => number;
|
|
38
|
+
recover: (outputlen: number, recoveryId: RecoveryIdType) => number;
|
|
39
|
+
}
|
|
40
|
+
declare const _default: Secp256k1WASM;
|
|
41
|
+
export default _default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function path(wasmFilename: string): string;
|
package/lib/wasm_path.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bitcoin-js/tiny-secp256k1-asmjs",
|
|
3
|
+
"version": "2.2.2",
|
|
4
|
+
"description": "A tiny secp256k1 JS",
|
|
5
|
+
"homepage": "https://github.com/bitcoinjs/tiny-secp256k1#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/bitcoinjs/tiny-secp256k1/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/bitcoinjs/tiny-secp256k1.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./lib/cjs/index.cjs",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"node": {
|
|
19
|
+
"module": "./lib/index.js",
|
|
20
|
+
"require": "./lib/cjs/index.cjs",
|
|
21
|
+
"import": "./lib/index.js"
|
|
22
|
+
},
|
|
23
|
+
"browser": "./lib/index.js",
|
|
24
|
+
"default": "./lib/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"browser": {
|
|
28
|
+
"./lib/rand.js": "./lib/rand.browser.js"
|
|
29
|
+
},
|
|
30
|
+
"types": "./lib/index.d.ts",
|
|
31
|
+
"files": [
|
|
32
|
+
"lib"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"uint8array-tools": "0.0.7"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@babel/eslint-parser": "^7.22.5",
|
|
39
|
+
"@babel/plugin-syntax-import-assertions": "^7.22.5",
|
|
40
|
+
"@types/node": "^14.14.35",
|
|
41
|
+
"@typescript-eslint/eslint-plugin": "^4.18.0",
|
|
42
|
+
"@typescript-eslint/parser": "^4.18.0",
|
|
43
|
+
"browser-run": "^10.1.0",
|
|
44
|
+
"buffer": "^6.0.3",
|
|
45
|
+
"eslint": "^7.22.0",
|
|
46
|
+
"eslint-config-prettier": "^8.1.0",
|
|
47
|
+
"eslint-plugin-prettier": "^3.3.1",
|
|
48
|
+
"nyc": "^15.1.0",
|
|
49
|
+
"path-browserify": "^1.0.1",
|
|
50
|
+
"prettier": "^2.2.1",
|
|
51
|
+
"process": "^0.11.10",
|
|
52
|
+
"sort-package-json": "^1.49.0",
|
|
53
|
+
"stream-browserify": "^3.0.0",
|
|
54
|
+
"tap-summary": "^4.0.0",
|
|
55
|
+
"tape": "^5.2.2",
|
|
56
|
+
"typescript": "^4.2.3",
|
|
57
|
+
"webpack": "^5.24.4",
|
|
58
|
+
"webpack-cli": "^4.5.0"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=14.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|