diff --git a/Cargo.toml b/Cargo.toml index 7e726fb..4367aeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ "polynomial", "univariate-polynomial-iop-zerotest", "halo2-trials", + "[Rfc1321]message-digest-v5-hash", "[Sha97]shamir-secret-sharing", "[Fel87]feldman-verifiable-secret-sharing", "[Sch91]schnorr-discrete-log-proof-of-knowledge", @@ -28,3 +29,4 @@ rand_core = "0.6.4" rand = "0.8.5" rand_chacha = "0.3.1" simba = "0.8.1" +itertools = "0.12.1" diff --git a/README.md b/README.md index f4be4e8..636ace2 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ cd Cryptography-Research && cargo test ``` ## References -- **\[Sha97\]**: Shamir's secret sharing https://apps.dtic.mil/sti/pdfs/ADA069397.pdf. -- **\[Fel87\]**: Feldman's verifiable secret sharing https://www.zkdocs.com/docs/zkdocs/protocol-primitives/verifiable-secret-sharing/ -- **\[Sch91\]**: Schnorr's DLog PoK https://www.zkdocs.com/docs/zkdocs/zero-knowledge-protocols/schnorr/ +- **\[Sha97\]**: [Shamir's secret sharing](https://apps.dtic.mil/sti/pdfs/ADA069397.pdf) +- **\[Fel87\]**: [Feldman's verifiable secret sharing](https://www.zkdocs.com/docs/zkdocs/protocol-primitives/verifiable-secret-sharing/) +- **\[Sch91\]**: [Schnorr's DLog PoK](https://www.zkdocs.com/docs/zkdocs/zero-knowledge-protocols/schnorr/) +- **\[rfc1321\]**: [Message Digest v5 hashing function](https://datatracker.ietf.org/doc/html/rfc1321) diff --git a/[rfc1321]message-digest-v5-hash/Cargo.toml b/[rfc1321]message-digest-v5-hash/Cargo.toml new file mode 100644 index 0000000..faac360 --- /dev/null +++ b/[rfc1321]message-digest-v5-hash/Cargo.toml @@ -0,0 +1,7 @@ +[package] +edition = "2021" +name = "message-digest-5" +version = "0.1.0" + +[dependencies] +itertools = { workspace = true } diff --git a/[rfc1321]message-digest-v5-hash/README.md b/[rfc1321]message-digest-v5-hash/README.md new file mode 100644 index 0000000..097bb03 --- /dev/null +++ b/[rfc1321]message-digest-v5-hash/README.md @@ -0,0 +1,5 @@ +# Message Digest version 5 + +See associated info at: [here](https://hackmd.io/@sraj/zk-hashfunc-comparison) + +## References diff --git a/[rfc1321]message-digest-v5-hash/src/lib.rs b/[rfc1321]message-digest-v5-hash/src/lib.rs new file mode 100644 index 0000000..ec8c92c --- /dev/null +++ b/[rfc1321]message-digest-v5-hash/src/lib.rs @@ -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; + + /// 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; +} + +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::>()[0..64] + .try_into() + .unwrap(), + shift_amounts: [ + [7, 12, 17, 22].into_iter().cycle().take(16).collect::>(), + [5, 9, 14, 20].into_iter().cycle().take(16).collect::>(), + [4, 11, 16, 23].into_iter().cycle().take(16).collect::>(), + [6, 10, 15, 21].into_iter().cycle().take(16).collect::>() + ].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 { + todo!() + } + + fn apply_compressor(state: &mut Self::InternalState, chunk: Self::Chunk) { + todo!() + } + + fn finalize() -> Self::HashOutput { + todo!() + } +} + +#[cfg(test)] +mod tests { + #[test] + fn two() { + assert!(2 == 2); + } +}