Skip to content

Commit

Permalink
Added preallocation size functions and added a test for the prealloca…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
elichai committed Jul 4, 2019
1 parent 1c0ae7d commit 9186f02
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
26 changes: 21 additions & 5 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod std_only {

impl<C: Context> Secp256k1<C> {
fn gen_new() -> Secp256k1<C> {
let buf = vec![0u8; Self::preallocate_size()].into_boxed_slice();
let buf = vec![0u8; Self::preallocate_size_gen()].into_boxed_slice();
let ptr = Box::into_raw(buf);
Secp256k1 {
ctx: unsafe { ffi::secp256k1_context_preallocated_create(ptr as *mut c_void, C::FLAGS) },
Expand Down Expand Up @@ -170,14 +170,14 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {

impl<'buf, C: Context + 'buf> Secp256k1<C> {
fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> {
if buf.len() < Self::preallocate_size() {
if buf.len() < Self::preallocate_size_gen() {
return Err(Error::NotEnoughMemory);
}
Ok(Secp256k1 {
ctx: unsafe {
ffi::secp256k1_context_preallocated_create(
buf.as_mut_ptr() as *mut c_void,
AllPreallocated::FLAGS)
C::FLAGS)
},
phantom: PhantomData,
buf: buf as *mut [u8],
Expand All @@ -190,18 +190,34 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
pub fn preallocated_new(buf: &'buf mut [u8]) -> Result<Secp256k1<AllPreallocated<'buf>>, Error> {
Secp256k1::preallocated_gen_new(buf)
}
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context
pub fn preallocate_size() -> usize {
Self::preallocate_size_gen()
}
}

impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
/// Creates a new Secp256k1 context that can only be used for signing
pub fn preallocated_new(buf: &'buf mut [u8]) -> Result<Secp256k1<SignOnlyPreallocated<'buf>>, Error> {
pub fn preallocated_signing_only(buf: &'buf mut [u8]) -> Result<Secp256k1<SignOnlyPreallocated<'buf>>, Error> {
Secp256k1::preallocated_gen_new(buf)
}

/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context
#[inline]
pub fn preallocate_signing_size() -> usize {
Self::preallocate_size_gen()
}
}

impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
/// Creates a new Secp256k1 context that can only be used for verification
pub fn preallocated_new(buf: &'buf mut [u8]) -> Result<Secp256k1<VerifyOnlyPreallocated<'buf>>, Error> {
pub fn preallocated_verification_only(buf: &'buf mut [u8]) -> Result<Secp256k1<VerifyOnlyPreallocated<'buf>>, Error> {
Secp256k1::preallocated_gen_new(buf)
}

/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context
#[inline]
pub fn preallocate_verification_size() -> usize {
Self::preallocate_size_gen()
}
}
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ impl<C: Context> Secp256k1<C> {
}

/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context
pub fn preallocate_size() -> usize {
pub(crate) fn preallocate_size_gen() -> usize {
unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) }
}

Expand Down Expand Up @@ -695,6 +695,30 @@ mod tests {
});
}

#[test]
fn test_preallocation() {
let mut buf_ful = vec![0u8; Secp256k1::preallocate_size()];
let mut buf_sign = vec![0u8; Secp256k1::preallocate_signing_size()];
let mut buf_vfy = vec![0u8; Secp256k1::preallocate_verification_size()];
//
let full = Secp256k1::preallocated_new(&mut buf_ful).unwrap();
let sign = Secp256k1::preallocated_signing_only(&mut buf_sign).unwrap();
let vrfy = Secp256k1::preallocated_verification_only(&mut buf_vfy).unwrap();

// drop(buf_vfy); // The buffer can't get dropped before the context.
// println!("{:?}", buf_ful[5]); // Can't even read the data thanks to the borrow checker.

let (sk, pk) = full.generate_keypair(&mut thread_rng());
let msg = Message::from_slice(&[2u8; 32]).unwrap();
// Try signing
assert_eq!(sign.sign(&msg, &sk), full.sign(&msg, &sk));
let sig = full.sign(&msg, &sk);

// Try verifying
assert!(vrfy.verify(&msg, &sig, &pk).is_ok());
assert!(full.verify(&msg, &sig, &pk).is_ok());
}

#[test]
fn capabilities() {
let sign = Secp256k1::signing_only();
Expand Down

0 comments on commit 9186f02

Please sign in to comment.