def __truediv__(self, other): if isinstance(other, FixedHomoFloat): if self.k - other.k < self.max_k and self.k - other.k >= 0: k1 = self.k - other.k M1 = hec.div(self.M, other.M) else: raise ValueError('Wrong k') elif is_builtin(other): M1 = hee.encrypttopoly(int(other * (10 ** self.k)), self.__key) M1 = hec.div(self.M, M1) k1 = 0 else: raise TypeError('Unknown type for division') return FixedHomoFloat(0, k=k1, M=M1, key=self.__key)
def __sub__(self, other): if isinstance(other, FixedHomoFloat): diff_k = self.k - other.k neg = False if diff_k < 0: neg = True diff_k *= -1 M1 = hee.encrypttopoly(int((10 ** diff_k)), self.__key) if not neg: M1 = hec.mul(other.M, M1) M1 = hec.sub(self.M, M1) k1 = self.k else: M1 = hec.mul(self.M, M1) if self.k + diff_k < self.max_k and self.k + diff_k >= 0: M1 = hec.sub(M1, other.M) k1 = self.k + diff_k elif self.k + diff_k > self.max_k: k1 = self.max_k - 1 M1 = hec.sub(M1, other.M) C1 = hee.encrypttopoly(int((10 ** (self.k + diff_k - self.max_k + 1))), self.__key) M1 = hec.div(M1, C1) elif is_builtin(other): M1 = hee.encrypttopoly(int(other * (10 ** self.k)), self.__key) M1 = hec.sub(self.M, M1) k1 = self.k else: raise TypeError('Unknown type for substraction') return FixedHomoFloat(0, k=k1, M=M1, key=self.__key)
def __rtruediv__(self, other): if is_builtin(other): M1 = hee.encrypttopoly(int(other * (10 ** self.k)), self.__key) M1 = hec.div(M1, self.M) k1 = 0 else: raise TypeError('Unknown type for division') return FixedHomoFloat(0, k=0, M=M1, key=self.__key)
def __mul__(self, other): if isinstance(other, FixedHomoFloat): if self.k + other.k < self.max_k and self.k + other.k >= 0: M1 = hec.mul(self.M, other.M) k1 = self.k + other.k elif self.k + other.k > self.max_k: k1 = self.max_k - 1 M1 = hec.mul(self.M, other.M) C1 = hee.encrypttopoly(int((10 ** (self.k + other.k - self.max_k + 1))), self.__key) M1 = hec.div(M1, C1) elif is_builtin(other): if self.k * 2 < self.max_k and self.k * 2 >= 0: M1 = hee.encrypttopoly(int(other * (10 ** self.k)), self.__key) M1 = hec.mul(self.M, M1) k1 = self.k * 2 elif self.k * 2 > self.max_k: M1 = hee.encrypttopoly(int(other * (10 ** self.k)), self.__key) k1 = self.max_k - 1 M1 = hec.mul(self.M, M1) C1 = hee.encrypttopoly(int((10 ** (2 * self.k - self.max_k + 1))), self.__key) M1 = hec.div(M1, C1) else: raise TypeError('Unknown type for multiplication') return FixedHomoFloat(0, k=k1, M=M1, key=self.__key)