Test scalar randomness.
MODIFY
WalletWasabi.Tests/UnitTests/Crypto/RandomTests.cs
WalletWasabi.Tests/UnitTests/Crypto/RandomTests.cs
@@ -114,5 +114,35 @@ namespace WalletWasabi.Tests.UnitTests.Crypto
}
}
}
+
+ [Fact]
+ public void ScalarInternalTests()
+ {
+ var mockRandom = new MockRandom();
+ IWasabiRandom iWasabiRandom = mockRandom;
+
+ mockRandom.GetBytesResults.Add(Scalar.Zero.ToBytes()); // The random should not be zero.
+ // ToDo: EC.N + new Scalar(1) will be Scalar(1) and the overflow will not be set, so it'd be valid. Investigate if it's good like this.
+ mockRandom.GetBytesResults.Add(EC.N.ToBytes()); // The random should not overfow.
+ mockRandom.GetBytesResults.Add(new Scalar(uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue).ToBytes()); // The random should not overfow.
+
+ var one = new Scalar(1);
+ mockRandom.GetBytesResults.Add(one.ToBytes());
+ var two = new Scalar(2);
+ mockRandom.GetBytesResults.Add(two.ToBytes());
+ var big = new Scalar(int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue);
+ mockRandom.GetBytesResults.Add(big.ToBytes());
+ var biggest = EC.N + one.Negate();
+ mockRandom.GetBytesResults.Add(biggest.ToBytes());
+
+ var randomScalar = iWasabiRandom.GetScalarNonZero();
+ Assert.Equal(one, randomScalar);
+ randomScalar = iWasabiRandom.GetScalarNonZero();
+ Assert.Equal(two, randomScalar);
+ randomScalar = iWasabiRandom.GetScalarNonZero();
+ Assert.Equal(big, randomScalar);
+ randomScalar = iWasabiRandom.GetScalarNonZero();
+ Assert.Equal(biggest, randomScalar);
+ }
}
}

MODIFY
WalletWasabi/Crypto/Randomness/MockRandom.cs
WalletWasabi/Crypto/Randomness/MockRandom.cs
using NBitcoin.Secp256k1;
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Text;
namespace WalletWasabi.Crypto.Randomness
{
public class MockRandom : IWasabiRandom
{
+ public List<byte[]> GetBytesResults { get; } = new List<byte[]>();
+
public void GetBytes(byte[] output)
{
- throw new NotImplementedException();
+ var first = GetBytesResults.First();
+ GetBytesResults.RemoveFirst();
+ Buffer.BlockCopy(first, 0, output, 0, first.Length);
}
public void GetBytes(Span<byte> output)
{
- throw new NotImplementedException();
+ var first = GetBytesResults.First();
+ GetBytesResults.RemoveFirst();
+ first.AsSpan().CopyTo(output);
}
}
}

Test scalar randomness.