Skip to content

Commit

Permalink
Exposed generic functions to create the Context
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Jul 5, 2019
1 parent 49f0cc1 commit 846faae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ mod std_only {
}

impl<C: Context> Secp256k1<C> {
fn gen_new() -> Secp256k1<C> {
/// Let's 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 {
Expand Down Expand Up @@ -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
}
}

Expand All @@ -158,7 +159,7 @@ unsafe impl<'buf> Context for VerifyOnlyPreallocated<'buf> {
const DESCRIPTION: &'static str = "verification only";

fn deallocate(ptr: *mut [u8]) {
let _ = ptr;
// Allocated by the user
}
}

Expand All @@ -167,12 +168,13 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
const DESCRIPTION: &'static str = "all capabilities";

fn deallocate(ptr: *mut [u8]) {
let _ = ptr;
// Allocated by the user
}
}

impl<'buf, C: Context + 'buf> Secp256k1<C> {
fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> {
/// Let's 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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}

Expand Down

0 comments on commit 846faae

Please sign in to comment.