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.
Files changed (284) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/dist/core/Engine.d.ts +43 -11
  3. package/dist/core/Engine.d.ts.map +1 -1
  4. package/dist/core/Engine.js +40 -10
  5. package/dist/core/Engine.js.map +1 -1
  6. package/dist/core/Hash.d.ts +36 -0
  7. package/dist/core/Hash.d.ts.map +1 -1
  8. package/dist/core/Hash.js +35 -0
  9. package/dist/core/Hash.js.map +1 -1
  10. package/dist/core/internal/engine.d.ts +8 -8
  11. package/dist/core/internal/engine.d.ts.map +1 -1
  12. package/dist/index.d.ts +5 -2
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +5 -2
  15. package/dist/index.js.map +1 -1
  16. package/dist/node/Ed25519.d.ts +33 -0
  17. package/dist/node/Ed25519.d.ts.map +1 -0
  18. package/dist/node/Ed25519.js +67 -0
  19. package/dist/node/Ed25519.js.map +1 -0
  20. package/dist/node/Engine.d.ts +29 -16
  21. package/dist/node/Engine.d.ts.map +1 -1
  22. package/dist/node/Engine.js +38 -14
  23. package/dist/node/Engine.js.map +1 -1
  24. package/dist/node/Hash.d.ts +17 -19
  25. package/dist/node/Hash.d.ts.map +1 -1
  26. package/dist/node/Hash.js +17 -19
  27. package/dist/node/Hash.js.map +1 -1
  28. package/dist/node/Keystore.d.ts +36 -0
  29. package/dist/node/Keystore.d.ts.map +1 -0
  30. package/dist/node/Keystore.js +72 -0
  31. package/dist/node/Keystore.js.map +1 -0
  32. package/dist/node/Mnemonic.d.ts +35 -0
  33. package/dist/node/Mnemonic.d.ts.map +1 -0
  34. package/dist/node/Mnemonic.js +46 -0
  35. package/dist/node/Mnemonic.js.map +1 -0
  36. package/dist/node/P256.d.ts +32 -0
  37. package/dist/node/P256.d.ts.map +1 -0
  38. package/dist/node/P256.js +34 -0
  39. package/dist/node/P256.js.map +1 -0
  40. package/dist/node/X25519.d.ts +31 -0
  41. package/dist/node/X25519.d.ts.map +1 -0
  42. package/dist/node/X25519.js +71 -0
  43. package/dist/node/X25519.js.map +1 -0
  44. package/dist/node/index.d.ts +95 -8
  45. package/dist/node/index.d.ts.map +1 -1
  46. package/dist/node/index.js +95 -8
  47. package/dist/node/index.js.map +1 -1
  48. package/dist/trusted-setups/Paths.d.ts +1 -0
  49. package/dist/trusted-setups/Paths.d.ts.map +1 -1
  50. package/dist/trusted-setups/Paths.js +1 -0
  51. package/dist/trusted-setups/Paths.js.map +1 -1
  52. package/dist/trusted-setups/Setups.d.ts +35 -0
  53. package/dist/trusted-setups/Setups.d.ts.map +1 -0
  54. package/dist/trusted-setups/Setups.js +29 -0
  55. package/dist/trusted-setups/Setups.js.map +1 -0
  56. package/dist/trusted-setups/index.d.ts +20 -0
  57. package/dist/trusted-setups/index.d.ts.map +1 -1
  58. package/dist/trusted-setups/index.js +20 -0
  59. package/dist/trusted-setups/index.js.map +1 -1
  60. package/dist/trusted-setups/internal/paths.d.ts.map +1 -1
  61. package/dist/trusted-setups/internal/paths.js +1 -1
  62. package/dist/trusted-setups/internal/paths.js.map +1 -1
  63. package/dist/trusted-setups/internal/setups/mainnet.d.ts +7 -0
  64. package/dist/trusted-setups/internal/setups/mainnet.d.ts.map +1 -0
  65. package/dist/trusted-setups/internal/setups/mainnet.js +11 -0
  66. package/dist/trusted-setups/internal/setups/mainnet.js.map +1 -0
  67. package/dist/wasm/Ed25519.d.ts +35 -0
  68. package/dist/wasm/Ed25519.d.ts.map +1 -0
  69. package/dist/wasm/Ed25519.js +88 -0
  70. package/dist/wasm/Ed25519.js.map +1 -0
  71. package/dist/wasm/Engine.d.ts +36 -26
  72. package/dist/wasm/Engine.d.ts.map +1 -1
  73. package/dist/wasm/Engine.js +44 -25
  74. package/dist/wasm/Engine.js.map +1 -1
  75. package/dist/wasm/Hash.d.ts +13 -19
  76. package/dist/wasm/Hash.d.ts.map +1 -1
  77. package/dist/wasm/Hash.js +21 -27
  78. package/dist/wasm/Hash.js.map +1 -1
  79. package/dist/wasm/Keystore.d.ts +51 -0
  80. package/dist/wasm/Keystore.d.ts.map +1 -0
  81. package/dist/wasm/Keystore.js +51 -0
  82. package/dist/wasm/Keystore.js.map +1 -0
  83. package/dist/wasm/Kzg.d.ts +112 -0
  84. package/dist/wasm/Kzg.d.ts.map +1 -0
  85. package/dist/wasm/Kzg.js +333 -0
  86. package/dist/wasm/Kzg.js.map +1 -0
  87. package/dist/wasm/Mnemonic.d.ts +33 -0
  88. package/dist/wasm/Mnemonic.d.ts.map +1 -0
  89. package/dist/wasm/Mnemonic.js +30 -0
  90. package/dist/wasm/Mnemonic.js.map +1 -0
  91. package/dist/wasm/Secp256k1.d.ts +55 -0
  92. package/dist/wasm/Secp256k1.d.ts.map +1 -0
  93. package/dist/wasm/Secp256k1.js +181 -0
  94. package/dist/wasm/Secp256k1.js.map +1 -0
  95. package/dist/wasm/X25519.d.ts +33 -0
  96. package/dist/wasm/X25519.d.ts.map +1 -0
  97. package/dist/wasm/X25519.js +53 -0
  98. package/dist/wasm/X25519.js.map +1 -0
  99. package/dist/wasm/index.d.ts +124 -12
  100. package/dist/wasm/index.d.ts.map +1 -1
  101. package/dist/wasm/index.js +124 -12
  102. package/dist/wasm/index.js.map +1 -1
  103. package/dist/wasm/internal/blake3.d.ts +11 -0
  104. package/dist/wasm/internal/blake3.d.ts.map +1 -0
  105. package/dist/wasm/internal/blake3.js +31 -0
  106. package/dist/wasm/internal/blake3.js.map +1 -0
  107. package/dist/wasm/internal/blake3.wasm.d.ts +3 -0
  108. package/dist/wasm/internal/blake3.wasm.d.ts.map +1 -0
  109. package/dist/wasm/internal/blake3.wasm.js +10 -0
  110. package/dist/wasm/internal/blake3.wasm.js.map +1 -0
  111. package/dist/wasm/internal/crypto25519.d.ts +16 -0
  112. package/dist/wasm/internal/crypto25519.d.ts.map +1 -0
  113. package/dist/wasm/internal/crypto25519.js +5 -0
  114. package/dist/wasm/internal/crypto25519.js.map +1 -0
  115. package/dist/wasm/internal/crypto25519.wasm.d.ts +3 -0
  116. package/dist/wasm/internal/crypto25519.wasm.d.ts.map +1 -0
  117. package/dist/wasm/internal/crypto25519.wasm.js +10 -0
  118. package/dist/wasm/internal/crypto25519.wasm.js.map +1 -0
  119. package/dist/wasm/internal/ed25519.d.ts +4 -0
  120. package/dist/wasm/internal/ed25519.d.ts.map +1 -0
  121. package/dist/wasm/internal/ed25519.js +21 -0
  122. package/dist/wasm/internal/ed25519.js.map +1 -0
  123. package/dist/wasm/internal/hashes.d.ts +12 -0
  124. package/dist/wasm/internal/hashes.d.ts.map +1 -0
  125. package/dist/wasm/internal/hashes.js +8 -0
  126. package/dist/wasm/internal/hashes.js.map +1 -0
  127. package/dist/wasm/internal/hashes.wasm.d.ts +4 -2
  128. package/dist/wasm/internal/hashes.wasm.d.ts.map +1 -1
  129. package/dist/wasm/internal/hashes.wasm.js +4 -2
  130. package/dist/wasm/internal/hashes.wasm.js.map +1 -1
  131. package/dist/wasm/internal/instantiate.d.ts +12 -0
  132. package/dist/wasm/internal/instantiate.d.ts.map +1 -1
  133. package/dist/wasm/internal/instantiate.js +23 -7
  134. package/dist/wasm/internal/instantiate.js.map +1 -1
  135. package/dist/wasm/internal/keystore.d.ts +10 -0
  136. package/dist/wasm/internal/keystore.d.ts.map +1 -0
  137. package/dist/wasm/internal/keystore.js +10 -0
  138. package/dist/wasm/internal/keystore.js.map +1 -0
  139. package/dist/wasm/internal/kzg.d.ts +17 -0
  140. package/dist/wasm/internal/kzg.d.ts.map +1 -0
  141. package/dist/wasm/internal/kzg.js +8 -0
  142. package/dist/wasm/internal/kzg.js.map +1 -0
  143. package/dist/wasm/internal/kzg.wasm.d.ts +3 -0
  144. package/dist/wasm/internal/kzg.wasm.d.ts.map +1 -0
  145. package/dist/wasm/internal/kzg.wasm.js +10 -0
  146. package/dist/wasm/internal/kzg.wasm.js.map +1 -0
  147. package/dist/wasm/internal/mnemonic.d.ts +4 -0
  148. package/dist/wasm/internal/mnemonic.d.ts.map +1 -0
  149. package/dist/wasm/internal/mnemonic.js +45 -0
  150. package/dist/wasm/internal/mnemonic.js.map +1 -0
  151. package/dist/wasm/internal/pbkdf2.d.ts +12 -0
  152. package/dist/wasm/internal/pbkdf2.d.ts.map +1 -0
  153. package/dist/wasm/internal/pbkdf2.js +51 -0
  154. package/dist/wasm/internal/pbkdf2.js.map +1 -0
  155. package/dist/wasm/internal/scrypt.d.ts +16 -0
  156. package/dist/wasm/internal/scrypt.d.ts.map +1 -0
  157. package/dist/wasm/internal/scrypt.js +78 -0
  158. package/dist/wasm/internal/scrypt.js.map +1 -0
  159. package/dist/wasm/internal/scrypt.wasm.d.ts +7 -0
  160. package/dist/wasm/internal/scrypt.wasm.d.ts.map +1 -0
  161. package/dist/wasm/internal/scrypt.wasm.js +14 -0
  162. package/dist/wasm/internal/scrypt.wasm.js.map +1 -0
  163. package/dist/wasm/internal/secp256k1.d.ts +16 -0
  164. package/dist/wasm/internal/secp256k1.d.ts.map +1 -0
  165. package/dist/wasm/internal/secp256k1.js +20 -0
  166. package/dist/wasm/internal/secp256k1.js.map +1 -0
  167. package/dist/wasm/internal/secp256k1.wasm.d.ts +3 -0
  168. package/dist/wasm/internal/secp256k1.wasm.d.ts.map +1 -0
  169. package/dist/wasm/internal/secp256k1.wasm.js +10 -0
  170. package/dist/wasm/internal/secp256k1.wasm.js.map +1 -0
  171. package/dist/wasm/internal/x25519.d.ts +4 -0
  172. package/dist/wasm/internal/x25519.d.ts.map +1 -0
  173. package/dist/wasm/internal/x25519.js +21 -0
  174. package/dist/wasm/internal/x25519.js.map +1 -0
  175. package/package.json +64 -2
  176. package/src/core/Engine.ts +84 -11
  177. package/src/core/Hash.ts +59 -0
  178. package/src/core/_test/Block.test.ts +9 -2
  179. package/src/core/_test/Engine.test-d.ts +56 -0
  180. package/src/core/_test/Engine.test.ts +95 -0
  181. package/src/core/_test/Hash.test-d.ts +18 -0
  182. package/src/core/_test/Hash.test.ts +100 -1
  183. package/src/core/_test/Hash.vectors.test.ts +7 -0
  184. package/src/core/_test/Transaction.test.ts +11 -2
  185. package/src/core/internal/engine.ts +8 -8
  186. package/src/index.ts +5 -2
  187. package/src/node/Ed25519.ts +87 -0
  188. package/src/node/Engine.ts +79 -23
  189. package/src/node/Hash.ts +25 -30
  190. package/src/node/Keystore.ts +103 -0
  191. package/src/node/Mnemonic.ts +68 -0
  192. package/src/node/P256.ts +48 -0
  193. package/src/node/X25519.ts +93 -0
  194. package/src/node/_test/Curves.fuzz.ts +76 -0
  195. package/src/node/_test/Ed25519.test-d.ts +35 -0
  196. package/src/node/_test/Ed25519.test.ts +220 -0
  197. package/src/node/_test/Engine.test-d.ts +28 -0
  198. package/src/node/_test/Engine.test.ts +62 -22
  199. package/src/node/_test/Hash.test-d.ts +16 -9
  200. package/src/node/_test/Hash.test.ts +10 -11
  201. package/src/node/_test/Keystore.fuzz.ts +58 -0
  202. package/src/node/_test/Keystore.test-d.ts +43 -0
  203. package/src/node/_test/Keystore.test.ts +198 -0
  204. package/src/node/_test/Mnemonic.fuzz.ts +31 -0
  205. package/src/node/_test/Mnemonic.test-d.ts +21 -0
  206. package/src/node/_test/Mnemonic.test.ts +98 -0
  207. package/src/node/_test/P256.test-d.ts +29 -0
  208. package/src/node/_test/P256.test.ts +102 -0
  209. package/src/node/_test/X25519.test-d.ts +28 -0
  210. package/src/node/_test/X25519.test.ts +239 -0
  211. package/src/node/index.ts +100 -8
  212. package/src/trusted-setups/Paths.ts +1 -0
  213. package/src/trusted-setups/README.md +39 -0
  214. package/src/trusted-setups/Setups.ts +46 -0
  215. package/src/trusted-setups/_test/Paths.test.ts +16 -0
  216. package/src/trusted-setups/_test/Setups.test-d.ts +7 -0
  217. package/src/trusted-setups/_test/Setups.test.ts +43 -0
  218. package/src/trusted-setups/_test/index.test.ts +1 -0
  219. package/src/trusted-setups/index.ts +21 -0
  220. package/src/trusted-setups/internal/paths.ts +4 -1
  221. package/src/trusted-setups/internal/setups/mainnet.source.json +7 -0
  222. package/src/trusted-setups/internal/setups/mainnet.ts +16 -0
  223. package/src/version.ts +1 -1
  224. package/src/wasm/Ed25519.ts +114 -0
  225. package/src/wasm/Engine.ts +78 -31
  226. package/src/wasm/Hash.bench.ts +2 -2
  227. package/src/wasm/Hash.ts +29 -50
  228. package/src/wasm/Keystore.bench.ts +26 -0
  229. package/src/wasm/Keystore.ts +66 -0
  230. package/src/wasm/Kzg.bench.ts +63 -0
  231. package/src/wasm/Kzg.ts +518 -0
  232. package/src/wasm/Mnemonic.ts +44 -0
  233. package/src/wasm/Secp256k1.bench.ts +79 -0
  234. package/src/wasm/Secp256k1.ts +248 -0
  235. package/src/wasm/X25519.ts +70 -0
  236. package/src/wasm/_test/Crypto25519.conformance.ts +57 -0
  237. package/src/wasm/_test/Crypto25519.fuzz.ts +187 -0
  238. package/src/wasm/_test/Ed25519.test-d.ts +28 -0
  239. package/src/wasm/_test/Ed25519.test.ts +168 -0
  240. package/src/wasm/_test/Ed25519.vectors.test.ts +73 -0
  241. package/src/wasm/_test/Engine.test-d.ts +29 -0
  242. package/src/wasm/_test/Engine.test.ts +59 -25
  243. package/src/wasm/_test/Hash.browser.test.ts +14 -7
  244. package/src/wasm/_test/Hash.fuzz.ts +58 -23
  245. package/src/wasm/_test/Hash.test-d.ts +16 -10
  246. package/src/wasm/_test/Hash.test.ts +148 -48
  247. package/src/wasm/_test/Hash.vectors.test.ts +15 -8
  248. package/src/wasm/_test/Keystore.browser.test.ts +73 -0
  249. package/src/wasm/_test/Keystore.fuzz.ts +163 -0
  250. package/src/wasm/_test/Keystore.test-d.ts +42 -0
  251. package/src/wasm/_test/Keystore.test.ts +477 -0
  252. package/src/wasm/_test/Keystore.vectors.test.ts +21 -0
  253. package/src/wasm/_test/Kzg.test-d.ts +19 -0
  254. package/src/wasm/_test/Kzg.test.ts +220 -0
  255. package/src/wasm/_test/Mnemonic.test-d.ts +17 -0
  256. package/src/wasm/_test/Mnemonic.test.ts +110 -0
  257. package/src/wasm/_test/Runtime.test.ts +9 -0
  258. package/src/wasm/_test/Secp256k1.test-d.ts +35 -0
  259. package/src/wasm/_test/Secp256k1.test.ts +354 -0
  260. package/src/wasm/_test/X25519.test-d.ts +21 -0
  261. package/src/wasm/_test/X25519.test.ts +194 -0
  262. package/src/wasm/_test/fixtures.ts +4 -0
  263. package/src/wasm/index.ts +130 -12
  264. package/src/wasm/internal/blake3.ts +45 -0
  265. package/src/wasm/internal/blake3.wasm.ts +11 -0
  266. package/src/wasm/internal/crypto25519.ts +43 -0
  267. package/src/wasm/internal/crypto25519.wasm.ts +11 -0
  268. package/src/wasm/internal/ed25519.ts +31 -0
  269. package/src/wasm/internal/hashes.ts +29 -0
  270. package/src/wasm/internal/hashes.wasm.ts +5 -2
  271. package/src/wasm/internal/instantiate.ts +29 -9
  272. package/src/wasm/internal/keystore.ts +19 -0
  273. package/src/wasm/internal/kzg.ts +50 -0
  274. package/src/wasm/internal/kzg.wasm.ts +11 -0
  275. package/src/wasm/internal/mnemonic.ts +56 -0
  276. package/src/wasm/internal/pbkdf2.ts +86 -0
  277. package/src/wasm/internal/scrypt.ts +131 -0
  278. package/src/wasm/internal/scrypt.wasm.ts +17 -0
  279. package/src/wasm/internal/secp256k1.ts +62 -0
  280. package/src/wasm/internal/secp256k1.wasm.ts +11 -0
  281. package/src/wasm/internal/x25519.ts +29 -0
  282. package/wasm/vendor/c-kzg-4844/LICENSE +201 -0
  283. package/wasm/vendor/c-kzg-4844/blst/LICENSE +201 -0
  284. 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.0'
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
+ }
@@ -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 and installs it.
32
+ * Compiles and installs every WASM implementation this entrypoint provides.
8
33
  *
