/// <summary>
        /// Doubling the point in elliptic curve
        /// </summary>
        /// <returns></returns>
        public IEllipticCurvePoint Doubling()
        {
            if (Infinity)
            {
                return(GetInfinity());
            }
            // Coordinates
            uint[] a                 = m_a4.m_Value;
            uint[] b                 = m_a6.m_Value;
            uint[] x                 = m_X.m_Value;
            uint[] y                 = m_Y.m_Value;
            uint[] z                 = m_Z.m_Value;
            uint[] modulo            = m_Modulo.m_Value;
            int    maxModuloBitIndex = BigHelper.MaxNonZeroBitIndex(modulo);
            int    length            = modulo.Length;

            // Get the cache
            uint[] cache = new uint[length * 2];
            uint[] s     = new uint[length];
            uint[] m     = new uint[length];

            uint[] x1 = new uint[length];
            uint[] y1 = new uint[length];
            uint[] z1 = new uint[length];


            // X^2
            ModuloOperations.Multiply(x, x, modulo, x1, cache);
            // Z^2
            ModuloOperations.Multiply(z, z, modulo, y1, cache);
            // Z1 = Z^2*X^2
            ModuloOperations.Multiply(x1, y1, modulo, z1, cache);

            // X^4
            ModuloOperations.Multiply(x1, x1, modulo, x1, cache);
            // Z^4
            ModuloOperations.Multiply(y1, y1, modulo, y1, cache);
            // b*Z^4
            ModuloOperations.Multiply(y1, y1, modulo, y1, cache);
            // X1 = X^4 + b*Z^4
            ModuloOperations.Addition(x1, y1, modulo);

            // a*Z1
            ModuloOperations.Multiply(z1, a, modulo, s, cache);
            // Y^2
            ModuloOperations.Multiply(y, y, modulo, m, cache);
            // a*Z1 + Y^2
            ModuloOperations.Addition(s, m, modulo);
            // a*Z1 + Y^2 + b*Z^4
            ModuloOperations.Addition(s, y1, modulo);
            // X1*(a*Z1 + Y^2 + b*Z^4)
            ModuloOperations.Multiply(s, x1, modulo, s, cache);
            // b*Z^4*Z1
            ModuloOperations.Multiply(y1, z1, modulo, y1, cache);

            // Y1 = b*Z^4*Z1 + X1*(a*Z1 + Y^2 + b*Z^4)
            ModuloOperations.Addition(y1, s, modulo);

            return(new EllipticCurvePointC(x1, y1, z1, this));
        }
        /// <summary>
        /// Удвоить точку на кривой
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IEllipticCurvePoint Doubling()
        {
            if (m_Y.Zero)
            {
                return(GetInfinity());
            }
            // Исходные координаты
            uint[] a                 = m_a4.m_Value;
            uint[] x                 = m_X.m_Value;
            uint[] y                 = m_Y.m_Value;
            uint[] z                 = m_Z.m_Value;
            uint[] modulo            = m_Modulo.m_Value;
            int    maxModuloBitIndex = BigHelper.MaxNonZeroBitIndex(modulo);
            int    length            = modulo.Length;

            // Выделим кэш операций умножения
            uint[] cache = new uint[length * 2];
            uint[] s     = new uint[length];
            uint[] m     = new uint[length];

            uint[] x1 = new uint[length];
            uint[] y1 = new uint[length];
            uint[] z1 = new uint[length];

            // Расчитаем m
            // Первое слагаемое a*Z1^4
            ModuloOperations.Multiply(z, z, modulo, z1, cache);
            ModuloOperations.Multiply(z1, z1, modulo, z1, cache);
            ModuloOperations.Multiply(z1, a, modulo, z1, cache);
            // Второе слагаемое 3*X1^2
            ModuloOperations.Multiply(x, x, modulo, m, cache);
            ModuloOperations.Multiply(m, 3, modulo, m, cache);
            // Результат m = 3*X1^2 + a*Z1^4
            ModuloOperations.Addition(m, z1, modulo);

            // Расчитаем s = 4*X1*Y1^2
            ModuloOperations.Multiply(y, y, modulo, z1, cache);

            ModuloOperations.Multiply(z1, x, modulo, s, cache);
            ModuloOperations.Multiply(s, 4, modulo, s, cache);

            // Расчитаем X3 = m^2 - 2*s
            ModuloOperations.Multiply(m, m, modulo, x1, cache);
            ModuloOperations.Multiply(s, 2, modulo, y1, cache);
            ModuloOperations.Substraction(x1, y1, modulo);

            // Расчитаем Y3 = m*(s - X3) - 8*Y1^4
            ModuloOperations.Multiply(z1, z1, modulo, z1, cache);
            ModuloOperations.Multiply(z1, 8, modulo, z1, cache);

            ModuloOperations.Substraction(s, x1, modulo, y1);
            ModuloOperations.Multiply(y1, m, modulo, y1, cache);
            ModuloOperations.Substraction(y1, z1, modulo);

            // Расчитаем Z3 = 2*Y1*Z1
            ModuloOperations.Multiply(y, z, modulo, z1, cache);
            ModuloOperations.Multiply(z1, 2, modulo, z1, cache);

            return(new EllipticCurvePointB(x1, y1, z1, this));
        }
 /// <summary>
 /// Invert this value at elliptic curve
 /// </summary>
 /// <returns></returns>
 public IEllipticCurvePoint Invert()
 {
     if (m_Y.Zero)
     {
         return(GetInfinity());
     }
     uint[] items = new uint[m_Modulo.m_Value.Length];
     ModuloOperations.Addition(m_X.m_Value, m_Y.m_Value, m_Modulo.m_Value, items);
     return(new EllipticCurvePointC(m_X, new IntBig(items), m_Z, this));
 }
