diff options
3 files changed, 24 insertions, 9 deletions
diff --git a/crypto/src/math/ec/multiplier/FixedPointCombMultiplier.cs b/crypto/src/math/ec/multiplier/FixedPointCombMultiplier.cs index e3da3f7c2..a8ef5a77a 100644 --- a/crypto/src/math/ec/multiplier/FixedPointCombMultiplier.cs +++ b/crypto/src/math/ec/multiplier/FixedPointCombMultiplier.cs @@ -21,10 +21,11 @@ namespace Org.BouncyCastle.Math.EC.Multiplier throw new InvalidOperationException("fixed-point comb doesn't support scalars larger than the curve order"); } - int width = GetWidthForCombSize(size); + int minWidth = GetWidthForCombSize(size); - FixedPointPreCompInfo info = FixedPointUtilities.Precompute(p, width); + FixedPointPreCompInfo info = FixedPointUtilities.Precompute(p, minWidth); ECPoint[] lookupTable = info.PreComp; + int width = info.Width; int d = (size + width - 1) / width; diff --git a/crypto/src/math/ec/multiplier/FixedPointPreCompInfo.cs b/crypto/src/math/ec/multiplier/FixedPointPreCompInfo.cs index 306f40a11..56a6326a1 100644 --- a/crypto/src/math/ec/multiplier/FixedPointPreCompInfo.cs +++ b/crypto/src/math/ec/multiplier/FixedPointPreCompInfo.cs @@ -12,10 +12,23 @@ */ protected ECPoint[] m_preComp = null; + /** + * The width used for the precomputation. If a larger width precomputation + * is already available this may be larger than was requested, so calling + * code should refer to the actual width. + */ + protected int m_width = -1; + public virtual ECPoint[] PreComp { get { return m_preComp; } set { this.m_preComp = value; } } + + public virtual int Width + { + get { return m_width; } + set { this.m_width = value; } + } } } diff --git a/crypto/src/math/ec/multiplier/FixedPointUtilities.cs b/crypto/src/math/ec/multiplier/FixedPointUtilities.cs index 194995e08..d927d010b 100644 --- a/crypto/src/math/ec/multiplier/FixedPointUtilities.cs +++ b/crypto/src/math/ec/multiplier/FixedPointUtilities.cs @@ -22,22 +22,22 @@ namespace Org.BouncyCastle.Math.EC.Multiplier return new FixedPointPreCompInfo(); } - public static FixedPointPreCompInfo Precompute(ECPoint p, int width) + public static FixedPointPreCompInfo Precompute(ECPoint p, int minWidth) { ECCurve c = p.Curve; - int n = 1 << width; + int n = 1 << minWidth; FixedPointPreCompInfo info = GetFixedPointPreCompInfo(c.GetPreCompInfo(p, PRECOMP_NAME)); ECPoint[] lookupTable = info.PreComp; - if (lookupTable == null || lookupTable.Length != n) + if (lookupTable == null || lookupTable.Length < n) { int bits = GetCombSize(c); - int d = (bits + width - 1) / width; + int d = (bits + minWidth - 1) / minWidth; - ECPoint[] pow2Table = new ECPoint[width]; + ECPoint[] pow2Table = new ECPoint[minWidth]; pow2Table[0] = p; - for (int i = 1; i < width; ++i) + for (int i = 1; i < minWidth; ++i) { pow2Table[i] = pow2Table[i - 1].TimesPow2(d); } @@ -47,7 +47,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier lookupTable = new ECPoint[n]; lookupTable[0] = c.Infinity; - for (int bit = width - 1; bit >= 0; --bit) + for (int bit = minWidth - 1; bit >= 0; --bit) { ECPoint pow2 = pow2Table[bit]; @@ -61,6 +61,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier c.NormalizeAll(lookupTable); info.PreComp = lookupTable; + info.Width = minWidth; c.SetPreCompInfo(p, PRECOMP_NAME, info); } |