mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:47:47 +00: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:
parent
71c54198fa
commit
b17a282b4b
4 changed files with 25 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue