Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/hybrid-array.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- run: cargo build --no-default-features --target ${{ matrix.target }} --features bytemuck
- run: cargo build --no-default-features --target ${{ matrix.target }} --features extra-sizes
- run: cargo build --no-default-features --target ${{ matrix.target }} --features serde
- run: cargo build --no-default-features --target ${{ matrix.target }} --features subtle
- run: cargo build --no-default-features --target ${{ matrix.target }} --features zeroize
- run: cargo build --no-default-features --target ${{ matrix.target }} --all-features

Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typenum = { version = "1.17", features = ["const-generics"] }
# optional dependencies
bytemuck = { version = "1", optional = true, default-features = false }
serde = { version = "1", optional = true, default-features = false }
subtle = { version = "2", optional = true, default-features = false }
zeroize = { version = "1.8", optional = true, default-features = false }

[dev-dependencies]
Expand Down
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ use typenum::{Diff, Sum};
#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};

#[cfg(feature = "subtle")]
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};

#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};

Expand Down Expand Up @@ -854,6 +857,41 @@ where
{
}

#[cfg(feature = "subtle")]
impl<T, U> ConditionallySelectable for Array<T, U>
where
Self: Copy,
T: ConditionallySelectable,
U: ArraySize,
{
#[inline]
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
let mut output = *a;
output.conditional_assign(b, choice);
output
}

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

#[cfg(feature = "subtle")]
impl<T, U> ConstantTimeEq for Array<T, U>
where
T: ConstantTimeEq,
U: ArraySize,
{
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
self.iter()
.zip(other.iter())
.fold(Choice::from(1), |acc, (a, b)| acc & a.ct_eq(b))
}
}

#[cfg(feature = "zeroize")]
impl<T, U> Zeroize for Array<T, U>
where
Expand Down
29 changes: 29 additions & 0 deletions tests/subtle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Tests for `subtle` crate integration.

#![cfg(feature = "subtle")]

use hybrid_array::{Array, typenum::U3};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};

#[test]
fn constant_time_eq() {
let a: Array<u8, U3> = Array([0, 0, 0]);
let b: Array<u8, U3> = Array([1, 2, 3]);

assert!(bool::from(a.ct_eq(&a)));
assert!(!bool::from(a.ct_ne(&a)));
assert!(!bool::from(a.ct_eq(&b)));
assert!(bool::from(a.ct_ne(&b)));
}

#[test]
fn conditional_select() {
let a: Array<u8, U3> = Array([0, 0, 0]);
let b: Array<u8, U3> = Array([1, 2, 3]);

let c = Array::conditional_select(&a, &b, Choice::from(0));
assert_eq!(a, c);

let d = Array::conditional_select(&a, &b, Choice::from(1));
assert_eq!(b, d);
}