Add basic random tests
MODIFY
WalletWasabi.Tests/UnitTests/Crypto/RandomTests.cs
WalletWasabi.Tests/UnitTests/Crypto/RandomTests.cs
@@ -2,12 +2,47 @@ using System;
using System.Collections.Generic;
using System.Text;
using WalletWasabi.Crypto.Randomness;
+using WalletWasabi.Helpers;
using Xunit;
namespace WalletWasabi.Tests.UnitTests.Crypto
{
public class RandomTests
{
+ [Fact]
+ public void BasicTests()
+ {
+ // Make sure byte array comparision works within hashset and that the underlying API won't pull the floor out.
+ var byteArray = new byte[] { 1, 2, 3 };
+ var sameByteArray = new byte[] { 1, 2, 3 };
+ var differentByteArray = new byte[] { 4, 5, 6 };
+ var twoSet = new HashSet<byte[]>(new ByteArrayEqualityComparer())
+ {
+ byteArray,
+ sameByteArray,
+ differentByteArray
+ };
+
+ Assert.True(ByteHelpers.CompareFastUnsafe(byteArray, sameByteArray));
+ Assert.False(ByteHelpers.CompareFastUnsafe(byteArray, differentByteArray));
+ Assert.Equal(2, twoSet.Count);
+
+ // It's probabilistically ensured that it never produces the same scalar, so unit test should pass always.
+ var pseudoSet = new HashSet<byte[]>();
+ var secureSet = new HashSet<byte[]>();
+ var count = 100;
+ IWasabiRandom iPseudoRandom = new SecureRandom();
+ using var secureRandom = new SecureRandom();
+ IWasabiRandom iSecureRandom = new SecureRandom();
+ for (int i = 0; i < count; i++)
+ {
+ pseudoSet.Add(iPseudoRandom.GetBytes(10));
+ secureSet.Add(iSecureRandom.GetBytes(10));
+ }
+ Assert.Equal(count, pseudoSet.Count);
+ Assert.Equal(count, secureSet.Count);
+ }
+
[Fact]
public void GetBytesArgumentTests()
{

ADD
WalletWasabi/Helpers/ByteArrayEqualityComparer.cs
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Text;
+
+namespace WalletWasabi.Helpers
+{
+ public class ByteArrayEqualityComparer : IEqualityComparer<byte[]>
+ {
+ public bool Equals([AllowNull] byte[] x, [AllowNull] byte[] y) => ByteHelpers.CompareFastUnsafe(x, y);
+
+ public int GetHashCode([DisallowNull] byte[] obj) => HashHelpers.ComputeHashCode(obj);
+ }
+}

MODIFY
WalletWasabi/Helpers/HashHelpers.cs
WalletWasabi/Helpers/HashHelpers.cs
@@ -21,5 +21,29 @@ namespace WalletWasabi.Helpers
return hash;
}
+
+ /// <summary>
+ /// https://stackoverflow.com/a/468084/2061103
+ /// </summary>
+ public static int ComputeHashCode(params byte[] data)
+ {
+ unchecked
+ {
+ const int P = 16777619;
+ int hash = (int)2166136261;
+
+ for (int i = 0; i < data.Length; i++)
+ {
+ hash = (hash ^ data[i]) * P;
+ }
+
+ hash += hash << 13;
+ hash ^= hash >> 7;
+ hash += hash << 3;
+ hash ^= hash >> 17;
+ hash += hash << 5;
+ return hash;
+ }
+ }
}
}

Add basic random tests