Ejemplo n.º 1
0
        public virtual bool Equals(ECPoint other)
        {
            if (this == other)
            {
                return(true);
            }
            if (null == other)
            {
                return(false);
            }

            ECCurve c1 = this.Curve, c2 = other.Curve;
            bool    n1 = (null == c1), n2 = (null == c2);
            bool    i1 = IsInfinity, i2 = other.IsInfinity;

            if (i1 || i2)
            {
                return((i1 && i2) && (n1 || n2 || c1.Equals(c2)));
            }

            ECPoint p1 = this, p2 = other;

            if (n1 && n2)
            {
                // Points with null curve are in affine form, so already normalized
            }
            else if (n1)
            {
                p2 = p2.Normalize();
            }
            else if (n2)
            {
                p1 = p1.Normalize();
            }
            else if (!c1.Equals(c2))
            {
                return(false);
            }
            else
            {
                // TODO Consider just requiring already normalized, to avoid silent performance degradation

                ECPoint[] points = new ECPoint[] { this, c1.ImportPoint(p2) };

                // TODO This is a little strong, really only requires coZNormalizeAll to get Zs equal
                c1.NormalizeAll(points);

                p1 = points[0];
                p2 = points[1];
            }

            return(p1.XCoord.Equals(p2.XCoord) && p1.YCoord.Equals(p2.YCoord));
        }
Ejemplo n.º 2
0
        internal static ECPoint ImplShamirsTrick(ECPoint P, BigInteger k,
                                                 ECPoint Q, BigInteger l)
        {
            ECCurve curve    = P.Curve;
            ECPoint infinity = curve.Infinity;

            // TODO conjugate co-Z addition (ZADDC) can return both of these
            ECPoint PaddQ = P.Add(Q);
            ECPoint PsubQ = P.Subtract(Q);

            ECPoint[] points = new ECPoint[] { Q, PsubQ, P, PaddQ };
            curve.NormalizeAll(points);

            ECPoint[] table = new ECPoint[] {
                points[3].Negate(), points[2].Negate(), points[1].Negate(),
                points[0].Negate(), infinity, points[0],
                points[1], points[2], points[3]
            };

            byte[] jsf = WNafUtilities.GenerateJsf(k, l);

            ECPoint R = infinity;

            int i = jsf.Length;

            while (--i >= 0)
            {
                int jsfi = jsf[i];

                // NOTE: The shifting ensures the sign is extended correctly
                int kDigit = ((jsfi << 24) >> 28), lDigit = ((jsfi << 28) >> 28);

                int index = 4 + (kDigit * 3) + lDigit;
                R = R.TwicePlus(table[index]);
            }

            return(R);
        }
Ejemplo n.º 3
0
        public static WNafPreCompInfo Precompute(ECPoint p, int width, bool includeNegated)
        {
            ECCurve         c = p.Curve;
            WNafPreCompInfo wnafPreCompInfo = GetWNafPreCompInfo(c.GetPreCompInfo(p));

            ECPoint[] preComp = wnafPreCompInfo.PreComp;
            if (preComp == null)
            {
                preComp = new ECPoint[] { p };
            }

            int preCompLen    = preComp.Length;
            int reqPreCompLen = 1 << System.Math.Max(0, width - 2);

            if (preCompLen < reqPreCompLen)
            {
                ECPoint twiceP = wnafPreCompInfo.Twice;
                if (twiceP == null)
                {
                    twiceP = preComp[0].Twice().Normalize();
                    wnafPreCompInfo.Twice = twiceP;
                }

                preComp = ResizeTable(preComp, reqPreCompLen);

                /*
                 * TODO Okeya/Sakurai paper has precomputation trick and  "Montgomery's Trick" to speed this up.
                 * Also, co-Z arithmetic could avoid the subsequent normalization too.
                 */
                for (int i = preCompLen; i < reqPreCompLen; i++)
                {
                    /*
                     * Compute the new ECPoints for the precomputation array. The values 1, 3, 5, ...,
                     * 2^(width-1)-1 times p are computed
                     */
                    preComp[i] = twiceP.Add(preComp[i - 1]);
                }

                /*
                 * Having oft-used operands in affine form makes operations faster.
                 */
                c.NormalizeAll(preComp);
            }

            wnafPreCompInfo.PreComp = preComp;

            if (includeNegated)
            {
                ECPoint[] preCompNeg = wnafPreCompInfo.PreCompNeg;

                int pos;
                if (preCompNeg == null)
                {
                    pos        = 0;
                    preCompNeg = new ECPoint[reqPreCompLen];
                }
                else
                {
                    pos = preCompNeg.Length;
                    if (pos < reqPreCompLen)
                    {
                        preCompNeg = ResizeTable(preCompNeg, reqPreCompLen);
                    }
                }

                while (pos < reqPreCompLen)
                {
                    preCompNeg[pos] = preComp[pos].Negate();
                    ++pos;
                }

                wnafPreCompInfo.PreCompNeg = preCompNeg;
            }

            c.SetPreCompInfo(p, wnafPreCompInfo);

            return(wnafPreCompInfo);
        }