Exemplo n.º 1
0
        protected internal bool SatisfiesCofactor()
        {
            BigInteger cofactor = Curve.Cofactor;

            if (cofactor != null && !cofactor.Equals(BigInteger.One))
            {
                return(!ECAlgorithms.ReferenceMultiply(this, cofactor).IsInfinity);
            }
            return(true);
        }
Exemplo n.º 2
0
        /**
         * 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. Where more
         * than one point is to be normalized, this method will generally be more efficient than
         * normalizing each point separately. An (optional) z-scaling factor can be applied; effectively
         * each z coordinate is scaled by this value prior to normalization (but only one
         * actual multiplication is needed).
         *
         * @param points
         *            An array of points that will be updated in place with their normalized versions,
         *            where necessary
         * @param off
         *            The start of the range of points to normalize
         * @param len
         *            The length of the range of points to normalize
         * @param iso
         *            The (optional) z-scaling factor - can be null
         */
        public virtual void NormalizeAll(ECPoint[] points, int off, int len, ECFieldElement iso)
        {
            CheckPoints(points, off, len);

            switch (this.CoordinateSystem)
            {
            case ECCurve.COORD_AFFINE:
            case ECCurve.COORD_LAMBDA_AFFINE:
            {
                if (iso != null)
                {
                    throw new ArgumentException("not valid for affine coordinates", "iso");
                }

                return;
            }
            }

            /*
             * Figure out which of the points actually need to be normalized
             */
            ECFieldElement[] zs      = new ECFieldElement[len];
            int[]            indices = new int[len];
            int count = 0;

            for (int i = 0; i < len; ++i)
            {
                ECPoint p = points[off + i];
                if (null != p && (iso != null || !p.IsNormalized()))
                {
                    zs[count]        = p.GetZCoord(0);
                    indices[count++] = off + i;
                }
            }

            if (count == 0)
            {
                return;
            }

            ECAlgorithms.MontgomeryTrick(zs, 0, count, iso);

            for (int j = 0; j < count; ++j)
            {
                int index = indices[j];
                points[index] = points[index].Normalize(zs[j]);
            }
        }
Exemplo n.º 3
0
        internal static ECPoint ImplSumOfMultiplies(ECPoint[] ps, BigInteger[] ks)
        {
            int num = ps.Length;

            bool[]            array  = new bool[num];
            WNafPreCompInfo[] array2 = new WNafPreCompInfo[num];
            byte[][]          array3 = new byte[num][];
            for (int i = 0; i < num; i++)
            {
                BigInteger bigInteger = ks[i];
                array[i]   = (bigInteger.SignValue < 0);
                bigInteger = bigInteger.Abs();
                int width = Math.Max(2, Math.Min(16, WNafUtilities.GetWindowSize(bigInteger.BitLength)));
                array2[i] = WNafUtilities.Precompute(ps[i], width, true);
                array3[i] = WNafUtilities.GenerateWindowNaf(width, bigInteger);
            }
            return(ECAlgorithms.ImplSumOfMultiplies(array, array2, array3));
        }
Exemplo n.º 4
0
        internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k, ECPointMap pointMapQ, BigInteger l)
        {
            bool flag  = k.SignValue < 0;
            bool flag2 = l.SignValue < 0;

            k = k.Abs();
            l = l.Abs();
            int             width            = Math.Max(2, Math.Min(16, WNafUtilities.GetWindowSize(Math.Max(k.BitLength, l.BitLength))));
            ECPoint         p                = WNafUtilities.MapPointWithPrecomp(P, width, true, pointMapQ);
            WNafPreCompInfo wNafPreCompInfo  = WNafUtilities.GetWNafPreCompInfo(P);
            WNafPreCompInfo wNafPreCompInfo2 = WNafUtilities.GetWNafPreCompInfo(p);

            ECPoint[] preCompP    = flag ? wNafPreCompInfo.PreCompNeg : wNafPreCompInfo.PreComp;
            ECPoint[] preCompQ    = flag2 ? wNafPreCompInfo2.PreCompNeg : wNafPreCompInfo2.PreComp;
            ECPoint[] preCompNegP = flag ? wNafPreCompInfo.PreComp : wNafPreCompInfo.PreCompNeg;
            ECPoint[] preCompNegQ = flag2 ? wNafPreCompInfo2.PreComp : wNafPreCompInfo2.PreCompNeg;
            byte[]    wnafP       = WNafUtilities.GenerateWindowNaf(width, k);
            byte[]    wnafQ       = WNafUtilities.GenerateWindowNaf(width, l);
            return(ECAlgorithms.ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ));
        }
