ox 1.0.5 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (337) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/dist/core/Base64.d.ts +10 -21
  3. package/dist/core/Base64.d.ts.map +1 -1
  4. package/dist/core/Base64.js +8 -127
  5. package/dist/core/Base64.js.map +1 -1
  6. package/dist/core/BinaryStateTree.d.ts.map +1 -1
  7. package/dist/core/BinaryStateTree.js +2 -2
  8. package/dist/core/BinaryStateTree.js.map +1 -1
  9. package/dist/core/Bls.d.ts.map +1 -1
  10. package/dist/core/Bls.js +19 -44
  11. package/dist/core/Bls.js.map +1 -1
  12. package/dist/core/Ed25519.d.ts.map +1 -1
  13. package/dist/core/Ed25519.js +7 -6
  14. package/dist/core/Ed25519.js.map +1 -1
  15. package/dist/core/Engine.d.ts +156 -0
  16. package/dist/core/Engine.d.ts.map +1 -0
  17. package/dist/core/Engine.js +210 -0
  18. package/dist/core/Engine.js.map +1 -0
  19. package/dist/core/Hash.d.ts +4 -4
  20. package/dist/core/Hash.d.ts.map +1 -1
  21. package/dist/core/Hash.js +9 -12
  22. package/dist/core/Hash.js.map +1 -1
  23. package/dist/core/Keystore.d.ts.map +1 -1
  24. package/dist/core/Keystore.js +15 -10
  25. package/dist/core/Keystore.js.map +1 -1
  26. package/dist/core/Mnemonic.d.ts.map +1 -1
  27. package/dist/core/Mnemonic.js +3 -2
  28. package/dist/core/Mnemonic.js.map +1 -1
  29. package/dist/core/P256.d.ts.map +1 -1
  30. package/dist/core/P256.js +8 -9
  31. package/dist/core/P256.js.map +1 -1
  32. package/dist/core/Secp256k1.d.ts.map +1 -1
  33. package/dist/core/Secp256k1.js +8 -9
  34. package/dist/core/Secp256k1.js.map +1 -1
  35. package/dist/core/WebCryptoP256.d.ts.map +1 -1
  36. package/dist/core/WebCryptoP256.js +2 -2
  37. package/dist/core/WebCryptoP256.js.map +1 -1
  38. package/dist/core/X25519.d.ts.map +1 -1
  39. package/dist/core/X25519.js +4 -3
  40. package/dist/core/X25519.js.map +1 -1
  41. package/dist/core/internal/bls.d.ts +12 -0
  42. package/dist/core/internal/bls.d.ts.map +1 -0
  43. package/dist/core/internal/bls.js +79 -0
  44. package/dist/core/internal/bls.js.map +1 -0
  45. package/dist/core/internal/codec/base64.d.ts +84 -0
  46. package/dist/core/internal/codec/base64.d.ts.map +1 -0
  47. package/dist/core/internal/codec/base64.js +177 -0
  48. package/dist/core/internal/codec/base64.js.map +1 -0
  49. package/dist/core/internal/codec/hex.d.ts +49 -4
  50. package/dist/core/internal/codec/hex.d.ts.map +1 -1
  51. package/dist/core/internal/codec/hex.js +109 -35
  52. package/dist/core/internal/codec/hex.js.map +1 -1
  53. package/dist/core/internal/curves.d.ts +17 -0
  54. package/dist/core/internal/curves.d.ts.map +1 -0
  55. package/dist/core/internal/curves.js +17 -0
  56. package/dist/core/internal/curves.js.map +1 -0
  57. package/dist/core/internal/ed25519.d.ts +14 -0
  58. package/dist/core/internal/ed25519.d.ts.map +1 -0
  59. package/dist/core/internal/ed25519.js +29 -0
  60. package/dist/core/internal/ed25519.js.map +1 -0
  61. package/dist/core/internal/engine.d.ts +278 -0
  62. package/dist/core/internal/engine.d.ts.map +1 -0
  63. package/dist/core/internal/engine.js +131 -0
  64. package/dist/core/internal/engine.js.map +1 -0
  65. package/dist/core/internal/hash.d.ts +12 -0
  66. package/dist/core/internal/hash.d.ts.map +1 -0
  67. package/dist/core/internal/hash.js +30 -0
  68. package/dist/core/internal/hash.js.map +1 -0
  69. package/dist/core/internal/keystore.d.ts +14 -0
  70. package/dist/core/internal/keystore.d.ts.map +1 -0
  71. package/dist/core/internal/keystore.js +32 -0
  72. package/dist/core/internal/keystore.js.map +1 -0
  73. package/dist/core/internal/mnemonic.d.ts +4 -0
  74. package/dist/core/internal/mnemonic.d.ts.map +1 -0
  75. package/dist/core/internal/mnemonic.js +14 -0
  76. package/dist/core/internal/mnemonic.js.map +1 -0
  77. package/dist/core/internal/p256.d.ts +14 -0
  78. package/dist/core/internal/p256.d.ts.map +1 -0
  79. package/dist/core/internal/p256.js +36 -0
  80. package/dist/core/internal/p256.js.map +1 -0
  81. package/dist/core/internal/secp256k1.d.ts +14 -0
  82. package/dist/core/internal/secp256k1.d.ts.map +1 -0
  83. package/dist/core/internal/secp256k1.js +36 -0
  84. package/dist/core/internal/secp256k1.js.map +1 -0
  85. package/dist/core/internal/x25519.d.ts +8 -0
  86. package/dist/core/internal/x25519.d.ts.map +1 -0
  87. package/dist/core/internal/x25519.js +20 -0
  88. package/dist/core/internal/x25519.js.map +1 -0
  89. package/dist/index.d.ts +38 -0
  90. package/dist/index.d.ts.map +1 -1
  91. package/dist/index.docs.d.ts +2 -0
  92. package/dist/index.docs.d.ts.map +1 -1
  93. package/dist/index.docs.js +2 -0
  94. package/dist/index.docs.js.map +1 -1
  95. package/dist/index.js +38 -0
  96. package/dist/index.js.map +1 -1
  97. package/dist/node/Ed25519.d.ts +33 -0
  98. package/dist/node/Ed25519.d.ts.map +1 -0
  99. package/dist/node/Ed25519.js +67 -0
  100. package/dist/node/Ed25519.js.map +1 -0
  101. package/dist/node/Engine.d.ts +68 -0
  102. package/dist/node/Engine.d.ts.map +1 -0
  103. package/dist/node/Engine.js +75 -0
  104. package/dist/node/Engine.js.map +1 -0
  105. package/dist/node/Hash.d.ts +37 -0
  106. package/dist/node/Hash.d.ts.map +1 -0
  107. package/dist/node/Hash.js +35 -0
  108. package/dist/node/Hash.js.map +1 -0
  109. package/dist/node/Keystore.d.ts +36 -0
  110. package/dist/node/Keystore.d.ts.map +1 -0
  111. package/dist/node/Keystore.js +72 -0
  112. package/dist/node/Keystore.js.map +1 -0
  113. package/dist/node/Mnemonic.d.ts +35 -0
  114. package/dist/node/Mnemonic.d.ts.map +1 -0
  115. package/dist/node/Mnemonic.js +46 -0
  116. package/dist/node/Mnemonic.js.map +1 -0
  117. package/dist/node/P256.d.ts +32 -0
  118. package/dist/node/P256.d.ts.map +1 -0
  119. package/dist/node/P256.js +34 -0
  120. package/dist/node/P256.js.map +1 -0
  121. package/dist/node/X25519.d.ts +31 -0
  122. package/dist/node/X25519.d.ts.map +1 -0
  123. package/dist/node/X25519.js +71 -0
  124. package/dist/node/X25519.js.map +1 -0
  125. package/dist/node/index.d.ts +126 -0
  126. package/dist/node/index.d.ts.map +1 -0
  127. package/dist/node/index.js +126 -0
  128. package/dist/node/index.js.map +1 -0
  129. package/dist/tempo/VirtualMaster.d.ts +2 -0
  130. package/dist/tempo/VirtualMaster.d.ts.map +1 -1
  131. package/dist/tempo/VirtualMaster.js +19 -5
  132. package/dist/tempo/VirtualMaster.js.map +1 -1
  133. package/dist/tempo/internal/mine.wasm.d.ts +2 -4
  134. package/dist/tempo/internal/mine.wasm.d.ts.map +1 -1
  135. package/dist/tempo/internal/mine.wasm.js +7 -12
  136. package/dist/tempo/internal/mine.wasm.js.map +1 -1
  137. package/dist/tempo/internal/virtualMasterPool.js +4 -1
  138. package/dist/tempo/internal/virtualMasterPool.js.map +1 -1
  139. package/dist/wasm/Ed25519.d.ts +35 -0
  140. package/dist/wasm/Ed25519.d.ts.map +1 -0
  141. package/dist/wasm/Ed25519.js +88 -0
  142. package/dist/wasm/Ed25519.js.map +1 -0
  143. package/dist/wasm/Engine.d.ts +71 -0
  144. package/dist/wasm/Engine.d.ts.map +1 -0
  145. package/dist/wasm/Engine.js +76 -0
  146. package/dist/wasm/Engine.js.map +1 -0
  147. package/dist/wasm/Hash.d.ts +43 -0
  148. package/dist/wasm/Hash.d.ts.map +1 -0
  149. package/dist/wasm/Hash.js +64 -0
  150. package/dist/wasm/Hash.js.map +1 -0
  151. package/dist/wasm/Keystore.d.ts +37 -0
  152. package/dist/wasm/Keystore.d.ts.map +1 -0
  153. package/dist/wasm/Keystore.js +34 -0
  154. package/dist/wasm/Keystore.js.map +1 -0
  155. package/dist/wasm/Mnemonic.d.ts +33 -0
  156. package/dist/wasm/Mnemonic.d.ts.map +1 -0
  157. package/dist/wasm/Mnemonic.js +30 -0
  158. package/dist/wasm/Mnemonic.js.map +1 -0
  159. package/dist/wasm/X25519.d.ts +33 -0
  160. package/dist/wasm/X25519.d.ts.map +1 -0
  161. package/dist/wasm/X25519.js +53 -0
  162. package/dist/wasm/X25519.js.map +1 -0
  163. package/dist/wasm/index.d.ts +115 -0
  164. package/dist/wasm/index.d.ts.map +1 -0
  165. package/dist/wasm/index.js +115 -0
  166. package/dist/wasm/index.js.map +1 -0
  167. package/dist/wasm/internal/blake3.d.ts +11 -0
  168. package/dist/wasm/internal/blake3.d.ts.map +1 -0
  169. package/dist/wasm/internal/blake3.js +31 -0
  170. package/dist/wasm/internal/blake3.js.map +1 -0
  171. package/dist/wasm/internal/blake3.wasm.d.ts +3 -0
  172. package/dist/wasm/internal/blake3.wasm.d.ts.map +1 -0
  173. package/dist/wasm/internal/blake3.wasm.js +10 -0
  174. package/dist/wasm/internal/blake3.wasm.js.map +1 -0
  175. package/dist/wasm/internal/crypto25519.d.ts +16 -0
  176. package/dist/wasm/internal/crypto25519.d.ts.map +1 -0
  177. package/dist/wasm/internal/crypto25519.js +5 -0
  178. package/dist/wasm/internal/crypto25519.js.map +1 -0
  179. package/dist/wasm/internal/crypto25519.wasm.d.ts +3 -0
  180. package/dist/wasm/internal/crypto25519.wasm.d.ts.map +1 -0
  181. package/dist/wasm/internal/crypto25519.wasm.js +10 -0
  182. package/dist/wasm/internal/crypto25519.wasm.js.map +1 -0
  183. package/dist/wasm/internal/ed25519.d.ts +4 -0
  184. package/dist/wasm/internal/ed25519.d.ts.map +1 -0
  185. package/dist/wasm/internal/ed25519.js +21 -0
  186. package/dist/wasm/internal/ed25519.js.map +1 -0
  187. package/dist/wasm/internal/hash.d.ts +9 -0
  188. package/dist/wasm/internal/hash.d.ts.map +1 -0
  189. package/dist/wasm/internal/hash.js +26 -0
  190. package/dist/wasm/internal/hash.js.map +1 -0
  191. package/dist/wasm/internal/hashes.d.ts +12 -0
  192. package/dist/wasm/internal/hashes.d.ts.map +1 -0
  193. package/dist/wasm/internal/hashes.js +8 -0
  194. package/dist/wasm/internal/hashes.js.map +1 -0
  195. package/dist/wasm/internal/hashes.wasm.d.ts +7 -0
  196. package/dist/wasm/internal/hashes.wasm.d.ts.map +1 -0
  197. package/dist/wasm/internal/hashes.wasm.js +14 -0
  198. package/dist/wasm/internal/hashes.wasm.js.map +1 -0
  199. package/dist/wasm/internal/instantiate.d.ts +56 -0
  200. package/dist/wasm/internal/instantiate.d.ts.map +1 -0
  201. package/dist/wasm/internal/instantiate.js +92 -0
  202. package/dist/wasm/internal/instantiate.js.map +1 -0
  203. package/dist/wasm/internal/mnemonic.d.ts +4 -0
  204. package/dist/wasm/internal/mnemonic.d.ts.map +1 -0
  205. package/dist/wasm/internal/mnemonic.js +45 -0
  206. package/dist/wasm/internal/mnemonic.js.map +1 -0
  207. package/dist/wasm/internal/pbkdf2.d.ts +12 -0
  208. package/dist/wasm/internal/pbkdf2.d.ts.map +1 -0
  209. package/dist/wasm/internal/pbkdf2.js +51 -0
  210. package/dist/wasm/internal/pbkdf2.js.map +1 -0
  211. package/dist/wasm/internal/x25519.d.ts +4 -0
  212. package/dist/wasm/internal/x25519.d.ts.map +1 -0
  213. package/dist/wasm/internal/x25519.js +21 -0
  214. package/dist/wasm/internal/x25519.js.map +1 -0
  215. package/dist/zod/TypedData.d.ts +12 -12
  216. package/package.json +81 -1
  217. package/src/core/Base64.ts +19 -166
  218. package/src/core/BinaryStateTree.ts +2 -3
  219. package/src/core/Bls.ts +27 -77
  220. package/src/core/Ed25519.ts +7 -7
  221. package/src/core/Engine.ts +308 -0
  222. package/src/core/Hash.ts +9 -12
  223. package/src/core/Keystore.ts +17 -17
  224. package/src/core/Mnemonic.ts +3 -6
  225. package/src/core/P256.ts +15 -24
  226. package/src/core/Rlp_tx.bench.ts +2 -4
  227. package/src/core/Secp256k1.ts +9 -14
  228. package/src/core/TxEnvelopeEip1559.bench.ts +0 -5
  229. package/src/core/WebCryptoP256.ts +2 -2
  230. package/src/core/X25519.ts +4 -3
  231. package/src/core/_test/AbiParameters.adversarial.fuzz.ts +1 -2
  232. package/src/core/_test/AbiParameters.fuzz.ts +1 -2
  233. package/src/core/_test/Base.fuzz.ts +1 -2
  234. package/src/core/_test/Cbor.fuzz.ts +1 -2
  235. package/src/core/_test/Engine.fuzz.ts +293 -0
  236. package/src/core/_test/Engine.test-d.ts +140 -0
  237. package/src/core/_test/Engine.test.ts +618 -0
  238. package/src/core/_test/Hash.vectors.test.ts +64 -0
  239. package/src/core/_test/Hex.fuzz.ts +1 -2
  240. package/src/core/_test/Rlp.fuzz.ts +1 -2
  241. package/src/core/_test/Signature.fuzz.ts +1 -2
  242. package/src/core/_test/curves.test.ts +7 -0
  243. package/src/core/_test/index.test.ts +1 -0
  244. package/src/core/internal/bls.ts +121 -0
  245. package/src/core/internal/codec/_test/base64.conformance.ts +156 -0
  246. package/src/core/internal/codec/_test/hex.conformance.ts +204 -0
  247. package/src/core/internal/codec/_test/hex.fuzz.ts +234 -0
  248. package/src/core/internal/codec/_test/hex.test.ts +124 -0
  249. package/src/core/internal/codec/base64.ts +222 -0
  250. package/src/core/internal/codec/hex.ts +115 -34
  251. package/src/core/internal/curves.ts +18 -0
  252. package/src/core/internal/ed25519.ts +64 -0
  253. package/src/core/internal/engine.ts +410 -0
  254. package/src/core/internal/hash.ts +48 -0
  255. package/src/core/internal/keystore.ts +107 -0
  256. package/src/core/internal/mnemonic.ts +18 -0
  257. package/src/core/internal/p256.ts +91 -0
  258. package/src/core/internal/secp256k1.ts +91 -0
  259. package/src/core/internal/x25519.ts +40 -0
  260. package/src/index.docs.ts +2 -0
  261. package/src/index.ts +39 -0
  262. package/src/node/Ed25519.ts +87 -0
  263. package/src/node/Engine.ts +125 -0
  264. package/src/node/Hash.ts +51 -0
  265. package/src/node/Keystore.ts +103 -0
  266. package/src/node/Mnemonic.ts +68 -0
  267. package/src/node/P256.ts +48 -0
  268. package/src/node/X25519.ts +93 -0
  269. package/src/node/_test/Curves.fuzz.ts +76 -0
  270. package/src/node/_test/Ed25519.test-d.ts +35 -0
  271. package/src/node/_test/Ed25519.test.ts +220 -0
  272. package/src/node/_test/Engine.test-d.ts +28 -0
  273. package/src/node/_test/Engine.test.ts +102 -0
  274. package/src/node/_test/Hash.test-d.ts +32 -0
  275. package/src/node/_test/Hash.test.ts +101 -0
  276. package/src/node/_test/Keystore.fuzz.ts +58 -0
  277. package/src/node/_test/Keystore.test-d.ts +43 -0
  278. package/src/node/_test/Keystore.test.ts +198 -0
  279. package/src/node/_test/Mnemonic.fuzz.ts +31 -0
  280. package/src/node/_test/Mnemonic.test-d.ts +21 -0
  281. package/src/node/_test/Mnemonic.test.ts +98 -0
  282. package/src/node/_test/P256.test-d.ts +29 -0
  283. package/src/node/_test/P256.test.ts +102 -0
  284. package/src/node/_test/X25519.test-d.ts +28 -0
  285. package/src/node/_test/X25519.test.ts +239 -0
  286. package/src/node/index.ts +132 -0
  287. package/src/tempo/VirtualMaster.test.ts +75 -0
  288. package/src/tempo/VirtualMaster.ts +28 -6
  289. package/src/tempo/internal/mine.wasm.ts +7 -13
  290. package/src/tempo/internal/virtualMasterPool.ts +4 -1
  291. package/src/tsconfig.json +2 -1
  292. package/src/version.ts +1 -1
  293. package/src/wasm/Ed25519.ts +114 -0
  294. package/src/wasm/Engine.ts +121 -0
  295. package/src/wasm/Hash.bench.ts +60 -0
  296. package/src/wasm/Hash.ts +88 -0
  297. package/src/wasm/Keystore.ts +48 -0
  298. package/src/wasm/Mnemonic.ts +44 -0
  299. package/src/wasm/X25519.ts +70 -0
  300. package/src/wasm/_test/Crypto25519.conformance.ts +57 -0
  301. package/src/wasm/_test/Crypto25519.fuzz.ts +187 -0
  302. package/src/wasm/_test/Ed25519.test-d.ts +28 -0
  303. package/src/wasm/_test/Ed25519.test.ts +168 -0
  304. package/src/wasm/_test/Ed25519.vectors.test.ts +73 -0
  305. package/src/wasm/_test/Engine.test-d.ts +28 -0
  306. package/src/wasm/_test/Engine.test.ts +96 -0
  307. package/src/wasm/_test/Hash.browser.test.ts +54 -0
  308. package/src/wasm/_test/Hash.fuzz.ts +223 -0
  309. package/src/wasm/_test/Hash.test-d.ts +33 -0
  310. package/src/wasm/_test/Hash.test.ts +311 -0
  311. package/src/wasm/_test/Hash.vectors.test.ts +124 -0
  312. package/src/wasm/_test/Keystore.browser.test.ts +42 -0
  313. package/src/wasm/_test/Keystore.fuzz.ts +113 -0
  314. package/src/wasm/_test/Keystore.test-d.ts +33 -0
  315. package/src/wasm/_test/Keystore.test.ts +192 -0
  316. package/src/wasm/_test/Keystore.vectors.test.ts +21 -0
  317. package/src/wasm/_test/Mnemonic.test-d.ts +17 -0
  318. package/src/wasm/_test/Mnemonic.test.ts +110 -0
  319. package/src/wasm/_test/Runtime.test.ts +21 -0
  320. package/src/wasm/_test/X25519.test-d.ts +21 -0
  321. package/src/wasm/_test/X25519.test.ts +194 -0
  322. package/src/wasm/_test/fixtures.ts +4 -0
  323. package/src/wasm/_test/runtime.wasm.ts +11 -0
  324. package/src/wasm/index.ts +120 -0
  325. package/src/wasm/internal/blake3.ts +45 -0
  326. package/src/wasm/internal/blake3.wasm.ts +11 -0
  327. package/src/wasm/internal/crypto25519.ts +43 -0
  328. package/src/wasm/internal/crypto25519.wasm.ts +11 -0
  329. package/src/wasm/internal/ed25519.ts +31 -0
  330. package/src/wasm/internal/hash.ts +50 -0
  331. package/src/wasm/internal/hashes.ts +29 -0
  332. package/src/wasm/internal/hashes.wasm.ts +17 -0
  333. package/src/wasm/internal/instantiate.ts +135 -0
  334. package/src/wasm/internal/mnemonic.ts +56 -0
  335. package/src/wasm/internal/pbkdf2.ts +86 -0
  336. package/src/wasm/internal/x25519.ts +29 -0
  337. package/src/tempo/internal/mine.c +0 -182
