Skip to content

Commit 45e3daa

Browse files
committed
test(sha256): add NIST test vectors and P2PKH script hash test
Verify compute_script_hash produces correct SHA-256 output after the rust-crypto to sha2 migration. Tests against NIST vectors for empty string and abc, plus a real P2PKH scriptPubKey.
1 parent 3a66890 commit 45e3daa

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

src/new_index/schema.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,3 +1975,52 @@ pub mod bench {
19751975
super::add_blocks(&[data.block_entry.clone()], &data.iconfig)
19761976
}
19771977
}
1978+
1979+
#[cfg(test)]
1980+
mod tests {
1981+
use super::*;
1982+
1983+
#[test]
1984+
fn test_sha256_empty_input() {
1985+
// NIST SHA-256 test vector: SHA-256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
1986+
let expected: FullHash = [
1987+
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f,
1988+
0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b,
1989+
0x78, 0x52, 0xb8, 0x55,
1990+
];
1991+
let mut hasher = Sha256::new();
1992+
hasher.update(b"");
1993+
let result: FullHash = hasher.finalize().into();
1994+
assert_eq!(result, expected);
1995+
}
1996+
1997+
#[test]
1998+
fn test_sha256_abc() {
1999+
// NIST SHA-256 test vector: SHA-256("abc") = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
2000+
let expected: FullHash = [
2001+
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae,
2002+
0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61,
2003+
0xf2, 0x00, 0x15, 0xad,
2004+
];
2005+
let mut hasher = Sha256::new();
2006+
hasher.update(b"abc");
2007+
let result: FullHash = hasher.finalize().into();
2008+
assert_eq!(result, expected);
2009+
}
2010+
2011+
#[test]
2012+
fn test_p2pkh_script_hash() {
2013+
// P2PKH scriptPubKey for address 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
2014+
// OP_DUP OP_HASH160 <20-byte-hash> OP_EQUALVERIFY OP_CHECKSIG
2015+
let script_hex = "76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac";
2016+
let script_bytes: Vec<u8> = Vec::from_hex(script_hex).unwrap();
2017+
let expected: Vec<u8> = Vec::from_hex(
2018+
"6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b"
2019+
).unwrap();
2020+
2021+
let mut hasher = Sha256::new();
2022+
hasher.update(&script_bytes);
2023+
let hash: FullHash = hasher.finalize().into();
2024+
assert_eq!(hash, expected.as_slice());
2025+
}
2026+
}

0 commit comments

Comments
 (0)