2020-04-07 22:37:42 +03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-07 22:37:42 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2020-05-28 20:40:53 +02:00
|
|
|
|
2020-04-10 01:00:37 +04:30
|
|
|
#include <AK/ByteBuffer.h>
|
2020-07-27 14:44:40 +02:00
|
|
|
#include <AK/Span.h>
|
2020-04-09 17:09:11 +03:00
|
|
|
#include <AK/String.h>
|
2020-04-07 22:37:42 +03:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
|
|
namespace Crypto {
|
2020-04-09 12:52:25 +03:00
|
|
|
|
|
|
|
|
struct UnsignedDivisionResult;
|
2020-04-29 22:35:54 +04:30
|
|
|
constexpr size_t STARTING_WORD_SIZE = 512;
|
2020-04-09 12:52:25 +03:00
|
|
|
|
2020-04-07 22:37:42 +03:00
|
|
|
class UnsignedBigInteger {
|
|
|
|
|
public:
|
2021-05-12 13:25:55 +02:00
|
|
|
using Word = u32;
|
|
|
|
|
static constexpr size_t BITS_IN_WORD = 32;
|
|
|
|
|
|
|
|
|
|
UnsignedBigInteger(Word x) { m_words.append(x); }
|
2020-04-08 19:04:36 +03:00
|
|
|
|
2021-05-12 13:25:55 +02:00
|
|
|
explicit UnsignedBigInteger(Vector<Word, STARTING_WORD_SIZE>&& words)
|
2020-05-03 10:56:00 +02:00
|
|
|
: m_words(move(words))
|
2020-04-08 19:04:36 +03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 16:16:05 -06:00
|
|
|
explicit UnsignedBigInteger(const u8* ptr, size_t length);
|
|
|
|
|
|
2020-05-28 20:40:53 +02:00
|
|
|
UnsignedBigInteger() { }
|
2020-04-07 22:37:42 +03:00
|
|
|
|
2020-04-08 13:07:47 +03:00
|
|
|
static UnsignedBigInteger create_invalid();
|
|
|
|
|
|
2021-02-25 21:10:47 +01:00
|
|
|
static UnsignedBigInteger import_data(const StringView& data) { return import_data((const u8*)data.characters_without_null_termination(), data.length()); }
|
2020-07-22 16:16:05 -06:00
|
|
|
static UnsignedBigInteger import_data(const u8* ptr, size_t length)
|
|
|
|
|
{
|
|
|
|
|
return UnsignedBigInteger(ptr, length);
|
|
|
|
|
}
|
2020-04-10 01:00:37 +04:30
|
|
|
|
2020-07-31 13:33:14 +04:30
|
|
|
size_t export_data(Bytes, bool remove_leading_zeros = false) const;
|
2020-04-10 01:00:37 +04:30
|
|
|
|
2020-05-03 10:56:00 +02:00
|
|
|
static UnsignedBigInteger from_base10(const String& str);
|
|
|
|
|
String to_base10() const;
|
|
|
|
|
|
2021-05-12 13:25:55 +02:00
|
|
|
const Vector<Word, STARTING_WORD_SIZE>& words() const { return m_words; }
|
2020-04-07 22:37:42 +03:00
|
|
|
|
2020-05-03 10:57:00 +02:00
|
|
|
void set_to_0();
|
2021-05-12 13:25:55 +02:00
|
|
|
void set_to(Word other);
|
2020-05-03 10:57:00 +02:00
|
|
|
void set_to(const UnsignedBigInteger& other);
|
2020-05-07 12:23:09 +02:00
|
|
|
|
|
|
|
|
void invalidate()
|
|
|
|
|
{
|
|
|
|
|
m_is_invalid = true;
|
|
|
|
|
m_cached_trimmed_length = {};
|
|
|
|
|
}
|
2020-05-03 10:56:00 +02:00
|
|
|
|
2021-05-12 22:47:07 +02:00
|
|
|
bool is_odd() const { return m_words.size() && (m_words[0] & 1); }
|
2020-05-03 10:56:00 +02:00
|
|
|
bool is_invalid() const { return m_is_invalid; }
|
|
|
|
|
|
|
|
|
|
size_t length() const { return m_words.size(); }
|
|
|
|
|
// The "trimmed length" is the number of words after trimming leading zeroed words
|
|
|
|
|
size_t trimmed_length() const;
|
|
|
|
|
|
2021-04-01 12:02:14 +04:30
|
|
|
void clamp_to_trimmed_length();
|
2021-05-12 10:47:21 +02:00
|
|
|
void resize_with_leading_zeros(size_t num_words);
|
2021-04-01 12:02:14 +04:30
|
|
|
|
2020-04-27 19:05:17 +04:30
|
|
|
UnsignedBigInteger plus(const UnsignedBigInteger& other) const;
|
|
|
|
|
UnsignedBigInteger minus(const UnsignedBigInteger& other) const;
|
2020-06-06 01:36:03 +04:30
|
|
|
UnsignedBigInteger bitwise_or(const UnsignedBigInteger& other) const;
|
|
|
|
|
UnsignedBigInteger bitwise_and(const UnsignedBigInteger& other) const;
|
|
|
|
|
UnsignedBigInteger bitwise_xor(const UnsignedBigInteger& other) const;
|
|
|
|
|
UnsignedBigInteger bitwise_not() const;
|
2020-04-08 19:04:36 +03:00
|
|
|
UnsignedBigInteger shift_left(size_t num_bits) const;
|
2020-05-03 10:56:00 +02:00
|
|
|
UnsignedBigInteger multiplied_by(const UnsignedBigInteger& other) const;
|
2020-04-27 19:05:17 +04:30
|
|
|
UnsignedDivisionResult divided_by(const UnsignedBigInteger& divisor) const;
|
2020-04-09 12:52:25 +03:00
|
|
|
|
|
|
|
|
void set_bit_inplace(size_t bit_index);
|
2020-04-07 22:37:42 +03:00
|
|
|
|
|
|
|
|
bool operator==(const UnsignedBigInteger& other) const;
|
2020-04-09 17:09:11 +03:00
|
|
|
bool operator!=(const UnsignedBigInteger& other) const;
|
2020-04-08 13:07:47 +03:00
|
|
|
bool operator<(const UnsignedBigInteger& other) const;
|
|
|
|
|
|
2020-04-07 22:37:42 +03:00
|
|
|
private:
|
2021-05-10 20:55:25 +02:00
|
|
|
friend class UnsignedBigIntegerAlgorithms;
|
2020-08-15 22:53:28 +02:00
|
|
|
// Little endian
|
2021-05-12 13:25:55 +02:00
|
|
|
// m_word[0] + m_word[1] * Word::MAX + m_word[2] * Word::MAX * Word::MAX + ...
|
|
|
|
|
Vector<Word, STARTING_WORD_SIZE> m_words;
|
2020-04-08 13:07:47 +03:00
|
|
|
|
|
|
|
|
// Used to indicate a negative result, or a result of an invalid operation
|
|
|
|
|
bool m_is_invalid { false };
|
2020-05-07 12:23:09 +02:00
|
|
|
|
|
|
|
|
mutable Optional<size_t> m_cached_trimmed_length;
|
2020-04-07 22:37:42 +03:00
|
|
|
};
|
|
|
|
|
|
2020-04-09 12:52:25 +03:00
|
|
|
struct UnsignedDivisionResult {
|
|
|
|
|
Crypto::UnsignedBigInteger quotient;
|
|
|
|
|
Crypto::UnsignedBigInteger remainder;
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-07 22:37:42 +03:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 13:32:26 +01:00
|
|
|
template<>
|
|
|
|
|
struct AK::Formatter<Crypto::UnsignedBigInteger> : Formatter<StringView> {
|
|
|
|
|
void format(FormatBuilder&, const Crypto::UnsignedBigInteger&);
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-10 01:00:37 +04:30
|
|
|
inline Crypto::UnsignedBigInteger
|
|
|
|
|
operator""_bigint(const char* string, size_t length)
|
|
|
|
|
{
|
|
|
|
|
return Crypto::UnsignedBigInteger::from_base10({ string, length });
|
|
|
|
|
}
|