1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:37:35 +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:
Idan Horowitz 2021-06-08 23:43:44 +03:00 committed by Linus Groh
parent 71c54198fa
commit b17a282b4b
4 changed files with 25 additions and 0 deletions

View file

@ -217,6 +217,11 @@ FLATTEN SignedDivisionResult SignedBigInteger::divided_by(const SignedBigInteger
};
}
u32 SignedBigInteger::hash() const
{
return m_unsigned_data.hash() * (1 - (2 * m_sign));
}
void SignedBigInteger::set_bit_inplace(size_t bit_index)
{
m_unsigned_data.set_bit_inplace(bit_index);

View file

@ -99,6 +99,8 @@ public:
SignedBigInteger multiplied_by(const UnsignedBigInteger& other) const;
SignedDivisionResult divided_by(const UnsignedBigInteger& divisor) const;
u32 hash() const;
void set_bit_inplace(size_t bit_index);
bool operator==(const SignedBigInteger& other) const;

View file

@ -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

View file

@ -56,6 +56,7 @@ public:
{
m_is_invalid = true;
m_cached_trimmed_length = {};
m_cached_hash = 0;
}
bool is_odd() const { return m_words.size() && (m_words[0] & 1); }
@ -78,6 +79,8 @@ public:
UnsignedBigInteger multiplied_by(const UnsignedBigInteger& other) const;
UnsignedDivisionResult divided_by(const UnsignedBigInteger& divisor) const;
u32 hash() const;
void set_bit_inplace(size_t bit_index);
bool operator==(const UnsignedBigInteger& other) const;
@ -90,6 +93,8 @@ private:
// m_word[0] + m_word[1] * Word::MAX + m_word[2] * Word::MAX * Word::MAX + ...
Vector<Word, STARTING_WORD_SIZE> m_words;
mutable u32 m_cached_hash { 0 };
// Used to indicate a negative result, or a result of an invalid operation
bool m_is_invalid { false };