Skip to content

WASM build uses wasi instead of freestanding due to C library dependencies #15

@roninjin10

Description

@roninjin10

Problem

The WASM build currently uses wasm32-wasi instead of the preferred wasm32-freestanding target because the crypto module depends on C libraries that require libc.

Dependency Chain

wasm_mod → primitives_mod → crypto_mod → C libraries (bn254_wrapper.a, c-kzg-4844.a, blst.a) → libc

Current Workaround

  • Using wasm32-wasi target which provides libc
  • Disabled C library features in WASM build via build options:
    • use_bn254 = false
    • use_c_kzg = false
  • BN254 and C-KZG precompiles are not available in WASM builds

Ideal Solution

To use wasm32-freestanding, we need to:

  1. Option A: Compile C libraries to WASM

    • Build bn254_wrapper, c-kzg-4844, and blst as WASM libraries
    • May require significant porting work
  2. Option B: Pure Zig implementations

    • Rewrite BN254 pairing operations in pure Zig
    • Rewrite KZG operations in pure Zig
    • Would be more maintainable but requires cryptographic expertise
  3. Option C: Conditional compilation

    • Keep current approach but improve feature flags
    • Make precompiles gracefully degrade when C libraries unavailable
    • Document which features are unavailable in freestanding WASM

References

  • See build.zig:523-531 for the TODO comment
  • WASM build configuration starts at line 533 in build.zig

Impact

  • WASM bundle size is larger due to WASI libc overhead
  • Requires WASI runtime support (wasmtime, wasmer, or browser polyfills)
  • Cannot run in minimal WASM environments that don't support WASI
  • Some precompiles (BN254, KZG) are disabled in WASM builds

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions