def testDividePositive1(self):
     print("Positive Division Test 1:")
     a = big.int2bytearray(120, 8)
     b = big.int2bytearray(4, 8)
     c = divide(a, b)
     ans = big.bytearray2int(c)
     printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
     print(printstatement)
     # print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
     self.assertEqual(ans, 120/4)
 def testModulo5(self):
     print("Modulo Test 5:")
     a = big.int2bytearray(1234567890)
     b = big.int2bytearray(2)
     c = modulo(a, b)
     ans = big.bytearray2int(c)
     printstatement = "\n" + str(big.bytearray2int(a)) + "%" + str(big.bytearray2int(b)) + "=" + str(ans)
     print(printstatement)
     # print("\n", big.bytearray2int(a), "%", big.bytearray2int(b), "=", ans)
     self.assertEqual(ans, 1234567890%2)
 def testModulo4(self):
     print("Modulo Test 4:")
     a = big.int2bytearray(3, 8)
     b = big.int2bytearray(-4, 8)
     c = modulo(a, b)
     ans = big.bytearray2int(c)
     printstatement = "\n" + str(big.bytearray2int(a)) + "%" + str(big.bytearray2int(b)) + "=" + str(ans)
     print(printstatement)
     # print("\n", big.bytearray2int(a), "%", big.bytearray2int(b), "=", ans)
     self.assertEqual(ans, 3%-4)
 def testDivideSignExt1(self):
     print("Sign Extension Division Test 1:")
     a = big.int2bytearray(1234567890)
     b = big.int2bytearray(2)
     c = divide(a, b)
     ans = big.bytearray2int(c)
     printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
     print(printstatement)
     # print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
     self.assertEqual(ans, 1234567890/2)
 def testDivideNegative4(self):
     print("Negative Division Test 4:")
     a = big.int2bytearray(-15, 8)
     b = big.int2bytearray(-5, 8)
     c = divide(a, b)
     ans = big.bytearray2int(c)
     printstatement = "\n" + str(big.bytearray2int(a)) + "/" + str(big.bytearray2int(b)) + "=" + str(ans)
     print(printstatement)
     # print("\n", big.bytearray2int(a), "/", big.bytearray2int(b), "=", ans)
     self.assertEqual(ans, -15/-5)
    def testExpMod3(self):
        print("Exponential Modulo Test 3:")
        a = 12
        aArray = big.int2bytearray(12, 8)

        b = 17
        bArray = big.int2bytearray(17, 8)

        n = 7
        nArray = big.int2bytearray(7, 8)

        c = moduloExp(aArray, bArray, nArray)
        ans = big.bytearray2int(c)
        printstatement = "\n" + str(a) + "^" + str(b) + "%" + str(n) + "=" + str(ans)
        print(printstatement)
        # print("\n", a, "^", b, "%", n, "=", ans)
        self.assertEqual(ans, pow(a, b, n))
    def testExpMod2(self):
        print("Exponential Modulo Test 2:")
        a = 9
        aArray = big.int2bytearray(9, 8)

        b = 3
        bArray = big.int2bytearray(3, 8)

        n = 4
        nArray = big.int2bytearray(4, 8)

        c = moduloExp(aArray, bArray, nArray)
        ans = big.bytearray2int(c)
        printstatement = "\n" + str(a) + "^" + str(b) + "%" + str(n) + "=" + str(ans)
        print(printstatement)
        # print("\n", a, "^", b, "%", n, "=", ans)
        self.assertEqual(ans, pow(a, b, n))
    def testExpMod1(self):
        print("Exponential Modulo Test 1:")
        a = 5
        aArray = big.int2bytearray(5, 8)

        b = 756
        bArray = big.int2bytearray(756, 8)

        n = 25
        nArray = big.int2bytearray(25, 8)

        c = moduloExp(aArray, bArray, nArray)
        ans = big.bytearray2int(c)
        printstatement = "\n" + str(a) + "^" + str(b) + "%" + str(n) + "=" + str(ans)
        print(printstatement)
        # print("\n", a, "^", b, "%", n, "=", ans)
        self.assertEqual(ans, pow(a, b, n))
def moduloExp(a, b, n):
    zero = bytearray(len(b))
    if (big.equal(b, zero)):
        return big.int2bytearray(1, len(b))

    z = moduloExp(a, big.half(b), n)
    if (big.is_even(b)):
        return modulo(big.multiply(z, z), n)
    else:
        return modulo(big.multiply(a, big.multiply(z, z)), n)
Esempio n. 10
0
def incrementByOne(number):
    return big.add(number, big.int2bytearray(1, len(number)))