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

Small modernization after MSRV bump #232

Merged
merged 4 commits into from Oct 4, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -62,7 +62,7 @@ script:
- if [ ${TRAVIS_RUST_VERSION} == "stable" -a "$TRAVIS_OS_NAME" = "linux" ]; then
clang --version &&
CARGO_TARGET_DIR=wasm cargo install --verbose --force wasm-pack &&
sed -i 's/\[lib\]/[lib]\ncrate-type = ["cdylib", "rlib"]/' Cargo.toml &&
printf '\n[lib]\ncrate-type = ["cdylib", "rlib"]\n' >> Cargo.toml &&
CC=clang-9 wasm-pack build &&
CC=clang-9 wasm-pack test --node;
fi
22 changes: 4 additions & 18 deletions Cargo.toml
@@ -1,5 +1,4 @@
[package]

name = "secp256k1"
version = "0.19.0"
authors = [ "Dawid Ciężarkiewicz <dpc@ucore.info>",
Expand All @@ -17,10 +16,6 @@ autoexamples = false # Remove when edition 2018 https://github.com/rust-lang/car
[package.metadata.docs.rs]
features = [ "rand", "rand-std", "serde", "recovery", "endomorphism" ]

[lib]
name = "secp256k1"
path = "src/lib.rs"

[features]
unstable = ["recovery", "rand-std"]
default = ["std"]
Expand All @@ -40,6 +35,10 @@ fuzztarget = ["secp256k1-sys/fuzztarget"]

[dependencies]
secp256k1-sys = { version = "0.3.0", default-features = false, path = "./secp256k1-sys" }
bitcoin_hashes = { version = "0.9", optional = true }
rand = { version = "0.6", default-features = false, optional = true }
serde = { version = "1.0", default-features = false, optional = true }


[dev-dependencies]
rand = "0.6"
Expand All @@ -51,19 +50,6 @@ bitcoin_hashes = "0.9"
wasm-bindgen-test = "0.3"
rand = { version = "0.6", features = ["wasm-bindgen"] }

[dependencies.bitcoin_hashes]
version = "0.9"
optional = true

[dependencies.rand]
version = "0.6"
optional = true
default-features = false

[dependencies.serde]
version = "1.0"
optional = true
default-features = false

[[example]]
name = "sign_verify_recovery"
Expand Down
4 changes: 0 additions & 4 deletions secp256k1-sys/Cargo.toml
Expand Up @@ -21,10 +21,6 @@ features = [ "recovery", "endomorphism", "lowmemory" ]
[build-dependencies]
cc = "1.0.28"

[lib]
name = "secp256k1_sys"
path = "src/lib.rs"

[features]
default = ["std"]
recovery = []
Expand Down
23 changes: 7 additions & 16 deletions secp256k1-sys/src/lib.rs
Expand Up @@ -16,16 +16,13 @@
//! Direct bindings to the underlying C library functions. These should
//! not be needed for most users.

#![crate_type = "lib"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![crate_name = "secp256k1_sys"]

#![cfg_attr(all(not(test), not(fuzztarget), not(feature = "std")), no_std)]
#![cfg_attr(feature = "dev", allow(unstable_features))]
#![cfg_attr(feature = "dev", feature(plugin))]
#![cfg_attr(feature = "dev", plugin(clippy))]
// Coding conventions
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]

#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#[cfg(any(test, feature = "std"))]
extern crate core;

Expand Down Expand Up @@ -97,9 +94,6 @@ impl_raw_debug!(PublicKey);
impl PublicKey {
/// Create a new (zeroed) public key usable for the FFI interface
pub fn new() -> PublicKey { PublicKey([0; 64]) }
/// Create a new (uninitialized) public key usable for the FFI interface
#[deprecated(since = "0.15.3", note = "Please use the new function instead")]
pub unsafe fn blank() -> PublicKey { PublicKey::new() }
}

