Improve StrobeHasher
MODIFY
WalletWasabi/Crypto/StrobeProtocol/StrobeHasher.cs
WalletWasabi/Crypto/StrobeProtocol/StrobeHasher.cs
@@ -16,42 +16,42 @@ namespace WalletWasabi.Crypto.StrobeProtocol
strobe = new Strobe128(ProtocolConstants.WabiSabiProtocolIdentifier);
Append(ProtocolConstants.DomainStrobeSeparator, domain);
}
+ public static StrobeHasher Create(string domain)
+ => new (domain);
- public void Append(string fieldName, object fieldValue)
- => Append(fieldName, fieldValue switch
- {
- IBitcoinSerializable serializable => serializable.ToHex(),
- Money money => money.Satoshi.ToString(CultureInfo.InvariantCulture),
- string str => str,
- uint numUint => numUint.ToString(CultureInfo.InvariantCulture),
- ulong numUlong => numUlong.ToString(CultureInfo.InvariantCulture),
- decimal numDecimal => numDecimal.ToString(CultureInfo.InvariantCulture),
- CredentialIssuerParameters issuerParameters => issuerParameters.ToString(),
- _ => throw new ArgumentException($"{fieldValue.GetType().Name} doesn't have a string representation for strobe hasher.")
- });
-
- public void Append(string fieldName, string fieldValue)
+ public StrobeHasher Append(string fieldName, IBitcoinSerializable serializable)
+ => Append(fieldName, serializable.ToBytes());
+
+ public StrobeHasher Append(string fieldName, Money money)
+ => Append(fieldName, money.Satoshi);
+
+ public StrobeHasher Append(string fieldName, uint num)
+ => Append(fieldName, BitConverter.GetBytes(num));
+
+ public StrobeHasher Append(string fieldName, long num)
+ => Append(fieldName, BitConverter.GetBytes(num));
+
+ public StrobeHasher Append(string fieldName, ulong num)
+ => Append(fieldName, BitConverter.GetBytes(num));
+
+ public StrobeHasher Append(string fieldName, CredentialIssuerParameters issuerParameters)
+ => Append($"{fieldName}.Cw", issuerParameters.Cw.ToBytes())
+ .Append($"{fieldName}.I", issuerParameters.I.ToBytes());
+
+ public StrobeHasher Append(string fieldName, string str)
+ => Append($"{fieldName}.Cw", Encoding.UTF8.GetBytes(str));
+
+ public StrobeHasher Append(string fieldName, byte[] serializedValue)
{
strobe.AddAssociatedMetaData(Encoding.UTF8.GetBytes(fieldName), false);
-
- var serializedValue = Encoding.UTF8.GetBytes(fieldValue);
strobe.AddAssociatedMetaData(BitConverter.GetBytes(serializedValue.Length), true);
strobe.AddAssociatedData(serializedValue, false);
+ return this;
}
public uint256 GetHash()
{
return new uint256(strobe.Prf(32, false));
}
-
- public static uint256 Combine(string domain, Dictionary<string, object> elements)
- {
- var hasher = new StrobeHasher(domain);
- foreach (var element in elements)
- {
- hasher.Append(element.Key, element.Value);
- }
- return hasher.GetHash();
- }
}
}

MODIFY
WalletWasabi/WabiSabi/Backend/Models/Alice.cs
WalletWasabi/WabiSabi/Backend/Models/Alice.cs
@@ -35,13 +35,10 @@ namespace WalletWasabi.WabiSabi.Backend.Models
}
private uint256 CalculateHash()
- => StrobeHasher.Combine(
- ProtocolConstants.AliceStrobeDomain,
- new()
- {
- { ProtocolConstants.AliceCoinTxOutStrobeLabel, Coin.TxOut },
- { ProtocolConstants.AliceCoinOutpointStrobeLabel, Coin.Outpoint },
- { ProtocolConstants.AliceOwnershipProofStrobeLabel, OwnershipProof },
- });
+ => StrobeHasher.Create(ProtocolConstants.AliceStrobeDomain)
+ .Append(ProtocolConstants.AliceCoinTxOutStrobeLabel, Coin.TxOut)
+ .Append(ProtocolConstants.AliceCoinOutpointStrobeLabel, Coin.Outpoint)
+ .Append(ProtocolConstants.AliceOwnershipProofStrobeLabel, OwnershipProof)
+ .GetHash();
}
}

MODIFY
WalletWasabi/WabiSabi/Backend/Rounds/Round.cs
WalletWasabi/WabiSabi/Backend/Rounds/Round.cs
@@ -130,17 +130,14 @@ namespace WalletWasabi.WabiSabi.Backend.Rounds
=> CoinjoinState.AssertSigning().AddWitness(index, witness);
private uint256 CalculateHash()
- => StrobeHasher.Combine(
- ProtocolConstants.RoundStrobeDomain,
- new()
- {
- { ProtocolConstants.RoundMaxInputCountByAliceStrobeLabel, MaxInputCountByAlice },
- { ProtocolConstants.RoundMinRegistrableAmountStrobeLabel, MinRegistrableAmount },
- { ProtocolConstants.RoundMaxRegistrableAmountStrobeLabel, MaxRegistrableAmount },
- { ProtocolConstants.RoundPerAliceVsizeAllocationStrobeLabel, PerAliceVsizeAllocation },
- { ProtocolConstants.RoundAmountCredentialIssuerParametersStrobeLabel, AmountCredentialIssuerParameters },
- { ProtocolConstants.RoundVsizeCredentialIssuerParametersStrobeLabel, VsizeCredentialIssuerParameters },
- { ProtocolConstants.RoundFeeRateStrobeLabel, FeeRate.SatoshiPerByte }
- });
+ => StrobeHasher.Create(ProtocolConstants.RoundStrobeDomain)
+ .Append(ProtocolConstants.RoundMaxInputCountByAliceStrobeLabel, MaxInputCountByAlice)
+ .Append(ProtocolConstants.RoundMinRegistrableAmountStrobeLabel, MinRegistrableAmount)
+ .Append(ProtocolConstants.RoundMaxRegistrableAmountStrobeLabel, MaxRegistrableAmount)
+ .Append(ProtocolConstants.RoundPerAliceVsizeAllocationStrobeLabel, PerAliceVsizeAllocation)
+ .Append(ProtocolConstants.RoundAmountCredentialIssuerParametersStrobeLabel, AmountCredentialIssuerParameters)
+ .Append(ProtocolConstants.RoundVsizeCredentialIssuerParametersStrobeLabel, VsizeCredentialIssuerParameters)
+ .Append(ProtocolConstants.RoundFeeRateStrobeLabel, FeeRate.FeePerK)
+ .GetHash();
}
}

Improve StrobeHasher