From d9a600384ea457c1117ac900c9e1ca3d164448f8 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 4 Jan 2024 13:56:40 +0700 Subject: Add some convenience methods to BigInteger --- crypto/src/math/BigInteger.cs | 6 ++++++ crypto/src/math/ec/abc/Tnaf.cs | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'crypto') 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)); } } -- cgit 1.4.1