Exemplo n.º 5
0
        public virtual void NormalizeAll(ECPoint[] points, int off, int len, ECFieldElement iso)
        {
            this.CheckPoints(points, off, len);
            int coordinateSystem = this.CoordinateSystem;

            if (coordinateSystem == 0 || coordinateSystem == 5)
            {
                if (iso != null)
                {
                    throw new ArgumentException("not valid for affine coordinates", "iso");
                }
                return;
            }
            else
            {
                ECFieldElement[] array  = new ECFieldElement[len];
                int[]            array2 = new int[len];
                int num = 0;
                for (int i = 0; i < len; i++)
                {
                    ECPoint eCPoint = points[off + i];
                    if (eCPoint != null && (iso != null || !eCPoint.IsNormalized()))
                    {
                        array[num]    = eCPoint.GetZCoord(0);
                        array2[num++] = off + i;
                    }
                }
                if (num == 0)
                {
                    return;
                }
                ECAlgorithms.MontgomeryTrick(array, 0, num, iso);
                for (int j = 0; j < num; j++)
                {
                    int num2 = array2[j];
                    points[num2] = points[num2].Normalize(array[j]);
                }
                return;
            }
        }
Exemplo n.º 6
0
        /**
         * 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. Where more
         * than one point is to be normalized, this method will generally be more efficient than
         * normalizing each point separately.
         *
         * @param points
         *            An array of points that will be updated in place with their normalized versions,
         *            where necessary
         */
        public virtual void NormalizeAll(ECPoint[] points)
        {
            CheckPoints(points);

            if (this.CoordinateSystem == ECCurve.COORD_AFFINE)
            {
                return;
            }

            /*
             * Figure out which of the points actually need to be normalized
             */
            ECFieldElement[] zs      = new ECFieldElement[points.Length];
            int[]            indices = new int[points.Length];
            int count = 0;

            for (int i = 0; i < points.Length; ++i)
            {
                ECPoint p = points[i];
                if (null != p && !p.IsNormalized())
                {
                    zs[count]        = p.GetZCoord(0);
                    indices[count++] = i;
                }
            }

            if (count == 0)
            {
                return;
            }

            ECAlgorithms.MontgomeryTrick(zs, 0, count);

            for (int j = 0; j < count; ++j)
            {
                int index = indices[j];
                points[index] = points[index].Normalize(zs[j]);
            }
        }
Exemplo n.º 7
0
        public virtual void NormalizeAll(ECPoint[] points, int off, int len, ECFieldElement iso)
        {
            this.CheckPoints(points, off, len);
            switch (this.CoordinateSystem)
            {
            case 0:
            case 5:
                if (iso != null)
                {
                    throw new ArgumentException("not valid for affine coordinates", "iso");
                }
                return;
            }
            ECFieldElement[] zs       = new ECFieldElement[len];
            int[]            numArray = new int[len];
            int index = 0;

            for (int i = 0; i < len; i++)
            {
                ECPoint point = points[off + i];
                if ((point != null) && ((iso != null) || !point.IsNormalized()))
                {
                    zs[index]         = point.GetZCoord(0);
                    numArray[index++] = off + i;
                }
            }
            if (index != 0)
            {
                ECAlgorithms.MontgomeryTrick(zs, 0, index, iso);
                for (int j = 0; j < index; j++)
                {
                    int num5 = numArray[j];
                    points[num5] = points[num5].Normalize(zs[j]);
                }
            }
        }
Exemplo n.º 8
0
        internal static ECPoint ImplSumOfMultipliesGlv(ECPoint[] ps, BigInteger[] ks, GlvEndomorphism glvEndomorphism)
        {
            BigInteger order = ps[0].Curve.Order;
            int        num   = ps.Length;

            BigInteger[] array = new BigInteger[num << 1];
            int          i     = 0;
            int          num2  = 0;

            while (i < num)
            {
                BigInteger[] array2 = glvEndomorphism.DecomposeScalar(ks[i].Mod(order));
                array[num2++] = array2[0];
                array[num2++] = array2[1];
                i++;
            }
            ECPointMap pointMap = glvEndomorphism.PointMap;

            if (glvEndomorphism.HasEfficientPointMap)
            {
                return(ECAlgorithms.ImplSumOfMultiplies(ps, pointMap, array));
            }
            ECPoint[] array3 = new ECPoint[num << 1];
            int       j      = 0;
            int       num3   = 0;

            while (j < num)
            {
                ECPoint eCPoint  = ps[j];
                ECPoint eCPoint2 = pointMap.Map(eCPoint);
                array3[num3++] = eCPoint;
                array3[num3++] = eCPoint2;
                j++;
            }
            return(ECAlgorithms.ImplSumOfMultiplies(array3, array));
        }
Exemplo n.º 9
0
        protected internal bool SatisfiesCofactor()
        {
            BigInteger cofactor = Curve.Cofactor;

            return(cofactor == null || cofactor.Equals(BigInteger.One) || !ECAlgorithms.ReferenceMultiply(this, cofactor).IsInfinity);
        }
Exemplo n.º 10
0
 public static void MontgomeryTrick(ECFieldElement[] zs, int off, int len)
 {
     ECAlgorithms.MontgomeryTrick(zs, off, len, null);
 }