1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:57:35 +00:00

LibCrypto: Add {Signed,Unsigned}BigInteger::from_base{2, 8, 16} helpers

These can be used to create BigInteger instances from non-decimal
number strings.
This commit is contained in:
Idan Horowitz 2021-06-14 02:18:06 +03:00 committed by Linus Groh
parent e4e6e03364
commit 2ad2e055e2
4 changed files with 69 additions and 2 deletions

View file

@ -27,7 +27,7 @@ size_t SignedBigInteger::export_data(Bytes data, bool remove_leading_zeros) cons
return m_unsigned_data.export_data(bytes_view, remove_leading_zeros) + 1; return m_unsigned_data.export_data(bytes_view, remove_leading_zeros) + 1;
} }
SignedBigInteger SignedBigInteger::from_base10(StringView str) static bool parse_sign(StringView& str)
{ {
bool sign = false; bool sign = false;
if (str.length() > 1) { if (str.length() > 1) {
@ -39,10 +39,37 @@ SignedBigInteger SignedBigInteger::from_base10(StringView str)
if (maybe_sign == '+') if (maybe_sign == '+')
str = str.substring_view(1, str.length() - 1); str = str.substring_view(1, str.length() - 1);
} }
return sign;
}
SignedBigInteger SignedBigInteger::from_base10(StringView str)
{
auto sign = parse_sign(str);
auto unsigned_data = UnsignedBigInteger::from_base10(str); auto unsigned_data = UnsignedBigInteger::from_base10(str);
return { move(unsigned_data), sign }; return { move(unsigned_data), sign };
} }
SignedBigInteger SignedBigInteger::from_base2(StringView str)
{
auto sign = parse_sign(str);
auto unsigned_data = UnsignedBigInteger::from_base2(str);
return { move(unsigned_data), sign };
}
SignedBigInteger SignedBigInteger::from_base8(StringView str)
{
auto sign = parse_sign(str);
auto unsigned_data = UnsignedBigInteger::from_base8(str);
return { move(unsigned_data), sign };
}
SignedBigInteger SignedBigInteger::from_base16(StringView str)
{
auto sign = parse_sign(str);
auto unsigned_data = UnsignedBigInteger::from_base16(str);
return { move(unsigned_data), sign };
}
String SignedBigInteger::to_base10() const String SignedBigInteger::to_base10() const
{ {
StringBuilder builder; StringBuilder builder;

View file

@ -62,8 +62,11 @@ public:
size_t export_data(Bytes, bool remove_leading_zeros = false) const; size_t export_data(Bytes, bool remove_leading_zeros = false) const;
static SignedBigInteger from_base2(StringView str);
static SignedBigInteger from_base8(StringView str);
static SignedBigInteger from_base10(StringView str); static SignedBigInteger from_base10(StringView str);
String to_base10() const; String to_base10() const;
static SignedBigInteger from_base16(StringView str);
u64 to_u64() const; u64 to_u64() const;

View file

@ -5,6 +5,7 @@
*/ */
#include "UnsignedBigInteger.h" #include "UnsignedBigInteger.h"
#include <AK/CharacterTypes.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/StringHash.h> #include <AK/StringHash.h>
#include <LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h> #include <LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h>
@ -71,7 +72,40 @@ UnsignedBigInteger UnsignedBigInteger::from_base10(const String& str)
UnsignedBigInteger ten { 10 }; UnsignedBigInteger ten { 10 };
for (auto& c : str) { for (auto& c : str) {
result = result.multiplied_by(ten).plus(c - '0'); result = result.multiplied_by(ten).plus(parse_ascii_digit(c));
}
return result;
}
UnsignedBigInteger UnsignedBigInteger::from_base2(const String& str)
{
UnsignedBigInteger result;
UnsignedBigInteger two { 2 };
for (auto& c : str) {
result = result.multiplied_by(two).plus(parse_ascii_digit(c));
}
return result;
}
UnsignedBigInteger UnsignedBigInteger::from_base8(const String& str)
{
UnsignedBigInteger result;
UnsignedBigInteger eight { 8 };
for (auto& c : str) {
result = result.multiplied_by(eight).plus(parse_ascii_digit(c));
}
return result;
}
UnsignedBigInteger UnsignedBigInteger::from_base16(const String& str)
{
UnsignedBigInteger result;
UnsignedBigInteger sixteen { 16 };
for (auto& c : str) {
result = result.multiplied_by(sixteen).plus(parse_ascii_hex_digit(c));
} }
return result; return result;
} }

View file

@ -53,8 +53,11 @@ public:
size_t export_data(Bytes, bool remove_leading_zeros = false) const; size_t export_data(Bytes, bool remove_leading_zeros = false) const;
static UnsignedBigInteger from_base2(const String& str);
static UnsignedBigInteger from_base8(const String& str);
static UnsignedBigInteger from_base10(const String& str); static UnsignedBigInteger from_base10(const String& str);
String to_base10() const; String to_base10() const;
static UnsignedBigInteger from_base16(const String& str);
u64 to_u64() const; u64 to_u64() const;