Commit 70cd5335 authored by jan.koester's avatar jan.koester
Browse files

perf

parent 30cf2a8f
Loading
Loading
Loading
Loading
+68 −51
Changes for src/crypto/rsa.cpp: 68 added lines, 51 removed lines.
Original line number Diff line number Diff line
@@ -536,54 +536,63 @@ namespace netplus {
        uint64_t carry = 0;
        size_t max_len = std::max(a.used, b.used);

        res.reserve(max_len + 1); // important
        res.reserve(max_len + 1);
        res.used = 0;

        const uint32_t* ap = a.data.get();
        const uint32_t* bp = b.data.get();
        uint32_t* rp = res.data.get();
        const size_t au = a.used;
        const size_t bu = b.used;

        for (size_t i = 0; i < max_len || carry; ++i) {
            uint64_t sum = carry
                + (i < a.used ? a.data[i] : 0)
                + (i < b.used ? b.data[i] : 0);
                + (i < au ? ap[i] : 0)
                + (i < bu ? bp[i] : 0);

            res.data[i] = (uint32_t)sum;
            rp[i] = (uint32_t)sum;
            carry = sum >> 32;
            res.used++;
        }

        if (res.used == 0) { res.used = 1; res.data[0] = 0; }
        if (res.used == 0) { res.used = 1; rp[0] = 0; }
    }


    void rsa::multiply(const bigInt& a, const bigInt& b, bigInt& res) {
        std::fill(res.data.get(), res.data.get() + res.capacity, 0);
        const uint32_t* __restrict__ ap = a.data.get();
        const uint32_t* __restrict__ bp = b.data.get();
        uint32_t* __restrict__ rp = res.data.get();
        const size_t au = a.used;
        const size_t bu = b.used;

        for (size_t i = 0; i < a.used; ++i) {
            uint64_t carry = 0;
            for (size_t j = 0; j < b.used; ++j) {
                uint64_t cur =
                    (uint64_t)res.data[i + j] +
                    (uint64_t)a.data[i] * (uint64_t)b.data[j] +
                    carry;
        // Zero only the output region we'll actually write
        std::memset(rp, 0, (au + bu) * sizeof(uint32_t));

                res.data[i + j] = (uint32_t)cur;
        for (size_t i = 0; i < au; ++i) {
            uint64_t carry = 0;
            const uint64_t ai = (uint64_t)ap[i];
            for (size_t j = 0; j < bu; ++j) {
                uint64_t cur = (uint64_t)rp[i + j] + ai * (uint64_t)bp[j] + carry;
                rp[i + j] = (uint32_t)cur;
                carry = cur >> 32;
            }

            // ADD carry to existing limb, then propagate if overflow
            size_t k = i + b.used;
            uint64_t cur = (uint64_t)res.data[k] + carry;
            res.data[k] = (uint32_t)cur;
            size_t k = i + bu;
            uint64_t cur = (uint64_t)rp[k] + carry;
            rp[k] = (uint32_t)cur;
            carry = cur >> 32;

            while (carry) {
                ++k;
                uint64_t cur2 = (uint64_t)res.data[k] + carry;
                res.data[k] = (uint32_t)cur2;
                uint64_t cur2 = (uint64_t)rp[k] + carry;
                rp[k] = (uint32_t)cur2;
                carry = cur2 >> 32;
            }
        }

        res.used = a.used + b.used;
        while (res.used > 1 && res.data[res.used - 1] == 0) res.used--;
        res.used = au + bu;
        while (res.used > 1 && rp[res.used - 1] == 0) res.used--;
    }


@@ -591,9 +600,11 @@ namespace netplus {
        if (a.used != b.used)
            return (a.used > b.used) ? 1 : -1;

        const uint32_t* ap = a.data.get();
        const uint32_t* bp = b.data.get();
        for (int i = (int)a.used - 1; i >= 0; --i) {
            if (a.data[i] != b.data[i])
                return (a.data[i] > b.data[i]) ? 1 : -1;
            if (ap[i] != bp[i])
                return (ap[i] > bp[i]) ? 1 : -1;
        }
        return 0;
    }
@@ -612,18 +623,23 @@ namespace netplus {
    void rsa::subtract(const bigInt& a, const bigInt& b, bigInt& res) {
        int64_t borrow = 0;
        res.reserve(a.used);
        for (size_t i = 0; i < a.used; ++i) {
            int64_t sub = (int64_t)a.data[i] - (i < b.used ? b.data[i] : 0) - borrow;
        const uint32_t* __restrict__ ap = a.data.get();
        const uint32_t* __restrict__ bp = b.data.get();
        uint32_t* __restrict__ rp = res.data.get();
        const size_t au = a.used;
        const size_t bu = b.used;
        for (size_t i = 0; i < au; ++i) {
            int64_t sub = (int64_t)ap[i] - (i < bu ? bp[i] : 0) - borrow;
            if (sub < 0) {
                sub += 0x100000000ULL;
                borrow = 1;
            } else {
                borrow = 0;
            }
            res.data[i] = (uint32_t)sub;
            rp[i] = (uint32_t)sub;
        }
        res.used = a.used;
        while (res.used > 1 && res.data[res.used - 1] == 0) res.used--;
        res.used = au;
        while (res.used > 1 && rp[res.used - 1] == 0) res.used--;
    }

    netplus::rsa::bigInt netplus::rsa::modPow(const netplus::rsa::bigInt& base,
@@ -1085,31 +1101,35 @@ namespace netplus {
    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;
        const size_t n = mod.used;
        const size_t need = 2 * n + 2;  // extra word for carry propagation

        // 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;
        // Ensure scratch has enough room
        if (scratch.capacity < need) scratch.reserve(need);

        // Perform standard schoolbook multiplication (scratch = a * b)
        // Perform schoolbook multiplication (scratch = a * b)
        multiply(a, b, scratch);
        if (scratch.capacity < 2 * n + 1) scratch.reserve(2 * n + 1);
        // Ensure any limbs beyond the product are zero for carry propagation
        if (scratch.capacity < need) scratch.reserve(need);
        for (size_t i = scratch.used; i < need; ++i)
            scratch.data[i] = 0;

        // Montgomery Reduction — use raw pointers
        uint32_t* __restrict__ sp = scratch.data.get();
        const uint32_t* __restrict__ mp = mod.data.get();

        // Montgomery Reduction
        for (size_t i = 0; i < n; i++) {
            uint32_t m = scratch.data[i] * n_prime;
            const uint64_t mi = (uint64_t)(sp[i] * n_prime);
            uint64_t carry = 0;
            for (size_t j = 0; j < n; j++) {
                uint64_t cur = (uint64_t)scratch.data[i + j] + (uint64_t)m * mod.data[j] + carry;
                scratch.data[i + j] = (uint32_t)cur;
                uint64_t cur = (uint64_t)sp[i + j] + mi * (uint64_t)mp[j] + carry;
                sp[i + j] = (uint32_t)cur;
                carry = cur >> 32;
            }
            // Propagate the carry
            size_t k = i + n;
            while (carry > 0) {
                uint64_t cur = (uint64_t)scratch.data[k] + carry;
                scratch.data[k] = (uint32_t)cur;
                uint64_t cur = (uint64_t)sp[k] + carry;
                sp[k] = (uint32_t)cur;
                carry = cur >> 32;
                k++;
            }
@@ -1117,21 +1137,18 @@ namespace netplus {

        // 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++) {
            out.data[i] = scratch.data[i + n];
        }
        uint32_t* __restrict__ op = out.data.get();
        std::memcpy(op, sp + n, (n + 1) * sizeof(uint32_t));
        out.used = n + 1;
        while (out.used > 1 && out.data[out.used - 1] == 0) out.used--;
        while (out.used > 1 && op[out.used - 1] == 0) out.used--;

        // 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());
            std::memcpy(op, scratch.data.get(), scratch.used * sizeof(uint32_t));
            if (out.capacity > out.used)
                std::fill(out.data.get() + out.used, out.data.get() + out.capacity, 0);
                std::memset(op + out.used, 0, (out.capacity - out.used) * sizeof(uint32_t));
        }
    }