Ejemplo n.º 1
0
        public BigNum LongPowerWindow(BigNum a, BigNum b)
        {
            Dictionary <char, int> HexToDecimal = new Dictionary <char, int>
            {
                { '0', 0 }, { '1', 1 }, { '2', 2 }, { '3', 3 }, { '4', 4 }, { '5', 5 }, { '6', 6 }, { '7', 7 }, { '8', 8 }, { '9', 9 }, { 'A', 10 }, { 'B', 11 }, { 'C', 12 }, { 'D', 13 }, { 'E', 14 }, { 'F', 15 }
            };
            var Bstring = b.ToString();

            var result = new BigNum("1");

            if (Compare(a, Zero) == 0)
            {
                return(Zero);
            }
            if (Compare(b, Zero) == 0)
            {
                return(One);
            }

            BigNum[] DegreesOfTwo = new BigNum[16];

            DegreesOfTwo[0] = new BigNum("1");
            DegreesOfTwo[1] = a;

            for (int k = 2; k < DegreesOfTwo.Length; k++)
            {
                DegreesOfTwo[k] = Multiplication(DegreesOfTwo[k - 1], a);
            }

            for (int i = 0; i < Bstring.Length; i++)
            {
                var x = Bstring[i];
                result = Multiplication(result, DegreesOfTwo[HexToDecimal[Bstring[i]]]);
                if (i != Bstring.Length - 1)
                {
                    for (int j = 1; j <= 4; j++)
                    {
                        result = Multiplication(result, result);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public BigNum[] LongDiv(BigNum a, BigNum b)
        {
            int    k = Bitlen(b);
            int    t = 0;
            BigNum r = new BigNum(a.ToString());
            BigNum q = new BigNum(1);
            BigNum c = new BigNum(1);

            while (Compare(r, b) >= 0)
            {
                t = Bitlen(r);
                c = ShiftBitsToHigh(b, t - k);
                if (Compare(r, c) == -1)
                {
                    t--;
                    c = ShiftBitsToHigh(b, t - k);
                }
                r = Substraction(r, c);
                q = Addition(q, ShiftBitsToHigh(One, t - k));
            }

            BigNum[] result = new BigNum[] { q, r };
            return(result);
        }