Skip to content

Commit

Permalink
fix the fishhash & fishhashplus implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
okilisan committed May 24, 2024
1 parent 9e40f23 commit 81d0192
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 86 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 23 additions & 6 deletions consensus/pow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct State {
pub(crate) target: Uint256,
// PRE_POW_HASH || TIME || 32 zero byte padding; without NONCE
pub(crate) hasher: PowB3Hash,
pub(crate) fishhasher: PowFishHash,
//pub(crate) fishhasher: PowFishHash,
pub(crate) header_version: u16,
}

Expand All @@ -35,10 +35,10 @@ impl State {
//let hasher = PowHash::new(pre_pow_hash, header.timestamp);
let hasher = PowB3Hash::new(pre_pow_hash, header.timestamp);
let matrix = Matrix::generate(pre_pow_hash);
let fishhasher = PowFishHash::new();
//let fishhasher = PowFishHash::new();
let header_version = header.version;

Self { matrix, target, hasher, fishhasher, header_version}
Self { matrix, target, hasher, /*fishhasher,*/ header_version}
}


Expand All @@ -52,7 +52,24 @@ impl State {
fn calculate_pow_khashv2(&self, nonce: u64) -> Uint256 {
// Hasher already contains PRE_POW_HASH || TIME || 32 zero byte padding; so only the NONCE is missing
let hash = self.hasher.clone().finalize_with_nonce(nonce);
let hash = self.matrix.heavy_hash(hash);
//println!("hash-1 : {:?}", hash);
let hash = PowFishHash::fishhash_kernel(&hash);
//println!("hash-2 : {:?}", hash);
//last b3 hash
let hash = PowB3Hash::hash(hash);
//println!("hash-3 : {:?}", hash);
Uint256::from_le_bytes(hash.as_bytes())
}

fn calculate_pow_khashv2plus(&self, nonce: u64) -> Uint256 {
// Hasher already contains PRE_POW_HASH || TIME || 32 zero byte padding; so only the NONCE is missing
let hash = self.hasher.clone().finalize_with_nonce(nonce);
//println!("hash-1 : {:?}", hash);
let hash = PowFishHash::fishhashplus_kernel(&hash);
//println!("hash-2 : {:?}", hash);
//last b3 hash
let hash = PowB3Hash::hash(hash);
//println!("hash-3 : {:?}", hash);
Uint256::from_le_bytes(hash.as_bytes())
}

Expand All @@ -61,9 +78,9 @@ impl State {
/// PRE_POW_HASH || TIME || 32 zero byte padding || NONCE
pub fn calculate_pow(&self, nonce: u64) -> Uint256 {
if self.header_version == constants::BLOCK_VERSION {
return self.calculate_pow_khashv1(nonce);
} else if self.header_version == constants::BLOCK_VERSION_KHASHV2 {
return self.calculate_pow_khashv2(nonce);
} else if self.header_version == constants::BLOCK_VERSION_KHASHV2 {
return self.calculate_pow_khashv2plus(nonce);
} else {
// TODO handle block version error
//Err(RuleError::WrongBlockVersion(self.header_version));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl HeaderProcessor {
Ok(())
}

// TODO : setup dual block version managment
fn check_header_version(&self, header: &Header) -> BlockProcessResult<()> {
if header.version != constants::BLOCK_VERSION_KHASHV2 {
return Err(RuleError::WrongBlockVersion(header.version));
Expand Down
1 change: 1 addition & 0 deletions crypto/hashes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repository.workspace = true
no-asm = ["keccak"]

[dependencies]
lazy_static = "1.4.0"
blake2b_simd.workspace = true
borsh.workspace = true
faster-hex.workspace = true
Expand Down
Loading

0 comments on commit 81d0192

Please sign in to comment.