WebAssembly proof of work
WebAssembly proof of work makes a client spend CPU time to find a nonce whose algorithm result starts with the required number of zero bits. Rust builds the client and server algorithm modules. Go runs the same modules on the server to verify the client result. The browser uses WebAssembly when possible and a generated JavaScript form when it is not. See client challenge runtime for the wider challenge flow and challenge UI delivery for page delivery.
System boundary
The system has one shared proof contract between the browser and the server. The browser searches for a valid (nonce, response) pair. The server does not trust the reported time or the client result; it recomputes the result from the challenge, nonce, algorithm, and difficulty.
flowchart LR
C[Challenge bytes and difficulty] --> B[Browser workers]
B -->|nonce and result hash| V[Go verification]
C --> V
V --> D{Algorithm and difficulty valid?}
D -->|yes| A[Allow challenge flow]
D -->|no| R[Reject response]
Execution paths
Browser solving
The browser selects the algorithm named by the challenge and spreads the nonce search across workers. Each worker starts at its own nonce and advances by the worker count, so the workers search separate lanes. A worker returns as soon as it finds a result that satisfies the difficulty.
The WebAssembly path fetches and compiles the algorithm module. It selects the SIMD module when the browser supports simd128 and otherwise uses the baseline module. If WebAssembly is not available, the client runs the generated wasm2js form of the baseline module. The shared worker runner handles progress, cancellation, worker failure, and the first successful result.
References: web/…/wasm.ts• L13-70 web/…/wasm2js.ts• L9-25 web/…/worker.ts• L146-180 web/…/wasm.ts• L8-71 web/…/wasm2js.ts• L18-73
Rust and Go module boundary
Rust supplies a common module shape for all three algorithms. The module accepts challenge bytes through a shared data buffer. It exposes work and validation entry points plus pointers and lengths for result data. Go loads the compiled module with wazero and uses the same boundary for both solving in tests and server-side verification. The browser uses the equivalent exported functions through the WebAssembly API.
The host callback reports search progress. It is useful to the browser UI but is not part of proof validity. The module interface is therefore a data and control boundary, not a trust boundary: only the server-side validation result decides whether a response passes.
References: wasm/…/lib.rs• L8-36 wasm/…/lib.rs• L46-60 wasm/wasm.go• L89-114 wasm/wasm.go• L231-269 web/…/wasm.ts• L3-22
Module delivery
The Rust crates build two browser variants: a baseline module and a SIMD module. The build step checks each module against the feature set supported by its target browser group. The baseline modules are also converted to JavaScript for the no-WebAssembly path. The server challenge setup loads the matching algorithm module as a static asset, while the browser builds its URL from the same algorithm name and deployment version.
The server and browser must keep algorithm names aligned. The server registers argon2id, hashx, and sha256; the browser maps those same names to the WebAssembly process. A name mismatch can produce a challenge that one side can issue but the other side cannot run.
References: wasm/…/build_wasm.sh• L8-10 wasm/…/build_wasm.sh• L104-114 wasm/…/build_wasm2js.sh• L8-11 lib/…/wasm.go• L20-45 web/…/index.ts• L1-11
Proof algorithms
Each algorithm treats difficulty as a count of leading zero bits in a 32-byte result. This differs from the regular JavaScript SHA-256 challenge, where difficulty is checked as leading hexadecimal zeroes. The algorithm choice changes the cost and resistance of the client search, but not the outer challenge contract.
Argon2id
Argon2id is a memory-hard password hashing function. It uses the raw challenge bytes as the password and the nonce as the salt, then checks the leading result bits. This makes each trial use a fixed memory cost instead of only a small hash operation.
The nonce byte order is chosen from the final challenge byte. The client and server use this same rule, so a verifier must use the module rather than reproduce only the algorithm name or a generic Argon2id call.
References: wasm/…/lib.rs• L18-74 wasm/…/lib.rs• L77-107 wasm/…/lib.rs• L126-144
HashX
HashX creates a program from the challenge and uses that program to produce a result for each nonce. It is intended to make specialized hardware less useful than it would be for a simple hash. As with the other WebAssembly algorithms, the result must satisfy the leading zero-bit rule.
Some challenge seeds do not produce a valid HashX program. In that case, both sides append the same increasing little-endian counter until they get a valid seed. This keeps the seed choice deterministic and prevents a client or server trap for that small set of challenges.
References: wasm/…/lib.rs• L18-75 wasm/…/lib.rs• L77-109 wasm/…/lib.rs• L126-145
SHA-256
The WebAssembly SHA-256 algorithm hashes the raw challenge bytes with the nonce bytes and checks the leading result bits. It is available for systems that need SHA-256, but Argon2id or HashX are the preferred choices when their properties fit the policy.
The nonce byte order also depends on the final challenge byte. This differs from the regular fast challenge, which hashes the challenge text followed by the decimal nonce. A verifier must use the selected WebAssembly module and must not substitute the regular JavaScript challenge rule.
References: wasm/…/lib.rs• L17-72 wasm/…/lib.rs• L74-105 wasm/…/lib.rs• L123-141 lib/…/proofofwork.go• L70-89
Verification boundary
The server receives the challenge response as form data. It first checks the challenge input and the required nonce, elapsedTime, and response fields. It parses the nonce as an integer, decodes the challenge and response from hex, then asks the selected Runner to recompute and compare the result. The elapsed time is recorded as a metric only; it does not prove that the work was done in the browser.
The Go Runner validates the module shape before use. It limits challenge input to a non-empty buffer of at most 4096 bytes and requires the returned verification bytes to have the module's exact result size. It runs verification in a bounded pool of four concurrent slots, so a burst of expensive checks cannot consume unlimited server resources.
sequenceDiagram
participant B as Browser
participant H as Challenge handler
participant W as Go WASM runner
participant M as Rust module
B->>H: nonce, response, elapsedTime
H->>H: Validate fields and decode hex
H->>W: challenge bytes, response bytes, nonce, difficulty
W->>M: Load challenge and response
M-->>W: Recomputed result and validity
W-->>H: valid or invalid
H-->>B: Accept or reject challenge
References: lib/…/wasm.go• L53-106 wasm/wasm.go• L185-209 wasm/wasm.go• L272-337 wasm/verification_limit_test.go• L10-39
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the algorithm registration in lib/…/wasm.go aligned with the browser map in web/…/index.ts and the generated
wasm2jsmap. - Keep the Rust export set and buffer sizes aligned with the Go Runner and both browser workers.
- If the challenge byte format or nonce encoding changes in one Rust algorithm, update the client asset and the server verifier together.
- If the WebAssembly feature targets change, rebuild both baseline and SIMD assets and regenerate the baseline
wasm2jsassets. - Preserve the exact result length and hex encoding expected by the form validation boundary.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| WebAssembly or SIMD is not supported | The client cannot use the native module path | WebAssembly capability detection selects generated wasm2js for unsupported browsers |
| A proxy changes the module content type | Streaming compilation fails | The client retries with buffered compilation |
| A strict worker Content Security Policy blocks Blob workers | Workers fail before they search | The shared worker runner demotes to direct worker URLs |
| The response is not valid hex or has the wrong byte length | Verification stops before algorithm validation | The handler decodes the response and the Runner checks the exact module size |
| A HashX seed violates program constraints | Naive module creation could trap | Client and server use the same deterministic seed retry rule |
| More than four server verifications wait at once | A later verification can block or hit its request context deadline | The bounded verification pool honors context cancellation |
Key files
| File | Why to read it |
|---|---|
| lib/…/wasm.go | Connects configured algorithm names to challenge issue and server validation. |
| wasm/wasm.go | Defines the Go-to-WebAssembly loading, memory, work, and verification boundary. |
| wasm/…/lib.rs | Defines the shared Rust module contract used by each proof algorithm. |
| wasm/…/lib.rs | Shows the memory-hard Argon2id proof and its raw-byte input rule. |
| wasm/…/lib.rs | Shows HashX program creation and deterministic seed recovery. |
| wasm/…/lib.rs | Shows the WebAssembly SHA-256 proof and nonce encoding rule. |
| web/…/wasm.ts | Selects the browser module variant and starts native WebAssembly workers. |
| web/…/wasm.ts | Adapts the module exports to worker input and result messages. |
| web/…/wasm2js.ts | Runs the generated JavaScript fallback for the same algorithms. |
| web/…/worker.ts | Owns worker lanes, progress, cancellation, and worker failure handling. |
| wasm/…/build_wasm.sh | Produces browser-compatible baseline and SIMD modules. |
| wasm/…/build_wasm2js.sh | Converts baseline modules for browsers without WebAssembly. |