Exemple #1
0
    public BF_BInteger Mul(BF_BInteger BI1)
    {
        BF_BInteger res = Zero();

        BF_BInteger[] mul_cache = new BF_BInteger[BYTE_COUNT * 8];
        mul_cache[0] = this.Duplicate();
        for (int i = 1; i < BYTE_COUNT * 8 / 2; i++)
        {
            mul_cache[i] = mul_cache[i - 1].Add(mul_cache[i - 1]);
        }

        uint temp_byte    = 0;
        uint temp_bit     = 0;
        int  number_index = 0;

        for (int i = 0; i < BYTE_COUNT * 8 / 2; i++)
        {
            number_index = BYTE_COUNT - 1 - (i / 8);
            temp_byte    = (uint)(BI1.numbers[number_index]);
            temp_bit     = (uint)(temp_byte & (1 << (i % 8)));
            if (temp_bit != 0)
            {
                res = res.Add(mul_cache[i]);
            }
        }


        return(res);
    }
Exemple #2
0
    public BF_BInteger Duplicate()
    {
        BF_BInteger res = new BF_BInteger();

        for (int i = 0; i < BYTE_COUNT; i++)
        {
            res.numbers[i] = numbers[i];
        }

        return(res);
    }
Exemple #3
0
    public BF_BInteger Zero()
    {
        BF_BInteger res = new BF_BInteger();

        for (int i = 0; i < BYTE_COUNT; i++)
        {
            res.numbers[i] = 0;
        }

        return(res);
    }
Exemple #4
0
        static void Main(string[] args)
        {
            BF_BInteger bi1 = new BF_BInteger();

            bi1.FromHex("ABCD12345");
            BF_BInteger bi2 = new BF_BInteger();

            bi2.FromHex("ABCD12345");
            BF_BInteger bi3 = bi1.Mul(bi2);
            BF_BInteger bi4 = bi3.Div(bi2);

            Console.WriteLine(bi4.ToString());
        }
Exemple #5
0
    public int Compare(BF_BInteger BI1)
    {
        for (int i = 0; i < BYTE_COUNT; i++)
        {
            if (this.numbers[i] > BI1.numbers[i])
            {
                return(1);
            }
            if (this.numbers[i] < BI1.numbers[i])
            {
                return(-1);
            }
        }

        return(0);
    }
Exemple #6
0
    public BF_BInteger  Add(BF_BInteger BI1)
    {
        BF_BInteger res                 = new BF_BInteger();
        uint        temp_byte           = 0;
        uint        lower_byte_overflow = 0;
        int         number_index        = 0;

        for (int i = 0; i < BYTE_COUNT; i++)
        {
            number_index = BYTE_COUNT - 1 - i;
            temp_byte    = this.numbers[number_index] + BI1.numbers[number_index] + lower_byte_overflow;
            res.numbers[number_index] = (uint)temp_byte % 256;
            lower_byte_overflow       = temp_byte / 256;
        }

        return(res);
    }
Exemple #7
0
    public BF_BInteger Neg()
    {
        BF_BInteger res       = new BF_BInteger();
        byte        temp_byte = 0;

        int number_index = 0;

        for (int i = 0; i < BYTE_COUNT; i++)
        {
            number_index = BYTE_COUNT - 1 - i;
            temp_byte    = (byte)this.numbers[number_index];
            temp_byte    = (byte)~temp_byte;
            res.numbers[number_index] = temp_byte;
        }

        return(res);
    }
Exemple #8
0
    public BF_BInteger Div(BF_BInteger BI1)
    {
        BF_BInteger res = Zero();

        BF_BInteger[] mul_cache = new BF_BInteger[BYTE_COUNT * 8];
        mul_cache[0] = BI1.Duplicate();
        for (int i = 1; i < BYTE_COUNT * 8 / 2; i++)
        {
            mul_cache[i] = mul_cache[i - 1].Add(mul_cache[i - 1]);
        }
        BF_BInteger temp_div        = Zero();
        BF_BInteger temptest        = Zero();
        uint        temp_byte       = 0;
        uint        temp_bit        = 0;
        int         number_index    = 0;
        int         cache_bit_index = 0;
        int         bit_index       = 0;

        for (int i = 0; i < BYTE_COUNT * 8 / 2; i++)
        {
            cache_bit_index = BYTE_COUNT * 8 / 2 - 1 - i;
            temptest        = temp_div.Add(mul_cache[cache_bit_index]);
            if (temptest.Compare(this) <= 0)
            {
                temp_div     = temptest.Duplicate();
                bit_index    = BYTE_COUNT * 8 / 2 + i;
                number_index = bit_index / 8;
                temp_byte    = (uint)res.numbers[number_index];
                temp_byte    = (uint)(temp_byte | (0x80 >> (i % 8)));
                res.numbers[number_index] = (uint)temp_byte % 256;
            }
        }


        return(res);
    }