@@ -0,0 +1,135 @@
1
+ import * as Errors from '../../core/Errors.js'
2
+
3
+ /**
4
+ * Shared WASM instantiation and memory plumbing.
5
+ *
6
+ * @internal
7
+ */
8
+
9
+ /** An instantiated WASM module, with helpers for moving bytes across. */
10
+ export type Module<exports extends Record<string, unknown>> = {
11
+ /** The module's exported functions. */
12
+ exports: exports
13
+ /** Address of the first byte we may write to. */
14
+ heapBase: number
15
+ /**
16
+ * Grows linear memory so `bytes` are available above `heapBase`.
17
+ *
18
+ * @throws {@link MemoryError} when the runtime refuses to grow.
19
+ */
20
+ reserve(bytes: number): void
21
+ /**
22
+ * A view over the module's current memory.
23
+ *
24
+ * Must be called after {@link Module.reserve} and again after anything that
25
+ * could grow memory: `WebAssembly.Memory.grow` detaches the previous
26
+ * `ArrayBuffer`, which silently turns any retained view into a zero-length one.
27
+ */
28
+ view(): Uint8Array
29
+ }
30
+
31
+ const pageSize = 65_536
32
+
33
+ function decode(base64: string): Uint8Array {
34
+ // `Uint8Array.fromBase64` where available, then Node/Bun, then browsers and
35
+ // edge runtimes.
36
+ const fromBase64 = (
37
+ Uint8Array as unknown as {
38
+ fromBase64?: ((value: string) => Uint8Array) | undefined
39
+ }
40
+ ).fromBase64
41
+ if (fromBase64) return fromBase64(base64)
42
+ if (typeof Buffer !== 'undefined')
43
+ return new Uint8Array(Buffer.from(base64, 'base64'))
44
+ const binary = atob(base64)
45
+ const bytes = new Uint8Array(binary.length)
46
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i)
47
+ return bytes
48
+ }
49
+
50
+ /**
51
+ * Instantiates a base64-encoded WASM module.
52
+ *
53
+ * Always asynchronous: browsers refuse to compile modules larger than a few
54
+ * kilobytes synchronously on the main thread, so there is no synchronous path to
55
+ * fall back to. Callers await this once and then call the module synchronously.
56
+ *
57
+ * @internal
58
+ */
59
+ export async function instantiate<exports extends Record<string, unknown>>(
60
+ base64: string,
61
+ ): Promise<Module<exports>> {
62
+ const { instance } = await WebAssembly.instantiate(decode(base64), {})
63
+ const exports = instance.exports as {
64
+ heap_base(): number
65
+ memory: WebAssembly.Memory
66
+ } & exports
67
+
68
+ const { memory } = exports
69
+ const heapBase = exports.heap_base()
70
+
71
+ let cached = new Uint8Array(memory.buffer)
72
+
73
+ return {
74
+ exports,
75
+ heapBase,
76
+ reserve(bytes) {
77
+ const required = heapBase + bytes
78
+ if (memory.buffer.byteLength >= required) return
79
+ const pages = Math.ceil((required - memory.buffer.byteLength) / pageSize)
80
+ try {
81
+ memory.grow(pages)
82
+ } catch (cause) {
83
+ throw new MemoryError({ bytes: required, cause: cause as Error })
84
+ }
85
+ cached = new Uint8Array(memory.buffer)
86
+ },
87
+ view() {
88
+ // `grow` detaches the old buffer, so compare identity rather than trusting
89
+ // the cached view.
90
+ if (cached.buffer !== memory.buffer)
91
+ cached = new Uint8Array(memory.buffer)
92
+ return cached
93
+ },
94
+ }
95
+ }
96
+
97
+ /**
98
+ * Memoizes a loader so concurrent callers compile the module once.
99
+ *
100
+ * The promise is memoized rather than its result, so a second call made while
101
+ * the first is still compiling awaits the same compilation. A rejected attempt
102
+ * is discarded, so a later call can try again.
103
+ *
104
+ * @internal
105
+ */
106
+ export function memoize<value>(
107
+ load: () => Promise<value>,
108
+ ): () => Promise<value> {
109
+ let cached: Promise<value> | undefined
110
+ return () => {
111
+ // Only success is memoized. Compilation can fail for reasons that pass --
112
+ // a runtime briefly out of memory, say -- and caching the rejection would
113
+ // make one bad moment permanent for the life of the process.
114
+ cached ??= load().catch((error) => {
115
+ cached = undefined
116
+ throw error
117
+ })
118
+ return cached
119
+ }
120
+ }
121
+
122
+ /** Thrown when a WASM module cannot grow its memory to the required size. */
123
+ export class MemoryError extends Errors.BaseError<Error> {
124
+ override readonly name = 'Wasm.MemoryError'
125
+
126
+ constructor({ bytes, cause }: { bytes: number; cause: Error }) {
127
+ super('WASM memory could not be grown to the required size.', {
128
+ cause,
129
+ metaMessages: [
130
+ `Required: ${bytes} bytes`,
131
+ 'The input may be too large for this runtime. Reset the engine slot with `Engine.reset` to fall back to the default implementation.',
132
+ ],
133
+ })
134
+ }
135
+ }
@@ -0,0 +1,56 @@
1
+ import type { Module } from './crypto25519.js'
2
+
3
+ const encoder = new TextEncoder()
4
+ const seedSize = 64
5
+ const wordCounts = [12, 15, 18, 21, 24]
6
+
7
+ /** Derives a BIP-39 seed and clears encoded and staged secret material. */
8
+ export function toSeed(
9
+ module: Module,
10
+ mnemonic: string,
11
+ passphrase = '',
12
+ ): Uint8Array {
13
+ const password = encoder.encode(normalize(mnemonic))
14
+ try {
15
+ const salt = encoder.encode(normalizeSalt(passphrase))
16
+ try {
17
+ const size = password.length + salt.length + seedSize
18
+ module.reserve(size)
19
+ const passwordPtr = module.heapBase
20
+ const saltPtr = passwordPtr + password.length
21
+ const seedPtr = saltPtr + salt.length
22
+ try {
23
+ const memory = module.view()
24
+ memory.set(password, passwordPtr)
25
+ memory.set(salt, saltPtr)
26
+ module.exports.mnemonic_to_seed(
27
+ passwordPtr,
28
+ password.length,
29
+ saltPtr,
30
+ salt.length,
31
+ seedPtr,
32
+ )
33
+ return module.view().slice(seedPtr, seedPtr + seedSize)
34
+ } finally {
35
+ module.exports.zero(passwordPtr, size)
36
+ }
37
+ } finally {
38
+ salt.fill(0)
39
+ }
40
+ } finally {
41
+ password.fill(0)
42
+ }
43
+ }
44
+
45
+ function normalize(mnemonic: string): string {
46
+ if (typeof mnemonic !== 'string')
47
+ throw new TypeError(`invalid mnemonic type: ${typeof mnemonic}`)
48
+ const normalized = mnemonic.normalize('NFKD')
49
+ if (!wordCounts.includes(normalized.split(' ').length))
50
+ throw new Error('Invalid mnemonic')
51
+ return normalized
52
+ }
53
+
54
+ function normalizeSalt(passphrase: string): string {
55
+ return `mnemonic${passphrase}`.normalize('NFKD')
56
+ }
@@ -0,0 +1,86 @@
1
+ import { pbkdf2Sha256ScratchSize } from './hashes.wasm.js'
2
+ import { MemoryError, type Module } from './instantiate.js'
3
+
4
+ /** WASM exports used by the PBKDF2-HMAC-SHA256 loader. @internal */
5
+ export type Exports = {
6
+ pbkdf2_sha256(
7
+ password: number,
8
+ passwordLength: number,
9
+ salt: number,
10
+ saltLength: number,
11
+ iterations: number,
12
+ out: number,
13
+ outLength: number,
14
+ scratch: number,
15
+ ): void
16
+ zero(ptr: number, length: number): void
17
+ }
18
+
19
+ const maximumUint32 = 0xffff_ffff
20
+ const wasm32Size = 0x1_0000_0000
21
+
22
+ /** Calls PBKDF2-HMAC-SHA256 and clears its complete linear-memory region. */
23
+ export function pbkdf2Sha256(
24
+ module: Module<Exports>,
25
+ password: Uint8Array,
26
+ salt: Uint8Array,
27
+ options: { c: number; dkLen: number },
28
+ ): Uint8Array {
29
+ const { c, dkLen } = options
30
+ assertPositiveUint32(c, 'c')
31
+ assertPositiveUint32(dkLen, 'dkLen')
32
+
33
+ const passwordLength = password.length
34
+ const saltLength = salt.length
35
+ if (saltLength > maximumUint32 - 4)
36
+ throw new RangeError(
37
+ `"salt" length must be <= ${maximumUint32 - 4}, got ${saltLength}`,
38
+ )
39
+
40
+ const passwordPtr = module.heapBase
41
+ const saltPtr = passwordPtr + passwordLength
42
+ const outPtr = saltPtr + saltLength + 4
43
+ const scratchPtr = align4(outPtr + dkLen)
44
+ const end = scratchPtr + pbkdf2Sha256ScratchSize
45
+ if (!Number.isSafeInteger(end) || end > wasm32Size)
46
+ throw new MemoryError({
47
+ bytes: end,
48
+ cause: new RangeError('PBKDF2 workspace exceeds wasm32 memory.'),
49
+ })
50
+
51
+ module.reserve(end - passwordPtr)
52
+ const view = module.view()
53
+ try {
54
+ view.set(password, passwordPtr)
55
+ view.set(salt, saltPtr)
56
+ module.exports.pbkdf2_sha256(
57
+ passwordPtr,
58
+ passwordLength,
59
+ saltPtr,
60
+ saltLength,
61
+ c,
62
+ outPtr,
63
+ dkLen,
64
+ scratchPtr,
65
+ )
66
+ return module.view().slice(outPtr, outPtr + dkLen)
67
+ } finally {
68
+ // This includes the password, salt and counter, staged output, alignment
69
+ // padding, and every byte of PBKDF2/HMAC working state.
70
+ module.exports.zero(passwordPtr, end - passwordPtr)
71
+ }
72
+ }
73
+
74
+ function align4(value: number) {
75
+ return Math.ceil(value / 4) * 4
76
+ }
77
+
78
+ function assertPositiveUint32(value: number, name: string) {
79
+ if (typeof value !== 'number')
80
+ throw new TypeError(`"${name}" expected number, got ${typeof value}`)
81
+ if (!Number.isSafeInteger(value) || value < 0)
82
+ throw new RangeError(`"${name}" expected integer >= 0, got ${value}`)
83
+ if (value < 1) throw new Error(`"${name}" must be >= 1`)
84
+ if (value > maximumUint32)
85
+ throw new RangeError(`"${name}" must be <= ${maximumUint32}, got ${value}`)
86
+ }
@@ -0,0 +1,29 @@
1
+ import type { Module } from './crypto25519.js'
2
+
3
+ const keySize = 32
4
+
5
+ /** Derives a shared secret and clears both keys and the staged result. */
6
+ export function getSharedSecret(
7
+ module: Module,
8
+ privateKey: Uint8Array,
9
+ publicKey: Uint8Array,
10
+ ): Uint8Array {
11
+ module.reserve(keySize * 3)
12
+ const privateKeyPtr = module.heapBase
13
+ const publicKeyPtr = privateKeyPtr + keySize
14
+ const sharedSecretPtr = publicKeyPtr + keySize
15
+ try {
16
+ const memory = module.view()
17
+ memory.set(privateKey, privateKeyPtr)
18
+ memory.set(publicKey, publicKeyPtr)
19
+ const valid = module.exports.x25519_get_shared_secret(
20
+ privateKeyPtr,
21
+ publicKeyPtr,
22
+ sharedSecretPtr,
23
+ )
24
+ if (valid !== 1) throw new Error('invalid private or public key received')
25
+ return module.view().slice(sharedSecretPtr, sharedSecretPtr + keySize)
26
+ } finally {
27
+ module.exports.zero(privateKeyPtr, keySize * 3)
28
+ }
29
+ }
@@ -1,182 +0,0 @@
1
- // TIP-1022 salt mining — isomorphic WASM (Node + browser).
2
- // Specialized single-block keccak256 for 52-byte input.
3
- //
4
- // Memory layout at offset 1024:
5
- // [0..19] address (set by JS)
6
- // [20..51] salt (set by JS, incremented in-place)
7
- // [52..83] hash out (written on match)
8
-
9
- typedef unsigned char uint8_t;
10
- typedef unsigned int uint32_t;
11
- typedef unsigned long long uint64_t;
12
-
13
- #define DATA_OFFSET 1024
14
-
15
- // ---------------------------------------------------------------------------
16
- // Keccak-f[1600] — fully unrolled, native uint64_t → wasm i64.
17
- // Based on XKCP / hash-wasm structure.
18
- // ---------------------------------------------------------------------------
19
-
20
- #define ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n))))
21
-
22
- static const uint64_t RC[24] = {
23
- 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
24
- 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
25
- 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
26
- 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
27
- 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
28
- 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
29
- 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
30
- 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL,
31
- };
32
-
33
- // Theta — fully unrolled column parity + diffusion
34
- #define XORED(i) (A[(i)] ^ A[(i)+5] ^ A[(i)+10] ^ A[(i)+15] ^ A[(i)+20])
35
-
36
- static inline void theta(uint64_t *A) {
37
- uint64_t D[5];
38
- D[0] = ROTL64(XORED(1), 1) ^ XORED(4);
39
- D[1] = ROTL64(XORED(2), 1) ^ XORED(0);
40
- D[2] = ROTL64(XORED(3), 1) ^ XORED(1);
41
- D[3] = ROTL64(XORED(4), 1) ^ XORED(2);
42
- D[4] = ROTL64(XORED(0), 1) ^ XORED(3);
43
- A[ 0]^=D[0]; A[ 1]^=D[1]; A[ 2]^=D[2]; A[ 3]^=D[3]; A[ 4]^=D[4];
44
- A[ 5]^=D[0]; A[ 6]^=D[1]; A[ 7]^=D[2]; A[ 8]^=D[3]; A[ 9]^=D[4];
45
- A[10]^=D[0]; A[11]^=D[1]; A[12]^=D[2]; A[13]^=D[3]; A[14]^=D[4];
46
- A[15]^=D[0]; A[16]^=D[1]; A[17]^=D[2]; A[18]^=D[3]; A[19]^=D[4];
47
- A[20]^=D[0]; A[21]^=D[1]; A[22]^=D[2]; A[23]^=D[3]; A[24]^=D[4];
48
- }
49
-
50
- // Rho — explicit per-lane rotations with compile-time constants
51
- static inline void rho(uint64_t *A) {
52
- A[ 1] = ROTL64(A[ 1], 1); A[ 2] = ROTL64(A[ 2], 62);
53
- A[ 3] = ROTL64(A[ 3], 28); A[ 4] = ROTL64(A[ 4], 27);
54
- A[ 5] = ROTL64(A[ 5], 36); A[ 6] = ROTL64(A[ 6], 44);
55
- A[ 7] = ROTL64(A[ 7], 6); A[ 8] = ROTL64(A[ 8], 55);
56
- A[ 9] = ROTL64(A[ 9], 20); A[10] = ROTL64(A[10], 3);
57
- A[11] = ROTL64(A[11], 10); A[12] = ROTL64(A[12], 43);
58
- A[13] = ROTL64(A[13], 25); A[14] = ROTL64(A[14], 39);
59
- A[15] = ROTL64(A[15], 41); A[16] = ROTL64(A[16], 45);
60
- A[17] = ROTL64(A[17], 15); A[18] = ROTL64(A[18], 21);
61
- A[19] = ROTL64(A[19], 8); A[20] = ROTL64(A[20], 18);
62
- A[21] = ROTL64(A[21], 2); A[22] = ROTL64(A[22], 61);
63
- A[23] = ROTL64(A[23], 56); A[24] = ROTL64(A[24], 14);
64
- }
65
-
66
- // Pi — single-temporary rotation chain (no scratch buffer)
67
- static inline void pi(uint64_t *A) {
68
- uint64_t A1 = A[1];
69
- A[ 1]=A[ 6]; A[ 6]=A[ 9]; A[ 9]=A[22]; A[22]=A[14]; A[14]=A[20];
70
- A[20]=A[ 2]; A[ 2]=A[12]; A[12]=A[13]; A[13]=A[19]; A[19]=A[23];
71
- A[23]=A[15]; A[15]=A[ 4]; A[ 4]=A[24]; A[24]=A[21]; A[21]=A[ 8];
72
- A[ 8]=A[16]; A[16]=A[ 5]; A[ 5]=A[ 3]; A[ 3]=A[18]; A[18]=A[17];
73
- A[17]=A[11]; A[11]=A[ 7]; A[ 7]=A[10]; A[10]=A1;
74
- }
75
-
76
- // Chi — unrolled nonlinear step
77
- #define CHI_ROW(i) { \
78
- uint64_t a0 = A[(i)], a1 = A[(i)+1]; \
79
- A[(i) ] ^= ~a1 & A[(i)+2]; \
80
- A[(i)+1] ^= ~A[(i)+2] & A[(i)+3]; \
81
- A[(i)+2] ^= ~A[(i)+3] & A[(i)+4]; \
82
- A[(i)+3] ^= ~A[(i)+4] & a0; \
83
- A[(i)+4] ^= ~a0 & a1; \
84
- }
85
-
86
- static inline void chi(uint64_t *A) {
87
- CHI_ROW(0); CHI_ROW(5); CHI_ROW(10); CHI_ROW(15); CHI_ROW(20);
88
- }
89
-
90
- static void keccak_f1600(uint64_t *A) {
91
- for (int r = 0; r < 24; r++) {
92
- theta(A); rho(A); pi(A); chi(A);
93
- A[0] ^= RC[r];
94
- }
95
- }
96
-
97
- // ---------------------------------------------------------------------------
98
- // Helpers
99
- // ---------------------------------------------------------------------------
100
-
101
- static inline uint64_t load64_le(const uint8_t *p) {
102
- return (uint64_t)p[0] | ((uint64_t)p[1] << 8) |
103
- ((uint64_t)p[2] <<16)| ((uint64_t)p[3] << 24) |
104
- ((uint64_t)p[4] <<32)| ((uint64_t)p[5] << 40) |
105
- ((uint64_t)p[6] <<48)| ((uint64_t)p[7] << 56);
106
- }
107
-
108
- static inline void store64_le(uint8_t *p, uint64_t v) {
109
- p[0]=(uint8_t)v; p[1]=(uint8_t)(v>>8); p[2]=(uint8_t)(v>>16); p[3]=(uint8_t)(v>>24);
110
- p[4]=(uint8_t)(v>>32); p[5]=(uint8_t)(v>>40); p[6]=(uint8_t)(v>>48); p[7]=(uint8_t)(v>>56);
111
- }
112
-
113
- static inline uint32_t load32_le(const uint8_t *p) {
114
- return (uint32_t)p[0] | ((uint32_t)p[1]<<8) | ((uint32_t)p[2]<<16) | ((uint32_t)p[3]<<24);
115
- }
116
-
117
- // ---------------------------------------------------------------------------
118
- // Mining entry point
119
- // ---------------------------------------------------------------------------
120
-
121
- __attribute__((export_name("mine")))
122
- int mine(int count) {
123
- uint8_t *mem = (uint8_t *)DATA_OFFSET;
124
- uint8_t *addr = mem; // 20 bytes
125
- uint8_t *salt = mem + 20; // 32 bytes
126
- uint8_t *hout = mem + 52; // 32 bytes
127
-
128
- // Pre-build base state for 52-byte keccak256 (one block, rate=136).
129
- // Block = [address(20) | salt(32) | 0x01 | zeros | 0x80]
130
- // Only lanes 0..6 are non-zero from the message, plus padding in lanes 6 & 16.
131
- uint64_t base[25];
132
- for (int i = 0; i < 25; i++) base[i] = 0;
133
-
134
- // Load address into lanes 0..2 (bytes 0..23, only 0..19 used)
135
- uint8_t buf[24];
136
- for (int i = 0; i < 20; i++) buf[i] = addr[i];
137
- for (int i = 20; i < 24; i++) buf[i] = 0;
138
- base[0] = load64_le(buf);
139
- base[1] = load64_le(buf + 8);
140
- base[2] = load64_le(buf + 16);
141
-
142
- // Keccak padding: byte 52 = 0x01, byte 135 = 0x80
143
- // Lane 6 (bytes 48..55): byte 52 = lane offset 4 → bit 32
144
- base[6] = (uint64_t)0x01 << 32;
145
- // Lane 16 (bytes 128..135): byte 135 = lane offset 7 → bit 56
146
- base[16] = (uint64_t)0x80 << 56;
147
-
148
- for (int iter = 0; iter < count; iter++) {
149
- // Copy base, XOR salt.
150
- // Salt occupies bytes 20..51 → lanes 2..6.
151
- uint64_t A[25];
152
- for (int i = 0; i < 25; i++) A[i] = base[i];
153
-
154
- // Lane 2 (bytes 16..23): upper 4 bytes = salt[0..3]
155
- A[2] ^= (uint64_t)load32_le(salt) << 32;
156
- // Lane 3 (bytes 24..31): salt[4..11]
157
- A[3] ^= load64_le(salt + 4);
158
- // Lane 4 (bytes 32..39): salt[12..19]
159
- A[4] ^= load64_le(salt + 12);
160
- // Lane 5 (bytes 40..47): salt[20..27]
161
- A[5] ^= load64_le(salt + 20);
162
- // Lane 6 (bytes 48..55): lower 4 bytes = salt[28..31]
163
- A[6] ^= (uint64_t)load32_le(salt + 28);
164
-
165
- keccak_f1600(A);
166
-
167
- // PoW check: first 4 bytes = low 32 bits of lane 0 must be zero.
168
- if ((uint32_t)A[0] == 0) {
169
- for (int i = 0; i < 4; i++)
170
- store64_le(hout + i * 8, A[i]);
171
- return 1;
172
- }
173
-
174
- // Increment big-endian salt.
175
- for (int i = 31; i >= 0; i--) {
176
- if (salt[i] < 0xFF) { salt[i]++; break; }
177
- salt[i] = 0;
178
- }
179
- }
180
-
181
- return 0;
182
- }