TEST(Crypto, LRSTest) { DsaPrivateKey base_key; Integer generator = base_key.GetGenerator(); Integer subgroup = base_key.GetSubgroupOrder(); Integer modulus = base_key.GetModulus(); QVector<DsaPrivateKey> private_keys; QVector<DsaPublicKey> public_keys; int keys = 10; for(int idx = 0; idx < keys; idx++) { private_keys.append(DsaPrivateKey(modulus, subgroup, generator)); public_keys.append(DsaPublicKey(modulus, subgroup, generator, private_keys.last().GetPublicElement())); } CryptoRandom rng; QByteArray context(1024, 0); rng.GenerateBlock(context); QVector<QSharedPointer<LRSPrivateKey> > lrss; LRSPublicKey lrp(public_keys, context); for(int idx = 0; idx < keys; idx++) { lrss.append(QSharedPointer<LRSPrivateKey>( new LRSPrivateKey(private_keys[idx], public_keys, context))); } QByteArray msg(1500, 0); rng.GenerateBlock(msg); foreach(const QSharedPointer<LRSPrivateKey> &lrs, lrss) { QByteArray signature = lrs->Sign(msg); lrp.Verify(msg, signature); EXPECT_TRUE(lrp.VerifyKey(*(lrs.data()))); }
// Supporting routine for mlr_logistic_regression(): static void mlr_logistic_regression_aux(double* xs, double* ys, int n, double* pm, double* pb, double m0, double b0, double tol, int maxits) { int its = 0; int done = FALSE; double m = m0; double b = b0; while (!done) { // Compute derivatives double dldm = 0.0; double dldb = 0.0; double d2ldm2 = 0.0; double d2ldmdb = 0.0; double d2ldb2 = 0.0; double ell0 = 0.0; for (int i = 0; i < n; i++) { double xi = xs[i]; double yi = ys[i]; double pi = lrp(xi, m0, b0); double qi = lrq(xi, m0, b0); dldm += xi*(yi - pi); dldb += yi - pi; double piqi = pi * qi; double xipiqi = xi*piqi; double xi2piqi = xi*xipiqi; d2ldm2 -= xi2piqi; d2ldmdb -= xipiqi; d2ldb2 -= piqi; ell0 += log(qi) + yi * (m0 * xi + b0); } // Form the Hessian double ha = d2ldm2; double hb = d2ldmdb; double hc = d2ldmdb; double hd = d2ldb2; // Invert the Hessian double D = ha*hd - hb*hc; double Hinva = hd/D; double Hinvb = -hb/D; double Hinvc = -hc/D; double Hinvd = ha/D; // Compute H^-1 times grad ell double Hinvgradm = Hinva*dldm + Hinvb*dldb; double Hinvgradb = Hinvc*dldm + Hinvd*dldb; // Update [m,b] m = m0 - Hinvgradm; b = b0 - Hinvgradb; double ell = 0.0; for (int i = 0; i < n; i++) { double xi = xs[i]; double yi = ys[i]; double qi = lrq(xi, m, b); ell += log(qi) + yi * (m0 * xi + b0); } // Check for convergence double dell = fmax(ell, ell0); double err = (dell == 0.0) ? 0.0 : fabs(ell - ell0) / dell; #if 0 printf("its=%d,m=%e,b=%e,dm=%e,db=%e,ell=%e\n", its, m0, b0, -Hinvgradm, -Hinvgradb, ell); #endif if (err < tol) done = TRUE; if (++its > maxits) { fprintf(stderr, "mlr_logistic_regression: Newton-Raphson convergence failed after %d iterations. m=%e, b=%e.\n", its, m, b); exit(1); } m0 = m; b0 = b; } *pm = m; *pb = b; }
int main( int argc, char* argv[]) { if(argc != 2) { std::cerr << "Usage : ./main.out <edge_file>" << std::endl; exit(1); } Network network; std::ifstream fin(argv[1]); std::cerr << "Loading input file" << std::endl; network.LoadFile( fin ); bool is_weighted = network.IsWeighted(); if( ! is_weighted ) { std::cerr << "All the link weights are 1. Analyze the network as a non-weighted network." << std::endl; } std::pair<double,double> fc; if( is_weighted ) { std::cerr << "Conducting percolation analysis" << std::endl; std::ofstream lrp("link_removal_percolation.dat"); lrp << "#fraction weak_link_removal_lcc susceptibility strong_link_removal_lcc susceptibility" << std::endl; fc = network.AnalyzeLinkRemovalPercolationVariableAccuracy( 0.02, 0.005, lrp ); lrp.flush(); } std::cerr << "Calculating local clustering coefficients" << std::endl; network.CalculateLocalCCs(); if( is_weighted ) { std::cerr << "Calculating overlaps" << std::endl; network.CalculateOverlaps(); } std::cerr << "Calculating degree distribution" << std::endl; std::ofstream dd("degree_distribution.dat"); const auto degree_distribution = network.DegreeDistribution(); for(const auto& f : degree_distribution ) { dd << f.first << ' ' << f.second << std::endl; } dd.flush(); if( is_weighted ) { std::cerr << "Calculating link weight distribution" << std::endl; // double edge_weight_bin_size = 1.0; std::ofstream ewd("edge_weight_distribution.dat"); for(const auto& f : network.EdgeWeightDistributionLogBin() ) { ewd << f.first << ' ' << f.second << std::endl; } ewd.flush(); } std::map<double, size_t> strength_distribution; if( is_weighted ) { std::cerr << "Calculating node strength distribution" << std::endl; double avg_s = network.AverageEdgeWeight() * network.AverageDegree(); double strength_bin_size = avg_s * 0.01; std::ofstream sd("strength_distribution.dat"); strength_distribution = network.StrengthDistribution(strength_bin_size); for(const auto& f :strength_distribution) { sd << f.first << ' ' << f.second << std::endl; } sd.flush(); } std::cerr << "Calculating c(k)" << std::endl; std::ofstream cc_d("cc_degree_correlation.dat"); for(const auto& f : network.CC_DegreeCorrelation() ) { cc_d << f.first << ' ' << f.second << std::endl; } cc_d.flush(); if( is_weighted ) { std::cerr << "Calculating s(k)" << std::endl; std::ofstream sdc("strength_degree_correlation.dat"); for(const auto& f : network.StrengthDegreeCorrelation() ) { sdc << f.first << ' ' << f.second << std::endl; } sdc.flush(); } std::cerr << "Calculating k_nn(k)" << std::endl; std::ofstream ndc("neighbor_degree_correlation.dat"); for(const auto& f : network.NeighborDegreeCorrelation() ) { ndc << f.first << ' ' << f.second << std::endl; } ndc.flush(); if( is_weighted ) { std::cerr << "Calculating O(w)" << std::endl; std::ofstream owc("overlap_weight_correlation.dat"); for(const auto& f : network.OverlapWeightCorrelationLogBin() ) { owc << f.first << ' ' << f.second << std::endl; } owc.flush(); } std::cerr << "Calculating scalar values" << std::endl; std::ofstream fout("_output.json"); fout << "{" << std::endl; fout << " \"NumNodes\": " << network.NumNodes() << ',' << std::endl; fout << " \"NumEdges\": " << network.NumEdges() << ',' << std::endl; fout << " \"AverageDegree\": " << network.AverageDegree() << ',' << std::endl; fout << " \"Assortativity\": " << network.PCC_k_knn() << ',' << std::endl; fout << " \"ArgMax_Pk\": " << ArgMax( degree_distribution ) << ',' << std::endl; fout << " \"ClusteringCoefficient\": " << network.ClusteringCoefficient() << ',' << std::endl; fout << " \"PCC_C_k\": " << network.PCC_C_k(); if( is_weighted ) { fout << ',' << std::endl; double ave_w = network.AverageEdgeWeight(); fout << " \"AverageEdgeWeight\": " << ave_w << ',' << std::endl; double ave_k = network.AverageDegree(); fout << " \"AverageStrength\": " << ave_w * ave_k << ',' << std::endl; double argmax_ps = ArgMax( strength_distribution ); fout << " \"ArgMax_Ps\": " << argmax_ps << ',' << std::endl; fout << " \"Normalized_ArgMax_Ps\": " << argmax_ps / (ave_w*ave_k) << ',' << std::endl; fout << " \"PCC_s_k\": " << network.PCC_s_k() << ',' << std::endl; fout << " \"AverageOverlap\": " << network.AverageOverlap() << ',' << std::endl; fout << " \"PCC_O_w\": " << network.PCC_O_w() << ',' << std::endl; fout << " \"Fc_Ascending\": " << fc.first << ',' << std::endl; fout << " \"Fc_Descending\": " << fc.second << ',' << std::endl; fout << " \"Delta_Fc\": " << fc.second - fc.first << std::endl; } fout << "}" << std::endl; return 0; }
void ConnectionManagerIn::execute(void * arg) { ConnectionManagerIn * pthis = (ConnectionManagerIn *)arg; flog<<"Connection manager In Init...\n"; TCPSocket * socket; event_data_t event_data; try { socket = pthis->cli->getClient()->getSocket(); } catch (vortex::Exception * e) { printf("%s\n",e->what()); flog <<"Cli init:"<<e->what()<<endl; delete e; } char * buffer = (char *)malloc(5); try { if (socket->pollRead()) socket->recv(buffer,5,0); LoginResponsePacket lrp(buffer,(size_t)5); if (lrp.getHeader().length()>0) flog << "Yay got a positive response!\n"; else flog << "login failed!\n"; } catch (vortex::Exception * e) { flog << "what 1"<<e->what()<<endl; //Soit Mauvais paquet //Soit timeout du socket } free(buffer); //LOGIN OK! //On peux rentrer dans la boucle principale de protocole while (1) { flog << "Main LOOP!\n"; try { //D'abord on checks si il y a des donnees a lire avec select flog << "POLL successful!\n"; char * buffer = (char *)malloc(5); socket->recv(buffer,5,0); bitBuffer header(buffer,5); flog << "Type:" <<(int)header.readChar(false)<<endl; switch (header.readChar(false)) { case event_connect_login: flog << "login" << endl; break; case event_connect_logout: flog << "logout" << endl; break; case event_world_add: flog << "add" << endl; break; case event_world_update: flog << "update" << endl; break; case event_world_del: flog << "del" << endl; break; case event_chat_incoming: flog << "chat" << endl; break; } free(buffer); } catch (vortex::Exception * e) { flog << "what2"<<e->what()<<endl; delete e; } //Apres on check les events: } flog << "main loop end\n"; }