milagro_bls icon indicating copy to clipboard operation
milagro_bls copied to clipboard

no-std not compiling

Open claravanstaden opened this issue 2 years ago • 3 comments

I am trying to use this crate with the Substrate framework, which requires the no-std feature. It looks like this crate supports running in no-std environments, if I look at Cargo.toml. However, when I try to compile the crate I get the following compiler errors:

error[E0433]: failed to resolve: could not find `prelude` in `alloc`
  --> src/lib.rs:15:20
   |
15 |     pub use alloc::prelude::v1::*;
   |                    ^^^^^^^ could not find `prelude` in `alloc`

error[E0433]: failed to resolve: use of undeclared type `Vec`
  --> src/keys.rs:58:27
   |
58 |             let mut prk = Vec::<u8>::with_capacity(1 + ikm.len());
   |                           ^^^ not found in this scope
   |
help: consider importing this struct
   |
5  | use alloc::vec::Vec;
   |

I will try to fix these in a forked version, but just wanted to get your input in case I might be missing something.

Is the crate only supported for nightly builds?

error[E0554]: `#![feature]` may not be used on the stable release channel
 --> src/lib.rs:4:5
  |
4 |     feature(alloc),
  |     ^^^^^^^^^^^^^^

error[E0554]: `#![feature]` may not be used on the stable release channel
 --> src/lib.rs:5:5
  |
5 |     feature(alloc_prelude),
  |     ^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: `#![feature]` may not be used on the stable release channel
 --> src/lib.rs:6:5
  |
6 |     feature(prelude_import)
  |     ^^^^^^^^^^^^^^^^^^^^^^^

claravanstaden avatar Apr 14 '22 13:04 claravanstaden

Hey sorry for the slow reply, does PR #53 resolve you issue such that it will now compile without std?

kirk-baird avatar Nov 01 '22 01:11 kirk-baird

No, that PR did not actually solve the issue. The command #![cfg_attr(not(std), no_std)] is not equivalent to #![no_std] (see: https://github.com/rust-lang/rust/issues/42190#issuecomment-303783899).

Related comments:

https://github.com/sigp/milagro_bls/blob/d3fc0a40cfe8b72ccda46ba050ee6786a59ce753/Cargo.toml#L13 There, it points to https://github.com/sigp/incubator-milagro-crypto-rust/tree/057d238936c0cbbe3a59dfae6f2405db1090f474 In that repo, we see the package amcl ’s lib.rs file: https://github.com/sigp/incubator-milagro-crypto-rust/blob/057d238936c0cbbe3a59dfae6f2405db1090f474/src/lib.rs So, what I did to try forcing a no_std environment was clone the amcl crate above, and add #![no_std] to the lib.rs and see if it would compile. It did not compile.

tbraun96 avatar Jan 05 '23 17:01 tbraun96

@tbraun96 good point!

@kirk-baird I think supporting no-std is actually a little more involved than PR https://github.com/sigp/milagro_bls/pull/53 accomplished. Essentially a clean way to do it is so like this:

Declare imports: https://github.com/serde-rs/serde/blob/master/serde/src/lib.rs#L151 Usage: https://github.com/serde-rs/serde/blob/master/serde/src/ser/mod.rs#L110

So you toggle between std and core/alloc dependent on if feature std is enabled or not, preferably in a central place in the crate and giving it your own mod name, like lib in serde's case, or std. Then you import it (use lib::*) where any std dependency is used (which is usually everywhere 😋 ).

I attempted this for the ssz_rs crate, PR here: https://github.com/ralexstokes/ssz-rs/pull/25

claravanstaden avatar Jan 05 '23 17:01 claravanstaden