Ejemplo n.º 1
0
 def __init__(self, degree, public_key):
     self.degree = degree
     self.public_key = public_key
     M = int(degree * 4)
     poly_degree = M // 2
     self.encoder = CKKSEncoder(poly_degree=poly_degree)
     self.evaluator = CKKSEvaluator(poly_degree=poly_degree)
Ejemplo n.º 2
0
class Alice:
    def __init__(self, degree):
        M = int(degree*4)
        poly_degree = M // 2
        scale = 1 << 28
        big_modulus = 1 << 1200
        ciph_modulus = 1 << 600
        self.param = CKKSParameters(
            poly_degree=poly_degree,
            ciph_modulus=ciph_modulus,
            big_modulus=big_modulus,
            scaling_factor=scale
        )
        self.model = LogisticRegression()
        self.keygen = CKKSKeyGenerator(self.param)
        self.public_key = self.keygen.public_key
        self.secret_key = self.keygen.secret_key
        self.encoder = CKKSEncoder(param=self.param)
        self.encryptor = CKKSEncryptor(self.param, self.public_key, self.secret_key)
        self.decryptor = CKKSDecryptor(self.param, self.secret_key)


    def fit(self, X, y):
        self.model.fit(X,y)

    def predict(self,X):
        return self.model.predict(X)

    def show(self,x_test,y_test):
        print("准确率为:{:.3f}%".format( self.model.score(x_test, y_test)*100))

    def encrypt_weights(self):
        coef = self.model.coef_[0,:].tolist()   #as raw message
        coef = align_list(coef)
        plain_coef = self.encoder.encode(coef,self.param.scaling_factor)
        encrypted_coef = self.encryptor.encrypt(plain_coef)
        intercept = self.model.intercept_.tolist()
        intercept = align_list(intercept)
        plain_intercept = self.encoder.encode(intercept,self.param.scaling_factor)
        encrypted_intercept = self.encryptor.encrypt(plain_intercept)
        return encrypted_coef, encrypted_intercept

    def decrypt_scores(self, encrypted_scores:list):
        score = []
        for c in encrypted_scores:
            plain_ = self.decryptor.decrypt(c)
            message_ = np.real(self.encoder.decode(plain_))
            total = np.sum(message_)
            score.append(total)
        return score
Ejemplo n.º 3
0
class Bob:
    def __init__(self, degree, public_key):
        self.public_key = public_key
        M = int(degree * 4)
        poly_degree = M // 2
        self.encoder = CKKSEncoder(poly_degree=poly_degree)
        self.evaluator = CKKSEvaluator(poly_degree=poly_degree)

    def set_weight(self, coef, intercept):
        self.coef = coef
        self.intercept = intercept

    def encrypted_scores(self, x: list):
        """

        :param x(list): the test_x to calculate with encrypted weight
        :return: encrypted_scores(Ciphertext)
        """
        x = align_list(
            x
        )  #change x into list whose length is equal to degree     STILL RAW MESSAGE
        plain_x = self.encoder.encode(x, self.coef.scaling_factor)
        ciph = self.evaluator.multiply_plain(self.coef, plain_x)
        ciph = self.evaluator.rescale(ciph, self.coef.scaling_factor)
        self.intercept = self.evaluator.lower_modulus(
            self.intercept, self.intercept.modulus // ciph.modulus)
        score = self.evaluator.add(self.intercept, ciph)
        return score

    def encrypted_evaluate(self, X):
        return [
            self.encrypted_scores(X[i, :].tolist()) for i in range(X.shape[0])
        ]  #shape_X (569, 30) shape_x: 30
Ejemplo n.º 4
0
class CKKS_Alice:
    def __init__(self, degree):
        self.degree = degree
        M = int(degree * 4)
        poly_degree = M // 2
        scale = 1 << 28
        big_modulus = 1 << 1200
        ciph_modulus = 1 << 600
        self.param = CKKSParameters(poly_degree=poly_degree,
                                    ciph_modulus=ciph_modulus,
                                    big_modulus=big_modulus,
                                    scaling_factor=scale)
        self.keygen = CKKSKeyGenerator(self.param)
        self.public_key = self.keygen.public_key
        self.secret_key = self.keygen.secret_key
        self.encoder = CKKSEncoder(param=self.param)
        self.encryptor = CKKSEncryptor(self.param, self.public_key,
                                       self.secret_key)
        self.decryptor = CKKSDecryptor(self.param, self.secret_key)

    def encrypt_weights(self, model):
        coef = model.coef_[0, :].tolist()  #as raw message
        coef = self.align_list(coef)
        plain_coef = self.encoder.encode(coef, self.param.scaling_factor)
        encrypted_coef = self.encryptor.encrypt(plain_coef)
        intercept = model.intercept_.tolist()
        intercept = self.align_list(intercept)
        plain_intercept = self.encoder.encode(intercept,
                                              self.param.scaling_factor)
        encrypted_intercept = self.encryptor.encrypt(plain_intercept)
        return encrypted_coef, encrypted_intercept

    def decrypt_scores(self, encrypted_scores: list):
        score = []
        for c in encrypted_scores:
            plain_ = self.decryptor.decrypt(c)
            message_ = np.real(self.encoder.decode(plain_))
            total = np.sum(message_)
            score.append(total)
        return score

    def align_list(self, x: list) -> list:
        length = len(x)
        zeros_num = int(self.degree - length)
        x.extend([0] * zeros_num)
        return x
Ejemplo n.º 5
0
 def __init__(self, degree, kernel='linear'):
     M = int(degree * 4)
     poly_degree = M // 2
     scale = 1 << 28
     big_modulus = 1 << 1200
     ciph_modulus = 1 << 600
     self.param = CKKSParameters(poly_degree=poly_degree,
                                 ciph_modulus=ciph_modulus,
                                 big_modulus=big_modulus,
                                 scaling_factor=scale)
     self.model = SVC(kernel=kernel)
     self.keygen = CKKSKeyGenerator(self.param)
     self.public_key = self.keygen.public_key
     self.secret_key = self.keygen.secret_key
     self.encoder = CKKSEncoder(param=self.param)
     self.encryptor = CKKSEncryptor(self.param, self.public_key,
                                    self.secret_key)
     self.decryptor = CKKSDecryptor(self.param, self.secret_key)
Ejemplo n.º 6
0
 def __init__(self, degree, multi_class='ovr', solver='liblinear'):
     M = int(degree * 4)
     poly_degree = M // 2
     scale = 1 << 28
     big_modulus = 1 << 1200
     ciph_modulus = 1 << 600
     self.param = CKKSParameters(poly_degree=poly_degree,
                                 ciph_modulus=ciph_modulus,
                                 big_modulus=big_modulus,
                                 scaling_factor=scale)
     self.model = LogisticRegression(multi_class=multi_class, solver=solver)
     self.keygen = CKKSKeyGenerator(self.param)
     self.public_key = self.keygen.public_key
     self.secret_key = self.keygen.secret_key
     self.encoder = CKKSEncoder(param=self.param)
     self.encryptor = CKKSEncryptor(self.param, self.public_key,
                                    self.secret_key)
     self.decryptor = CKKSDecryptor(self.param, self.secret_key)