Beispiel #4
0
 /// <summary>
 ///  Increments the value
 /// </summary>
 /// <param name="value">The value to increment</param>
 /// <returns>Incremented value</returns>
 public static MIntBig operator ++(MIntBig value)
 {
     if (value as object == null)
     {
         throw new ArgumentNullException("value");
     }
     uint[] items = new uint[value.m_Modulo.m_Value.Length];
     ModuloOperations.Addition(value.m_Value.m_Value, 1, value.m_Modulo.m_Value, items);
     return(new MIntBig(new IntBig(items), value.m_Modulo));
 }
Beispiel #5
0
 /// <summary>
 /// Adds the values
 /// </summary>
 /// <param name="value1">The first value to add</param>
 /// <param name="value2">The second value to add</param>
 /// <returns>The sum of values</returns>
 public static MIntBig operator +(MIntBig value1, MIntBig value2)
 {
     if (value1 as object == null)
     {
         throw new ArgumentNullException("value1");
     }
     if (value2 as object == null)
     {
         throw new ArgumentNullException("value2");
     }
     if (value1.m_Modulo != value2.m_Modulo)
     {
         throw new Exception("Modules not equal!");
     }
     uint[] items = new uint[value1.m_Modulo.m_Value.Length];
     ModuloOperations.Addition(value1.m_Value.m_Value, value2.m_Value.m_Value, value1.m_Modulo.m_Value, items);
     return(new MIntBig(new IntBig(items), value1.m_Modulo));
 }
        /// <summary>
        /// Adds the this value with another value in elliptic curve
        /// </summary>
        /// <param name="value">The value to add with this value</param>
        /// <returns>The sum of values</returns>
        public IEllipticCurvePoint Addition(IEllipticCurvePoint value)
        {
            EllipticCurvePointC value2 = value as EllipticCurvePointC;

            if (value2 as object == null)
            {
                throw new Exception("Incorrect point type!");
            }
            if (m_a4 != value2.m_a4 || m_a6 != value2.m_a6 || m_Modulo != value2.m_Modulo)
            {
                throw new Exception("Incorrect value elliptic curve parameters!");
            }
            if (value2.Infinity)
            {
                return(new EllipticCurvePointC(m_X, m_Y, m_Z, this));
            }
            if (Infinity)
            {
                return(new EllipticCurvePointC(value2.m_X, value2.m_Y, value2.m_Z, value2));
            }
            if (Equals(value2))
            {
                return(Doubling());
            }
            // Coordinates
            uint[] a                 = m_a4.m_Value;
            uint[] x1                = m_X.m_Value;
            uint[] y1                = m_Y.m_Value;
            uint[] z1                = m_Z.m_Value;
            uint[] x2                = value2.m_X.m_Value;
            uint[] y2                = value2.m_Y.m_Value;
            uint[] z2                = value2.m_Z.m_Value;
            uint[] modulo            = m_Modulo.m_Value;
            int    maxModuloBitIndex = BigHelper.MaxNonZeroBitIndex(modulo);
            int    length            = modulo.Length;

            // Get the cache
            uint[] cache = new uint[length * 2];

            uint[] A = new uint[length];
            uint[] D = new uint[length];
            uint[] B = new uint[length];
            uint[] C = new uint[length];

            uint[] xr = new uint[length];
            uint[] yr = new uint[length];
            uint[] zr = new uint[length];
            // Z1^2
            ModuloOperations.Multiply(z1, z1, modulo, A, cache);
            // Z1^2 * a
            ModuloOperations.Multiply(z1, a, modulo, D, cache);
            // X2 * Z1
            ModuloOperations.Multiply(x2, z1, modulo, B, cache);
            // B = X2 * Z1 + X1
            ModuloOperations.Addition(B, x1, modulo);

            // C = Z1*B
            ModuloOperations.Multiply(z1, B, modulo, C, cache);

            // C + Z1^2 * a
            ModuloOperations.Addition(D, C, modulo);
            // B * (C + Z1^2 * a)
            ModuloOperations.Multiply(D, B, modulo, D, cache);
            // D = B^2 * (C + Z1^2 * a)
            ModuloOperations.Multiply(D, B, modulo, D, cache);

            // Y2 * Z1^2
            ModuloOperations.Multiply(A, y2, modulo, A, cache);
            // A =  Y2 * Z1^2 + Y1
            ModuloOperations.Addition(A, y1, modulo);

            if (BigHelper.IfZero(B))
            {
                if (BigHelper.IfZero(A))
                {
                    return(value2.Doubling());
                }
                else
                {
                    return(GetInfinity());
                }
            }

            // Z3 = C^2
            ModuloOperations.Multiply(C, C, modulo, zr, cache);

            // E = A * C
            ModuloOperations.Multiply(C, A, modulo, C, cache);

            // A^2
            ModuloOperations.Multiply(A, A, modulo, xr, cache);
            // A^2 + D
            ModuloOperations.Addition(xr, D, modulo);
            // X3 = A^2 + D + E
            ModuloOperations.Addition(xr, C, modulo);

            // X2 * Z3
            ModuloOperations.Multiply(x2, zr, modulo, A, cache);
            // F = X3 + X2 * Z3
            ModuloOperations.Addition(A, xr, modulo);

            // X2 + Y2
            ModuloOperations.Addition(x2, y2, modulo, B);
            // (X2 + Y2) * Z3
            ModuloOperations.Multiply(B, zr, modulo, B, cache);
            // G = (X2 + Y2) * Z3^2
            ModuloOperations.Multiply(B, zr, modulo, B, cache);

            // E + Z3
            ModuloOperations.Addition(C, zr, modulo, yr);
            // (E + Z3) * F
            ModuloOperations.Multiply(yr, A, modulo, yr, cache);
            // (E + Z3) * F + G
            ModuloOperations.Addition(yr, B, modulo, yr);

            return(new EllipticCurvePointC(xr, yr, zr, this));
        }