Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pre allocation context creation #125

Merged
merged 7 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should still export this. It's needed to estimate the allocation size in generic contexts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly didn't want to confuse the user with too many functions, some generic and some not

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