mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-20 08:18:55 +00:00
LibCrypto: Don't compute 2*N remainders in Adler32
Otherwise Zlib decompression spends half of the time computing the checksum.
This commit is contained in:
parent
b7b51d01c3
commit
7ae990d2e5
Notes:
sideshowbarker
2024-07-16 18:06:41 +09:00
Author: https://github.com/DanShaders
Commit: 7ae990d2e5
Pull-request: https://github.com/SerenityOS/serenity/pull/24408
Reviewed-by: https://github.com/alimpfard ✅
1 changed files with 25 additions and 3 deletions
|
@ -12,10 +12,32 @@ namespace Crypto::Checksum {
|
||||||
|
|
||||||
void Adler32::update(ReadonlyBytes data)
|
void Adler32::update(ReadonlyBytes data)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < data.size(); i++) {
|
// See https://github.com/SerenityOS/serenity/pull/24408#discussion_r1609051678
|
||||||
m_state_a = (m_state_a + data.at(i)) % 65521;
|
constexpr size_t iterations_without_overflow = 380368439;
|
||||||
m_state_b = (m_state_b + m_state_a) % 65521;
|
|
||||||
|
u64 state_a = m_state_a;
|
||||||
|
u64 state_b = m_state_b;
|
||||||
|
while (data.size()) {
|
||||||
|
// You can verify that no overflow will happen here during at least
|
||||||
|
// `iterations_without_overflow` iterations using the following Python script:
|
||||||
|
//
|
||||||
|
// state_a = 65520
|
||||||
|
// state_b = 65520
|
||||||
|
// for i in range(380368439):
|
||||||
|
// state_a += 255
|
||||||
|
// state_b += state_a
|
||||||
|
// print(state_b < 2 ** 64)
|
||||||
|
auto chunk = data.slice(0, min(data.size(), iterations_without_overflow));
|
||||||
|
for (u8 byte : chunk) {
|
||||||
|
state_a += byte;
|
||||||
|
state_b += state_a;
|
||||||
}
|
}
|
||||||
|
state_a %= 65521;
|
||||||
|
state_b %= 65521;
|
||||||
|
data = data.slice(chunk.size());
|
||||||
|
}
|
||||||
|
m_state_a = state_a;
|
||||||
|
m_state_b = state_b;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 Adler32::digest()
|
u32 Adler32::digest()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue