-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AlignType and use it for buffer allocations #141
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,7 +155,9 @@ pub use secp256k1_sys as ffi; | |
#[cfg(any(test, feature = "rand"))] use rand::Rng; | ||
#[cfg(any(test, feature = "std"))] extern crate core; | ||
|
||
use core::{fmt, ptr, str}; | ||
use core::{fmt, mem, ptr, str}; | ||
|
||
pub use ffi::AlignType; | ||
|
||
#[macro_use] | ||
mod macros; | ||
|
@@ -530,7 +532,7 @@ impl std::error::Error for Error { | |
pub struct Secp256k1<C: Context> { | ||
ctx: *mut ffi::Context, | ||
phantom: PhantomData<C>, | ||
buf: *mut [u8], | ||
buf: *mut [AlignType], | ||
} | ||
|
||
// The underlying secp context does not contain any references to memory it does not own | ||
|
@@ -601,8 +603,19 @@ impl<C: Context> Secp256k1<C> { | |
} | ||
|
||
/// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add comment that this returns the size in AlignType units (also for the specific methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good Point! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think maybe they should return bytes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
||
/// | ||
/// Notice that the memory returned is in [AlignType](type.AlignType.html) | ||
/// | ||
pub fn preallocate_size_gen() -> usize { | ||
unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) } | ||
assert!(mem::align_of::<AlignType>() >= mem::align_of::<u8>()); | ||
assert!(mem::align_of::<AlignType>() >= mem::align_of::<usize>()); | ||
assert!(mem::size_of::<AlignType>() >= mem::size_of::<usize>()); | ||
assert!(mem::align_of::<AlignType>() >= mem::align_of::<&AlignType>()); | ||
|
||
let bytes_size = unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) }; | ||
let word_size = mem::size_of::<AlignType>(); | ||
let n_words = (bytes_size + word_size - 1) / word_size; | ||
n_words | ||
} | ||
|
||
/// (Re)randomizes the Secp256k1 context for cheap sidechannel resistance; | ||
|
@@ -718,7 +731,7 @@ mod tests { | |
use super::constants; | ||
use super::{Secp256k1, Signature, Message}; | ||
use super::Error::{InvalidMessage, IncorrectSignature, InvalidSignature}; | ||
use ffi; | ||
use ffi::{self, AlignType}; | ||
use context::*; | ||
|
||
macro_rules! hex { | ||
|
@@ -736,7 +749,7 @@ mod tests { | |
let ctx_sign = unsafe { ffi::secp256k1_context_create(SignOnlyPreallocated::FLAGS) }; | ||
let ctx_vrfy = unsafe { ffi::secp256k1_context_create(VerifyOnlyPreallocated::FLAGS) }; | ||
|
||
let buf: *mut [u8] = &mut [0u8;0] as _; | ||
let buf: *mut [AlignType] = &mut [0 as AlignType;0] as _; | ||
let full: Secp256k1<AllPreallocated> = Secp256k1{ctx: ctx_full, phantom: PhantomData, buf}; | ||
let sign: Secp256k1<SignOnlyPreallocated> = Secp256k1{ctx: ctx_sign, phantom: PhantomData, buf}; | ||
let vrfy: Secp256k1<VerifyOnlyPreallocated> = Secp256k1{ctx: ctx_vrfy, phantom: PhantomData, buf}; | ||
|
@@ -795,10 +808,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![0; Secp256k1::preallocate_size()]; | ||
let mut buf_sign = vec![0; Secp256k1::preallocate_signing_size()]; | ||
let mut buf_vfy = vec![0; 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(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0 as AlignType
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rust automatically assign the right type because it's then passed to
Secp256k1::preallocated_new
which accepts[AlignType]