Skip to content

Commit

Permalink
Merge #331: Update the code to edition 2018, and update dependencies
Browse files Browse the repository at this point in the history
5d2f1ce Fix WASM build (Elichai Turkel)
39aaac6 Use new trait TryFrom and do small refactoring (Elichai Turkel)
7d3a149 Move more things from the std feature to the alloc feature (Elichai Turkel)
bc8c713 Replace c_void with core::ffi::c_void (Elichai Turkel)
26a52bc Update secp256k1-sys to edition 2018 and fix imports (Elichai Turkel)
ebe46a4 Update rand to 0.8 and replace CounterRng with mock::StepRng (Elichai Turkel)
626835f Update secp256k1 to edition 2018 and fix imports (Elichai Turkel)
67c0922 Update MSRV in CI and Readme from 1.29 to 1.41 (Elichai Turkel)

Pull request description:

  As proposed in rust-bitcoin/rust-bitcoin#510 (comment) this PR raises the MSRV to 1.41.1 it also changes the code to be Edition 2018.

  The PR contains a few things:
  * Moving to edition 2018 and fixing the imports
  * Sorting and combining imports to make them more concise
  * Replacing our c_void with `core::ffi::c_void`
  * Bumping the `rand` version to latest and modifying our `RngCore` implementations accordingly
  * Doing some small refactoring and using the new `TryInto` trait where it makes the code nicer

  If people prefer I can split this PR into multiple and/or drop some commits

ACKs for top commit:
  tcharding:
    ACK 5d2f1ce
  apoelstra:
    ACK 5d2f1ce

Tree-SHA512: 5bf84e7ebb6286d59f8cada0bb712c46336f0dd6c35b67e6f4ba323b5484ad925b99b73e778ae4608f123938e7ee8705a0aec576cd9c065072c4ecf1248e3470
  • Loading branch information
apoelstra committed Jun 8, 2022
2 parents 33f76e4 + 5d2f1ce commit 4f7f138
Show file tree
Hide file tree
Showing 19 changed files with 258 additions and 416 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/rust.yml
Expand Up @@ -36,7 +36,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: [stable, beta, nightly, 1.29.0]
rust: [stable, beta, nightly, 1.41.1]
steps:
- name: Checkout Crate
uses: actions/checkout@v2
Expand All @@ -46,9 +46,6 @@ jobs:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- name: Pin cc if rust 1.29
if: matrix.rust == '1.29.0'
run: cargo generate-lockfile --verbose && cargo update -p cc --precise "1.0.41" --verbose
- name: Running cargo
env:
DO_FEATURE_MATRIX: true
Expand All @@ -59,7 +56,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: [stable, beta, nightly] # No 1.29 because WASM requires Rust 1.30
rust: [stable, beta, nightly] # wasm-pack doesn't support rust 1.41.1
steps:
- name: Checkout Crate
uses: actions/checkout@v2
Expand Down
16 changes: 8 additions & 8 deletions Cargo.toml
Expand Up @@ -10,7 +10,7 @@ documentation = "https://docs.rs/secp256k1/"
description = "Rust wrapper library for Pieter Wuille's `libsecp256k1`. Implements ECDSA and BIP 340 signatures for the SECG elliptic curve group secp256k1 and related utilities."
keywords = [ "crypto", "ECDSA", "secp256k1", "libsecp256k1", "bitcoin" ]
readme = "README.md"
autoexamples = false # Remove when edition 2018 https://github.com/rust-lang/cargo/issues/5330
edition = "2018"

# Should make docs.rs show all functions, even those behind non-default features
[package.metadata.docs.rs]
Expand All @@ -20,10 +20,10 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
unstable = ["recovery", "rand-std"]
default = ["std"]
std = ["secp256k1-sys/std"]
std = ["alloc", "secp256k1-sys/std"]
# allow use of Secp256k1::new and related API that requires an allocator
alloc = []
bitcoin-hashes-std = ["bitcoin_hashes/std"]
alloc = ["secp256k1-sys/alloc"]
bitcoin-hashes-std = ["bitcoin_hashes/std"]
rand-std = ["rand/std"]
recovery = ["secp256k1-sys/recovery"]
lowmemory = ["secp256k1-sys/lowmemory"]
Expand All @@ -43,17 +43,17 @@ serde = { version = "1.0", default-features = false, optional = true }
# You likely only want to enable these if you explicitly do not want to use "std", otherwise enable
# the respective -std feature e.g., bitcoin-hashes-std
bitcoin_hashes = { version = "0.10", default-features = false, optional = true }
rand = { version = "0.6", default-features = false, optional = true }
rand = { version = "0.8", default-features = false, optional = true }

[dev-dependencies]
rand = "0.6"
rand_core = "0.4"
rand = "0.8"
rand_core = "0.6"
serde_test = "1.0"
bitcoin_hashes = "0.10"

