Skip to content

Conversation

@palas
Copy link
Contributor

@palas palas commented Oct 23, 2025

Changelog

- description: |
    Use WASI call to generate random bytes instead of JS snippet
  type:
  - compatible
  projects:
  - cardano-wasm

Context

We want to produce a WASM module that doesn't depend on JavaScript function calls to work, only in WASI, so that it can be used from other languages. This PR modifies the call used to generate random ByteStrings so that it doesn't depend on JavaScript but uses the random_get primitive from WASI. Which is described as producing high quality randomness, and is actually implemented in terms of crypto.getRandomValues in the shim we use.

How to trust this PR

It is tricky, because it is really sensitive code, and the code is quite simple, but it does rely on the WASI implementation underneath. I will do some testing and you can see the implementation in the shim we are currently using for JS here: https://github.com/bjorn3/browser_wasi_shim/blob/8dee2473332e540b3f3a9c59259dd027dc1e6853/src/wasi.ts#L886

Something worrying is It seems it uses Math.random() as fallback, which is not cryptographically secure. But until we can include WASI2, this seems the best primitive.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

@palas palas force-pushed the use-wasi-for-randomness branch 2 times, most recently from ce9c7cf to f83a020 Compare October 24, 2025 19:57
@palas palas force-pushed the use-wasi-for-randomness branch from f83a020 to 6c6658e Compare October 24, 2025 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant