1

I am new to rust and encountered the following error when trying to use the secp256k1 crate with the provided example code

error[E0432]: unresolved import `secp256k1::rand::rng`
 --> src/main.rs:1:22
  |
1 | use secp256k1::rand::rng::OsRng;
  |                      ^^^ could not find `rng` in `rand`

error: aborting due to previous error

My code is really simple

use secp256k1::rand::rng::OsRng;
use secp256k1::Secp256k1;

fn main() {
    let secp = Secp256k1::new();
    let mut rng = OsRng::new().expect("OsRng");
}

with the Cargo.toml containing

[dependencies]
secp256k1 = {version="0.20.1", features = ["rand"]}

Are we missing something to get this working?

I tried using rustc 1.22.1 (b01adbbc3 2020-07-08) and then rustc 1.51.0 (2fd73fabe 2021-03-23)

Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • Try changing `secp256k1::rand::rng::OsRng` to `secp256k1::rand::rngs::OsRng`. It looks like the module was renamed and the authors of `secp256k1` have not updated their documentation yet. – Locke Apr 13 '21 at 22:07
  • 1
    @Locke Renamed to `secp256k1::rand::rngs::OsRng` and still getting a new error: *no `OsRng` in `rand::rngs`* – Nyxynyx Apr 13 '21 at 22:10
  • Then you need to also add the `std` feature to `rand`. You can do this by adding it as a dependency `rand = {version = "0.6", features = ["std"]}`. `0.6` is the version used by `secp256k1`. You might also be able to add it to the default features as `[features] default = ["rand/std"]`, but I'm not sure if that would work. – Locke Apr 13 '21 at 22:14
  • 1
    @Locke Adding `rand = {version = "0.6", features = ["std"]}` got it working. Thanks! – Nyxynyx Apr 13 '21 at 22:18
  • Fwiw, it seems you can now just enable the "rand-std" feature on secp256k1 now instead of doing all that. – TomLisankie Feb 22 '22 at 04:02

0 Answers0