Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace rand_core's std feature with alloc #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jvff
Copy link

@jvff jvff commented Aug 21, 2024

Motivation

The std feature from rand_core activates the alloc feature and the getrandom feature, which ends up including getrandom as a dependency.

For WebAssembly targets, it's only possible to obtain entropy if there's an external function that provides that. Because of this, getrandom only works if there are Javascript APIs enabled. If there aren't, it fails to build.

Solution

The source of entropy isn't needed inside the library, therefore, the getrandom feature does not need to be enabled. Therefore it's enough to replace enabling the std feature with enabling just the alloc feature.

There is a use case in a documentation test, which uses OsRng. However, it's still not necessary to manually enable the getrandom feature in dev-dependencies, because the curve25519-dalek dependency's default feature enables rand_core's std dependency.

Release Notes

I believe this is a backward compatible change. I think there could be a situation where some project has a dependency on rand_core without its default features and on merlin, and unknowingly uses getrandom, which with this PR would fail to compile. However, the solution would be to fix the rand_core dependency to enable the required feature.

The `std` feature from `rand_core` activates the `alloc` feature and the
`getrandom` feature, which ends up including `getrandom` as a dependency.

For WebAssembly targets, it's only possible to obtain entropy if there's
an external function that provides that. Because of this, `getrandom`
only works if there are Javascript APIs enabled. If there aren't, it
fails to build.

The source of entropy isn't needed inside the library, therefore, the
`getrandom` feature does not need to be enabled. There is a use case in
a documentation test, which uses `OsRng`. However, it's still not
necessary to manually enable the `getrandom` feature in
`dev-dependencies`, because the `curve25519-dalek` dependency's
`default` feature enables `rand_core`'s `std` dependency.
JayWhite2357 pushed a commit to spaceandtimelabs/sxt-proof-of-sql that referenced this pull request Aug 28, 2024
# Rationale for this change

To build this crate for `wasm32-unknown-unknown` to run on runtimes with
a limited custom API available (e.g., Linera contracts), `wasm-bindgen`
should not be a dependency. That's because `wasm-bindgen` generates
bindings for the Javascript APIs available in browsers (and similar
runtimes). For more bare-bones runtimes, these APIs aren't available.

# What changes are included in this PR?

Disabling the `default` features of `chrono`, `rand` and `rand-core`.

Unfortunately, this doesn't complete the work to support
`wasm32-unknown-unknown` without JS, because a fix is also needed in
`merlin` (zkcrypto/merlin#8).

# Are these changes tested?

Tested with `cargo test`.
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