Add functions to create from raw context

This commit is contained in:
Elichai Turkel 2019-11-06 16:15:22 +02:00
parent 5de62f80f3
commit 5f8096e8a8
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
1 changed files with 59 additions and 1 deletions

View File

@ -1,4 +1,5 @@
use core::marker::PhantomData; use core::marker::PhantomData;
use ptr;
use ffi::{self, CPtr}; use ffi::{self, CPtr};
use types::{c_uint, c_void}; use types::{c_uint, c_void};
use Error; use Error;
@ -199,6 +200,25 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
pub fn preallocate_size() -> usize { pub fn preallocate_size() -> usize {
Self::preallocate_size_gen() Self::preallocate_size_gen()
} }
/// Create a context from a raw context.
///
/// # Safety
/// This is highly unsafe, due to the number of conditions that aren't checked.
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
/// when generating the context.
/// * The user must handle the freeing of the context(using the correct functions) by himself.
/// * Violating these may lead to Undefined Behavior.
///
pub unsafe fn from_raw_all(raw_ctx: *mut ffi::Context) -> Secp256k1<AllPreallocated<'buf>> {
Secp256k1 {
ctx: raw_ctx,
phantom: PhantomData,
buf: ptr::null_mut::<[u8;0]>() as *mut [u8] ,
}
}
} }
impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> { impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
@ -212,6 +232,25 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
pub fn preallocate_signing_size() -> usize { pub fn preallocate_signing_size() -> usize {
Self::preallocate_size_gen() Self::preallocate_size_gen()
} }
/// Create a context from a raw context.
///
/// # Safety
/// This is highly unsafe, due to the number of conditions that aren't checked.
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
/// when generating the context.
/// * The user must handle the freeing of the context(using the correct functions) by himself.
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.,
///
pub unsafe fn from_raw_signining_only(raw_ctx: *mut ffi::Context) -> Secp256k1<SignOnlyPreallocated<'buf>> {
Secp256k1 {
ctx: raw_ctx,
phantom: PhantomData,
buf: ptr::null_mut::<[u8;0]>() as *mut [u8] ,
}
}
} }
impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> { impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
@ -225,4 +264,23 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
pub fn preallocate_verification_size() -> usize { pub fn preallocate_verification_size() -> usize {
Self::preallocate_size_gen() Self::preallocate_size_gen()
} }
/// Create a context from a raw context.
///
/// # Safety
/// This is highly unsafe, due to the number of conditions that aren't checked.
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
/// when generating the context.
/// * The user must handle the freeing of the context(using the correct functions) by himself.
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.,
///
pub unsafe fn from_raw_verification_only(raw_ctx: *mut ffi::Context) -> Secp256k1<VerifyOnlyPreallocated<'buf>> {
Secp256k1 {
ctx: raw_ctx,
phantom: PhantomData,
buf: ptr::null_mut::<[u8;0]>() as *mut [u8] ,
}
}
} }