Update WabiSabi code with Nullable Reference Types
MODIFY
WalletWasabi.Tests/UnitTests/Crypto/GroupElements/GeneralTests.cs
WalletWasabi.Tests/UnitTests/Crypto/GroupElements/GeneralTests.cs
@@ -126,7 +126,7 @@ namespace WalletWasabi.Tests.UnitTests.Crypto.GroupElements
var ge = new GroupElement(EC.G * one);
// Kinda clunky, but otherwise CodeFactor won't be happy.
- GroupElement n = null;
+ GroupElement? n = null;
Assert.False(ge == n);
Assert.True(ge != n);

MODIFY
WalletWasabi.Tests/UnitTests/Crypto/GroupElements/OperationTests.cs
WalletWasabi.Tests/UnitTests/Crypto/GroupElements/OperationTests.cs
@@ -12,11 +12,6 @@ namespace WalletWasabi.Tests.UnitTests.Crypto.GroupElements
[Fact]
public void Addition()
{
- Assert.Throws<ArgumentNullException>(() => GroupElement.G + null);
- Assert.Throws<ArgumentNullException>(() => null + GroupElement.G);
- Assert.Throws<ArgumentNullException>(() => GroupElement.Infinity + null);
- Assert.Throws<ArgumentNullException>(() => null + GroupElement.Infinity);
-
var gen1 = GroupElement.Infinity + GroupElement.G;
var gen2 = GroupElement.G + GroupElement.Infinity;
var inf = GroupElement.Infinity + GroupElement.Infinity;
@@ -46,11 +41,6 @@ namespace WalletWasabi.Tests.UnitTests.Crypto.GroupElements
[Fact]
public void Subtraction()
{
- Assert.Throws<ArgumentNullException>(() => GroupElement.G - null);
- Assert.Throws<ArgumentNullException>(() => null - GroupElement.G);
- Assert.Throws<ArgumentNullException>(() => GroupElement.Infinity - null);
- Assert.Throws<ArgumentNullException>(() => null - GroupElement.Infinity);
-
var minusGen = GroupElement.Infinity - GroupElement.G;
var gen = GroupElement.G - GroupElement.Infinity;
var inf = GroupElement.Infinity - GroupElement.Infinity;

MODIFY
WalletWasabi.Tests/UnitTests/Crypto/RandomTests.cs
WalletWasabi.Tests/UnitTests/Crypto/RandomTests.cs
@@ -71,7 +71,7 @@ namespace WalletWasabi.Tests.UnitTests.Crypto
foreach (var random in randoms.Where(x => x is IDisposable))
{
- (random as IDisposable).Dispose();
+ (random as IDisposable)?.Dispose();
}
}
@@ -107,7 +107,7 @@ namespace WalletWasabi.Tests.UnitTests.Crypto
foreach (var random in randoms.Where(x => x is IDisposable))
{
- (random as IDisposable).Dispose();
+ (random as IDisposable)?.Dispose();
}
}

MODIFY
WalletWasabi/Crypto/GroupElement.cs
WalletWasabi/Crypto/GroupElement.cs
@@ -38,13 +38,13 @@ namespace WalletWasabi.Crypto
public bool IsInfinity => Ge.IsInfinity;
- public override bool Equals(object obj) => Equals(obj as GroupElement);
+ public override bool Equals(object? obj) => Equals(obj as GroupElement);
- public bool Equals(GroupElement other) => this == other;
+ public bool Equals(GroupElement? other) => this == other;
public override int GetHashCode() => Ge.GetHashCode();
- public static bool operator ==(GroupElement a, GroupElement b)
+ public static bool operator ==(GroupElement? a, GroupElement? b)
{
if (a is null && b is null)
{
@@ -64,7 +64,7 @@ namespace WalletWasabi.Crypto
}
}
- public static bool operator !=(GroupElement a, GroupElement b) => !(a == b);
+ public static bool operator !=(GroupElement? a, GroupElement? b) => !(a == b);
/// <summary>
/// ToString is only used for nice visual representation during debugging. Do not rely on the result for anything else.
@@ -86,26 +86,14 @@ namespace WalletWasabi.Crypto
}
public static GroupElement operator +(GroupElement a, GroupElement b)
- {
- Guard.NotNull(nameof(a), a);
- Guard.NotNull(nameof(b), b);
-
- return new GroupElement(a.Ge.ToGroupElementJacobian().AddVariable(b.Ge, out _));
- }
+ => new GroupElement(a.Ge.ToGroupElementJacobian().AddVariable(b.Ge, out _));
public static GroupElement operator -(GroupElement a, GroupElement b)
- {
- Guard.NotNull(nameof(a), a);
- Guard.NotNull(nameof(b), b);
-
- return a + new GroupElement(b.Ge.Negate());
- }
+ => a + new GroupElement(b.Ge.Negate());
/// <param name="scalar">It's ok for the scalar to overflow.</param>
public static GroupElement operator *(Scalar scalar, GroupElement groupElement)
{
- Guard.NotNull(nameof(GroupElement), groupElement);
-
// For some strange reason scalar * GE.Infinity isn't infinity. Let's fix it as it should be, since:
// 2 * GE.Infinity = GE.Infinity + GE.Infinity = GE.Infinity.
if (groupElement.IsInfinity)

MODIFY
WalletWasabi/Crypto/StringCipher.cs
WalletWasabi/Crypto/StringCipher.cs
@@ -22,9 +22,9 @@ namespace WalletWasabi.Crypto
// Salt is randomly generated each time, but is preprended to encrypted cipher text
// so that the same Salt value can be used when decrypting.
byte[] salt = Generate128BitsOfRandomEntropy();
- byte[] iv = null;
- byte[] cipherTextBytes = null;
- byte[] key = null;
+ byte[] iv;
+ byte[] cipherTextBytes;
+ byte[] key;
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (var password = new Rfc2898DeriveBytes(passPhrase, salt, DerivationIterations))
@@ -68,8 +68,8 @@ namespace WalletWasabi.Crypto
public static string Decrypt(string cipherText, string passPhrase)
{
var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
- byte[] key = null;
- byte[] iv = null;
+ byte[] key;
+ byte[] iv;
using var memoryStream = new MemoryStream(cipherTextBytesWithSaltAndIv);
var cipherLength = 0;

Update WabiSabi code with Nullable Reference Types