-
Notifications
You must be signed in to change notification settings - Fork 0
add: MD5 implementation #13
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
base: main
Are you sure you want to change the base?
Changes from all commits
cb91b67
3ab74b4
e708094
5e1ede1
bae7450
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [package] | ||
| edition = "2021" | ||
| name = "message-digest-5" | ||
| version = "0.1.0" | ||
|
|
||
| [dependencies] | ||
| itertools = { workspace = true } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,5 @@ | ||||||
| # Message Digest version 5 | ||||||
supragya marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| See associated info at: [here](https://hackmd.io/@sraj/zk-hashfunc-comparison) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider improving the link text to be more descriptive than "here" to enhance document readability. - See associated info at: [here](https://hackmd.io/@sraj/zk-hashfunc-comparison)
+ See associated info at: [Comparative Analysis of Hash Functions](https://hackmd.io/@sraj/zk-hashfunc-comparison)Committable suggestion
Suggested change
|
||||||
|
|
||||||
| ## References | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// A generalized trait for hashing systems using | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// a Merkle–Damgård construction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| trait MerkleDamgard { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type InternalState; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type Chunk; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type HashOutput; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// An MD-compliant input padding logic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn pad_input(message: &[u8]) -> Vec<Self::Chunk>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Compressor step to consume a chunk and affect the state | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn apply_compressor(state: &mut Self::InternalState, chunk: Self::Chunk); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Final output producer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn finalize() -> Self::HashOutput; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the trait - trait MerkleDamgard {
+ pub trait MerkleDamgard {Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct MessageDigestV5 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| round_constants: [u32; 64], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shift_amounts: [u32; 64], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| initialization_state: [u32; 4], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl MessageDigestV5 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[rustfmt::skip] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn new() -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The table formula function: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // K[i] = floor(2^32 * abs(sin(i)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn round_constant_generator(i: u32) -> u32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let x: f64 = i as f64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let sin_eval = x.sin().abs(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // note: 4294967296 == 2^32 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (4294967296.0 * sin_eval) as u32 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| round_constants: (0..64) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map(|x| round_constant_generator(x+1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .collect::<Vec<u32>>()[0..64] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .try_into() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shift_amounts: [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [7, 12, 17, 22].into_iter().cycle().take(16).collect::<Vec<u32>>(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [5, 9, 14, 20].into_iter().cycle().take(16).collect::<Vec<u32>>(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [4, 11, 16, 23].into_iter().cycle().take(16).collect::<Vec<u32>>(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [6, 10, 15, 21].into_iter().cycle().take(16).collect::<Vec<u32>>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ].concat()[0..64] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .try_into() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| initialization_state: [0x67452301u32, 0xefcdab89u32, 0x98badcfeu32, 0x10325476u32], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl MerkleDamgard for MessageDigestV5 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type InternalState = [u32; 4]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type Chunk = u32; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type HashOutput = [u32; 4]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn pad_input(message: &[u8]) -> Vec<Self::Chunk> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| todo!() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn apply_compressor(state: &mut Self::InternalState, chunk: Self::Chunk) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| todo!() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn finalize() -> Self::HashOutput { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| todo!() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+61
to
+71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Complete the implementations of Would you like assistance in implementing these methods or should I open a GitHub issue to track this task? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(test)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mod tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn two() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(2 == 2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+76
to
+79
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test function Would you like me to help generate more detailed unit tests? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Add blank lines around the list for better Markdown formatting.
Committable suggestion