Skip to content
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

keccak-hash: add keccak256_range and keccak512_range functions #370

Merged
merged 5 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion keccak-hash/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ The format is based on [Keep a Changelog].

## [Unreleased]

## [0.4.2] - 2020-03-16
## [0.5.1] - 2020-04-10
- Added `keccak256_range` and `keccak512_range` functions. [#370](https://github.com/paritytech/parity-common/pull/370)

## [0.5.0] - 2020-03-16
- License changed from GPL3 to dual MIT/Apache2. [#342](https://github.com/paritytech/parity-common/pull/342)
- Updated dependencies. [#361](https://github.com/paritytech/parity-common/pull/361)
- Updated tiny-keccak. [#260](https://github.com/paritytech/parity-common/pull/260)

## [0.4.1] - 2019-10-24
### Dependencies
Expand Down
50 changes: 50 additions & 0 deletions keccak-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,63 @@ pub fn keccak256(data: &mut [u8]) {
keccak256.finalize(data);
}

/// Computes in-place keccak256 hash of `data[range]`.
///
/// The `range` argument specifies a subslice of `data` in bytes to be hashed.
/// The resulting hash will be written back to `data`.
/// # Panics
///
/// If `range` is out of bounds.
///
/// # Example
///
/// ```
/// let mut data = [1u8; 32];
/// keccak_hash::keccak256_range(&mut data, 0..8);
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
ordian marked this conversation as resolved.
Show resolved Hide resolved
/// let expected = [
/// 0x54, 0x84, 0x4f, 0x69, 0xb4, 0xda, 0x4b, 0xb4, 0xa9, 0x9f, 0x24, 0x59, 0xb5, 0x11, 0xd4, 0x42,
/// 0xcc, 0x5b, 0xd2, 0xfd, 0xf4, 0xc3, 0x54, 0xd2, 0x07, 0xbb, 0x13, 0x08, 0x94, 0x43, 0xaf, 0x68,
/// ];
/// assert_eq!(&data, &expected);
/// ```
pub fn keccak256_range(data: &mut [u8], range: core::ops::Range<usize>) {
let mut keccak256 = Keccak::v256();
keccak256.update(&data[range]);
keccak256.finalize(data);
}

/// Computes in-place keccak512 hash of `data`.
pub fn keccak512(data: &mut [u8]) {
let mut keccak512 = Keccak::v512();
keccak512.update(data.as_ref());
keccak512.finalize(data);
}

/// Computes in-place keccak512 hash of `data[range]`.
///
/// The `range` argument specifies a subslice of `data` in bytes to be hashed.
/// The resulting hash will be written back to `data`.
/// # Panics
///
/// If `range` is out of bounds.
///
/// # Example
///
/// ```
/// let mut data = [1u8; 64];
/// keccak_hash::keccak512_range(&mut data, 0..8);
/// let expected = [
/// 0x90, 0x45, 0xc5, 0x9e, 0xd3, 0x0e, 0x1f, 0x42, 0xac, 0x35, 0xcc, 0xc9, 0x55, 0x7c, 0x77, 0x17,
/// 0xc8, 0x89, 0x3a, 0x77, 0x6c, 0xea, 0x2e, 0xf3, 0x88, 0xea, 0xe5, 0xc0, 0xea, 0x40, 0x26, 0x64,
/// ];
/// assert_eq!(&data[..32], &expected);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note, that I'm checking here only the first 32 bytes of data, because Debug is not implemented for arrays of larger size 🙈

/// ```
pub fn keccak512_range(data: &mut [u8], range: core::ops::Range<usize>) {
let mut keccak512 = Keccak::v512();
keccak512.update(&data[range]);
keccak512.finalize(data);
}

pub fn keccak_256(input: &[u8], output: &mut [u8]) {
write_keccak(input, output);
}
Expand Down