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 96ca40f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -148,31 +149,32 @@ 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
}
}

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
}
}

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);
}
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 96ca40f

Please sign in to comment.