Skip to content

Migrate to digest::newtype! #678

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

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/sha3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo hack build --target ${{ matrix.target }} --each-feature --exclude-features default,std,asm
- run: cargo hack build --target ${{ matrix.target }} --each-feature --exclude-features default,asm

test:
needs: set-msrv
Expand Down
32 changes: 16 additions & 16 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ opt-level = 2

[patch.crates-io]
# https://github.com/RustCrypto/traits/pull/1787
digest = { git = "https://github.com/RustCrypto/traits" }
# https://github.com/RustCrypto/traits/pull/1799
digest = { git = "https://github.com/RustCrypto/traits", branch = "digest/newtype" }
4 changes: 2 additions & 2 deletions ascon-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ hex-literal = "1"
base16ct = { version = "0.2", features = ["alloc"] }

[features]
default = ["std"]
std = ["digest/std"]
default = ["alloc"]
alloc = ["digest/alloc"]
zeroize = ["ascon/zeroize", "digest/zeroize"]

[package.metadata.docs.rs]
Expand Down
86 changes: 55 additions & 31 deletions ascon-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ use ascon::State;
pub use digest::{self, Digest, ExtendableOutput, Reset, Update, XofReader};
use digest::{
HashMarker, Output, OutputSizeUser,
block_buffer::Eager,
consts::{U8, U32},
consts::{U8, U32, U40},
core_api::{
AlgorithmName, Block, Buffer, BufferKindUser, CoreWrapper, ExtendableOutputCore,
FixedOutputCore, UpdateCore, XofReaderCore, XofReaderCoreWrapper,
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, ExtendableOutputCore,
FixedOutputCore, UpdateCore, XofReaderCore,
},
crypto_common::BlockSizeUser,
crypto_common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
};

/// Produce mask for padding.
Expand Down Expand Up @@ -176,6 +175,26 @@ impl AlgorithmName for AsconCore {
}
}

impl SerializableState for AsconCore {
type SerializedStateSize = U40;

fn serialize(&self) -> SerializedState<Self> {
self.state.state.as_bytes().into()
}

fn deserialize(
serialized_state: &SerializedState<Self>,
) -> Result<Self, DeserializeStateError> {
let state = ascon::State::from(&serialized_state.0);
Ok(Self {
state: HashCore {
state,
phantom: PhantomData,
},
})
}
}

/// Ascon XOF
#[derive(Clone, Debug, Default)]
pub struct AsconXofCore {
Expand Down Expand Up @@ -241,29 +260,34 @@ impl AlgorithmName for AsconXofCore {
}
}

/// Ascon-Hash256
///
/// ```
/// use ascon_hash::{AsconHash256, Digest};
///
/// let mut hasher = AsconHash256::new();
/// hasher.update(b"some bytes");
/// let digest = hasher.finalize();
/// assert_eq!(&digest[..], b"\xe9\x09\xc2\xf6\xda\x9c\xb3\x02\x84\x23\x26\x5c\x8f\x23\xfc\x2d\x26\xbf\xc0\xf3\xdb\x70\x46\x83\xef\x16\xb7\x87\xa9\x45\xed\x68");
/// ```
pub type AsconHash256 = CoreWrapper<AsconCore>;
/// Ascon-XOF128
///
/// ```
/// use ascon_hash::{AsconXof128, ExtendableOutput, Update, XofReader};
///
/// let mut xof = AsconXof128::default();
/// xof.update(b"some bytes");
/// let mut reader = xof.finalize_xof();
/// let mut dst = [0u8; 5];
/// reader.read(&mut dst);
/// assert_eq!(&dst, b"\x8c\x7d\xd1\x14\xa0");
/// ```
pub type AsconXof128 = CoreWrapper<AsconXofCore>;
/// Reader for AsconXOF output
pub type AsconXof128Reader = XofReaderCoreWrapper<AsconXofReaderCore>;
impl SerializableState for AsconXofCore {
type SerializedStateSize = U40;

fn serialize(&self) -> SerializedState<Self> {
self.state.state.as_bytes().into()
}

fn deserialize(
serialized_state: &SerializedState<Self>,
) -> Result<Self, DeserializeStateError> {
let state = ascon::State::from(&serialized_state.0);
Ok(Self {
state: HashCore {
state,
phantom: PhantomData,
},
})
}
}

digest::newtype_fixed_hash!(
/// Ascon-Hash256
pub struct AsconHash256(AsconCore);
);

digest::newtype_xof_hash!(
/// Ascon-XOF128 hasher.
pub struct AsconXof128(AsconXofCore);
/// Ascon-XOF128 reader.
pub struct AsconXof128Reader(AsconXofReaderCore);
);
4 changes: 2 additions & 2 deletions belt-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ hex-literal = "1"
base16ct = { version = "0.2", features = ["alloc"] }

[features]
default = ["oid", "std"]
std = ["digest/std"]
default = ["alloc", "oid"]
alloc = ["digest/alloc"]
oid = ["digest/oid"]
zeroize = ["digest/zeroize"]

Expand Down
Loading
Loading