Loading src/crypto/rsa.cpp +60 −36 Changes for src/crypto/rsa.cpp: 60 added lines, 36 removed lines. Original line number Diff line number Diff line Loading @@ -646,7 +646,6 @@ namespace netplus { // exp == 0 => 1 mod mod if (exp.isZero()) { bigInt one(1U, 1); // one % mod bigInt q(one.capacity), r(mod.capacity); netplus::rsa::divide(one, mod, q, r); return r; Loading Loading @@ -679,34 +678,40 @@ namespace netplus { return rem; }; // Montgomery multiply: returns (a*b*R^{-1}) mod N auto montMul = [&](const bigInt& a, const bigInt& b) -> bigInt { return netplus::rsa::montgomeryMultiply(a, b, mod, n_prime); }; size_t n = mod.used; // Pre-allocate ALL scratch buffers once — no allocations inside the loop bigInt scratch(2 * n + 2); // for montgomeryMultiply_into product bigInt temp(n + 2); // for montgomeryMultiply_into output // Convert into Montgomery domain correctly: // a_bar = a * R mod N // Using montMul(a, R^2) = a*R^2*R^{-1} = a*R (mod N) // Convert into Montgomery domain bigInt a = base % mod; bigInt R2 = calculateR2Mod(mod); bigInt a_bar = montMul(a, R2); bigInt a_bar(n + 2); montgomeryMultiply_into(a, R2, mod, n_prime, scratch, a_bar); // res_bar = 1 * R mod N (Montgomery form of 1) // Either R mod N directly, or montMul(1, R2) bigInt one(1U, 1); bigInt res_bar = montMul(one, R2); bigInt res_bar(n + 2); montgomeryMultiply_into(one, R2, mod, n_prime, scratch, res_bar); // Exponentiation (left-to-right square-and-multiply) // Exponentiation (left-to-right square-and-multiply) — zero allocations for (size_t i = exp.bitLength(); i-- > 0; ) { res_bar = montMul(res_bar, res_bar); // res_bar = montMul(res_bar, res_bar) — square montgomeryMultiply_into(res_bar, res_bar, mod, n_prime, scratch, temp); std::swap(res_bar, temp); if (exp.isBitSet(i)) { res_bar = montMul(res_bar, a_bar); // res_bar = montMul(res_bar, a_bar) — multiply montgomeryMultiply_into(res_bar, a_bar, mod, n_prime, scratch, temp); std::swap(res_bar, temp); } } // Convert out of Montgomery domain: montMul(res_bar, 1) = res_bar*1*R^{-1} = result return montMul(res_bar, one); // Convert out of Montgomery domain: montMul(res_bar, 1) = result montgomeryMultiply_into(res_bar, one, mod, n_prime, scratch, temp); return temp; } void rsa::divide(const bigInt& dividend, const bigInt& divisor, bigInt& quotient, bigInt& remainder) { Loading Loading @@ -1069,46 +1074,65 @@ namespace netplus { rsa::bigInt rsa::montgomeryMultiply(const bigInt& a, const bigInt& b, const bigInt& mod, uint32_t n_prime) { size_t n = mod.used; bigInt res(2 * n + 1); bigInt final_res(n + 1); montgomeryMultiply_into(a, b, mod, n_prime, res, final_res); return final_res; } // In-place Montgomery multiply: writes result into |out|, uses |scratch| as temp. // Both scratch and out must be pre-allocated with sufficient capacity. void rsa::montgomeryMultiply_into(const bigInt& a, const bigInt& b, const bigInt& mod, uint32_t n_prime, bigInt& scratch, bigInt& out) { size_t n = mod.used; // Ensure scratch has enough room for the full product + reduction if (scratch.capacity < 2 * n + 1) scratch.reserve(2 * n + 1); std::fill(scratch.data.get(), scratch.data.get() + scratch.capacity, 0); scratch.used = 1; // Perform standard schoolbook multiplication (res = a * b) multiply(a, b, res); res.reserve(2 * n + 1); // Perform standard schoolbook multiplication (scratch = a * b) multiply(a, b, scratch); if (scratch.capacity < 2 * n + 1) scratch.reserve(2 * n + 1); // Montgomery Reduction for (size_t i = 0; i < n; i++) { uint32_t m = res.data[i] * n_prime; uint32_t m = scratch.data[i] * n_prime; uint64_t carry = 0; for (size_t j = 0; j < n; j++) { uint64_t cur = (uint64_t)res.data[i + j] + (uint64_t)m * mod.data[j] + carry; res.data[i + j] = (uint32_t)cur; uint64_t cur = (uint64_t)scratch.data[i + j] + (uint64_t)m * mod.data[j] + carry; scratch.data[i + j] = (uint32_t)cur; carry = cur >> 32; } // Propagate the carry size_t k = i + n; while (carry > 0) { uint64_t cur = (uint64_t)res.data[k] + carry; res.data[k] = (uint32_t)cur; uint64_t cur = (uint64_t)scratch.data[k] + carry; scratch.data[k] = (uint32_t)cur; carry = cur >> 32; k++; } } // Shift right by N words (divide by R) bigInt final_res(n + 1); // Shift right by N words (divide by R) — write into out if (out.capacity < n + 1) out.reserve(n + 1); for (size_t i = 0; i < n + 1; i++) { final_res.data[i] = res.data[i + n]; out.data[i] = scratch.data[i + n]; } final_res.used = n + 1; while (final_res.used > 1 && final_res.data[final_res.used - 1] == 0) final_res.used--; out.used = n + 1; while (out.used > 1 && out.data[out.used - 1] == 0) out.used--; // Final step: if res >= mod, subtract mod if (compare(final_res, mod) >= 0) { bigInt actual_final(mod.capacity); subtract(final_res, mod, actual_final); return actual_final; // Final step: if out >= mod, subtract mod in-place if (compare(out, mod) >= 0) { // We need a temp for subtract; reuse scratch subtract(out, mod, scratch); // Copy back out.used = scratch.used; std::copy(scratch.data.get(), scratch.data.get() + scratch.used, out.data.get()); if (out.capacity > out.used) std::fill(out.data.get() + out.used, out.data.get() + out.capacity, 0); } return final_res; } rsa::bigInt rsa::bytesToBigIntBE(const std::vector<uint8_t>& bytes) { Loading src/crypto/rsa.h +3 −0 Changes for src/crypto/rsa.h: 3 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -137,6 +137,9 @@ namespace netplus { static bigInt calculateRMod(const bigInt& mod); static bigInt calculateR2Mod(const bigInt& mod); static bigInt montgomeryMultiply(const bigInt& a, const bigInt& b, const bigInt& mod, uint32_t n_prime); static void montgomeryMultiply_into(const bigInt& a, const bigInt& b, const bigInt& mod, uint32_t n_prime, bigInt& scratch, bigInt& out); friend class x509cert; friend class tls; friend class rsa_pss_sha256; Loading Loading
src/crypto/rsa.cpp +60 −36 Changes for src/crypto/rsa.cpp: 60 added lines, 36 removed lines. Original line number Diff line number Diff line Loading @@ -646,7 +646,6 @@ namespace netplus { // exp == 0 => 1 mod mod if (exp.isZero()) { bigInt one(1U, 1); // one % mod bigInt q(one.capacity), r(mod.capacity); netplus::rsa::divide(one, mod, q, r); return r; Loading Loading @@ -679,34 +678,40 @@ namespace netplus { return rem; }; // Montgomery multiply: returns (a*b*R^{-1}) mod N auto montMul = [&](const bigInt& a, const bigInt& b) -> bigInt { return netplus::rsa::montgomeryMultiply(a, b, mod, n_prime); }; size_t n = mod.used; // Pre-allocate ALL scratch buffers once — no allocations inside the loop bigInt scratch(2 * n + 2); // for montgomeryMultiply_into product bigInt temp(n + 2); // for montgomeryMultiply_into output // Convert into Montgomery domain correctly: // a_bar = a * R mod N // Using montMul(a, R^2) = a*R^2*R^{-1} = a*R (mod N) // Convert into Montgomery domain bigInt a = base % mod; bigInt R2 = calculateR2Mod(mod); bigInt a_bar = montMul(a, R2); bigInt a_bar(n + 2); montgomeryMultiply_into(a, R2, mod, n_prime, scratch, a_bar); // res_bar = 1 * R mod N (Montgomery form of 1) // Either R mod N directly, or montMul(1, R2) bigInt one(1U, 1); bigInt res_bar = montMul(one, R2); bigInt res_bar(n + 2); montgomeryMultiply_into(one, R2, mod, n_prime, scratch, res_bar); // Exponentiation (left-to-right square-and-multiply) // Exponentiation (left-to-right square-and-multiply) — zero allocations for (size_t i = exp.bitLength(); i-- > 0; ) { res_bar = montMul(res_bar, res_bar); // res_bar = montMul(res_bar, res_bar) — square montgomeryMultiply_into(res_bar, res_bar, mod, n_prime, scratch, temp); std::swap(res_bar, temp); if (exp.isBitSet(i)) { res_bar = montMul(res_bar, a_bar); // res_bar = montMul(res_bar, a_bar) — multiply montgomeryMultiply_into(res_bar, a_bar, mod, n_prime, scratch, temp); std::swap(res_bar, temp); } } // Convert out of Montgomery domain: montMul(res_bar, 1) = res_bar*1*R^{-1} = result return montMul(res_bar, one); // Convert out of Montgomery domain: montMul(res_bar, 1) = result montgomeryMultiply_into(res_bar, one, mod, n_prime, scratch, temp); return temp; } void rsa::divide(const bigInt& dividend, const bigInt& divisor, bigInt& quotient, bigInt& remainder) { Loading Loading @@ -1069,46 +1074,65 @@ namespace netplus { rsa::bigInt rsa::montgomeryMultiply(const bigInt& a, const bigInt& b, const bigInt& mod, uint32_t n_prime) { size_t n = mod.used; bigInt res(2 * n + 1); bigInt final_res(n + 1); montgomeryMultiply_into(a, b, mod, n_prime, res, final_res); return final_res; } // In-place Montgomery multiply: writes result into |out|, uses |scratch| as temp. // Both scratch and out must be pre-allocated with sufficient capacity. void rsa::montgomeryMultiply_into(const bigInt& a, const bigInt& b, const bigInt& mod, uint32_t n_prime, bigInt& scratch, bigInt& out) { size_t n = mod.used; // Ensure scratch has enough room for the full product + reduction if (scratch.capacity < 2 * n + 1) scratch.reserve(2 * n + 1); std::fill(scratch.data.get(), scratch.data.get() + scratch.capacity, 0); scratch.used = 1; // Perform standard schoolbook multiplication (res = a * b) multiply(a, b, res); res.reserve(2 * n + 1); // Perform standard schoolbook multiplication (scratch = a * b) multiply(a, b, scratch); if (scratch.capacity < 2 * n + 1) scratch.reserve(2 * n + 1); // Montgomery Reduction for (size_t i = 0; i < n; i++) { uint32_t m = res.data[i] * n_prime; uint32_t m = scratch.data[i] * n_prime; uint64_t carry = 0; for (size_t j = 0; j < n; j++) { uint64_t cur = (uint64_t)res.data[i + j] + (uint64_t)m * mod.data[j] + carry; res.data[i + j] = (uint32_t)cur; uint64_t cur = (uint64_t)scratch.data[i + j] + (uint64_t)m * mod.data[j] + carry; scratch.data[i + j] = (uint32_t)cur; carry = cur >> 32; } // Propagate the carry size_t k = i + n; while (carry > 0) { uint64_t cur = (uint64_t)res.data[k] + carry; res.data[k] = (uint32_t)cur; uint64_t cur = (uint64_t)scratch.data[k] + carry; scratch.data[k] = (uint32_t)cur; carry = cur >> 32; k++; } } // Shift right by N words (divide by R) bigInt final_res(n + 1); // Shift right by N words (divide by R) — write into out if (out.capacity < n + 1) out.reserve(n + 1); for (size_t i = 0; i < n + 1; i++) { final_res.data[i] = res.data[i + n]; out.data[i] = scratch.data[i + n]; } final_res.used = n + 1; while (final_res.used > 1 && final_res.data[final_res.used - 1] == 0) final_res.used--; out.used = n + 1; while (out.used > 1 && out.data[out.used - 1] == 0) out.used--; // Final step: if res >= mod, subtract mod if (compare(final_res, mod) >= 0) { bigInt actual_final(mod.capacity); subtract(final_res, mod, actual_final); return actual_final; // Final step: if out >= mod, subtract mod in-place if (compare(out, mod) >= 0) { // We need a temp for subtract; reuse scratch subtract(out, mod, scratch); // Copy back out.used = scratch.used; std::copy(scratch.data.get(), scratch.data.get() + scratch.used, out.data.get()); if (out.capacity > out.used) std::fill(out.data.get() + out.used, out.data.get() + out.capacity, 0); } return final_res; } rsa::bigInt rsa::bytesToBigIntBE(const std::vector<uint8_t>& bytes) { Loading
src/crypto/rsa.h +3 −0 Changes for src/crypto/rsa.h: 3 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -137,6 +137,9 @@ namespace netplus { static bigInt calculateRMod(const bigInt& mod); static bigInt calculateR2Mod(const bigInt& mod); static bigInt montgomeryMultiply(const bigInt& a, const bigInt& b, const bigInt& mod, uint32_t n_prime); static void montgomeryMultiply_into(const bigInt& a, const bigInt& b, const bigInt& mod, uint32_t n_prime, bigInt& scratch, bigInt& out); friend class x509cert; friend class tls; friend class rsa_pss_sha256; Loading