Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ std = ["alloc", "secp256k1-sys/std"]
# allow use of Secp256k1::new and related API that requires an allocator
alloc = ["secp256k1-sys/alloc"]
bitcoin-hashes = ["bitcoin_hashes"] # Feature alias because of the underscore.
bitcoin-hashes-std = ["bitcoin-hashes", "bitcoin_hashes/std"]
rand-std = ["rand/std", "rand/std_rng"]
bitcoin-hashes-std = ["std", "bitcoin_hashes/std"]
rand-std = ["std", "rand/std", "rand/std_rng"]
recovery = ["secp256k1-sys/recovery"]
lowmemory = ["secp256k1-sys/lowmemory"]
global-context = ["std"]
Expand All @@ -46,10 +46,8 @@ bitcoin_hashes = { version = "0.11", default-features = false, optional = true }
rand = { version = "0.8", default-features = false, optional = true }

[dev-dependencies]
rand = "0.8"
rand_core = "0.6"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR, IIRC I had WIP changes that add rand_core feature so this one would have to go too. But it's OK now.

serde_test = "1.0"
bitcoin_hashes = "0.11"
bincode = "1.3.3"

# cbor does not build on WASM, we use it in a single trivial test (an example of when
Expand All @@ -64,15 +62,15 @@ getrandom = { version = "0.2", features = ["js"] }

[[example]]
name = "sign_verify_recovery"
required-features = ["std", "recovery"]
required-features = ["recovery", "bitcoin-hashes-std"]

[[example]]
name = "sign_verify"
required-features = ["std"]
required-features = ["bitcoin-hashes-std"]

[[example]]
name = "generate_keys"
required-features = ["std", "rand-std"]
required-features = ["rand-std"]

[workspace]
members = ["secp256k1-sys"]
Expand Down
14 changes: 5 additions & 9 deletions contrib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

set -ex

FEATURES="bitcoin-hashes global-context lowmemory rand recovery serde std alloc"
# These features are typically enabled along with the 'std' feature, so we test
# them together with 'std'.
STD_FEATURES="rand-std bitcoin-hashes-std"
FEATURES="bitcoin-hashes global-context lowmemory rand recovery serde std alloc bitcoin-hashes-std rand-std"

cargo --version
rustc --version
Expand Down Expand Up @@ -49,17 +46,16 @@ if [ "$DO_FEATURE_MATRIX" = true ]; then
RUSTFLAGS='--cfg=fuzzing' RUSTDOCFLAGS='--cfg=fuzzing' cargo test --all
RUSTFLAGS='--cfg=fuzzing' RUSTDOCFLAGS='--cfg=fuzzing' cargo test --all --features="$FEATURES"
cargo test --all --features="rand serde"
cargo test --features="$STD_FEATURES"

if [ "$NIGHTLY" = true ]; then
cargo test --all --all-features
RUSTFLAGS='--cfg=fuzzing' RUSTDOCFLAGS='--cfg=fuzzing' cargo test --all --all-features
fi

# Examples
cargo run --example sign_verify --features=std
cargo run --example sign_verify_recovery --features=std,recovery
cargo run --example generate_keys --features=std,rand-std
cargo run --example sign_verify --features=bitcoin-hashes-std
cargo run --example sign_verify_recovery --features=recovery,bitcoin-hashes-std
cargo run --example generate_keys --features=rand-std
fi

# Build the docs if told to (this only works with the nightly toolchain)
Expand Down Expand Up @@ -96,7 +92,7 @@ fi
# Bench if told to, only works with non-stable toolchain (nightly, beta).
if [ "$DO_BENCH" = true ]
then
RUSTFLAGS='--cfg=bench' cargo bench --features=recovery
RUSTFLAGS='--cfg=bench' cargo bench --features=recovery,rand-std
fi

exit 0
3 changes: 1 addition & 2 deletions examples/generate_keys.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
extern crate secp256k1;

use secp256k1::rand::thread_rng;
use secp256k1::{PublicKey, Secp256k1, SecretKey};

