Improving Guards
MODIFY
WalletWasabi.Tests/UnitTests/Crypto/ZeroKnowledge/LinearRelationTests.cs
WalletWasabi.Tests/UnitTests/Crypto/ZeroKnowledge/LinearRelationTests.cs
@@ -141,17 +141,20 @@ namespace WalletWasabi.Tests.UnitTests.Crypto.ZeroKnowledge
);
// The witness should have the same number of components as the number of generators in the equations
- Assert.ThrowsAny<ArgumentException>(() => new Knowledge(statement, new ScalarVector(Scalar.Zero, Scalar.Zero)));
+ var ex = Assert.ThrowsAny<ArgumentException>(() => new Knowledge(statement, new ScalarVector(Scalar.Zero, Scalar.Zero)));
+ Assert.Contains("size does not match", ex.Message);
// Incorrect witness (multiplying by generators does not produce the public point)
- Assert.ThrowsAny<ArgumentException>(() => new Knowledge(statement, new ScalarVector(Scalar.One)));
+ ex = Assert.ThrowsAny<ArgumentException>(() => new Knowledge(statement, new ScalarVector(Scalar.One)));
+ Assert.Contains("witness is not solution of the equation", ex.Message);
// Incorrect statement generators (effectively incorrect witness)
var badStatement = new Statement(
new Equation(a, new GroupElementVector(Generators.Gh)),
new Equation(b, new GroupElementVector(Generators.Gg))
);
- Assert.ThrowsAny<ArgumentException>(() => new Knowledge(badStatement, new ScalarVector(Scalar.One)));
+ ex = Assert.ThrowsAny<ArgumentException>(() => new Knowledge(badStatement, new ScalarVector(Scalar.One)));
+ Assert.Contains("witness is not solution of the equation", ex.Message);
}
}
}

MODIFY
WalletWasabi/Crypto/ZeroKnowledge/LinearRelation/Knowledge.cs
WalletWasabi/Crypto/ZeroKnowledge/LinearRelation/Knowledge.cs
@@ -11,12 +11,12 @@ namespace WalletWasabi.Crypto.ZeroKnowledge.LinearRelation
{
Guard.NotNull(nameof(statement), statement);
Guard.NotNullOrEmpty(nameof(witness), witness);
- Guard.True($"{nameof(witness)} size does not match {nameof(statement)}.{nameof(statement.Equations)}", witness.Count() == statement.Equations.First().Generators.Count());
+ Guard.True(nameof(witness), witness.Count() == statement.Equations.First().Generators.Count(), $"{nameof(witness)} size does not match {nameof(statement)}.{nameof(statement.Equations)}");
// don't try to prove something which isn't true
foreach (var equation in statement.Equations)
{
- Guard.True($"{nameof(witness)} is not solution of the {nameof(equation)}", equation.VerifySolution(witness));
+ Guard.True(nameof(witness), equation.VerifySolution(witness), $"{nameof(witness)} is not solution of the {nameof(equation)}");
}
Statement = statement;

MODIFY
WalletWasabi/Helpers/Guard.cs
WalletWasabi/Helpers/Guard.cs
@@ -6,19 +6,19 @@ namespace WalletWasabi.Helpers
{
public static class Guard
{
- public static bool True(string parameterName, bool? value)
- => AssertBool(parameterName, true, value);
+ public static bool True(string parameterName, bool? value, string? description = null)
+ => AssertBool(parameterName, true, value, description);
- public static bool False(string parameterName, bool? value)
- => AssertBool(parameterName, false, value);
+ public static bool False(string parameterName, bool? value, string? description = null)
+ => AssertBool(parameterName, false, value, description);
- private static bool AssertBool(string parameterName, bool expectedValue, bool? value)
+ private static bool AssertBool(string parameterName, bool expectedValue, bool? value, string? description = null)
{
NotNull(parameterName, value);
if (value != expectedValue)
{
- throw new ArgumentOutOfRangeException(parameterName, value, $"Parameter must be {expectedValue}.");
+ throw new ArgumentOutOfRangeException(parameterName, value, description ?? $"Parameter must be {expectedValue}.");
}
return (bool)value;

Improving Guards