-
Notifications
You must be signed in to change notification settings - Fork 253
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
tiger: implement TTH algorithm #494
Draft
baodrate
wants to merge
2
commits into
RustCrypto:master
Choose a base branch
from
baodrate:tth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use crate::{Digest, Tiger, TigerCore}; | ||
use arrayvec::ArrayVec; | ||
use core::fmt; | ||
use digest::{ | ||
core_api::{ | ||
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, FixedOutputCore, | ||
OutputSizeUser, Reset, UpdateCore, | ||
}, | ||
typenum::Unsigned, | ||
typenum::U1024, | ||
HashMarker, Output, | ||
}; | ||
|
||
#[derive(Clone)] | ||
struct TigerTreeNode { | ||
level: u32, | ||
hash: Output<TigerCore>, | ||
} | ||
|
||
/// Core Tiger hasher state. | ||
#[derive(Clone)] | ||
pub struct TigerTreeCore { | ||
nodes: ArrayVec<TigerTreeNode, MAX_TREE_HEIGHT>, | ||
hasher: Tiger, | ||
blocks_processed: usize, | ||
} | ||
|
||
impl TigerTreeCore { | ||
fn add_node(&mut self, level: u32, hash: Output<TigerCore>) { | ||
match self.nodes.last() { | ||
Some(last) if last.level == level => { | ||
let new_hash = Tiger::digest([&[NODE_SIG][..], &last.hash, &hash].concat()); | ||
self.nodes.pop(); | ||
self.add_node(level + 1, new_hash); | ||
} | ||
_ => self.nodes.push(TigerTreeNode { level, hash }), | ||
} | ||
} | ||
} | ||
|
||
impl Default for TigerTreeCore { | ||
fn default() -> Self { | ||
Self { | ||
nodes: ArrayVec::default(), | ||
hasher: Tiger::new_with_prefix([LEAF_SIG]), | ||
blocks_processed: 0, | ||
} | ||
} | ||
} | ||
|
||
type DataBlockSize = U1024; | ||
const LEAF_SIG: u8 = 0u8; | ||
const NODE_SIG: u8 = 1u8; | ||
/// The maximum height of a TigerTree based on the maximum addressable data length | ||
// max height = log2(usize::MAX / DataBlockSize) = log2(2^usize::BITS) - log2(DataBlockSize) = usize::BITS - log2(2^10) | ||
const MAX_TREE_HEIGHT: usize = (usize::BITS - 10 + 1) as usize; | ||
/// The number of TigerCore blocks in a TigerTree data block | ||
const LEAF_BLOCKS: usize = DataBlockSize::USIZE / <TigerCore as BlockSizeUser>::BlockSize::USIZE; | ||
|
||
impl HashMarker for TigerTreeCore {} | ||
|
||
impl BlockSizeUser for TigerTreeCore { | ||
type BlockSize = <TigerCore as BlockSizeUser>::BlockSize; | ||
} | ||
|
||
impl BufferKindUser for TigerTreeCore { | ||
type BufferKind = <TigerCore as BufferKindUser>::BufferKind; | ||
} | ||
|
||
impl OutputSizeUser for TigerTreeCore { | ||
type OutputSize = <TigerCore as OutputSizeUser>::OutputSize; | ||
} | ||
|
||
impl TigerTreeCore { | ||
#[inline] | ||
fn finalize_blocks(&mut self) { | ||
let hasher = core::mem::replace(&mut self.hasher, Tiger::new_with_prefix([LEAF_SIG])); | ||
let hash = hasher.finalize(); | ||
self.add_node(0, hash); | ||
self.blocks_processed = 0; | ||
} | ||
|
||
#[inline] | ||
fn update_block(&mut self, block: Block<Self>) { | ||
self.hasher.update(block); | ||
self.blocks_processed += 1; | ||
if self.blocks_processed == LEAF_BLOCKS { | ||
self.finalize_blocks(); | ||
} | ||
} | ||
} | ||
|
||
impl UpdateCore for TigerTreeCore { | ||
#[inline] | ||
fn update_blocks(&mut self, blocks: &[Block<Self>]) { | ||
for block in blocks { | ||
self.update_block(*block); | ||
} | ||
} | ||
} | ||
|
||
impl FixedOutputCore for TigerTreeCore { | ||
#[inline] | ||
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { | ||
if buffer.get_pos() > 0 { | ||
self.hasher.update(buffer.get_data()); | ||
self.blocks_processed += 1; | ||
} | ||
|
||
if self.blocks_processed > 0 { | ||
self.finalize_blocks() | ||
} | ||
|
||
let mut hash = self | ||
.nodes | ||
.pop() | ||
.map(|n| n.hash) | ||
.unwrap_or_else(|| Tiger::digest([LEAF_SIG])); | ||
while let Some(left) = self.nodes.pop() { | ||
hash = Tiger::digest([&[NODE_SIG][..], &left.hash, &hash].concat()); | ||
} | ||
out.copy_from_slice(hash.as_slice()); | ||
} | ||
} | ||
|
||
impl Reset for TigerTreeCore { | ||
#[inline] | ||
fn reset(&mut self) { | ||
*self = Default::default(); | ||
} | ||
} | ||
|
||
impl AlgorithmName for TigerTreeCore { | ||
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str("TigerTree") | ||
} | ||
} | ||
|
||
impl fmt::Debug for TigerTreeCore { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str("TigerTreeCore { ... }") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to write it like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's embarrassing, I meant to clean that up before submitting the PR. Will do