[*] i was taking a shit when i remembered our hashcash algorithm is fundamentally borked

This commit is contained in:
Reece Wilson 2023-05-20 02:38:47 +01:00
parent b7de15d78e
commit 824602b1fa

View File

@ -12,50 +12,87 @@ namespace Aurora::Crypto::HashCash
{ {
AUKN_SYM bool CheckSalt(AuUInt64 token, AuUInt8 power, const HashCashAnswer &answer) AUKN_SYM bool CheckSalt(AuUInt64 token, AuUInt8 power, const HashCashAnswer &answer)
{ {
AuUInt8 count {};
AuArray<AuUInt8, 16> hashBuffer;
struct Hash for (AU_ITERATE_N(i, 2))
{ {
AuUInt64 token; AuUInt64 uCoefficient {};
AuUInt64 y;
} hash;
static_assert(sizeof(Hash) == 16);
hash.token = token; AuArray<AuUInt8, 16> hashBuffer;
hash.y = answer.y; 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); 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)); AuBitScanForward(count, AuReadU64LE(hashBuffer.data(), 0));
return count == power; if (count != power)
{
return false;
}
}
return true;
} }
AUKN_SYM HashCashAnswer FindAnswer(AuUInt64 token, AuUInt8 power) AUKN_SYM HashCashAnswer FindAnswer(AuUInt64 token, AuUInt8 power)
{ {
HashCashAnswer answer; HashCashAnswer answer {};
AuArray<AuUInt8, 16> refBuffer;
AuArray<AuUInt8, 16> rollingBuffer;
AuArray<AuUInt8, 16> hashBuffer; AuArray<AuUInt8, 16> hashBuffer;
struct Hash for (AU_ITERATE_N(i, 2))
{ {
AuUInt64 token; if (i == 0)
AuUInt64 y; {
} hash; AuHashing::RMD128(AuMemoryViewRead(&token, sizeof(token)), refBuffer);
static_assert(sizeof(Hash) == 16); }
else
hash.token = token; {
hash.y = AuRng::RngU64(); AuHashing::RMD128(AuMemoryViewRead(&token, sizeof(token)), refBuffer);
AuHashing::RMD128(AuMemoryViewRead(refBuffer.data(), refBuffer.size()), refBuffer);
}
AuUInt64 uTicks {};
AuUInt8 count {}; AuUInt8 count {};
do do
{ {
hash.y++; rollingBuffer = refBuffer;
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); while (AuBitScanForward(count, AuReadU64LE(hashBuffer.data(), 0)), count != power);
answer.y = hash.y; uTicks--;
if (i == 0)
{
answer.y = uTicks;
}
else
{
answer.y |= uTicks << 32;
}
}
return answer; return answer;
} }
} }