Skip to content
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 BlackBox #123

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Neg, Not};
use core::option::Option;

#[cfg(feature = "core_hint_black_box")]
use core::hint::black_box;

/// The `Choice` struct represents a choice for use in conditional assignment.
///
/// It is a wrapper around a `u8`, which should have the value either `1` (true)
Expand Down Expand Up @@ -217,25 +220,23 @@
/// code may break in a non-destructive way in the future, “constant-time” code
/// is a continually moving target, and this is better than doing nothing.
#[inline(never)]
fn black_box(input: u8) -> u8 {
debug_assert!((input == 0u8) | (input == 1u8));

fn black_box<T: Copy>(input: T) -> T {
unsafe {
// Optimization barrier
//
// Unsafe is ok, because:
// - &input is not NULL;
// - size of input is not zero;
// - u8 is neither Sync, nor Send;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also #91

// - u8 is Copy, so input is always live;
// - u8 type is always properly aligned.
core::ptr::read_volatile(&input as *const u8)
// SAFETY:
// - &input is not NULL because we own input;
// - input is Copy and always live;
// - input is always properly aligned.
core::ptr::read_volatile(&input)
}
}

impl From<u8> for Choice {
#[inline]
fn from(input: u8) -> Choice {
debug_assert!((input == 0u8) | (input == 1u8));

// Our goal is to prevent the compiler from inferring that the value held inside the
// resulting `Choice` struct is really an `i1` instead of an `i8`.
Choice(black_box(input))
Expand Down Expand Up @@ -571,7 +572,7 @@
}

#[cfg(feature = "const-generics")]
impl<T, const N: usize> ConditionallySelectable for [T; N]

Check failure on line 575 in src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo test (1.41.0)

const generics are unstable

Check failure on line 575 in src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo test (1.41.0)

const generics are unstable
where
T: ConditionallySelectable,
{
Expand All @@ -583,7 +584,7 @@
}

fn conditional_assign(&mut self, other: &Self, choice: Choice) {
for (a_i, b_i) in self.iter_mut().zip(other) {

Check failure on line 587 in src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo test (1.41.0)

core::iter::IntoIterator` is not satisfied

Check failure on line 587 in src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo test (1.41.0)

core::iter::IntoIterator` is not satisfied
a_i.conditional_assign(b_i, choice)
}
}
Expand Down Expand Up @@ -986,3 +987,21 @@
(a as u8).ct_lt(&(b as u8))
}
}

/// Wrapper type which implements an optimization barrier for all accesses.
#[derive(Clone, Copy, Debug)]
pub struct BlackBox<T: Copy>(T);

impl<T: Copy> BlackBox<T> {
/// Constructs a new instance of `BlackBox` which will wrap the specified value.
///
/// All access to the inner value will be mediated by a `black_box` optimization barrier.
pub fn new(value: T) -> Self {
Self(value)
}

/// Read the inner value, applying an optimization barrier on access.
pub fn get(self) -> T {
black_box(self.0)
}
}
7 changes: 7 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,10 @@ fn less_than_ordering() {
assert_eq!(cmp::Ordering::Greater.ct_lt(&cmp::Ordering::Less).unwrap_u8(), 0);
assert_eq!(cmp::Ordering::Less.ct_lt(&cmp::Ordering::Greater).unwrap_u8(), 1);
}

#[test]
fn black_box_round_trip() {
let n = 42u64;
let black_box = BlackBox::new(n);
assert_eq!(n, black_box.get());
}
Loading