diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-02-07 12:56:46 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-02-07 12:56:46 +0700 |
commit | c89dd16807951633fce1e2e7d34858868a4ca076 (patch) | |
tree | c9fd16a709023572e008fd5ddac86d423ad82cd6 | |
parent | Update OCB draft references (diff) | |
download | BouncyCastle.NET-ed25519-c89dd16807951633fce1e2e7d34858868a4ca076.tar.xz |
Index precomputation info by name
-rw-r--r-- | crypto/src/math/ec/ECCurve.cs | 26 | ||||
-rw-r--r-- | crypto/src/math/ec/ECPoint.cs | 3 | ||||
-rw-r--r-- | crypto/src/math/ec/multiplier/FixedPointUtilities.cs | 6 | ||||
-rw-r--r-- | crypto/src/math/ec/multiplier/WNafUtilities.cs | 8 | ||||
-rw-r--r-- | crypto/src/math/ec/multiplier/WTauNafMultiplier.cs | 7 |
5 files changed, 36 insertions, 14 deletions
diff --git a/crypto/src/math/ec/ECCurve.cs b/crypto/src/math/ec/ECCurve.cs index 82cf1367b..5c7592a92 100644 --- a/crypto/src/math/ec/ECCurve.cs +++ b/crypto/src/math/ec/ECCurve.cs @@ -116,26 +116,40 @@ namespace Org.BouncyCastle.Math.EC return coord == COORD_AFFINE; } - public virtual PreCompInfo GetPreCompInfo(ECPoint p) + public virtual PreCompInfo GetPreCompInfo(ECPoint point, string name) { - CheckPoint(p); - return p.m_preCompInfo; + CheckPoint(point); + lock (point) + { + IDictionary table = point.m_preCompTable; + return table == null ? null : (PreCompInfo)table[name]; + } } /** - * Sets the <code>PreCompInfo</code> for a point on this curve. Used by + * Adds <code>PreCompInfo</code> for a point on this curve, under a given name. Used by * <code>ECMultiplier</code>s to save the precomputation for this <code>ECPoint</code> for use * by subsequent multiplication. * * @param point * The <code>ECPoint</code> to store precomputations for. + * @param name + * A <code>String</code> used to index precomputations of different types. * @param preCompInfo * The values precomputed by the <code>ECMultiplier</code>. */ - public virtual void SetPreCompInfo(ECPoint point, PreCompInfo preCompInfo) + public virtual void SetPreCompInfo(ECPoint point, string name, PreCompInfo preCompInfo) { CheckPoint(point); - point.m_preCompInfo = preCompInfo; + lock (point) + { + IDictionary table = point.m_preCompTable; + if (null == table) + { + point.m_preCompTable = table = Platform.CreateHashtable(4); + } + table[name] = preCompInfo; + } } public virtual ECPoint ImportPoint(ECPoint p) diff --git a/crypto/src/math/ec/ECPoint.cs b/crypto/src/math/ec/ECPoint.cs index 4b6a2cbce..f8bbf04e7 100644 --- a/crypto/src/math/ec/ECPoint.cs +++ b/crypto/src/math/ec/ECPoint.cs @@ -50,7 +50,8 @@ namespace Org.BouncyCastle.Math.EC protected internal readonly ECFieldElement[] m_zs; protected internal readonly bool m_withCompression; - protected internal PreCompInfo m_preCompInfo = null; + // Dictionary is (string -> PreCompInfo) + protected internal IDictionary m_preCompTable = null; protected ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression) : this(curve, x, y, GetInitialZCoords(curve), withCompression) diff --git a/crypto/src/math/ec/multiplier/FixedPointUtilities.cs b/crypto/src/math/ec/multiplier/FixedPointUtilities.cs index 8a04fcdc1..194995e08 100644 --- a/crypto/src/math/ec/multiplier/FixedPointUtilities.cs +++ b/crypto/src/math/ec/multiplier/FixedPointUtilities.cs @@ -4,6 +4,8 @@ namespace Org.BouncyCastle.Math.EC.Multiplier { public class FixedPointUtilities { + public static readonly string PRECOMP_NAME = "bc_fixed_point"; + public static int GetCombSize(ECCurve c) { BigInteger order = c.Order; @@ -25,7 +27,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier ECCurve c = p.Curve; int n = 1 << width; - FixedPointPreCompInfo info = GetFixedPointPreCompInfo(c.GetPreCompInfo(p)); + FixedPointPreCompInfo info = GetFixedPointPreCompInfo(c.GetPreCompInfo(p, PRECOMP_NAME)); ECPoint[] lookupTable = info.PreComp; if (lookupTable == null || lookupTable.Length != n) @@ -60,7 +62,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier info.PreComp = lookupTable; - c.SetPreCompInfo(p, info); + c.SetPreCompInfo(p, PRECOMP_NAME, info); } return info; diff --git a/crypto/src/math/ec/multiplier/WNafUtilities.cs b/crypto/src/math/ec/multiplier/WNafUtilities.cs index d37da8a5e..eac47222b 100644 --- a/crypto/src/math/ec/multiplier/WNafUtilities.cs +++ b/crypto/src/math/ec/multiplier/WNafUtilities.cs @@ -4,6 +4,8 @@ namespace Org.BouncyCastle.Math.EC.Multiplier { public abstract class WNafUtilities { + public static readonly string PRECOMP_NAME = "bc_wnaf"; + private static int[] DEFAULT_WINDOW_SIZE_CUTOFFS = new int[]{ 13, 41, 121, 337, 897, 2305 }; public static int[] GenerateCompactNaf(BigInteger k) @@ -188,7 +190,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier * most one is non-zero. * @param k The integer of which the Window NAF is computed. * @return The Window NAF of the given width, such that the following holds: - * <code>k = ∑<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup> + * <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup> * </code>, where the <code>k<sub>i</sub></code> denote the elements of the * returned <code>byte[]</code>. */ @@ -292,7 +294,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier public static WNafPreCompInfo Precompute(ECPoint p, int width, bool includeNegated) { ECCurve c = p.Curve; - WNafPreCompInfo wnafPreCompInfo = GetWNafPreCompInfo(c.GetPreCompInfo(p)); + WNafPreCompInfo wnafPreCompInfo = GetWNafPreCompInfo(c.GetPreCompInfo(p, PRECOMP_NAME)); ECPoint[] preComp = wnafPreCompInfo.PreComp; if (preComp == null) @@ -363,7 +365,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier wnafPreCompInfo.PreCompNeg = preCompNeg; } - c.SetPreCompInfo(p, wnafPreCompInfo); + c.SetPreCompInfo(p, PRECOMP_NAME, wnafPreCompInfo); return wnafPreCompInfo; } diff --git a/crypto/src/math/ec/multiplier/WTauNafMultiplier.cs b/crypto/src/math/ec/multiplier/WTauNafMultiplier.cs index b87b87000..dda778eea 100644 --- a/crypto/src/math/ec/multiplier/WTauNafMultiplier.cs +++ b/crypto/src/math/ec/multiplier/WTauNafMultiplier.cs @@ -11,6 +11,9 @@ namespace Org.BouncyCastle.Math.EC.Multiplier public class WTauNafMultiplier : AbstractECMultiplier { + // TODO Create WTauNafUtilities class and move various functionality into it + internal static readonly string PRECOMP_NAME = "bc_wtnaf"; + /** * Multiplies a {@link org.bouncycastle.math.ec.F2mPoint F2mPoint} * by <code>k</code> using the reduced <code>τ</code>-adic NAF (RTNAF) @@ -33,7 +36,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier ZTauElement rho = Tnaf.PartModReduction(k, m, a, s, mu, (sbyte)10); - return MultiplyWTnaf(p, rho, curve.GetPreCompInfo(p), a, mu); + return MultiplyWTnaf(p, rho, curve.GetPreCompInfo(p, PRECOMP_NAME), a, mu); } /** @@ -80,7 +83,7 @@ namespace Org.BouncyCastle.Math.EC.Multiplier WTauNafPreCompInfo pre = new WTauNafPreCompInfo(); pre.PreComp = pu; - curve.SetPreCompInfo(p, pre); + curve.SetPreCompInfo(p, PRECOMP_NAME, pre); } else { |