Repository navigation
Overlap Detection Anonymization
This page documents a method of anonymization whereby the rough amount of overlap between different buckets is measured in an efficient manner. This contrasts with our current form of overlap detection, whereby we can only detect complete matches (using a hash of the users in a bucket). We'll give the new approach a working name of "Rhoda", which comes from "Rough Overlap Detection Anonymization".
Rhoda deals with a class of difference attack that only requires that the attacker can isolate a single user (the victim). To isolate means to uniquely identify the user in the query based on one or more user attributes. The attacker doesn't need to be able to isolate any other users. The attacks are described in the following section.
- Criteria for identifying Attack Clusters
- Validating Non-overlap Users as Victims
- Adjusting for Victims
- Which Buckets to Compare
- Computing Approximate Overlap using Bucket Digests
- Identifying Attack Clusters
- Identifying Suspicious Behavior
Attack 1: Single-Barbell Create two buckets. Both buckets have the same set of "base" users B1 (doesn't matter what users or how many, as long as they are the same), but one bucket contains the victim if the victim has a given attribute, and the other bucket contains the victim is the victim does not have the attribute. Generate 40 or 50 of these, each with a different set of base users. The take the sum of all the first buckets, and the sum of all the second buckets. The higher sum contains the victim (where V=1 if the victim has the attribute, and V=0 if not):
| Set of first buckets | Set of second buckets |
|---|---|
| [B1+V] | [B1+(!V)] |
Note that creating the set of base users is as simple as hashing some attribute (location), and counting the users where the last X bits of the hash value are Y. Each different Y generates a different set of orthogonal (non-overlapping) base users.
Attack 2: Hammer-Cluster Create a bucket with base users B1, and another bucket with base users B2, where B1 and B2 are have no users in common. Then create a third bucket that contains both B1 and B2, as well as the victim if the victim has the desired attribute (and without the victim otherwise). Generate 40 or 50 of these three-bucket tuples. Take the sum of the individual B1 and B2 buckets. Take the sum of the combined B1+B2 buckets. Divide each of the sums by the number of tuples. The latter sum will be roughly 1 greater than the former sum if the victim has the attribute:
| Set of first buckets | Set of second buckets |
|---|---|
| [B1],[B2] | [B1+B2+V] |
Here we take V to mean V OR !V, depending on whether or not V has the attribute. Alternatively, we could put V in the first set if it has the attribute, and in the second set if not:
| Set of first buckets | Set of second buckets |
|---|---|
| [B1+V],[B2+V] | [B1+B2+(!V)] |
(Moving forward, we'll assume that the attack can take either approach to inserting the victim, so we won't mention it again.)
Lets refer to B1 as a "block" of users. The bucket on the right consists of two blocks (B1 and B2) potentially plus the victim.
Note that for this attack, the blocks B1 and B2 do not have to be the same size. Indeed, the attacker cannot control the exact size of the buckets.
This attack can be generalized to any number of blocks:
| Set of first buckets | Set of second buckets |
|---|---|
| [B1],[B2],...[Bn] | [B1+B2+...+Bn+V] |
As the number of buckets increases, the overall amount of noise also increases, requiring more tuples to reach a given amount of confidence. Also, the more buckets, the larger the bucket on the right must be. If the bucket on the right is bigger than 1000 or so, the base amount of noise will increase (because we increase the noise as long as we stay within 1% error). For these reasons, there is a limit on how many buckets can be used for this attack.
(The reason for the names "barbell" and "hammer" will become clear later on.)
Attack 3: General-Cluster Another variant of the above is to have multiple buckets on both the left and the right. For instance:
| Set of first buckets | Set of second buckets |
|---|---|
| [B1+B2],[B3+B4] | [B1+B3+V],[B2+B4+V] |
There are a huge number of cluster variations. The above example has four blocks total, with two buckets on the left and two buckets on the right. But there could be more buckets on either side, and each side can have a different number of buckets. For instance, the following has three buckets on the left, each with two blocks, and two buckets on the right, each with three blocks:
| Set of first buckets | Set of second buckets |
|---|---|
| [B1+B2],[B2+B3],[B3,B4] | [B1+B2+B3+V],[B2+B3+B4+V] |
The attack works as long as there are identical sets of blocks on the left and right. (In the above example, B1 and B4 appear once on each side, and B2 and B3 each appear twice on each side.) This is necessary for that the blocks on both sides cancel each other out.
Strictly speaking, it is also better for the attacker if no two buckets in the attack are identical. This is because identical buckets produce identical fixed noise, so it then takes more buckets to eliminate the noise through averaging.
There are a number of important variants of the general cluster. These variants are important because they allow the attacker to have substantial control over how much overlap appears between the buckets in a cluster. This control allows the attacker to try to manipulate clusters so as to avoid detection. In particular, our method for efficiently measuring overlap is only accurate to within 10% or worse.
Before discussing the variants, we need to first define overlap. In general, two buckets that are to be measured for overlap may be of different size. We define overlap as:
Overlap: The fraction of users in the smaller bucket that appear in the larger bucket.
Some examples:
- If two buckets have identical users lists, then they have 100% overlap.
- If two buckets have no common users, then they have 0% overlap.
- If one bucket has 100 users, and the other bucket has 200 users, and all 100 users of the smaller bucket appear in the bigger bucket, then they are considered to have 100% overlap.
- If one bucket has 100 users, and the other bucket has 200 users, and there are 25 users in common between both buckets, then they are considered to have 25% overlap.
In our system, we have both estimated overlap and true overlap. Estimated overlap uses a highly efficient mechanism that roughly measures overlap based on a digest of the bucket (see Summary of Tunable Parameters). True overlap is an exact measure based on a comparison of the user lists. Normally when we refer to simply "overlap" in this document, we mean estimated overlap.
An attacker can roughly control the number of users in any given block. He can do this by, for instance, manipulating the number of bits in a mask that must match a give value. For example, a block where the last 6 bits are zero produces twice as many users as a block where the last 7 bits are zero. Or, an attacker can create a block by combining N smaller blocks and treating them as a unit. Of course, an attacker can control exactly which blocks go into each bucket.
In what follows, I introduce a different nomenclature. Each block is represented by a letter. An upper-case letter refers to a large block, and a lower-case letter refers to a small block. (Again, the large block may be composed of multiple small blocks, but they are treated as a single block.) In practice, the large block is around 10X - 15X bigger than the smaller block. The reason for this has to do with the sensitivity of our overlap detection algorithm. This will become clearer later on.
In the new nomenclature, the following cluster:
| Set of first buckets | Set of second buckets |
|---|---|
| [B1+B2],[B3+B4] | [B1+B3+V],[B2+B4+V] |
is written as:
| left | right |
|---|---|
| AB | AC |
| CD | BD |
where B1=A, B2=B, B3=C, and B4=D. The victim V is implied.
It is important to note that in the above cluster, there is relatively high overlap (say 50% or so) between all buckets on the left and all buckets on the right. In other words, each pair 'AB-AC', 'AB-BD', 'CD-AC', and 'CD-BD' has roughly 50% overlap. This has important implications for the ability of cloaks to detect this attack cluster. Keeping in mind that the attacker needs 40 or 50 of these clusters to guess the victim attribute with high confidence, suppose that the attacker generated the following attack clusters:
| left | right | - | left | right | - | left | right | - | left | right | ... |
|---|---|---|---|---|---|---|---|---|---|---|---|
| AB | AC | - | EF | EG | - | IJ | IK | - | MN | MO | ... |
| CD | BD | - | GH | FH | - | KL | JL | - | OP | NP | ... |
These clusters are easy to identify, because there is substantial overlap within each cluster (4 overlapping pairs), and no overlap between clusters. The following attacks make it harder to detect clusters.
Attack 4: Barbell-Cluster This attack looks like the following:
| left | right |
|---|---|
| aB | Bc |
| cD | Da |
Keeping in mind that the block 'a' is 10X to 15X smaller than the block 'B', the bucket pair 'aB-Bc' appears to have very high overlap (> 90%). Our overlap detection cannot reliably distinguish between a pair of buckets with say 95% overlap and 100% overlap. We refer to this pair as a "barbell" (two high-overlap objects of roughly similar size). Likewise, the pair 'cD-Da' is a barbell. However, the pairs 'cD-Bc' and 'aB-Da' have very little overlap. Our overlap detection can't reliable distinguish these from buckets with no overlap.
The attacker can create a barbell cluster with any number of barbells:
| left | right |
|---|---|
| aB | Bc |
| cD | De |
| eF | Fg |
| ... | ... |
| yZ | Za |
The disadvantage of more barbells is that there is more noise introduced. The advantage, however, is that the cost of identifying these clusters gets combinatorially expensive. (In fact, as will be seen later, we don't try. Rather, we simply determine that there is an unusually large number of barbells, and report this.)
Attack 5: Nunchuk-Cluster This attack looks like the following:
| left | right |
|---|---|
| a | aB |
| Bc | cD |
| De | e |
Here, the pair 'a-aB' has 100% overlap, and 'aB-Bc' has high overlap. I refer to the first pair as a "hammer" (high-overlap pair with a "heavy" large-bucket head, and a "light" small-bucket handle). Perhaps this is getting a bit silly, but I refer to these two pairs together as a nunchuk. The two nunchuks are connected by a hard-to-detect low overlap pair 'Bc-cD'.
Attack 6: Barbell-Nunchuk-Cluster This attack is as follows:
| left | right |
|---|---|
| A | Ab |
| bC | Cd |
| d | - |
Attack 7: Perfect-Cluster A small version of a perfect cluster looks like this:
| left | right |
|---|---|
| abcd | aeim |
| efgh | bfjn |
| ijkl | cgko |
| mnop | dhlp |
Here, every bucket on the left shares exactly one block with every bucket on the right. A perfect cluster has 2N buckets and N^2 blocks. The buckets in the above example have 25% overlap, but a cluster with N=16 (and 256 blocks) would have an difficult-to-detect 6% overlap between all left and right pairs. Never-the-less, this attack looks suspicious because the attacker needs probably 100 or so such clusters because of the amount of noise produced, which means 1600 or so roughly equal-sized buckets.
There are a bunch of variations on these attacks. First, the different types of clusters can be mixed up while attacking a given victim. Of course, the buckets from different clusters can be interspersed, and multiple victims can be attacked at once, with the buckets from these different attacks being interspersed. The buckets with victims can be generated before the buckets without victims, or vice versa. The attacker may vary the size of buckets overall (some clusters have larger buckets, some smaller). In addition, non-attack buckets can be interspersed with the attack buckets.
The attacker can also put "plants" in the buckets. A plant is a user that the attacker can isolate, and therefore cause to be in a bucket with 100% certainty. The use of plants can, for instance, reduce the amount of overlap in pairs of buckets in an attack cluster. In the extreme, if the attacker could generate buckets with all plants, then the attack pairs need not have any overlap. Rhoda can deal with a small number of plants, but not the extreme case. The expectation is that using lots of plants will result in suspicious looking queries (tasks with an unusually large number of lines of code, or an unusually large number of unique tasks). I have not tested this yet.
At a very high level, the cloak's defense algorithm works like this:
- Measure overlap between bucket pairs
- Try to identify attack clusters, identify victims within those clusters, and adjust answer counts to neutralize the effect of the victims
- Generate an overlap "signature", detect suspicious behavior, and take appropriate action (report the suspicious queries, or in the extreme block the analyst).
In the simulations I've built, I do the first two items pretty well. Doing the third item will require some more simulation measurements, but critically will also require some real experience with real customers. That's fine...we have time to do this.
We consider a cluster to be an attack cluster when the absolute (and true) number of non-overlapping users between the users on the left and the users on the right is less than some threshold. In the code the threshold is called SIZE_CLOSE_THRESH and is set to 30, or half the size of the smaller bucket, whichever is smaller. The non-overlapping users can come from both sides of the attack cluster.
Note that by "attack cluster", we include all of the attacks described above. In other words, an attack cluster can be as few as two buckets, one on each side, or any other combination.
The value of sct essentially reflects the number of plants that the attacker needs to avoid having his attack clusters identified. Without any plants, the number of non-overlap nodes in an attack cluster will be at most one---the victim. With plants, the number can be higher. The larger the value for sct, the more plants are required to get around the threshold. The downside of large sct, however, is that there is a higher chance that a user who is not a victim is incorrectly identified as a victim and adjusted for.
To avoid this, we require that a user be a non-overlap user in attack clusters multiple times before adjusting for the user. The basic idea is to keep track of the number of times that a user is non-overlap in an attack cluster, and only adjust for the user when the number of times exceeds some threshold. However, we want to allow for a user that has exceeded this threshold to be placed back below the threshold if the user is found not to be non-overlap in any attack clusters for a long time.
The algorithm I implemented goes like this (see hightouch.c and hightouch.h).
- Every time a user is non-overlap in an attack cluster, the
touchescounter is incremented. (We call this "touching" a user.) - If the
touchescounter exceeds a noisy threshold HT_ATTACK_SUPPRESS_THRESH (set at 5), then the user is considered to be a victim, and is adjusted for. The SD for the noisy threshold is set at HT_NOISE_SD = 1. - If the user is included in some number of consecutive buckets without being a non-overlap user in an attack cluster, then the
touchescounter is decremented.- This number of consecutive buckets, called
drecrementThreshold, varies with the number of times the user is touched. Specifically, each time a user is touched, thedrecrementThresholddoubles. It is initially set at HT_INIT_DECREMENT_TOUCHES_THRESH = 1000. It has a max value of HT_DECREMENT_TOUCHES_MAX = 1M. (Note that currently we never reducedrecrementThreshold. We may want to consider this at some point, because otherwise once a user is touched a number of times, it becomes very hard to reduce thetouchcounter.)
- This number of consecutive buckets, called
I also considered having a higher touches threshold, above which the user is always suppressed, whether it is in an attack bucket or not. We need real experience to decide if something like this is going to hurt us. The key question is how often users get touched in a normal (non-attack) setting.
To adjust for the victim(s) in an attack cluster, the number of victims on the left side must equal the number of victims on the right side. This effectively forces the cluster into emulating the case where the victim does not have the attribute being measured.
An attack cluster can only be determined to be an attack cluster when the last bucket of the cluster is examined. The victim may or may not appear in this last bucket, and may or may not appear in one or more earlier buckets from the attack cluster. If an adjustment is needed, then it must be made to the count of the last bucket. The algorithm is as follows:
- Count the number of victims on the left side and right side of the attack cluster. A victim is counted once for each time it appears (i.e. if it is in three buckets on the left side, the count is 3).
- Recall that a user is considered a victim only if the number of touches exceeds the HT_ATTACK_SUPPRESS_THRESH threshold.
- Adjust the count of the last bucket to remove any difference in the two victim counts.
Some examples:
- The number of victims on the right is 3, and on the left is 0. The last bucket is on the left. The last bucket's count is adjusted by adding 3.
- The number of victims on the right is 3, and on the left is 0. The last bucket is on the right. The last bucket's count is adjusted by subtracting 3.
- The number of victims on the right is 2, and on the left is 6. The last bucket is on the right. The last bucket's count is adjusted by adding 4.
In the simulation code, we allow for the case where a bucket may be part of multiple attack clusters. I don't know if this is something we really need to deal with or not. I haven't thought through possible attacks where this is exploited. Anyway, none of the attacks I coded up so far test this situation.
For efficiency, we want to minimize the number of buckets that can be considered as part of an attack cluster. Here we describe cases where buckets cannot be part of an attack cluster, and can therefore be ignored.
Buckets generated by streaming queries: A streaming query is one whereby data is fed into the query as it arrives, and output buckets are produced only when enough change occurs in the user list of a given named bucket.
Automatic sliding window queries: A sliding window query is similar to a streaming query, except that it takes place over some past window of time, rather than in real time as data arrives. The first bucket of a sliding window query must be considered for attack clusters, but not subsequent buckets.
Queries with no tasks: Buckets from "pure-SQL" queries do not need to be considered.
Queries with pre-approved tasks: Assuming that we build up over time a set of attested tasks or aggregation functions, buckets produced from these tasks to not need to be considered.
Buckets produced from queries covering different time periods: If two queries do not have overlapping time periods defined for them, then the buckets produced by the different queries do not need to be compared.
Queries with non-overlapping attribute (column) ranges: More generally, buckets from any two queries that are pre-filtered with non-overlapping attribute ranges do not have to be compared. For example, if one query pre-filters users based on one geographical area, and another query pre-filters on another non-overlapping geographical area, then the buckets from these queries do not have to be compared.
Rhoda has an efficient way of determining approximate overlap between pairs of buckets. This is done by storing a digest of each bucket. Digests are variants on bloom filters (see Bucket Digest Details). A digest is roughly 1KB in size, and consists of:
- A bitmap which is a digest (bloom filter variant) of the list of user IDs
- The true count
- A pointer to the query that generated the bucket
These bucket digests are stored for as long as the data that produced the bucket is stored.
The basic idea of the bucket digest is that a logical OR of two digest bitmaps will produce a bitmap that will have statistically fewer bits set if there is greater overlap, and more bits set if there is less overlap. The combination of the three bitmaps gives a pretty good measure of overlap.
The digest is relatively compact, and the comparison operation is very fast: almost 2M compares per second on my laptop, though ignoring overheads of looking up digests etc.
The overlap measure of digest comparisons is rough: error is typically plus or minus 10% or so, and can be more. Never-the-less, the mechanism is effective for identifying attack clusters.
The basic steps in identifying an attack cluster are as follows:
- Measure the overlap between pairs of buckets using digests
- Build bucket groups based on amount of overlap
- Form potential attack clusters by placing buckets within a bucket group on the right and left, and looking for similar sized sides.
- When potential attack clusters are found, re-query to produce the full buckets, determine the exact non-overlap users, and check the Criteria for identifying Attack Clusters.
The first step is relatively efficient, assuming that we eliminate the need to compare most bucket pairs based on the constraints in Which Buckets to Compare. The second step is also efficient.
The third step can be inefficient if bucket groups are very large, because of the combinatoric number of possible attack clusters. Therefore we place limits on the size of the attack cluster, and as well hope that usually large clusters are not produced.
Step four is inefficient, because queries must be recomputed, and full bucket comparisons must be made. We hope that step four is relatively rare.
The basic idea behind step two is to try to form connected graphs where the nodes in the graph are buckets, and the links are overlap values that fall above a certain threshold. This works as long as 1) the attack cluster forms such a connected graph, and 2) buckets not in attack clusters with each other do not form a connected graph (a "false graph"). These conditions don't always hold, but we force the attacker to generate a distinct overlap signature to avoid the conditions.
The single-barbell and hammer-cluster attack clusters are the easiest: they both form connected graphs out of 100% overlap "links". In experiments, we found it effective to filter at 85% overlap: the large majority of single-barbell and hammer-cluster attack clusters are found this way. In my code, this is called the NEAR_MATCH_THRESHOLD.
As a rule, the more buckets a general-cluster has, the less overlap between the buckets. In our experiments, we found that most general-cluster attack clusters could be found using an overlap threshold of 15%, while still avoiding finding too many false clusters. This threshold is called the WEAK_MATCH_THRESHOLD in my code.
Hammers, barbells, and nunchuks individually form connected graphs from NEAR_MATCH_THRESHOLD overlap links. However, the attack clusters built from them (the barbell-cluster, nunchuk-cluster, and nunchuk-hammer-cluster) cannot necessarily be detected even with the WEAK_MATCH_THRESHOLD. We therefore have specific mechanisms to search for these attack clusters.
Overall, the algorithm for identifying attack clusters goes as follows (reference code is in defenses.c):
- At reception of a new bucket, measure the overlap between the new bucket and all previous eligible buckets.
- If the overlap is >= NEAR_MATCH_THRESHOLD (85%):
- If bucket sizes (number of users) are within SIZE_CLOSE_THRESH (30) (thus indicating a possible single-barbell attack cluster): 1. Re-query, and check the [attack cluster criteria]#criteria-for-identifying-attack-clusters).
- Else if the difference in bucket sizes is greater than SIZE_CLOSE_THRESH (thus indicating part of a possible hammer-cluster): 1. Add the new bucket to a hammer-cluster data structure associated with the "head" bucket (the larger bucket). 1. If two or more "handle" (smaller) buckets in the data structure, check all combinations of handle buckets to see if the sum of the sizes of the handle buckets is with SIZE_CLOSE_THRESH (30) of the head bucket. 1. For each such combination, re-query, and check the [attack cluster criteria]#criteria-for-identifying-attack-clusters). 1. If an attack cluster is identified, then remove the corresponding handle buckets from the data stuct.
- If the overlap is >= WEAK_MATCH_THRESHOLD (15%):
- Combine the new bucket and previous bucket into a data structure that holds a general cluster: 1. If neither the previous bucket nor the new bucket is part of an existing cluster, start a new cluster with the two buckets. 1. If both the new bucket and the previous bucket are part of a cluster, join the two clusters into a single cluster.
- If the resulting cluster has between 3 and MAX_CLUSTER_SIZE (10) buckets: 1. Check all combinations of buckets in the cluster for a possible attack cluster with at least 2 buckets on a side, and no more than MAX_ATTACK_CLUSTER (8) buckets in total (both sides). A potential attack cluster is found when the sum of sizes on one side is within CLUSTER_CLOSE_SIZE (5) of the sum of sizes on the other side. 1. For each such potential attack cluster, re-query all involved buckets, and check the [attack cluster criteria]#criteria-for-identifying-attack-clusters). 1. If an attack cluster was found, remove the involved buckets from the cluster data structure. (Note that if these buckets are involved in still more attack clusters, they will probably be added back in later.)
The above mechanisms are designed to detect and prevent attacks. However, an attacker may be able to bypass the mechanisms by building larger clusters than we can efficiently detect. Therefore, we would like to be able to detect this kind of behavior.
This detection work is not yet done. The intent is to look for the following kinds of behavior:
- Lots of barbells, but with few of them being actual attack barbells (could be a barbell-cluster).
- Lots of nunchuks (could be a barbell-nunchuk cluster).
- An unusual number of similar-sized clusters, with low overlap (could be a large perfect cluster, or nearly perfect cluster).
- An unusual number of small buckets (size between 20 and 50) in combination with buckets of between size 500 and 1000. This could be a hammer-cluster attack, but where the ratio between small and large bucket is enough to bypass the digest comparison function.
I'll report on this after the experiments are done.
SIZE_CLOSE_THRESH: 30, or half of smaller bucket, whichever is smaller
- Two cluster sides that have less overlap than this are considered to be an attack cluster. The non-overlapping users are high-touch processed.
HT_ATTACK_SUPPRESS_THRESH: 5 (noisy threshold)
- Threshold for the number of times a user is touched (found to be a non-overlapping user in an attack cluster). Above this threshold, the user is considered to be a victim, and bucket counts are adjusted to account for the victim.
HT_NOISE_SD: 1
- Standard deviation for the HT_ATTACK_SUPPRESS_THRESH noisy threshold
HT_INIT_DECREMENT_TOUCHES_THRESH: 1000
- Initial setting of
drecrementThreshold, which is used to decrement thetouchcounter.drecrementThresholddoubles with each touch.
HT_DECREMENT_TOUCHES_MAX: 1000000
- Maximum setting of
drecrementThreshold.
NEAR_MATCH_THRESHOLD: 85%
- Overlap threshold for identifying barbells and hammers (used for single-barbell, hammer-cluster, barbell-cluster, nunchuk-cluster, and nunchuk-hammer-cluster).
WEAK_MATCH_THRESHOLD: 15%
- Overlap threshold for identifying general clusters.
MAX_CLUSTER_SIZE: 10
- Maximum number of buckets in a cluster data structure above which we don't search for an attack cluster (because the number of combinations is too high). Instead, we keep some statistics, look for suspicious behavior, and report.
MAX_ATTACK_CLUSTER: 8
- Maximum number of buckets in a potential attack cluster that we'll check for close bucket sizes. (Note, with MAX_ATTACK_CLUSTER of 8, we'll look for clusters of 2x6, 3x5, and 4x4 only.)
CLUSTER_CLOSE_SIZE: 5
- The size difference between the sum of bucket sizes on the left and right of a general cluster within which we consider the cluster to be a potential attack cluster. Note that this is less than SIZE_CLOSE_THRESH. The reason is to minimize false positives, whereby the size difference indicates a possible attack cluster, but in reality (after the expensive operation of re-querying), there is no attack cluster.
The bucket digest that lets us efficiently (in time and space) determine the amount of overlap (shared users) between two buckets. The data record takes a little less than 1kB per bucket. The interface is:
digest = makeDigest(bucket)
overlap = computeOverlap(digest1, digest2)
makeDigest() doesn't require any sorting, so runs O(N), where N is the number of users in the bucket. computeOverlap() costs around a dozen bitwise compare operations.
computeOverlap() can compare buckets of different size. In this case, the reported overlap is the percentage of users in the smaller bucket that are also in the larger bucket. computeOverlap() only works for sure if the larger bucket is no more than 16X larger than the smaller bucket, and never works if the larger bucket is more than 64X larger than the smaller.
computeOverlap() is quite accurate for bucket that have nearly full overlap, or nearly zero overlap. computerOverlap() is less accurate for buckets with partial overlap. A pair of buckets with, say, 80% overlap might be computed as having between 50% and 100% overlap. The average of 15 or so such compares, however, converges pretty well at around 80%. Note that, in fact, I have implemented two versions of computeOverlap() (and two variants of one of the versions), though both versions use the same digest (i.e. there is only one version of makeDigest()).
The basic mechanism is a variant on the bloom filter. The principle is simple: we set certain bits in the filter according to the set of users in the corresponding bucket. The more bits that match between two filters, the higher the overlap in the corresponding buckets.
I want to keep the filters relatively small. Of course, if every user (in a large bucket) is placed into a small filter, then the large majority of bits will be set to 1, and the filter won't work well. Therefore, what we do is sample from the users in a deterministic way, and build our filters from the sampled users. We can sample by taking only the users whose last X bits of the User ID are zero (here assuming that the User IDs are random uniformly distributed....otherwise we hash them first). The larger the bucket, the bigger X is used, resulting in a smaller proportion of sampled users.
In fact, we generate up to three filters per bucket, each filter 1024 bits wide, and each using a different sampling rate. We use the following numbers:
| Bucket Size | Sampling Rates |
|---|---|
| < 128 | 1 |
| >= 128, < 512 | 1, 1/4 |
| >= 512, < 2048 | 1, 1/4, 1/16 |
| >= 2048, < 8192 | 1/4, 1/16, 1/64 |
| >= 8192, < 32768 | 1/16, 1/64, 1/256 |
| >= 32768, < 131072 | 1/64, 1/256, 1/1024 |
| . . . | . . . |
Two filters can only be compared if they have the same sampling rate (and therefore are sampling from the same subset of users). What this means in practice is that if one bucket is more than 16X larger than another bucket, it may not be possible to compare them. As an example, a bucket with 200 users can be compared with a bucket with 3000 users, because they share a sampling rate of 1/4. The 200-user bucket, however, cannot be compared with a 9000-user bucket.
Reference code for selecting sampling rates is as follows (C language....sorry):
/*
* level=0 means no filter
* level=1 means all bucket entries are put into filter
* level=2 means 1/4 the bucket entries are put into filter
* level=3 means 1/16 the bucket entries are put into the filter
* level=N means that 1/(2^^2(N-1)) bucket entries are put into the filter
*/
#define EMPTY_FILTER 0
typedef struct one_filter_t {
uint8_t level;
uint64_t filter[FILTER_LEN]; // 1024 bits wide.
} one_filter;
typedef struct bucket_t {
one_filter filters[FILTERS_PER_BUCKET];
int bsize; // number of entries
uint64_t *list; // pointer to first entry
} bucket;
#define F_ONE_MAX 0x80
#define F_TWO_MAX (F_ONE_MAX<<2)
#define F_THREE_MAX (F_TWO_MAX<<2)
#define MAX_LEVEL 24 // absurdly large bucket (must be 24)
setLevelsBasedOnBucketSize(bucket *bp)
{
int min, max, level;
if (bp->bsize < F_ONE_MAX) {
// special case, only one level possible
bp->filters[0].level = 1;
bp->filters[1].level = 0;
bp->filters[2].level = 0;
}
else if (bp->bsize < F_TWO_MAX) {
// special case, only one level possible
bp->filters[0].level = 1;
bp->filters[1].level = 2;
bp->filters[2].level = 0;
}
else {
min = F_TWO_MAX;
max = F_THREE_MAX;
for (level = 1; level < MAX_LEVEL; level++) {
if ((bp->bsize >= min) && (bp->bsize < max)) {
bp->filters[0].level = level;
bp->filters[1].level = level+1;
bp->filters[2].level = level+2;
break;
}
min <<= 2;
max <<= 2;
}
}
// MAX_LEVEL is so high, we'll never get here without
// setting the levels
}
To make a single filter (one of the three), we mask the user ID with 10 bits (0x3ff), and set the bit in the mask corresponding to the resulting value. (We must hash the user ID in advance if it is not uniformly distributed.)
The reference code for building a single filter is as follows:
makeOneFilter(bucket *bp, int i)
{
uint64_t mask_presets[MAX_LEVEL] = {
0,
0,
0xc000000000000000,
0xf000000000000000,
0xfc00000000000000,
0xff00000000000000,
0xffc0000000000000,
0xfff0000000000000,
0xfffc000000000000,
0xffff000000000000,
0xffffc00000000000,
0xfffff00000000000,
0xfffffc0000000000,
0xffffff0000000000,
0xffffffc000000000,
0xfffffff000000000,
0xfffffffc00000000,
0xffffffff00000000,
0xffffffffc0000000,
0xfffffffff0000000,
0xfffffffffc000000,
0xffffffffff000000,
0xffffffffffc00000,
0xffffffffff000000};
uint64_t mask;
int j, k;
uint64_t *lp;
mask = mask_presets[bp->filters[i].level];
k = 0;
lp = bp->list;
for (j = 0; j < bp->bsize; j++) {
if ((*lp & mask) == 0) {
setFilterBit(&(bp->filters[i]), (*lp & 0x3ff));
}
lp++;
}
}
Given that two filters may have multiple matching sampling rates, we have the question of which matching rate to use. In particular, we want to avoid filters with too few bits set, and filters with too many bits set. After some experimentation, I settled on favoring a sampling rate where the bucket size of the larger bucket is between 300 and 800 (after sampling), and the smaller bucket is at least 100 (after sampling). Note that, although I settled on this, in fact the system is pretty robust to these numbers, as long as the basic principle is followed. Here is the reference code:
typedef struct compare_t {
int common; // bits common between both filters
int first; // total bits the first filter has
int second; // total bits the second filter has
int numFirst; // number of entries in first bucket
int numSecond; // number of entries in second bucket
int level; // the level used for the comparison
// 0 means no comparison made
int index1;
int index2;
} compare;
/*
* Returns 1 if a pair of filters with matching level were
* found. Else returns 0.
*/
compareFullFilters(bucket *bp1, bucket *bp2, compare *c)
{
int i, j;
int save_i=0, save_j=0, firstTime=1;
int high, low, temp;
c->level = 0; // indicates that no comparison was made
c->numFirst = bp1->bsize;
c->numSecond = bp2->bsize;
for (i = 0; i < FILTERS_PER_BUCKET; i++) {
for (j = 0; j < FILTERS_PER_BUCKET; j++) {
if (bp1->filters[i].level == bp2->filters[j].level) {
if (bp1->filters[i].level == 0) {goto done;}
// Ideally, the largest user count is between 300 and 800,
// and the smallest is least 100.
// This avoids excessive set bits due to simply large numbers
// (should consider avoiding this by having a 4th filter????)
high = bp1->bsize >> ((bp1->filters[i].level * 2) - 2);
low = bp2->bsize >> ((bp2->filters[j].level * 2) - 2);
if (low > high) {
temp = low;
low = high;
high = temp;
}
if (firstTime == 1) {
firstTime = 0;
save_i = i;
save_j = j;
if ((high >= 300) && (high <= 800) && (low >= 100)) {goto done;}
}
else if ((high >= 300) && (high <= 800) && (low >= 100)) {
save_i = i;
save_j = j;
goto done;
}
}
}
}
done:
if (firstTime == 0) {
c->level = bp1->filters[save_i].level;
c->index1 = save_i;
c->index2 = save_j;
compareFilterPair(&(bp1->filters[save_i]), &(bp2->filters[save_j]), c);
}
}
Finally, the reference code for comparing two filters, once they are selected, is:
/*
* Finds 1) the number of bit positions in common between the
* two filters, and the unique bits of the first and second
* filters. Happily (erroneously) compares two filters of different
* levels. Up to calling routing to ensure this doesn't
* happen. Never fails.
*/
compareFilterPair(one_filter *of1, one_filter *of2, compare *c)
{
unsigned int i;
uint64_t anded;
c->common = 0; c->first = 0; c->second = 0;
for (i = 0; i < FILTER_LEN; i++) {
c->first += __builtin_popcountll(of1->filter[i]);
c->second += __builtin_popcountll(of2->filter[i]);
c->common += __builtin_popcountll(of1->filter[i] & of2->filter[i]);
}
}
Given these three items, the number of bits in each buckets' filter, and the number of common '1' bits between them, the question is, "can this information be used as an indication of overlap?"
To measure this, I ran an experiment where I created 10M pairs of buckets, where the smaller bucket ranged in size from 1 user to 10K users, and the larger bucket was between 1X and 16X times larger than the smaller bucket. For each pair, we varied the overlap between the two buckets between 0% and 100%. All random numbers are uniform distributed.
For each pair, we measured a number of things:
- The number of bits set in the filter of each bucket.
- The number of "common bits", which means the number of bits set in both filters for each bit position (the bits that are set when the two filters are AND'd).
- The number of bits that are 1 in one filter, and 0 in the other filter.
- The number of bits that are zero in both filters for each bit position.
Following is a scatterplot of the total number of set bits in the filter for the smaller bucket (the smaller filter) on the X axis, and the total number of common bits---where the bit position is set in both filters on the y axis. The color represents the true amount of overlap (ground truth). (The x-axis label of "Number One / Star Bits" means the number of bits that are one for the small bucket, and * for the large bucket.)

This picture shows that the digest can be a good basis for measuring overlap, because it shows that there is a good correlation between the two measures (number of smaller-filter bits and number of common bits). This correlation exists across different bucket sizes, and different amounts of overlap. This means that we can use this method to get reasonable estimates on overlap given pretty much any two buckets (as long as within 16X size of each other).
Further, we see that for complete overlap and zero overlap, we get clear signals. (The X=Y diagonal is pure blue, and the bottom of the graph is pure green.) In the middle, however, it is a bit noisy, with a range of overlap values interspersed.
The following is a similar scatter plot for the same experiment, except now each dot corresponds to the average overlap for a set of up to 16 bucket pairs. Here we see that much, though not all, of the noise of the pure scatter plot is removed:

The following is a scatter plot where each dot gives the standard deviation for the set of up-to-sixteen samples.

Here again, we see that the complete-overlap and no-overlap edges of the plot are clean, but in the middle there can be substantial variation between measurements (upwards of 25%!).
The following two graphs show the max and min of the up-to-16 samples:


This shows pretty clearly that there is a substantial middle range where a pair with some overlap may be reported as full or no overlap, in the extreme. This is unfortunate in that it means we might often re-query and compare buckets when we really shouldn't have to. It remains to be seen whether we can minimize this (by looking for averages over a number of buckets).
Note that I tried hard to find out if any particular factors are causing this variation, for instance bucket size, bucket size ratio, sampling rate, number of set bits, but I couldn't find anything. To now, this is the best I can do.
Note also that I couldn't find any other basis on which to make the measurement (i.e. the number of bit positions that are 1 in one filter and 0 in the other). I think there is substantial work still to be done on finding a good filter, but my hope is that this is good enough for practical purposes.
We have developed two methods for implementing computeOverlap. We need to do more measurements and tuning to decide which is best. Both methods use a lookup into two-dimensional tables. In the first method, the indices into the tables are: number of ones in the smaller filter and number of common ones. In what follows, the term "filter size" refers to the number of ones in the filter, and the term "common size" refers to the number of common ones.
In the variation of the first method, we also take into account the sizes of the two buckets being compared. In the second method, the indices are for all practical purposes all three values: the small filter size, the large filter size, and the common size.. The value returned from the tables is the estimated overlap. These two methods are described in the following two sections.
I suspect that there is still room for lots of improvement in computeOverlap, but currently the methods I've developed are working pretty well in actual attacks and defenses, so I'm leaving it as is for now.
In this method, we basically take the scatterplot from above (labeled "Average Overlap Estimate (16 Samples)"), and use it as a lookup table.
One way to do this would be to simply build a 1024 X 1024 array, each cell in the array representing a possible value of small filter size and common size, and then index that array after computing the two values. A slightly more space-efficient approach is to recognize that most of the locations in that array are not reachable, and build an array that is small filter size on one axis, and the difference between small filter size and common size on the other.
The following graph shows that array, assuming the range 0-800 for the small filter size, and the range 0-256 for the difference between small filter size and common size, where each bit position is the average of up-to-16 samples.

The array isn't completely filled in because many of the array positions are rarely if ever reached. We can artificially fill them in. To do this, everything to the left is given the value of '0' (for no overlap). Everything else can be filled in with the nearest value to its left. The following graph shows how the empty cells would be filled in using this procedure:

The final array (combining initially filled cells and artificially filled cells) looks like this:

This 800 X 256 2-dimensional array can be used as the lookup table for estimating overlap.
In some testing of this array as a lookup table, I found that often the X,0 position in the array is not 63 (perfect match) as I had expected. Indeed, sometimes the table tells me that a pair of perfect matches are, say, 91% or 92% matching. The reason for this is that I populated the table with many bucket pairs that differ by 16X in size. So, for instance, we can have cases where the small filter size is 25, and large filter size is 400. If these two buckets have 90% overlap, then 22 or 23 of the matching bits will be due to the overlap, but the remaining 2 or 3 bits might match by chance (since there is a 1/3 chance that a given bit will be set in the larger filter). I think this may be pulling the average down. The following scatterplot plots small filter size (numFirst), against large filter size (numSecond). The color shows the size ratio between the two buckets. You can see that pairs with a large size ratio contribute to cases where we have a small number of filter bits against a large number of filter bits.

Working with the above design, I found that for buckets of similar size and relatively low overlap, the amount of overlap was being under-estimated, thus making it hard to detect attacks. This particular case is interesting because attacks that have relatively low overlap on all buckets also tend to have a similar size.
As a result, I calibrated a second overlap table. Unlike the first table, which covers a wide range of bucket sizes and variations in bucket sizes (ratio up to 1/16), this second table only deals with buckets where the smaller bucket is 2000 users or less, and the larger bucket is no more than 50% larger than the smaller bucket. Within these limitations, this second table is much more accurate than the first table. Specifically, the overlap estimates look like this:

and the standard deviation among the samples is much smaller than the previous case:

As a result of having two tables, the routing that compares two digests first checks to see if the conditions stated above hold true (smaller bucket < 2000 entries, larger bucket < 1.5x the smaller bucket), and chooses the new table to lookup the overlap estimate. The resulting modified reference code is here:
/*
* Doesn't matter which bucket is bigger.
*/
compareFullFilters(bucket *bp1, bucket *bp2, compare *c)
{
int i, j;
int save_i=0, save_j=0, firstTime=1;
int high, low;
bucket *bl, *bs; // large and small
// force bp1 to be the smaller bucket
if (bp1->bsize > bp2->bsize) {
bl = bp1;
bs = bp2;
}
else {
bl = bp2;
bs = bp1;
}
c->level = 0; // indicates that no comparison was made
c->numFirst = bs->bsize;
c->numSecond = bl->bsize;
for (i = 0; i < FILTERS_PER_BUCKET; i++) {
for (j = 0; j < FILTERS_PER_BUCKET; j++) {
if (bs->filters[i].level == bl->filters[j].level) {
if (bs->filters[i].level == 0) {goto done;}
// Ideally, the largest user count is between 300 and 800,
// and the smallest is least 100.
// This avoids excessive set bits due to simply large numbers
// (should consider avoiding this by having a 4th filter????)
low = bs->bsize >> ((bs->filters[i].level * 2) - 2);
high = bl->bsize >> ((bl->filters[j].level * 2) - 2);
if (firstTime == 1) {
firstTime = 0;
save_i = i;
save_j = j;
if ((high >= 300) && (high <= 800) && (low >= 100)) {goto done;}
}
else if ((high >= 300) && (high <= 800) && (low >= 100)) {
save_i = i;
save_j = j;
goto done;
}
}
}
}
done:
if (firstTime == 0) {
c->level = bs->filters[save_i].level;
c->index1 = save_i;
c->index2 = save_j;
compareFilterPair(&(bs->filters[save_i]), &(bl->filters[save_j]), c);
if ((i = c->first) >= 800) {
i = 799;
}
if ((j = (c->first - c->common)) >= 256) {
j = 255;
}
// to avoid floating point math in this operation, we use
// (bs->bsize + (bs->bsize>>1)) instead of (bs->bsize * 1.5)
if ((bs->bsize <= 2000) && ((bs->bsize + (bs->bsize >> 1)) >= bl->bsize)) {
c->overlap = partial_overlap_array[i][j];
}
else {
c->overlap = full_overlap_array[i][j];
}
}
}
The second method can be thought of as a generalization of the variation of the first method. By this I mean the following. In the first method variation, we start to take into account the size of the two buckets: as long as the two buckets are relatively small (less than 2000) and similar in size, then I use a separate lookup table. Since the number of bits set in the two filters corresponds roughly to the size of the bucket (at least after sampling), we could take into account the sizes of the two buckets by using both small and large filter size in the indices. A brute-force way to do this would be to have a 1024x1024x1024 array, indexed by small filter size, large filter size, and common size, and returning estimated overlap, but this would be a pretty large table (1GByte).
To save space, what I do instead if the following. I have four 1024x1024 tables, each indexed by small filter size and large filter size. The first table returns the expected common size if there is no overlap between the two buckets. The second table returns the expected common size if there is 25% overlap between the two buckets. The third and fourth for 50% and 75% respectively. A fifth table for 100% overlap is not needed, because the common size for 100% overlap is exactly the small filter size. In the lookup itself, we find two common sizes from the tables such that the common size is between the two table common sizes. We then interpolate between the two table common sizes to compute the estimated overlap.
By way of example: say we compare two filters, and measure small filter size = 420, large filter size = 680, and common size = 200. Now suppose that from the tables we find that the expected common size at 25% overlap is 190, and the expected common size at 50% overlap is 240. Interpolating 200 between 190 (25%) and 240 (50%), we obtain 30% as the estimated overlap.
The reason this interpolation can work can be seen from the following four graphs. These graphs answer the question: "what is the expected common size given a certain overlap, a certain small filter size, and a certain large filter size?" The overlaps (x axis) are set at 0%, 25%, 50%, 75%, and 100%. The small filter size differs for each graph, and are set as 10, 50, 300, and 600 (labeled X=10, X=50, X=300, and X=600). The large filter size varies as shown in each graph (labeled Y).
The data points were produced as follows (see computeOverlap2.c). We determine the minimum common size as the given small filter size time the fraction of overlap (i.e. if given small filter size is 300, and the overlap is 50%, then there will be a minimum of 150 common bits). We set this many bits in random (but identical) positions in both filters. Then for the remaining number of bits in each filter, we set that many additional random bits. For each given small filter size, large filter size, and overlap, we do this 100 times. The point on the line is the average of the 100 runs. The error boxes correspond to one standard deviation, and the whiskers to max and min values.




Just eyeballing these pictures, we can see that the interpolation between the data points will not deviate far from the overall curve of the lines, and that the interpolation will in any event stay within one standard deviation.
To produce the arrays used for the computeOverlap() lookups, we populate each array with the average of 100 runs, for every value of small filter size and large filter size (for 0%, 25%, 50%, and 75% overlaps). The resulting arrays are shown as scatterplots as follows:



