@pezkuwi/wasm-crypto 7.5.9 → 7.5.11
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 +201 -0
- package/bundle-pezkuwi-wasm-crypto.js +777 -0
- package/bundle.d.ts +37 -0
- package/bundle.js +165 -0
- package/cjs/bundle.d.ts +37 -0
- package/cjs/bundle.js +171 -0
- package/cjs/index.js +5 -0
- package/cjs/init.js +21 -0
- package/cjs/initNone.js +20 -0
- package/cjs/initOnlyAsm.js +20 -0
- package/cjs/initOnlyWasm.js +20 -0
- package/cjs/initWasmAsm.js +20 -0
- package/cjs/package.json +3 -0
- package/cjs/packageDetect.js +10 -0
- package/cjs/packageInfo.js +4 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/{src/init.ts → init.d.ts} +2 -11
- package/init.js +17 -0
- package/initNone.d.ts +10 -0
- package/{src/initNone.ts → initNone.js} +4 -10
- package/initOnlyAsm.d.ts +10 -0
- package/{src/initOnlyAsm.ts → initOnlyAsm.js} +4 -10
- package/initOnlyWasm.d.ts +10 -0
- package/{src/initOnlyWasm.ts → initOnlyWasm.js} +4 -10
- package/initWasmAsm.d.ts +10 -0
- package/{src/initWasmAsm.ts → initWasmAsm.js} +4 -10
- package/package.json +162 -16
- package/packageDetect.d.ts +1 -0
- package/{src/packageDetect.ts → packageDetect.js} +0 -8
- package/packageInfo.d.ts +6 -0
- package/packageInfo.js +1 -0
- package/Cargo.toml +0 -50
- package/Xargo.toml +0 -2
- package/build/bundle.d.ts +0 -37
- package/src/bundle.ts +0 -247
- package/src/index.ts +0 -6
- package/src/lib.rs +0 -24
- package/src/mod.ts +0 -4
- package/src/packageInfo.ts +0 -6
- package/src/rs/.editorconfig +0 -10
- package/src/rs/bip39.rs +0 -139
- package/src/rs/ed25519.rs +0 -142
- package/src/rs/hashing.rs +0 -322
- package/src/rs/secp256k1.rs +0 -150
- package/src/rs/sr25519.rs +0 -331
- package/src/rs/vrf.rs +0 -144
- package/test/all/bip39.js +0 -86
- package/test/all/ed25519.js +0 -84
- package/test/all/hashing.js +0 -138
- package/test/all/index.js +0 -126
- package/test/all/secp256k1.js +0 -105
- package/test/all/sr25519.js +0 -211
- package/test/all/vrf.js +0 -74
- package/test/asm.js +0 -10
- package/test/deno.ts +0 -37
- package/test/jest.spec.ts +0 -24
- package/test/loader-build.js +0 -39
- package/test/wasm.js +0 -8
- package/tsconfig.build.json +0 -19
- package/tsconfig.spec.json +0 -16
- /package/{build → cjs}/index.d.ts +0 -0
- /package/{build → cjs}/init.d.ts +0 -0
- /package/{build → cjs}/initNone.d.ts +0 -0
- /package/{build → cjs}/initOnlyAsm.d.ts +0 -0
- /package/{build → cjs}/initOnlyWasm.d.ts +0 -0
- /package/{build → cjs}/initWasmAsm.d.ts +0 -0
- /package/{build → cjs}/packageDetect.d.ts +0 -0
- /package/{build → cjs}/packageInfo.d.ts +0 -0
package/test/all/bip39.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2026 @pezkuwi/wasm-crypto authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
/* global it */
|
|
5
|
-
|
|
6
|
-
import { assert, u8aToHex } from '@pezkuwi/util';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @param {*} wasm
|
|
10
|
-
*/
|
|
11
|
-
export function bip39Generate (wasm) {
|
|
12
|
-
it('generates a bip39 phrase', () => {
|
|
13
|
-
const res = wasm.bip39Generate(21);
|
|
14
|
-
|
|
15
|
-
// console.log('\tPHR', res);
|
|
16
|
-
|
|
17
|
-
assert(res.split(' ').length === 21, 'ERROR: Invalid bip39 Phase length');
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @param {*} wasm
|
|
23
|
-
*/
|
|
24
|
-
export function bip39GenerateSubsequent (wasm) {
|
|
25
|
-
it('generates different subsequent mnemonics', () => {
|
|
26
|
-
const val1 = wasm.bip39Generate(24);
|
|
27
|
-
const val2 = wasm.bip39Generate(24);
|
|
28
|
-
|
|
29
|
-
// console.log('\tVL1', val1);
|
|
30
|
-
// console.log('\tVL2', val2);
|
|
31
|
-
|
|
32
|
-
assert(val1 !== val2, 'ERROR: Subsequent mnemonics are the same');
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @param {*} wasm
|
|
38
|
-
*/
|
|
39
|
-
export function bip39Validate (wasm) {
|
|
40
|
-
it('validates a created mnemonic', () => {
|
|
41
|
-
const res = wasm.bip39Validate(wasm.bip39Generate(12));
|
|
42
|
-
|
|
43
|
-
// console.log('\tVAL', res);
|
|
44
|
-
|
|
45
|
-
assert(res, 'ERROR: Invalid bip39 validation');
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @param {*} wasm
|
|
51
|
-
*/
|
|
52
|
-
export function bip39ToEntropy (wasm) {
|
|
53
|
-
it('creates correct entropy for a known mnemonic', () => {
|
|
54
|
-
const res = u8aToHex(wasm.bip39ToEntropy('legal winner thank year wave sausage worth useful legal winner thank yellow'));
|
|
55
|
-
|
|
56
|
-
// console.log('\tENT', res);
|
|
57
|
-
|
|
58
|
-
assert(res === '0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f', 'ERROR: Invalid bip39 entropy');
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @param {*} wasm
|
|
64
|
-
*/
|
|
65
|
-
export function bip39ToMiniSecret (wasm) {
|
|
66
|
-
it('creates the correct minisecret from a mnemonic', () => {
|
|
67
|
-
const res = u8aToHex(wasm.bip39ToMiniSecret('legal winner thank year wave sausage worth useful legal winner thank yellow', 'Bizinikiwi'));
|
|
68
|
-
|
|
69
|
-
// console.log('\tMIN', res);
|
|
70
|
-
|
|
71
|
-
assert(res === '0x4313249608fe8ac10fd5886c92c4579007272cb77c21551ee5b8d60b78041685', 'ERROR: Invalid bip39 mini secret');
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* @param {*} wasm
|
|
77
|
-
*/
|
|
78
|
-
export function bip39ToSeed (wasm) {
|
|
79
|
-
it('creates the correct seed for a mnemonic', () => {
|
|
80
|
-
const res = u8aToHex(wasm.bip39ToSeed('seed sock milk update focus rotate barely fade car face mechanic mercy', ''));
|
|
81
|
-
|
|
82
|
-
// console.log('\tSEE', res);
|
|
83
|
-
|
|
84
|
-
assert(res === '0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027', 'ERROR: Invalid bip39 mini secret');
|
|
85
|
-
});
|
|
86
|
-
}
|
package/test/all/ed25519.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2026 @pezkuwi/wasm-crypto authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
/* global it */
|
|
5
|
-
|
|
6
|
-
import crypto from 'crypto';
|
|
7
|
-
|
|
8
|
-
import { assert, hexToU8a, stringToU8a, u8aToHex } from '@pezkuwi/util';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @internal
|
|
12
|
-
* @param {*} pair
|
|
13
|
-
*/
|
|
14
|
-
function extractKeys (pair) {
|
|
15
|
-
return [pair, pair.slice(32), pair.slice(0, 32)];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @internal
|
|
20
|
-
* @param {*} wasm
|
|
21
|
-
*/
|
|
22
|
-
function randomPair (wasm) {
|
|
23
|
-
return extractKeys(wasm.ed25519KeypairFromSeed(crypto.randomBytes(32)));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @param {*} wasm
|
|
28
|
-
*/
|
|
29
|
-
export function ed25519PairFromSeed (wasm) {
|
|
30
|
-
it('creates a known pair from a known seed', () => {
|
|
31
|
-
const [pair, pk, sk] = extractKeys(wasm.ed25519KeypairFromSeed(stringToU8a('12345678901234567890123456789012')));
|
|
32
|
-
|
|
33
|
-
// console.log('\tSEC', u8aToHex(sk));
|
|
34
|
-
// console.log('\tPUB', u8aToHex(pk));
|
|
35
|
-
|
|
36
|
-
assert(u8aToHex(sk) === '0x3132333435363738393031323334353637383930313233343536373839303132', 'Secretkey does not match');
|
|
37
|
-
assert(u8aToHex(pk) === '0x2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee', 'Public key does not match');
|
|
38
|
-
assert(u8aToHex(pair) === '0x31323334353637383930313233343536373839303132333435363738393031322f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee', 'ERROR: pairFromSeed() does not match');
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @param {*} wasm
|
|
44
|
-
*/
|
|
45
|
-
export function ed25519SignAndVerify (wasm) {
|
|
46
|
-
it('signs and verifies', () => {
|
|
47
|
-
const [, pk, sk] = randomPair(wasm);
|
|
48
|
-
const signature = wasm.ed25519Sign(pk, sk, stringToU8a('this is a message'));
|
|
49
|
-
const isValid = wasm.ed25519Verify(signature, stringToU8a('this is a message'), pk);
|
|
50
|
-
|
|
51
|
-
// console.log('\tSIG', u8aToHex(signature));
|
|
52
|
-
// console.log('\tRES', isValid);
|
|
53
|
-
|
|
54
|
-
assert(isValid, 'ERROR: Unable to verify signature');
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @param {*} wasm
|
|
60
|
-
*/
|
|
61
|
-
export function ed25519VerifyExisting (wasm) {
|
|
62
|
-
it('verifies a known signature', () => {
|
|
63
|
-
const isValid = wasm.ed25519Verify(hexToU8a('0x90588f3f512496f2dd40571d162e8182860081c74e2085316e7c4396918f07da412ee029978e4dd714057fe973bd9e7d645148bf7b66680d67c93227cde95202'), stringToU8a('this is a message'), hexToU8a('0x2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee'));
|
|
64
|
-
|
|
65
|
-
// console.log('\tRES', isValid);
|
|
66
|
-
|
|
67
|
-
assert(isValid, 'ERROR: Unable to verify signature');
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* @param {*} wasm
|
|
73
|
-
*/
|
|
74
|
-
export function ed25519Benchmark (wasm) {
|
|
75
|
-
it('runs a verification benchamark', () => {
|
|
76
|
-
const MESSAGE = stringToU8a('this is a message');
|
|
77
|
-
|
|
78
|
-
for (let i = 0; i < 256; i++) {
|
|
79
|
-
const [, pk, sk] = randomPair(wasm);
|
|
80
|
-
|
|
81
|
-
assert(wasm.ed25519Verify(wasm.ed25519Sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature');
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}
|
package/test/all/hashing.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2026 @pezkuwi/wasm-crypto authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
/* global it */
|
|
5
|
-
|
|
6
|
-
import { assert, stringToU8a, u8aToHex } from '@pezkuwi/util';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @param {*} wasm
|
|
10
|
-
*/
|
|
11
|
-
export function blake2bHash (wasm) {
|
|
12
|
-
it('creates a known blake2b hash', () => {
|
|
13
|
-
const hash = u8aToHex(wasm.blake2b(stringToU8a('abc'), new Uint8Array(), 64));
|
|
14
|
-
|
|
15
|
-
// console.log('\tRES', hash);
|
|
16
|
-
|
|
17
|
-
assert(hash === '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923', 'ERROR: blake2bHash does not match');
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @param {*} wasm
|
|
23
|
-
*/
|
|
24
|
-
export function hmacSha256 (wasm) {
|
|
25
|
-
it('creates a known hmacSha256', () => {
|
|
26
|
-
const hash = u8aToHex(wasm.hmacSha256(stringToU8a('secret'), stringToU8a('some message')));
|
|
27
|
-
|
|
28
|
-
// console.log('\tRES', hash);
|
|
29
|
-
|
|
30
|
-
assert(hash === '0xf28a70b41263840e5c059a0a733336e0957efba87902aa8cca11441d4b0c96d7', 'ERROR: hmacSha256 does not match');
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @param {*} wasm
|
|
36
|
-
*/
|
|
37
|
-
export function hmacSha512 (wasm) {
|
|
38
|
-
it('creates a known hmacSha512', () => {
|
|
39
|
-
const hash = u8aToHex(wasm.hmacSha512(stringToU8a('secret'), stringToU8a('some message')));
|
|
40
|
-
|
|
41
|
-
// console.log('\tRES', hash);
|
|
42
|
-
|
|
43
|
-
assert(hash === '0x295832e97ed77be75a9fa98029497e4a722c4b9a2f21b39d34f1befa931a39ec520fd24711d6f5c03501384ea66b83066a01a82c57a0460f8cd1f471fcce5841', 'ERROR: hmacSha512 does not match');
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* @param {*} wasm
|
|
49
|
-
*/
|
|
50
|
-
export function keccak256 (wasm) {
|
|
51
|
-
it('creates a known keccak256 hash', () => {
|
|
52
|
-
const hash = u8aToHex(wasm.keccak256(stringToU8a('test value')));
|
|
53
|
-
|
|
54
|
-
// console.log('\tRES', hash);
|
|
55
|
-
|
|
56
|
-
assert(hash === '0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e', 'ERROR: keccak256 does not match');
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* @param {*} wasm
|
|
62
|
-
*/
|
|
63
|
-
export function keccak512 (wasm) {
|
|
64
|
-
it('creates a known keccak512 hash', () => {
|
|
65
|
-
const hash = u8aToHex(wasm.keccak512(stringToU8a('test')));
|
|
66
|
-
|
|
67
|
-
// console.log('\tRES', hash);
|
|
68
|
-
|
|
69
|
-
assert(hash === '0x1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e', 'ERROR: keccak512 does not match');
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* @param {*} wasm
|
|
75
|
-
*/
|
|
76
|
-
export function pbkdf2Hash (wasm) {
|
|
77
|
-
it('creates a known pbkdf2 hash', () => {
|
|
78
|
-
const hash = u8aToHex(wasm.pbkdf2(stringToU8a('hello world'), stringToU8a('this is a salt'), 2048));
|
|
79
|
-
|
|
80
|
-
// console.log('\tRES', hash);
|
|
81
|
-
|
|
82
|
-
assert(hash === '0x5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270', 'ERROR: pbkdf2Hash does not match');
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @param {*} wasm
|
|
88
|
-
*/
|
|
89
|
-
export function scryptHash (wasm) {
|
|
90
|
-
it('creates a known scrypt hash', () => {
|
|
91
|
-
const hash = u8aToHex(wasm.scrypt(stringToU8a('password'), stringToU8a('salt'), 14, 8, 1));
|
|
92
|
-
|
|
93
|
-
// console.log('\tRES', hash);
|
|
94
|
-
|
|
95
|
-
assert(hash === '0x745731af4484f323968969eda289aeee005b5903ac561e64a5aca121797bf7734ef9fd58422e2e22183bcacba9ec87ba0c83b7a2e788f03ce0da06463433cda6', 'ERROR: scryptHash does not match');
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* @param {*} wasm
|
|
101
|
-
*/
|
|
102
|
-
export function sha256Hash (wasm) {
|
|
103
|
-
it('creates a known sha256 hash', () => {
|
|
104
|
-
const hash = u8aToHex(wasm.sha256(stringToU8a('hello world')));
|
|
105
|
-
|
|
106
|
-
// console.log('\tRES', hash);
|
|
107
|
-
|
|
108
|
-
assert(hash === '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9', 'ERROR: sha256Hash does not match');
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* @param {*} wasm
|
|
114
|
-
*/
|
|
115
|
-
export function sha512Hash (wasm) {
|
|
116
|
-
it('creates a known sha512 hash', () => {
|
|
117
|
-
const hash = u8aToHex(wasm.sha512(stringToU8a('hello world')));
|
|
118
|
-
|
|
119
|
-
// console.log('\tRES', hash);
|
|
120
|
-
|
|
121
|
-
assert(hash === '0x309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f', 'ERROR: sha512Hash does not match');
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* @param {*} wasm
|
|
127
|
-
*/
|
|
128
|
-
export function twoxHash (wasm) {
|
|
129
|
-
it('creates a known twox hash', () => {
|
|
130
|
-
const hash64 = u8aToHex(wasm.twox(stringToU8a('abc'), 1));
|
|
131
|
-
const hash256 = u8aToHex(wasm.twox(stringToU8a('abc'), 4));
|
|
132
|
-
|
|
133
|
-
// console.log('\t64 ', hash64);
|
|
134
|
-
// console.log('\t256', hash256);
|
|
135
|
-
|
|
136
|
-
assert(hash64 === '0x990977adf52cbc44' && hash256 === '0x990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b', 'ERROR: twoxHash does not match');
|
|
137
|
-
});
|
|
138
|
-
}
|
package/test/all/index.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2026 @pezkuwi/wasm-crypto authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
/* global describe */
|
|
5
|
-
|
|
6
|
-
import { assert } from '@pezkuwi/util';
|
|
7
|
-
|
|
8
|
-
import * as bip39 from './bip39.js';
|
|
9
|
-
import * as ed25519 from './ed25519.js';
|
|
10
|
-
import * as hashing from './hashing.js';
|
|
11
|
-
import * as secp256k1 from './secp256k1.js';
|
|
12
|
-
import * as sr25519 from './sr25519.js';
|
|
13
|
-
import * as vrf from './vrf.js';
|
|
14
|
-
|
|
15
|
-
export const tests = {
|
|
16
|
-
// We place secp256k1 first, this allows the interaction with it in the
|
|
17
|
-
// hashing (specifically scrypt) test not be an issue (ASM.js only)
|
|
18
|
-
// https://github.com/pezkuwichain/pezkuwi-wasm/issues/307
|
|
19
|
-
secp256k1,
|
|
20
|
-
// eslint-disable-next-line sort-keys
|
|
21
|
-
bip39,
|
|
22
|
-
ed25519,
|
|
23
|
-
hashing,
|
|
24
|
-
sr25519,
|
|
25
|
-
vrf
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @param {string} name
|
|
30
|
-
* @param {*} wasm
|
|
31
|
-
*/
|
|
32
|
-
export async function initRun (name, wasm) {
|
|
33
|
-
const result = await wasm.waitReady();
|
|
34
|
-
|
|
35
|
-
console.log(`*** waitReady()=${result} for ${wasm.bridge.type}`);
|
|
36
|
-
|
|
37
|
-
assert(name.toLowerCase() === wasm.bridge.type, `Incorrect environment launched, expected ${name.toLowerCase()}, found ${wasm.bridge.type}`);
|
|
38
|
-
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @param {string} name
|
|
44
|
-
* @param {*} wasm
|
|
45
|
-
*/
|
|
46
|
-
export function runAll (name, wasm) {
|
|
47
|
-
/** @type {string[]} */
|
|
48
|
-
const failed = [];
|
|
49
|
-
let count = 0;
|
|
50
|
-
|
|
51
|
-
Object
|
|
52
|
-
.entries(tests)
|
|
53
|
-
.forEach(([describeName, tests]) => {
|
|
54
|
-
describe(describeName, () => {
|
|
55
|
-
Object
|
|
56
|
-
.entries(tests)
|
|
57
|
-
.forEach(([name, test]) => {
|
|
58
|
-
const timerId = `\t${name}`;
|
|
59
|
-
|
|
60
|
-
count++;
|
|
61
|
-
|
|
62
|
-
try {
|
|
63
|
-
console.time(timerId);
|
|
64
|
-
console.log();
|
|
65
|
-
|
|
66
|
-
test(wasm);
|
|
67
|
-
|
|
68
|
-
console.timeEnd(timerId);
|
|
69
|
-
} catch (error) {
|
|
70
|
-
console.error();
|
|
71
|
-
console.error(error);
|
|
72
|
-
|
|
73
|
-
failed.push(name);
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
if (failed.length) {
|
|
80
|
-
throw new Error(`\n*** ${name}: FAILED: ${failed.length} of ${count}: ${failed.join(', ')}`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* @param {string} type
|
|
86
|
-
* @param {*} wasm
|
|
87
|
-
*/
|
|
88
|
-
export function runUnassisted (type, wasm) {
|
|
89
|
-
console.log(`\n*** ${type}: Running tests`);
|
|
90
|
-
|
|
91
|
-
// For these we are pass-through describe and it handlers
|
|
92
|
-
// @ts-expect-error We are hacking this, so expect TS to be unhappy...
|
|
93
|
-
globalThis.describe = (name, fn) => {
|
|
94
|
-
console.log('\n', name);
|
|
95
|
-
|
|
96
|
-
// We expect this to be handled top-level, in the test itself
|
|
97
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
98
|
-
fn();
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
// @ts-expect-error We are hacking this, so expect TS to be unhappy...
|
|
102
|
-
globalThis.it = (name, fn) => {
|
|
103
|
-
console.log(`\t${name}`);
|
|
104
|
-
|
|
105
|
-
// We expect this to be handled top-level, in the test itself
|
|
106
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
107
|
-
fn();
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
console.time(type);
|
|
111
|
-
|
|
112
|
-
initRun(type, wasm)
|
|
113
|
-
.then(() => {
|
|
114
|
-
runAll(type, wasm);
|
|
115
|
-
console.log(`\n*** ${type}: All passed`);
|
|
116
|
-
console.timeEnd(type);
|
|
117
|
-
console.log();
|
|
118
|
-
process.exit(0);
|
|
119
|
-
})
|
|
120
|
-
.catch((error) => {
|
|
121
|
-
console.error(`\n*** ${type}: FAILED:`, error.message, '\n');
|
|
122
|
-
console.timeEnd(type);
|
|
123
|
-
console.log();
|
|
124
|
-
process.exit(-1);
|
|
125
|
-
});
|
|
126
|
-
}
|
package/test/all/secp256k1.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2026 @pezkuwi/wasm-crypto authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
/* global it */
|
|
5
|
-
|
|
6
|
-
import { assert, hexToU8a, u8aToHex } from '@pezkuwi/util';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @param {*} wasm
|
|
10
|
-
*/
|
|
11
|
-
export function secp256k1Compress (wasm) {
|
|
12
|
-
it('compresses a known publicKey', () => {
|
|
13
|
-
const pubkey = '0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af';
|
|
14
|
-
const result = u8aToHex(wasm.secp256k1Compress(hexToU8a(pubkey)));
|
|
15
|
-
|
|
16
|
-
// console.log('\tRES', result);
|
|
17
|
-
|
|
18
|
-
assert(result === '0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077', 'ERROR: secp256k1Compress does not match');
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @param {*} wasm
|
|
24
|
-
*/
|
|
25
|
-
export function secp256k1Expand (wasm) {
|
|
26
|
-
it('expands a known compressed key', () => {
|
|
27
|
-
const pubkey = '0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077';
|
|
28
|
-
const result = u8aToHex(wasm.secp256k1Expand(hexToU8a(pubkey)));
|
|
29
|
-
|
|
30
|
-
// console.log('\tRES', result);
|
|
31
|
-
|
|
32
|
-
assert(result === '0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af', 'ERROR: secp256k1Expand does not match');
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @param {*} wasm
|
|
38
|
-
*/
|
|
39
|
-
export function secp256k1FromSeed1 (wasm) {
|
|
40
|
-
it('creates a known pair from seed (1)', () => {
|
|
41
|
-
const seckey = '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae';
|
|
42
|
-
const result = u8aToHex(wasm.secp256k1FromSeed(hexToU8a(seckey)));
|
|
43
|
-
|
|
44
|
-
// console.log('\tRES', result);
|
|
45
|
-
|
|
46
|
-
assert(result === '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae03fd8c74f795ced92064b86191cb2772b1e3a0947740aa0a5a6e379592471fd85b', 'ERROR: secp256k1FromSeed does not match');
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* @param {*} wasm
|
|
52
|
-
*/
|
|
53
|
-
export function secp256k1FromSeed2 (wasm) {
|
|
54
|
-
it('creates a known pair from seed (2)', () => {
|
|
55
|
-
// https://github.com/pezkuwichain/pezkuwi-wasm/issues/307
|
|
56
|
-
const seckey = new Uint8Array([
|
|
57
|
-
203, 109, 249, 222, 30, 252, 167, 163, 153, 138, 142,
|
|
58
|
-
173, 78, 2, 21, 157, 95, 169, 156, 62, 13, 79,
|
|
59
|
-
214, 67, 38, 103, 57, 11, 180, 114, 104, 84
|
|
60
|
-
]);
|
|
61
|
-
const expected = u8aToHex(new Uint8Array([
|
|
62
|
-
203, 109, 249, 222, 30, 252, 167, 163, 153, 138, 142,
|
|
63
|
-
173, 78, 2, 21, 157, 95, 169, 156, 62, 13, 79,
|
|
64
|
-
214, 67, 38, 103, 57, 11, 180, 114, 104, 84, 2,
|
|
65
|
-
10, 16, 145, 52, 31, 229, 102, 75, 250, 23, 130,
|
|
66
|
-
213, 224, 71, 121, 104, 144, 104, 201, 22, 176, 76,
|
|
67
|
-
179, 101, 236, 49, 83, 117, 86, 132, 217, 161
|
|
68
|
-
]));
|
|
69
|
-
const result = u8aToHex(wasm.secp256k1FromSeed(seckey));
|
|
70
|
-
|
|
71
|
-
// console.log('\tRES', result);
|
|
72
|
-
|
|
73
|
-
assert(result === expected, 'ERROR: secp256k1FromSeed does not match');
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @param {*} wasm
|
|
79
|
-
*/
|
|
80
|
-
export function secp256k1Recover (wasm) {
|
|
81
|
-
it('recovers a publicKey from signature', () => {
|
|
82
|
-
const sig = '0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff';
|
|
83
|
-
const msg = '0xa30b64ce1eedf409c8afb801d72c05234e64849ea538c15dd3c8cf4ffcf166c9';
|
|
84
|
-
const result = u8aToHex(wasm.secp256k1Recover(hexToU8a(msg), hexToU8a(sig), 0));
|
|
85
|
-
|
|
86
|
-
// console.log('\tRES', result);
|
|
87
|
-
|
|
88
|
-
assert(result === '0x028d13da15a02f3a70677339d51b14177ee9b49657662b35e56a9d9dee17db1d30', 'ERROR: secp256k1Recover does not match');
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* @param {*} wasm
|
|
94
|
-
*/
|
|
95
|
-
export function secp256k1Sign (wasm) {
|
|
96
|
-
it('creates a known signature', () => {
|
|
97
|
-
const sec = '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae';
|
|
98
|
-
const msg = '0x68c731589a583d08b70861683b59ce3dd56284cb2f0da5b6cd83e6641dac3aab';
|
|
99
|
-
const result = u8aToHex(wasm.secp256k1Sign(hexToU8a(msg), hexToU8a(sec)));
|
|
100
|
-
|
|
101
|
-
// console.log('\tRES', result);
|
|
102
|
-
|
|
103
|
-
assert(result === '0xdf92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba83b105adec9e9872647a7d8bb28855b45e22805aea3d097953cbb1391f671d13e01', 'ERROR: secp256k1Sign does not match');
|
|
104
|
-
});
|
|
105
|
-
}
|