Rust halo2_proofs crate on webassembly fails to compile

I’m trying to compile a rust library to target webassembly so I can use it within a web page.

cargo build --target=wasm32-unknown-unknown

But it fails with this:

error: The multicore feature flag is not supported on wasm32 architectures without atomics
  --> /home/andrew/.cargo/registry/src/index.crates.io-6f17d22bba15001f/halo2_proofs-0.3.0/src/multicore.rs:10:1
   |
10 | / compile_error!(
11 | |     "The multicore feature flag is not supported on wasm32 architectures without atomics"
12 | | );
   | |_^

error[E0432]: unresolved imports `maybe_rayon::current_num_threads`, `maybe_rayon::iter::IndexedParallelIterator`
  --> /home/andrew/.cargo/registry/src/index.crates.io-6f17d22bba15001f/halo2_proofs-0.3.0/src/multicore.rs:20:23
   |
20 | pub use maybe_rayon::{current_num_threads, iter::IndexedParallelIterator};
   |                       ^^^^^^^^^^^^^^^^^^^  ^^^^^^-----------------------
   |                       |                    |     |
   |                       |                    |     help: a similar name exists in the module: `IntoParallelIterator`
   |                       |                    no `IndexedParallelIterator` in `iter`
   |                       no `current_num_threads` in the root

error[E0277]: expected a `Fn<(&impl Fn() -> T + Send + Sync, Result<T, E>)>` closure, found `impl Fn(T, Result<T, E>) -> Result<T, E> + Send + Sync`
  --> /home/andrew/.cargo/registry/src/index.crates.io-6f17d22bba15001f/halo2_proofs-0.3.0/src/multicore.rs:56:35
   |
56 |         self.try_fold(&identity, &fold_op)
   |              --------             ^^^^^^^ expected an `Fn<(&impl Fn() -> T + Send + Sync, Result<T, E>)>` closure, found `impl Fn(T, Result<T, E>) -> Result<T, E> + Send + Sync`
   |              |
   |              required by a bound introduced by this call
   |
   = note: expected a closure with arguments `(T, Result<_, _>)`
              found a closure with arguments `(&impl Fn() -> T + Send + Sync, Result<_, _>)`
   = note: required for `&impl Fn(T, Result<T, E>) -> Result<T, E> + Send + Sync` to implement `FnMut<(&impl Fn() -> T + Send + Sync, Result<T, E>)>`
note: required by a bound in `try_fold`
  --> /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/traits/iterator.rs:2296:5

error[E0277]: expected a `FnOnce<(&impl Fn() -> T + Send + Sync, Result<T, E>)>` closure, found `impl Fn(T, Result<T, E>) -> Result<T, E> + Send + Sync`
  --> /home/andrew/.cargo/registry/src/index.crates.io-6f17d22bba15001f/halo2_proofs-0.3.0/src/multicore.rs:56:34
   |
56 |         self.try_fold(&identity, &fold_op)
   |              --------            ^^^^^^^^ expected an `FnOnce<(&impl Fn() -> T + Send + Sync, Result<T, E>)>` closure, found `impl Fn(T, Result<T, E>) -> Result<T, E> + Send + Sync`
   |              |
   |              required by a bound introduced by this call
   |
   = note: expected a closure with arguments `(T, Result<_, _>)`
              found a closure with arguments `(&impl Fn() -> T + Send + Sync, Result<_, _>)`
note: required by a bound in `try_fold`
  --> /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/iter/traits/iterator.rs:2296:5

Some errors have detailed explanations: E0277, E0432.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `halo2_proofs` (lib) due to 4 previous errors

To try to resolve the first issue, I added this to a new .cargo\config file (I also tried .cargo\config.toml):

[target.wasm32-unknown-unknown]
rustflags = ["-C", "target-feature=+atomics,+bulk-memory"]

But that made no difference.
Any suggestions?

3 Likes

you will need to switch to a nightly version of Rust and enable support for wasm atomics. this command worked for me:

RUSTFLAGS='-C target-feature=+atomics' cargo +nightly build --target=wasm32-unknown-unknown

for more details, there’s also a guide on compiling to wasm in the halo2 book: WASM Guide - The halo2 Book

1 Like

Thanks. That gets me closer. The build now fails with just this:

error[E0463]: can't find crate for `panic_abort`

The book link you shared doesn’t mention this.
Any ideas?

BTW I’m using this command to build:

cargo +nightly check -Zbuild-std --target=wasm32-unknown-unknown

I had to add -Zbuild-std because without it I get an absolute mess of errors.

It seems this last error only occurs when my cargo.toml file has this:

[lib]
crate-type = ["cdylib"]

But I need that section for the non-wasm targets. I wonder how I can condition this out just for that.