impl Default for PublicKey {
Expand All @@ -123,9 +117,6 @@ impl_raw_debug!(Signature);
impl Signature {
/// Create a new (zeroed) signature usable for the FFI interface
pub fn new() -> Signature { Signature([0; 64]) }
/// Create a new (uninitialized) signature usable for the FFI interface
#[deprecated(since = "0.15.3", note = "Please use the new function instead")]
pub unsafe fn blank() -> Signature { Signature::new() }
}

impl Default for Signature {
Expand Down Expand Up @@ -467,7 +458,7 @@ mod fuzz_dummy {
use self::std::{ptr, mem};
use self::std::boxed::Box;
use types::*;
use ::{Signature, Context, NonceFn, EcdhHashFn, PublicKey,
use {Signature, Context, NonceFn, EcdhHashFn, PublicKey,
SECP256K1_START_NONE, SECP256K1_START_VERIFY, SECP256K1_START_SIGN,
SECP256K1_SER_COMPRESSED, SECP256K1_SER_UNCOMPRESSED};

Expand Down
2 changes: 1 addition & 1 deletion secp256k1-sys/src/macros.rs
Expand Up @@ -130,7 +130,7 @@ macro_rules! impl_array_newtype {
&dat[..]
}
}
impl ::CPtr for $thing {
impl $crate::CPtr for $thing {
type Target = $ty;
fn as_c_ptr(&self) -> *const Self::Target {
if self.is_empty() {
Expand Down
3 changes: 0 additions & 3 deletions secp256k1-sys/src/recovery.rs
Expand Up @@ -28,9 +28,6 @@ impl_raw_debug!(RecoverableSignature);
impl RecoverableSignature {
/// Create a new (zeroed) signature usable for the FFI interface
pub fn new() -> RecoverableSignature { RecoverableSignature([0; 65]) }
/// Create a new (uninitialized) signature usable for the FFI interface
#[deprecated(since = "0.15.3", note = "Please use the new function instead")]
pub unsafe fn blank() -> RecoverableSignature { RecoverableSignature::new() }
}

impl Default for RecoverableSignature {
Expand Down
2 changes: 1 addition & 1 deletion src/context.rs
Expand Up @@ -14,7 +14,7 @@ pub use self::std_only::*;
pub mod global {
use std::ops::Deref;
use std::sync::Once;
use ::{Secp256k1, All};
use {Secp256k1, All};

/// Proxy struct for global `SECP256K1` context
pub struct GlobalContext {
Expand Down
58 changes: 12 additions & 46 deletions src/lib.rs
Expand Up @@ -37,18 +37,10 @@
//! trigger any assertion failures in the upstream library.
//!
//! ```rust
//! extern crate secp256k1;
//! # #[cfg(feature="bitcoin_hashes")]
//! extern crate bitcoin_hashes;
//! # #[cfg(feature="rand")]
//! extern crate rand;
//!
//! #
//! # fn main() {
//! # #[cfg(all(feature="rand", feature="bitcoin_hashes"))] {
//! use rand::rngs::OsRng;
//! use secp256k1::rand::rngs::OsRng;
//! use secp256k1::{Secp256k1, Message};
//! use bitcoin_hashes::sha256;
//! use secp256k1::bitcoin_hashes::sha256;
//!
//! let secp = Secp256k1::new();
//! let mut rng = OsRng::new().expect("OsRng");
Expand All @@ -57,15 +49,14 @@
//!
//! let sig = secp.sign(&message, &secret_key);
//! assert!(secp.verify(&message, &sig, &public_key).is_ok());
//! # } }
//! # }
//! ```
//!
//! The above code requires `rust-secp256k1` to be compiled with the `rand` and `bitcoin_hashes`
//! feature enabled, to get access to [`generate_keypair`](struct.Secp256k1.html#method.generate_keypair)
//! Alternately, keys and messages can be parsed from slices, like
//!
//! ```rust
//! # fn main() {
//! use self::secp256k1::{Secp256k1, Message, SecretKey, PublicKey};
//!
//! let secp = Secp256k1::new();
Expand All @@ -77,13 +68,11 @@
//!
//! let sig = secp.sign(&message, &secret_key);
//! assert!(secp.verify(&message, &sig, &public_key).is_ok());
//! # }
//! ```
//!
//! Users who only want to verify signatures can use a cheaper context, like so:
//!
//! ```rust
//! # fn main() {
//! use secp256k1::{Secp256k1, Message, Signature, PublicKey};
//!
//! let secp = Secp256k1::verification_only();
Expand Down Expand Up @@ -115,44 +104,28 @@
//! ]).expect("compact signatures are 64 bytes; DER signatures are 68-72 bytes");
//!
//! assert!(secp.verify(&message, &sig, &public_key).is_ok());
//! # }
//! ```
//!
//! Observe that the same code using, say [`signing_only`](struct.Secp256k1.html#method.signing_only)
//! to generate a context would simply not compile.
//!

#![crate_type = "lib"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![crate_name = "secp256k1"]

// Coding conventions
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![warn(missing_docs)]

// In general, rust is absolutely horrid at supporting users doing things like,
// for example, compiling Rust code for real environments. Disable useless lints
// that don't do anything but annoy us and cant actually ever be resolved.
#![allow(bare_trait_objects)]
#![allow(ellipsis_inclusive_range_patterns)]

#![cfg_attr(feature = "dev", allow(unstable_features))]
#![cfg_attr(feature = "dev", feature(plugin))]
#![cfg_attr(feature = "dev", plugin(clippy))]


#![cfg_attr(all(not(test), not(fuzztarget), not(feature = "std")), no_std)]
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#![cfg_attr(all(test, feature = "unstable"), feature(test))]

#[macro_use]
pub extern crate secp256k1_sys;
pub use secp256k1_sys as ffi;

#[cfg(feature = "bitcoin_hashes")] extern crate bitcoin_hashes;
#[cfg(feature = "bitcoin_hashes")] pub extern crate bitcoin_hashes;
#[cfg(all(test, feature = "unstable"))] extern crate test;
#[cfg(any(test, feature = "rand"))] pub extern crate rand;
#[cfg(any(test))] extern crate rand_core;
Expand Down Expand Up @@ -575,9 +548,7 @@ impl fmt::Display for Error {
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn description(&self) -> &str { self.as_str() }
}
impl std::error::Error for Error {}


/// The secp256k1 engine, used to execute all signature operations
Expand Down Expand Up @@ -676,7 +647,7 @@ impl<C: Context> Secp256k1<C> {
// However, if this DOES fail, the result is potentially weaker side-channel
// resistance, which is deadly and undetectable, so we take out the entire
// thread to be on the safe side.
assert!(err == 1);
assert_eq!(err, 1);
}
}

Expand Down Expand Up @@ -723,13 +694,8 @@ impl<C: Verification> Secp256k1<C> {
/// verify-capable context.
///
/// ```rust
/// # extern crate secp256k1;
/// # #[cfg(feature="rand")]
/// # extern crate rand;
/// #
/// # fn main() {
/// # #[cfg(feature="rand")] {
/// # use rand::OsRng;
/// # use secp256k1::rand::rngs::OsRng;
/// # use secp256k1::{Secp256k1, Message, Error};
/// #
/// # let secp = Secp256k1::new();
Expand All @@ -742,7 +708,7 @@ impl<C: Verification> Secp256k1<C> {
///
/// let message = Message::from_slice(&[0xcd; 32]).expect("32 bytes");
/// assert_eq!(secp.verify(&message, &sig, &public_key), Err(Error::IncorrectSignature));
/// # } }
/// # }
/// ```
#[inline]
pub fn verify(&self, msg: &Message, sig: &Signature, pk: &key::PublicKey) -> Result<(), Error> {
Expand All @@ -769,9 +735,9 @@ fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
for c in hex.bytes() {
b <<= 4;
match c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => return Err(()),
}
if (idx & 1) == 1 {
Expand Down