Skip to content

Commit

Permalink
Simplify tiger
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Jan 11, 2024
1 parent c080ac9 commit f3a4279
Showing 1 changed file with 36 additions and 110 deletions.
146 changes: 36 additions & 110 deletions tiger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,31 @@ const S0: State = [

/// Core Tiger hasher state.
#[derive(Clone)]
pub struct TigerCore {
pub struct TigerCore<const IS_VER2: bool> {
block_len: u64,
state: State,
}

/// Tiger hasher state.
pub type Tiger = CoreWrapper<TigerCore>;
pub type Tiger = CoreWrapper<TigerCore<false>>;
/// Tiger2 hasher state.
pub type Tiger2 = CoreWrapper<TigerCore<true>>;

impl HashMarker for TigerCore {}
impl<const IS_VER2: bool> HashMarker for TigerCore<IS_VER2> {}

impl BlockSizeUser for TigerCore {
impl<const IS_VER2: bool> BlockSizeUser for TigerCore<IS_VER2> {
type BlockSize = U64;
}

impl BufferKindUser for TigerCore {
impl<const IS_VER2: bool> BufferKindUser for TigerCore<IS_VER2> {
type BufferKind = Eager;
}

impl OutputSizeUser for TigerCore {
impl<const IS_VER2: bool> OutputSizeUser for TigerCore<IS_VER2> {
type OutputSize = U24;
}

impl UpdateCore for TigerCore {
impl<const IS_VER2: bool> UpdateCore for TigerCore<IS_VER2> {
#[inline]
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.block_len += blocks.len() as u64;
Expand All @@ -69,143 +71,67 @@ impl UpdateCore for TigerCore {
}
}

impl FixedOutputCore for TigerCore {
impl<const IS_VER2: bool> FixedOutputCore for TigerCore<IS_VER2> {
#[inline]
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let bs = Self::BlockSize::U64;
let pos = buffer.get_pos() as u64;
let bit_len = 8 * (pos + bs * self.block_len);

buffer.digest_pad(1, &bit_len.to_le_bytes(), |b| {
compress(&mut self.state, b.as_ref())
});
for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) {
chunk.copy_from_slice(&v.to_le_bytes());
}
}
}

impl Default for TigerCore {
fn default() -> Self {
Self {
block_len: 0,
state: S0,
}
}
}

impl Reset for TigerCore {
fn reset(&mut self) {
*self = Default::default();
}
}

impl AlgorithmName for TigerCore {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Tiger")
}
}

impl fmt::Debug for TigerCore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("TigerCore { ... }")
}
}

impl Drop for TigerCore {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
{
self.state.zeroize();
self.block_len.zeroize();
if IS_VER2 {
buffer.len64_padding_le(bit_len, |b| compress(&mut self.state, b.as_ref()));
} else {
buffer.digest_pad(1, &bit_len.to_le_bytes(), |b| {
compress(&mut self.state, b.as_ref())
});
}
}
}

#[cfg(feature = "zeroize")]
impl ZeroizeOnDrop for TigerCore {}

/// Core Tiger2 hasher state.
#[derive(Clone)]
pub struct Tiger2Core {
block_len: u64,
state: State,
}

/// Tiger2 hasher state.
pub type Tiger2 = CoreWrapper<Tiger2Core>;

impl HashMarker for Tiger2Core {}

impl BlockSizeUser for Tiger2Core {
type BlockSize = U64;
}

impl BufferKindUser for Tiger2Core {
type BufferKind = Eager;
}

impl OutputSizeUser for Tiger2Core {
type OutputSize = U24;
}

impl UpdateCore for Tiger2Core {
#[inline]
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.block_len += blocks.len() as u64;
for block in blocks {
compress(&mut self.state, block.as_ref());
}
}
}

impl FixedOutputCore for Tiger2Core {
#[inline]
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let bs = Self::BlockSize::U64;
let pos = buffer.get_pos() as u64;
let bit_len = 8 * (pos + bs * self.block_len);

buffer.len64_padding_le(bit_len, |b| compress(&mut self.state, b.as_ref()));
for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) {
chunk.copy_from_slice(&v.to_le_bytes());
}
}
}

impl Default for Tiger2Core {
impl<const IS_VER2: bool> Default for TigerCore<IS_VER2> {
#[inline]
fn default() -> Self {
Self {
block_len: 0,
state: [
0x0123_4567_89AB_CDEF,
0xFEDC_BA98_7654_3210,
0xF096_A5B4_C3B2_E187,
],
state: S0,
}
}
}

impl Reset for Tiger2Core {
impl<const IS_VER2: bool> Reset for TigerCore<IS_VER2> {
#[inline]
fn reset(&mut self) {
*self = Default::default();
}
}

impl AlgorithmName for Tiger2Core {
impl<const IS_VER2: bool> AlgorithmName for TigerCore<IS_VER2> {
#[inline]
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Tiger2")
if IS_VER2 {
f.write_str("Tiger2")
} else {
f.write_str("Tiger")
}
}
}

impl fmt::Debug for Tiger2Core {
impl<const IS_VER2: bool> fmt::Debug for TigerCore<IS_VER2> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Tiger2Core { ... }")
if IS_VER2 {
f.write_str("Tiger2Core { ... }")
} else {
f.write_str("TigerCore { ... }")
}
}
}

impl Drop for Tiger2Core {
impl<const IS_VER2: bool> Drop for TigerCore<IS_VER2> {
#[inline]
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
{
Expand All @@ -216,4 +142,4 @@ impl Drop for Tiger2Core {
}

#[cfg(feature = "zeroize")]
impl ZeroizeOnDrop for Tiger2Core {}
impl<const IS_VER2: bool> ZeroizeOnDrop for TigerCore<IS_VER2> {}

0 comments on commit f3a4279

Please sign in to comment.