Exposed generic functions to create the Context

This commit is contained in:
Elichai Turkel 2019-07-04 21:18:36 -04:00
parent 49f0cc1c46
commit 96ca40faed
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
2 changed files with 13 additions and 11 deletions

View File

@ -7,7 +7,7 @@ use Secp256k1;
#[cfg(feature = "std")]
pub use self::std_only::*;
/// A trait for all kinds of Context's that let's you define the exact flags and a function to deallocate memory.
/// A trait for all kinds of Context's that Lets you define the exact flags and a function to deallocate memory.
/// * DO NOT * implement it for your own types.
pub unsafe trait Context {
/// Flags for the ffi.
@ -86,7 +86,8 @@ mod std_only {
}
impl<C: Context> Secp256k1<C> {
fn gen_new() -> Secp256k1<C> {
/// Lets you create a context in a generic manner(sign/verify/all)
pub fn gen_new() -> Secp256k1<C> {
let buf = vec![0u8; Self::preallocate_size_gen()].into_boxed_slice();
let ptr = Box::into_raw(buf);
Secp256k1 {
@ -148,8 +149,8 @@ unsafe impl<'buf> Context for SignOnlyPreallocated<'buf> {
const FLAGS: c_uint = ffi::SECP256K1_START_SIGN;
const DESCRIPTION: &'static str = "signing only";
fn deallocate(ptr: *mut [u8]) {
let _ = ptr;
fn deallocate(_ptr: *mut [u8]) {
// Allocated by the user
}
}
@ -157,8 +158,8 @@ unsafe impl<'buf> Context for VerifyOnlyPreallocated<'buf> {
const FLAGS: c_uint = ffi::SECP256K1_START_VERIFY;
const DESCRIPTION: &'static str = "verification only";
fn deallocate(ptr: *mut [u8]) {
let _ = ptr;
fn deallocate(_ptr: *mut [u8]) {
// Allocated by the user
}
}
@ -166,13 +167,14 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
const FLAGS: c_uint = SignOnlyPreallocated::FLAGS | VerifyOnlyPreallocated::FLAGS;
const DESCRIPTION: &'static str = "all capabilities";
fn deallocate(ptr: *mut [u8]) {
let _ = ptr;
fn deallocate(_ptr: *mut [u8]) {
// Allocated by the user
}
}
impl<'buf, C: Context + 'buf> Secp256k1<C> {
fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> {
/// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all)
pub fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> {
if buf.len() < Self::preallocate_size_gen() {
return Err(Error::NotEnoughMemory);
}

View File

@ -570,8 +570,8 @@ impl<C: Context> Secp256k1<C> {
&self.ctx
}
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context
pub(crate) fn preallocate_size_gen() -> usize {
/// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all)
pub fn preallocate_size_gen() -> usize {
unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) }
}