-
Notifications
You must be signed in to change notification settings - Fork 39
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
Switch from sha256 to keccak256 #23
Changes from all commits
f192cf3
ee06bdd
407e1b6
e7595ac
6e562ac
8076d36
2ce0686
7095489
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
//! Wrappers for the SHA256 cryptographic hash algorithm. | ||
//! Wrappers for the Keccak256 cryptographic hash algorithm. | ||
use crate::ByteFmt; | ||
use sha2::{digest::Update as _, Digest as _}; | ||
use sha3::{digest::Update as _, Digest as _}; | ||
|
||
#[cfg(test)] | ||
mod test; | ||
pub mod testonly; | ||
|
||
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. Off-topic (?): Why does this module need to be public? AFAICT, it doesn't contain public items. 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. Not sure if you refer to line 5 (added) or 6 (already exists).
|
||
/// SHA256 hash. | ||
/// Keccak256 hash. | ||
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct Sha256(pub(crate) [u8; 32]); | ||
pub struct Keccak256(pub(crate) [u8; 32]); | ||
|
||
impl Sha256 { | ||
/// Computes a SHA-256 hash of a message. | ||
impl Keccak256 { | ||
/// Computes a Keccak256 hash of a message. | ||
pub fn new(msg: &[u8]) -> Self { | ||
Self(sha2::Sha256::new().chain(msg).finalize().into()) | ||
Self(sha3::Keccak256::new().chain(msg).finalize().into()) | ||
} | ||
|
||
/// Interprets the specified `bytes` as a hash digest (i.e., a reverse operation to [`Self::as_bytes()`]). | ||
/// It is caller's responsibility to ensure that `bytes` are actually a SHA-256 hash digest. | ||
/// It is caller's responsibility to ensure that `bytes` are actually a Keccak256 hash digest. | ||
pub fn from_bytes(bytes: [u8; 32]) -> Self { | ||
Self(bytes) | ||
} | ||
|
@@ -26,7 +28,7 @@ impl Sha256 { | |
} | ||
} | ||
|
||
impl ByteFmt for Sha256 { | ||
impl ByteFmt for Keccak256 { | ||
fn decode(bytes: &[u8]) -> anyhow::Result<Self> { | ||
Ok(Self(bytes.try_into()?)) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#[test] | ||
fn test_keccak256() -> Result<(), Box<dyn std::error::Error>> { | ||
use crate::keccak256::Keccak256; | ||
|
||
// Test vectors obtained from https://emn178.github.io/online-tools/keccak_256.html | ||
let test_vectors: Vec<(&[u8], [u8; 32])> = vec![ | ||
( | ||
b"testing", | ||
hex::decode("5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02")? | ||
.try_into() | ||
.unwrap(), | ||
), | ||
( | ||
b"", | ||
hex::decode("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")? | ||
.try_into() | ||
.unwrap(), | ||
), | ||
( | ||
&[0x12, 0x34, 0x56], | ||
hex::decode("6adf031833174bbe4c85eafe59ddb54e6584648c2c962c6f94791ab49caa0ad4")? | ||
.try_into() | ||
.unwrap(), | ||
), | ||
]; | ||
|
||
for (input, expected_hash) in &test_vectors { | ||
let hash = Keccak256::new(input); | ||
assert_eq!(hash.as_bytes(), expected_hash); | ||
} | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//! Random hash generation, intended for use in testing | ||
|
||
use crate::keccak256::Keccak256; | ||
use rand::{ | ||
distributions::{Distribution, Standard}, | ||
Rng, | ||
}; | ||
|
||
impl Distribution<Keccak256> for Standard { | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Keccak256 { | ||
Keccak256(rng.gen()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ pub mod bls12_381; | |
pub mod bn254; | ||
pub mod ed25519; | ||
mod fmt; | ||
pub mod sha256; | ||
pub mod keccak256; |
This file was deleted.
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.
this has nothing to do with evm. Is keccak faster than sha?
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.
Depends. On hardware implementations Keccak/SHA3 is faster, but if there's no hardware support then it's slower than SHA2.
That being said, I just think it's easier for now to just use one hash algorithm for everything. If/when we do code profiling and realize that hashing is a bottleneck, we can change to a faster algorithm like BLAKE3 wherever we can.