diff --git a/Source/Crypto/HashCash/HashCash.cpp b/Source/Crypto/HashCash/HashCash.cpp index 6a6e6801..4df5afa5 100644 --- a/Source/Crypto/HashCash/HashCash.cpp +++ b/Source/Crypto/HashCash/HashCash.cpp @@ -12,50 +12,87 @@ namespace Aurora::Crypto::HashCash { AUKN_SYM bool CheckSalt(AuUInt64 token, AuUInt8 power, const HashCashAnswer &answer) { - AuUInt8 count {}; - AuArray hashBuffer; - struct Hash + for (AU_ITERATE_N(i, 2)) { - AuUInt64 token; - AuUInt64 y; - } hash; - static_assert(sizeof(Hash) == 16); + AuUInt64 uCoefficient {}; - hash.token = token; - hash.y = answer.y; + AuArray hashBuffer; + if (i == 0) + { + AuHashing::RMD128(AuMemoryViewRead(&token, sizeof(token)), hashBuffer); + uCoefficient = answer.y & 0xFFFFFFFF; + } + else + { + AuHashing::RMD128(AuMemoryViewRead(&token, sizeof(token)), hashBuffer); + AuHashing::RMD128(AuMemoryViewRead(hashBuffer.data(), hashBuffer.size()), hashBuffer); + uCoefficient = (answer.y >> 32) & 0xFFFFFFFF; + } - AuHashing::RMD128(AuMemoryViewRead(&hash, 16), hashBuffer); - - AuBitScanForward(count, AuReadU64LE(hashBuffer.data(), 0)); - - return count == power; + AuWriteU64LE(hashBuffer.data(), + hashBuffer.size() - 8, + AuReadU64LE(hashBuffer.data(), hashBuffer.size() - 8) + uCoefficient); + AuHashing::RMD128(AuMemoryViewRead(hashBuffer.data(), hashBuffer.size()), hashBuffer); + + AuUInt8 count {}; + AuBitScanForward(count, AuReadU64LE(hashBuffer.data(), 0)); + + if (count != power) + { + return false; + } + } + + return true; } AUKN_SYM HashCashAnswer FindAnswer(AuUInt64 token, AuUInt8 power) { - HashCashAnswer answer; + HashCashAnswer answer {}; + AuArray refBuffer; + AuArray rollingBuffer; AuArray hashBuffer; - struct Hash + for (AU_ITERATE_N(i, 2)) { - AuUInt64 token; - AuUInt64 y; - } hash; - static_assert(sizeof(Hash) == 16); + if (i == 0) + { + AuHashing::RMD128(AuMemoryViewRead(&token, sizeof(token)), refBuffer); + } + else + { + AuHashing::RMD128(AuMemoryViewRead(&token, sizeof(token)), refBuffer); + AuHashing::RMD128(AuMemoryViewRead(refBuffer.data(), refBuffer.size()), refBuffer); + } - hash.token = token; - hash.y = AuRng::RngU64(); + AuUInt64 uTicks {}; + AuUInt8 count {}; + do + { + rollingBuffer = refBuffer; - AuUInt8 count {}; - do - { - hash.y++; - AuHashing::RMD128(AuMemoryViewRead(&hash, 16), hashBuffer); + AuWriteU64LE(rollingBuffer.data(), + rollingBuffer.size() - 8, + AuReadU64LE(rollingBuffer.data(), rollingBuffer.size() - 8) + (uTicks++)); + + AuHashing::RMD128(AuMemoryViewRead(rollingBuffer.data(), rollingBuffer.size()), + hashBuffer); + } + while (AuBitScanForward(count, AuReadU64LE(hashBuffer.data(), 0)), count != power); + + uTicks--; + + if (i == 0) + { + answer.y = uTicks; + } + else + { + answer.y |= uTicks << 32; + } } - while (AuBitScanForward(count, AuReadU64LE(hashBuffer.data(), 0)), count != power); - - answer.y = hash.y; + return answer; } } \ No newline at end of file