def _evaluate_fermat(self, n): start_init_time = time() fermat = Fermat(n, progress=False) end_init_time = time() start_search_time = time() res = fermat.search() end_search_time = time() self.init_times.append(end_init_time - start_init_time) self.search_times.append(end_search_time - start_search_time) self.res_list.append(res) self.primes_used_list.append([]) self.R_size_list.append(0) self.total_i_list.append(fermat.total_i) self.last_j_list.append(0) self.last_r_list.append(fermat.p_plus_q) self.T_list.append(fermat.p_plus_q - fermat.sqrt_n + 1) Tp = fermat.total_i + 1 self.Tp_list.append(Tp) del fermat start_qs_time = time() qs_res = qs.factor(n) end_qs_time = time() self.qs_times.append(end_qs_time - start_qs_time) self.qs_res_list.append(qs_res)
def compute_knn_geodesic_distance(data, k): distances = distance_matrix(data,data) # Initialize the model f_aprox_D = Fermat(1, path_method='D', k=k) # Fit f_aprox_D.fit(distances) knn_dist = f_aprox_D.get_distances() return knn_dist
def compute_fermat_distance_D(data, p, k): #Compute euclidean distances distances = distance_matrix(data, data) # Initialize the model fermat = Fermat(alpha=p, path_method='D', k=k) #method Dijkstra # Fit fermat.fit(distances) ##Compute Fermat distances fermat_dist = fermat.get_distances() return fermat_dist
def compute_kNN_distance(data, k): ''' Computes the estimator of geodesic distance using kNN graph. ''' distances = distance_matrix(data, data) # Initialize the model f_aprox_D = Fermat(1, path_method='D', k=k) # Fit f_aprox_D.fit(distances) adj_dist = f_aprox_D.get_distances() return adj_dist
def compute_fermat_distances(data, alpha): #Compute euclideandistances distances = distance_matrix(data, data) # Initialize the model fermat = Fermat(alpha=alpha, path_method='FW') # Fit fermat.fit(distances) ##Compute Fermat distances fermat_dist = fermat.get_distances() return fermat_dist
def fermat(self): path = getcwd() + '/Attacks/Fermat' sys.path.append(path) from fermat import Fermat fermat = Fermat(self.args.n, self.args.e) d = fermat.d
def compute_fermat_distance(data, p): ''' Computes the sample Fermat distance. ''' #Compute euclidean distances distances = distance_matrix(data, data) # Initialize the model fermat = Fermat(alpha=p, path_method='FW') # method Floyd-Warshall # Fit fermat.fit(distances) ##Compute Fermat distances fermat_dist = fermat.get_distances() return fermat_dist
def fermat_distance_matrix(X, metric="euclidean", scale=True, d=3, k=10, landmarks=50): ''' Parameters ---------- X : GENOTYPE MATRIX (NxSNPs- rows are individuals) distance : STR Distance used to calculate fermat distance. The default is "euclidean". scale : BOOL. Wether to scale distances prior to fermat estimation. The default is True. d : INT, optional Related to size of latent Space/Manifold dim. k : INT, optional Number of neighbours used in estimation. Larger is more exact but has more computational cost. The default is 10. landmarks : INT, optional See fermat docs. The default is 50. Returns ------- None. ''' if metric == "euclidean": dist = euclidean_distances(X.T, X.T) else: raise NotImplementedError if scale: avg = np.mean(dist) if avg == 0: avg = avg + 10e-10 dist = dist / avg fermat = Fermat(d, path_method='L', k=k, landmarks=landmarks) print("calculating fermat approx distances") fermat.fit(dist) fermat_distances = fermat.get_distances() return fermat_distances
def reduce_dim(geno_train, method = "kpca", codif = "no_codif"): ''' Get 2D SNP features. Input ----- geno_train: 2D Nxp array (N: samples, p: markers.) method: dimension reduction method (kPCA, tSNE, AE) codif: input codification (no_codif vs OHE) Output ------ features_2d: 2D 2xp array with extracted features (cartesian coordinates) ''' if method == "kpca": kpca = KernelPCA(n_components = 2) features_2d = kpca.fit_transform(geno_train.T) elif method == "tsne": if multi: tsne = multi_TSNE(n_jobs=-1) else: tsne = TSNE() features_2d = tsne.fit_transform(geno_train.T) elif method == "fermat": print("calculating euclidean distances") X = euclidean_distances(geno_train.T, geno_train.T) avg = np.mean(X) if avg == 0: avg = avg+10e-10 X = X / avg fermat = Fermat(3, path_method='L', k=10, landmarks=50) print("calculating fermat approx distances") fermat.fit(X) distances = fermat.get_distances() tsne = TSNE(metric ='precomputed') print("fitting TSNE") features_2d = tsne.fit_transform(distances) cal = str(date.today()) pickle.dump( features_2d, open( f"features_{cal}.p", "wb" ) ) return features_2d
# course lectured in Federal University of Santa Catarina. import time from lcg import LCG from blumblumshub import BBS from millerrabin import Millerrabin from fermat import Fermat ## # Funcao main que ira gerar os numeros primos if __name__ == '__main__': tamanhos = [40, 56, 80, 128, 168, 224, 256, 512, 1024, 2048, 4096] #Tamanhos dos numeros que serao gerados pelos geradores lcg = LCG(74573) #semente do LCG miller = Millerrabin(5) #5 iteracoes fermat = Fermat(5) #5 iteracoes outFile = open("numeroPrimos.txt", "wb") #Testando LCG com Miller print "Gerando possiveis numeros primos com LCG e Miller.\n" start_lcg_miller_time = time.time() outFile.write("Possiveis numeros primos gerados por LCG e Miller: \n") for m in tamanhos: while True: numeroPrimo = lcg.gerador(m, 1103515245, 12345) if miller.teste(numeroPrimo): outFile.write(str(numeroPrimo) + "\n") print "Um possivel numero primo de tamanho ", m, " bits foi gerado em ", ( time.time() - start_lcg_miller_time), " segundos." break