Пример #1
0
        /// <summary>
        /// Returns k * point computed using the double and <see cref="PointAdd"/> algorithm.
        /// </summary>
        /// <param name="k"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        public BigIntegerPoint ScalarMult(BigInteger k, BigIntegerPoint point)
        {
            if (EllipticCurveHelpers.MathMod(k, this.N) == 0 || point.IsEmpty())
            {
                return(BigIntegerPoint.GetEmpty());
            }

            if (k < 0)
            {
                return(this.ScalarMult(-k, this.NegatePoint(point)));
            }

            BigIntegerPoint result = BigIntegerPoint.GetEmpty();
            BigIntegerPoint addend = point;

            while (k > 0)
            {
                if ((k & 1) > 0)
                {
                    result = this.PointAdd(result, addend);
                }

                addend = this.PointAdd(addend, addend);

                k >>= 1;
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Returns the result of lhs + rhs according to the group law.
        /// </summary>
        /// <param name="lhs"></param>
        /// <param name="rhs"></param>
        /// <returns></returns>
        public BigIntegerPoint PointAdd(BigIntegerPoint lhs, BigIntegerPoint rhs)
        {
            if (lhs.IsEmpty())
            {
                return(rhs);
            }

            if (rhs.IsEmpty())
            {
                return(lhs);
            }

            BigInteger x1 = lhs.X, y1 = lhs.Y;
            BigInteger x2 = rhs.X, y2 = rhs.Y;

            if (x1 == x2 && y1 != y2)
            {
                return(BigIntegerPoint.GetEmpty());
            }

            BigInteger m;

            if (x1 == x2)
            {
                m = (3 * x1 * x1 + this.A) * this.InverseMod(2 * y1, this.P);
            }
            else
            {
                m = (y1 - y2) * this.InverseMod(x1 - x2, this.P);
            }

            BigInteger      x3     = m * m - x1 - x2;
            BigInteger      y3     = y1 + m * (x3 - x1);
            BigIntegerPoint result = new BigIntegerPoint(EllipticCurveHelpers.MathMod(x3, this.P), EllipticCurveHelpers.MathMod(-y3, this.P));

            return(result);
        }