From a1f04c18f436a7206c990e1c1625250f5dee93b3 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 22 Jan 2014 12:47:20 +0700 Subject: Add foundations for supporting other coordinate systems Add curve configuration Multipliers now live on the curve instead of points --- crypto/src/math/ec/ECPoint.cs | 75 +++++++++---------------------------------- 1 file changed, 16 insertions(+), 59 deletions(-) (limited to 'crypto/src/math/ec/ECPoint.cs') diff --git a/crypto/src/math/ec/ECPoint.cs b/crypto/src/math/ec/ECPoint.cs index d81558939..1b00b764f 100644 --- a/crypto/src/math/ec/ECPoint.cs +++ b/crypto/src/math/ec/ECPoint.cs @@ -16,7 +16,6 @@ namespace Org.BouncyCastle.Math.EC internal readonly ECCurve curve; internal readonly ECFieldElement x, y; internal readonly bool withCompression; - internal ECMultiplier multiplier = null; internal PreCompInfo preCompInfo = null; protected internal ECPoint( @@ -49,6 +48,18 @@ namespace Org.BouncyCastle.Math.EC get { return y; } } + + /** + * Normalization ensures that any projective coordinate is 1, and therefore that the x, y + * coordinates reflect those of the equivalent point in an affine coordinate system. + * + * @return a new ECPoint instance representing the same point, but with normalized coordinates + */ + public virtual ECPoint Normalize() + { + return this; + } + public bool IsInfinity { get { return x == null && y == null; } @@ -91,17 +102,6 @@ namespace Org.BouncyCastle.Math.EC return hc; } -// /** -// * Mainly for testing. Explicitly set the ECMultiplier. -// * @param multiplier The ECMultiplier to be used to multiply -// * this ECPoint. -// */ -// internal void SetECMultiplier( -// ECMultiplier multiplier) -// { -// this.multiplier = multiplier; -// } - /** * Sets the PreCompInfo. Used by ECMultipliers * to save the precomputation for this ECPoint to store the @@ -137,23 +137,6 @@ namespace Org.BouncyCastle.Math.EC { return TwicePlus(this); } - - /** - * Sets the appropriate ECMultiplier, unless already set. - */ - internal virtual void AssertECMultiplier() - { - if (this.multiplier == null) - { - lock (this) - { - if (this.multiplier == null) - { - this.multiplier = new WNafMultiplier(); - } - } - } - } } public abstract class ECPointBase @@ -222,8 +205,7 @@ namespace Org.BouncyCastle.Math.EC if (k.SignValue == 0) return this.curve.Infinity; - AssertECMultiplier(); - return this.multiplier.Multiply(this, k, preCompInfo); + return this.Curve.GetMultiplier().Multiply(this, k, preCompInfo); } } @@ -271,7 +253,7 @@ namespace Org.BouncyCastle.Math.EC { get { - return this.Y.ToBigInteger().TestBit(0); + return this.Y.TestBitZero(); } } @@ -541,8 +523,7 @@ namespace Org.BouncyCastle.Math.EC // X9.62 4.2.2 and 4.3.6: // if x = 0 then ypTilde := 0, else ypTilde is the rightmost // bit of y * x^(-1) - return this.X.ToBigInteger().SignValue != 0 - && this.Y.Multiply(this.X.Invert()).ToBigInteger().TestBit(0); + return !this.X.IsZero && this.Y.Divide(this.X).TestBitZero(); } } @@ -658,7 +639,7 @@ namespace Org.BouncyCastle.Math.EC // if x1 == 0, then (x1, y1) == (x1, x1 + y1) // and hence this = -this and thus 2(x1, y1) == infinity - if (this.x.ToBigInteger().SignValue == 0) + if (this.x.IsZero) return this.curve.Infinity; F2mFieldElement lambda = (F2mFieldElement) this.x.Add(this.y.Divide(this.x)); @@ -674,29 +655,5 @@ namespace Org.BouncyCastle.Math.EC { return new F2mPoint(curve, this.x, this.x.Add(this.y), withCompression); } - - /** - * Sets the appropriate ECMultiplier, unless already set. - */ - internal override void AssertECMultiplier() - { - if (this.multiplier == null) - { - lock (this) - { - if (this.multiplier == null) - { - if (((F2mCurve) this.curve).IsKoblitz) - { - this.multiplier = new WTauNafMultiplier(); - } - else - { - this.multiplier = new WNafMultiplier(); - } - } - } - } - } } } -- cgit 1.4.1