-
Notifications
You must be signed in to change notification settings - Fork 165
[WIP] ctutils: automatically use optimized 1-byte CtEq
#1369
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //! Utilities for leveraging the optimized implementations of `Cmov`/`CmovEq` for types whose size | ||
| //! is 1-byte. | ||
|
|
||
| use crate::{Choice, traits::no_uninit::NoUninit}; | ||
| use cmov::CmovEq; | ||
| use core::slice; | ||
|
|
||
| /// Perform constant-time equality comparison on slices of 1-byte sized types using the optimized | ||
| /// implementation of `CmovEq` for byte slices. | ||
| pub(crate) fn ct_eq<T: NoUninit>(a: &[T], b: &[T]) -> Choice { | ||
| assert_eq!( | ||
| size_of::<T>(), | ||
| 1, | ||
| "this function is intended for 1-byte sized types" | ||
| ); | ||
|
|
||
| // SAFETY: | ||
| // - We asserted above that `size_of::<T>() == size_of::<u8>() == 1`. | ||
| // - The `NoUninit` bound ensures the type does not contain uninitialized memory. | ||
| // - We don't need to worry about alignment because all types are 1-byte. | ||
| // - 1-byte is too small to contain a pointer/reference. | ||
| // - We source the slice length directly from the other valid slice. | ||
| #[allow(unsafe_code)] | ||
| let (a, b) = unsafe { | ||
| ( | ||
| slice::from_raw_parts(a.as_ptr() as *const u8, a.len()), | ||
| slice::from_raw_parts(b.as_ptr() as *const u8, b.len()), | ||
| ) | ||
| }; | ||
|
|
||
| let mut ret = Choice::FALSE; | ||
| a.cmoveq(b, 1, &mut ret.0); | ||
| ret | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use core::num::{NonZeroI8, NonZeroU8}; | ||
|
|
||
| macro_rules! ct_eq_test { | ||
| ($name:ident, $a:expr, $b:expr) => { | ||
| #[test] | ||
| fn $name() { | ||
| let x = $a; | ||
| let y = $b; | ||
|
|
||
| let a = [x, x, x]; | ||
| let b = [x, x, y]; | ||
| let c = [x, y, y]; | ||
| let d = [y, y, y]; | ||
|
|
||
| assert!(super::ct_eq(&a, &a).to_bool()); | ||
| assert!(super::ct_eq(&b, &b).to_bool()); | ||
| assert!(super::ct_eq(&c, &c).to_bool()); | ||
| assert!(super::ct_eq(&d, &d).to_bool()); | ||
|
|
||
| for rhs in &[b, c, d] { | ||
| assert!(!super::ct_eq(&a, rhs).to_bool()); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| ct_eq_test!(i8_ct_eq, 1i8, 2i8); | ||
| ct_eq_test!(u8_ct_eq, 1u8, 2u8); | ||
| ct_eq_test!( | ||
| non_zero_i8_ct_eq, | ||
| NonZeroI8::new(1i8).unwrap(), | ||
| NonZeroI8::new(2i8).unwrap() | ||
| ); | ||
| ct_eq_test!( | ||
| non_zero_u8_ct_eq, | ||
| NonZeroU8::new(1u8).unwrap(), | ||
| NonZeroU8::new(2u8).unwrap() | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,7 @@ | |
| extern crate alloc; | ||
|
|
||
| mod bytes; | ||
| mod byteutils; | ||
| mod choice; | ||
| mod ct_option; | ||
| mod traits; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #![allow( | ||
| clippy::missing_safety_doc, | ||
| clippy::undocumented_unsafe_blocks, | ||
| unsafe_code | ||
| )] | ||
|
|
||
| use crate::{Choice, CtOption}; | ||
| use core::{ | ||
| cmp, | ||
| num::{ | ||
| NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroU8, NonZeroU16, | ||
| NonZeroU32, NonZeroU64, NonZeroU128, | ||
| }, | ||
| }; | ||
|
|
||
| #[cfg(feature = "alloc")] | ||
| use alloc::{boxed::Box, vec::Vec}; | ||
|
|
||
| /// Marker trait for types which do not contain uninitialized memory. | ||
| pub unsafe trait NoUninit {} | ||
|
|
||
| // Impl `NoUninit` for the given type | ||
| macro_rules! impl_no_uninit { | ||
| ( $($ty:ty),+ ) => { | ||
| $( | ||
| unsafe impl NoUninit for $ty {} | ||
| )+ | ||
| }; | ||
| } | ||
|
|
||
| impl_no_uninit!( | ||
| i8, | ||
| i16, | ||
| i32, | ||
| i64, | ||
| i128, | ||
| isize, | ||
| u8, | ||
| u16, | ||
| u32, | ||
| u64, | ||
| u128, | ||
| usize, | ||
| NonZeroI8, | ||
| NonZeroI16, | ||
| NonZeroI32, | ||
| NonZeroI64, | ||
| NonZeroI128, | ||
| NonZeroU8, | ||
| NonZeroU16, | ||
| NonZeroU32, | ||
| NonZeroU64, | ||
| NonZeroU128 | ||
| ); | ||
|
|
||
| unsafe impl NoUninit for Choice {} | ||
| unsafe impl NoUninit for cmp::Ordering {} | ||
| unsafe impl<T: NoUninit> NoUninit for CtOption<T> {} | ||
| unsafe impl<T: NoUninit> NoUninit for [T] {} | ||
| unsafe impl<T: NoUninit, const N: usize> NoUninit for [T; N] {} | ||
|
|
||
| #[cfg(feature = "alloc")] | ||
| unsafe impl<T: NoUninit> NoUninit for Box<T> {} | ||
| #[cfg(feature = "alloc")] | ||
| unsafe impl<T: NoUninit> NoUninit for Vec<T> {} | ||
|
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. This impl is not sound, vec does not have layout guarantees and there could be padding under |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For Box it should be fine since I think it's guaranteed that it's just a pointer, but you don't even need the
T: NoUninitbound here :). the implicitT: Sizedmatters though since fat pointer layout isn't guaranteed