diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-01-16 18:33:19 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-01-16 18:33:19 +0700 |
commit | eb7f836b7a8aa3ebdc004786889adfb73506968f (patch) | |
tree | a1e4b9a0df5e5d4285dee94b6afdd6cddac9ad16 | |
parent | Avoid allocations (diff) | |
download | BouncyCastle.NET-ed25519-eb7f836b7a8aa3ebdc004786889adfb73506968f.tar.xz |
Refactoring in RsaCoreEngine
-rw-r--r-- | crypto/src/crypto/engines/RSACoreEngine.cs | 33 |
1 files changed, 12 insertions, 21 deletions
diff --git a/crypto/src/crypto/engines/RSACoreEngine.cs b/crypto/src/crypto/engines/RSACoreEngine.cs index 2af447841..ffa448b3d 100644 --- a/crypto/src/crypto/engines/RSACoreEngine.cs +++ b/crypto/src/crypto/engines/RSACoreEngine.cs @@ -118,39 +118,30 @@ namespace Org.BouncyCastle.Crypto.Engines { CheckInitialised(); - if (key is RsaPrivateCrtKeyParameters) + if (key is RsaPrivateCrtKeyParameters crt) { // // we have the extra factors, use the Chinese Remainder Theorem - the author // wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for // advice regarding the expression of this. // - RsaPrivateCrtKeyParameters crtKey = (RsaPrivateCrtKeyParameters)key; - - BigInteger p = crtKey.P; - BigInteger q = crtKey.Q; - BigInteger dP = crtKey.DP; - BigInteger dQ = crtKey.DQ; - BigInteger qInv = crtKey.QInv; - - BigInteger mP, mQ, h, m; + BigInteger p = crt.P; + BigInteger q = crt.Q; + BigInteger dP = crt.DP; + BigInteger dQ = crt.DQ; + BigInteger qInv = crt.QInv; // mP = ((input Mod p) ^ dP)) Mod p - mP = (input.Remainder(p)).ModPow(dP, p); + BigInteger mP = (input.Remainder(p)).ModPow(dP, p); - // mQ = ((input Mod q) ^ dQ)) Mod q - mQ = (input.Remainder(q)).ModPow(dQ, q); + // mQ = ((input Mod q) ^ dQ)) Mod q + BigInteger mQ = (input.Remainder(q)).ModPow(dQ, q); // h = qInv * (mP - mQ) Mod p - h = mP.Subtract(mQ); - h = h.Multiply(qInv); - h = h.Mod(p); // Mod (in Java) returns the positive residual - - // m = h * q + mQ - m = h.Multiply(q); - m = m.Add(mQ); + BigInteger h = mP.Subtract(mQ).Multiply(qInv).Mod(p); - return m; + // m = h * q + mQ + return h.Multiply(q).Add(mQ); } return input.ModPow(key.Exponent, key.Modulus); |