Skip to content

Commit 65a2b4f

Browse files
authored
Merge pull request #300 from devrandom/alloc
New alloc feature
2 parents 5ff59f7 + b5ff47a commit 65a2b4f

File tree

6 files changed

+47
-5
lines changed

6 files changed

+47
-5
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ features = [ "rand", "rand-std", "serde", "recovery" ]
2020
unstable = ["recovery", "rand-std"]
2121
default = ["std"]
2222
std = ["secp256k1-sys/std"]
23+
# allow use of Secp256k1::new and related API that requires an allocator
24+
alloc = []
2325
rand-std = ["rand/std"]
2426
recovery = ["secp256k1-sys/recovery"]
2527
lowmemory = ["secp256k1-sys/lowmemory"]

contrib/test.sh

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ if [ "$DO_ASAN" = true ]; then
7676
RUSTFLAGS='-Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes' \
7777
cargo test --lib --all --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu &&
7878
cargo run --release --manifest-path=./no_std_test/Cargo.toml | grep -q "Verified Successfully"
79+
cargo run --release --features=alloc --manifest-path=./no_std_test/Cargo.toml | grep -q "Verified alloc Successfully"
7980
fi
8081

8182
# Test if panic in C code aborts the process (either with a real panic or with SIGILL)

no_std_test/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ name = "no_std_test"
33
version = "0.1.0"
44
authors = ["Elichai Turkel <[email protected]>"]
55

6+
[features]
7+
alloc = ["secp256k1/alloc", "wee_alloc"]
8+
69
[dependencies]
10+
wee_alloc = { version = "0.4.5", optional = true }
711
secp256k1 = { path = "../", default-features = false, features = ["serde", "rand", "recovery"] }
812
libc = { version = "0.2", default-features = false }
913
serde_cbor = { version = "0.10", default-features = false } # A random serializer that supports no-std.

no_std_test/src/main.rs

+30
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,24 @@
4343
#![feature(start)]
4444
#![feature(core_intrinsics)]
4545
#![feature(panic_info_message)]
46+
#![feature(alloc_error_handler)]
4647
#![no_std]
4748
extern crate libc;
4849
extern crate secp256k1;
4950
extern crate serde_cbor;
5051

52+
#[cfg(feature = "alloc")]
53+
extern crate alloc;
54+
55+
use core::alloc::Layout;
56+
57+
#[cfg(feature = "alloc")]
58+
extern crate wee_alloc;
59+
60+
#[cfg(feature = "alloc")]
61+
#[global_allocator]
62+
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
63+
5164
use core::fmt::{self, write, Write};
5265
use core::intrinsics;
5366
use core::panic::PanicInfo;
@@ -120,6 +133,17 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
120133
assert_ne!(x_arr, [0u8; 32]);
121134
assert_ne!(&y_arr[..], &[0u8; 32][..]);
122135

136+
#[cfg(feature = "alloc")]
137+
{
138+
let secp_alloc = Secp256k1::new();
139+
let public_key = PublicKey::from_secret_key(&secp_alloc, &secret_key);
140+
let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
141+
142+
let sig = secp_alloc.sign(&message, &secret_key);
143+
assert!(secp_alloc.verify(&message, &sig, &public_key).is_ok());
144+
unsafe { libc::printf("Verified alloc Successfully!\n\0".as_ptr() as _) };
145+
}
146+
123147
unsafe { libc::printf("Verified Successfully!\n\0".as_ptr() as _) };
124148
0
125149
}
@@ -171,3 +195,9 @@ fn panic(info: &PanicInfo) -> ! {
171195
buf.print();
172196
intrinsics::abort()
173197
}
198+
199+
#[alloc_error_handler]
200+
fn alloc_error(_layout: Layout) -> ! {
201+
unsafe { libc::printf("alloc shi1\n\0".as_ptr() as _) };
202+
intrinsics::abort()
203+
}

src/context.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use ffi::types::{c_uint, c_void};
55
use Error;
66
use Secp256k1;
77

8-
#[cfg(feature = "std")]
9-
pub use self::std_only::*;
8+
#[cfg(any(feature = "std", feature = "alloc"))]
9+
pub use self::alloc_only::*;
1010

1111
#[cfg(feature = "global-context-less-secure")]
1212
/// Module implementing a singleton pattern for a global `Secp256k1` context
@@ -93,14 +93,18 @@ mod private {
9393
impl<'buf> Sealed for SignOnlyPreallocated<'buf> {}
9494
}
9595

96-
#[cfg(feature = "std")]
97-
mod std_only {
96+
#[cfg(any(feature = "std", feature = "alloc"))]
97+
mod alloc_only {
98+
#[cfg(feature = "std")]
99+
use std::alloc;
100+
#[cfg(not(feature = "std"))]
101+
use alloc::alloc;
102+
98103
impl private::Sealed for SignOnly {}
99104
impl private::Sealed for All {}
100105
impl private::Sealed for VerifyOnly {}
101106

102107
use super::*;
103-
use std::alloc;
104108
const ALIGN_TO: usize = mem::align_of::<AlignedType>();
105109

106110
/// Represents the set of capabilities needed for signing.

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub use secp256k1_sys as ffi;
135135
#[cfg(any(test, feature = "rand"))] use rand::Rng;
136136
#[cfg(any(test, feature = "std"))] extern crate core;
137137
#[cfg(all(test, target_arch = "wasm32"))] extern crate wasm_bindgen_test;
138+
#[cfg(feature = "alloc")] extern crate alloc;
138139

139140
use core::{fmt, ptr, str};
140141

0 commit comments

Comments
 (0)