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

LibCrypto: Rename UnsignedBigInteger APIs to match their actions

This commit is contained in:
AnotherTest 2020-04-27 19:05:17 +04:30 committed by Andreas Kling
parent e366416d51
commit adab43987d
5 changed files with 62 additions and 62 deletions

View file

@ -35,7 +35,7 @@ UnsignedBigInteger UnsignedBigInteger::from_base10(const String& str)
UnsignedBigInteger ten { 10 };
for (auto& c : str) {
result = result.multiply(ten).add(c - '0');
result = result.multiplied_by(ten).plus(c - '0');
}
return result;
}
@ -46,7 +46,7 @@ String UnsignedBigInteger::to_base10() const
UnsignedBigInteger temp(*this);
while (temp != UnsignedBigInteger { 0 }) {
auto div_result = temp.divide({ 10 });
auto div_result = temp.divided_by({ 10 });
ASSERT(div_result.remainder.words()[0] < 10);
builder.append(static_cast<char>(div_result.remainder.words()[0] + '0'));
temp = div_result.quotient;
@ -69,7 +69,7 @@ bool UnsignedBigInteger::operator!=(const UnsignedBigInteger& other) const
/**
* Complexity: O(N) where N is the number of words in the larger number
*/
UnsignedBigInteger UnsignedBigInteger::add(const UnsignedBigInteger& other) const
UnsignedBigInteger UnsignedBigInteger::plus(const UnsignedBigInteger& other) const
{
const UnsignedBigInteger* const longer = (length() > other.length()) ? this : &other;
const UnsignedBigInteger* const shorter = (longer == &other) ? this : &other;
@ -111,7 +111,7 @@ UnsignedBigInteger UnsignedBigInteger::add(const UnsignedBigInteger& other) cons
/**
* Complexity: O(N) where N is the number of words in the larger number
*/
UnsignedBigInteger UnsignedBigInteger::sub(const UnsignedBigInteger& other) const
UnsignedBigInteger UnsignedBigInteger::minus(const UnsignedBigInteger& other) const
{
UnsignedBigInteger result;
@ -149,7 +149,7 @@ UnsignedBigInteger UnsignedBigInteger::sub(const UnsignedBigInteger& other) cons
* So to multiple x*y, we go over each '1' bit in x (say the i'th bit),
* and add y<<i to the result.
*/
UnsignedBigInteger UnsignedBigInteger::multiply(const UnsignedBigInteger& other) const
UnsignedBigInteger UnsignedBigInteger::multiplied_by(const UnsignedBigInteger& other) const
{
UnsignedBigInteger result;
// iterate all bits
@ -161,7 +161,7 @@ UnsignedBigInteger UnsignedBigInteger::multiply(const UnsignedBigInteger& other)
const size_t shift_amount = word_index * UnsignedBigInteger::BITS_IN_WORD + bit_index;
auto shift_result = other.shift_left(shift_amount);
result = result.add(shift_result);
result = result.plus(shift_result);
}
}
return result;
@ -175,7 +175,7 @@ UnsignedBigInteger UnsignedBigInteger::multiply(const UnsignedBigInteger& other)
* so we set the ith bit in the quotient and reduce divisor<<i from the dividend.
* When we're done, what's left from the dividend is the remainder.
*/
UnsignedDivisionResult UnsignedBigInteger::divide(const UnsignedBigInteger& divisor) const
UnsignedDivisionResult UnsignedBigInteger::divided_by(const UnsignedBigInteger& divisor) const
{
UnsignedBigInteger leftover_dividend(*this);
UnsignedBigInteger quotient;
@ -187,7 +187,7 @@ UnsignedDivisionResult UnsignedBigInteger::divide(const UnsignedBigInteger& divi
const size_t shift_amount = word_index * UnsignedBigInteger::BITS_IN_WORD + bit_index;
UnsignedBigInteger divisor_shifted = divisor.shift_left(shift_amount);
UnsignedBigInteger temp_subtraction_result = leftover_dividend.sub(divisor_shifted);
UnsignedBigInteger temp_subtraction_result = leftover_dividend.minus(divisor_shifted);
if (!temp_subtraction_result.is_invalid()) {
leftover_dividend = temp_subtraction_result;
quotient.set_bit_inplace(shift_amount);
@ -231,7 +231,7 @@ UnsignedBigInteger UnsignedBigInteger::shift_left(size_t num_bits) const
// Shifting the last word can produce a carry
u32 carry_word = temp_result.shift_left_get_one_word(num_bits, temp_result.length());
if (carry_word != 0) {
result = result.add(UnsignedBigInteger(carry_word).shift_left_by_n_words(temp_result.length()));
result = result.plus(UnsignedBigInteger(carry_word).shift_left_by_n_words(temp_result.length()));
}
return result;
}
@ -333,7 +333,7 @@ UnsignedBigInteger UnsignedBigInteger::import_data(const u8* ptr, size_t length)
for (size_t i = 0; i < length; ++i) {
auto part = UnsignedBigInteger { ptr[length - i - 1] }.shift_left(8 * i);
integer = integer.add(part);
integer = integer.plus(part);
}
return integer;
@ -349,7 +349,7 @@ size_t UnsignedBigInteger::export_data(AK::ByteBuffer& data)
if (copy.length() == 0)
break;
data[size - i - 1] = copy.m_words[0] & 0xff;
copy = copy.divide(256).quotient;
copy = copy.divided_by(256).quotient;
}
return i;
}

View file

@ -62,12 +62,12 @@ public:
const AK::Vector<u32, STARTING_WORD_SIZE>& words() const { return m_words; }
UnsignedBigInteger add(const UnsignedBigInteger& other) const;
UnsignedBigInteger sub(const UnsignedBigInteger& other) const;
UnsignedBigInteger multiply(const UnsignedBigInteger& other) const;
UnsignedBigInteger plus(const UnsignedBigInteger& other) const;
UnsignedBigInteger minus(const UnsignedBigInteger& other) const;
UnsignedBigInteger multiplied_by(const UnsignedBigInteger& other) const;
UnsignedBigInteger shift_left(size_t num_bits) const;
UnsignedDivisionResult divide(const UnsignedBigInteger& divisor) const;
UnsignedDivisionResult divided_by(const UnsignedBigInteger& divisor) const;
void set_bit_inplace(size_t bit_index);

View file

@ -41,35 +41,35 @@ static auto ModularInverse(const UnsignedBigInteger& a_, const UnsignedBigIntege
auto a = a_;
auto u = a;
if (a.words()[0] % 2 == 0)
u = u.add(b);
u = u.plus(b);
auto v = b;
auto x = UnsignedBigInteger { 0 };
auto d = b.sub(1);
UnsignedBigInteger x { 0 };
auto d = b.minus(1);
while (!(v == 1)) {
while (v < u) {
u = u.sub(v);
d = d.add(x);
u = u.minus(v);
d = d.plus(x);
while (u.words()[0] % 2 == 0) {
if (d.words()[0] % 2 == 1) {
d = d.add(b);
d = d.plus(b);
}
u = u.divide(2).quotient;
d = d.divide(2).quotient;
u = u.divided_by(2).quotient;
d = d.divided_by(2).quotient;
}
}
v = v.sub(u);
x = x.add(d);
v = v.minus(u);
x = x.plus(d);
while (v.words()[0] % 2 == 0) {
if (x.words()[0] % 2 == 1) {
x = x.add(b);
x = x.plus(b);
}
v = v.divide(2).quotient;
x = x.divide(2).quotient;
v = v.divided_by(2).quotient;
x = x.divided_by(2).quotient;
}
}
return x.divide(b).remainder;
return x.divided_by(b).remainder;
}
static auto ModularPower(const UnsignedBigInteger& b, const UnsignedBigInteger& e, const UnsignedBigInteger& m) -> UnsignedBigInteger
@ -86,10 +86,10 @@ static auto ModularPower(const UnsignedBigInteger& b, const UnsignedBigInteger&
dbg() << ep.to_base10();
#endif
if (ep.words()[0] % 2 == 1) {
exp = exp.multiply(base).divide(m).remainder;
exp = exp.multiplied_by(base).divided_by(m).remainder;
}
ep = ep.divide(2).quotient;
base = base.multiply(base).divide(m).remainder;
ep = ep.divided_by(2).quotient;
base = base.multiplied_by(base).divided_by(m).remainder;
}
return exp;
}
@ -100,10 +100,10 @@ static auto GCD(const UnsignedBigInteger& a, const UnsignedBigInteger& b) -> Uns
for (;;) {
if (a_ == 0)
return b_;
b_ = b_.divide(a_).remainder;
b_ = b_.divided_by(a_).remainder;
if (b_ == 0)
return a_;
a_ = a_.divide(b_).remainder;
a_ = a_.divided_by(b_).remainder;
}
}
@ -111,24 +111,24 @@ static auto LCM(const UnsignedBigInteger& a, const UnsignedBigInteger& b) -> Uns
{
auto temp = GCD(a, b);
auto div = a.divide(temp);
auto div = a.divided_by(temp);
#ifdef NT_DEBUG
dbg() << "quot: " << div.quotient << " rem: " << div.remainder;
#endif
return temp == 0 ? 0 : (a.divide(temp).quotient.multiply(b));
return temp == 0 ? 0 : (a.divided_by(temp).quotient.multiplied_by(b));
}
template<size_t test_count>
static bool MR_primality_test(UnsignedBigInteger n, const Vector<UnsignedBigInteger, test_count>& tests)
{
auto prev = n.sub({ 1 });
auto prev = n.minus({ 1 });
auto b = prev;
auto r = 0;
auto div_result = b.divide(2);
auto div_result = b.divided_by(2);
while (div_result.quotient == 0) {
div_result = b.divide(2);
div_result = b.divided_by(2);
b = div_result.quotient;
++r;
}
@ -170,7 +170,7 @@ static UnsignedBigInteger random_number(const UnsignedBigInteger& min, const Uns
vec.append(*(u32*)buf + i);
}
UnsignedBigInteger offset { move(vec) };
return offset.add(min);
return offset.plus(min);
}
static bool is_probably_prime(const UnsignedBigInteger& p)
@ -183,7 +183,7 @@ static bool is_probably_prime(const UnsignedBigInteger& p)
Vector<UnsignedBigInteger, 256> tests;
UnsignedBigInteger seven { 7 };
for (size_t i = 0; i < tests.size(); ++i)
tests.append(random_number(seven, p.sub(2)));
tests.append(random_number(seven, p.minus(2)));
return MR_primality_test(p, tests);
}
@ -192,7 +192,7 @@ static UnsignedBigInteger random_big_prime(size_t bits)
{
ASSERT(bits >= 33);
UnsignedBigInteger min = UnsignedBigInteger::from_base10("6074001000").shift_left(bits - 33);
UnsignedBigInteger max = UnsignedBigInteger { 1 }.shift_left(bits).sub(1);
UnsignedBigInteger max = UnsignedBigInteger { 1 }.shift_left(bits).minus(1);
for (;;) {
auto p = random_number(min, max);
if (is_probably_prime(p))

View file

@ -129,11 +129,11 @@ public:
do {
p = NumberTheory::random_big_prime(bits / 2);
q = NumberTheory::random_big_prime(bits / 2);
lambda = NumberTheory::LCM(p.sub(1), q.sub(1));
lambda = NumberTheory::LCM(p.minus(1), q.minus(1));
dbg() << "checking combination p=" << p << ", q=" << q << ", lambda=" << lambda.length();
} while (!(NumberTheory::GCD(e, lambda) == 1));
auto n = p.multiply(q);
auto n = p.multiplied_by(q);
auto d = NumberTheory::ModularInverse(e, lambda);
dbg() << "Your keys are Pub{n=" << n << ", e=" << e << "} and Priv{n=" << n << ", d=" << d << "}";