コード例 #1
0
        /// <summary>
        /// Modulus.
        /// </summary>
        /// <param name="mod">Modulus.</param>
        public void Mod(GXBigInteger mod)
        {
            /*
             * //value = value - (mod * (value / mod) )
             * GXBigInteger tmp = new GXBigInteger(this);
             * tmp.Div(mod);
             * tmp.Multiply(mod);
             * Sub(tmp);
             * changed = true;
             */
            GXBigInteger current = new GXBigInteger(1);
            GXBigInteger denom   = new GXBigInteger(mod);
            bool         neq     = negative;

            negative = false;
            // while denom < this.
            while (denom.Compare(this) == -1)
            {
                current.Lshift(1);
                denom.Lshift(1);
            }
            //If overflow.
            if (denom.Compare(this) == 1)
            {
                if (current.IsOne)
                {
                    if (neq)
                    {
                        Sub(mod);
                        negative = false;
                    }
                    return;
                }
                current.Rshift(1);
                denom.Rshift(1);
                while (!current.IsZero)
                {
                    int r = Compare(denom);
                    if (r == 1)
                    {
                        Sub(denom);
                    }
                    else if (r == 0)
                    {
                        break;
                    }
                    current.Rshift(1);
                    denom.Rshift(1);
                }
            }
            else
            {
                Clear();
            }
            if (neq)
            {
                Sub(mod);
                negative = false;
            }
        }
コード例 #2
0
        public void Div(GXBigInteger value)
        {
            GXBigInteger current = new GXBigInteger(1);
            GXBigInteger denom   = new GXBigInteger(value);
            GXBigInteger tmp     = new GXBigInteger(this);
            bool         neq     = negative;

            negative = false;
            try
            {
                // while denom < this.
                while (denom.Compare(this) == -1)
                {
                    current.Lshift(1);
                    denom.Lshift(1);
                }
                //If overflow.
                if (denom.Compare(this) == 1)
                {
                    if (current.IsOne)
                    {
                        Clear();
                        return;
                    }
                    Clear();
                    current.Rshift(1);
                    denom.Rshift(1);
                    while (!current.IsZero)
                    {
                        int r = tmp.Compare(denom);
                        if (r == 1)
                        {
                            tmp.Sub(denom);
                            Add(current);
                        }
                        else if (r == 0)
                        {
                            Add(current);
                            break;
                        }
                        current.Rshift(1);
                        denom.Rshift(1);
                    }
                    current.Data = Data;
                }
            }
            finally
            {
                negative = neq;
            }
            Data    = current.Data;
            changed = true;
        }
コード例 #3
0
        /// <summary>
        /// Multily elliptic curve point and scalar.
        /// </summary>
        /// <remarks>
        /// Y^2 = X^3 + A*X + B (mod p)
        /// </remarks>
        /// <param name="eccSize"></param>
        /// <param name="p">Point to multiply</param>
        /// <param name="n">Scalar to multiply</param>
        /// <param name="N">Elliptic curve order.</param>
        /// <param name="A"></param>
        /// <param name="P">Prime number</param>
        internal static GXEccPoint JacobianMultiply(GXEccPoint p, GXBigInteger n, GXBigInteger N, GXBigInteger A, GXBigInteger P)
        {
            GXBigInteger tmp;

            if (p.y.IsZero || n.IsZero)
            {
                return(new GXEccPoint(0, 0, 1));
            }
            if (n.IsOne)
            {
                return(p);
            }
            if (n.Compare(0) == -1 || n.Compare(N) != -1)
            {
                tmp = new GXBigInteger(n);
                tmp.Mod(N);
                return(JacobianMultiply(p, tmp, N, A, P));
            }
            if (n.IsEven)
            {
                tmp = new GXBigInteger(n);
                tmp.Rshift(1);
                return(JacobianDouble(JacobianMultiply(p, tmp, N, A, P), A, P));
            }
            tmp = new GXBigInteger(n);
            tmp.Rshift(1);
            GXEccPoint p2 = JacobianDouble(JacobianMultiply(p, tmp, N, A, P), A, P);

            JacobianAdd(p2, p, A, P);
            return(p2);
        }
コード例 #4
0
        /// <summary>
        /// Modulus.
        /// </summary>
        /// <param name="mod">Modulus.</param>
        public void Mod(GXBigInteger mod)
        {
            GXBigInteger current = new GXBigInteger(1);
            GXBigInteger denom   = new GXBigInteger(mod);

            //Shift UInt32 values to make this faster.
            if (denom.Count < Count - 1)
            {
                UInt32[] tmp = new UInt32[Count - denom.Count - 1];
                //Append UInt32 values.
                current.InsertRange(0, tmp);
                denom.InsertRange(0, tmp);
                current.changed = denom.changed = true;
            }
            bool neq = negative;

            negative = false;
            // while denom < this.
            while (denom.Compare(this) == -1)
            {
                current.Lshift(1);
                denom.Lshift(1);
            }
            //If overflow.
            if (denom.Compare(this) == 1)
            {
                if (current.IsOne)
                {
                    if (neq)
                    {
                        Sub(mod);
                        negative = false;
                    }
                    return;
                }
                current.Rshift(1);
                denom.Rshift(1);
                while (!current.IsZero)
                {
                    int r = Compare(denom);
                    if (r == 1)
                    {
                        Sub(denom);
                    }
                    else if (r == 0)
                    {
                        break;
                    }
                    current.Rshift(1);
                    denom.Rshift(1);
                }
            }
            else
            {
                Clear();
            }
            if (neq)
            {
                Sub(mod);
                negative = false;
            }
            changed = true;
        }
コード例 #5
0
        public void Div(GXBigInteger value)
        {
            GXBigInteger current = new GXBigInteger(1);
            GXBigInteger denom   = new GXBigInteger(value);
            GXBigInteger tmp     = new GXBigInteger(this);
            bool         neq     = negative;

            negative = false;
            try
            {
                //Shift UInt32 values to make this faster.
                if (denom.Count < Count - 1)
                {
                    UInt32[] tmp2 = new UInt32[Count - denom.Count - 1];
                    //Append UInt32 values.
                    current.InsertRange(0, tmp2);
                    denom.InsertRange(0, tmp2);
                    current.changed = denom.changed = true;
                }

                // while denom < this.
                while (denom.Compare(this) == -1)
                {
                    current.Lshift(1);
                    denom.Lshift(1);
                }
                //If overflow.
                if (denom.Compare(this) == 1)
                {
                    if (current.IsOne)
                    {
                        Clear();
                        return;
                    }
                    Clear();
                    current.Rshift(1);
                    denom.Rshift(1);
                    while (!current.IsZero)
                    {
                        int r = tmp.Compare(denom);
                        if (r == 1)
                        {
                            tmp.Sub(denom);
                            Add(current);
                        }
                        else if (r == 0)
                        {
                            Add(current);
                            break;
                        }
                        current.Rshift(1);
                        denom.Rshift(1);
                    }
                    current.Data = Data;
                }
            }
            finally
            {
                negative = neq;
            }
            Data    = current.Data;
            changed = true;
        }