diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-01-04 13:56:40 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-01-04 13:56:40 +0700 |
commit | d9a600384ea457c1117ac900c9e1ca3d164448f8 (patch) | |
tree | 8e6caccf0880b799a7513b4fa782dc4fdc380b87 | |
parent | Refactoring around Asn1TaggedObject (diff) | |
download | BouncyCastle.NET-ed25519-d9a600384ea457c1117ac900c9e1ca3d164448f8.tar.xz |
Add some convenience methods to BigInteger
-rw-r--r-- | crypto/src/math/BigInteger.cs | 6 | ||||
-rw-r--r-- | crypto/src/math/ec/abc/Tnaf.cs | 4 |
2 files changed, 7 insertions, 3 deletions
diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs index 42b5b5089..e5ab22e92 100644 --- a/crypto/src/math/BigInteger.cs +++ b/crypto/src/math/BigInteger.cs @@ -1719,6 +1719,8 @@ namespace Org.BouncyCastle.Math return (biggie.sign >= 0 ? biggie : biggie.Add(m)); } + public BigInteger ModDivide(BigInteger y, BigInteger m) => ModMultiply(y.ModInverse(m), m); + public BigInteger ModInverse( BigInteger m) { @@ -1875,6 +1877,10 @@ namespace Org.BouncyCastle.Math Array.Clear(x, 0, x.Length); } + public BigInteger ModMultiply(BigInteger y, BigInteger m) => Multiply(y).Mod(m); + + public BigInteger ModSquare(BigInteger m) => Square().Mod(m); + public BigInteger ModPow(BigInteger e, BigInteger m) { if (m.sign < 1) diff --git a/crypto/src/math/ec/abc/Tnaf.cs b/crypto/src/math/ec/abc/Tnaf.cs index d8e9b6ae0..3707a974a 100644 --- a/crypto/src/math/ec/abc/Tnaf.cs +++ b/crypto/src/math/ec/abc/Tnaf.cs @@ -512,9 +512,7 @@ namespace Org.BouncyCastle.Math.EC.Abc { // For w <> 4, the values must be computed BigInteger[] us = GetLucas(mu, w, false); - BigInteger twoToW = BigInteger.Zero.SetBit(w); - BigInteger u1invert = us[1].ModInverse(twoToW); - return us[0].ShiftLeft(1).Multiply(u1invert).Mod(twoToW); + return us[0].ShiftLeft(1).ModDivide(us[1], BigInteger.One.ShiftLeft(w)); } } |