Example #1
0
    def test_big_integer_to_ba(self):
        b1 = BigInteger(8972340892734890723)
        ba = b1.ToByteArray()

        integer = BigInteger.from_bytes(ba, 'little')
        self.assertEqual(integer, 8972340892734890723)

        b2 = BigInteger(-100)
        b2ba = b2.ToByteArray()
        integer2 = BigInteger.from_bytes(b2ba, 'little', signed=True)
        self.assertEqual(integer2, -100)

        b3 = BigInteger(128)
        b3ba = b3.ToByteArray()
        self.assertEqual(b3ba, b'\x80\x00')

        b4 = BigInteger(0)
        b4ba = b4.ToByteArray()
        self.assertEqual(b4ba, b'\x00')

        b5 = BigInteger(-146)
        b5ba = b5.ToByteArray()
        self.assertEqual(b'\x6e\xff', b5ba)

        b6 = BigInteger(-48335248028225339427907476932896373492484053930)
        b6ba = b6.ToByteArray()
        self.assertEqual(20, len(b6ba))

        b7 = BigInteger(-399990000)
        b7ba = b7.ToByteArray()
        self.assertEqual(b'\x10\xa3\x28\xe8', b7ba)
Example #2
0
    def test_big_integer_to_ba(self):
        b1 = BigInteger(8972340892734890723)
        ba = b1.ToByteArray()

        integer = BigInteger.from_bytes(ba, 'little')
        self.assertEqual(integer, 8972340892734890723)

        b2 = BigInteger(-100)
        b2ba = b2.ToByteArray()
        integer2 = BigInteger.from_bytes(b2ba, 'little')
        self.assertEqual(integer2, 65436)

        b3 = BigInteger(128)
        b3ba = b3.ToByteArray()
        self.assertEqual(b3ba, b'\x80\x00')

        b4 = BigInteger(0)
        b4ba = b4.ToByteArray()
        self.assertEqual(b4ba, b'\x00')
Example #3
0
    def test_big_integer_modulo(self):
        b1 = BigInteger(860593)
        b2 = BigInteger(-201)
        self.assertEqual(112, b1 % b2)

        b1 = BigInteger(20195283520469175757)
        b2 = BigInteger(1048576)
        self.assertEqual(888269, b1 % b2)

        b1 = BigInteger(
            -18224909727634776050312394179610579601844989529623334093909233530432892596607
        )
        b2 = BigInteger(14954691977398614017)
        self.assertEqual(-3100049211437790421, b1 % b2)

        b3 = BigInteger.from_bytes(bytearray(b'+K\x05\xbe\xaai\xfa\xd4'),
                                   'little',
                                   signed=True)
        self.assertEqual(b3, b1 % b2)