Remove unnecessary validations
MODIFY
WalletWasabi/Crypto/CoordinatorParameters.cs
WalletWasabi/Crypto/CoordinatorParameters.cs
using WalletWasabi.Crypto.Groups;
+using WalletWasabi.Helpers;
namespace WalletWasabi.Crypto
{
@@ -6,8 +7,8 @@ namespace WalletWasabi.Crypto
{
public CoordinatorParameters(GroupElement cw, GroupElement i)
{
- Cw = CryptoGuard.NotNullOrInfinity(nameof(cw), cw);
- I = CryptoGuard.NotNullOrInfinity(nameof(i), i);
+ Cw = Guard.NotNullOrInfinity(nameof(cw), cw);
+ I = Guard.NotNullOrInfinity(nameof(i), i);
}
public GroupElement Cw { get; }

DELETE
WalletWasabi/Crypto/CryptoGuards.cs
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using NBitcoin.Secp256k1;
-using WalletWasabi.Crypto.Groups;
-
-namespace WalletWasabi.Crypto
-{
- public static class CryptoGuard
- {
- [DebuggerStepThrough]
- public static GroupElement NotNullOrInfinity(string parameterName, GroupElement groupElement)
- => groupElement?.IsInfinity switch
- {
- null => throw new ArgumentNullException(parameterName),
- true => throw new ArgumentException("Point at infinity is not a valid value.", parameterName),
- false => groupElement
- };
-
- [DebuggerStepThrough]
- public static IEnumerable<GroupElement> NotNullOrInfinity(string parameterName, IEnumerable<GroupElement> groupElements)
- => groupElements switch
- {
- null => throw new ArgumentNullException(parameterName),
- _ when !groupElements.Any() => throw new ArgumentException(parameterName),
- _ => groupElements.Select((ge, i) => NotNullOrInfinity($"{parameterName}[{i}]", ge)).ToList()
- };
-
- [DebuggerStepThrough]
- public static Scalar NotZero(string parameterName, Scalar scalar)
- => scalar switch
- {
- _ when scalar.IsZero => throw new ArgumentException("Value cannot be zero.", parameterName),
- _ => scalar
- };
- }
-}

MODIFY
WalletWasabi/Crypto/Mac.cs
WalletWasabi/Crypto/Mac.cs
@@ -9,8 +9,8 @@ namespace WalletWasabi.Crypto
{
private MAC(Scalar t, GroupElement v)
{
- T = CryptoGuard.NotZero(nameof(t), t);
- V = CryptoGuard.NotNullOrInfinity(nameof(v), v);
+ T = Guard.NotZero(nameof(t), t);
+ V = Guard.NotNullOrInfinity(nameof(v), v);
}
public Scalar T { get; }
@@ -31,8 +31,8 @@ namespace WalletWasabi.Crypto
public static MAC ComputeMAC(CoordinatorSecretKey sk, GroupElement ma, Scalar t)
{
Guard.NotNull(nameof(sk), sk);
- CryptoGuard.NotZero(nameof(t), t);
- CryptoGuard.NotNullOrInfinity(nameof(ma), ma);
+ Guard.NotZero(nameof(t), t);
+ Guard.NotNullOrInfinity(nameof(ma), ma);
return ComputeAlgebraicMAC((sk.X0, sk.X1), (sk.W * Generators.Gw) + (sk.Ya * ma), t);
}

MODIFY
WalletWasabi/Crypto/ZeroKnowledge/LinearRelation/Equation.cs
WalletWasabi/Crypto/ZeroKnowledge/LinearRelation/Equation.cs
@@ -23,9 +23,8 @@ namespace WalletWasabi.Crypto.ZeroKnowledge.LinearRelation
// representation of a specific point.
internal class Equation
{
- public Equation(GroupElement publicPoint, GroupElementVector generators)
+ internal Equation(GroupElement publicPoint, GroupElementVector generators)
{
- Guard.NotNullOrEmpty(nameof(generators), generators);
PublicPoint = Guard.NotNull(nameof(publicPoint), publicPoint);
Generators = generators;
}
@@ -63,7 +62,7 @@ namespace WalletWasabi.Crypto.ZeroKnowledge.LinearRelation
// challenge is 0), so only respond if that is the case
foreach (var secretNonce in secretNonces)
{
- CryptoGuard.NotZero(nameof(secretNonce), secretNonce);
+ Guard.NotZero(nameof(secretNonce), secretNonce);
}
// Taking the discrete logarithms of both sides of the verification

MODIFY
WalletWasabi/Crypto/ZeroKnowledge/LinearRelation/Knowledge.cs
WalletWasabi/Crypto/ZeroKnowledge/LinearRelation/Knowledge.cs
@@ -4,12 +4,10 @@ using WalletWasabi.Helpers;
namespace WalletWasabi.Crypto.ZeroKnowledge.LinearRelation
{
- public class Knowledge
+ internal class Knowledge
{
- public Knowledge(Statement statement, ScalarVector witness)
+ internal Knowledge(Statement statement, ScalarVector witness)
{
- Guard.NotNull(nameof(statement), statement);
- Guard.NotNullOrEmpty(nameof(witness), witness);
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

MODIFY
WalletWasabi/Crypto/ZeroKnowledge/Proof.cs
WalletWasabi/Crypto/ZeroKnowledge/Proof.cs
@@ -4,9 +4,9 @@ namespace WalletWasabi.Crypto.ZeroKnowledge
{
public class Proof
{
- public Proof(GroupElementVector publicNonces, ScalarVector responses)
+ internal Proof(GroupElementVector publicNonces, ScalarVector responses)
{
- CryptoGuard.NotNullOrInfinity(nameof(publicNonces), publicNonces);
+ Guard.NotNullOrInfinity(nameof(publicNonces), publicNonces);
Guard.NotNullOrEmpty(nameof(responses), responses);
PublicNonces = publicNonces;

MODIFY
WalletWasabi/Crypto/ZeroKnowledge/Transcript.cs
WalletWasabi/Crypto/ZeroKnowledge/Transcript.cs
@@ -53,14 +53,12 @@ namespace WalletWasabi.Crypto.ZeroKnowledge
public void CommitPublicNonces(IEnumerable<GroupElement> publicNonces)
{
- CryptoGuard.NotNullOrInfinity(nameof(publicNonces), publicNonces);
+ Guard.NotNullOrInfinity(nameof(publicNonces), publicNonces);
AddMessages(PublicNonceTag, publicNonces.Select(x => x.ToBytes()));
}
- public void CommitStatement(Statement statement)
+ internal void CommitStatement(Statement statement)
{
- Guard.NotNull(nameof(statement.Generators), statement.Generators);
- Guard.NotNull(nameof(statement.PublicPoints), statement.PublicPoints);
AddMessages(StatementTag, statement.PublicPoints.Select(x => x.ToBytes()).Concat(statement.Generators.Select(x => x.ToBytes())));
}

MODIFY
WalletWasabi/Helpers/Guard.cs
WalletWasabi/Helpers/Guard.cs
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
+using NBitcoin.Secp256k1;
+using WalletWasabi.Crypto.Groups;
namespace WalletWasabi.Helpers
{
@@ -179,5 +182,32 @@ namespace WalletWasabi.Helpers
? ""
: str.Trim();
}
+
+ [DebuggerStepThrough]
+ public static GroupElement NotNullOrInfinity(string parameterName, GroupElement groupElement)
+ => groupElement?.IsInfinity switch
+ {
+ null => throw new ArgumentNullException(parameterName),
+ true => throw new ArgumentException("Point at infinity is not a valid value.", parameterName),
+ false => groupElement
+ };
+
+ [DebuggerStepThrough]
+ public static IEnumerable<GroupElement> NotNullOrInfinity(string parameterName, IEnumerable<GroupElement> groupElements)
+ => groupElements switch
+ {
+ null => throw new ArgumentNullException(parameterName),
+ _ when !groupElements.Any() => throw new ArgumentException(parameterName),
+ _ => groupElements.Select((ge, i) => NotNullOrInfinity($"{parameterName}[{i}]", ge)).ToList()
+ };
+
+ [DebuggerStepThrough]
+ public static Scalar NotZero(string parameterName, Scalar scalar)
+ => scalar switch
+ {
+ _ when scalar.IsZero => throw new ArgumentException("Value cannot be zero.", parameterName),
+ _ => scalar
+ };
+
}
}

Remove unnecessary validations