/// <summary> /// This method returns the calculated amount of subbatches which is assumed to be the opimum. /// To determine all possible subbatches, the batchsize must be factorized into its primes. /// </summary> /// <param name="keyTranslator"></param> /// <returns></returns> public int GetAmountOfSubbatches(IKeyTranslator keyTranslator) { if (this.keyTranslator != keyTranslator) { //init code: this.keyTranslator = keyTranslator; //Find factors of OpenCL batch size: List <Msieve.Factor> factors = Msieve.TrivialFactorization(keyTranslator.GetOpenCLBatchSize()); amountOfSubbatchesFactors = new List <int>(); foreach (var fac in factors) { for (int i = 0; i < fac.count; i++) { amountOfSubbatchesFactors.Add((int)fac.factor); } } amountOfSubbatches = keyTranslator.GetOpenCLBatchSize(); batchSizeFactors = new List <int>(); DecreaseAmountOfSubbatches(); lastDuration = TimeSpan.MaxValue; optimisticDecrease = false; lastStepIncrease = false; if (openCLMode == 1) //normal load { DecreaseAmountOfSubbatches(); } } return(amountOfSubbatches); }
/// <summary> /// Generates the OpenCL code and returns it as a string. /// </summary> /// <param name="keyTranslator">The KeyTranslator to use. This is important for mapping the key movements to code.</param> /// <returns>The OpenCL code</returns> public string CreateOpenCLBruteForceCode(IKeyTranslator keyTranslator) { if (keyTranslatorOfCode == keyTranslator) { return(openCLCode); } int bytesUsed = controlCost.GetBytesToUse(); if (encryptedData.Length < bytesUsed) { bytesUsed = encryptedData.Length; } string code = encryptionController.GetOpenCLCode(bytesUsed, iv); if (code == null) { throw new Exception("OpenCL not supported in this configuration!"); } //put cost function stuff into code: code = controlCost.ModifyOpenCLCode(code); //put input to be bruteforced into code: string inputarray = string.Format("__constant unsigned char inn[{0}] = {{ \n", bytesUsed); for (int i = 0; i < bytesUsed; i++) { inputarray += String.Format("0x{0:X2}, ", this.encryptedData[i]); } inputarray = inputarray.Substring(0, inputarray.Length - 2); inputarray += "}; \n"; code = code.Replace("$$INPUTARRAY$$", inputarray); //put key movement of pattern into code: code = keyTranslator.ModifyOpenCLCode(code, approximateNumberOfKeys); keyTranslatorOfCode = keyTranslator; this.openCLCode = code; return(code); }
/// <summary> /// Generates the OpenCL code and creates the kernel out of it. /// </summary> /// <param name="oclManager">The OpenCL manager to use.</param> /// <param name="keyTranslator">The KeyTranslator to use. This is important for mapping the key movements to code.</param> /// <returns>The Kernel</returns> public Kernel GetBruteforceKernel(OpenCLManager oclManager, IKeyTranslator keyTranslator) { //caching: if (keyTranslatorOfCode == keyTranslator) { return(openCLKernel); } try { var program = oclManager.CompileSource(CreateOpenCLBruteForceCode(keyTranslator)); //keySearcher.GuiLogMessage(string.Format("Using OpenCL with (virtually) {0} threads.", keyTranslator.GetOpenCLBatchSize()), NotificationLevel.Info); openCLKernel = program.CreateKernel("bruteforceKernel"); return(openCLKernel); } catch (Exception ex) { throw new Exception(Resources.An_error_occured_when_trying_to_compile_OpenCL_code__ + ex.Message); } }
protected InputReader(TextReader reader, IKeyTranslator <T> translator) { this.Translator = translator; this.Reader = reader; }