|
|
|
@@ -0,0 +1,170 @@
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
//! Litecoin's fixed scrypt proof-of-work hash for validating reported shares.
|
|
|
|
|
//! This module hashes one supplied header and never searches for a nonce.
|
|
|
|
|
|
|
|
|
|
use crate::pow::sha256;
|
|
|
|
|
|
|
|
|
|
const N: usize = 1024;
|
|
|
|
|
|
|
|
|
|
fn hmac_sha256(key: &[u8], message: &[u8]) -> [u8; 32] {
|
|
|
|
|
let mut block = [0_u8; 64];
|
|
|
|
|
if key.len() > block.len() {
|
|
|
|
|
block[..32].copy_from_slice(&sha256(key));
|
|
|
|
|
} else {
|
|
|
|
|
block[..key.len()].copy_from_slice(key);
|
|
|
|
|
}
|
|
|
|
|
let mut inner = Vec::with_capacity(64 + message.len());
|
|
|
|
|
inner.extend(block.iter().map(|byte| byte ^ 0x36));
|
|
|
|
|
inner.extend_from_slice(message);
|
|
|
|
|
let inner_hash = sha256(&inner);
|
|
|
|
|
let mut outer = [0_u8; 96];
|
|
|
|
|
for (destination, byte) in outer[..64].iter_mut().zip(block) {
|
|
|
|
|
*destination = byte ^ 0x5c;
|
|
|
|
|
}
|
|
|
|
|
outer[64..].copy_from_slice(&inner_hash);
|
|
|
|
|
sha256(&outer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PBKDF2-HMAC-SHA256 with the fixed single iteration specified for scrypt.
|
|
|
|
|
fn pbkdf2_one(password: &[u8], salt: &[u8], output: &mut [u8]) {
|
|
|
|
|
let mut message = Vec::with_capacity(salt.len() + 4);
|
|
|
|
|
message.extend_from_slice(salt);
|
|
|
|
|
message.extend_from_slice(&[0_u8; 4]);
|
|
|
|
|
for (index, chunk) in output.chunks_mut(32).enumerate() {
|
|
|
|
|
let block = u32::try_from(index + 1).expect("fixed scrypt output fits u32");
|
|
|
|
|
message[salt.len()..].copy_from_slice(&block.to_be_bytes());
|
|
|
|
|
chunk.copy_from_slice(&hmac_sha256(password, &message)[..chunk.len()]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn salsa20_8(input: &mut [u32; 16]) {
|
|
|
|
|
let original = *input;
|
|
|
|
|
let mut x = original;
|
|
|
|
|
for _ in 0..4 {
|
|
|
|
|
x[4] ^= x[0].wrapping_add(x[12]).rotate_left(7);
|
|
|
|
|
x[8] ^= x[4].wrapping_add(x[0]).rotate_left(9);
|
|
|
|
|
x[12] ^= x[8].wrapping_add(x[4]).rotate_left(13);
|
|
|
|
|
x[0] ^= x[12].wrapping_add(x[8]).rotate_left(18);
|
|
|
|
|
x[9] ^= x[5].wrapping_add(x[1]).rotate_left(7);
|
|
|
|
|
x[13] ^= x[9].wrapping_add(x[5]).rotate_left(9);
|
|
|
|
|
x[1] ^= x[13].wrapping_add(x[9]).rotate_left(13);
|
|
|
|
|
x[5] ^= x[1].wrapping_add(x[13]).rotate_left(18);
|
|
|
|
|
x[14] ^= x[10].wrapping_add(x[6]).rotate_left(7);
|
|
|
|
|
x[2] ^= x[14].wrapping_add(x[10]).rotate_left(9);
|
|
|
|
|
x[6] ^= x[2].wrapping_add(x[14]).rotate_left(13);
|
|
|
|
|
x[10] ^= x[6].wrapping_add(x[2]).rotate_left(18);
|
|
|
|
|
x[3] ^= x[15].wrapping_add(x[11]).rotate_left(7);
|
|
|
|
|
x[7] ^= x[3].wrapping_add(x[15]).rotate_left(9);
|
|
|
|
|
x[11] ^= x[7].wrapping_add(x[3]).rotate_left(13);
|
|
|
|
|
x[15] ^= x[11].wrapping_add(x[7]).rotate_left(18);
|
|
|
|
|
|
|
|
|
|
x[1] ^= x[0].wrapping_add(x[3]).rotate_left(7);
|
|
|
|
|
x[2] ^= x[1].wrapping_add(x[0]).rotate_left(9);
|
|
|
|
|
x[3] ^= x[2].wrapping_add(x[1]).rotate_left(13);
|
|
|
|
|
x[0] ^= x[3].wrapping_add(x[2]).rotate_left(18);
|
|
|
|
|
x[6] ^= x[5].wrapping_add(x[4]).rotate_left(7);
|
|
|
|
|
x[7] ^= x[6].wrapping_add(x[5]).rotate_left(9);
|
|
|
|
|
x[4] ^= x[7].wrapping_add(x[6]).rotate_left(13);
|
|
|
|
|
x[5] ^= x[4].wrapping_add(x[7]).rotate_left(18);
|
|
|
|
|
x[11] ^= x[10].wrapping_add(x[9]).rotate_left(7);
|
|
|
|
|
x[8] ^= x[11].wrapping_add(x[10]).rotate_left(9);
|
|
|
|
|
x[9] ^= x[8].wrapping_add(x[11]).rotate_left(13);
|
|
|
|
|
x[10] ^= x[9].wrapping_add(x[8]).rotate_left(18);
|
|
|
|
|
x[12] ^= x[15].wrapping_add(x[14]).rotate_left(7);
|
|
|
|
|
x[13] ^= x[12].wrapping_add(x[15]).rotate_left(9);
|
|
|
|
|
x[14] ^= x[13].wrapping_add(x[12]).rotate_left(13);
|
|
|
|
|
x[15] ^= x[14].wrapping_add(x[13]).rotate_left(18);
|
|
|
|
|
}
|
|
|
|
|
for ((word, initial), mixed) in input.iter_mut().zip(original).zip(x) {
|
|
|
|
|
*word = initial.wrapping_add(mixed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn block_mix(input: &[u32; 32]) -> [u32; 32] {
|
|
|
|
|
let mut x: [u32; 16] = input[16..].try_into().expect("fixed block size");
|
|
|
|
|
let mut output = [0_u32; 32];
|
|
|
|
|
for (index, chunk) in input.chunks_exact(16).enumerate() {
|
|
|
|
|
for (word, source) in x.iter_mut().zip(chunk) {
|
|
|
|
|
*word ^= source;
|
|
|
|
|
}
|
|
|
|
|
salsa20_8(&mut x);
|
|
|
|
|
output[index * 16..(index + 1) * 16].copy_from_slice(&x);
|
|
|
|
|
}
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Hash one 80-byte Litecoin header with scrypt(N=1024, r=1, p=1, dkLen=32).
|
|
|
|
|
/// The return value is the raw little-endian PoW digest.
|
|
|
|
|
pub fn scrypt_1024_1_1_256(header: &[u8; 80]) -> [u8; 32] {
|
|
|
|
|
let mut initial = [0_u8; 128];
|
|
|
|
|
pbkdf2_one(header, header, &mut initial);
|
|
|
|
|
let mut x = [0_u32; 32];
|
|
|
|
|
for (word, bytes) in x.iter_mut().zip(initial.chunks_exact(4)) {
|
|
|
|
|
*word = u32::from_le_bytes(bytes.try_into().expect("four-byte word"));
|
|
|
|
|
}
|
|
|
|
|
let mut memory = vec![[0_u32; 32]; N];
|
|
|
|
|
for saved in &mut memory {
|
|
|
|
|
*saved = x;
|
|
|
|
|
x = block_mix(&x);
|
|
|
|
|
}
|
|
|
|
|
for _ in 0..N {
|
|
|
|
|
let index = x[16] as usize & (N - 1);
|
|
|
|
|
for (word, saved) in x.iter_mut().zip(memory[index]) {
|
|
|
|
|
*word ^= saved;
|
|
|
|
|
}
|
|
|
|
|
x = block_mix(&x);
|
|
|
|
|
}
|
|
|
|
|
for (word, bytes) in x.iter().zip(initial.chunks_exact_mut(4)) {
|
|
|
|
|
bytes.copy_from_slice(&word.to_le_bytes());
|
|
|
|
|
}
|
|
|
|
|
let mut digest = [0_u8; 32];
|
|
|
|
|
pbkdf2_one(header, &initial, &mut digest);
|
|
|
|
|
digest
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Validate one supplied header against an exact little-endian pool target.
|
|
|
|
|
pub fn verify_scrypt_header(header: &[u8; 80], target_le: &[u8; 32]) -> ([u8; 32], bool) {
|
|
|
|
|
let digest = scrypt_1024_1_1_256(header);
|
|
|
|
|
let meets_target = digest.iter().rev().cmp(target_le.iter().rev()).is_le();
|
|
|
|
|
(digest, meets_target)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn litecoin_block_29255_matches_published_scrypt_pow() {
|
|
|
|
|
let header_hex = concat!(
|
|
|
|
|
"01000000f615f7ce3b4fc6b8f61e8f89aedb1d0852507650533a9e3b10b9bbcc30639f27",
|
|
|
|
|
"9fcaa86746e1ef52d3edb3c4ad8259920d509bd073605c9bf1d59983752a6b06b817bb4",
|
|
|
|
|
"ea78e011d012d59d4"
|
|
|
|
|
);
|
|
|
|
|
let mut header = [0_u8; 80];
|
|
|
|
|
for (byte, pair) in header.iter_mut().zip(header_hex.as_bytes().chunks_exact(2)) {
|
|
|
|
|
*byte = u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap();
|
|
|
|
|
}
|
|
|
|
|
let expected_big = "0000000110c8357966576df46f3b802ca897deb7ad18b12f1c24ecff6386ebd9";
|
|
|
|
|
let mut expected = [0_u8; 32];
|
|
|
|
|
for (byte, pair) in expected
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.rev()
|
|
|
|
|
.zip(expected_big.as_bytes().chunks_exact(2))
|
|
|
|
|
{
|
|
|
|
|
*byte = u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap();
|
|
|
|
|
}
|
|
|
|
|
let (digest, accepted) = verify_scrypt_header(&header, &expected);
|
|
|
|
|
assert_eq!(digest, expected);
|
|
|
|
|
assert!(accepted);
|
|
|
|
|
assert!(!verify_scrypt_header(&header, &[0_u8; 32]).1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn fixed_header_matches_independent_openssl_scrypt() {
|
|
|
|
|
let header = core::array::from_fn(|index| index as u8);
|
|
|
|
|
let expected = "bc540a1a801df96e493005c71e010e2d387607fbf0fec416fd3c2645aa1ba9d2";
|
|
|
|
|
let actual = scrypt_1024_1_1_256(&header);
|
|
|
|
|
let actual_hex: String = actual.iter().map(|byte| format!("{byte:02x}")).collect();
|
|
|
|
|
assert_eq!(actual_hex, expected);
|
|
|
|
|
}
|
|
|
|
|
}
|