From adab43987dc04cb28002bfb24b89a01eaa4e4500 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Mon, 27 Apr 2020 19:05:17 +0430 Subject: [PATCH] LibCrypto: Rename UnsignedBigInteger APIs to match their actions --- .../LibCrypto/BigInt/UnsignedBigInteger.cpp | 22 ++++---- .../LibCrypto/BigInt/UnsignedBigInteger.h | 8 +-- .../LibCrypto/NumberTheory/ModularFunctions.h | 54 +++++++++---------- Libraries/LibCrypto/PK/RSA.h | 4 +- Userland/test-crypto.cpp | 36 ++++++------- 5 files changed, 62 insertions(+), 62 deletions(-) diff --git a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp index 5ba55c4049d..88de7d4ddc9 100644 --- a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp +++ b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp @@ -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(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<& 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); diff --git a/Libraries/LibCrypto/NumberTheory/ModularFunctions.h b/Libraries/LibCrypto/NumberTheory/ModularFunctions.h index 59d2122eb0c..9c423dd8188 100644 --- a/Libraries/LibCrypto/NumberTheory/ModularFunctions.h +++ b/Libraries/LibCrypto/NumberTheory/ModularFunctions.h @@ -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 static bool MR_primality_test(UnsignedBigInteger n, const Vector& 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 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)) diff --git a/Libraries/LibCrypto/PK/RSA.h b/Libraries/LibCrypto/PK/RSA.h index b0fcdba1606..4c50f36e415 100644 --- a/Libraries/LibCrypto/PK/RSA.h +++ b/Libraries/LibCrypto/PK/RSA.h @@ -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 << "}"; diff --git a/Userland/test-crypto.cpp b/Userland/test-crypto.cpp index 59686c24cde..4f6629404b0 100644 --- a/Userland/test-crypto.cpp +++ b/Userland/test-crypto.cpp @@ -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 { 4294967289, 0, 1 }) { + if (num1.plus(num2).words() == Vector { 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 { 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 { 1483061863, 446680044, 1123294122, 191895498, 3347106536, 16, 0, 0, 0 }); Crypto::UnsignedBigInteger num2(Vector { 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 { 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 0 }) { + if (num.minus(1).words() == Vector { 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 { 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 { 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 { 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 { 2300984486, 2637503534, 2022805584, 107 }), Crypto::UnsignedBigInteger(Vector { 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);