Changeset 1462


Ignore:
Timestamp:
May 21, 2010, 9:19:03 PM (12 years ago)
Author:
Sven Rech
Message:

some changes to quadratic sieve (doesn't work atm)

Location:
trunk/CrypPlugins/QuadraticSieve
Files:
1 added
2 edited

Legend:

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

    r1453 r1462  
    3131using System.Reflection;
    3232using System.Numerics;
     33using System.Collections.Generic;
     34using System.Diagnostics;
    3335
    3436namespace Cryptool.Plugins.QuadraticSieve
     
    5961        private int start_relations;
    6062        private ArrayList conf_list;
    61         private static Assembly msieveDLL;
    62         private static Type msieve;
    6363        private bool userStopped = false;
    64         private IntPtr factorList;
    65         private ArrayList factors;
     64        private FactorManager factorManager;
     65
     66        private static Assembly msieveDLL = null;
     67        private static Type msieve = null;
    6668
    6769        #endregion
     
    7779        #endregion
    7880
    79         /// <summary>
    80         /// Static constructor
    81         ///
    82         /// loads the msieve / msieve64 dll
    83         /// </summary>
    84         static QuadraticSieve()
    85         {
    86             //Load msieve.dll:
    87             string s = Directory.GetCurrentDirectory();
    88             string dllname;
    89             if (IntPtr.Size == 4)
    90                 dllname = "msieve.dll";
    91             else
    92                 dllname = "msieve64.dll";
    93             msieveDLL = Assembly.LoadFile(Directory.GetCurrentDirectory() + "\\AppReferences\\" + dllname);
    94             msieve = msieveDLL.GetType("Msieve.msieve");
    95         }
    96        
    9781        #region public
    9882
     
    141125            userStopped = false;
    142126
    143             if (InputNumber is Object)
     127            if (InputNumber != 0)
    144128            {
    145129                if (InputNumber.ToString().Length >= 275)
     
    164148                DateTime start_time = DateTime.Now;
    165149
    166                 //init msieve with callbacks:
    167                 MethodInfo initMsieve = msieve.GetMethod("initMsieve");
    168                 Object callback_struct = Activator.CreateInstance(msieveDLL.GetType("Msieve.callback_struct"));
    169                 FieldInfo showProgressField = msieveDLL.GetType("Msieve.callback_struct").GetField("showProgress");
    170                 FieldInfo prepareSievingField = msieveDLL.GetType("Msieve.callback_struct").GetField("prepareSieving");
    171                 FieldInfo getTrivialFactorlistField = msieveDLL.GetType("Msieve.callback_struct").GetField("getTrivialFactorlist");
    172                 Delegate showProgressDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.showProgressDelegate"), this, "showProgress");
    173                 Delegate prepareSievingDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.prepareSievingDelegate"), this, "prepareSieving");
    174                 Delegate getTrivialFactorlistDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.getTrivialFactorlistDelegate"), this, "getTrivialFactorlist");
    175                 showProgressField.SetValue(callback_struct, showProgressDel);
    176                 prepareSievingField.SetValue(callback_struct, prepareSievingDel);
    177                 getTrivialFactorlistField.SetValue(callback_struct, getTrivialFactorlistDel);
    178                 initMsieve.Invoke(null, new object[1] { callback_struct });
     150                initMsieveDLL();
     151                factorManager = new FactorManager(msieve.GetMethod("getPrimeFactors"), msieve.GetMethod("getCompositeFactors"));
     152                factorManager.FactorsChanged += this.FactorsChanged;
    179153
    180154                //Now factorize:               
     
    184158                    if (settings.DeleteCache && File.Exists(file))
    185159                        File.Delete(file);
    186                     MethodInfo factorize = msieve.GetMethod("start");
    187                     factorize.Invoke(null, new object[] { InputNumber.ToString(), file });
     160                    MethodInfo start = msieve.GetMethod("start");
     161                    start.Invoke(null, new object[] { InputNumber.ToString(), file });
    188162                    obj = IntPtr.Zero;
    189163                }
     
    195169                }
    196170
    197                 if (factors != null && !userStopped)
     171                if (!userStopped)
    198172                {
    199173                    timeLeft_message = "0 seconds left";
     
    209183                    }
    210184                    , null);
    211                     BigInteger[] outs = new BigInteger[factors.Count];
    212                     for (int i = 0; i < factors.Count; i++)
    213                     {
    214                         outs[i] = BigInteger.Parse((string)factors[i]);
    215                     }
    216                     OutputFactors = outs;
     185
     186                    Debug.Assert(factorManager.CalculateNumber() == InputNumber);
     187                    OutputFactors = factorManager.getPrimeFactors();
    217188                }
    218189                else
     
    367338
    368339        /// <summary>
    369         /// Actualize the progress
     340        /// Actualize the progress of the current sieving process
    370341        /// </summary>
    371342        /// <param name="conf"></param>
     
    376347            if (num_relations == -1)    //sieving finished
    377348            {
    378                 showFactorList();
    379349                ProgressChanged(0.9, 1.0);
    380350                GuiLogMessage("Sieving finished", NotificationLevel.Info);
    381351                stopThreads();
    382                 yieldqueue.Clear();               
     352                yieldqueue.Clear();
    383353            }
    384354            else
     
    409379                if (!sieving_started)
    410380                {
    411                     showFactorList();
    412 
    413381                    MethodInfo getCurrentFactor = msieve.GetMethod("getCurrentFactor");
    414382                    //GuiLogMessage((String)(getCurrentFactor.Invoke(null, new object[] { conf })), NotificationLevel.Debug);
     
    427395                }               
    428396            }
    429         }
    430 
    431         private void showFactorList()
    432         {
    433             MethodInfo getPrimeFactors = msieve.GetMethod("getPrimeFactors");
    434             ArrayList fl = (ArrayList)(getPrimeFactors.Invoke(null, new object[] { factorList }));
    435             foreach (Object o in fl)
    436                 GuiLogMessage("Prim Faktoren: " + (String)o, NotificationLevel.Debug);
    437 
    438             MethodInfo getCompositeFactors = msieve.GetMethod("getCompositeFactors");
    439             ArrayList fl2 = (ArrayList)(getCompositeFactors.Invoke(null, new object[] { factorList }));
    440             foreach (Object o in fl2)
    441                 GuiLogMessage("Zusammengesetzte Faktoren: " + (String)o, NotificationLevel.Debug);
    442397        }
    443398
     
    481436        }
    482437
     438        /// <summary>
     439        /// This callback method is called by msieve. "list" is the trivial factor list (i.e. it consists of the factors that have been found without
     440        /// using the quadratic sieve algorithm).
     441        /// The method then factors all the factors that are still composite by using the quadratic sieve.
     442        /// </summary>
    483443        private void getTrivialFactorlist(IntPtr list, IntPtr obj)
    484444        {
    485             factorList = list;
    486             MethodInfo getPrimeFactors = msieve.GetMethod("getPrimeFactors");
    487             factors = (ArrayList)(getPrimeFactors.Invoke(null, new object[] { factorList }));
    488             GuiLogMessage("TEST", NotificationLevel.Debug);
    489 
    490             //showFactorList();
     445            //add the trivial factors to the factor list:
     446            factorManager.AddFactors(list);
     447
     448            MethodInfo factor_mpqs = msieve.GetMethod("factor_mpqs");
     449
     450            //Now factorize as often as needed:
     451            while (!factorManager.OnlyPrimes())
     452            {
     453                //get one composite factor, which we want to sieve now:
     454                BigInteger compositeFactor = factorManager.GetCompositeFactor();
     455
     456                //now start quadratic sieve on it:               
     457                IntPtr resultList = (IntPtr)factor_mpqs.Invoke(null, new object[2] { obj, compositeFactor.ToString() });
     458                factorManager.ReplaceCompositeByFactors(compositeFactor, resultList);
     459            }
    491460        }
    492461
     
    565534        }
    566535
     536        private void FactorsChanged(List<BigInteger> primeFactors, List<BigInteger> compositeFactors)
     537        {
     538            GuiLogMessage("Factors Changed:", NotificationLevel.Debug);
     539
     540            foreach (BigInteger pf in primeFactors)
     541                GuiLogMessage("Prime Factor: " + pf.ToString(), NotificationLevel.Debug);
     542
     543            foreach (BigInteger cf in compositeFactors)
     544                GuiLogMessage("Composite Factor: " + cf.ToString(), NotificationLevel.Debug);
     545        }
     546
    567547        /// <summary>
    568548        /// Logs a message to the CrypTool gui
     
    583563        }
    584564
     565        /// <summary>
     566        /// dynamically loads the msieve dll file and sets the callbacks
     567        /// </summary>
     568        private void initMsieveDLL()
     569        {
     570            //Load msieve.dll (if necessary):
     571            if (msieve == null || msieveDLL == null)
     572            {
     573                string s = Directory.GetCurrentDirectory();
     574                string dllname;
     575                if (IntPtr.Size == 4)
     576                    dllname = "msieve.dll";
     577                else
     578                    dllname = "msieve64.dll";
     579                msieveDLL = Assembly.LoadFile(Directory.GetCurrentDirectory() + "\\AppReferences\\" + dllname);
     580                msieve = msieveDLL.GetType("Msieve.msieve");
     581            }
     582
     583            //init msieve with callbacks:
     584            MethodInfo initMsieve = msieve.GetMethod("initMsieve");
     585            Object callback_struct = Activator.CreateInstance(msieveDLL.GetType("Msieve.callback_struct"));
     586            FieldInfo showProgressField = msieveDLL.GetType("Msieve.callback_struct").GetField("showProgress");
     587            FieldInfo prepareSievingField = msieveDLL.GetType("Msieve.callback_struct").GetField("prepareSieving");
     588            FieldInfo getTrivialFactorlistField = msieveDLL.GetType("Msieve.callback_struct").GetField("getTrivialFactorlist");
     589            Delegate showProgressDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.showProgressDelegate"), this, "showProgress");
     590            Delegate prepareSievingDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.prepareSievingDelegate"), this, "prepareSieving");
     591            Delegate getTrivialFactorlistDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.getTrivialFactorlistDelegate"), this, "getTrivialFactorlist");
     592            showProgressField.SetValue(callback_struct, showProgressDel);
     593            prepareSievingField.SetValue(callback_struct, prepareSievingDel);
     594            getTrivialFactorlistField.SetValue(callback_struct, getTrivialFactorlistDel);
     595            initMsieve.Invoke(null, new object[1] { callback_struct });
     596        }
    585597        #endregion
    586598
  • trunk/CrypPlugins/QuadraticSieve/QuadraticSieve.csproj

    r1448 r1462  
    132132  </ItemGroup>
    133133  <ItemGroup>
     134    <Compile Include="FactorManager.cs" />
    134135    <Compile Include="Properties\AssemblyInfo.cs" />
    135136    <Compile Include="QuadraticSieve.cs" />
Note: See TracChangeset for help on using the changeset viewer.