mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-30 19:47:17 +02:00
LibCrypto: Add hash methods to {Signed, Unsigned}BigInteger
These just use hash the underlying bytes that make up the integer words
This commit is contained in:
committed by
Linus Groh
parent
71c54198fa
commit
b17a282b4b
Notes:
sideshowbarker
2024-07-18 12:34:22 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/b17a282b4b0 Pull-request: https://github.com/SerenityOS/serenity/pull/7928 Reviewed-by: https://github.com/linusg
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "UnsignedBigInteger.h"
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/StringHash.h>
|
||||
#include <LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h>
|
||||
|
||||
namespace Crypto {
|
||||
@@ -106,6 +107,7 @@ void UnsignedBigInteger::set_to_0()
|
||||
m_words.clear_with_capacity();
|
||||
m_is_invalid = false;
|
||||
m_cached_trimmed_length = {};
|
||||
m_cached_hash = 0;
|
||||
}
|
||||
|
||||
void UnsignedBigInteger::set_to(UnsignedBigInteger::Word other)
|
||||
@@ -114,6 +116,7 @@ void UnsignedBigInteger::set_to(UnsignedBigInteger::Word other)
|
||||
m_words.resize_and_keep_capacity(1);
|
||||
m_words[0] = other;
|
||||
m_cached_trimmed_length = {};
|
||||
m_cached_hash = 0;
|
||||
}
|
||||
|
||||
void UnsignedBigInteger::set_to(const UnsignedBigInteger& other)
|
||||
@@ -122,6 +125,7 @@ void UnsignedBigInteger::set_to(const UnsignedBigInteger& other)
|
||||
m_words.resize_and_keep_capacity(other.m_words.size());
|
||||
__builtin_memcpy(m_words.data(), other.m_words.data(), other.m_words.size() * sizeof(u32));
|
||||
m_cached_trimmed_length = {};
|
||||
m_cached_hash = 0;
|
||||
}
|
||||
|
||||
size_t UnsignedBigInteger::trimmed_length() const
|
||||
@@ -252,6 +256,14 @@ FLATTEN UnsignedDivisionResult UnsignedBigInteger::divided_by(const UnsignedBigI
|
||||
return UnsignedDivisionResult { quotient, remainder };
|
||||
}
|
||||
|
||||
u32 UnsignedBigInteger::hash() const
|
||||
{
|
||||
if (m_cached_hash != 0)
|
||||
return m_cached_hash;
|
||||
|
||||
return m_cached_hash = string_hash((const char*)m_words.data(), sizeof(Word) * m_words.size());
|
||||
}
|
||||
|
||||
void UnsignedBigInteger::set_bit_inplace(size_t bit_index)
|
||||
{
|
||||
const size_t word_index = bit_index / UnsignedBigInteger::BITS_IN_WORD;
|
||||
@@ -265,6 +277,7 @@ void UnsignedBigInteger::set_bit_inplace(size_t bit_index)
|
||||
m_words[word_index] |= (1 << inner_word_index);
|
||||
|
||||
m_cached_trimmed_length = {};
|
||||
m_cached_hash = 0;
|
||||
}
|
||||
|
||||
bool UnsignedBigInteger::operator==(const UnsignedBigInteger& other) const
|
||||
|
||||
Reference in New Issue
Block a user