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 ;
@@ -199,6 +200,25 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
199
200
pub fn preallocate_size ( ) -> usize {
200
201
Self :: preallocate_size_gen ( )
201
202
}
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
+ }
202
222
}
203
223
204
224
impl < ' buf > Secp256k1 < SignOnlyPreallocated < ' buf > > {
@@ -212,6 +232,25 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
212
232
pub fn preallocate_signing_size ( ) -> usize {
213
233
Self :: preallocate_size_gen ( )
214
234
}
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
+ }
215
254
}
216
255
217
256
impl < ' buf > Secp256k1 < VerifyOnlyPreallocated < ' buf > > {
@@ -225,4 +264,23 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
225
264
pub fn preallocate_verification_size ( ) -> usize {
226
265
Self :: preallocate_size_gen ( )
227
266
}
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