w3f / schnorrkel

Schnorr VRFs and signatures on the Ristretto group
BSD 3-Clause "New" or "Revised" License
306 stars 92 forks source link

Broken `no_std` because of `rand/std_rng` #95

Closed Ujang360 closed 9 months ago

Ujang360 commented 9 months ago

Problem Background

I'm developing a no_std library that requires schnorrkel. I included schnorrkel = { version = "0.11.2", default-features = false } in the Cargo.toml file, but this test failed because of the lines bellow got triggered:

https://github.com/w3f/schnorrkel/blob/38035b50f49ecc40e10831bb8c11b59c143b7aa9/src/lib.rs#L237-L251

So I added the getrandom features, like schnorrkel = { version = "0.11.2", default-features = false, features = ["getrandom"] }. But, the getrandom feature enables rand/std_rng, which then breaks the #![no_std] build.

😢

Proposed Fix

On my local test, removing rand/std_rng in getrandom feature solves the problem.

https://github.com/w3f/schnorrkel/blob/38035b50f49ecc40e10831bb8c11b59c143b7aa9/Cargo.toml#L55

Dependencies Tree Before Removing rand/std_rng

command: cargo tree -e features,no-dev,no-build

├── schnorrkel feature "getrandom"
│   ├── schnorrkel v0.11.2
│   │   ├── arrayref v0.3.7
│   │   ├── arrayvec v0.7.4 (*)
│   │   ├── merlin v3.0.0
│   │   │   ├── byteorder v1.4.3
│   │   │   ├── keccak v0.1.4
│   │   │   ├── rand_core v0.6.4 (*)
│   │   │   └── zeroize feature "zeroize_derive" (*)
│   │   ├── rand_core v0.6.4 (*)
│   │   ├── sha2 v0.10.8 (*)
│   │   ├── subtle v2.5.0
│   │   ├── zeroize feature "zeroize_derive" (*)
│   │   ├── curve25519-dalek feature "digest" (*)
│   │   ├── curve25519-dalek feature "legacy_compatibility"
│   │   │   └── curve25519-dalek v4.1.1 (*)
│   │   ├── curve25519-dalek feature "precomputed-tables" (*)
│   │   ├── curve25519-dalek feature "zeroize" (*)
│   │   └── rand feature "default"
│   │       ├── rand v0.8.5 (*)
│   │       ├── rand feature "std"
│   │       │   ├── rand v0.8.5 (*)
│   │       │   ├── rand_core feature "std"
│   │       │   │   ├── rand_core v0.6.4 (*)
│   │       │   │   ├── rand_core feature "alloc"
│   │       │   │   │   └── rand_core v0.6.4 (*)
│   │       │   │   ├── rand_core feature "getrandom" (*)
│   │       │   │   └── getrandom feature "std"
│   │       │   │       └── getrandom v0.2.10 (*)
│   │       │   ├── rand feature "alloc"
│   │       │   │   ├── rand v0.8.5 (*)
│   │       │   │   └── rand_core feature "alloc" (*)
│   │       │   ├── rand feature "getrandom" (*)
│   │       │   ├── rand feature "libc"
│   │       │   │   └── rand v0.8.5 (*)
│   │       │   ├── rand feature "rand_chacha"
│   │       │   │   └── rand v0.8.5 (*)
│   │       │   └── rand_chacha feature "std"
│   │       │       ├── rand_chacha v0.3.1 (*)
│   │       │       └── ppv-lite86 feature "std"
│   │       │           └── ppv-lite86 v0.2.17
│   │       └── rand feature "std_rng"
│   │           ├── rand v0.8.5 (*)
│   │           └── rand feature "rand_chacha" (*)
│   ├── rand_core feature "getrandom" (*)
│   ├── rand feature "std_rng" (*)
│   └── schnorrkel feature "rand"
│       └── schnorrkel v0.11.2 (*)

Dependencies Tree After Removing rand/std_rng

command: cargo tree -e features,no-dev,no-build

├── schnorrkel feature "getrandom"
│   ├── schnorrkel v0.11.2 (https://github.com/goro-network/schnorrkel?rev=7eabd4c#7eabd4c4)
│   │   ├── arrayref v0.3.7
│   │   ├── arrayvec v0.7.4 (*)
│   │   ├── merlin v3.0.0
│   │   │   ├── byteorder v1.4.3
│   │   │   ├── keccak v0.1.4
│   │   │   ├── rand_core v0.6.4 (*)
│   │   │   └── zeroize feature "zeroize_derive" (*)
│   │   ├── rand_core v0.6.4 (*)
│   │   ├── sha2 v0.10.8 (*)
│   │   ├── subtle v2.5.0
│   │   ├── zeroize feature "zeroize_derive" (*)
│   │   ├── curve25519-dalek feature "digest" (*)
│   │   ├── curve25519-dalek feature "legacy_compatibility"
│   │   │   └── curve25519-dalek v4.1.1 (*)
│   │   ├── curve25519-dalek feature "precomputed-tables" (*)
│   │   └── curve25519-dalek feature "zeroize" (*)
│   └── rand_core feature "getrandom" (*)

Extra Note

This is my test commands:

cargo no-std-check --manifest-path Cargo.toml --no-default-features --features="getrandom" && \
cargo test --no-default-features --features="getrandom" && \
cargo test
burdges commented 9 months ago

Yes, the getrandom crate could do various things, not all of which are std.

burdges commented 9 months ago

Anything else? I'll publish this version if no.

Ujang360 commented 9 months ago

Anything else? I'll publish this version if no.

Wow that was fast! Nothing else, and yes please 🙏🏽

burdges commented 9 months ago

done

Ujang360 commented 9 months ago

Thank you very much @burdges