[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.3"
rand = { version = "0.6", features = ["wasm-bindgen"] }
getrandom = { version = "0.2", features = ["js"] }


[[example]]
Expand Down
16 changes: 1 addition & 15 deletions README.md
Expand Up @@ -20,21 +20,7 @@ Contributions to this library are welcome. A few guidelines:
* Any breaking changes must have an accompanied entry in CHANGELOG.md
* No new dependencies, please.
* No crypto should be implemented in Rust, with the possible exception of hash functions. Cryptographic contributions should be directed upstream to libsecp256k1.
* This library should always compile with any combination of features on **Rust 1.29**.

## A note on Rust 1.29 support

The build dependency `cc` might require a more recent version of the Rust compiler.
To ensure compilation with Rust 1.29.0, pin its version in your `Cargo.lock`
with `cargo update -p cc --precise 1.0.41`. If you're using `secp256k1` in a library,
to make sure it compiles in CI, you'll need to generate a lockfile first.
Example for Travis CI:
```yml
before_script:
- if [ "$TRAVIS_RUST_VERSION" == "1.29.0" ]; then
cargo generate-lockfile --verbose && cargo update -p cc --precise "1.0.41" --verbose;
fi
```
* This library should always compile with any combination of features on **Rust 1.41.1**.

## Fuzzing

Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
@@ -1 +1 @@
msrv = "1.29"
msrv = "1.41.1"
4 changes: 2 additions & 2 deletions examples/generate_keys.rs
@@ -1,11 +1,11 @@
extern crate secp256k1;

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

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

Expand Down
4 changes: 3 additions & 1 deletion secp256k1-sys/Cargo.toml
Expand Up @@ -13,6 +13,7 @@ keywords = [ "secp256k1", "libsecp256k1", "ffi" ]
readme = "README.md"
build = "build.rs"
links = "rustsecp256k1_v0_5_0"
edition = "2018"

# Should make docs.rs show all functions, even those behind non-default features
[package.metadata.docs.rs]
Expand All @@ -29,5 +30,6 @@ libc = "0.2"
default = ["std"]
recovery = []
lowmemory = []
std = []
std = ["alloc"]
alloc = []

29 changes: 14 additions & 15 deletions secp256k1-sys/src/lib.rs
Expand Up @@ -17,21 +17,20 @@
//! not be needed for most users.

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

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

#[cfg(any(test, feature = "std"))]
extern crate core;

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(fuzzing)]
const THIS_UNUSED_CONSTANT_IS_YOUR_WARNING_THAT_ALL_THE_CRYPTO_IN_THIS_LIB_IS_DISABLED_FOR_FUZZING: usize = 0;

#[macro_use]
mod macros;
pub mod types;

