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

Enable build with global context less secure #387

Closed
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rand-std = ["rand/std"]
recovery = ["secp256k1-sys/recovery"]
lowmemory = ["secp256k1-sys/lowmemory"]
global-context = ["std", "rand-std", "global-context-less-secure"]
global-context-less-secure = []
global-context-less-secure = ["std"]
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather we actually fix the build rather than depend on std (see conversation at #359 for one way to do this). The reason for global-context-less-secure, in part, is that we want to support global-context without the rand dependency, in part for platforms where we can't use rand. There are some platforms (like wasm, specifically), where we can technically build without no-std, but where we may not actually have an actual std environment to call, relying on LTO to remove the calls that we can't make.

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool, I'm off for the next few days. I'll convert this to draft and re-spin when I'm back. Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Now I have read that link I think this whole PR should be closed.


[dependencies]
secp256k1-sys = { version = "0.4.2", default-features = false, path = "./secp256k1-sys" }
Expand Down
2 changes: 1 addition & 1 deletion contrib/test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh -ex

FEATURES="bitcoin_hashes global-context lowmemory rand rand-std recovery serde"
FEATURES="bitcoin_hashes global-context-less-secure global-context lowmemory rand rand-std recovery serde"

# Use toolchain if explicitly specified
if [ -n "$TOOLCHAIN" ]
Expand Down
4 changes: 2 additions & 2 deletions secp256k1-sys/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(non_camel_case_types)]
use core::{fmt, mem};
use core::fmt;

pub type c_int = i32;
pub type c_uchar = u8;
Expand Down Expand Up @@ -46,7 +46,7 @@ impl AlignedType {
}

#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
pub(crate) const ALIGN_TO: usize = mem::align_of::<AlignedType>();
pub(crate) const ALIGN_TO: usize = ::core::mem::align_of::<AlignedType>();

#[cfg(test)]
mod tests {
Expand Down
3 changes: 2 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod global {
#[cfg(feature = "global-context")]
use rand;

use std::ops::Deref;
use core::ops::Deref;
use std::sync::Once;
use {Secp256k1, All};

Expand All @@ -35,6 +35,7 @@ pub mod global {
impl Deref for GlobalContext {
type Target = Secp256k1<All>;

#[allow(unused_mut)] // Unused when "global-context" is not enabled.
fn deref(&self) -> &Self::Target {
static ONCE: Once = Once::new();
static mut CONTEXT: Option<Secp256k1<All>> = None;
Expand Down
38 changes: 36 additions & 2 deletions src/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,29 @@ use ::{SecretKey, KeyPair, to_hex};
use constants::SECRET_KEY_SIZE;

macro_rules! impl_display_secret {
// Default hasher exists only in standard library and not alloc
($thing:ident) => {
#[cfg(feature = "std")]
#[cfg(feature = "bitocoin_hashes")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
impl ::core::fmt::Debug for $thing {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
use bitcoin_hashes::sha256;

let tag = "rust-secp256k1DEBUG";

let mut engine = sha256::Hash::engine();
let tag_hash = sha256::Hash::hash(tag.as_bytes());
engine.input(&tag_hash[..]);
engine.input(&tag_hash[..]);
engine.input(&self.serialize_secret());
let hash = sha256::Hash::from_engine(e).into_inner();

f.debug_tuple(stringify!($thing))
.field(&format_args!("#{:016x}", hash))
.finish()
}
}

#[cfg(all(not(feature = "bitocoin_hashes"), feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl ::core::fmt::Debug for $thing {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
Expand All @@ -44,6 +64,15 @@ macro_rules! impl_display_secret {
.finish()
}
}

// Fallback to make sure we can build cleanly with any combination of features.
#[cfg(all(not(feature = "bitocoin_hashes"), not(feature = "std")))]
#[cfg(not(feature = "std"))]
impl ::core::fmt::Debug for $thing {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "<secret requires std or bitcoin_hashes feature to display>")
}
}
}
}

Expand Down Expand Up @@ -92,6 +121,7 @@ impl SecretKey {
/// # Example
///
/// ```
/// # #[cfg(feature = "std")] {
/// use secp256k1::ONE_KEY;
/// let key = ONE_KEY;
/// // Normal display hides value
Expand All @@ -108,6 +138,7 @@ impl SecretKey {
/// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")",
/// format!("{:?}", key.display_secret())
/// );
/// # }
/// ```
#[inline]
pub fn display_secret(&self) -> DisplaySecret {
Expand All @@ -125,6 +156,7 @@ impl KeyPair {
/// # Example
///
/// ```
/// # #[cfg(feature = "std")] {
/// use secp256k1::ONE_KEY;
/// use secp256k1::KeyPair;
/// use secp256k1::Secp256k1;
Expand All @@ -147,6 +179,8 @@ impl KeyPair {
/// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")",
/// format!("{:?}", key.display_secret())
/// );
/// # }
/// ```
#[inline]
pub fn display_secret(&self) -> DisplaySecret {
DisplaySecret { secret: self.serialize_secret() }
Expand Down