Skip to content

Commit

Permalink
[WIP] Migrate to hybrid-array; MSRV 1.65
Browse files Browse the repository at this point in the history
Builds on RustCrypto/traits#1319.

Migrates the following crates away from using `generic-array` to using
`hybrid-array` instead:

- `block-buffer`
- `block-padding`
- `dbl`
- `inout`
  • Loading branch information
tarcieri committed Oct 8, 2023
1 parent eb03093 commit 2ca9557
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 165 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/block-buffer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -37,10 +37,9 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
- run: rm ../Cargo.toml
- run: cargo build --target ${{ matrix.target }}

# TODO(tarcieri): re-enable after next `crypto-common` release
minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
with:
Expand All @@ -51,15 +50,13 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
- run: rm ../Cargo.toml
- run: cargo test
- run: cargo test --all-features
4 changes: 2 additions & 2 deletions .github/workflows/block-padding.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -49,7 +49,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/dbl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
rust:
- 1.41.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -37,8 +37,6 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
- run: rm ../Cargo.toml
- run: cargo build --target ${{ matrix.target }}

minimal-versions:
Expand All @@ -51,14 +49,12 @@ jobs:
strategy:
matrix:
rust:
- 1.41.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
- run: rm ../Cargo.toml
- run: cargo test
4 changes: 2 additions & 2 deletions .github/workflows/inout.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -50,7 +50,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
Expand Down
35 changes: 8 additions & 27 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io]
crypto-common = { git = "https://github.com/RustCrypto/traits", branch = "crypto-common/hybrid-array" }
hybrid-array = { path = "hybrid-array" }
1 change: 0 additions & 1 deletion block-buffer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ readme = "README.md"

[dependencies]
crypto-common = "0.2.0-pre"
generic-array = "0.14"

[dev-dependencies]
hex-literal = "0.3.3"
2 changes: 1 addition & 1 deletion block-buffer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Unless you explicitly state otherwise, any contribution intentionally submitted
[docs-image]: https://docs.rs/block-buffer/badge.svg
[docs-link]: https://docs.rs/block-buffer/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.41+-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.65+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260052-utils
[build-image]: https://github.com/RustCrypto/utils/workflows/block-buffer/badge.svg?branch=master&event=push
Expand Down
23 changes: 10 additions & 13 deletions block-buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
)]
#![warn(missing_docs, rust_2018_idioms)]

pub use generic_array;
pub use crypto_common::{array, Block};

use core::{fmt, ops::Add, slice};
pub use crypto_common::Block;
use crypto_common::{BlockSizeUser, BlockSizes};
use generic_array::{
use array::{
typenum::{Add1, B1},
ArrayLength, GenericArray,
Array, ArraySize,
};
use core::{fmt, ops::Add, slice};
use crypto_common::{BlockSizeUser, BlockSizes};

mod read;
mod sealed;

pub use read::ReadBuffer;

/// Block with additional one byte
type BlockP1<BlockSize> = GenericArray<u8, Add1<BlockSize>>;
type BlockP1<BlockSize> = Array<u8, Add1<BlockSize>>;

/// Trait for buffer kinds.
pub trait BufferKind: sealed::Sealed {}
Expand Down Expand Up @@ -304,7 +303,7 @@ impl<BS: BlockSizes> BlockBuffer<BS, Lazy> {
pub fn serialize(&self) -> BlockP1<BS>
where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
Add1<BS>: ArraySize,
{
let mut res = BlockP1::<BS>::default();
res[0] = self.pos;
Expand All @@ -318,7 +317,7 @@ impl<BS: BlockSizes> BlockBuffer<BS, Lazy> {
pub fn deserialize(buffer: &BlockP1<BS>) -> Result<Self, Error>
where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
Add1<BS>: ArraySize,
{
let pos = buffer[0];
if !<Lazy as sealed::Sealed>::invariant(pos as usize, BS::USIZE) {
Expand All @@ -327,9 +326,7 @@ impl<BS: BlockSizes> BlockBuffer<BS, Lazy> {
if buffer[1..][pos as usize..].iter().any(|&b| b != 0) {
return Err(Error);
}
Ok(Self {
buffer: GenericArray::clone_from_slice(&buffer[1..]),
pos,
})
let buffer = Array::clone_from_slice(&buffer[1..]);
Ok(Self { buffer, pos })
}
}
12 changes: 6 additions & 6 deletions block-buffer/src/sealed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::array::{Array, ArraySize};
use core::slice;
use generic_array::{ArrayLength, GenericArray};

/// Sealed trait for buffer kinds.
pub trait Sealed {
Expand All @@ -14,7 +14,7 @@ pub trait Sealed {
fn invariant(pos: usize, block_size: usize) -> bool;

/// Split input data into slice of blocks and tail.
fn split_blocks<N: ArrayLength<u8>>(data: &[u8]) -> (&[GenericArray<u8, N>], &[u8]);
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]);
}

impl Sealed for super::Eager {
Expand All @@ -35,14 +35,14 @@ impl Sealed for super::Eager {
}

#[inline(always)]
fn split_blocks<N: ArrayLength<u8>>(data: &[u8]) -> (&[GenericArray<u8, N>], &[u8]) {
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]) {
let nb = data.len() / N::USIZE;
let blocks_len = nb * N::USIZE;
let tail_len = data.len() - blocks_len;
// SAFETY: we guarantee that created slices do not point
// outside of `data`
unsafe {
let blocks_ptr = data.as_ptr() as *const GenericArray<u8, N>;
let blocks_ptr = data.as_ptr() as *const Array<u8, N>;
let tail_ptr = data.as_ptr().add(blocks_len);
(
slice::from_raw_parts(blocks_ptr, nb),
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Sealed for super::Lazy {
}

#[inline(always)]
fn split_blocks<N: ArrayLength<u8>>(data: &[u8]) -> (&[GenericArray<u8, N>], &[u8]) {
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]) {
if data.is_empty() {
return (&[], &[]);
}
Expand All @@ -84,7 +84,7 @@ impl Sealed for super::Lazy {
// SAFETY: we guarantee that created slices do not point
// outside of `data`
unsafe {
let blocks_ptr = data.as_ptr() as *const GenericArray<u8, N>;
let blocks_ptr = data.as_ptr() as *const Array<u8, N>;
let tail_ptr = data.as_ptr().add(blocks_len);
(
slice::from_raw_parts(blocks_ptr, nb),
Expand Down
Loading

0 comments on commit 2ca9557

Please sign in to comment.