Nodes/ComfyUI ARG Toolkit/RSA Restore Prime Factors
ComfyUI Node

RSA Restore Prime Factors

Turn a leaked d into p and q

By AzelusLightvale·Created about a year ago·Updated a day ago· 3
RSA Restore Prime Factors
    • p
    • q
    ◄private_modulus65537►
    ◄public_exponent1►
    ◄private_exponent1►

    Normally factoring a 2048-bit RSA modulus is the thing that doesn't happen. Unless somebody leaked the private exponent - then it's a five-line algorithm, and RSA Restore Prime Factors is that algorithm with two sockets.

    If you've got n, e and d, this node gets you p and q. Which is the whole game, because with the factors you can rebuild a working private key and read whatever the ciphertext says.

    Why the leak is fatal

    RSA's security rests on n = p*q being hard to factor. The private exponent d is defined as the inverse of e modulo lcm(p-1, q-1), which means e*d - 1 is an exact multiple of that LCM - and knowing a multiple of the group order is enough to split n. Write e*d - 1 = 2^t * r with r odd, then repeatedly square random values mod n; a nontrivial square root of 1 appears, and gcd of value - 1 with n gives a factor.

    That's it. It's the standard first move in every CTF that hands you a private key component, and it's why "leaking d but not p/q" is not a partial leak, it's a full one.

    The mechanism here

    Thin wrapper over pyca:

    return rsa.rsa_recover_prime_factors(private_modulus, public_exponent, private_exponent)
    

    The library's implementation is the 2^t * r procedure above, and it returns the pair with p > q. The pack just exposes it as a node, so the outputs are two INTs labelled p and q - same order pyca gives them, which is larger prime first.

    Inputs

    private_modulus is n - confusingly named, since n is public in any real key. The tooltip says "the private modulus (n)", and it's the same number either way.

    public_exponent is e. Note the default here is 1, not 65537 (the pack isn't consistent about that across nodes), and 1 is never valid - an exponent of 1 means identity, and the recovery algorithm will produce nothing usable.

    private_exponent is d, which defaults to 1 too. Set all three deliberately.

    All three are integer widgets capped at 4,294,967,296, so for anything bigger than a toy key the values have to arrive over wires - n and e from RSA Public Numbers Values, d from RSA Private Numbers Values. Then the outputs p and q feed RSA Private Numbers into Key (along with the public numbers container) and you're back to a live key object for RSA Decrypt Data. Or, if you only had p, q and e, RSA Restore Private Exponent goes the other direction and computes d.

    What it feels like when it works

    Nothing dramatic. Two enormous integers appear, you wire them onward, and a ciphertext that's been sitting opaque in your graph decrypts into readable text on the other end of the chain. There is a genuinely satisfying moment where a puzzle that looked like a wall turns out to be one modular-inverse away from over. That's the entire appeal of this pack.

    Install

    ComfyUI Manager → search ComfyUI ARG Toolkit → install → restart → hard-refresh the browser tab. Or manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/AzelusLightvale/ComfyUI-ARG-Toolkit
    pip install -r requirements.txt
    

    Nothing to download into models/. The dependency that does the work is cryptography. One practical warning about the manual path: requirements.txt is a locked uv export containing torch and torchaudio alongside the crypto libs, so pip install -r into a live ComfyUI venv may swap your CUDA torch build. Manager handles it better; if you go manual, verify torch.cuda.is_available() before you blame the next workflow.

    Common issues

    ValueError: Unable to compute factors p and q from exponent d. The library's own error when the three numbers are inconsistent - d doesn't correspond to e and n. Someone transcribed a digit wrong, or you're mixing a modulus from one key with an exponent from another.

    It returns factors but they're tiny or nonsense. You left public_exponent at 1. Also check you didn't confuse n with d, which is easy when both are four-hundred-digit numbers and the labels are all you have.

    Can't type the numbers in. The 32-bit widget cap - real RSA material has to come from wires.

    ComfyUI hangs for a moment on a big key. The algorithm is a few modular exponentiations on a 2048-bit modulus; it's not instant, and it's not hung. It also isn't hardened against a deliberately hostile key, which is a fine trade for a puzzle tool and a bad one for anything adversarial.

    Worth saying plainly: this is a young single-author GPLv3 pack with a manual-testing testing story and essentially no public discussion. As a puzzle workbench it's the only thing of its kind inside ComfyUI, and that alone is worth the install.

    CategoryARG Toolkit/Cryptography/Modern/Asymmetric

    Inputs (3)

    NameTypeDefaultDescription
    private_modulusINT655371–4294967296The private modulus (n).
    public_exponentINT11–4294967296The public exponent (e)
    private_exponentINT11–4294967296The private exponent (d)

    Outputs (2)

    NameTypeDescription
    pINT—
    qINT—