Merge rust-bitcoin/rust-secp256k1#541: Remove size field

129ba3cd58 Remove size field (Tobin C. Harding)

Pull request description:

  The `Secp256k1` `size` field is a cached value that we get using `ffi::secp256k1_context_preallocated_size` or
  `ffi::secp256k1_context_preallocated_clone_size`, both of which just return the result of `sizeof(rustsecp256k1_v0_6_1_context)`. Instead of caching this value we can just call
  `ffi::secp256k1_context_preallocated_clone_size` in the `Drop` implementation.

  Fix #537

ACKs for top commit:
  apoelstra:
    ACK 129ba3cd58

Tree-SHA512: 3fce7863065e4b485fd2d1fdbbfe7002fa6188f1a703d89fdda570a1a32471d298e2e33fb8c5951a56a79facb5d2b427d58e473b5cb1d68eb02ffed728392b97
This commit is contained in:
Andrew Poelstra 2022-11-29 15:36:31 +00:00
commit 3760ef6b0c
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 8 additions and 25 deletions

View File

@ -212,7 +212,6 @@ mod alloc_only {
ffi::secp256k1_context_preallocated_create(ptr as *mut c_void, C::FLAGS) ffi::secp256k1_context_preallocated_create(ptr as *mut c_void, C::FLAGS)
}, },
phantom: PhantomData, phantom: PhantomData,
size,
}; };
#[cfg(all( #[cfg(all(
@ -273,7 +272,6 @@ mod alloc_only {
ffi::secp256k1_context_preallocated_clone(self.ctx, ptr as *mut c_void) ffi::secp256k1_context_preallocated_clone(self.ctx, ptr as *mut c_void)
}, },
phantom: PhantomData, phantom: PhantomData,
size,
} }
} }
} }
@ -329,7 +327,6 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {
) )
}, },
phantom: PhantomData, phantom: PhantomData,
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
}) })
} }
} }
@ -358,11 +355,7 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
pub unsafe fn from_raw_all( pub unsafe fn from_raw_all(
raw_ctx: *mut ffi::Context, raw_ctx: *mut ffi::Context,
) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>> { ) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>> {
ManuallyDrop::new(Secp256k1 { ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData })
ctx: raw_ctx,
phantom: PhantomData,
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
})
} }
} }
@ -393,11 +386,7 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
pub unsafe fn from_raw_signing_only( pub unsafe fn from_raw_signing_only(
raw_ctx: *mut ffi::Context, raw_ctx: *mut ffi::Context,
) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>> { ) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>> {
ManuallyDrop::new(Secp256k1 { ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData })
ctx: raw_ctx,
phantom: PhantomData,
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
})
} }
} }
@ -428,10 +417,6 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
pub unsafe fn from_raw_verification_only( pub unsafe fn from_raw_verification_only(
raw_ctx: *mut ffi::Context, raw_ctx: *mut ffi::Context,
) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>> { ) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>> {
ManuallyDrop::new(Secp256k1 { ManuallyDrop::new(Secp256k1 { ctx: raw_ctx, phantom: PhantomData })
ctx: raw_ctx,
phantom: PhantomData,
size: 0, // We don't care about the size because it's the caller responsibility to deallocate.
})
} }
} }

View File

@ -371,7 +371,6 @@ impl std::error::Error for Error {
pub struct Secp256k1<C: Context> { pub struct Secp256k1<C: Context> {
ctx: *mut ffi::Context, ctx: *mut ffi::Context,
phantom: PhantomData<C>, phantom: PhantomData<C>,
size: usize,
} }
// The underlying secp context does not contain any references to memory it does not own. // The underlying secp context does not contain any references to memory it does not own.
@ -388,8 +387,9 @@ impl<C: Context> Eq for Secp256k1<C> {}
impl<C: Context> Drop for Secp256k1<C> { impl<C: Context> Drop for Secp256k1<C> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
let size = ffi::secp256k1_context_preallocated_clone_size(self.ctx);
ffi::secp256k1_context_preallocated_destroy(self.ctx); ffi::secp256k1_context_preallocated_destroy(self.ctx);
C::deallocate(self.ctx as _, self.size); C::deallocate(self.ctx as _, size);
} }
} }
} }
@ -556,13 +556,11 @@ mod tests {
let ctx_sign = unsafe { ffi::secp256k1_context_create(SignOnlyPreallocated::FLAGS) }; let ctx_sign = unsafe { ffi::secp256k1_context_create(SignOnlyPreallocated::FLAGS) };
let ctx_vrfy = unsafe { ffi::secp256k1_context_create(VerifyOnlyPreallocated::FLAGS) }; let ctx_vrfy = unsafe { ffi::secp256k1_context_create(VerifyOnlyPreallocated::FLAGS) };
let size = 0; let full: Secp256k1<AllPreallocated> = Secp256k1 { ctx: ctx_full, phantom: PhantomData };
let full: Secp256k1<AllPreallocated> =
Secp256k1 { ctx: ctx_full, phantom: PhantomData, size };
let sign: Secp256k1<SignOnlyPreallocated> = let sign: Secp256k1<SignOnlyPreallocated> =
Secp256k1 { ctx: ctx_sign, phantom: PhantomData, size }; Secp256k1 { ctx: ctx_sign, phantom: PhantomData };
let vrfy: Secp256k1<VerifyOnlyPreallocated> = let vrfy: Secp256k1<VerifyOnlyPreallocated> =
Secp256k1 { ctx: ctx_vrfy, phantom: PhantomData, size }; Secp256k1 { ctx: ctx_vrfy, phantom: PhantomData };
let (sk, pk) = full.generate_keypair(&mut rand::thread_rng()); let (sk, pk) = full.generate_keypair(&mut rand::thread_rng());
let msg = Message::from_slice(&[2u8; 32]).unwrap(); let msg = Message::from_slice(&[2u8; 32]).unwrap();