public static string FindNonce(byte[] blockHeaderWithoutOnce, int difficulty, UInt32 maxIteration, UInt256 targetDiff) { byte[] nonce; var hashTable = new Hashtable(); var extraPadding = ConvertHexStringToByte("000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"); //var targetDiff = DifficultyToTarget(difficulty); var foundNone = ""; Parallel.For(0, maxIteration, (i, loopState) => { nonce = BitConverter.GetBytes(i).Take(4).ToArray(); var newBlockHeader = new List <byte>(blockHeaderWithoutOnce); newBlockHeader.AddRange(nonce); newBlockHeader.AddRange(extraPadding); var hash = BuildDoubleHash(newBlockHeader.ToArray()); var str = ConvertByteToString(hash); //verify difficulty var hashUint256 = new UInt256(hash); if (hashUint256.CompareTo(targetDiff) <= 0 || str.StartsWith("0000")) { Console.WriteLine(str); foundNone = ConvertByteToString(nonce); loopState.Break(); } }); return(foundNone); }
protected override List<uint> ScanHash_CryptoPP(MinerData md, UInt256 target) { uint nonce = (uint)md.nHashesDone; List<uint> results = new List<uint>(1); UInt256 hashResult = new UInt256(); byte[] tmp = new byte[16 * 4]; DateTime endLhutc = DateTime.UtcNow + new TimeSpan(0, 0, 1); long count = 0; while (true) { count++; Single(md, hashResult, nonce, tmp); var lastInt = BitConverter.ToInt32(hashResult.bytes, 7*4); if (lastInt == 0 && hashResult.CompareTo(target) < 0) results.Add(nonce); nonce++; if (DateTime.UtcNow >= endLhutc || nonce == 0) break; } md.nHashesDone += count; HashedSome(count); return results; }
public void TestCompareTo() { byte[] temp = new byte[32]; temp[31] = 0x01; UInt256 result = new UInt256(temp); Assert.AreEqual(0, UInt256.Zero.CompareTo(UInt256.Zero)); Assert.AreEqual(-1, UInt256.Zero.CompareTo(result)); Assert.AreEqual(1, result.CompareTo(UInt256.Zero)); }
public UInt256 GetHashTarget() { UInt256 hashTarget = new UInt256("00000000ffff0000000000000000000000000000000000000000000000000000"); UInt256 maxHashTarget = new UInt256("00000000ffffffff000000000000000000000000000000000000000000000000"); if (Difficulty == 1.0) { } else { // divide by difficulty to figure out the share target var asDouble = (double)(hashTarget.ToBigInteger()); var adjusted = asDouble / Difficulty; hashTarget = new UInt256((System.Numerics.BigInteger)adjusted); if (hashTarget.CompareTo(maxHashTarget) > 0) hashTarget = maxHashTarget; } return hashTarget; }