Encryption is divided into two parts: updateKey and process. Each time you get a message with a new RSA key, call updateKey before getting into encryption/decryption. If you process messages with the same key continuously, only updateKey needs be called once at the beginning.
Two implementations of function keydateKey are provided. One of them has two inputs: modulus and exponent. The other one has three inputs: modulus, exponent, and rMod. The extract argument “rMod” is actually 2^(2*N) mod modulus. This is a key parameter in the encryption/decryption calculation. If you have pre-calculated the arguments, call the second updateKey and set it up directly. If you do not have it, call the first one and do the calculation on chip with an extract resource.
RSA encryption and decryption are basically the same calculations: big integer modulus exponential calculation. Instead of straight calculation, convert the big integer into its Montgomery field and do an exponential calculation. Finally, convert the result back to normal representation. In such cases, avoid most integer division and multiplication to save resource and have higher frequency.