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
4 changes: 2 additions & 2 deletions .github/workflows/hybrid-array.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-all-features --optional-deps bytemuck,serde,subtle,zeroize
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-all-features --optional-deps bytemuck,ctutils,serde,subtle,zeroize

careful:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -105,5 +105,5 @@ jobs:
with:
toolchain: ${{ matrix.toolchain }}
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo hack test --feature-powerset --optional-deps arbitrary,bytemuck,serde,subtle,zeroize --group-features arbitrary,bytemuck,serde,subtle,zeroize
- run: cargo hack test --feature-powerset --optional-deps arbitrary,bytemuck,serde,subtle,zeroize --group-features arbitrary,bytemuck,ctutils,serde,subtle,zeroize
- run: cargo test --all-features --release
16 changes: 16 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
arbitrary = { version = "1", optional = true }
bytemuck = { version = "1", optional = true, default-features = false }
ctutils = { version = "0.4", optional = true }
serde = { version = "1", optional = true, default-features = false }
subtle = { version = "2", optional = true, default-features = false, features = ["const-generics"] }
zeroize = { version = "1.8", optional = true, default-features = false }
Expand Down
55 changes: 44 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ use arbitrary::Arbitrary;
#[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 @@ -1087,38 +1084,74 @@ where
{
}

#[cfg(feature = "ctutils")]
impl<T, U> ctutils::CtAssign for Array<T, U>
where
[T]: ctutils::CtAssign,
U: ArraySize,
{
#[inline]
fn ct_assign(&mut self, other: &Self, choice: ctutils::Choice) {
self.as_mut_slice().ct_assign(other.as_slice(), choice);
}
}

#[cfg(feature = "ctutils")]
impl<T, U> ctutils::CtSelect for Array<T, U>
where
U: ArraySize,
U::ArrayType<T>: ctutils::CtSelect,
{
#[inline]
fn ct_select(&self, other: &Self, choice: ctutils::Choice) -> Self {
Self(self.0.ct_select(&other.0, choice))
}
}

#[cfg(feature = "ctutils")]
impl<T, U> ctutils::CtEq for Array<T, U>
where
U: ArraySize,
U::ArrayType<T>: ctutils::CtEq,
{
#[inline]
fn ct_eq(&self, other: &Self) -> ctutils::Choice {
self.0.ct_eq(&other.0)
}
}

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

fn conditional_assign(&mut self, other: &Self, choice: Choice) {
fn conditional_assign(&mut self, other: &Self, choice: subtle::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>
impl<T, U> subtle::ConstantTimeEq for Array<T, U>
where
T: ConstantTimeEq,
T: subtle::ConstantTimeEq,
U: ArraySize,
{
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
fn ct_eq(&self, other: &Self) -> subtle::Choice {
self.iter()
.zip(other.iter())
.fold(Choice::from(1), |acc, (a, b)| acc & a.ct_eq(b))
.fold(subtle::Choice::from(1), |acc, (a, b)| acc & a.ct_eq(b))
}
}

Expand Down
42 changes: 42 additions & 0 deletions tests/ctutils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Tests for `ctutils` integration.

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

use ctutils::{Choice, CtAssign, CtEq, CtSelect};
use hybrid_array::{Array, typenum::U3};

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

c.ct_assign(&b, Choice::FALSE);
assert_eq!(a, c);

c.ct_assign(&b, Choice::TRUE);
assert_eq!(b, c);
}

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

assert!(a.ct_eq(&a).to_bool());
assert!(!a.ct_ne(&a).to_bool());
assert!(!a.ct_eq(&b).to_bool());
assert!(a.ct_ne(&b).to_bool());
}

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

let c = a.ct_select(&b, Choice::FALSE);
assert_eq!(a, c);

let d = a.ct_select(&b, Choice::TRUE);
assert_eq!(b, d);
}