1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:17:44 +00:00

LibCrypto+LibJS: Remove the create_from methods from BigInteger

Instead we just use a specific constructor. With this set of
constructors using curly braces for constructing is highly recommended.
As then it will not do too many implicit conversions which could lead to
unexpected loss of data or calling the much slower double constructor.

Also to ensure we don't feed (Un)SignedBigInteger infinities we throw
RangeError earlier for Durations.
This commit is contained in:
davidot 2022-08-26 00:49:50 +02:00 committed by Linus Groh
parent 528891bf69
commit 791855deab
19 changed files with 77 additions and 82 deletions

View file

@ -45,6 +45,12 @@ public:
explicit SignedBigInteger(double value);
explicit SignedBigInteger(i64 value)
: m_sign(value < 0)
, m_unsigned_data(value < 0 ? static_cast<u64>(-(value + 1)) + 1 : static_cast<u64>(value))
{
}
[[nodiscard]] static SignedBigInteger create_invalid()
{
return { UnsignedBigInteger::create_invalid(), false };
@ -53,19 +59,6 @@ public:
[[nodiscard]] static SignedBigInteger import_data(StringView data) { return import_data((u8 const*)data.characters_without_null_termination(), data.length()); }
[[nodiscard]] static SignedBigInteger import_data(u8 const* ptr, size_t length);
[[nodiscard]] static SignedBigInteger create_from(i64 value)
{
auto sign = false;
u64 unsigned_value;
if (value < 0) {
unsigned_value = static_cast<u64>(-(value + 1)) + 1;
sign = true;
} else {
unsigned_value = value;
}
return SignedBigInteger { UnsignedBigInteger::create_from(unsigned_value), sign };
}
size_t export_data(Bytes, bool remove_leading_zeros = false) const;
[[nodiscard]] static SignedBigInteger from_base(u16 N, StringView str);

View file

@ -41,6 +41,14 @@ public:
explicit UnsignedBigInteger(double value);
explicit UnsignedBigInteger(u64 value)
{
static_assert(sizeof(u64) == sizeof(Word) * 2);
m_words.resize_and_keep_capacity(2);
m_words[0] = static_cast<Word>(value & 0xFFFFFFFF);
m_words[1] = static_cast<Word>((value >> 32) & 0xFFFFFFFF);
}
UnsignedBigInteger() = default;
[[nodiscard]] static UnsignedBigInteger create_invalid();
@ -51,16 +59,6 @@ public:
return UnsignedBigInteger(ptr, length);
}
[[nodiscard]] static UnsignedBigInteger create_from(u64 value)
{
VERIFY(sizeof(Word) == 4);
UnsignedBigInteger integer;
integer.m_words.resize(2);
integer.m_words[0] = static_cast<Word>(value & 0xFFFFFFFF);
integer.m_words[1] = static_cast<Word>((value >> 32) & 0xFFFFFFFF);
return integer;
}
size_t export_data(Bytes, bool remove_leading_zeros = false) const;
[[nodiscard]] static UnsignedBigInteger from_base(u16 N, StringView str);