public void ComparisonIsConstantTime() { var runTimes = new List <RunTime>(); var a = new byte[128]; var b = new byte[128]; for (var i = 0; i < 100000; i++) { var differencePosition = i % a.Length; b[differencePosition] = 0x01; var stopwatch = Stopwatch.StartNew(); HashComparer.Compare(a, b); stopwatch.Stop(); if (i > 10) // Exclude first runs to exclude startup effects { runTimes.Add(new RunTime(differencePosition, stopwatch.ElapsedTicks)); } b[differencePosition] = 0x00; } var correlation = StatisticalOperations.Correlation( runTimes.Select(x => (double)x.DifferencePosition).ToArray(), runTimes.Select(x => (double)x.ElapsedTicks).ToArray()); Console.WriteLine($"Correlation: {correlation}"); Assert.That(correlation, Is.EqualTo(0).Within(0.01), "Correlation"); }
public List <WriterDescription> GetDestination(InnerData ev) { _lock.EnterReadLock(); var ret = new List <WriterDescription>(); if (_servers.Count(x => x.IsAvailable) >= _configuration.CountReplics) { string current = ev.Transaction.EventHash; for (int i = 0; i < _configuration.CountReplics; i++) { var find = _map.AvailableMap.FirstOrDefault( x => HashComparer.Compare(current, x.End) <= 0 && !ret.Contains(x.ServerId)); if (find == null && _map.AvailableMap.Count > 0) { current = Consts.StartHashInRing; find = _map.AvailableMap.FirstOrDefault( x => HashComparer.Compare(current, x.End) <= 0 && !ret.Contains(x.ServerId)); } if (find == null) { Logger.Logger.Instance.Error(Errors.NotEnoughServers); ret.Clear(); break; } current = find.End; ret.Add(find.ServerId); } } _lock.ExitReadLock(); return(ret); }
public void EqualArraysReturnTrue() { var a = new byte[] { 0x41, 0x42, 0x43 }; var b = new byte[] { 0x41, 0x42, 0x43 }; Assert.That(HashComparer.Compare(a, b), Is.True); }
public void DifferentArraysReturnFalse() { var a = new byte[] { 0x41, 0x42, 0x43 }; var b = new byte[] { 0x01, 0x02, 0x03 }; Assert.That(HashComparer.Compare(a, b), Is.False); }
public void ArraysOfDifferentLengthReturnFalse() { var a = new byte[3]; var b = new byte[4]; Assert.That(HashComparer.Compare(a, b), Is.False); }
public bool IsMine(MetaData data) { return (_hash.Exists( x => HashComparer.Compare(x.Key, data.Hash) <= 0 && HashComparer.Compare(data.Hash, x.Value) <= 0)); }
/// <summary> /// Takes two hashes, sorts them, and concatenates them. /// </summary> private static byte[] JoinHashesPairwise(byte[] h1, byte[] h2) { if (h1.Length == 0) { return(h2); } if (h2.Length == 0) { return(h1); } HashComparer comparer = new HashComparer(); if (comparer.Compare(h1, h2) < 0) { return(h1.Concat(h2).ToArray()); } else { return(h2.Concat(h1).ToArray()); } }
public bool IsMine(string hash) { return(HashComparer.Compare(Begin, hash) <= 0 && HashComparer.Compare(hash, End) <= 0); }