Пример #1
0
        public static void ListDevices()
        {
            int deviceId = 0;

            foreach (CLDeviceInfo device in CLRuntime.GetDevices())
            {
                if (!device.CompilerAvailable)
                {
                    continue;
                }
                //get preferredWorkGroupSize
                ulong preferredWorkGroupSize = getPreferredWorkGroupSize(device.DeviceId);  // moved to external function as we call it also in Main

                //display device
                Console.WriteLine("Id:{0} Name:{1}",
                                  deviceId, device.Name.Trim());
                Console.WriteLine("    PreferredGroupSizeMultiple:{0} ComputeUnits:{1} ClockFrequency:{2}",
                                  preferredWorkGroupSize, device.MaxComputeUnits, device.MaxClockFrequency);
                Console.WriteLine("    MaxConstantBufferSize:{0} MaxConstantArgs:{1} MaxMemAllocSize:{2}",
                                  device.MaxConstantBufferSize, device.MaxConstantArgs, device.MaxMemAllocSize);
                Console.WriteLine("");
                deviceId++;
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            ProgramParameters             parms     = ProgramParameters.Instance;
            Func <Mode, Action <string> > parseMode = (m) => (s) => { if (!string.IsNullOrEmpty(s))
                                                                      {
                                                                          parms.ProgramMode = m;
                                                                      }
            };
            OptionSet p = new OptionSet()
                          .Add <uint>("k|keysize=", "Specifies keysize for the RSA key", (i) => parms.KeySize = i)
                          .Add("n|nonoptimized", "Runs non-optimized kernel", parseMode(Mode.NonOptimized))
                          .Add("l|listdevices", "Lists the devices that can be used.", parseMode(Mode.ListDevices))
                          .Add("h|?|help", "Displays command line usage help.", parseMode(Mode.Help))
                          .Add <uint>("d|device=", "Specifies the opencl device that should be used.", (i) => parms.DeviceId        = i)
                          .Add <uint>("g|groupsize=", "Specifies the number of threads in a workgroup.", (i) => parms.WorkGroupSize = i)
                          .Add <uint>("w|worksize=", "Specifies the number of hashes preformed at one time.", (i) => parms.WorkSize = i)
                          .Add <uint>("t|cputhreads=", "Specifies the number of CPU threads to use when creating work. (EXPERIMENTAL - OpenSSL not thread-safe)", (i) => parms.CpuThreads = i)
                          .Add <string>("p|save-kernel=", "Saves the generated kernel to this path.", (i) => parms.SaveGeneratedKernelPath  = i)
                          .Add <string>("o|output=", "Saves the generated key(s) and address(es) to this path.", (i) => parms.KeyOutputPath = i)
                          .Add("c|continue", "Continue to search for keys rather than exiting when a key is found.", (i) => { if (!string.IsNullOrEmpty(i))
                                                                                                                              {
                                                                                                                                  parms.ContinueGeneration = true;
                                                                                                                              }
                               })
            ;



            List <string> extra = p.Parse(args);

            if (parms.ProgramMode == Mode.NonOptimized || parms.ProgramMode == Mode.Normal)
            {
                if (extra.Count < 1)
                {
                    parms.ProgramMode = Mode.Help;
                }
                else
                {
                    parms.Regex = extra.ToDelimitedString("|");
                }
            }

            //_runtime.Run(ProgramParameters.Instance,"prefix[abcdef]");
            switch (parms.ProgramMode)
            {
            case Mode.Help:
                Help(p);
                break;

            case Mode.ListDevices:
                ListDevices();
                break;

            case Mode.Normal:
            case Mode.NonOptimized:
            {
                // If no Work Group Size provided, then query the selected device for preferred, if not found set to 32.
                if (parms.WorkGroupSize == 0)
                {
                    ulong preferredWorkGroupSize = 32;
                    uint  deviceId = 0;
                    foreach (CLDeviceInfo device in CLRuntime.GetDevices())
                    {
                        if (!device.CompilerAvailable)
                        {
                            continue;
                        }
                        if (deviceId == parms.DeviceId)
                        {
                            preferredWorkGroupSize = getPreferredWorkGroupSize(device.DeviceId);
                            break;
                        }
                        deviceId++;
                    }

                    parms.WorkGroupSize = (uint)preferredWorkGroupSize;
                }

                Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
                _runtime.Run(ProgramParameters.Instance);
            }
            break;
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            ProgramParameters             parms     = ProgramParameters.Instance;
            Func <Mode, Action <string> > parseMode = (m) => (s) => { if (!string.IsNullOrEmpty(s))
                                                                      {
                                                                          parms.ProgramMode = m;
                                                                      }
            };
            OptionSet p = new OptionSet()
                          .Add <uint>("k|keysize=", "Specifies keysize for the RSA key", (i) => parms.KeySize = i)
                          .Add("n|nonoptimized", "Runs non-optimized kernel", parseMode(Mode.NonOptimized))
                          .Add("l|listdevices", "Lists the devices that can be used.", parseMode(Mode.ListDevices))
                          .Add("h|?|help", "Displays command line usage help.", parseMode(Mode.Help))
                          .Add <uint>("d|device=", "Specifies the opencl device that should be used.", (i) => parms.DeviceId        = i)
                          .Add <uint>("g|groupsize=", "Specifies the number of threads in a workgroup.", (i) => parms.WorkGroupSize = i)
                          .Add <uint>("w|worksize=", "Specifies the number of hashes preformed at one time.", (i) => parms.WorkSize = i)
                          .Add <uint>("t|cputhreads=", "Specifies the number of CPU threads to use when creating work. (EXPERIMENTAL - OpenSSL not thread-safe)", (i) => parms.CpuThreads = i)
                          .Add <string>("pidfile=", "Specifies a file where the process id will be written; file will be deleted at exit", (i) => parms.PIDFile = i)
                          .Add <string>("m|modulifile=", "Specifies a file containing public key moduli", (i) => parms.RSAModuliPath = i)
                          .Add("s|write-moduli", "Writes moduli and private keys for a given pattern to the file specified with -m", (i) => { if (parms.ProgramMode != Mode.Help)
                                                                                                                                              {
                                                                                                                                                  parms.ProgramMode = Mode.WriteModuli;
                                                                                                                                              }
                               })
                          .Add("r|read-results=", "Reads a results file (generated by a remote miner) and output the winning key (-m must be specified)", (i) => parms.InputResultsPath = i)
                          .Add <string>("p|save-kernel=", "Saves the generated kernel to this path.", (i) => parms.SaveGeneratedKernelPath  = i)
                          .Add <string>("o|output=", "Saves the generated key(s) and address(es) to this path.", (i) => parms.KeyOutputPath = i)
                          .Add("c|continue", "Continue to search for keys rather than exiting when a key is found.", (i) => { if (!string.IsNullOrEmpty(i))
                                                                                                                              {
                                                                                                                                  parms.ContinueGeneration = true;
                                                                                                                              }
                               })
            ;

            List <string> extra = p.Parse(args);

            if (parms.InputResultsPath != null && parms.ProgramMode != Mode.Help)
            {
                parms.ProgramMode = Mode.ReadResults;
            }

            if (parms.ProgramMode == Mode.NonOptimized || parms.ProgramMode == Mode.Normal || parms.ProgramMode == Mode.WriteModuli)
            {
                if (extra.Count < 1)
                {
                    parms.ProgramMode = Mode.Help;
                }
                else
                {
                    parms.Regex = extra.ToDelimitedString("|");
                }
            }

            if (parms.PIDFile != null)
            {
                File.WriteAllText(parms.PIDFile, System.Diagnostics.Process.GetCurrentProcess().Id.ToString());
            }

            //_runtime.Run(ProgramParameters.Instance,"prefix[abcdef]");
            switch (parms.ProgramMode)
            {
            case Mode.Help:
                Help(p);
                break;

            case Mode.ListDevices:
                ListDevices();
                break;

            case Mode.WriteModuli:
                WriteModuli();
                break;

            case Mode.ReadResults:
                ReadResults();
                break;

            case Mode.Normal:
            case Mode.NonOptimized:
            {
                // If no Work Group Size provided, then query the selected device for preferred, if not found set to 32.
                if (parms.WorkGroupSize == 0)
                {
                    ulong preferredWorkGroupSize = 32;
                    uint  deviceId = 0;
                    foreach (CLDeviceInfo device in CLRuntime.GetDevices())
                    {
                        if (!device.CompilerAvailable)
                        {
                            continue;
                        }
                        if (deviceId == parms.DeviceId)
                        {
                            preferredWorkGroupSize = getPreferredWorkGroupSize(device.DeviceId);
                            break;
                        }
                        deviceId++;
                    }

                    parms.WorkGroupSize = (uint)preferredWorkGroupSize;
                }

                // If a moduli file is specified, read it and create or read a .used file
                if (parms.RSAModuliPath != null)
                {
                    if (File.Exists(parms.RSAModuliPath))
                    {
                        // Read and/or create used file
                        string           usedPath = parms.RSAModuliPath + ".used";
                        HashSet <string> used     = null;
                        if (File.Exists(usedPath))
                        {
                            used = new HashSet <string>();
                            foreach (var line in File.ReadAllLines(usedPath))
                            {
                                used.Add(line.Trim());
                            }
                        }
                        else
                        {
                            File.WriteAllText(usedPath, "");
                        }

                        // Set up used file to be written later
                        parms.UsedModuliFile = new StreamWriter(usedPath, true);

                        // Read moduli file
                        parms.RSAPublicModuli = new Queue <BigNumber>();
                        foreach (var line in File.ReadAllLines(parms.RSAModuliPath))
                        {
                            if (used == null || !used.Contains(line.Trim()))
                            {
                                parms.RSAPublicModuli.Enqueue(BigNumber.FromDecimalString(line.Trim()));
                            }
                        }
                    }
                }

                Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
                try {
                    _runtime.Run(ProgramParameters.Instance);
                }
                finally {
                    Shutdown();
                }
            }
            break;
            }
        }
Пример #4
0
        public static void ReadResults()
        {
            ProgramParameters parms = ProgramParameters.Instance;

            string rsaPrivFn = parms.RSAModuliPath + ".priv";

            if (!File.Exists(rsaPrivFn))
            {
                Console.WriteLine("Error: expecting private key file at {0}.", rsaPrivFn);
                return;
            }
            if (!File.Exists(parms.InputResultsPath))
            {
                Console.WriteLine("Error: expecting results from miner at {0}.", parms.InputResultsPath);
                return;
            }

            // Create a map to hold (public modulus as decimal string) -> (private key as pem)
            IDictionary <string, string> modulusKeyMap = new Dictionary <string, string>();

            // Read the priv key list file
            string currentModulus = null, currentPEM = null;

            foreach (string l in File.ReadAllLines(rsaPrivFn))
            {
                string line = l.Trim();
                if (line.StartsWith("Public Modulus: "))
                {
                    currentModulus = line.Replace("Public Modulus: ", "");
                }

                if (line.StartsWith("-----BEGIN RSA PRIVATE KEY-----"))
                {
                    currentPEM = "";
                }
                currentPEM += line + "\n";
                if (line.StartsWith("-----END RSA PRIVATE KEY-----"))
                {
                    modulusKeyMap.Add(currentModulus, currentPEM);
                }
            }

            // Read the results file
            string modulus = null, exponent = null, address = null;

            foreach (string l in File.ReadAllLines(parms.InputResultsPath))
            {
                //string[] split = l.Trim().Split(":".ToCharArray(), 2);
                string line = l.Trim();
                if (line.StartsWith("Public Modulus: "))
                {
                    modulus = line.Replace("Public Modulus: ", "");
                }
                if (line.StartsWith("Public Exponent: "))
                {
                    exponent = line.Replace("Public Exponent: ", "");
                }
                if (line.StartsWith("Address/Hash: "))
                {
                    address = line.Replace("Address/Hash: ", "");
                }

                if (modulus != null && exponent != null && address != null)
                {
                    // Find the modulus in the private key map
                    string pem;
                    if (!modulusKeyMap.TryGetValue(modulus, out pem))
                    {
                        throw new InvalidDataException(String.Format("Modulus {0} is missing from the private key data file.", modulus));
                    }

                    // Load the PEM into the RSA
                    RSAWrapper rsa = new RSAWrapper();
                    rsa.FromPrivateKeyPEM(pem);

                    // Verify that modulus matches
                    if (rsa.Rsa.PublicModulus != BigNumber.FromDecimalString(modulus))
                    {
                        throw new InvalidDataException("Modulus of PEM does not match declared value.");
                    }

                    // Change the public exponent
                    rsa.ChangePublicExponent(BigNumber.FromDecimalString(exponent));

                    // Check the key's sanity
                    rsa.CheckSanity();

                    // Verify the hash
                    if (rsa.OnionHash + ".onion" != address)
                    {
                        throw new InvalidDataException("Onion hash of key does not match declared value.");
                    }

                    // Yay the key is good! Output it as required
                    CLRuntime.OutputKey(rsa);

                    modulus  = null;
                    exponent = null;
                    address  = null;
                }
            }
        }