Skip to content

Commit 5f8096e

Browse files
committed
Add functions to create from raw context
1 parent 5de62f8 commit 5f8096e

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

src/context.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::marker::PhantomData;
2+
use ptr;
23
use ffi::{self, CPtr};
34
use types::{c_uint, c_void};
45
use Error;
@@ -199,6 +200,25 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
199200
pub fn preallocate_size() -> usize {
200201
Self::preallocate_size_gen()
201202
}
203+
204+
/// Create a context from a raw context.
205+
///
206+
/// # Safety
207+
/// This is highly unsafe, due to the number of conditions that aren't checked.
208+
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
209+
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
210+
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
211+
/// when generating the context.
212+
/// * The user must handle the freeing of the context(using the correct functions) by himself.
213+
/// * Violating these may lead to Undefined Behavior.
214+
///
215+
pub unsafe fn from_raw_all(raw_ctx: *mut ffi::Context) -> Secp256k1<AllPreallocated<'buf>> {
216+
Secp256k1 {
217+
ctx: raw_ctx,
218+
phantom: PhantomData,
219+
buf: ptr::null_mut::<[u8;0]>() as *mut [u8] ,
220+
}
221+
}
202222
}
203223

204224
impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
@@ -212,6 +232,25 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
212232
pub fn preallocate_signing_size() -> usize {
213233
Self::preallocate_size_gen()
214234
}
235+
236+
/// Create a context from a raw context.
237+
///
238+
/// # Safety
239+
/// This is highly unsafe, due to the number of conditions that aren't checked.
240+
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
241+
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
242+
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
243+
/// when generating the context.
244+
/// * The user must handle the freeing of the context(using the correct functions) by himself.
245+
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.,
246+
///
247+
pub unsafe fn from_raw_signining_only(raw_ctx: *mut ffi::Context) -> Secp256k1<SignOnlyPreallocated<'buf>> {
248+
Secp256k1 {
249+
ctx: raw_ctx,
250+
phantom: PhantomData,
251+
buf: ptr::null_mut::<[u8;0]>() as *mut [u8] ,
252+
}
253+
}
215254
}
216255

217256
impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
@@ -225,4 +264,23 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
225264
pub fn preallocate_verification_size() -> usize {
226265
Self::preallocate_size_gen()
227266
}
228-
}
267+
268+
/// Create a context from a raw context.
269+
///
270+
/// # Safety
271+
/// This is highly unsafe, due to the number of conditions that aren't checked.
272+
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
273+
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
274+
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
275+
/// when generating the context.
276+
/// * The user must handle the freeing of the context(using the correct functions) by himself.
277+
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.,
278+
///
279+
pub unsafe fn from_raw_verification_only(raw_ctx: *mut ffi::Context) -> Secp256k1<VerifyOnlyPreallocated<'buf>> {
280+
Secp256k1 {
281+
ctx: raw_ctx,
282+
phantom: PhantomData,
283+
buf: ptr::null_mut::<[u8;0]>() as *mut [u8] ,
284+
}
285+
}
286+
}

0 commit comments

Comments
 (0)