net-client.cc

Go to the documentation of this file.
00001 
00007 #include "at_startup.H"
00008 
00009 // sanity checks
00010 #ifndef IS_CLIENT
00011  #error "The net-client is a client!"
00012 #endif
00013 
00014 #ifdef IS_SERVER
00015  #error "The net-client is not a server!"
00016 #endif
00017 
00018 #ifndef USE_NETWORK
00019  #error "The net-client uses network features!"
00020 #endif
00021 
00022 #ifdef IS_STANDALONE
00023  #error "The net-client is not standalone!"
00024 #endif
00025 
00026 
00027 
00028 extern "C"
00029 {
00030  #include <unistd.h>
00031 }
00032 
00033 #include <cstdio>
00034 #include <cstdlib>
00035 #include <fstream>
00036 #include <sstream>
00037 #include <cmath>
00038 #include <gmp.h>
00039 #include <list>
00040 #include <set>
00041 #include <ctime>
00042 
00043 #include "qsieve.H"
00044 #include "StaticFactorbase.H"
00045 
00046 
00047 #include <vector>
00048 #include <stack>
00049 #include <algorithm>
00050 
00051 extern "C"
00052 {
00053  #include <sys/utsname.h>
00054  #include <pthread.h>
00055  #include <sys/wait.h>
00056  #include <unistd.h>
00057 }
00058 
00059 #include <csignal>
00060 #include "mutex.H"
00061 
00062 
00063 #ifdef NOTIFY_PARENT
00064  #warning "undefining NOTIFY_PARENT, since this is not a networked server"
00065  #undef NOTIFY_PARENT
00066 #endif
00067 
00068 
00069 const string StaticRelationsFile  = "static_relations.dat";
00070 const string SpecialRelationsFile = "special_relations.dat";
00071 
00072 
00073 string communication_name;
00074 
00075 #include "Client_IO.H"
00076 
00077 static socket_piper SocketPiper;
00078 ostream& communication_stream = *SocketPiper.detach_OutPipe(256*1024); // request 256 KB output buffer
00079 istream& CClientRelation_Delivery::PipeInput = *SocketPiper.detach_InPipe(16*1024); // as the bottleneck is the network connection to the server, the input buffer can be small
00080 
00081 
00082 #include "mpqsPolynom.H"
00083 
00084 
00085 mpz_t n, // number to factorize (will be reduced during factorization)
00086       kN; // input for MPQS (includes a suitable multiplier)
00087 
00088 
00089 #include "StaticRelations.H"
00090 #include "DynamicRelations.H"
00091 #include "SpecialRelations.H"
00092 
00093 #include "Sieving.H"
00094 #include "ConfigFile.cc"
00095 
00096 
00097 CmpqsPolynom Polynom; // object to manage polynomial computations for multipolynomial sieve (MPQS)
00098 #include "mpqsStatistics.cc" // statistical stuff about found relations, client connections, etc.
00099 
00100 
00101 #include "modulo.H" // modulo operations for unsigned int
00102 using namespace numtheory;
00103 
00104 #include "polynomial.H" /* for fast polynomial arithmetic & fft-continuation */
00105 #include "fft_param.cc"  /* discrete fast fourier transform */
00106 #include "elliptic_curve.H" /* invariant part of elliptic curve method (ecm) */
00107 #include "elliptic_curve-variant.cc" /* variant part of elliptic curve method (ecm) */
00108 
00109 #include "StaticRelations.cc"
00110 #include "CRelation-inc.cc"
00111 #include "Client_IO.cc"
00112 
00113 
00114 void kill_all_other_threads()
00115 {
00116   //cout << "Killing explicitly all other threads..." << endl;
00117   //pthread_kill_other_threads_np();
00118 }
00119 
00120 
00121 void cleanup_memory()
00122 {
00123 #ifdef VERBOSE_INFO
00124   cout << "cleanup allocated memory" << endl;
00125 #endif
00126   StaticRelations::cleanup_memory();
00127   CSieveStaticFactors::destruct();
00128   mpz_clear(CmpqsFactor::DLP_Threshold);
00129   mpz_clear(kN); mpz_clear(n);
00130 }
00131 
00132 
00133 
00134 int main(const int argc, const char* const argv[])
00135 {
00136 
00137 #ifdef USE_NCURSES
00138   new Cncursed(); // trigger activation of ncursed streams
00139 #endif
00140 
00141   PrintHeader("Qsieve net-client");
00142     
00143   if (argc!=2)
00144     {
00145       cerr << "servername expected!" << endl;
00146       exit(1);
00147     }
00148 
00149   atexit(kill_all_other_threads);
00150   cout.setf(ios::fixed); // fixed decimal notation, not scientific notation!
00151 
00152   mpz_init(n); // our number to factorize
00153   mpz_init(kN);
00154 
00155   atexit(cleanup_memory); // on successful exit free allocated data
00156 
00157   Read_ConfigFile();
00158 
00159   communication_name = argv[1];
00160 
00161   CClientRelation_Delivery::init();
00162 
00163   { 
00164     // first execute the elliptic curves (net-client):
00165 
00166     // we should try to continue an aborted ECM, if possible
00167     {
00168       ifstream in("ecm-recover.dat");
00169       if (in) // does a recovery file exist?
00170        {
00171         in.close();
00172         elliptic_curves elliptic_curve; // create curve
00173         elliptic_curve.go(-1,elcu_Phase1,elcu_Phase2); // try to recover curve and retry factorization with elliptic curve
00174        }
00175     }
00176 
00177     int ecm_sigma = -1, runde=0;
00178     while (true)
00179      {
00180        unix_io_stream communication_stream(communication_name, server_port);
00181  waitloop:
00182        communication_stream << "ECM?" << endl;
00183        communication_stream >> ecm_sigma;
00184        if (ecm_sigma==-3)
00185         {
00186           // -3 is special token for idle waiting loop
00187           string s;
00188           communication_stream >> s;
00189           cout << s << endl;
00190           goto waitloop;
00191         }
00192 
00193        if (ecm_sigma<0) break; // sigma<0 -> curves are done!
00194        communication_stream >> elcu_Phase1 >> elcu_Phase2;
00195        communication_stream >> n; // get number
00196        cout << "elliptic curve " << ++runde << endl;
00197        elliptic_curves elliptic_curve; // create ecm object
00198        elliptic_curve.go(ecm_sigma,elcu_Phase1,elcu_Phase2); // try factorization using this curve
00199      }
00200   }
00201   
00202   {
00203     unix_io_stream communication_stream(communication_name, server_port);
00204     communication_stream << QsieveLogon << endl;
00205     communication_stream >> n;
00206     communication_stream >> StaticRelations::Size_StaticFactorbase;
00207     communication_stream >> Factor_Threshold;
00208     communication_stream >> LogicalSieveSize;
00209 #ifdef VERBOSE_NOTICE
00210     cout << "n=" << n << endl;
00211     cout << "Size of static factorbase: " << StaticFactorbase::Size() << endl;
00212     cout << "Factor-Threshold (Exponent): " << Factor_Threshold << endl;
00213     cout << "Sieveinterval per polynomial: [" << -LogicalSieveSize << "," << LogicalSieveSize << "]" << endl;
00214 #endif
00215   }
00216 
00217   determine_best_MPQS_Multiplier(n,kN,MPQS_Multiplier); // optimal multiplier value to sieve with
00218   
00219   if ( sqrt(mpz_get_d(kN)) < PhysicalSieveSize )
00220     {
00221       cerr << "Sieve size too big (you may want to reduce its size)!" << endl;
00222       exit(1);
00223     }
00224 
00225   // Set a threshold for Double-Large-Primes,
00226   // this is the square of the maximal Single Large Prime...
00227   mpz_init(CmpqsFactor::DLP_Threshold);
00228   mpz_set_ui(CmpqsFactor::DLP_Threshold,SingleLargePrime_Threshold);
00229   mpz_mul(CmpqsFactor::DLP_Threshold,CmpqsFactor::DLP_Threshold,CmpqsFactor::DLP_Threshold);
00230   
00231   StaticFactorbase::compute_StaticFactorbase();
00232   CSieveStaticFactors::initialize();
00233   SieveControl::compute_SieveThreshold(); // Threshold for detecting relations during sieving phase
00234   for (int i=0; i<64; ++i) SieveArray_[i]=-1; // initialize array underflow trigger values
00235   Polynom.compute_first_polynomial(kN,LogicalSieveSize); // compute the first MPQS polynomial to sieve with
00236 
00237   display_StatusLegend();
00238 
00239   mpz_t UpperBound_D;
00240   mpz_init(UpperBound_D);
00241 
00242   while (true)
00243    {
00244      // main loop: get MPQS interval and sieve them
00245 
00246      CClientPolynomFetcher::fetch(UpperBound_D);
00247 
00248      do // sieve relations
00249       {
00250         if (StaticRelations::Count() > 4*StaticFactorbase::Size())
00251          {
00252            cerr << "#static relations > 4*StaticFactorbase::Size(): this is weird." << endl;
00253            cerr << "I will sleep 30 seconds for transmission to end, and then I will abort." << endl;
00254            sleep(30);
00255            exit(1);
00256          }
00257         CClientDynamicFactorFetcher::fetch();
00258         do_sieving();
00259         if (!communication_stream)
00260          {
00261            MARK;
00262            cerr << "communication stream is in error state! aborting..." << endl;
00263            exit(1);
00264          }
00265         Polynom.compute_next_polynomial();
00266       } while (Polynom < UpperBound_D);
00267 
00268    }
00269 
00270   // these lines are unreachable as long as nobody breaks the while(true) loop
00271   mpz_clear (UpperBound_D);
00272   return 0;
00273 }

Generated on Wed Nov 7 23:29:25 2007 for Qsieve by  doxygen 1.5.4