forked from rust-bitcoin/rust-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge rust-bitcoin#3989: Do
hashes
file re-org
85e0330 Run the formatter (Tobin C. Harding) 7be0db7 hashes: Move bench and test code into files (Tobin C. Harding) 665fa9d hashes: Pull crypto out into submodule (Tobin C. Harding) 1bfd1e0 hashes: Make module subdirectories (Tobin C. Harding) Pull request description: This is an attempt at point 3 in rust-bitcoin#3961 (comment) However instead of using `api.rs` and `implementation.rs` it uses `crypto.rs` for the cryptography stuff and leaves the rest in `mod.rs`. This whole PR is internal changes only. Almost entirely code moves, review is easy if you have your `diff` configured nicely. ACKs for top commit: Kixunil: ACK 85e0330 apoelstra: ACK 85e0330; successfully ran local tests; look great! thanks! Tree-SHA512: e52e6410e86fc93ec34a72be8c64f02148f46958f8f5c1850075b1a7f41886c220f09144ccd05a98a551c356a5d25524c8884fc8578a205b27f385ec0725f13b
- Loading branch information
Showing
26 changed files
with
1,291 additions
and
1,287 deletions.
There are no files selected for viewing
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,33 @@ | ||
use test::Bencher; | ||
|
||
use crate::{ripemd160, Hash, HashEngine}; | ||
|
||
#[bench] | ||
pub fn ripemd160_10(bh: &mut Bencher) { | ||
let mut engine = ripemd160::Hash::engine(); | ||
let bytes = [1u8; 10]; | ||
bh.iter(|| { | ||
engine.input(&bytes); | ||
}); | ||
bh.bytes = bytes.len() as u64; | ||
} | ||
|
||
#[bench] | ||
pub fn ripemd160_1k(bh: &mut Bencher) { | ||
let mut engine = ripemd160::Hash::engine(); | ||
let bytes = [1u8; 1024]; | ||
bh.iter(|| { | ||
engine.input(&bytes); | ||
}); | ||
bh.bytes = bytes.len() as u64; | ||
} | ||
|
||
#[bench] | ||
pub fn ripemd160_64k(bh: &mut Bencher) { | ||
let mut engine = ripemd160::Hash::engine(); | ||
let bytes = [1u8; 65536]; | ||
bh.iter(|| { | ||
engine.input(&bytes); | ||
}); | ||
bh.bytes = bytes.len() as u64; | ||
} |
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,95 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
|
||
//! RIPEMD160 implementation. | ||
#[cfg(bench)] | ||
mod benches; | ||
mod crypto; | ||
#[cfg(bench)] | ||
mod tests; | ||
|
||
use core::cmp; | ||
|
||
use crate::{incomplete_block_len, HashEngine as _}; | ||
|
||
crate::internal_macros::general_hash_type! { | ||
160, | ||
false, | ||
"Output of the RIPEMD160 hash function." | ||
} | ||
|
||
#[cfg(not(hashes_fuzz))] | ||
fn from_engine(mut e: HashEngine) -> Hash { | ||
// pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining | ||
let n_bytes_hashed = e.bytes_hashed; | ||
|
||
let zeroes = [0; BLOCK_SIZE - 8]; | ||
e.input(&[0x80]); | ||
if crate::incomplete_block_len(&e) > zeroes.len() { | ||
e.input(&zeroes); | ||
} | ||
let pad_length = zeroes.len() - incomplete_block_len(&e); | ||
e.input(&zeroes[..pad_length]); | ||
debug_assert_eq!(incomplete_block_len(&e), zeroes.len()); | ||
|
||
e.input(&(8 * n_bytes_hashed).to_le_bytes()); | ||
debug_assert_eq!(incomplete_block_len(&e), 0); | ||
|
||
Hash(e.midstate()) | ||
} | ||
|
||
#[cfg(hashes_fuzz)] | ||
fn from_engine(e: HashEngine) -> Hash { | ||
let mut res = e.midstate(); | ||
res[0] ^= (e.bytes_hashed & 0xff) as u8; | ||
Hash(res) | ||
} | ||
|
||
const BLOCK_SIZE: usize = 64; | ||
|
||
/// Engine to compute RIPEMD160 hash function. | ||
#[derive(Clone)] | ||
pub struct HashEngine { | ||
buffer: [u8; BLOCK_SIZE], | ||
h: [u32; 5], | ||
bytes_hashed: u64, | ||
} | ||
|
||
impl HashEngine { | ||
/// Constructs a new SHA256 hash engine. | ||
pub const fn new() -> Self { | ||
Self { | ||
h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0], | ||
bytes_hashed: 0, | ||
buffer: [0; BLOCK_SIZE], | ||
} | ||
} | ||
|
||
#[cfg(not(hashes_fuzz))] | ||
fn midstate(&self) -> [u8; 20] { | ||
let mut ret = [0; 20]; | ||
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_exact_mut(4)) { | ||
ret_bytes.copy_from_slice(&(*val).to_le_bytes()); | ||
} | ||
ret | ||
} | ||
|
||
#[cfg(hashes_fuzz)] | ||
fn midstate(&self) -> [u8; 20] { | ||
let mut ret = [0; 20]; | ||
ret.copy_from_slice(&self.buffer[..20]); | ||
ret | ||
} | ||
} | ||
|
||
impl Default for HashEngine { | ||
fn default() -> Self { Self::new() } | ||
} | ||
|
||
impl crate::HashEngine for HashEngine { | ||
const BLOCK_SIZE: usize = 64; | ||
|
||
fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } | ||
|
||
crate::internal_macros::engine_input_impl!(); | ||
} |
Oops, something went wrong.