Esempio n. 1
0
    def mod_sub(self, other, m):
        """ Returns the difference of self and other modulo m.

        Example:
            >>> Bn(10).mod_sub(Bn(2), Bn(11))
            Bn(8)
        """

        r = Bn()
        _C.bn_sub(r.bn, self.bn, other.bn)
        _C.bn_mod(r.bn, r.bn, m.bn)
        return r
Esempio n. 2
0
    def mod_mul(self, other, m):
        """ Return the product of self and other modulo m.

        Example:
            >>> Bn(10).mod_mul(Bn(2), Bn(11))
            Bn(9)
        """

        r = Bn()
        _C.bn_mul(r.bn, self.bn, other.bn)
        _C.bn_mod(r.bn, r.bn, m.bn)
        return r
Esempio n. 3
0
    def mod_add(self, other, m):
        """ Returns the sum of self and other modulo m.

        Example:
            >>> Bn(10).mod_add(2, 11)
            Bn(1)
            >>> Bn(10).mod_add(Bn(2), Bn(11))
            Bn(1)
        """

        r = Bn()
        _C.bn_add(r.bn, self.bn, other.bn)
        _C.bn_mod(r.bn, r.bn, m.bn)
        return r
Esempio n. 4
0
    def mod_inverse(self, m):
        """ Compute the inverse mod m, such that self * res == 1 mod m.

        Example:
            >>> Bn(10).mod_inverse(m = Bn(11))
            Bn(10)
            >>> Bn(10).mod_mul(Bn(10), m = Bn(11)) == Bn(1)
            True
        """
        gcd = Bn()
        inv = Bn()
        _C.bn_gcd_ext(gcd.bn, inv.bn, _FFI.NULL, self.bn, m.bn)
        _C.bn_mod(inv.bn, inv.bn, m.bn)

        if gcd != Bn(1):
            raise Exception("No inverse for ", self, "modulo ", m)

        return inv
Esempio n. 5
0
 def __mod__(self, other):
     rem = Bn()
     _C.bn_mod(rem.bn, self.bn, other.bn)
     return rem