1
1
use core:: marker:: PhantomData ;
2
+ use ptr;
2
3
use ffi:: { self , CPtr } ;
3
4
use types:: { c_uint, c_void} ;
4
5
use Error ;
@@ -214,6 +215,25 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
214
215
pub fn preallocate_size ( ) -> usize {
215
216
Self :: preallocate_size_gen ( )
216
217
}
218
+
219
+ /// Create a context from a raw context.
220
+ ///
221
+ /// # Safety
222
+ /// This is highly unsafe, due to the number of conditions that aren't checked.
223
+ /// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
224
+ /// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
225
+ /// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
226
+ /// when generating the context.
227
+ /// * The user must handle the freeing of the context(using the correct functions) by himself.
228
+ /// * Violating these may lead to Undefined Behavior.
229
+ ///
230
+ pub unsafe fn from_raw_all ( raw_ctx : * mut ffi:: Context ) -> Secp256k1 < AllPreallocated < ' buf > > {
231
+ Secp256k1 {
232
+ ctx : raw_ctx,
233
+ phantom : PhantomData ,
234
+ buf : ptr:: null_mut :: < [ u8 ; 0 ] > ( ) as * mut [ u8 ] ,
235
+ }
236
+ }
217
237
}
218
238
219
239
impl < ' buf > Secp256k1 < SignOnlyPreallocated < ' buf > > {
@@ -227,6 +247,25 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
227
247
pub fn preallocate_signing_size ( ) -> usize {
228
248
Self :: preallocate_size_gen ( )
229
249
}
250
+
251
+ /// Create a context from a raw context.
252
+ ///
253
+ /// # Safety
254
+ /// This is highly unsafe, due to the number of conditions that aren't checked.
255
+ /// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
256
+ /// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
257
+ /// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
258
+ /// when generating the context.
259
+ /// * The user must handle the freeing of the context(using the correct functions) by himself.
260
+ /// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.,
261
+ ///
262
+ pub unsafe fn from_raw_signining_only ( raw_ctx : * mut ffi:: Context ) -> Secp256k1 < SignOnlyPreallocated < ' buf > > {
263
+ Secp256k1 {
264
+ ctx : raw_ctx,
265
+ phantom : PhantomData ,
266
+ buf : ptr:: null_mut :: < [ u8 ; 0 ] > ( ) as * mut [ u8 ] ,
267
+ }
268
+ }
230
269
}
231
270
232
271
impl < ' buf > Secp256k1 < VerifyOnlyPreallocated < ' buf > > {
@@ -240,4 +279,23 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
240
279
pub fn preallocate_verification_size ( ) -> usize {
241
280
Self :: preallocate_size_gen ( )
242
281
}
243
- }
282
+
283
+ /// Create a context from a raw context.
284
+ ///
285
+ /// # Safety
286
+ /// This is highly unsafe, due to the number of conditions that aren't checked.
287
+ /// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
288
+ /// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
289
+ /// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
290
+ /// when generating the context.
291
+ /// * The user must handle the freeing of the context(using the correct functions) by himself.
292
+ /// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.,
293
+ ///
294
+ pub unsafe fn from_raw_verification_only ( raw_ctx : * mut ffi:: Context ) -> Secp256k1 < VerifyOnlyPreallocated < ' buf > > {
295
+ Secp256k1 {
296
+ ctx : raw_ctx,
297
+ phantom : PhantomData ,
298
+ buf : ptr:: null_mut :: < [ u8 ; 0 ] > ( ) as * mut [ u8 ] ,
299
+ }
300
+ }
301
+ }
0 commit comments