From ee5955bdeabe49da548be8cbe6d79c3ac67677e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=B6hm?= <134922046+LordofGhost@users.noreply.github.com> Date: Sat, 11 Jul 2026 11:54:49 +0200 Subject: [PATCH 1/2] Update BigInt submodule --- lib/BigInt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/BigInt b/lib/BigInt index d3f446e..6b19bc4 160000 --- a/lib/BigInt +++ b/lib/BigInt @@ -1 +1 @@ -Subproject commit d3f446eb6237a0891f17eaebc939bf419ea42a7c +Subproject commit 6b19bc461e654b32b0741b84360f1347964a1976 From 3ea7fa2ec147f2cf999d59a6eb2583fe02e93ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=B6hm?= <134922046+LordofGhost@users.noreply.github.com> Date: Sat, 11 Jul 2026 12:03:26 +0200 Subject: [PATCH 2/2] Update references to BigInt --- CMakeLists.txt | 4 ++-- docs/algorithms/div.md | 2 +- src/decrypt.cpp | 6 +++--- src/encrypt.cpp | 6 +++--- src/helper.h | 4 ++-- src/keyPair.cpp | 42 +++++++++++++++++++++--------------------- src/keyPair.h | 20 ++++++++++---------- 7 files changed, 42 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cb6ef34..3122c12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,5 +11,5 @@ add_subdirectory(src) include(CTest) add_subdirectory(tests) -target_link_libraries(RSA PUBLIC Base256) -target_include_directories(RSA PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file +target_link_libraries(RSA PUBLIC BigInt) +target_include_directories(RSA PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/docs/algorithms/div.md b/docs/algorithms/div.md index cc18030..988b57e 100644 --- a/docs/algorithms/div.md +++ b/docs/algorithms/div.md @@ -102,7 +102,7 @@ When the loop finishes processing the final bit (`dividendIndex < 0`), whatever *(Note: Because of this architecture, evaluating `A % B` requires the exact same computational effort as `A / B`. Therefore, if both the quotient and remainder are needed, they are extracted simultaneously to halve CPU cycles).* ### 5. Final Normalization -Even though pre-allocation is tightly bound to the `initialDividendIndex`, the final quotient might have leading zeros depending on the magnitude of the divisor. The `div` function concludes by stripping any trailing zero-bytes from the little-endian vector to maintain strict `Base256` normalization guarantees. +Even though pre-allocation is tightly bound to the `initialDividendIndex`, the final quotient might have leading zeros depending on the magnitude of the divisor. The `div` function concludes by stripping any trailing zero-bytes from the little-endian vector to maintain strict `BigInt` normalization guarantees. --- diff --git a/src/decrypt.cpp b/src/decrypt.cpp index be9503d..b6214ca 100644 --- a/src/decrypt.cpp +++ b/src/decrypt.cpp @@ -25,10 +25,10 @@ std::string decrypt(keyPair& keyPair, const std::vector& ciphertext) { std::vector chunk(ciphertext.begin() + i, ciphertext.begin() + i + blockSize); // 2. Construct representation from the extracted block chunk (using 64-bit limbs) - const operations::Base256 c_num(bytesToByteArray(chunk)); + const operations::BigInt c_num(bytesToByteArray(chunk)); // 3. Perform RSA mathematical operation: M = C^d mod n - operations::Base256 m_num = + operations::BigInt m_num = modPow(c_num, keyPair.getPrivateKey().d, keyPair.getPrivateKey().n); // 4. Retrieve the decrypted byte value and convert it back to a character @@ -42,4 +42,4 @@ std::string decrypt(keyPair& keyPair, const std::vector& ciphertext) { return plaintext; } -} // namespace core::decryptor \ No newline at end of file +} // namespace core::decryptor diff --git a/src/encrypt.cpp b/src/encrypt.cpp index 8813c29..6d56682 100644 --- a/src/encrypt.cpp +++ b/src/encrypt.cpp @@ -16,10 +16,10 @@ std::vector encrypt(keyPair& keyPair, const std::string& plaintext) { for (const char c : plaintext) { // 1. Convert the character byte to the internal 64-bit limb representation - const operations::Base256 m(static_cast(c)); + const operations::BigInt m(static_cast(c)); // 2. Perform RSA mathematical operation: C = M^e mod n - operations::Base256 c_num = modPow(m, keyPair.getPublicKey().e, keyPair.getPublicKey().n); + operations::BigInt c_num = modPow(m, keyPair.getPublicKey().e, keyPair.getPublicKey().n); // 3. Extract raw bytes from the computed ciphertext number (padded to target block size) std::vector c_bytes = byteArrayToBytes(c_num.getBytes(), blockSize); @@ -30,4 +30,4 @@ std::vector encrypt(keyPair& keyPair, const std::string& plaintext) { return ciphertext; } -} // namespace core::encryptor \ No newline at end of file +} // namespace core::encryptor diff --git a/src/helper.h b/src/helper.h index 27fabb8..3ff7864 100644 --- a/src/helper.h +++ b/src/helper.h @@ -4,7 +4,7 @@ #include #include -#include "base256.h" +#include "bigint.h" #include "key_fwd.h" // Converts a little-endian byte array to a 64-bit word array @@ -52,4 +52,4 @@ } } return bytes; -} \ No newline at end of file +} diff --git a/src/keyPair.cpp b/src/keyPair.cpp index 33bb2c7..88c9cc4 100644 --- a/src/keyPair.cpp +++ b/src/keyPair.cpp @@ -65,14 +65,14 @@ std::vector generateCandidateBytes() { } // Generates a cryptographically secure 2048-bit prime number -operations::Base256 generateSecurePrime() { +operations::BigInt generateSecurePrime() { std::vector candidateBytes = generateCandidateBytes(); - // Convert 256 byte vector to Base256 representation using 64-bit limbs - operations::Base256 candidate(bytesToByteArray(candidateBytes)); + // Convert 256 byte vector to BigInt representation using 64-bit limbs + operations::BigInt candidate(bytesToByteArray(candidateBytes)); // Search sequentially for the next prime using the math_utils library while (!operations::math::isPrime(candidate)) { - candidate += operations::Base256(2); + candidate += operations::BigInt(2); } return candidate; } @@ -80,28 +80,28 @@ operations::Base256 generateSecurePrime() { // Default Constructor: Generates a new secure 4096-bit RSA keypair keyPair::keyPair() { - const operations::Base256 p = generateSecurePrime(); - operations::Base256 q = generateSecurePrime(); + const operations::BigInt p = generateSecurePrime(); + operations::BigInt q = generateSecurePrime(); // Ensure p and q are not identical while (p == q) { q = generateSecurePrime(); } - operations::Base256 phi = (p - operations::Base256(1)) * (q - operations::Base256(1)); - const operations::Base256 e(65537); + operations::BigInt phi = (p - operations::BigInt(1)) * (q - operations::BigInt(1)); + const operations::BigInt e(65537); // Ensure e and phi are coprime - while (operations::math::gcd(e, phi) != operations::Base256(1)) { + while (operations::math::gcd(e, phi) != operations::BigInt(1)) { q = generateSecurePrime(); while (p == q) { q = generateSecurePrime(); } - phi = (p - operations::Base256(1)) * (q - operations::Base256(1)); + phi = (p - operations::BigInt(1)) * (q - operations::BigInt(1)); } - const operations::Base256 n = p * q; - const operations::Base256 d = operations::math::modInverse(e, phi); + const operations::BigInt n = p * q; + const operations::BigInt d = operations::math::modInverse(e, phi); public_key.n = n; public_key.e = e; @@ -127,9 +127,9 @@ std::vector PublicKey::serialize() const { return keyPair::s_serialize( std::vector PrivateKey::serialize() const { return keyPair::s_serialize(n, d); } -// Serializes two Base256 fields with Big-endian size headers -std::vector keyPair::s_serialize(const operations::Base256 &first, - const operations::Base256 &second) { +// Serializes two BigInt fields with Big-endian size headers +std::vector keyPair::s_serialize(const operations::BigInt &first, + const operations::BigInt &second) { std::vector serialized; // Safely extract the raw byte stream from the 64-bit limb vectors @@ -156,9 +156,9 @@ std::vector keyPair::s_serialize(const operations::Base256 &first, return serialized; } -// Deserializes two Base256 fields with Big-endian size headers -bool keyPair::s_deserialize(const std::vector &data, operations::Base256 &outFirst, - operations::Base256 &outSecond) { +// Deserializes two BigInt fields with Big-endian size headers +bool keyPair::s_deserialize(const std::vector &data, operations::BigInt &outFirst, + operations::BigInt &outSecond) { if (data.size() < 8) return false; size_t index = 0; @@ -189,8 +189,8 @@ bool keyPair::s_deserialize(const std::vector &data, operations::Base25 std::vector secondBytes(secondBegin, secondEnd); // Convert deserialized byte streams back into 64-bit limb vectors - outFirst = operations::Base256(bytesToByteArray(firstBytes)); - outSecond = operations::Base256(bytesToByteArray(secondBytes)); + outFirst = operations::BigInt(bytesToByteArray(firstBytes)); + outSecond = operations::BigInt(bytesToByteArray(secondBytes)); return true; } @@ -274,4 +274,4 @@ uint8_t keyPair::getBase64Index(char letter) { } } return 0; -} \ No newline at end of file +} diff --git a/src/keyPair.h b/src/keyPair.h index 961e110..0b69a6f 100644 --- a/src/keyPair.h +++ b/src/keyPair.h @@ -6,7 +6,7 @@ #include #include -#include "base256.h" +#include "bigint.h" #include "key_fwd.h" #define KEY_FOLDER "rsa-keys" @@ -14,15 +14,15 @@ enum { NONE, PUBLIC, PRIVATE, BOTH }; struct PublicKey { - operations::Base256 n; - operations::Base256 e; + operations::BigInt n; + operations::BigInt e; [[nodiscard]] std::vector serialize() const; }; struct PrivateKey { - operations::Base256 n; - operations::Base256 d; + operations::BigInt n; + operations::BigInt d; [[nodiscard]] std::vector serialize() const; }; @@ -51,10 +51,10 @@ class keyPair { static keyPair create(const std::vector &pubData, const std::vector &privData); - static std::vector s_serialize(const operations::Base256 &first, - const operations::Base256 &second); - static bool s_deserialize(const std::vector &data, operations::Base256 &outFirst, - operations::Base256 &outSecond); + static std::vector s_serialize(const operations::BigInt &first, + const operations::BigInt &second); + static bool s_deserialize(const std::vector &data, operations::BigInt &outFirst, + operations::BigInt &outSecond); // Base64 helper functions static std::string base64Encode(const std::vector &data); @@ -62,4 +62,4 @@ class keyPair { static uint8_t getBase64Index(char letter); }; -#endif \ No newline at end of file +#endif