Ejemplo n.º 1
0
        List <bool> BlumMicali(int size, int key)
        {
            MyBigType g     = new MyBigType("1347981406723692103108327603051596927581014069867");
            MyBigType p     = new MyBigType("1347981406723692103108327603051596927581014069863");
            MyBigType x0    = new MyBigType(key);
            MyBigType x     = new MyBigType(1);
            string    ccout = "x0 = " + Convert.ToString(x0);

            MessageBox.Show(ccout);
            List <bool> klucz = new List <bool>();

            for (int s = 0; s < size * 8; s++)
            {
                x = powermodulo(g, x0, p);
                if (x > (p - 1) / 2)
                {
                    klucz.Add(false);
                }
                else
                {
                    klucz.Add(true);
                }
                x0 = x;
            }
            return(klucz);
        }
Ejemplo n.º 2
0
        public static int Compare(MyBigType leftSide, MyBigType rightSide)
        {
            if (object.ReferenceEquals(leftSide, rightSide))
            {
                return(0);
            }

            if (object.ReferenceEquals(leftSide, null))
            {
                throw new ArgumentNullException("leftSide");
            }

            if (object.ReferenceEquals(rightSide, null))
            {
                throw new ArgumentNullException("rightSide");
            }

            if (leftSide > rightSide)
            {
                return(1);
            }
            if (leftSide == rightSide)
            {
                return(0);
            }
            return(-1);
        }
Ejemplo n.º 3
0
        List <bool> BlumMicali(int size)
        {
            var       random = new Random();
            MyBigType g      = new MyBigType("1347981406723692103108327603051596927581014069867");
            MyBigType p      = new MyBigType("1347981406723692103108327603051596927581014069863");

            x0i = random.Next(10, 500);
            MyBigType x0    = new MyBigType(x0i);
            MyBigType x     = new MyBigType(1);
            string    ccout = "x0 = " + Convert.ToString(x0);

            System.IO.File.WriteAllText(@".\ZaszyfrowanyKlucz.txt", Convert.ToString(x0));
            MessageBox.Show(ccout);
            List <bool> klucz = new List <bool>();

            for (int s = 0; s < size * 8; s++)
            {
                x = powermodulo(g, x0, p);
                if (x > (p - 1) / 2)
                {
                    klucz.Add(false);
                }
                else
                {
                    klucz.Add(true);
                }
                x0 = x;
            }
            return(klucz);
        }
