Skip to content

Commit 767246a

Browse files
committed
Make preallocated use AlignedType
1 parent fd206ab commit 767246a

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
233233

234234
impl<'buf, C: Context + 'buf> Secp256k1<C> {
235235
/// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all)
236-
pub fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> {
236+
pub fn preallocated_gen_new(buf: &'buf mut [AlignedType]) -> Result<Secp256k1<C>, Error> {
237237
#[cfg(target_arch = "wasm32")]
238238
ffi::types::sanity_checks_for_wasm();
239239

@@ -254,7 +254,7 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {
254254

255255
impl<'buf> Secp256k1<AllPreallocated<'buf>> {
256256
/// Creates a new Secp256k1 context with all capabilities
257-
pub fn preallocated_new(buf: &'buf mut [u8]) -> Result<Secp256k1<AllPreallocated<'buf>>, Error> {
257+
pub fn preallocated_new(buf: &'buf mut [AlignedType]) -> Result<Secp256k1<AllPreallocated<'buf>>, Error> {
258258
Secp256k1::preallocated_gen_new(buf)
259259
}
260260
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context
@@ -284,7 +284,7 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
284284

285285
impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
286286
/// Creates a new Secp256k1 context that can only be used for signing
287-
pub fn preallocated_signing_only(buf: &'buf mut [u8]) -> Result<Secp256k1<SignOnlyPreallocated<'buf>>, Error> {
287+
pub fn preallocated_signing_only(buf: &'buf mut [AlignedType]) -> Result<Secp256k1<SignOnlyPreallocated<'buf>>, Error> {
288288
Secp256k1::preallocated_gen_new(buf)
289289
}
290290

@@ -316,7 +316,7 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
316316

317317
impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
318318
/// Creates a new Secp256k1 context that can only be used for verification
319-
pub fn preallocated_verification_only(buf: &'buf mut [u8]) -> Result<Secp256k1<VerifyOnlyPreallocated<'buf>>, Error> {
319+
pub fn preallocated_verification_only(buf: &'buf mut [AlignedType]) -> Result<Secp256k1<VerifyOnlyPreallocated<'buf>>, Error> {
320320
Secp256k1::preallocated_gen_new(buf)
321321
}
322322

src/lib.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ pub use key::PublicKey;
151151
pub use context::*;
152152
use core::marker::PhantomData;
153153
use core::ops::Deref;
154-
use ffi::CPtr;
154+
use core::mem;
155+
use ffi::{CPtr, types::AlignedType};
155156

156157
#[cfg(feature = "global-context")]
157158
pub use context::global::SECP256K1;
@@ -630,7 +631,10 @@ impl<C: Context> Secp256k1<C> {
630631

631632
/// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all)
632633
pub fn preallocate_size_gen() -> usize {
633-
unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) }
634+
let word_size = mem::size_of::<AlignedType>();
635+
let bytes = unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) };
636+
637+
(bytes + word_size - 1) / word_size
634638
}
635639

636640
/// (Re)randomizes the Secp256k1 context for cheap sidechannel resistance;
@@ -763,7 +767,7 @@ mod tests {
763767
use super::constants;
764768
use super::{Secp256k1, Signature, Message};
765769
use super::Error::{InvalidMessage, IncorrectSignature, InvalidSignature};
766-
use ffi;
770+
use ffi::{self, types::AlignedType};
767771
use context::*;
768772

769773
macro_rules! hex {
@@ -840,10 +844,10 @@ mod tests {
840844

841845
#[test]
842846
fn test_preallocation() {
843-
let mut buf_ful = vec![0u8; Secp256k1::preallocate_size()];
844-
let mut buf_sign = vec![0u8; Secp256k1::preallocate_signing_size()];
845-
let mut buf_vfy = vec![0u8; Secp256k1::preallocate_verification_size()];
846-
//
847+
let mut buf_ful = vec![AlignedType::zeroed(); Secp256k1::preallocate_size()];
848+
let mut buf_sign = vec![AlignedType::zeroed(); Secp256k1::preallocate_signing_size()];
849+
let mut buf_vfy = vec![AlignedType::zeroed(); Secp256k1::preallocate_verification_size()];
850+
847851
let full = Secp256k1::preallocated_new(&mut buf_ful).unwrap();
848852
let sign = Secp256k1::preallocated_signing_only(&mut buf_sign).unwrap();
849853
let vrfy = Secp256k1::preallocated_verification_only(&mut buf_vfy).unwrap();

0 commit comments

Comments
 (0)