Skip to content
Open
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
3 changes: 1 addition & 2 deletions src/bb/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ pub fn bytes_are_equal(a: &[u8], b: &[u8]) -> BoolMask {
#[allow(clippy::into_iter_on_ref)]
let rem = a_rem
.into_iter()
.copied()
.map(Word::from)
.map(Word::from_u8)
.zip(b_rem.into_iter().copied().map(Word::from))
.fold(0, |acc, (a, b)| acc | (a ^ b));
acc |= rem;
Expand Down
Empty file added src/bb/ops.rs
Empty file.
32 changes: 32 additions & 0 deletions src/bb/word/aarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::{super::BoolMask, Word, WordOps};
use crate::polyfill;
use core::{arch::asm, mem};

impl WordOps for Word {
fn from_u8(a: &u8) -> Self {
let mut r: Self;
unsafe {
asm!(
"ldrb {r:w}, [{a_ptr}]", // zero-extended to 32-bits, then 64-bits.
a_ptr = in(reg) polyfill::ptr::from_ref(a),
r = lateout(reg) r,
options(nostack, readonly)
);
}
r
}

fn is_zero(self) -> BoolMask {
let r: u64;
unsafe {
asm!(
"subs {r}, {a}, #1",
"sbc {r}, {r}, {r}", // r - r - carry = 0 - carry = -carry.
a = in(reg) self,
r = lateout(reg) r,
options(nomem, nostack, pure)
);
}
unsafe { mem::transmute::<u64, BoolMask>(r) }
}
}
5 changes: 5 additions & 0 deletions src/bb/word/fallback.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use super::{super::BoolMask, Word, WordOps};

impl WordOps for Word {
#[inline(always)]
fn from_u8(a: &u8) -> Self {
Self::from(*a)
}

#[inline]
fn is_zero(self) -> BoolMask {
use crate::limb::{Limb, LimbMask}; // XXX: Backwards dependency.
Expand Down
10 changes: 9 additions & 1 deletion src/bb/word/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use super::{BoolMask, LeakyWord};
use cfg_if::cfg_if;

/// A native word that may hold a secret.
///
Expand All @@ -28,6 +29,7 @@ use super::{BoolMask, LeakyWord};
pub(crate) type Word = LeakyWord;

pub(crate) trait WordOps: Copy {
fn from_u8(a: &u8) -> Self;
fn is_zero(self) -> BoolMask;
}

Expand All @@ -47,4 +49,10 @@ impl From<LeakyWord> for Word {
}
*/

mod fallback;
cfg_if! {
if #[cfg(target_arch = "aarch64")] {
mod aarch64;
} else {
mod fallback;
}
}
Loading