-
Notifications
You must be signed in to change notification settings - Fork 492
/
BlockchainAnalyzer.cs
296 lines (259 loc) · 10.5 KB
/
BlockchainAnalyzer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using NBitcoin;
using System.Collections.Generic;
using System.Linq;
using WalletWasabi.Blockchain.Keys;
using WalletWasabi.Blockchain.Transactions;
namespace WalletWasabi.Blockchain.Analysis;
public class BlockchainAnalyzer
{
private static long[] StdDenoms = new[]
{
5000L, 6561L, 8192L, 10000L, 13122L, 16384L, 19683L, 20000L, 32768L, 39366L, 50000L, 59049L, 65536L, 100000L, 118098L,
131072L, 177147L, 200000L, 262144L, 354294L, 500000L, 524288L, 531441L, 1000000L, 1048576L, 1062882L, 1594323L, 2000000L,
2097152L, 3188646L, 4194304L, 4782969L, 5000000L, 8388608L, 9565938L, 10000000L, 14348907L, 16777216L, 20000000L,
28697814L, 33554432L, 43046721L, 50000000L, 67108864L, 86093442L, 100000000L, 129140163L, 134217728L, 200000000L,
258280326L, 268435456L, 387420489L, 500000000L, 536870912L, 774840978L, 1000000000L, 1073741824L, 1162261467L,
2000000000L, 2147483648L, 2324522934L, 3486784401L, 4294967296L, 5000000000L, 6973568802L, 8589934592L, 10000000000L,
10460353203L, 17179869184L, 20000000000L, 20920706406L, 31381059609L, 34359738368L, 50000000000L, 62762119218L,
68719476736L, 94143178827L, 100000000000L, 137438953472L
};
public BlockchainAnalyzer(int privacyLevelThreshold)
{
PrivacyLevelThreshold = privacyLevelThreshold;
}
public int PrivacyLevelThreshold { get; }
/// <summary>
/// Sets clusters and anonymity sets for related HD public keys.
/// </summary>
public void Analyze(SmartTransaction tx)
{
var ownInputCount = tx.WalletInputs.Count;
var foreignInputCount = tx.ForeignInputs.Count;
var foreignOutputCount = tx.ForeignOutputs.Count;
if (ownInputCount == 0)
{
AnalyzeReceive(tx);
}
else if (foreignInputCount == 0 && foreignOutputCount > 0)
{
AnalyzeNormalSpend(tx);
}
else
{
int startingOutputAnonset;
var distinctWalletInputPubKeys = tx.WalletInputs.Select(x => x.HdPubKey).ToHashSet();
if (foreignInputCount == 0)
{
startingOutputAnonset = AnalyzeSelfSpendWalletInputs(distinctWalletInputPubKeys);
AnalyzeSelfSpendWalletOutputs(tx, startingOutputAnonset);
}
else
{
AnalyzeCoinjoinWalletInputs(tx, out startingOutputAnonset, out int nonMixedAnonScore);
AnalyzeCoinjoinWalletOutputs(tx, startingOutputAnonset, nonMixedAnonScore, distinctWalletInputPubKeys);
}
AdjustWalletInputs(tx, distinctWalletInputPubKeys, startingOutputAnonset);
}
AnalyzeClusters(tx);
}
private static void AnalyzeCoinjoinWalletInputs(SmartTransaction tx, out int mixedAnonScore, out int nonMixedAnonScore)
{
// Consolidation in coinjoins is the only type of consolidation that's acceptable,
// because coinjoins are an exception from common input ownership heuristic.
// Calculate weighted average.
mixedAnonScore = (int)(tx.WalletInputs.Sum(x => x.HdPubKey.AnonymitySet * x.Amount) / tx.WalletInputs.Sum(x => x.Amount));
nonMixedAnonScore = tx.WalletInputs.Min(x => x.HdPubKey.AnonymitySet);
}
private int AnalyzeSelfSpendWalletInputs(HashSet<HdPubKey> distinctWalletInputPubKeys)
{
int startingOutputAnonset = Intersect(distinctWalletInputPubKeys.Select(x => x.AnonymitySet));
foreach (var key in distinctWalletInputPubKeys)
{
key.SetAnonymitySet(startingOutputAnonset);
}
return startingOutputAnonset;
}
/// <summary>
/// Estimate input cluster anonymity set size, penalizing input consolidations to accounting for intersection attacks.
/// </summary>
private int Intersect(IEnumerable<int> anonsets)
{
// Sanity check.
if (!anonsets.Any())
{
return 1;
}
// Our smallest anonset is the relevant here, because anonsets cannot grow by intersection punishments.
var smallestAnon = anonsets.Min();
// Punish intersection exponentially.
// If there is only a single anonset then the exponent should be zero to divide by 1 thus retain the input coin anonset.
var intersectPenalty = Math.Pow(2, anonsets.Count() - 1);
var intersectionAnonset = smallestAnon / Math.Max(1, intersectPenalty);
// The minimum anonymity set size is 1, enforce it when the punishment is very large.
var normalizedIntersectionAnonset = Math.Max(1, (int)intersectionAnonset);
return normalizedIntersectionAnonset;
}
private void AnalyzeCoinjoinWalletOutputs(SmartTransaction tx, int startingMixedOutputAnonset, int startingNonMixedOutputAnonset, ISet<HdPubKey> distinctWalletInputPubKeys)
{
var indistinguishableWalletOutputs = tx.WalletOutputs
.GroupBy(x => x.Amount)
.ToDictionary(x => x.Key, y => y.Count());
var indistinguishableOutputs = tx.Transaction.Outputs
.GroupBy(x => x.ScriptPubKey)
.ToDictionary(x => x.Key, y => y.Sum(z => z.Value))
.GroupBy(x => x.Value)
.ToDictionary(x => x.Key, y => y.Count());
var outputValues = tx.Transaction.Outputs.Select(x => x.Value).OrderByDescending(x => x).ToArray();
var secondLargestOutputAmount = outputValues.Distinct().OrderByDescending(x => x).Take(2).Last();
bool? isWasabi2Cj = null;
var foreignInputCount = tx.ForeignInputs.Count;
foreach (var newCoin in tx.WalletOutputs)
{
var output = newCoin.TxOut;
var equalOutputCount = indistinguishableOutputs[output.Value];
var ownEqualOutputCount = indistinguishableWalletOutputs[output.Value];
// Anonset gain cannot be larger than others' input count.
var anonset = Math.Min(equalOutputCount - ownEqualOutputCount, foreignInputCount);
// Picking randomly an output would make our anonset: total/ours.
anonset /= ownEqualOutputCount;
// If no anonset gain achieved on the output, then it's best to assume it's change.
int startingOutputAnonset;
if (anonset == 0)
{
isWasabi2Cj ??=
tx.Transaction.Inputs.Count >= 50 // 50 was the minimum input count at the beginning of Wasabi 2.
&& outputValues.Count(x => StdDenoms.Contains(x.Satoshi)) > tx.Transaction.Outputs.Count * 0.8 // Most of the outputs contains the denomination.
&& outputValues.SequenceEqual(outputValues); // Outputs are ordered descending.
// When WW2 denom output isn't too large, then it's not change.
if (isWasabi2Cj is true && StdDenoms.Contains(newCoin.Amount.Satoshi) && newCoin.Amount < secondLargestOutputAmount)
{
startingOutputAnonset = startingMixedOutputAnonset;
}
else
{
startingOutputAnonset = startingNonMixedOutputAnonset;
}
}
else
{
startingOutputAnonset = startingMixedOutputAnonset;
}
// Account for the inherited anonymity set size from the inputs in the
// anonymity set size estimate.
anonset += startingOutputAnonset;
HdPubKey hdPubKey = newCoin.HdPubKey;
uint256 txid = tx.GetHash();
if (hdPubKey.AnonymitySet == HdPubKey.DefaultHighAnonymitySet)
{
// If the new coin's HD pubkey haven't been used yet
// then its anonset haven't been set yet.
// In that case the acquired anonset does not have to be intersected with the default anonset,
// so this coin gets the acquired anonset.
hdPubKey.SetAnonymitySet(anonset, txid);
}
else if (distinctWalletInputPubKeys.Contains(hdPubKey))
{
// If it's a reuse of an input's pubkey, then intersection punishment is senseless.
hdPubKey.SetAnonymitySet(startingOutputAnonset, txid);
}
else if (tx.WalletOutputs.Where(x => x != newCoin).Select(x => x.HdPubKey).Contains(hdPubKey))
{
// If it's a reuse of another output's pubkey, then intersection punishment can only go as low as the inherited anonset.
hdPubKey.SetAnonymitySet(Math.Max(startingOutputAnonset, Intersect(new[] { anonset, hdPubKey.AnonymitySet })), txid);
}
else if (hdPubKey.OutputAnonSetReasons.Contains(txid))
{
// If we already processed this transaction for this script
// then we'll go with normal processing.
// It may be a duplicated processing or new information arrived (like other wallet loaded)
// If there are more anonsets already
// then it's address reuse that we have already punished so leave it alone.
if (hdPubKey.OutputAnonSetReasons.Count == 1)
{
hdPubKey.SetAnonymitySet(anonset, txid);
}
}
else
{
// It's address reuse.
hdPubKey.SetAnonymitySet(Intersect(new[] { anonset, hdPubKey.AnonymitySet }), txid);
}
}
}
/// <summary>
/// Adjusts the anonset of the inputs to the newly calculated output anonsets.
/// </summary>
private static void AdjustWalletInputs(SmartTransaction tx, HashSet<HdPubKey> distinctWalletInputPubKeys, int startingOutputAnonset)
{
// Sanity check.
if (!tx.WalletOutputs.Any())
{
return;
}
var smallestOutputAnonset = tx.WalletOutputs.Min(x => x.HdPubKey.AnonymitySet);
if (smallestOutputAnonset < startingOutputAnonset)
{
foreach (var key in distinctWalletInputPubKeys)
{
key.SetAnonymitySet(smallestOutputAnonset);
}
}
}
private void AnalyzeSelfSpendWalletOutputs(SmartTransaction tx, int startingOutputAnonset)
{
foreach (var key in tx.WalletOutputs.Select(x => x.HdPubKey))
{
if (key.AnonymitySet == HdPubKey.DefaultHighAnonymitySet)
{
key.SetAnonymitySet(startingOutputAnonset, tx.GetHash());
}
else
{
key.SetAnonymitySet(Intersect(new[] { startingOutputAnonset, key.AnonymitySet }), tx.GetHash());
}
}
}
private static void AnalyzeReceive(SmartTransaction tx)
{
// No matter how much anonymity a user would had gained in a tx, if the money comes from outside, then make anonset 1.
foreach (var key in tx.WalletOutputs.Select(x => x.HdPubKey))
{
key.SetAnonymitySet(1, tx.GetHash());
}
}
private static void AnalyzeNormalSpend(SmartTransaction tx)
{
// If all our inputs are ours and there's more than one output that isn't,
// then we can assume that the money was sent to learn our inputs.
// AND if there're outputs that go to someone else,
// then we can assume that the people learnt our change outputs,
// or at the very least assume that all the changes in the tx is ours.
// For example even if the assumed change output is a payment to someone, a blockchain analyzer
// probably would just assume it's ours and go on with its life.
foreach (var key in tx.WalletInputs.Select(x => x.HdPubKey))
{
key.SetAnonymitySet(1);
}
foreach (var key in tx.WalletOutputs.Select(x => x.HdPubKey))
{
key.SetAnonymitySet(1, tx.GetHash());
}
}
private void AnalyzeClusters(SmartTransaction tx)
{
foreach (var newCoin in tx.WalletOutputs)
{
// Forget clusters when no unique outputs created in coinjoins,
// otherwise in half mixed wallets all the labels quickly gravitate into a single cluster
// making pocket selection unusable.
if (newCoin.HdPubKey.AnonymitySet < 2)
{
// Set clusters.
foreach (var spentCoin in tx.WalletInputs)
{
newCoin.HdPubKey.Cluster.Merge(spentCoin.HdPubKey.Cluster);
}
}
}
}
}