Skip to content

Add no_std support for Substrate/WASM runtimes #55

@nol4lej

Description

@nol4lej

Currently, light-poseidon cannot be used in Substrate runtimes or other WASM environments that don't have access to the standard library. This limits its adoption in the Polkadot/Substrate ecosystem and other blockchain platforms.

Add no_std support while maintaining full backward compatibility. I have already implemented and tested this in my fork at https://github.com/orbinum/light-poseidon-nostd

Changes Required

Cargo.toml:

  • Add std feature flag (enabled by default)
  • Configure all dependencies with default-features = false
  • Make thiserror optional (only available with std)
  • Add curve feature to ark-bn254 for Fr support in no_std

lib.rs:

  • Add #![cfg_attr(not(feature = "std"), no_std)]
  • Import extern crate alloc for no_std environments
  • Use alloc::vec::Vec conditionally
  • Create dual implementation of PoseidonError (with/without thiserror)

parameters/bn254_x5.rs:

  • Import alloc::vec macro for no_std
  • Replace std::convert with core::convert

Verification

All changes have been tested and verified:

# no_std build for WASM
cargo build --no-default-features --target wasm32-unknown-unknown ✓

# Standard build
cargo build ✓

# All tests pass
cargo test ✓ (35 tests)

Usage

# For no_std environments
[dependencies]
light-poseidon = { version = "0.4.0", default-features = false }

# For std environments (default, unchanged)
[dependencies]
light-poseidon = "0.4.0"

Benefits

  • Enables use in Substrate runtimes and WASM targets
  • Zero breaking changes (std enabled by default)
  • All existing tests pass
  • No performance impact

I'm ready to submit a PR with these changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions