Пример #1
0
        internal static void Subtract(System.Security.Cryptography.BigInt a, System.Security.Cryptography.BigInt b, ref System.Security.Cryptography.BigInt c)
        {
            byte num  = 0;
            int  num2 = 0;

            if (a < b)
            {
                Subtract(b, a, ref c);
                Negate(ref c);
            }
            else
            {
                int index = 0;
                int size  = a.Size;
                int num5  = 0;
                for (index = 0; index < size; index++)
                {
                    num2 = (a.GetDigit(index) - b.GetDigit(index)) - num;
                    num  = 0;
                    if (num2 < 0)
                    {
                        num2 += 0x100;
                        num   = 1;
                    }
                    c.SetDigit(index, (byte)(num2 & 0xff), ref num5);
                }
                c.Size = num5;
            }
        }
Пример #2
0
 private static void Multiply(System.Security.Cryptography.BigInt a, int b, ref System.Security.Cryptography.BigInt c)
 {
     if (b == 0)
     {
         c.Clear();
     }
     else
     {
         int num  = 0;
         int num2 = 0;
         int size = a.Size;
         int num4 = 0;
         for (int i = 0; i < size; i++)
         {
             num2 = (b * a.GetDigit(i)) + num;
             num  = num2 / 0x100;
             c.SetDigit(i, (byte)(num2 % 0x100), ref num4);
         }
         if (num != 0)
         {
             byte[] bytes = BitConverter.GetBytes(num);
             for (int j = 0; j < bytes.Length; j++)
             {
                 c.SetDigit(size + j, bytes[j], ref num4);
             }
         }
         c.Size = num4;
     }
 }
Пример #3
0
 internal static void Divide(System.Security.Cryptography.BigInt numerator, System.Security.Cryptography.BigInt denominator, ref System.Security.Cryptography.BigInt quotient, ref System.Security.Cryptography.BigInt remainder)
 {
     if (numerator < denominator)
     {
         quotient.Clear();
         remainder.CopyFrom(numerator);
     }
     else if (numerator == denominator)
     {
         quotient.Clear();
         quotient.SetDigit(0, 1);
         remainder.Clear();
     }
     else
     {
         System.Security.Cryptography.BigInt a = new System.Security.Cryptography.BigInt();
         a.CopyFrom(numerator);
         System.Security.Cryptography.BigInt num2 = new System.Security.Cryptography.BigInt();
         num2.CopyFrom(denominator);
         uint num3 = 0;
         while (num2.Size < a.Size)
         {
             num2.Multiply(0x100);
             num3++;
         }
         if (num2 > a)
         {
             num2.Divide(0x100);
             num3--;
         }
         int num4  = 0;
         int digit = 0;
         int b     = 0;
         System.Security.Cryptography.BigInt c = new System.Security.Cryptography.BigInt();
         quotient.Clear();
         for (int i = 0; i <= num3; i++)
         {
             num4  = (a.Size == num2.Size) ? a.GetDigit(a.Size - 1) : ((0x100 * a.GetDigit(a.Size - 1)) + a.GetDigit(a.Size - 2));
             digit = num2.GetDigit(num2.Size - 1);
             b     = num4 / digit;
             if (b >= 0x100)
             {
                 b = 0xff;
             }
             Multiply(num2, b, ref c);
             while (c > a)
             {
                 b--;
                 Multiply(num2, b, ref c);
             }
             quotient.Multiply(0x100);
             Add(quotient, (byte)b, ref quotient);
             Subtract(a, c, ref a);
             num2.Divide(0x100);
         }
         remainder.CopyFrom(a);
     }
 }
Пример #4
0
        internal void FromDecimal(string decNum)
        {
            System.Security.Cryptography.BigInt a = new System.Security.Cryptography.BigInt();
            System.Security.Cryptography.BigInt c = new System.Security.Cryptography.BigInt();
            int length = decNum.Length;

            for (int i = 0; i < length; i++)
            {
                if ((decNum[i] <= '9') && (decNum[i] >= '0'))
                {
                    Multiply(a, 10, ref c);
                    Add(c, (byte)(decNum[i] - '0'), ref a);
                }
            }
            this.CopyFrom(a);
        }
Пример #5
0
        internal static void Negate(ref System.Security.Cryptography.BigInt a)
        {
            int size = 0;

            for (int i = 0; i < 0x80; i++)
            {
                a.SetDigit(i, (byte)(~a.GetDigit(i) & 0xff), ref size);
            }
            for (int j = 0; j < 0x80; j++)
            {
                a.SetDigit(j, (byte)(a.GetDigit(j) + 1), ref size);
                if ((a.GetDigit(j) & 0xff) != 0)
                {
                    break;
                }
                a.SetDigit(j, (byte)(a.GetDigit(j) & 0xff), ref size);
            }
            a.Size = size;
        }
Пример #6
0
        internal static void Add(System.Security.Cryptography.BigInt a, byte b, ref System.Security.Cryptography.BigInt c)
        {
            byte digit = b;
            int  num2  = 0;
            int  size  = a.Size;
            int  num4  = 0;

            for (int i = 0; i < size; i++)
            {
                num2 = a.GetDigit(i) + digit;
                c.SetDigit(i, (byte)(num2 & 0xff), ref num4);
                digit = (byte)((num2 >> 8) & 0xff);
            }
            if (digit != 0)
            {
                c.SetDigit(a.Size, digit, ref num4);
            }
            c.Size = num4;
        }
Пример #7
0
        internal string ToDecimal()
        {
            if (this.IsZero())
            {
                return("0");
            }
            System.Security.Cryptography.BigInt denominator = new System.Security.Cryptography.BigInt(10);
            System.Security.Cryptography.BigInt numerator   = new System.Security.Cryptography.BigInt();
            System.Security.Cryptography.BigInt quotient    = new System.Security.Cryptography.BigInt();
            System.Security.Cryptography.BigInt remainder   = new System.Security.Cryptography.BigInt();
            numerator.CopyFrom(this);
            char[] array  = new char[(int)Math.Ceiling((double)((this.m_size * 2) * 1.21))];
            int    length = 0;

            do
            {
                Divide(numerator, denominator, ref quotient, ref remainder);
                array[length++] = decValues[remainder.IsZero() ? 0 : remainder.m_elements[0]];
                numerator.CopyFrom(quotient);
            }while (!quotient.IsZero());
            Array.Reverse(array, 0, length);
            return(new string(array, 0, length));
        }
Пример #8
0
 internal void CopyFrom(System.Security.Cryptography.BigInt a)
 {
     Array.Copy(a.m_elements, this.m_elements, 0x80);
     this.m_size = a.m_size;
 }