Changeset 1511


Ignore:
Timestamp:
May 26, 2010, 11:14:50 PM (12 years ago)
Author:
Sven Rech
Message:

some peer2peer preparation stuff for quadratic sieve

Location:
trunk/CrypPlugins/QuadraticSieve
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/QuadraticSieve/FactorManager.cs

    r1463 r1511  
    6565            AddFactorsWithoutFiringEvent(factorList);
    6666            FactorsChanged(primeFactors, compositeFactors);
     67        }
     68
     69        /// <summary>
     70        /// adds the factor list as composite factors
     71        /// </summary>
     72        public void AddCompositeFactors(List<BigInteger> factors)
     73        {
     74            compositeFactors.AddRange(factors);
     75        }
     76
     77        /// <summary>
     78        /// adds the factor list as prime factors
     79        /// </summary>
     80        public void AddPrimeFactors(List<BigInteger> factors)
     81        {
     82            primeFactors.AddRange(factors);
     83        }
     84
     85        /// <summary>
     86        /// uses the informations from the factorManager parameter to transform some composite factors to prime factors.
     87        /// returns true if this factorManager has more informations than the parameter factorManager.
     88        /// </summary>
     89        public bool Synchronize(FactorManager factorManager)
     90        {
     91            throw new NotImplementedException();
     92        }
     93
     94        /// <summary>
     95        /// Returns true if the composite list contains the param.
     96        /// </summary>
     97        public bool ContainsComposite(BigInteger composite)
     98        {
     99            foreach (BigInteger c in compositeFactors)
     100                if (c == composite)
     101                    return true;
     102            return false;
    67103        }
    68104
  • trunk/CrypPlugins/QuadraticSieve/PeerToPeer.cs

    r1510 r1511  
    55using System.IO.Compression;
    66using System.IO;
     7using Cryptool.P2P;
     8using System.Numerics;
     9using System.Diagnostics;
    710
    8 namespace QuadraticSieve
     11namespace Cryptool.Plugins.QuadraticSieve
    912{
    1013    class PeerToPeer
    1114    {
     15        private string channel;
     16        private BigInteger number;
     17        private BigInteger factor;
     18        private int head;
     19
     20        private byte[] ReadYield(int index)
     21        {
     22            byte[] yield = P2PManager.Retrieve(channel + "#" + factor + "!" + index);
     23            if (yield == null)
     24                return null;
     25
     26            MemoryStream memStream = new MemoryStream();
     27            DeflateStream defStream = new DeflateStream(memStream, CompressionMode.Decompress);
     28            defStream.Read(yield, 0, yield.Length);
     29            byte[] decompressedYield = memStream.ToArray();
     30
     31            return decompressedYield;
     32        }
     33
    1234        /// <summary>
    1335        /// Compresses the yield and puts it in the DHT.
    1436        /// Returns the compressed size
    1537        /// </summary>
    16         internal int put(byte[] serializedYield)
     38        public int Put(byte[] serializedYield)
    1739        {
    1840            MemoryStream memStream = new MemoryStream();
     
    2143            byte[] compressedYield = memStream.ToArray();
    2244
    23 
     45            P2PManager.Store(channel + "#" + factor + "!" + head, compressedYield);
     46            head++;
     47            P2PManager.Store(channel + "#" + factor + "HEAD:" + head, head.ToString());
    2448
    2549            return compressedYield.Length;
    2650        }
     51
     52        /// <summary>
     53        /// Sets the channel in which we want to sieve
     54        /// </summary>
     55        public void SetChannel(string channel)
     56        {
     57            this.channel = channel;
     58        }
     59
     60        /// <summary>
     61        /// Sets the number to sieve
     62        /// </summary>
     63        public void SetNumber(BigInteger number)
     64        {
     65            this.number = number;
     66        }
     67
     68        /// <summary>
     69        /// Sets the factor to sieve next
     70        /// </summary>
     71        public void SetFactor(BigInteger factor)
     72        {
     73            this.factor = factor;
     74            Debug.Assert(this.number % this.factor == 0);
     75
     76            byte[] h = P2PManager.Retrieve(channel + "#" + factor + "HEAD:" + head);
     77            if (h != null)
     78                head = int.Parse(h.ToString());
     79            else
     80                head = 0;
     81        }       
     82
     83        /// <summary>
     84        /// Synchronizes the factorManager with the DHT.
     85        /// Return false if this.factor is not a composite factor in the DHT (which means, that another peer already finished sieving this.factor).
     86        /// </summary>
     87        public bool SyncFactorManager(FactorManager factorManager)
     88        {
     89            FactorManager dhtFactorManager = new FactorManager(null, null);
     90            //TODO: add factors from dht to dhtFactorManager here.
     91
     92            if (dhtFactorManager == null || factorManager.Synchronize(dhtFactorManager))
     93            {
     94                //TODO: store factorManager to DHT
     95            }
     96
     97            return factorManager.ContainsComposite(this.factor);
     98        }
    2799    }
    28100}
  • trunk/CrypPlugins/QuadraticSieve/QuadraticSieve.cs

    r1510 r1511  
    141141                usePeer2Peer = false;
    142142            }
     143            if (usePeer2Peer)
     144            {
     145                peerToPeer.SetChannel(settings.Channel);
     146                peerToPeer.SetNumber(InputNumber);
     147            }
    143148
    144149            sumSize = 0;
     
    430435
    431436            //manage the yields of the other threads:
    432             manageYields(conf, max_relations);  //this method returns as soon as there are enough relations found
    433             if (userStopped)
    434                 return;
    435 
    436             //sieving is finished now, so give some informations and stop threads:
    437             GuiLogMessage("Data size: " + (sumSize / 1024) / 1024 + " MB!", NotificationLevel.Debug);
    438             ProgressChanged(0.9, 1.0);
    439             GuiLogMessage("Sieving finished", NotificationLevel.Info);
    440             quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
    441             {
    442                 quadraticSieveQuickWatchPresentation.factorInfo.Content = "Found enough relations! Please wait...";
    443             }, null);
     437            if (manageYields(conf, max_relations))  //this method returns as soon as there are enough relations found
     438            {
     439                //sieving is finished now, so give some informations and stop threads:
     440                ProgressChanged(0.9, 1.0);
     441                GuiLogMessage("Sieving finished", NotificationLevel.Info);
     442                quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
     443                {
     444                    quadraticSieveQuickWatchPresentation.factorInfo.Content = "Found enough relations! Please wait...";
     445                }, null);
     446            }
    444447            stopThreads();
    445448            if (yieldqueue != null)
     
    447450        }
    448451
    449         private void manageYields(IntPtr conf, int max_relations)
     452        /// <summary>
     453        /// Manages the whole yields that are created during the sieving process by the other threads (and other peers).
     454        /// Returns true, if enough relations have been found.
     455        /// </summary>
     456        private bool manageYields(IntPtr conf, int max_relations)
    450457        {
    451458            MethodInfo serializeYield = msieve.GetMethod("serializeYield");
     
    462469                yieldEvent.WaitOne();               //wait until queue is not empty
    463470                if (userStopped)
    464                     return;
     471                    return false;
    465472                while (yieldqueue.Count != 0)       //get all the results from the helper threads, and store them
    466473                {
    467                     IntPtr yield = (IntPtr)yieldqueue.Dequeue();
    468                     byte[] serializedYield = (byte[])serializeYield.Invoke(null, new object[] { yield });
     474                    IntPtr yield = (IntPtr)yieldqueue.Dequeue();                   
    469475
    470476                    if (usePeer2Peer)
    471477                    {
    472                         int compressedSize = peerToPeer.put(serializedYield);
     478                        byte[] serializedYield = (byte[])serializeYield.Invoke(null, new object[] { yield });
     479                        int compressedSize = peerToPeer.Put(serializedYield);
    473480                        sumSize += (uint)compressedSize;
    474481                    }
     
    478485                num_relations = (int)getNumRelations.Invoke(null, new object[] { conf });
    479486                showProgressPresentation(max_relations, num_relations, start_relations, start_sieving_time);
    480             }           
     487
     488                if (usePeer2Peer && !peerToPeer.SyncFactorManager(factorManager))   //an other peer already finished sieving
     489                {
     490                    return false;
     491                }
     492            }
     493            return true;
    481494        }
    482495
     
    522535            factorManager.AddFactors(list);
    523536
     537            if (usePeer2Peer)
     538                peerToPeer.SyncFactorManager(factorManager);
     539           
    524540            MethodInfo msieve_run_core = msieve.GetMethod("msieve_run_core");
    525541
     
    529545                //get one composite factor, which we want to sieve now:
    530546                BigInteger compositeFactor = factorManager.GetCompositeFactor();
    531                 quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
    532                 {
    533                     String compRep;
    534                     if (compositeFactor.ToString().Length < 6)
    535                         compRep = compositeFactor.ToString();
    536                     else
    537                         compRep = compositeFactor.ToString().Substring(0, 4) + "...";
    538                     quadraticSieveQuickWatchPresentation.factorInfo.Content = "Now sieving first composite factor! (" + compRep + ")";
    539                 }, null);
     547                showFactorInformations(compositeFactor);
     548                if (usePeer2Peer)
     549                    peerToPeer.SetFactor(compositeFactor);
    540550
    541551                //now start quadratic sieve on it:               
     
    543553                if (userStopped)
    544554                    return;
    545                 factorManager.ReplaceCompositeByFactors(compositeFactor, resultList);
    546             }
     555                if (!usePeer2Peer || peerToPeer.SyncFactorManager(factorManager))       //add the result list to factorManager if no other peer already updated it
     556                    factorManager.ReplaceCompositeByFactors(compositeFactor, resultList);
     557
     558                if (usePeer2Peer)
     559                    peerToPeer.SyncFactorManager(factorManager);
     560            }
     561        }
     562
     563        private void showFactorInformations(BigInteger compositeFactor)
     564        {
     565            quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
     566            {
     567                String compRep;
     568                if (compositeFactor.ToString().Length < 6)
     569                    compRep = compositeFactor.ToString();
     570                else
     571                    compRep = compositeFactor.ToString().Substring(0, 4) + "...";
     572                quadraticSieveQuickWatchPresentation.factorInfo.Content = "Now sieving first composite factor! (" + compRep + ")";
     573            }, null);
    547574        }
    548575
Note: See TracChangeset for help on using the changeset viewer.