Ejemplo n.º 4
0
        // Returns true if n is prime
        public static bool isPrime(MyBigType n)
        {
            // Corner cases
            if (n <= 1)
            {
                return(false);
            }
            if (n <= 3)
            {
                return(true);
            }

            // This is checked so that we can skip
            // middle five numbers in below loop
            if (n % 2 == 0 || n % 3 == 0)
            {
                return(false);
            }

            for (MyBigType i = 5; i *i <= n; i = i + 6)
            {
                if (n % i == 0 || n % (i + 2) == 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 5
0
        // Utility function to store prime factors of a number
        public static void findPrimefactors(HashSet <MyBigType> s, MyBigType n)
        {
            // Print the number of 2s that divide n
            while (n % 2 == 0)
            {
                s.Add(2);
                n = n / 2;
            }

            // n must be odd at this point. So we can skip
            // one element (Note i = i +2)
            for (int i = 3; i <= n.Pow(2); i = i + 2)
            {
                // While i divides n, print i and divide n
                while (n % i == 0)
                {
                    s.Add(i);
                    n = n / i;
                }
            }

            // This condition is to handle the case when
            // n is a prime number greater than 2
            if (n > 2)
            {
                s.Add(n);
            }
        }
Ejemplo n.º 6
0
        public static MyBigType Pow(MyBigType b, MyBigType power)
        {
            if (b == null)
            {
                throw new ArgumentNullException("b");
            }

            if (power == null)
            {
                throw new ArgumentNullException("power");
            }

            if (power < 0)
            {
                throw new ArgumentOutOfRangeException("power", "Currently negative exponents are not supported");
            }


            MyBigType result = 1;

            while (power != 0)
            {
                if ((power & 1) != 0)
                {
                    result *= b;
                }
                power >>= 1;
                b      *= b;
            }

            return(result);
        }
Ejemplo n.º 7
0
        public override bool Equals(object obj)
        {
            if (object.ReferenceEquals(obj, null))
            {
                return(false);
            }

            if (object.ReferenceEquals(this, obj))
            {
                return(true);
            }

            MyBigType c = (MyBigType)obj;

            if (this.m_digits.DataUsed != c.m_digits.DataUsed)
            {
                return(false);
            }

            for (int idx = 0; idx < this.m_digits.DataUsed; idx++)
            {
                if (this.m_digits[idx] != c.m_digits[idx])
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 8
0
        //// Returns true if n is prime
        //public static bool isPrime(MyBigType n)
        //{
        //    // Corner cases
        //    if (n <= 1)
        //    {
        //        return false;
        //    }
        //    if (n <= 3)
        //    {
        //        return true;
        //    }

        //    // This is checked so that we can skip
        //    // middle five numbers in below loop
        //    if (n % 2 == 0 || n % 3 == 0)
        //    {
        //        return false;
        //    }

        //    for (MyBigType i = 5; i * i <= n; i = i + 6)
        //    {
        //        if (n % i == 0 || n % (i + 2) == 0)
        //        {
        //            return false;
        //        }
        //    }

        //    return true;
        //}

        ///* Iterative Function to calculate (x^n)%p in
        //O(logy) */
        //public static MyBigType power(MyBigType x, MyBigType y, MyBigType p)
        //{
        //    MyBigType res = 1;     // Initialize result

        //    x = x % p; // Update x if it is more than or
        //               // equal to p

        //    while (y > 0)
        //    {
        //        // If y is odd, multiply x with result
        //        if (y % 2 == 1)
        //        {
        //            res = (res * x) % p;
        //        }

        //        // y must be even now
        //        y = y >> 1; // y = y/2
        //        x = (x * x) % p;
        //    }
        //    return res;
        //}

        //// Utility function to store prime factors of a number
        //public static void findPrimefactors(HashSet<MyBigType> s, MyBigType n)
        //{
        //    // Print the number of 2s that divide n
        //    while (n % 2 == 0)
        //    {
        //        s.Add(2);
        //        n = n / 2;
        //    }

        //    // n must be odd at this point. So we can skip
        //    // one element (Note i = i +2)
        //    for (int i = 3; i <= n.Pow(2); i = i + 2)
        //    {
        //        // While i divides n, print i and divide n
        //        while (n % i == 0)
        //        {
        //            s.Add(i);
        //            n = n / i;
        //        }
        //    }

        //    // This condition is to handle the case when
        //    // n is a prime number greater than 2
        //    if (n > 2)
        //    {
        //        s.Add(n);
        //    }
        //}

        //// Function to find smallest primitive root of n
        //public static MyBigType findPrimitive(MyBigType n)
        //{
        //    HashSet<MyBigType> s = new HashSet<MyBigType>();

        //    // Check if n is prime or not
        //    if (isPrime(n) == false)
        //    {
        //        return -1;
        //    }

        //    // Find value of Euler Totient function of n
        //    // Since n is a prime number, the value of Euler
        //    // Totient function is n-1 as there are n-1
        //    // relatively prime numbers.
        //    MyBigType phi = n - 1;

        //    // Find prime factors of phi and store in a set
        //    findPrimefactors(s, phi);

        //    // Check for every number from 2 to phi
        //    for (int r = 2; r <= phi; r++)
        //    {
        //        // Iterate through all prime factors of phi.
        //        // and check if we found a power with value 1
        //        bool flag = false;
        //        foreach (MyBigType a in s)
        //        {

        //            // Check if r^((phi)/primefactors) mod n
        //            // is 1 or not
        //            if (power(r, phi / (a), n) == 1)
        //            {
        //                flag = true;
        //                break;
        //            }
        //        }

        //        // If there was no power with value 1.
        //        if (flag == false)
        //        {
        //            return r;
        //        }
        //    }

        //    // If no primitive root found
        //    return -1;
        //}



        public static MyBigType RightShift(MyBigType leftSide, int shiftCount)
        {
            if (leftSide == null)
            {
                throw new ArgumentNullException("leftSide");
            }

            return(leftSide >> shiftCount);
        }
Ejemplo n.º 9
0
 public static MyBigType Abs(MyBigType leftSide)
 {
     if (object.ReferenceEquals(leftSide, null))
     {
         throw new ArgumentNullException("leftSide");
     }
     if (leftSide.IsNegative)
     {
         return(-leftSide);
     }
     return(leftSide);
 }
Ejemplo n.º 10
0
        //MyBigType powerTo(MyBigType x, MyBigType y, MyBigType p)
        //{
        //    MyBigType res = new MyBigType(1);
        //    x = x % p;
        //    MyBigType zero = new MyBigType(0);
        //    while (y > zero)
        //    {
        //        if ((y % 2) == 1)
        //        {
        //            res *= x;
        //            res = res % p;
        //        }
        //        x *= x;
        //        x = x % p;
        //        y /= 2;
        //    }
        //    return res;
        //}

        static MyBigType powermodulo(MyBigType x, MyBigType y, MyBigType p)
        {
            MyBigType res = 1;

            x = x % p;
            while (y > 0)
            {
                if ((y & 1) == 1)
                {
                    res = (res * x) % p;
                }
                y = y >> 1;
                x = (x * x) % p;
            }
            return(res);
        }
Ejemplo n.º 11
0
        // Function to find smallest primitive root of n
        public static MyBigType findPrimitive(MyBigType n)
        {
            HashSet <MyBigType> s = new HashSet <MyBigType>();

            // Check if n is prime or not
            if (isPrime(n) == false)
            {
                return(-1);
            }

            // Find value of Euler Totient function of n
            // Since n is a prime number, the value of Euler
            // Totient function is n-1 as there are n-1
            // relatively prime numbers.
            MyBigType phi = n - 1;

            // Find prime factors of phi and store in a set
            findPrimefactors(s, phi);

            // Check for every number from 2 to phi
            for (int r = 2; r <= phi; r++)
            {
                // Iterate through all prime factors of phi.
                // and check if we found a power with value 1
                bool flag = false;
                foreach (MyBigType a in s)
                {
                    // Check if r^((phi)/primefactors) mod n
                    // is 1 or not
                    if (power(r, phi / (a), n) == 1)
                    {
                        flag = true;
                        break;
                    }
                }

                // If there was no power with value 1.
                if (flag == false)
                {
                    return(r);
                }
            }

            // If no primitive root found
            return(-1);
        }
Ejemplo n.º 12
0
        private void Construct(string digits, int radix)
        {
            if (digits == null)
            {
                throw new ArgumentNullException("digits");
            }

            MyBigType multiplier = new MyBigType(1);
            MyBigType result     = new MyBigType();

            digits = digits.ToUpper(System.Globalization.CultureInfo.CurrentCulture).Trim();

            int nDigits = (digits[0] == '-' ? 1 : 0);

            for (int idx = digits.Length - 1; idx >= nDigits; idx--)
            {
                int d = (int)digits[idx];
                if (d >= '0' && d <= '9')
                {
                    d -= '0';
                }
                else if (d >= 'A' && d <= 'Z')
                {
                    d = (d - 'A') + 10;
                }
                else
                {
                    throw new ArgumentOutOfRangeException("digits");
                }

                if (d >= radix)
                {
                    throw new ArgumentOutOfRangeException("digits");
                }
                result     += (multiplier * d);
                multiplier *= radix;
            }

            if (digits[0] == '-')
            {
                result = -result;
            }

            this.m_digits = result.m_digits;
        }
Ejemplo n.º 13
0
        private static void Divide(MyBigType leftSide, MyBigType rightSide, out MyBigType quotient, out MyBigType remainder)
        {
            if (leftSide.IsZero)
            {
                quotient  = new MyBigType();
                remainder = new MyBigType();
                return;
            }

            if (rightSide.m_digits.DataUsed == 1)
            {
                SingleDivide(leftSide, rightSide, out quotient, out remainder);
            }
            else
            {
                MultiDivide(leftSide, rightSide, out quotient, out remainder);
            }
        }
Ejemplo n.º 14
0
        private static void SingleDivide(MyBigType leftSide, MyBigType rightSide, out MyBigType quotient, out MyBigType remainder)
        {
            if (rightSide.IsZero)
            {
                throw new DivideByZeroException();
            }

            ArrayForDigits remainderDigits = new ArrayForDigits(leftSide.m_digits);

            remainderDigits.ResetDataUsed();

            int   pos      = remainderDigits.DataUsed - 1;
            ulong divisor  = (ulong)rightSide.m_digits[0];
            ulong dividend = (ulong)remainderDigits[pos];

            unsignIn[] result = new unsignIn[leftSide.m_digits.Count];
            leftSide.m_digits.CopyTo(result, 0, result.Length);
            int resultPos = 0;

            if (dividend >= divisor)
            {
                result[resultPos++]  = (unsignIn)(dividend / divisor);
                remainderDigits[pos] = (unsignIn)(dividend % divisor);
            }
            pos--;

            while (pos >= 0)
            {
                dividend                 = ((ulong)(remainderDigits[pos + 1]) << ArrayForDigits.DataSizeBits) + (ulong)remainderDigits[pos];
                result[resultPos++]      = (unsignIn)(dividend / divisor);
                remainderDigits[pos + 1] = 0;
                remainderDigits[pos--]   = (unsignIn)(dividend % divisor);
            }
            remainder = new MyBigType(remainderDigits);

            ArrayForDigits quotientDigits = new ArrayForDigits(resultPos + 1, resultPos);
            int            j = 0;

            for (int i = quotientDigits.DataUsed - 1; i >= 0; i--, j++)
            {
                quotientDigits[j] = result[i];
            }
            quotient = new MyBigType(quotientDigits);
        }
Ejemplo n.º 15
0
        public MyBigType findPrime(MyBigType a)
        {
            bool      isPrime = true;
            MyBigType i       = new MyBigType(2);

            for (; ; a++)
            {
                while (i < a && isPrime == true)
                {
                    if (a % i == 0)
                    {
                        isPrime = false;
                    }
                    i++;
                }
                i       = 2;
                isPrime = true;
            }
        }
Ejemplo n.º 16
0
        public static MyBigType operator *(MyBigType leftSide, MyBigType rightSide)
        {
            if (object.ReferenceEquals(leftSide, null))
            {
                throw new ArgumentNullException("leftSide");
            }
            if (object.ReferenceEquals(rightSide, null))
            {
                throw new ArgumentNullException("rightSide");
            }

            bool leftSideNeg  = leftSide.IsNegative;
            bool rightSideNeg = rightSide.IsNegative;

            leftSide  = Abs(leftSide);
            rightSide = Abs(rightSide);

            ArrayForDigits da = new ArrayForDigits(leftSide.m_digits.DataUsed + rightSide.m_digits.DataUsed);

            da.DataUsed = da.Count;

            for (int i = 0; i < leftSide.m_digits.DataUsed; i++)
            {
                ulong carry = 0;
                for (int j = 0, k = i; j < rightSide.m_digits.DataUsed; j++, k++)
                {
                    ulong val = ((ulong)leftSide.m_digits[i] * (ulong)rightSide.m_digits[j]) + (ulong)da[k] + carry;

                    da[k] = (unsignIn)(val & ArrayForDigits.AllBits);
                    carry = (val >> ArrayForDigits.DataSizeBits);
                }

                if (carry != 0)
                {
                    da[i + rightSide.m_digits.DataUsed] = (unsignIn)carry;
                }
            }

            MyBigType result = new MyBigType(da);

            return(leftSideNeg != rightSideNeg ? -result : result);
        }
Ejemplo n.º 17
0
        /* Iterative Function to calculate (x^n)%p in
         * O(logy) */
        public static MyBigType power(MyBigType x, MyBigType y, MyBigType p)
        {
            MyBigType res = 1; // Initialize result

            x = x % p;         // Update x if it is more than or
                               // equal to p

            while (y > 0)
            {
                // If y is odd, multiply x with result
                if (y % 2 == 1)
                {
                    res = (res * x) % p;
                }

                // y must be even now
                y = y >> 1; // y = y/2
                x = (x * x) % p;
            }
            return(res);
        }
Ejemplo n.º 18
0
        public string ToString(int radix)
        {
            if (radix < 2 || radix > 36)
            {
                throw new ArgumentOutOfRangeException("radix");
            }

            if (IsZero)
            {
                return("0");
            }

            MyBigType a        = this;
            bool      negative = a.IsNegative;

            a = Abs(this);

            MyBigType quotient;
            MyBigType remainder;
            MyBigType biRadix = new MyBigType(radix);

            const string charSet = "0123456789abcdefghijklmnopqrstuvwxyz";

            System.Collections.ArrayList al = new System.Collections.ArrayList();
            while (a.m_digits.DataUsed > 1 || (a.m_digits.DataUsed == 1 && a.m_digits[0] != 0))
            {
                Divide(a, biRadix, out quotient, out remainder);
                al.Insert(0, charSet[(int)remainder.m_digits[0]]);
                a = quotient;
            }

            string result = new String((char[])al.ToArray(typeof(char)));

            if (radix == 10 && negative)
            {
                return("-" + result);
            }
            return(result);
        }
Ejemplo n.º 19
0
 public MyBigType Pow(MyBigType power)
 {
     return(Pow(this, power));
 }
Ejemplo n.º 20
0
 public static MyBigType Subtract(MyBigType leftSide, MyBigType rightSide)
 {
     return(leftSide - rightSide);
 }
Ejemplo n.º 21
0
 public MyBigType(MyBigType m)
 {
     m_digits = m.m_digits;
 }
Ejemplo n.º 22
0
        private static void MultiDivide(MyBigType leftSide, MyBigType rightSide, out MyBigType quotient, out MyBigType remainder)
        {
            if (rightSide.IsZero)
            {
                throw new DivideByZeroException();
            }

            unsignIn val = rightSide.m_digits[rightSide.m_digits.DataUsed - 1];
            int      d   = 0;

            for (uint mask = ArrayForDigits.HiBitSet; mask != 0 && (val & mask) == 0; mask >>= 1)
            {
                d++;
            }

            int remainderLen = leftSide.m_digits.DataUsed + 1;

            unsignIn[] remainderDat = new unsignIn[remainderLen];
            leftSide.m_digits.CopyTo(remainderDat, 0, leftSide.m_digits.DataUsed);

            ArrayForDigits.ShiftLeft(remainderDat, d);
            rightSide = rightSide << d;

            ulong firstDivisor  = rightSide.m_digits[rightSide.m_digits.DataUsed - 1];
            ulong secondDivisor = (rightSide.m_digits.DataUsed < 2 ? (unsignIn)0 : rightSide.m_digits[rightSide.m_digits.DataUsed - 2]);

            int            divisorLen   = rightSide.m_digits.DataUsed + 1;
            ArrayForDigits dividendPart = new ArrayForDigits(divisorLen, divisorLen);

            unsignIn[] result    = new unsignIn[leftSide.m_digits.Count + 1];
            int        resultPos = 0;

            ulong carryBit = (ulong)0x1 << ArrayForDigits.DataSizeBits; // 0x100000000

            for (int j = remainderLen - rightSide.m_digits.DataUsed, pos = remainderLen - 1; j > 0; j--, pos--)
            {
                ulong dividend = ((ulong)remainderDat[pos] << ArrayForDigits.DataSizeBits) + (ulong)remainderDat[pos - 1];
                ulong qHat     = (dividend / firstDivisor);
                ulong rHat     = (dividend % firstDivisor);

                while (pos >= 2)
                {
                    if (qHat == carryBit || (qHat * secondDivisor) > ((rHat << ArrayForDigits.DataSizeBits) + remainderDat[pos - 2]))
                    {
                        qHat--;
                        rHat += firstDivisor;
                        if (rHat < carryBit)
                        {
                            continue;
                        }
                    }
                    break;
                }

                for (int h = 0; h < divisorLen; h++)
                {
                    dividendPart[divisorLen - h - 1] = remainderDat[pos - h];
                }

                MyBigType dTemp = new MyBigType(dividendPart);
                MyBigType rTemp = rightSide * (long)qHat;
                while (rTemp > dTemp)
                {
                    qHat--;
                    rTemp -= rightSide;
                }

                rTemp = dTemp - rTemp;
                for (int h = 0; h < divisorLen; h++)
                {
                    remainderDat[pos - h] = rTemp.m_digits[rightSide.m_digits.DataUsed - h];
                }

                result[resultPos++] = (unsignIn)qHat;
            }

            Array.Reverse(result, 0, resultPos);
            quotient = new MyBigType(new ArrayForDigits(result));

            int            n   = ArrayForDigits.ShiftRight(remainderDat, d);
            ArrayForDigits rDA = new ArrayForDigits(n, n);

            rDA.CopyFrom(remainderDat, 0, 0, rDA.DataUsed);
            remainder = new MyBigType(rDA);
        }
Ejemplo n.º 23
0
 public int CompareTo(MyBigType value)
 {
     return(Compare(this, value));
 }