Skip to content

Commit

Permalink
Make preallocated use AlignedType
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Aug 28, 2020
1 parent b09494f commit 1b73d4e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/context.rs
Expand Up @@ -232,7 +232,7 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {

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

Expand All @@ -253,7 +253,7 @@ impl<'buf, C: Context + 'buf> Secp256k1<C> {

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

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

Expand Down Expand Up @@ -315,7 +315,7 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {

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

Expand Down
18 changes: 11 additions & 7 deletions src/lib.rs
Expand Up @@ -177,7 +177,8 @@ pub use key::PublicKey;
pub use context::*;
use core::marker::PhantomData;
use core::ops::Deref;
use ffi::CPtr;
use core::mem;
use ffi::{CPtr, types::AlignedType};

#[cfg(feature = "global-context")]
pub use context::global::SECP256K1;
Expand Down Expand Up @@ -656,7 +657,10 @@ impl<C: Context> Secp256k1<C> {

/// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all)
pub fn preallocate_size_gen() -> usize {
unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) }
let word_size = mem::size_of::<AlignedType>();
let bytes = unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) };

(bytes + word_size - 1) / word_size
}

/// (Re)randomizes the Secp256k1 context for cheap sidechannel resistance;
Expand Down Expand Up @@ -795,7 +799,7 @@ mod tests {
use super::constants;
use super::{Secp256k1, Signature, Message};
use super::Error::{InvalidMessage, IncorrectSignature, InvalidSignature};
use ffi;
use ffi::{self, types::AlignedType};
use context::*;

macro_rules! hex {
Expand Down Expand Up @@ -872,10 +876,10 @@ mod tests {

#[test]
fn test_preallocation() {
let mut buf_ful = vec![0u8; Secp256k1::preallocate_size()];
let mut buf_sign = vec![0u8; Secp256k1::preallocate_signing_size()];
let mut buf_vfy = vec![0u8; Secp256k1::preallocate_verification_size()];
//
let mut buf_ful = vec![AlignedType::zeroed(); Secp256k1::preallocate_size()];
let mut buf_sign = vec![AlignedType::zeroed(); Secp256k1::preallocate_signing_size()];
let mut buf_vfy = vec![AlignedType::zeroed(); Secp256k1::preallocate_verification_size()];

let full = Secp256k1::preallocated_new(&mut buf_ful).unwrap();
let sign = Secp256k1::preallocated_signing_only(&mut buf_sign).unwrap();
let vrfy = Secp256k1::preallocated_verification_only(&mut buf_vfy).unwrap();
Expand Down

0 comments on commit 1b73d4e

Please sign in to comment.