Expand Down Expand Up @@ -585,11 +584,11 @@ extern "C" {
///
/// The newly created secp256k1 raw context.
#[no_mangle]
#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))]
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
pub unsafe extern "C" fn rustsecp256k1_v0_5_0_context_create(flags: c_uint) -> *mut Context {
use core::mem;
use std::alloc;
use crate::alloc::alloc;
assert!(ALIGN_TO >= mem::align_of::<usize>());
assert!(ALIGN_TO >= mem::align_of::<&usize>());
assert!(ALIGN_TO >= mem::size_of::<usize>());
Expand All @@ -605,8 +604,8 @@ pub unsafe extern "C" fn rustsecp256k1_v0_5_0_context_create(flags: c_uint) -> *
secp256k1_context_preallocated_create(ptr, flags)
}

#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))]
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context {
rustsecp256k1_v0_5_0_context_create(flags)
}
Expand All @@ -618,19 +617,19 @@ pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context {
/// The pointer shouldn't be used after passing to this function, consider it as passing it to `free()`.
///
#[no_mangle]
#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))]
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
pub unsafe extern "C" fn rustsecp256k1_v0_5_0_context_destroy(ctx: *mut Context) {
use std::alloc;
use crate::alloc::alloc;
secp256k1_context_preallocated_destroy(ctx);
let ptr = (ctx as *mut u8).sub(ALIGN_TO);
let bytes = (ptr as *mut usize).read();
let layout = alloc::Layout::from_size_align(bytes, ALIGN_TO).unwrap();
alloc::dealloc(ptr, layout);
}

#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))]
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) {
rustsecp256k1_v0_5_0_context_destroy(ctx)
}
Expand Down
30 changes: 15 additions & 15 deletions secp256k1-sys/src/macros.rs
Expand Up @@ -69,14 +69,14 @@ macro_rules! impl_array_newtype {

impl PartialOrd for $thing {
#[inline]
fn partial_cmp(&self, other: &$thing) -> Option<::core::cmp::Ordering> {
fn partial_cmp(&self, other: &$thing) -> Option<core::cmp::Ordering> {
self[..].partial_cmp(&other[..])
}
}

impl Ord for $thing {
#[inline]
fn cmp(&self, other: &$thing) -> ::core::cmp::Ordering {
fn cmp(&self, other: &$thing) -> core::cmp::Ordering {
self[..].cmp(&other[..])
}
}
Expand All @@ -89,7 +89,7 @@ macro_rules! impl_array_newtype {
}
}

impl ::core::ops::Index<usize> for $thing {
impl core::ops::Index<usize> for $thing {
type Output = $ty;

#[inline]
Expand All @@ -99,41 +99,41 @@ macro_rules! impl_array_newtype {
}
}

impl ::core::ops::Index<::core::ops::Range<usize>> for $thing {
impl core::ops::Index<core::ops::Range<usize>> for $thing {
type Output = [$ty];

#[inline]
fn index(&self, index: ::core::ops::Range<usize>) -> &[$ty] {
fn index(&self, index: core::ops::Range<usize>) -> &[$ty] {
let &$thing(ref dat) = self;
&dat[index]
}
}

impl ::core::ops::Index<::core::ops::RangeTo<usize>> for $thing {
impl core::ops::Index<core::ops::RangeTo<usize>> for $thing {
type Output = [$ty];

#[inline]
fn index(&self, index: ::core::ops::RangeTo<usize>) -> &[$ty] {
fn index(&self, index: core::ops::RangeTo<usize>) -> &[$ty] {
let &$thing(ref dat) = self;
&dat[index]
}
}

impl ::core::ops::Index<::core::ops::RangeFrom<usize>> for $thing {
impl core::ops::Index<core::ops::RangeFrom<usize>> for $thing {
type Output = [$ty];

#[inline]
fn index(&self, index: ::core::ops::RangeFrom<usize>) -> &[$ty] {
fn index(&self, index: core::ops::RangeFrom<usize>) -> &[$ty] {
let &$thing(ref dat) = self;
&dat[index]
}
}

impl ::core::ops::Index<::core::ops::RangeFull> for $thing {
impl core::ops::Index<core::ops::RangeFull> for $thing {
type Output = [$ty];

#[inline]
fn index(&self, _: ::core::ops::RangeFull) -> &[$ty] {
fn index(&self, _: core::ops::RangeFull) -> &[$ty] {
let &$thing(ref dat) = self;
&dat[..]
}
Expand All @@ -142,15 +142,15 @@ macro_rules! impl_array_newtype {
type Target = $ty;
fn as_c_ptr(&self) -> *const Self::Target {
if self.is_empty() {
::core::ptr::null()
core::ptr::null()
} else {
self.as_ptr()
}
}

fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
if self.is_empty() {
::core::ptr::null::<Self::Target>() as *mut _
core::ptr::null::<Self::Target>() as *mut _
} else {
self.as_mut_ptr()
}
Expand All @@ -162,8 +162,8 @@ macro_rules! impl_array_newtype {
#[macro_export]
macro_rules! impl_raw_debug {
($thing:ident) => {
impl ::core::fmt::Debug for $thing {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
impl core::fmt::Debug for $thing {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
for i in self[..].iter().cloned() {
write!(f, "{:02x}", i)?;
}
Expand Down
19 changes: 8 additions & 11 deletions secp256k1-sys/src/recovery.rs
Expand Up @@ -15,9 +15,9 @@

//! # FFI of the recovery module

use ::types::*;
use ::core::fmt;
use {Context, Signature, NonceFn, PublicKey, CPtr};
use crate::{Context, Signature, NonceFn, PublicKey, CPtr, impl_array_newtype};
use crate::types::*;
use core::fmt;

/// Library-internal representation of a Secp256k1 signature + recovery ID
#[repr(C)]
Expand Down Expand Up @@ -98,13 +98,10 @@ extern "C" {

#[cfg(fuzzing)]
mod fuzz_dummy {
use super::*;
use std::slice;
use core::slice;

use secp256k1_ec_pubkey_create;
use secp256k1_ec_pubkey_parse;
use secp256k1_ec_pubkey_serialize;
use SECP256K1_SER_COMPRESSED;
use crate::{secp256k1_ec_pubkey_create, secp256k1_ec_pubkey_parse, secp256k1_ec_pubkey_serialize, SECP256K1_SER_COMPRESSED};
use super::*;

/// Sets sig to msg32||full pk
pub unsafe fn secp256k1_ecdsa_sign_recoverable(
Expand Down Expand Up @@ -170,6 +167,6 @@ mod fuzz_dummy {
1
}
}
#[cfg(fuzzing)]
pub use self::fuzz_dummy::*;

#[cfg(fuzzing)]
pub use self::fuzz_dummy::*;

0 comments on commit 4f7f138

Please sign in to comment.