ox 1.1.2 → 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.
Files changed (131) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/core/Hash.d.ts +36 -0
  3. package/dist/core/Hash.d.ts.map +1 -1
  4. package/dist/core/Hash.js +35 -0
  5. package/dist/core/Hash.js.map +1 -1
  6. package/dist/node/Hash.d.ts +3 -3
  7. package/dist/node/Hash.js +3 -3
  8. package/dist/trusted-setups/Paths.d.ts +1 -0
  9. package/dist/trusted-setups/Paths.d.ts.map +1 -1
  10. package/dist/trusted-setups/Paths.js +1 -0
  11. package/dist/trusted-setups/Paths.js.map +1 -1
  12. package/dist/trusted-setups/Setups.d.ts +35 -0
  13. package/dist/trusted-setups/Setups.d.ts.map +1 -0
  14. package/dist/trusted-setups/Setups.js +29 -0
  15. package/dist/trusted-setups/Setups.js.map +1 -0
  16. package/dist/trusted-setups/index.d.ts +20 -0
  17. package/dist/trusted-setups/index.d.ts.map +1 -1
  18. package/dist/trusted-setups/index.js +20 -0
  19. package/dist/trusted-setups/index.js.map +1 -1
  20. package/dist/trusted-setups/internal/paths.d.ts.map +1 -1
  21. package/dist/trusted-setups/internal/paths.js +1 -1
  22. package/dist/trusted-setups/internal/paths.js.map +1 -1
  23. package/dist/trusted-setups/internal/setups/mainnet.d.ts +7 -0
  24. package/dist/trusted-setups/internal/setups/mainnet.d.ts.map +1 -0
  25. package/dist/trusted-setups/internal/setups/mainnet.js +11 -0
  26. package/dist/trusted-setups/internal/setups/mainnet.js.map +1 -0
  27. package/dist/wasm/Engine.d.ts +3 -2
  28. package/dist/wasm/Engine.d.ts.map +1 -1
  29. package/dist/wasm/Engine.js +5 -5
  30. package/dist/wasm/Engine.js.map +1 -1
  31. package/dist/wasm/Keystore.d.ts +23 -9
  32. package/dist/wasm/Keystore.d.ts.map +1 -1
  33. package/dist/wasm/Keystore.js +30 -13
  34. package/dist/wasm/Keystore.js.map +1 -1
  35. package/dist/wasm/Kzg.d.ts +112 -0
  36. package/dist/wasm/Kzg.d.ts.map +1 -0
  37. package/dist/wasm/Kzg.js +333 -0
  38. package/dist/wasm/Kzg.js.map +1 -0
  39. package/dist/wasm/Secp256k1.d.ts +55 -0
  40. package/dist/wasm/Secp256k1.d.ts.map +1 -0
  41. package/dist/wasm/Secp256k1.js +181 -0
  42. package/dist/wasm/Secp256k1.js.map +1 -0
  43. package/dist/wasm/index.d.ts +44 -1
  44. package/dist/wasm/index.d.ts.map +1 -1
  45. package/dist/wasm/index.js +44 -1
  46. package/dist/wasm/index.js.map +1 -1
  47. package/dist/wasm/internal/instantiate.d.ts +12 -0
  48. package/dist/wasm/internal/instantiate.d.ts.map +1 -1
  49. package/dist/wasm/internal/instantiate.js +23 -7
  50. package/dist/wasm/internal/instantiate.js.map +1 -1
  51. package/dist/wasm/internal/keystore.d.ts +10 -0
  52. package/dist/wasm/internal/keystore.d.ts.map +1 -0
  53. package/dist/wasm/internal/keystore.js +10 -0
  54. package/dist/wasm/internal/keystore.js.map +1 -0
  55. package/dist/wasm/internal/kzg.d.ts +17 -0
  56. package/dist/wasm/internal/kzg.d.ts.map +1 -0
  57. package/dist/wasm/internal/kzg.js +8 -0
  58. package/dist/wasm/internal/kzg.js.map +1 -0
  59. package/dist/wasm/internal/kzg.wasm.d.ts +3 -0
  60. package/dist/wasm/internal/kzg.wasm.d.ts.map +1 -0
  61. package/dist/wasm/internal/kzg.wasm.js +10 -0
  62. package/dist/wasm/internal/kzg.wasm.js.map +1 -0
  63. package/dist/wasm/internal/scrypt.d.ts +16 -0
  64. package/dist/wasm/internal/scrypt.d.ts.map +1 -0
  65. package/dist/wasm/internal/scrypt.js +78 -0
  66. package/dist/wasm/internal/scrypt.js.map +1 -0
  67. package/dist/wasm/internal/scrypt.wasm.d.ts +7 -0
  68. package/dist/wasm/internal/scrypt.wasm.d.ts.map +1 -0
  69. package/dist/wasm/internal/scrypt.wasm.js +14 -0
  70. package/dist/wasm/internal/scrypt.wasm.js.map +1 -0
  71. package/dist/wasm/internal/secp256k1.d.ts +16 -0
  72. package/dist/wasm/internal/secp256k1.d.ts.map +1 -0
  73. package/dist/wasm/internal/secp256k1.js +20 -0
  74. package/dist/wasm/internal/secp256k1.js.map +1 -0
  75. package/dist/wasm/internal/secp256k1.wasm.d.ts +3 -0
  76. package/dist/wasm/internal/secp256k1.wasm.d.ts.map +1 -0
  77. package/dist/wasm/internal/secp256k1.wasm.js +10 -0
  78. package/dist/wasm/internal/secp256k1.wasm.js.map +1 -0
  79. package/package.json +19 -2
  80. package/src/core/Hash.ts +59 -0
  81. package/src/core/_test/Block.test.ts +9 -2
  82. package/src/core/_test/Hash.test-d.ts +18 -0
  83. package/src/core/_test/Hash.test.ts +100 -1
  84. package/src/core/_test/Hash.vectors.test.ts +1 -2
  85. package/src/core/_test/Transaction.test.ts +11 -2
  86. package/src/node/Hash.ts +3 -3
  87. package/src/node/_test/Hash.test-d.ts +1 -0
  88. package/src/trusted-setups/Paths.ts +1 -0
  89. package/src/trusted-setups/README.md +39 -0
  90. package/src/trusted-setups/Setups.ts +46 -0
  91. package/src/trusted-setups/_test/Paths.test.ts +16 -0
  92. package/src/trusted-setups/_test/Setups.test-d.ts +7 -0
  93. package/src/trusted-setups/_test/Setups.test.ts +43 -0
  94. package/src/trusted-setups/_test/index.test.ts +1 -0
  95. package/src/trusted-setups/index.ts +21 -0
  96. package/src/trusted-setups/internal/paths.ts +4 -1
  97. package/src/trusted-setups/internal/setups/mainnet.source.json +7 -0
  98. package/src/trusted-setups/internal/setups/mainnet.ts +16 -0
  99. package/src/version.ts +1 -1
  100. package/src/wasm/Engine.ts +8 -6
  101. package/src/wasm/Keystore.bench.ts +26 -0
  102. package/src/wasm/Keystore.ts +32 -14
  103. package/src/wasm/Kzg.bench.ts +63 -0
  104. package/src/wasm/Kzg.ts +518 -0
  105. package/src/wasm/Secp256k1.bench.ts +79 -0
  106. package/src/wasm/Secp256k1.ts +248 -0
  107. package/src/wasm/_test/Engine.test-d.ts +1 -0
  108. package/src/wasm/_test/Hash.browser.test.ts +3 -0
  109. package/src/wasm/_test/Hash.test-d.ts +1 -0
  110. package/src/wasm/_test/Hash.test.ts +2 -2
  111. package/src/wasm/_test/Keystore.browser.test.ts +31 -0
  112. package/src/wasm/_test/Keystore.fuzz.ts +50 -0
  113. package/src/wasm/_test/Keystore.test-d.ts +10 -1
  114. package/src/wasm/_test/Keystore.test.ts +289 -4
  115. package/src/wasm/_test/Kzg.test-d.ts +19 -0
  116. package/src/wasm/_test/Kzg.test.ts +220 -0
  117. package/src/wasm/_test/Runtime.test.ts +9 -0
  118. package/src/wasm/_test/Secp256k1.test-d.ts +35 -0
  119. package/src/wasm/_test/Secp256k1.test.ts +354 -0
  120. package/src/wasm/index.ts +46 -1
  121. package/src/wasm/internal/instantiate.ts +29 -9
  122. package/src/wasm/internal/keystore.ts +19 -0
  123. package/src/wasm/internal/kzg.ts +50 -0
  124. package/src/wasm/internal/kzg.wasm.ts +11 -0
  125. package/src/wasm/internal/scrypt.ts +131 -0
  126. package/src/wasm/internal/scrypt.wasm.ts +17 -0
  127. package/src/wasm/internal/secp256k1.ts +62 -0
  128. package/src/wasm/internal/secp256k1.wasm.ts +11 -0
  129. package/wasm/vendor/c-kzg-4844/LICENSE +201 -0
  130. package/wasm/vendor/c-kzg-4844/blst/LICENSE +201 -0
  131. 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.1.2'
