def QueryGen(queryPoint, S, T_cols): """ 用于用户输入的一个查询数据点,产生线性变换所需矩阵 :param queryPoint: :return: """ #对用户输入的queryPoint做特殊构造以便于后文的计算 # print(queryPoint.shape) # print( queryPoint[0]) dim = queryPoint.shape[0] #注意数据维度的获取 #对查询向量做特殊构造 newQueryPoint = np.zeros((1, dim + 2), dtype=object) temp = queryPoint.dot(queryPoint.T) newQueryPoint[0][0] = temp newQueryPoint[0][1] = 1 for j in range(dim): newQueryPoint[0][j + 2] = (-2) * queryPoint[j] dim = newQueryPoint.shape[1] #更新dim newQueryPoint.resize((1, dim)) # 转换数据维度 G = (np.copy(newQueryPoint)) newPs, newPm = mvhe.getinvertiblematrix(1 + T_cols) newT = mvhe.getRandomMatrix(1, T_cols, mvhe.tBound) # 产生新随机矩阵T newS = mvhe.getSecretKey(newT, newPs) # 新密钥 GS = G.dot(S) M = mvhe.KeySwicthMatrix(GS, newT, newPm) return M, newS
def QueryGen(queryDataSet, S, T_cols): """ 用于用户输入的一个查询数据点,产生线性变换所需矩阵 :param queryPoint: :return: """ # 对用户输入的queryDataSet做特殊构造以便于后文的计算 queryPoints, dim = queryDataSet.shape queryDataReshape = np.zeros((queryPoints, dim + 2), dtype=object) for i in range(queryPoints): temp = queryDataSet[i].dot(queryDataSet[i].T) queryDataReshape[i][0] = temp queryDataReshape[i][1] = 1 for j in range(dim): queryDataReshape[i][j + 2] = (-2) * testData[i][j] dim = queryDataReshape.shape[1] # 更新dim queryDataReshape.resize((queryPoints, dim)) # 转换数据维度 G = (np.copy(queryDataReshape)) newPs, newPm = mvhe.getinvertiblematrix(queryPoints + T_cols) newT = mvhe.getRandomMatrix(queryPoints, T_cols, mvhe.tBound) # 产生新随机矩阵T newS = mvhe.getSecretKey(newT, newPs) # 新密钥 GS = G.dot(S) M = mvhe.KeySwicthMatrix(GS, newT, newPm) return M, newS
def DataUpload(trainData): trainDataNums,dim = trainData.shape #数据构造 trainDataReshape = np.zeros((trainDataNums, dim + 2), dtype=object) for i in range(trainDataNums): trainDataReshape[i][0] = 1 temp = trainData[i].dot(trainData[i].T) trainDataReshape[i][1] = temp for j in range(dim): trainDataReshape[i][j + 2] = trainData[i][j] dim = trainDataReshape.shape[1] #维度更新 #加密 # 初始化参数 T_cols = 1 Ps, Pm = mvhe.getinvertiblematrix(dim + T_cols) T = mvhe.getRandomMatrix(dim, T_cols, mvhe.tBound) S = mvhe.getSecretKey(T, Ps) #加密 encOftrainDataReshape = np.zeros((trainDataNums, dim + T.shape[1]), dtype=object) for i in range(trainDataNums): encOftrainDataReshape[i] = mvhe.encrypt(T, Pm, trainDataReshape[i]) return encOftrainDataReshape, S, T_cols
# dataset = read_points('CDR_Z.txt') dataset = read_points('CDR_raw_2d_Z.txt') dataset = np.array(dataset) print("Dataset has %d items and %d dims" % (dataset.shape[0], dataset.shape[1])) print(dataset[0]) # 参数设置 row = dataset.shape[0] col = dataset.shape[1] print(row, col) K = 1 N = col St, Mt = mvhe.getinvertiblematrix(N + K) T = mvhe.getRandomMatrix(N, K, mvhe.tBound) S = mvhe.getSecretKey(T, St) H = np.dot(S.T, S) def main(): for i in range(2, 10): k = i + 1 start = time.time() vhe_kmeans(dataset, k) end = time.time() print((end - start) / 3600, 'hour') t = open("vhe_time.txt", 'a+') print((end - start) / 3600, 'hour', file=t) t.close()