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
Notes: sideshowbarker 2024-07-19 07:03:59 +09:00
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 << "}";

View File

@ -1190,7 +1190,7 @@ Crypto::UnsignedBigInteger bigint_fibonacci(size_t n)
Crypto::UnsignedBigInteger num1(0);
Crypto::UnsignedBigInteger num2(1);
for (size_t i = 0; i < n; ++i) {
Crypto::UnsignedBigInteger t = num1.add(num2);
Crypto::UnsignedBigInteger t = num1.plus(num2);
num2 = num1;
num1 = t;
}
@ -1216,7 +1216,7 @@ void bigint_addition_edgecases()
I_TEST((BigInteger | Edge Cases));
Crypto::UnsignedBigInteger num1;
Crypto::UnsignedBigInteger num2(70);
Crypto::UnsignedBigInteger num3 = num1.add(num2);
Crypto::UnsignedBigInteger num3 = num1.plus(num2);
bool pass = (num3 == num2);
pass &= (num1 == Crypto::UnsignedBigInteger(0));
@ -1230,7 +1230,7 @@ void bigint_addition_edgecases()
I_TEST((BigInteger | Borrow with zero));
Crypto::UnsignedBigInteger num1({ UINT32_MAX - 3, UINT32_MAX });
Crypto::UnsignedBigInteger num2({ UINT32_MAX - 2, 0 });
if (num1.add(num2).words() == Vector<u32> { 4294967289, 0, 1 }) {
if (num1.plus(num2).words() == Vector<u32> { 4294967289, 0, 1 }) {
PASS;
} else {
FAIL(Incorrect Result);
@ -1245,7 +1245,7 @@ void bigint_subtraction()
Crypto::UnsignedBigInteger num1(80);
Crypto::UnsignedBigInteger num2(70);
if (num1.sub(num2) == Crypto::UnsignedBigInteger(10)) {
if (num1.minus(num2) == Crypto::UnsignedBigInteger(10)) {
PASS;
} else {
FAIL(Incorrect Result);
@ -1256,7 +1256,7 @@ void bigint_subtraction()
Crypto::UnsignedBigInteger num1(50);
Crypto::UnsignedBigInteger num2(70);
if (num1.sub(num2).is_invalid()) {
if (num1.minus(num2).is_invalid()) {
PASS;
} else {
FAIL(Incorrect Result);
@ -1266,8 +1266,8 @@ void bigint_subtraction()
I_TEST((BigInteger | Subtraction with borrow));
Crypto::UnsignedBigInteger num1(UINT32_MAX);
Crypto::UnsignedBigInteger num2(1);
Crypto::UnsignedBigInteger num3 = num1.add(num2);
Crypto::UnsignedBigInteger result = num3.sub(num2);
Crypto::UnsignedBigInteger num3 = num1.plus(num2);
Crypto::UnsignedBigInteger result = num3.minus(num2);
if (result == num1) {
PASS;
} else {
@ -1278,8 +1278,8 @@ void bigint_subtraction()
I_TEST((BigInteger | Subtraction with large numbers));
Crypto::UnsignedBigInteger num1 = bigint_fibonacci(343);
Crypto::UnsignedBigInteger num2 = bigint_fibonacci(218);
Crypto::UnsignedBigInteger result = num1.sub(num2);
if ((result.add(num2) == num1)
Crypto::UnsignedBigInteger result = num1.minus(num2);
if ((result.plus(num2) == num1)
&& (result.words() == Vector<u32> { 811430588, 2958904896, 1130908877, 2830569969, 3243275482, 3047460725, 774025231, 7990 })) {
PASS;
} else {
@ -1290,14 +1290,14 @@ void bigint_subtraction()
I_TEST((BigInteger | Subtraction with large numbers 2));
Crypto::UnsignedBigInteger num1(Vector<u32> { 1483061863, 446680044, 1123294122, 191895498, 3347106536, 16, 0, 0, 0 });
Crypto::UnsignedBigInteger num2(Vector<u32> { 4196414175, 1117247942, 1123294122, 191895498, 3347106536, 16 });
Crypto::UnsignedBigInteger result = num1.sub(num2);
Crypto::UnsignedBigInteger result = num1.minus(num2);
// this test only verifies that we don't crash on an assertion
PASS;
}
{
I_TEST((BigInteger | Subtraction Regerssion 1));
auto num = Crypto::UnsignedBigInteger { 1 }.shift_left(256);
if (num.sub(1).words() == Vector<u32> { 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 0 }) {
if (num.minus(1).words() == Vector<u32> { 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 0 }) {
PASS;
} else {
FAIL(Incorrect Result);
@ -1311,7 +1311,7 @@ void bigint_multiplication()
I_TEST((BigInteger | Simple Multipliction));
Crypto::UnsignedBigInteger num1(8);
Crypto::UnsignedBigInteger num2(251);
Crypto::UnsignedBigInteger result = num1.multiply(num2);
Crypto::UnsignedBigInteger result = num1.multiplied_by(num2);
if (result.words() == Vector<u32> { 2008 }) {
PASS;
} else {
@ -1322,7 +1322,7 @@ void bigint_multiplication()
I_TEST((BigInteger | Multiplications with big numbers 1));
Crypto::UnsignedBigInteger num1 = bigint_fibonacci(200);
Crypto::UnsignedBigInteger num2(12345678);
Crypto::UnsignedBigInteger result = num1.multiply(num2);
Crypto::UnsignedBigInteger result = num1.multiplied_by(num2);
if (result.words() == Vector<u32> { 669961318, 143970113, 4028714974, 3164551305, 1589380278, 2 }) {
PASS;
} else {
@ -1333,7 +1333,7 @@ void bigint_multiplication()
I_TEST((BigInteger | Multiplications with big numbers 2));
Crypto::UnsignedBigInteger num1 = bigint_fibonacci(200);
Crypto::UnsignedBigInteger num2 = bigint_fibonacci(341);
Crypto::UnsignedBigInteger result = num1.multiply(num2);
Crypto::UnsignedBigInteger result = num1.multiplied_by(num2);
if (result.words() == Vector<u32> { 3017415433, 2741793511, 1957755698, 3731653885, 3154681877, 785762127, 3200178098, 4260616581, 529754471, 3632684436, 1073347813, 2516430 }) {
PASS;
} else {
@ -1347,7 +1347,7 @@ void bigint_division()
I_TEST((BigInteger | Simple Division));
Crypto::UnsignedBigInteger num1(27194);
Crypto::UnsignedBigInteger num2(251);
auto result = num1.divide(num2);
auto result = num1.divided_by(num2);
Crypto::UnsignedDivisionResult expected = { Crypto::UnsignedBigInteger(108), Crypto::UnsignedBigInteger(86) };
if (result.quotient == expected.quotient && result.remainder == expected.remainder) {
PASS;
@ -1359,7 +1359,7 @@ void bigint_division()
I_TEST((BigInteger | Division with big numbers));
Crypto::UnsignedBigInteger num1 = bigint_fibonacci(386);
Crypto::UnsignedBigInteger num2 = bigint_fibonacci(238);
auto result = num1.divide(num2);
auto result = num1.divided_by(num2);
Crypto::UnsignedDivisionResult expected = {
Crypto::UnsignedBigInteger(Vector<u32> { 2300984486, 2637503534, 2022805584, 107 }),
Crypto::UnsignedBigInteger(Vector<u32> { 1483061863, 446680044, 1123294122, 191895498, 3347106536, 16, 0, 0, 0 })
@ -1374,8 +1374,8 @@ void bigint_division()
I_TEST((BigInteger | Combined test));
auto num1 = bigint_fibonacci(497);
auto num2 = bigint_fibonacci(238);
auto div_result = num1.divide(num2);
if (div_result.quotient.multiply(num2).add(div_result.remainder) == num1) {
auto div_result = num1.divided_by(num2);
if (div_result.quotient.multiplied_by(num2).plus(div_result.remainder) == num1) {
PASS;
} else {
FAIL(Incorrect Result);