public static void BulkSpeedTest(HashInfo hashInfo) { Console.WriteLine("[BULK SPEED TEST]"); Console.WriteLine($"Frequency: {s_Frequency / 1e6d:F2} MHz"); Console.WriteLine($"Keys Length: {BST_SIZE} Bytes"); Console.WriteLine($"Repetitions: {BST_TRIALS}"); using (SpeedTestOptimizer()) { for (Int32 i = 0; i < WARMUP_ITERATIONS; ++i) { GetAverageSpeed(hashInfo, BST_SIZE, BST_TRIALS, 0); } Double[] speed = new Double[8]; for (Int32 align = 0; align < 8; ++align) { speed[align] = GetAverageSpeed(hashInfo, BST_SIZE, BST_TRIALS, align); Console.WriteLine($" - Alignment {align}: {FormatSpeed(speed[align])}"); } Console.WriteLine($" - Average Speed: {FormatSpeed(StatsUtilities.Mean(speed))}"); } }
private static Double GetAverageSpeed(HashInfo hashInfo, Int32 length, Int32 repetitions, Int32 align) { RandomXS r = new RandomXS(); Byte[] key = new Byte[length + 512]; unsafe { fixed(Byte *pin = key) { UInt64 pinValue = (UInt64)pin; UInt64 alignValue = ((pinValue + 255ul) & 0xFFFFFFFFFFFFFF00ul) + (UInt64)align; Int32 offset = (Int32)(alignValue - pinValue); List <Double> results = new List <Double>(repetitions); for (Int32 i = 0; i < repetitions; ++i) { r.NextBytes(key, offset, length); Hash hash = hashInfo.Initializer((UInt32)i); Double start = NativeMethods.GetTime(); hash.ComputeHash(key, offset, length); Double end = NativeMethods.GetTime(); Double ms = ((end - start + 1.0d) * 1000.0d) / s_Frequency; Double bps = (length * 1000.0d) / ms; if (bps >= 0.0d) { results.Add(bps); } } Double mean = StatsUtilities.Mean(results); Double threshold = 2.0d * StatsUtilities.StandardDeviation(results, mean); for (Int32 i = results.Count - 1; i >= 0; --i) { if (Math.Abs(results[i] - mean) > threshold) { results.RemoveAt(i); } } return(StatsUtilities.Mean(results)); } } }
public static void DifferentialTest(HashInfo hashInfo) { RandomXS r = new RandomXS(0x000000B2u); Int32 hashBytes = hashInfo.Length / 8; Int32 hashBits = hashInfo.Length; Console.WriteLine("[[ DIFFERENTIAL TEST ]]"); Console.WriteLine($"Maximum Errors: {DT_MAXIMUMERRORS}"); Boolean resultOverall = true; for (Int32 i = 0; i < s_ParametersDT.Length; ++i) { dynamic p = s_ParametersDT[i]; Int32 keysBytes = p.KeysBytes; Int32 keysBits = keysBytes * 8; Byte[] key1 = new Byte[keysBytes]; Byte[] key2 = new Byte[keysBytes]; Int32 bits = p.Bits; Int32 repetitions = p.Repetitions; Int32 steps = repetitions / 10; Double tests = StatsUtilities.ChooseUpToK(keysBits, p.Bits) * repetitions; Double expectedCollisions = Math.Round(tests / Math.Pow(2.0d, hashBits)); Console.WriteLine($"> Bits: 1-{bits}"); Console.WriteLine($" Repetitions: {repetitions}"); Console.WriteLine($" Tests: {tests:F0}"); Console.WriteLine($" Expected Collisions: {expectedCollisions:F0}"); Console.Write(" Result: "); Byte[] h2 = new Byte[hashBytes]; UInt64 skipped = 0; List <Byte[]> diffs = new List <Byte[]>(DT_MAXIMUMERRORS); for (Int32 j = 0; j < repetitions; ++j) { if ((j % steps) == 0) { Console.Write((skipped > 0) ? "!" : "."); } r.NextBytes(key1); UnsafeBuffer.BlockCopy(key1, 0, key2, 0, keysBytes); Hash hash = hashInfo.Initializer(r.NextValue()); Byte[] h1 = hash.ComputeHash(key1); skipped += DifferentialTestRecursion(diffs, hash, keysBytes, key1, key2, hashBytes, h1, h2, 0, p.Bits); } Boolean result = true; if (skipped > 0) { result = false; Console.WriteLine(" FAILED"); Console.WriteLine(" - Errors Threshold Exceeded"); } else { Int32 ignoredCollisions = DifferentialTestIgnored(diffs, ref result); Console.WriteLine(result ? " PASSED" : " FAILED"); Console.WriteLine($" - Observed Collisions: {diffs.Count} ({ignoredCollisions} Ignored)"); } resultOverall &= result; } Console.WriteLine($"Test Result: {(resultOverall ? "PASSED" : "FAILED")}"); }