ox 1.1.0 → 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/CHANGELOG.md +54 -0
- package/dist/core/Engine.d.ts +43 -11
- package/dist/core/Engine.d.ts.map +1 -1
- package/dist/core/Engine.js +40 -10
- package/dist/core/Engine.js.map +1 -1
- package/dist/core/Hash.d.ts +36 -0
- package/dist/core/Hash.d.ts.map +1 -1
- package/dist/core/Hash.js +35 -0
- package/dist/core/Hash.js.map +1 -1
- package/dist/core/internal/engine.d.ts +8 -8
- package/dist/core/internal/engine.d.ts.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/node/Ed25519.d.ts +33 -0
- package/dist/node/Ed25519.d.ts.map +1 -0
- package/dist/node/Ed25519.js +67 -0
- package/dist/node/Ed25519.js.map +1 -0
- package/dist/node/Engine.d.ts +29 -16
- package/dist/node/Engine.d.ts.map +1 -1
- package/dist/node/Engine.js +38 -14
- package/dist/node/Engine.js.map +1 -1
- package/dist/node/Hash.d.ts +17 -19
- package/dist/node/Hash.d.ts.map +1 -1
- package/dist/node/Hash.js +17 -19
- package/dist/node/Hash.js.map +1 -1
- package/dist/node/Keystore.d.ts +36 -0
- package/dist/node/Keystore.d.ts.map +1 -0
- package/dist/node/Keystore.js +72 -0
- package/dist/node/Keystore.js.map +1 -0
- package/dist/node/Mnemonic.d.ts +35 -0
- package/dist/node/Mnemonic.d.ts.map +1 -0
- package/dist/node/Mnemonic.js +46 -0
- package/dist/node/Mnemonic.js.map +1 -0
- package/dist/node/P256.d.ts +32 -0
- package/dist/node/P256.d.ts.map +1 -0
- package/dist/node/P256.js +34 -0
- package/dist/node/P256.js.map +1 -0
- package/dist/node/X25519.d.ts +31 -0
- package/dist/node/X25519.d.ts.map +1 -0
- package/dist/node/X25519.js +71 -0
- package/dist/node/X25519.js.map +1 -0
- package/dist/node/index.d.ts +95 -8
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +95 -8
- package/dist/node/index.js.map +1 -1
- package/dist/trusted-setups/Paths.d.ts +1 -0
- package/dist/trusted-setups/Paths.d.ts.map +1 -1
- package/dist/trusted-setups/Paths.js +1 -0
- package/dist/trusted-setups/Paths.js.map +1 -1
- package/dist/trusted-setups/Setups.d.ts +35 -0
- package/dist/trusted-setups/Setups.d.ts.map +1 -0
- package/dist/trusted-setups/Setups.js +29 -0
- package/dist/trusted-setups/Setups.js.map +1 -0
- package/dist/trusted-setups/index.d.ts +20 -0
- package/dist/trusted-setups/index.d.ts.map +1 -1
- package/dist/trusted-setups/index.js +20 -0
- package/dist/trusted-setups/index.js.map +1 -1
- package/dist/trusted-setups/internal/paths.d.ts.map +1 -1
- package/dist/trusted-setups/internal/paths.js +1 -1
- package/dist/trusted-setups/internal/paths.js.map +1 -1
- package/dist/trusted-setups/internal/setups/mainnet.d.ts +7 -0
- package/dist/trusted-setups/internal/setups/mainnet.d.ts.map +1 -0
- package/dist/trusted-setups/internal/setups/mainnet.js +11 -0
- package/dist/trusted-setups/internal/setups/mainnet.js.map +1 -0
- package/dist/wasm/Ed25519.d.ts +35 -0
- package/dist/wasm/Ed25519.d.ts.map +1 -0
- package/dist/wasm/Ed25519.js +88 -0
- package/dist/wasm/Ed25519.js.map +1 -0
- package/dist/wasm/Engine.d.ts +36 -26
- package/dist/wasm/Engine.d.ts.map +1 -1
- package/dist/wasm/Engine.js +44 -25
- package/dist/wasm/Engine.js.map +1 -1
- package/dist/wasm/Hash.d.ts +13 -19
- package/dist/wasm/Hash.d.ts.map +1 -1
- package/dist/wasm/Hash.js +21 -27
- package/dist/wasm/Hash.js.map +1 -1
- package/dist/wasm/Keystore.d.ts +51 -0
- package/dist/wasm/Keystore.d.ts.map +1 -0
- package/dist/wasm/Keystore.js +51 -0
- package/dist/wasm/Keystore.js.map +1 -0
- package/dist/wasm/Kzg.d.ts +112 -0
- package/dist/wasm/Kzg.d.ts.map +1 -0
- package/dist/wasm/Kzg.js +333 -0
- package/dist/wasm/Kzg.js.map +1 -0
- package/dist/wasm/Mnemonic.d.ts +33 -0
- package/dist/wasm/Mnemonic.d.ts.map +1 -0
- package/dist/wasm/Mnemonic.js +30 -0
- package/dist/wasm/Mnemonic.js.map +1 -0
- package/dist/wasm/Secp256k1.d.ts +55 -0
- package/dist/wasm/Secp256k1.d.ts.map +1 -0
- package/dist/wasm/Secp256k1.js +181 -0
- package/dist/wasm/Secp256k1.js.map +1 -0
- package/dist/wasm/X25519.d.ts +33 -0
- package/dist/wasm/X25519.d.ts.map +1 -0
- package/dist/wasm/X25519.js +53 -0
- package/dist/wasm/X25519.js.map +1 -0
- package/dist/wasm/index.d.ts +124 -12
- package/dist/wasm/index.d.ts.map +1 -1
- package/dist/wasm/index.js +124 -12
- package/dist/wasm/index.js.map +1 -1
- package/dist/wasm/internal/blake3.d.ts +11 -0
- package/dist/wasm/internal/blake3.d.ts.map +1 -0
- package/dist/wasm/internal/blake3.js +31 -0
- package/dist/wasm/internal/blake3.js.map +1 -0
- package/dist/wasm/internal/blake3.wasm.d.ts +3 -0
- package/dist/wasm/internal/blake3.wasm.d.ts.map +1 -0
- package/dist/wasm/internal/blake3.wasm.js +10 -0
- package/dist/wasm/internal/blake3.wasm.js.map +1 -0
- package/dist/wasm/internal/crypto25519.d.ts +16 -0
- package/dist/wasm/internal/crypto25519.d.ts.map +1 -0
- package/dist/wasm/internal/crypto25519.js +5 -0
- package/dist/wasm/internal/crypto25519.js.map +1 -0
- package/dist/wasm/internal/crypto25519.wasm.d.ts +3 -0
- package/dist/wasm/internal/crypto25519.wasm.d.ts.map +1 -0
- package/dist/wasm/internal/crypto25519.wasm.js +10 -0
- package/dist/wasm/internal/crypto25519.wasm.js.map +1 -0
- package/dist/wasm/internal/ed25519.d.ts +4 -0
- package/dist/wasm/internal/ed25519.d.ts.map +1 -0
- package/dist/wasm/internal/ed25519.js +21 -0
- package/dist/wasm/internal/ed25519.js.map +1 -0
- package/dist/wasm/internal/hashes.d.ts +12 -0
- package/dist/wasm/internal/hashes.d.ts.map +1 -0
- package/dist/wasm/internal/hashes.js +8 -0
- package/dist/wasm/internal/hashes.js.map +1 -0
- package/dist/wasm/internal/hashes.wasm.d.ts +4 -2
- package/dist/wasm/internal/hashes.wasm.d.ts.map +1 -1
- package/dist/wasm/internal/hashes.wasm.js +4 -2
- package/dist/wasm/internal/hashes.wasm.js.map +1 -1
- package/dist/wasm/internal/instantiate.d.ts +12 -0
- package/dist/wasm/internal/instantiate.d.ts.map +1 -1
- package/dist/wasm/internal/instantiate.js +23 -7
- package/dist/wasm/internal/instantiate.js.map +1 -1
- package/dist/wasm/internal/keystore.d.ts +10 -0
- package/dist/wasm/internal/keystore.d.ts.map +1 -0
- package/dist/wasm/internal/keystore.js +10 -0
- package/dist/wasm/internal/keystore.js.map +1 -0
- package/dist/wasm/internal/kzg.d.ts +17 -0
- package/dist/wasm/internal/kzg.d.ts.map +1 -0
- package/dist/wasm/internal/kzg.js +8 -0
- package/dist/wasm/internal/kzg.js.map +1 -0
- package/dist/wasm/internal/kzg.wasm.d.ts +3 -0
- package/dist/wasm/internal/kzg.wasm.d.ts.map +1 -0
- package/dist/wasm/internal/kzg.wasm.js +10 -0
- package/dist/wasm/internal/kzg.wasm.js.map +1 -0
- package/dist/wasm/internal/mnemonic.d.ts +4 -0
- package/dist/wasm/internal/mnemonic.d.ts.map +1 -0
- package/dist/wasm/internal/mnemonic.js +45 -0
- package/dist/wasm/internal/mnemonic.js.map +1 -0
- package/dist/wasm/internal/pbkdf2.d.ts +12 -0
- package/dist/wasm/internal/pbkdf2.d.ts.map +1 -0
- package/dist/wasm/internal/pbkdf2.js +51 -0
- package/dist/wasm/internal/pbkdf2.js.map +1 -0
- package/dist/wasm/internal/scrypt.d.ts +16 -0
- package/dist/wasm/internal/scrypt.d.ts.map +1 -0
- package/dist/wasm/internal/scrypt.js +78 -0
- package/dist/wasm/internal/scrypt.js.map +1 -0
- package/dist/wasm/internal/scrypt.wasm.d.ts +7 -0
- package/dist/wasm/internal/scrypt.wasm.d.ts.map +1 -0
- package/dist/wasm/internal/scrypt.wasm.js +14 -0
- package/dist/wasm/internal/scrypt.wasm.js.map +1 -0
- package/dist/wasm/internal/secp256k1.d.ts +16 -0
- package/dist/wasm/internal/secp256k1.d.ts.map +1 -0
- package/dist/wasm/internal/secp256k1.js +20 -0
- package/dist/wasm/internal/secp256k1.js.map +1 -0
- package/dist/wasm/internal/secp256k1.wasm.d.ts +3 -0
- package/dist/wasm/internal/secp256k1.wasm.d.ts.map +1 -0
- package/dist/wasm/internal/secp256k1.wasm.js +10 -0
- package/dist/wasm/internal/secp256k1.wasm.js.map +1 -0
- package/dist/wasm/internal/x25519.d.ts +4 -0
- package/dist/wasm/internal/x25519.d.ts.map +1 -0
- package/dist/wasm/internal/x25519.js +21 -0
- package/dist/wasm/internal/x25519.js.map +1 -0
- package/package.json +64 -2
- package/src/core/Engine.ts +84 -11
- package/src/core/Hash.ts +59 -0
- package/src/core/_test/Block.test.ts +9 -2
- package/src/core/_test/Engine.test-d.ts +56 -0
- package/src/core/_test/Engine.test.ts +95 -0
- package/src/core/_test/Hash.test-d.ts +18 -0
- package/src/core/_test/Hash.test.ts +100 -1
- package/src/core/_test/Hash.vectors.test.ts +7 -0
- package/src/core/_test/Transaction.test.ts +11 -2
- package/src/core/internal/engine.ts +8 -8
- package/src/index.ts +5 -2
- package/src/node/Ed25519.ts +87 -0
- package/src/node/Engine.ts +79 -23
- package/src/node/Hash.ts +25 -30
- package/src/node/Keystore.ts +103 -0
- package/src/node/Mnemonic.ts +68 -0
- package/src/node/P256.ts +48 -0
- package/src/node/X25519.ts +93 -0
- package/src/node/_test/Curves.fuzz.ts +76 -0
- package/src/node/_test/Ed25519.test-d.ts +35 -0
- package/src/node/_test/Ed25519.test.ts +220 -0
- package/src/node/_test/Engine.test-d.ts +28 -0
- package/src/node/_test/Engine.test.ts +62 -22
- package/src/node/_test/Hash.test-d.ts +16 -9
- package/src/node/_test/Hash.test.ts +10 -11
- package/src/node/_test/Keystore.fuzz.ts +58 -0
- package/src/node/_test/Keystore.test-d.ts +43 -0
- package/src/node/_test/Keystore.test.ts +198 -0
- package/src/node/_test/Mnemonic.fuzz.ts +31 -0
- package/src/node/_test/Mnemonic.test-d.ts +21 -0
- package/src/node/_test/Mnemonic.test.ts +98 -0
- package/src/node/_test/P256.test-d.ts +29 -0
- package/src/node/_test/P256.test.ts +102 -0
- package/src/node/_test/X25519.test-d.ts +28 -0
- package/src/node/_test/X25519.test.ts +239 -0
- package/src/node/index.ts +100 -8
- package/src/trusted-setups/Paths.ts +1 -0
- package/src/trusted-setups/README.md +39 -0
- package/src/trusted-setups/Setups.ts +46 -0
- package/src/trusted-setups/_test/Paths.test.ts +16 -0
- package/src/trusted-setups/_test/Setups.test-d.ts +7 -0
- package/src/trusted-setups/_test/Setups.test.ts +43 -0
- package/src/trusted-setups/_test/index.test.ts +1 -0
- package/src/trusted-setups/index.ts +21 -0
- package/src/trusted-setups/internal/paths.ts +4 -1
- package/src/trusted-setups/internal/setups/mainnet.source.json +7 -0
- package/src/trusted-setups/internal/setups/mainnet.ts +16 -0
- package/src/version.ts +1 -1
- package/src/wasm/Ed25519.ts +114 -0
- package/src/wasm/Engine.ts +78 -31
- package/src/wasm/Hash.bench.ts +2 -2
- package/src/wasm/Hash.ts +29 -50
- package/src/wasm/Keystore.bench.ts +26 -0
- package/src/wasm/Keystore.ts +66 -0
- package/src/wasm/Kzg.bench.ts +63 -0
- package/src/wasm/Kzg.ts +518 -0
- package/src/wasm/Mnemonic.ts +44 -0
- package/src/wasm/Secp256k1.bench.ts +79 -0
- package/src/wasm/Secp256k1.ts +248 -0
- package/src/wasm/X25519.ts +70 -0
- package/src/wasm/_test/Crypto25519.conformance.ts +57 -0
- package/src/wasm/_test/Crypto25519.fuzz.ts +187 -0
- package/src/wasm/_test/Ed25519.test-d.ts +28 -0
- package/src/wasm/_test/Ed25519.test.ts +168 -0
- package/src/wasm/_test/Ed25519.vectors.test.ts +73 -0
- package/src/wasm/_test/Engine.test-d.ts +29 -0
- package/src/wasm/_test/Engine.test.ts +59 -25
- package/src/wasm/_test/Hash.browser.test.ts +14 -7
- package/src/wasm/_test/Hash.fuzz.ts +58 -23
- package/src/wasm/_test/Hash.test-d.ts +16 -10
- package/src/wasm/_test/Hash.test.ts +148 -48
- package/src/wasm/_test/Hash.vectors.test.ts +15 -8
- package/src/wasm/_test/Keystore.browser.test.ts +73 -0
- package/src/wasm/_test/Keystore.fuzz.ts +163 -0
- package/src/wasm/_test/Keystore.test-d.ts +42 -0
- package/src/wasm/_test/Keystore.test.ts +477 -0
- package/src/wasm/_test/Keystore.vectors.test.ts +21 -0
- package/src/wasm/_test/Kzg.test-d.ts +19 -0
- package/src/wasm/_test/Kzg.test.ts +220 -0
- package/src/wasm/_test/Mnemonic.test-d.ts +17 -0
- package/src/wasm/_test/Mnemonic.test.ts +110 -0
- package/src/wasm/_test/Runtime.test.ts +9 -0
- package/src/wasm/_test/Secp256k1.test-d.ts +35 -0
- package/src/wasm/_test/Secp256k1.test.ts +354 -0
- package/src/wasm/_test/X25519.test-d.ts +21 -0
- package/src/wasm/_test/X25519.test.ts +194 -0
- package/src/wasm/_test/fixtures.ts +4 -0
- package/src/wasm/index.ts +130 -12
- package/src/wasm/internal/blake3.ts +45 -0
- package/src/wasm/internal/blake3.wasm.ts +11 -0
- package/src/wasm/internal/crypto25519.ts +43 -0
- package/src/wasm/internal/crypto25519.wasm.ts +11 -0
- package/src/wasm/internal/ed25519.ts +31 -0
- package/src/wasm/internal/hashes.ts +29 -0
- package/src/wasm/internal/hashes.wasm.ts +5 -2
- package/src/wasm/internal/instantiate.ts +29 -9
- package/src/wasm/internal/keystore.ts +19 -0
- package/src/wasm/internal/kzg.ts +50 -0
- package/src/wasm/internal/kzg.wasm.ts +11 -0
- package/src/wasm/internal/mnemonic.ts +56 -0
- package/src/wasm/internal/pbkdf2.ts +86 -0
- package/src/wasm/internal/scrypt.ts +131 -0
- package/src/wasm/internal/scrypt.wasm.ts +17 -0
- package/src/wasm/internal/secp256k1.ts +62 -0
- package/src/wasm/internal/secp256k1.wasm.ts +11 -0
- package/src/wasm/internal/x25519.ts +29 -0
- package/wasm/vendor/c-kzg-4844/LICENSE +201 -0
- package/wasm/vendor/c-kzg-4844/blst/LICENSE +201 -0
- package/src/trusted-setups/internal/setups/mainnet.txt +0 -4163
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** @internal */
|
|
2
|
-
export const version = '1.
|
|
2
|
+
export const version = '1.2.0'
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type * as Engine from '../core/Engine.js'
|
|
2
|
+
import type * as Errors from '../core/Errors.js'
|
|
3
|
+
import * as crypto25519 from './internal/crypto25519.js'
|
|
4
|
+
import * as ed25519 from './internal/ed25519.js'
|
|
5
|
+
import * as internal from './internal/instantiate.js'
|
|
6
|
+
|
|
7
|
+
export * from '../core/Ed25519.js'
|
|
8
|
+
export { MemoryError } from './internal/instantiate.js'
|
|
9
|
+
|
|
10
|
+
const keySize = 32
|
|
11
|
+
const signatureSize = 64
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Compiles WASM implementations of selected [`Ed25519`](/api/Ed25519)
|
|
15
|
+
* primitives, without installing them.
|
|
16
|
+
*
|
|
17
|
+
* Public-key conversion and random key generation remain on Ox's default
|
|
18
|
+
* implementation. The public conversion is omitted because Monocypher does not
|
|
19
|
+
* reproduce Ox's invalid-point validation.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts twoslash
|
|
23
|
+
* // @noErrors
|
|
24
|
+
* import { Engine } from 'ox'
|
|
25
|
+
* import { Ed25519 } from 'ox/wasm'
|
|
26
|
+
*
|
|
27
|
+
* await Engine.install({ Ed25519: Ed25519.engine() })
|
|
28
|
+
*
|
|
29
|
+
* Ed25519.getPublicKey({ privateKey: '0x...' })
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @returns The WASM implementation of part of the `Ed25519` slot.
|
|
33
|
+
*/
|
|
34
|
+
export async function engine(): Promise<engine.ReturnType> {
|
|
35
|
+
const module = await crypto25519.load()
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
getPublicKey(privateKey) {
|
|
39
|
+
assertLength(privateKey, keySize, 'private key')
|
|
40
|
+
module.reserve(keySize * 2)
|
|
41
|
+
const privateKeyPtr = module.heapBase
|
|
42
|
+
const publicKeyPtr = privateKeyPtr + keySize
|
|
43
|
+
try {
|
|
44
|
+
module.view().set(privateKey, privateKeyPtr)
|
|
45
|
+
module.exports.ed25519_get_public_key(privateKeyPtr, publicKeyPtr)
|
|
46
|
+
return module.view().slice(publicKeyPtr, publicKeyPtr + keySize)
|
|
47
|
+
} finally {
|
|
48
|
+
module.exports.zero(privateKeyPtr, keySize * 2)
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
sign(payload, privateKey) {
|
|
52
|
+
assertLength(privateKey, keySize, 'private key')
|
|
53
|
+
return ed25519.sign(module, payload, privateKey)
|
|
54
|
+
},
|
|
55
|
+
toMontgomerySecret(privateKey) {
|
|
56
|
+
assertLength(privateKey, keySize, 'private key')
|
|
57
|
+
module.reserve(keySize * 2)
|
|
58
|
+
const privateKeyPtr = module.heapBase
|
|
59
|
+
const secretKeyPtr = privateKeyPtr + keySize
|
|
60
|
+
try {
|
|
61
|
+
module.view().set(privateKey, privateKeyPtr)
|
|
62
|
+
module.exports.ed25519_to_montgomery_secret(privateKeyPtr, secretKeyPtr)
|
|
63
|
+
return module.view().slice(secretKeyPtr, secretKeyPtr + keySize)
|
|
64
|
+
} finally {
|
|
65
|
+
module.exports.zero(privateKeyPtr, keySize * 2)
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
verify(signature, payload, publicKey) {
|
|
69
|
+
assertLength(signature, signatureSize, 'signature')
|
|
70
|
+
assertLength(publicKey, keySize, 'public key')
|
|
71
|
+
const size = signatureSize + payload.length + keySize
|
|
72
|
+
module.reserve(size)
|
|
73
|
+
const signaturePtr = module.heapBase
|
|
74
|
+
const payloadPtr = signaturePtr + signatureSize
|
|
75
|
+
const publicKeyPtr = payloadPtr + payload.length
|
|
76
|
+
try {
|
|
77
|
+
const memory = module.view()
|
|
78
|
+
memory.set(signature, signaturePtr)
|
|
79
|
+
memory.set(payload, payloadPtr)
|
|
80
|
+
memory.set(publicKey, publicKeyPtr)
|
|
81
|
+
return (
|
|
82
|
+
module.exports.ed25519_verify(
|
|
83
|
+
signaturePtr,
|
|
84
|
+
payloadPtr,
|
|
85
|
+
payload.length,
|
|
86
|
+
publicKeyPtr,
|
|
87
|
+
) === 1
|
|
88
|
+
)
|
|
89
|
+
} finally {
|
|
90
|
+
module.exports.zero(signaturePtr, size)
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export declare namespace engine {
|
|
97
|
+
/** Every `Ed25519` primitive this module implements. */
|
|
98
|
+
type ReturnType = {
|
|
99
|
+
[key in
|
|
100
|
+
| 'getPublicKey'
|
|
101
|
+
| 'sign'
|
|
102
|
+
| 'toMontgomerySecret'
|
|
103
|
+
| 'verify']-?: NonNullable<Engine.Eddsa[key]>
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
type ErrorType = internal.MemoryError | Errors.GlobalErrorType
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function assertLength(value: Uint8Array, length: number, name: string): void {
|
|
110
|
+
if (value.length !== length)
|
|
111
|
+
throw new RangeError(
|
|
112
|
+
`Ed25519 ${name} must be ${length} bytes, got ${value.length}`,
|
|
113
|
+
)
|
|
114
|
+
}
|
package/src/wasm/Engine.ts
CHANGED
|
@@ -1,55 +1,81 @@
|
|
|
1
1
|
import * as CoreEngine from '../core/Engine.js'
|
|
2
2
|
import type * as Errors from '../core/Errors.js'
|
|
3
|
+
import * as Ed25519 from './Ed25519.js'
|
|
3
4
|
import * as Hash from './Hash.js'
|
|
5
|
+
import * as keystore from './internal/keystore.js'
|
|
6
|
+
import * as Mnemonic from './Mnemonic.js'
|
|
7
|
+
import * as X25519 from './X25519.js'
|
|
4
8
|
import type * as internal from './internal/instantiate.js'
|
|
5
9
|
|
|
10
|
+
export {
|
|
11
|
+
AsyncScopeError,
|
|
12
|
+
get,
|
|
13
|
+
InvalidSlotValueError,
|
|
14
|
+
reset,
|
|
15
|
+
set,
|
|
16
|
+
UnknownPrimitiveError,
|
|
17
|
+
UnknownSlotError,
|
|
18
|
+
with,
|
|
19
|
+
} from '../core/Engine.js'
|
|
20
|
+
export type {
|
|
21
|
+
Bls,
|
|
22
|
+
Ecdh,
|
|
23
|
+
Ecdsa,
|
|
24
|
+
Eddsa,
|
|
25
|
+
Engine,
|
|
26
|
+
Hash,
|
|
27
|
+
Keystore,
|
|
28
|
+
Mnemonic,
|
|
29
|
+
} from '../core/Engine.js'
|
|
30
|
+
|
|
6
31
|
/**
|
|
7
|
-
* Compiles every implementation this entrypoint provides
|
|
32
|
+
* Compiles and installs every WASM implementation this entrypoint provides.
|
|
8
33
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
34
|
+
* Slot compilation starts concurrently. Nothing is installed until every slot
|
|
35
|
+
* resolves successfully.
|
|
11
36
|
*
|
|
12
|
-
* Call
|
|
13
|
-
* at call time, so values computed beforehand used whatever was
|
|
37
|
+
* Call this once, during startup, before any crypto call. Ox resolves the
|
|
38
|
+
* engine at call time, so values computed beforehand used whatever was
|
|
39
|
+
* installed then.
|
|
14
40
|
*
|
|
15
41
|
* @example
|
|
16
42
|
* ```ts twoslash
|
|
17
43
|
* // @noErrors
|
|
18
|
-
* import { Hash } from 'ox'
|
|
19
|
-
* import { Engine } from 'ox/wasm'
|
|
44
|
+
* import { Engine, Hash } from 'ox/wasm'
|
|
20
45
|
*
|
|
21
|
-
* await Engine.
|
|
46
|
+
* await Engine.install()
|
|
22
47
|
*
|
|
23
|
-
* Hash.
|
|
48
|
+
* Hash.sha256('0xdeadbeef')
|
|
24
49
|
* ```
|
|
25
50
|
*
|
|
26
51
|
* @returns The engine that was installed.
|
|
27
52
|
*/
|
|
28
|
-
export async function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
53
|
+
export async function install(): Promise<install.ReturnType> {
|
|
54
|
+
return CoreEngine.install({
|
|
55
|
+
Ed25519: Ed25519.engine(),
|
|
56
|
+
Hash: Hash.engine(),
|
|
57
|
+
Keystore: keystore.engine(),
|
|
58
|
+
Mnemonic: Mnemonic.engine(),
|
|
59
|
+
X25519: X25519.engine(),
|
|
60
|
+
})
|
|
32
61
|
}
|
|
33
62
|
|
|
34
|
-
export declare namespace
|
|
35
|
-
type ReturnType =
|
|
63
|
+
export declare namespace install {
|
|
64
|
+
type ReturnType = engine.ReturnType
|
|
36
65
|
|
|
37
66
|
type ErrorType =
|
|
38
|
-
|
|
|
39
|
-
| CoreEngine.
|
|
67
|
+
| engine.ErrorType
|
|
68
|
+
| CoreEngine.install.ErrorType
|
|
40
69
|
| Errors.GlobalErrorType
|
|
41
70
|
}
|
|
42
71
|
|
|
43
72
|
/**
|
|
44
|
-
* Compiles every implementation this entrypoint provides, without
|
|
45
|
-
* it.
|
|
73
|
+
* Compiles every WASM implementation this entrypoint provides, without
|
|
74
|
+
* installing it.
|
|
46
75
|
*
|
|
47
|
-
* Reach for this where an engine has to exist as a value
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* engines does not need it, because
|
|
51
|
-
* [`Engine.set`](/api/Engine/set) merges -- `await load()` followed by
|
|
52
|
-
* `Engine.set({ Secp256k1 })` leaves both in place.
|
|
76
|
+
* Reach for this where an engine has to exist as a value, such as a benchmark
|
|
77
|
+
* or an [`Engine.with`](/api/Engine/with) scope. Use the individual module's
|
|
78
|
+
* `engine` function to select only some slots.
|
|
53
79
|
*
|
|
54
80
|
* @example
|
|
55
81
|
* ```ts twoslash
|
|
@@ -57,20 +83,41 @@ export declare namespace load {
|
|
|
57
83
|
* import { Engine, Hash } from 'ox'
|
|
58
84
|
* import { Engine as Wasm } from 'ox/wasm'
|
|
59
85
|
*
|
|
60
|
-
* const wasm = await Wasm.
|
|
86
|
+
* const wasm = await Wasm.engine()
|
|
61
87
|
*
|
|
62
|
-
* Engine.with(wasm, () => Hash.
|
|
88
|
+
* Engine.with(wasm, () => Hash.sha256('0xdeadbeef'))
|
|
63
89
|
* ```
|
|
64
90
|
*
|
|
65
91
|
* @returns An engine, ready to install.
|
|
66
92
|
*/
|
|
67
|
-
export async function
|
|
68
|
-
|
|
93
|
+
export async function engine(): Promise<engine.ReturnType> {
|
|
94
|
+
const [ed25519, hash, keystoreEngine, mnemonic, x25519] = await Promise.all([
|
|
95
|
+
Ed25519.engine(),
|
|
96
|
+
Hash.engine(),
|
|
97
|
+
keystore.engine(),
|
|
98
|
+
Mnemonic.engine(),
|
|
99
|
+
X25519.engine(),
|
|
100
|
+
])
|
|
101
|
+
return {
|
|
102
|
+
Ed25519: ed25519,
|
|
103
|
+
Hash: hash,
|
|
104
|
+
Keystore: keystoreEngine,
|
|
105
|
+
Mnemonic: mnemonic,
|
|
106
|
+
X25519: x25519,
|
|
107
|
+
}
|
|
69
108
|
}
|
|
70
109
|
|
|
71
|
-
export declare namespace
|
|
110
|
+
export declare namespace engine {
|
|
72
111
|
/** Every slot this entrypoint supplies, each with its primitives present. */
|
|
73
|
-
type ReturnType =
|
|
112
|
+
type ReturnType = {
|
|
113
|
+
Ed25519: Ed25519.engine.ReturnType
|
|
114
|
+
Hash: Hash.engine.ReturnType
|
|
115
|
+
Keystore: {
|
|
116
|
+
pbkdf2Sha256: NonNullable<CoreEngine.Keystore['pbkdf2Sha256']>
|
|
117
|
+
}
|
|
118
|
+
Mnemonic: Mnemonic.engine.ReturnType
|
|
119
|
+
X25519: X25519.engine.ReturnType
|
|
120
|
+
}
|
|
74
121
|
|
|
75
122
|
type ErrorType = internal.MemoryError | Errors.GlobalErrorType
|
|
76
123
|
}
|
package/src/wasm/Hash.bench.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { ripemd160 as noble_ripemd160 } from '@noble/hashes/legacy.js'
|
|
|
3
3
|
import { sha256 as noble_sha256 } from '@noble/hashes/sha2.js'
|
|
4
4
|
import { keccak_256 as noble_keccak256 } from '@noble/hashes/sha3.js'
|
|
5
5
|
import { bench, describe } from 'vp/test'
|
|
6
|
-
import {
|
|
6
|
+
import { engine as wasmEngine } from './Hash.js'
|
|
7
7
|
|
|
8
8
|
// Sizes expose fixed boundary overhead and sustained throughput. Crossovers
|
|
9
9
|
// vary by primitive, runtime, and processor.
|
|
@@ -12,7 +12,7 @@ import { create as createWasm } from './Hash.js'
|
|
|
12
12
|
// run Rust, and this portable benchmark must remain browser-compatible.
|
|
13
13
|
const sizes = [32, 64, 256, 1024, 4096, 65_536, 1_048_576] as const
|
|
14
14
|
|
|
15
|
-
const wasm =
|
|
15
|
+
const wasm = await wasmEngine()
|
|
16
16
|
const key = Uint8Array.from({ length: 32 }, (_, index) => index % 97)
|
|
17
17
|
|
|
18
18
|
for (const size of sizes) {
|
package/src/wasm/Hash.ts
CHANGED
|
@@ -1,38 +1,22 @@
|
|
|
1
1
|
import type * as Engine from '../core/Engine.js'
|
|
2
2
|
import type * as Errors from '../core/Errors.js'
|
|
3
|
+
import * as blake3 from './internal/blake3.js'
|
|
3
4
|
import * as hash from './internal/hash.js'
|
|
4
|
-
import
|
|
5
|
+
import * as hashes from './internal/hashes.js'
|
|
5
6
|
import * as internal from './internal/instantiate.js'
|
|
6
7
|
|
|
8
|
+
export * from '../core/Hash.js'
|
|
7
9
|
export { MemoryError } from './internal/instantiate.js'
|
|
8
10
|
|
|
9
|
-
type Exports = hash.Exports & {
|
|
10
|
-
keccak256(input: number, length: number, out: number): void
|
|
11
|
-
ripemd160(input: number, length: number, out: number): void
|
|
12
|
-
sha256(input: number, length: number, out: number): void
|
|
13
|
-
}
|
|
14
|
-
|
|
15
11
|
/** Digest sizes, in bytes. */
|
|
16
12
|
const digestSize = { keccak256: 32, ripemd160: 20, sha256: 32 }
|
|
17
13
|
|
|
18
14
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* Handing every caller the same object means one of them customising a slot
|
|
22
|
-
* for composition silently changes what a later `create` -- or `Engine.load`
|
|
23
|
-
* -- installs.
|
|
24
|
-
*
|
|
25
|
-
* @internal
|
|
26
|
-
*/
|
|
27
|
-
const instantiate = /*#__PURE__*/ internal.memoize(() =>
|
|
28
|
-
internal.instantiate<Exports>(wasmBase64),
|
|
29
|
-
)
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Compiles the WASM implementation of the [`Hash`](/api/Hash) primitives, without
|
|
33
|
-
* installing it.
|
|
15
|
+
* Compiles the WASM implementation of the [`Hash`](/api/Hash) primitives,
|
|
16
|
+
* without installing it.
|
|
34
17
|
*
|
|
35
|
-
* Most callers want
|
|
18
|
+
* Most callers want
|
|
19
|
+
* [`Engine.install`](/wasm/crypto/Engine/install) instead, which compiles every
|
|
36
20
|
* implementation this entrypoint provides and installs them in one call. Reach
|
|
37
21
|
* for this to take the `Hash` slot on its own, or to hold the implementation
|
|
38
22
|
* without touching the installed engine.
|
|
@@ -46,19 +30,22 @@ const instantiate = /*#__PURE__*/ internal.memoize(() =>
|
|
|
46
30
|
* @example
|
|
47
31
|
* ```ts twoslash
|
|
48
32
|
* // @noErrors
|
|
49
|
-
* import { Engine
|
|
50
|
-
* import
|
|
33
|
+
* import { Engine } from 'ox'
|
|
34
|
+
* import { Hash } from 'ox/wasm'
|
|
51
35
|
*
|
|
52
|
-
* Engine.
|
|
36
|
+
* await Engine.install({ Hash: Hash.engine() })
|
|
53
37
|
*
|
|
54
38
|
* Hash.keccak256('0xdeadbeef')
|
|
55
39
|
* ```
|
|
56
40
|
*
|
|
57
|
-
* @returns
|
|
41
|
+
* @returns The WASM implementation of the `Hash` slot.
|
|
58
42
|
*/
|
|
59
|
-
export async function
|
|
43
|
+
export async function engine(): Promise<engine.ReturnType> {
|
|
60
44
|
{
|
|
61
|
-
const module = await
|
|
45
|
+
const [blake3Module, module] = await Promise.all([
|
|
46
|
+
blake3.load(),
|
|
47
|
+
hashes.load(),
|
|
48
|
+
])
|
|
62
49
|
|
|
63
50
|
// Copies `input` in, runs `hash`, and copies the digest back out.
|
|
64
51
|
function call(
|
|
@@ -77,32 +64,24 @@ export async function create(): Promise<create.ReturnType> {
|
|
|
77
64
|
}
|
|
78
65
|
|
|
79
66
|
return {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
},
|
|
67
|
+
blake3: (input) => blake3.hash(blake3Module, input),
|
|
68
|
+
hmacSha256: (key, message) => hash.hmacSha256(module, key, message),
|
|
69
|
+
keccak256: (input) => call('keccak256', input),
|
|
70
|
+
ripemd160: (input) => call('ripemd160', input),
|
|
71
|
+
sha256: (input) => call('sha256', input),
|
|
86
72
|
}
|
|
87
73
|
}
|
|
88
74
|
}
|
|
89
75
|
|
|
90
|
-
export declare namespace
|
|
91
|
-
/**
|
|
92
|
-
* The `Hash` slot, carrying every primitive this module implements.
|
|
93
|
-
*
|
|
94
|
-
* {@link ox#Engine.Engine} is optional all the way down so that an engine can
|
|
95
|
-
* fill in as little as it likes. This one always fills the same four, and
|
|
96
|
-
* saying so is what spares callers a non-null assertion per primitive.
|
|
97
|
-
*/
|
|
76
|
+
export declare namespace engine {
|
|
77
|
+
/** Every `Hash` primitive this module implements. */
|
|
98
78
|
type ReturnType = {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
79
|
+
[key in
|
|
80
|
+
| 'blake3'
|
|
81
|
+
| 'hmacSha256'
|
|
82
|
+
| 'keccak256'
|
|
83
|
+
| 'ripemd160'
|
|
84
|
+
| 'sha256']-?: NonNullable<Engine.Hash[key]>
|
|
106
85
|
}
|
|
107
86
|
|
|
108
87
|
type ErrorType = internal.MemoryError | Errors.GlobalErrorType
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { scrypt as scrypt_noble } from '@noble/hashes/scrypt.js'
|
|
2
|
+
import { bench, describe } from 'vp/test'
|
|
3
|
+
import { engine as wasmEngine } from './Keystore.js'
|
|
4
|
+
|
|
5
|
+
// Run `pnpm bench --project core src/wasm/Keystore.bench.ts`. These cases expose
|
|
6
|
+
// the effect of block size, parallelization, and the public defaults.
|
|
7
|
+
const cases = [
|
|
8
|
+
{ N: 1024, dkLen: 32, p: 1, r: 1 },
|
|
9
|
+
{ N: 16_384, dkLen: 32, p: 1, r: 8 },
|
|
10
|
+
{ N: 262_144, dkLen: 32, p: 8, r: 1 },
|
|
11
|
+
] as const
|
|
12
|
+
|
|
13
|
+
const wasm = await wasmEngine()
|
|
14
|
+
const password = new Uint8Array(16).fill(0x5a)
|
|
15
|
+
const salt = new Uint8Array(32).fill(0xa5)
|
|
16
|
+
|
|
17
|
+
for (const options of cases)
|
|
18
|
+
describe(`scrypt (N=${options.N}, r=${options.r}, p=${options.p})`, () => {
|
|
19
|
+
bench('ox', () => {
|
|
20
|
+
scrypt_noble(password, salt, options)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
bench('ox/wasm', () => {
|
|
24
|
+
wasm.scrypt(password, salt, options)
|
|
25
|
+
})
|
|
26
|
+
})
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type * as Engine from '../core/Engine.js'
|
|
2
|
+
import type * as Errors from '../core/Errors.js'
|
|
3
|
+
import * as internal from './internal/instantiate.js'
|
|
4
|
+
import * as keystore from './internal/keystore.js'
|
|
5
|
+
import * as scrypt from './internal/scrypt.js'
|
|
6
|
+
|
|
7
|
+
export * from '../core/Keystore.js'
|
|
8
|
+
export { MemoryError } from './internal/instantiate.js'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Compiles the WASM implementations of the [`Keystore`](/api/Keystore)
|
|
12
|
+
* synchronous key-derivation primitives, without installing them.
|
|
13
|
+
*
|
|
14
|
+
* Most callers can use [`Engine.install`](/wasm/crypto/Engine/install).
|
|
15
|
+
* Install this factory explicitly when an application has benchmarked and
|
|
16
|
+
* selected WASM scrypt.
|
|
17
|
+
*
|
|
18
|
+
* This provider stays synchronous and leaves AES and asynchronous key
|
|
19
|
+
* derivation on Ox's default implementation.
|
|
20
|
+
*
|
|
21
|
+
* Scrypt uses a standalone artifact and grows memory only when called.
|
|
22
|
+
* WebAssembly cannot shrink that capacity. The provider clears its contents
|
|
23
|
+
* and enforces Noble's 1 GiB temporary-workspace limit.
|
|
24
|
+
*
|
|
25
|
+
* The aggregate [`Engine.install`](/wasm/crypto/Engine/install) installs only
|
|
26
|
+
* PBKDF2 from this provider. Install this factory explicitly to opt into
|
|
27
|
+
* scrypt, whose relative performance depends on its parameters and runtime.
|
|
28
|
+
*
|
|
29
|
+
* Copied inputs, derived output, and the complete workspace are cleared after
|
|
30
|
+
* every return or trap.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts twoslash
|
|
34
|
+
* // @noErrors
|
|
35
|
+
* import { Engine } from 'ox'
|
|
36
|
+
* import { Keystore } from 'ox/wasm'
|
|
37
|
+
*
|
|
38
|
+
* await Engine.install({ Keystore: Keystore.engine() })
|
|
39
|
+
*
|
|
40
|
+
* Keystore.scrypt({ password: 'testpassword' })
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @returns The WASM implementations of synchronous PBKDF2-HMAC-SHA256 and
|
|
44
|
+
* scrypt.
|
|
45
|
+
*/
|
|
46
|
+
export async function engine(): Promise<engine.ReturnType> {
|
|
47
|
+
const [keystoreEngine, scryptModule] = await Promise.all([
|
|
48
|
+
keystore.engine(),
|
|
49
|
+
scrypt.load(),
|
|
50
|
+
])
|
|
51
|
+
return {
|
|
52
|
+
...keystoreEngine,
|
|
53
|
+
scrypt: (password, salt, options) =>
|
|
54
|
+
scrypt.derive(scryptModule, password, salt, options),
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export declare namespace engine {
|
|
59
|
+
/** The WASM `Keystore` primitives this module implements. */
|
|
60
|
+
type ReturnType = {
|
|
61
|
+
pbkdf2Sha256: NonNullable<Engine.Keystore['pbkdf2Sha256']>
|
|
62
|
+
scrypt: NonNullable<Engine.Keystore['scrypt']>
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
type ErrorType = internal.MemoryError | Errors.GlobalErrorType
|
|
66
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { afterAll, bench, describe } from 'vp/test'
|
|
2
|
+
import { mainnet as trustedSetup } from '../trusted-setups/Setups.js'
|
|
3
|
+
import { create } from './Kzg.js'
|
|
4
|
+
|
|
5
|
+
const once = {
|
|
6
|
+
iterations: 1,
|
|
7
|
+
time: 0,
|
|
8
|
+
warmupIterations: 0,
|
|
9
|
+
warmupTime: 0,
|
|
10
|
+
} as const
|
|
11
|
+
|
|
12
|
+
describe('create', () => {
|
|
13
|
+
bench(
|
|
14
|
+
'precompute 0',
|
|
15
|
+
async () => {
|
|
16
|
+
const kzg = await create({ trustedSetup })
|
|
17
|
+
kzg.dispose()
|
|
18
|
+
},
|
|
19
|
+
once,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
bench(
|
|
23
|
+
'precompute 8',
|
|
24
|
+
async () => {
|
|
25
|
+
const kzg = await create({ precompute: 8, trustedSetup })
|
|
26
|
+
kzg.dispose()
|
|
27
|
+
},
|
|
28
|
+
once,
|
|
29
|
+
)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const kzg = await create({ trustedSetup })
|
|
33
|
+
const blob = new Uint8Array(131_072)
|
|
34
|
+
const commitment = kzg.blobToKzgCommitment(blob)
|
|
35
|
+
const { cells, proofs } = kzg.computeCellsAndKzgProofs(blob)
|
|
36
|
+
const indices = cells.map((_, index) => index)
|
|
37
|
+
const commitments = cells.map(() => commitment)
|
|
38
|
+
const partialIndices = indices.filter((index) => index % 2 === 0)
|
|
39
|
+
const partialCells = cells.filter((_, index) => index % 2 === 0)
|
|
40
|
+
|
|
41
|
+
afterAll(() => kzg.dispose())
|
|
42
|
+
|
|
43
|
+
describe('operations', () => {
|
|
44
|
+
bench('blobToKzgCommitment', () => {
|
|
45
|
+
kzg.blobToKzgCommitment(blob)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
bench('computeCells', () => {
|
|
49
|
+
kzg.computeCells(blob)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
bench('computeCellsAndKzgProofs', () => {
|
|
53
|
+
kzg.computeCellsAndKzgProofs(blob)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
bench('recoverCellsAndKzgProofs', () => {
|
|
57
|
+
kzg.recoverCellsAndKzgProofs(partialIndices, partialCells)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
bench('verifyCellKzgProofBatch', () => {
|
|
61
|
+
kzg.verifyCellKzgProofBatch(commitments, indices, cells, proofs)
|
|
62
|
+
})
|
|
63
|
+
})
|