|
| 1 | +use crate::{Digest, Tiger, TigerCore}; |
| 2 | +use alloc::vec::Vec; |
| 3 | +use core::fmt; |
| 4 | +use digest::{ |
| 5 | + core_api::{ |
| 6 | + AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, FixedOutputCore, |
| 7 | + OutputSizeUser, Reset, UpdateCore, |
| 8 | + }, |
| 9 | + typenum::Unsigned, |
| 10 | + typenum::U1024, |
| 11 | + HashMarker, Output, |
| 12 | +}; |
| 13 | + |
| 14 | +/// Core Tiger hasher state. |
| 15 | +#[derive(Clone)] |
| 16 | +pub struct TigerTreeCore { |
| 17 | + leaves: Vec<Output<TigerCore>>, |
| 18 | + hasher: Tiger, |
| 19 | + blocks_processed: usize, |
| 20 | +} |
| 21 | + |
| 22 | +impl Default for TigerTreeCore { |
| 23 | + fn default() -> Self { |
| 24 | + Self { |
| 25 | + leaves: Vec::default(), |
| 26 | + hasher: Tiger::new_with_prefix([LEAF_SIG]), |
| 27 | + blocks_processed: 0, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +type DataBlockSize = U1024; |
| 33 | +const LEAF_SIG: u8 = 0u8; |
| 34 | +const NODE_SIG: u8 = 1u8; |
| 35 | +/// The number of TigerCore blocks in a TigerTree data block |
| 36 | +const LEAF_BLOCKS: usize = DataBlockSize::USIZE / <TigerCore as BlockSizeUser>::BlockSize::USIZE; |
| 37 | + |
| 38 | +impl HashMarker for TigerTreeCore {} |
| 39 | + |
| 40 | +impl BlockSizeUser for TigerTreeCore { |
| 41 | + type BlockSize = <TigerCore as BlockSizeUser>::BlockSize; |
| 42 | +} |
| 43 | + |
| 44 | +impl BufferKindUser for TigerTreeCore { |
| 45 | + type BufferKind = <TigerCore as BufferKindUser>::BufferKind; |
| 46 | +} |
| 47 | + |
| 48 | +impl OutputSizeUser for TigerTreeCore { |
| 49 | + type OutputSize = <TigerCore as OutputSizeUser>::OutputSize; |
| 50 | +} |
| 51 | + |
| 52 | +impl TigerTreeCore { |
| 53 | + #[inline] |
| 54 | + fn finalize_blocks(&mut self) { |
| 55 | + let hasher = core::mem::replace(&mut self.hasher, Tiger::new_with_prefix([LEAF_SIG])); |
| 56 | + let hash = hasher.finalize(); |
| 57 | + self.leaves.push(hash); |
| 58 | + self.blocks_processed = 0; |
| 59 | + } |
| 60 | + |
| 61 | + #[inline] |
| 62 | + fn update_block(&mut self, block: Block<Self>) { |
| 63 | + self.hasher.update(block); |
| 64 | + self.blocks_processed += 1; |
| 65 | + if self.blocks_processed == LEAF_BLOCKS { |
| 66 | + self.finalize_blocks(); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl UpdateCore for TigerTreeCore { |
| 72 | + #[inline] |
| 73 | + fn update_blocks(&mut self, blocks: &[Block<Self>]) { |
| 74 | + for block in blocks { |
| 75 | + self.update_block(*block); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl FixedOutputCore for TigerTreeCore { |
| 81 | + #[inline] |
| 82 | + fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { |
| 83 | + if buffer.get_pos() > 0 { |
| 84 | + self.hasher.update(buffer.get_data()); |
| 85 | + self.blocks_processed += 1; |
| 86 | + } |
| 87 | + |
| 88 | + if self.blocks_processed > 0 { |
| 89 | + self.finalize_blocks() |
| 90 | + } |
| 91 | + |
| 92 | + let result = hash_nodes(self.leaves.as_slice()); |
| 93 | + out.copy_from_slice(&result); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[inline] |
| 98 | +fn hash_nodes(hashes: &[Output<TigerCore>]) -> Output<TigerCore> { |
| 99 | + match hashes.len() { |
| 100 | + 0 => hash_nodes(&[Tiger::digest([LEAF_SIG])]), |
| 101 | + 1 => hashes[0], |
| 102 | + _ => { |
| 103 | + let left_hashes = hashes.iter().step_by(2); |
| 104 | + |
| 105 | + let right_hashes = hashes.iter().map(Some).skip(1).chain([None]).step_by(2); |
| 106 | + |
| 107 | + let next_level_hashes: Vec<Output<TigerCore>> = left_hashes |
| 108 | + .zip(right_hashes) |
| 109 | + .map(|(left, right)| match right { |
| 110 | + Some(right) => Tiger::digest([&[NODE_SIG][..], left, right].concat()), |
| 111 | + None => *left, |
| 112 | + }) |
| 113 | + .collect(); |
| 114 | + |
| 115 | + hash_nodes(next_level_hashes.as_slice()) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl Reset for TigerTreeCore { |
| 121 | + #[inline] |
| 122 | + fn reset(&mut self) { |
| 123 | + *self = Default::default(); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl AlgorithmName for TigerTreeCore { |
| 128 | + fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 129 | + f.write_str("TigerTree") |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl fmt::Debug for TigerTreeCore { |
| 134 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 135 | + f.write_str("TigerTreeCore { ... }") |
| 136 | + } |
| 137 | +} |
0 commit comments