2
+ export const version = '1.2.0'
@@ -2,7 +2,7 @@ import * as CoreEngine from '../core/Engine.js'
2
2
  import type * as Errors from '../core/Errors.js'
3
3
  import * as Ed25519 from './Ed25519.js'
4
4
  import * as Hash from './Hash.js'
5
- import * as Keystore from './Keystore.js'
5
+ import * as keystore from './internal/keystore.js'
6
6
  import * as Mnemonic from './Mnemonic.js'
7
7
  import * as X25519 from './X25519.js'
8
8
  import type * as internal from './internal/instantiate.js'
@@ -54,7 +54,7 @@ export async function install(): Promise<install.ReturnType> {
54
54
  return CoreEngine.install({
55
55
  Ed25519: Ed25519.engine(),
56
56
  Hash: Hash.engine(),
57
- Keystore: Keystore.engine(),
57
+ Keystore: keystore.engine(),
58
58
  Mnemonic: Mnemonic.engine(),
59
59
  X25519: X25519.engine(),
60
60
  })
@@ -91,17 +91,17 @@ export declare namespace install {
91
91
  * @returns An engine, ready to install.
92
92
  */
93
93
  export async function engine(): Promise<engine.ReturnType> {
94
- const [ed25519, hash, keystore, mnemonic, x25519] = await Promise.all([
94
+ const [ed25519, hash, keystoreEngine, mnemonic, x25519] = await Promise.all([
95
95
  Ed25519.engine(),
96
96
  Hash.engine(),
97
- Keystore.engine(),
97
+ keystore.engine(),
98
98
  Mnemonic.engine(),
99
99
  X25519.engine(),
100
100
  ])
101
101
  return {
102
102
  Ed25519: ed25519,
103
103
  Hash: hash,
104
- Keystore: keystore,
104
+ Keystore: keystoreEngine,
105
105
  Mnemonic: mnemonic,
106
106
  X25519: x25519,
107
107
  }
@@ -112,7 +112,9 @@ export declare namespace engine {
112
112
  type ReturnType = {
113
113
  Ed25519: Ed25519.engine.ReturnType
114
114
  Hash: Hash.engine.ReturnType
115
- Keystore: Keystore.engine.ReturnType
115
+ Keystore: {
116
+ pbkdf2Sha256: NonNullable<CoreEngine.Keystore['pbkdf2Sha256']>
117
+ }
116
118
  Mnemonic: Mnemonic.engine.ReturnType
117
119
  X25519: X25519.engine.ReturnType
118
120
  }
@@ -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
+ })
@@ -1,21 +1,33 @@
1
1
  import type * as Engine from '../core/Engine.js'
2
2
  import type * as Errors from '../core/Errors.js'
3
- import * as hashes from './internal/hashes.js'
4
3
  import * as internal from './internal/instantiate.js'
5
- import * as pbkdf2 from './internal/pbkdf2.js'
4
+ import * as keystore from './internal/keystore.js'
5
+ import * as scrypt from './internal/scrypt.js'
6
6
 
7
7
  export * from '../core/Keystore.js'
8
8
  export { MemoryError } from './internal/instantiate.js'
9
9
 
10
10
  /**
11
- * Compiles the WASM implementation of the [`Keystore`](/api/Keystore) PBKDF2
12
- * primitive, without installing it.
11
+ * Compiles the WASM implementations of the [`Keystore`](/api/Keystore)
12
+ * synchronous key-derivation primitives, without installing them.
13
13
  *
14
- * Most callers want
15
- * [`Engine.install`](/wasm/crypto/Engine/install) instead, which compiles every
16
- * implementation this entrypoint provides and installs them in one call. This
17
- * provider deliberately stays synchronous and leaves AES, asynchronous PBKDF2,
18
- * and scrypt on Ox's default implementation.
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.
19
31
  *
20
32
  * @example
21
33
  * ```ts twoslash
@@ -25,16 +37,21 @@ export { MemoryError } from './internal/instantiate.js'
25
37
  *
26
38
  * await Engine.install({ Keystore: Keystore.engine() })
27
39
  *
28
- * Keystore.pbkdf2({ password: 'testpassword' })
40
+ * Keystore.scrypt({ password: 'testpassword' })
29
41
  * ```
30
42
  *
31
- * @returns The WASM implementation of synchronous PBKDF2-HMAC-SHA256.
43
+ * @returns The WASM implementations of synchronous PBKDF2-HMAC-SHA256 and
44
+ * scrypt.
32
45
  */
33
46
  export async function engine(): Promise<engine.ReturnType> {
34
- const module = await hashes.load()
47
+ const [keystoreEngine, scryptModule] = await Promise.all([
48
+ keystore.engine(),
49
+ scrypt.load(),
50
+ ])
35
51
  return {
36
- pbkdf2Sha256: (password, salt, options) =>
37
- pbkdf2.pbkdf2Sha256(module, password, salt, options),
52
+ ...keystoreEngine,
53
+ scrypt: (password, salt, options) =>
54
+ scrypt.derive(scryptModule, password, salt, options),
38
55
  }
39
56
  }
40
57
 
@@ -42,6 +59,7 @@ export declare namespace engine {
42
59
  /** The WASM `Keystore` primitives this module implements. */
43
60
  type ReturnType = {
44
61
  pbkdf2Sha256: NonNullable<Engine.Keystore['pbkdf2Sha256']>
62
+ scrypt: NonNullable<Engine.Keystore['scrypt']>
45
63
  }
46
64
 
47
65
  type ErrorType = internal.MemoryError | Errors.GlobalErrorType
@@ -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
+ })