feat(wasm): support "pure JS" mode

Closes #1159

This uses the binaryen tool wasm2js to compile the Anubis WASM blobs
to JavaScript. This produces biblically large (520Ki) outputs when you
inline both hashx and sha256 solvers, but this is a tradeoff that I'm
willing to accept. The performance is good enough in my testing with
JIT enabled. I fear that this may end up being terrible with JIT
disabled. I have no idea if this will work on big endian or not.

Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
Xe Iaso
2025-09-30 14:21:29 +00:00
parent 705da2fa3c
commit 643b4719d8
13 changed files with 205 additions and 26 deletions

View File

@@ -0,0 +1,23 @@
import {
memory,
data_ptr,
set_data_length,
anubis_work,
anubis_validate,
result_hash_ptr,
result_hash_size,
verification_hash_ptr,
verification_hash_size,
} from "../../../static/wasm/baseline/hashx.mjs";
export default {
memory,
data_ptr,
set_data_length,
anubis_work,
anubis_validate,
result_hash_ptr,
result_hash_size,
verification_hash_ptr,
verification_hash_size
};

View File

@@ -1,4 +1,4 @@
import { u } from "../../lib/xeact";
import { u } from "../../../lib/xeact";
import { simd } from "wasm-feature-detect";
// import { compile } from '@haribala/wasm2js';
@@ -13,20 +13,21 @@ interface ProcessOptions {
const getHardwareConcurrency = () =>
navigator.hardwareConcurrency !== undefined ? navigator.hardwareConcurrency : 1;
// https://stackoverflow.com/questions/47879864/how-can-i-check-if-a-browser-supports-webassembly
const isWASMSupported = (() => {
try {
if (typeof WebAssembly === "object"
&& typeof WebAssembly.instantiate === "function") {
const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
if (module instanceof WebAssembly.Module)
return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
}
} catch (e) {
return false;
}
return false;
})();
// // https://stackoverflow.com/questions/47879864/how-can-i-check-if-a-browser-supports-webassembly
// const isWASMSupported = (() => {
// try {
// if (typeof WebAssembly === "object"
// && typeof WebAssembly.instantiate === "function") {
// const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
// if (module instanceof WebAssembly.Module)
// return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
// }
// } catch (e) {
// return false;
// }
// return false;
// })();
const isWASMSupported = false;
export default function process(
options: ProcessOptions,
@@ -38,8 +39,10 @@ export default function process(
): Promise<string> {
const { basePrefix, version, algorithm } = options;
let worker = "wasm";
if (!isWASMSupported) {
throw new Error("WebAssembly is not supported on this platform. Please report ALL details about your browser, environment, OS, CPU, device vendor, and other details to https://github.com/TecharoHQ/anubis/issues/1116. Remember: being polite means you get this fixed.");
worker = "wasm2js";
}
return new Promise(async (resolve, reject) => {
@@ -49,10 +52,7 @@ export default function process(
wasmFeatures = "simd128";
}
const module = await fetch(u(`${basePrefix}/.within.website/x/cmd/anubis/static/wasm/${wasmFeatures}/${algorithm}.wasm?cacheBuster=${version}`))
.then(x => WebAssembly.compileStreaming(x));
const webWorkerURL = `${options.basePrefix}/.within.website/x/cmd/anubis/static/js/worker/wasm.mjs?cacheBuster=${version}`;
const webWorkerURL = `${basePrefix}/.within.website/x/cmd/anubis/static/js/worker/${worker}.mjs?cacheBuster=${version}`;
const workers: Worker[] = [];
let settled = false;
@@ -103,7 +103,7 @@ export default function process(
difficulty,
nonce: i,
threads,
module,
algorithm,
});
}
});

View File

@@ -0,0 +1,23 @@
import {
memory,
data_ptr,
set_data_length,
anubis_work,
anubis_validate,
result_hash_ptr,
result_hash_size,
verification_hash_ptr,
verification_hash_size,
} from "../../../static/wasm/baseline/sha256.mjs";
export default {
memory,
data_ptr,
set_data_length,
anubis_work,
anubis_validate,
result_hash_ptr,
result_hash_size,
verification_hash_ptr,
verification_hash_size
};