fn main() {
let secp = Secp256k1::new();
let mut rng = thread_rng();
let mut rng = rand::thread_rng();
// First option:
let (seckey, pubkey) = secp.generate_keypair(&mut rng);

Expand Down
7 changes: 3 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ pub mod global {
/// ```
/// # #[cfg(all(feature = "global-context", feature = "rand-std"))] {
/// use secp256k1::{PublicKey, SECP256K1};
/// use secp256k1::rand::thread_rng;
/// let _ = SECP256K1.generate_keypair(&mut thread_rng());
/// let _ = SECP256K1.generate_keypair(&mut rand::thread_rng());
/// # }
/// ```
pub static SECP256K1: &GlobalContext = &GlobalContext { __private: () };
Expand Down Expand Up @@ -106,7 +105,7 @@ mod private {
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc"))))]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
mod alloc_only {
use core::marker::PhantomData;

Expand Down Expand Up @@ -176,7 +175,7 @@ mod alloc_only {
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
/// If `rand-std` feature is not enabled please consider randomizing the context as follows:
/// ```
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
/// # #[cfg(feature = "rand-std")] {
/// # use secp256k1::Secp256k1;
/// # use secp256k1::rand::{thread_rng, RngCore};
/// let mut ctx = Secp256k1::new();
Expand Down
37 changes: 17 additions & 20 deletions src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ const SHARED_SECRET_SIZE: usize = constants::SECRET_KEY_SIZE;
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
/// # use secp256k1::Secp256k1;
/// # #[cfg(feature = "rand-std")] {
/// # use secp256k1::{rand, Secp256k1};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR but this looks very confusing - it looks as if #[cfg] was controlling use, not the whole block. If we don't want to indent it I suggest we put { on the next line - this is unusual in Rust but much more readable in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The old formatting was chosen with "this line will be hidden" in mind.

I have a mild preference for indenting vs putting { on the next line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its already laborious to write example code in comments because of zero tooling support, we should be careful what we choose because we (I) will have to go back and change every example in the whole codebase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then let's do { on the next line since that's easiest :P and say that this is a very low priority thing.

/// # use secp256k1::ecdh::SharedSecret;
/// # use secp256k1::rand::thread_rng;
/// let s = Secp256k1::new();
/// let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
/// let (sk2, pk2) = s.generate_keypair(&mut thread_rng());
/// let (sk1, pk1) = s.generate_keypair(&mut rand::thread_rng());
/// let (sk2, pk2) = s.generate_keypair(&mut rand::thread_rng());
/// let sec1 = SharedSecret::new(&pk2, &sk1);
/// let sec2 = SharedSecret::new(&pk1, &sk2);
/// assert_eq!(sec1, sec2);
Expand Down Expand Up @@ -122,14 +121,13 @@ impl AsRef<[u8]> for SharedSecret {
///
/// # Examples
/// ```
/// # #[cfg(all(feature = "bitcoin-hashes-std", feature = "rand-std", feature = "std"))] {
/// # use secp256k1::{ecdh, Secp256k1, PublicKey, SecretKey};
/// # #[cfg(all(feature = "bitcoin-hashes-std", feature = "rand-std"))] {
/// # use secp256k1::{ecdh, rand, Secp256k1, PublicKey, SecretKey};
/// # use secp256k1::hashes::{Hash, sha512};
/// # use secp256k1::rand::thread_rng;
///
/// let s = Secp256k1::new();
/// let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
/// let (sk2, pk2) = s.generate_keypair(&mut thread_rng());
/// let (sk1, pk1) = s.generate_keypair(&mut rand::thread_rng());
/// let (sk2, pk2) = s.generate_keypair(&mut rand::thread_rng());
///
/// let point1 = ecdh::shared_secret_point(&pk2, &sk1);
/// let secret1 = sha512::Hash::hash(&point1);
Expand Down Expand Up @@ -201,19 +199,18 @@ impl<'de> ::serde::Deserialize<'de> for SharedSecret {
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use rand::thread_rng;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

use super::SharedSecret;
use crate::Secp256k1;

#[test]
#[cfg(all(feature = "rand-std", any(feature = "alloc", feature = "std")))]
#[cfg(feature = "rand-std")]
fn ecdh() {
let s = Secp256k1::signing_only();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
let (sk2, pk2) = s.generate_keypair(&mut thread_rng());
let (sk1, pk1) = s.generate_keypair(&mut rand::thread_rng());
let (sk2, pk2) = s.generate_keypair(&mut rand::thread_rng());

let sec1 = SharedSecret::new(&pk2, &sk1);
let sec2 = SharedSecret::new(&pk1, &sk2);
Expand Down Expand Up @@ -241,15 +238,15 @@ mod tests {

#[test]
#[cfg(not(fuzzing))]
#[cfg(all(feature = "std", feature = "rand-std", feature = "bitcoin-hashes-std"))]
#[cfg(all(feature = "bitcoin-hashes-std", feature = "rand-std"))]
fn bitcoin_hashes_and_sys_generate_same_secret() {
use bitcoin_hashes::{sha256, Hash, HashEngine};

use crate::ecdh::shared_secret_point;

let s = Secp256k1::signing_only();
let (sk1, _) = s.generate_keypair(&mut thread_rng());
let (_, pk2) = s.generate_keypair(&mut thread_rng());
let (sk1, _) = s.generate_keypair(&mut rand::thread_rng());
let (_, pk2) = s.generate_keypair(&mut rand::thread_rng());

let secret_sys = SharedSecret::new(&pk2, &sk1);

Expand All @@ -266,7 +263,7 @@ mod tests {
}

#[test]
#[cfg(all(feature = "serde", any(feature = "alloc", feature = "std")))]
#[cfg(all(feature = "serde", feature = "alloc"))]
fn serde() {
use serde_test::{assert_tokens, Configure, Token};
#[rustfmt::skip]
Expand All @@ -291,8 +288,8 @@ mod tests {
}

#[cfg(bench)]
#[cfg(feature = "rand-std")] // Currently only a single bench that requires "rand-std".
mod benches {
use rand::thread_rng;
use test::{black_box, Bencher};

use super::SharedSecret;
Expand All @@ -301,7 +298,7 @@ mod benches {
#[bench]
pub fn bench_ecdh(bh: &mut Bencher) {
let s = Secp256k1::signing_only();
let (sk, pk) = s.generate_keypair(&mut thread_rng());
let (sk, pk) = s.generate_keypair(&mut rand::thread_rng());

bh.iter(|| {
let res = SharedSecret::new(&pk, &sk);
Expand Down
7 changes: 3 additions & 4 deletions src/ecdsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,11 @@ impl<C: Verification> Secp256k1<C> {
/// verify-capable context.
///
/// ```rust
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
/// # use secp256k1::rand::thread_rng;
/// # use secp256k1::{Secp256k1, Message, Error};
/// # #[cfg(feature = "rand-std")] {
/// # use secp256k1::{rand, Secp256k1, Message, Error};
/// #
/// # let secp = Secp256k1::new();
/// # let (secret_key, public_key) = secp.generate_keypair(&mut thread_rng());
/// # let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());
/// #
/// let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
/// let sig = secp.sign_ecdsa(&message, &secret_key);
Expand Down
Loading