9
- * WASM must be compiled asynchronously, so this is where the `await` lives.
10
- * Everything afterwards, including the hashing itself, is synchronous.
34
+ * Slot compilation starts concurrently. Nothing is installed until every slot
35
+ * resolves successfully.
11
36
  *
12
- * Call it once, during startup, before any crypto call. ox resolves the engine
13
- * at call time, so values computed beforehand used whatever was installed then.
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.load()
46
+ * await Engine.install()
22
47
  *
23
- * Hash.keccak256('0xdeadbeef')
48
+ * Hash.sha256('0xdeadbeef')
24
49
  * ```
25
50
  *
26
51
  * @returns The engine that was installed.
27
52
  */
28
- export async function load(): Promise<load.ReturnType> {
29
- const engine = await create()
30
- CoreEngine.set(engine)
31
- return engine
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 load {
35
- type ReturnType = create.ReturnType
63
+ export declare namespace install {
64
+ type ReturnType = engine.ReturnType
36
65
 
37
66
  type ErrorType =
38
- | create.ErrorType
39
- | CoreEngine.set.ErrorType
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 installing
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: measuring one
48
- * implementation against another, or installing through
49
- * [`Engine.with`](/api/Engine/with) for the duration of a call. Combining
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.create()
86
+ * const wasm = await Wasm.engine()
61
87
  *
62
- * Engine.with(wasm, () => Hash.keccak256('0xdeadbeef'))
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 create(): Promise<create.ReturnType> {
68
- return { ...(await Hash.create()) }
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 create {
110
+ export declare namespace engine {
72
111
  /** Every slot this entrypoint supplies, each with its primitives present. */
73
- type ReturnType = Hash.create.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
  }
@@ -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 { create as createWasm } from './Hash.js'
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 = (await createWasm()).Hash
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 { wasmBase64 } from './internal/hashes.wasm.js'
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
- * Compilation is memoized; the engine object deliberately is not.
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 {@link ox#Engine.load} instead, which compiles every
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, Hash } from 'ox'
50
- * import * as WasmHash from 'ox/wasm/Hash'
33
+ * import { Engine } from 'ox'
34
+ * import { Hash } from 'ox/wasm'
51
35
  *
52
- * Engine.set(await WasmHash.create())
36
+ * await Engine.install({ Hash: Hash.engine() })
53
37
  *
54
38
  * Hash.keccak256('0xdeadbeef')
55
39
  * ```
56
40
  *
57
- * @returns An engine supplying the `Hash` slot.
41
+ * @returns The WASM implementation of the `Hash` slot.
58
42
  */
59
- export async function create(): Promise<create.ReturnType> {
43
+ export async function engine(): Promise<engine.ReturnType> {
60
44
  {
61
- const module = await instantiate()
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
- Hash: {
81
- hmacSha256: (key, message) => hash.hmacSha256(module, key, message),
82
- keccak256: (input) => call('keccak256', input),
83
- ripemd160: (input) => call('ripemd160', input),
84
- sha256: (input) => call('sha256', input),
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 create {
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
- Hash: {
100
- [key in
101
- | 'hmacSha256'
102
- | 'keccak256'
103
- | 'ripemd160'
104
- | 'sha256']-?: NonNullable<Engine.Hash[key]>
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
+ })