Skip to content

Commit ef6836d

Browse files
committed
Fully describe safety requirements
Currently we have a wildcard on safety requirements, saying more or less "plus a bunch of other stuff we don't mention". This is not helpful. Attempt to fully describe the safety requirements of creating a context from a raw context (all, signing only, and verification only). Fix: #544
1 parent 29c1363 commit ef6836d

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/context.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,15 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
352352
/// Creates a context from a raw context.
353353
///
354354
/// # Safety
355-
/// This is highly unsafe, due to the number of conditions that aren't checked.
356-
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
357-
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
358-
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
359-
/// when generating the context.
360-
/// * The user must handle the freeing of the context(using the correct functions) by himself.
361-
/// * Violating these may lead to Undefined Behavior.
362355
///
356+
/// This is highly unsafe due to the number of conditions that aren't checked, specifically:
357+
///
358+
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer that was generated by *exactly*
359+
/// the same code/version of the libsecp256k1 used here.
360+
/// * The capabilities (`All`/`SignOnly`/`VerifyOnly`) of the context *must* match the flags
361+
/// passed to libsecp256k1 when generating the context.
362+
/// * The user must handle the freeing of the context (using the correct functions) by himself.
363+
/// * `raw_ctx` must point to writable memory (cannot be `ffi::secp256k1_context_no_precomp`).
363364
pub unsafe fn from_raw_all(
364365
raw_ctx: NonNull<ffi::Context>,
365366
) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>> {
@@ -379,18 +380,18 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
379380
#[inline]
380381
pub fn preallocate_signing_size() -> usize { Self::preallocate_size_gen() }
381382

382-
/// Creates a context from a raw context.
383+
/// Creates a context from a raw context that can only be used for signing.
383384
///
384385
/// # Safety
385386
///
386-
/// This is highly unsafe, due to the number of conditions that aren't checked.
387-
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
388-
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
389-
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
390-
/// when generating the context.
391-
/// * The user must handle the freeing of the context(using the correct functions) by himself.
392-
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
387+
/// This is highly unsafe due to the number of conditions that aren't checked, specifically:
393388
///
389+
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer that was generated by *exactly*
390+
/// the same code/version of the libsecp256k1 used here.
391+
/// * The capabilities (`All`/`SignOnly`/`VerifyOnly`) of the context *must* match the flags
392+
/// passed to libsecp256k1 when generating the context.
393+
/// * The user must handle the freeing of the context (using the correct functions) by himself.
394+
/// * `raw_ctx` must point to writable memory (cannot be `ffi::secp256k1_context_no_precomp`).
394395
pub unsafe fn from_raw_signing_only(
395396
raw_ctx: NonNull<ffi::Context>,
396397
) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>> {
@@ -410,18 +411,18 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
410411
#[inline]
411412
pub fn preallocate_verification_size() -> usize { Self::preallocate_size_gen() }
412413

413-
/// Creates a context from a raw context.
414+
/// Creates a context from a raw context that can only be used for verification.
414415
///
415416
/// # Safety
416417
///
417-
/// This is highly unsafe, due to the number of conditions that aren't checked.
418-
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
419-
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
420-
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
421-
/// when generating the context.
422-
/// * The user must handle the freeing of the context(using the correct functions) by himself.
423-
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
418+
/// This is highly unsafe due to the number of conditions that aren't checked, specifically:
424419
///
420+
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer that was generated by *exactly*
421+
/// the same code/version of the libsecp256k1 used here.
422+
/// * The capabilities (`All`/`SignOnly`/`VerifyOnly`) of the context *must* match the flags
423+
/// passed to libsecp256k1 when generating the context.
424+
/// * The user must handle the freeing of the context (using the correct functions) by himself.
425+
/// * `raw_ctx` must point to writable memory (cannot be `ffi::secp256k1_context_no_precomp`).
425426
pub unsafe fn from_raw_verification_only(
426427
raw_ctx: NonNull<ffi::Context>,
427428
) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>> {

0 commit comments

Comments
 (0)