Skip to content

Commit 6f607e9

Browse files
committed
refactor(sha256): use bitcoin::hashes::sha256 instead of sha2 crate
Per reviewer feedback on PR #208: bitcoin_hashes::sha256 is already available via the bitcoin crate re-export and provides equivalent SHA-NI hardware acceleration. Drops the direct sha2 dependency. Bench (ubuntu-latest w/ SHA-NI, 1000 tx status hashes): rust-crypto 273.8 µs sha2 54.2 µs bitcoin_hashes 53.9 µs
1 parent 45e3daa commit 6f607e9

File tree

5 files changed

+15
-94
lines changed

5 files changed

+15
-94
lines changed

Cargo.lock

Lines changed: 0 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ prometheus = "0.14"
5050
rayon = "1.5.0"
5151
rocksdb = "0.24"
5252
tikv-jemallocator = "0.6"
53-
sha2 = "0.10"
5453
serde = "1.0.118"
5554
serde_derive = "1.0.118"
5655
serde_json = "1.0.60"

src/electrum/server.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use std::sync::{Arc, Mutex, RwLock};
66
use std::thread;
77
use std::time::Instant;
88

9-
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
9+
use bitcoin::hashes::{sha256, sha256d::Hash as Sha256dHash, Hash, HashEngine};
1010
use bitcoin::hex::DisplayHex;
11-
use sha2::{Digest, Sha256};
1211
use error_chain::ChainedError;
1312
use serde_json::{from_str, Value};
1413

@@ -75,17 +74,17 @@ fn get_status_hash(txs: Vec<(Txid, Option<BlockId>)>, query: &Query) -> Option<F
7574
if txs.is_empty() {
7675
None
7776
} else {
78-
let mut hasher = Sha256::new();
77+
let mut engine = sha256::Hash::engine();
7978
for (txid, blockid) in txs {
8079
let is_mempool = blockid.is_none();
8180
let has_unconfirmed_parents = is_mempool
8281
.and_then(|| Some(query.has_unconfirmed_parents(&txid)))
8382
.unwrap_or(false);
8483
let height = get_electrum_height(blockid, has_unconfirmed_parents);
8584
let part = format!("{}:{}:", txid, height);
86-
hasher.update(part.as_bytes());
85+
engine.input(part.as_bytes());
8786
}
88-
Some(hasher.finalize().into())
87+
Some(sha256::Hash::from_engine(engine).to_byte_array())
8988
}
9089
}
9190

@@ -525,10 +524,10 @@ impl Connection {
525524
}
526525

527526
fn hash_ip_with_salt(&self, ip: &str) -> String {
528-
let mut hasher = Sha256::new();
529-
hasher.update(self.salt.as_bytes());
530-
hasher.update(ip.as_bytes());
531-
format!("{:x}", hasher.finalize())
527+
let mut engine = sha256::Hash::engine();
528+
engine.input(self.salt.as_bytes());
529+
engine.input(ip.as_bytes());
530+
format!("{:x}", sha256::Hash::from_engine(engine))
532531
}
533532

534533
fn log_rpc_event(&self, mut log: Value) {

src/new_index/precache.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::errors::*;
33
use crate::new_index::ChainQuery;
44
use crate::util::FullHash;
55

6-
use sha2::{Digest, Sha256};
76
use rayon::prelude::*;
87

8+
use bitcoin::hashes::{sha256, Hash};
99
use bitcoin::hex::FromHex;
1010
use std::fs::File;
1111
use std::io;
@@ -73,7 +73,5 @@ fn address_to_scripthash(addr: &str) -> Result<FullHash> {
7373
}
7474

7575
pub fn compute_script_hash(data: &[u8]) -> FullHash {
76-
let mut hasher = Sha256::new();
77-
hasher.update(data);
78-
hasher.finalize().into()
76+
sha256::Hash::hash(data).to_byte_array()
7977
}

src/new_index/schema.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
1+
use bitcoin::hashes::{sha256, sha256d::Hash as Sha256dHash, Hash};
22
use bitcoin::hex::FromHex;
33
#[cfg(not(feature = "liquid"))]
44
use bitcoin::merkle_tree::MerkleBlock;
55

6-
use sha2::{Digest, Sha256};
76
use itertools::Itertools;
87
use rayon::prelude::*;
98

@@ -1437,9 +1436,7 @@ fn addr_search_filter(prefix: &str) -> Bytes {
14371436
pub type FullHash = [u8; 32]; // serialized SHA256 result
14381437

14391438
pub fn compute_script_hash(script: &Script) -> FullHash {
1440-
let mut hasher = Sha256::new();
1441-
hasher.update(script.as_bytes());
1442-
hasher.finalize().into()
1439+
sha256::Hash::hash(script.as_bytes()).to_byte_array()
14431440
}
14441441

14451442
pub fn parse_hash(hash: &FullHash) -> Sha256dHash {
@@ -1988,9 +1985,7 @@ mod tests {
19881985
0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b,
19891986
0x78, 0x52, 0xb8, 0x55,
19901987
];
1991-
let mut hasher = Sha256::new();
1992-
hasher.update(b"");
1993-
let result: FullHash = hasher.finalize().into();
1988+
let result: FullHash = sha256::Hash::hash(b"").to_byte_array();
19941989
assert_eq!(result, expected);
19951990
}
19961991

@@ -2002,9 +1997,7 @@ mod tests {
20021997
0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61,
20031998
0xf2, 0x00, 0x15, 0xad,
20041999
];
2005-
let mut hasher = Sha256::new();
2006-
hasher.update(b"abc");
2007-
let result: FullHash = hasher.finalize().into();
2000+
let result: FullHash = sha256::Hash::hash(b"abc").to_byte_array();
20082001
assert_eq!(result, expected);
20092002
}
20102003

@@ -2018,9 +2011,7 @@ mod tests {
20182011
"6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b"
20192012
).unwrap();
20202013

2021-
let mut hasher = Sha256::new();
2022-
hasher.update(&script_bytes);
2023-
let hash: FullHash = hasher.finalize().into();
2014+
let hash: FullHash = sha256::Hash::hash(&script_bytes).to_byte_array();
20242015
assert_eq!(hash, expected.as_slice());
20252016
}
20262017
}

0 commit comments

Comments
 (0)