Algorithm Search⌘K

Algorithmadvanced

Spatial cloaking

Also called location k-anonymity.

Replaces a user's location by a region that holds at least k users before it goes to a location-based service, by quadtree subdivision.

Time
O(n h) worst
Space
O(n) extra
Where
h = the depth of the finest quadrant; k = the anonymity parameter; n = the number of users known to the anonymizer
Properties
deterministicnot randomized
Origin
Marco Gruteser, Dirk Grunwald, 2003

Spatial cloaking hides who sent a request to a location-based service. A trusted anonymity server sits between the users and the service. It knows the current position of every user. When a user asks a question such as "where is the nearest fuel station", the server replaces the exact position by a region that also holds at least k−1k - 1 other users, removes the network address, and sends the region to the service. Gruteser and Grunwald (MobiSys 2003) called this k-anonymous location information and gave the adaptive-interval cloaking algorithm. It goes down a quadtree from the whole area towards the user's quadrant and stops just before the quadrant would hold fewer than kk users. Dense areas get small regions and sparse areas get large ones. For nn users and a finest level hh the descent takes O(nh)O(n h) time in the word-RAM model, or O(h)O(h) with a tree of counts. The value of kk is a policy choice. The guarantee holds against an attacker who sees only one region; Kalnis et al. (2007) showed that an attacker who knows the positions and the algorithm can sometimes still find the user.

In plain words

A driver asks a map service for nearby hotels. The service does not need the exact spot to answer. A helper server that the driver trusts takes the request and swaps the exact spot for an area.

The helper starts with the whole map. It cuts the current area into four equal squares and keeps the square that holds the driver. It does this again and again. Before each cut it counts the people in the current square. When the square holds fewer than kk people, the helper stops and sends the previous square, which still held at least kk.

The service then sees a square with at least kk people in it. From that square alone, it cannot tell which of them asked. In a city centre the square is small, so the answer stays useful. In the country the square must be large.

The problem

The input is a snapshot of user positions UU in the square A=[0,2h)2A = [0, 2^h)^2 with integer coordinates, the position p∈Up \in U of the requester, and an integer k≥1k \ge 1. The quadrants of a square [x,x+s)×[y,y+s)[x, x + s) \times [y, y + s) are its four half-open squares of side s/2s/2. A cell is AA or a quadrant of a cell; the cells of side 1 are at depth hh.

The output is the smallest cell that holds pp and at least kk users of UU, counting the requester, or NONE if ∣U∣<k|U| < k. Gruteser and Grunwald define a location as k-anonymous when it cannot be told apart from the location of at least k−1k - 1 other subjects (Section 5.1); a cell with kk users meets this for one request. They also allow the region to carry a time interval, which gives temporal cloaking below.

The algorithm

  1. CLOAK(p, U, k, h) ▷ U holds the requester at p; all points lie in [0, 2^h)^2
  2. 1q ← [0, 2^h)^2, q_prev ← q, d ← 0
  3. 2V ← U
  4. 3if |V| < k then return NONE
  5. 4loop
  6. 5if |V| < k then return q_prev
  7. 6if d = h then return q
  8. 7q_prev ← q
  9. 8q ← the quadrant of q that holds p
  10. 9V ← the users of V that lie in q
  11. 10d ← d + 1

Line L1 starts at the whole area that the server covers, and line L2 takes all known users. Line L5 is step 4 of Table 2 in the paper: when the current cell holds fewer than kk users, the previous cell, which held at least kk, is the answer. Lines L7 to L9 are steps 5 to 8: cut the cell into four, remember the old cell, keep the quadrant of pp and drop the users outside it. Table 2 does not have lines L3 and L6. Line L3 refuses a request that no region can cloak; without it, the first pass of the loop returns the whole area with fewer than kk users. Line L6 stops at cells of side 1, because kk users at the same point would make the loop run for ever.

Example

The area is [0,8)2[0, 8)^2, so h=3h = 3, and k=3k = 3. Eight synthetic users stand at u1=(1,1)u_1 = (1, 1), u2=(3,2)u_2 = (3, 2), u3=(2,3)u_3 = (2, 3), u4=(0,3)u_4 = (0, 3), u5=(5,1)u_5 = (5, 1), u6=(6,6)u_6 = (6, 6), u7=(7,5)u_7 = (7, 5) and u8=(2,6)u_8 = (2, 6). User u1u_1 sends a request.

pass dd qq VV ∣V∣\lvert V \rvert action
1 0 [0,8)2[0, 8)^2 u1,…,u8u_1, \ldots, u_8 8 8≥38 \ge 3: qprev←[0,8)2q_{prev} \leftarrow [0, 8)^2, q←[0,4)2q \leftarrow [0, 4)^2
2 1 [0,4)2[0, 4)^2 u1,u2,u3,u4u_1, u_2, u_3, u_4 4 4≥34 \ge 3: qprev←[0,4)2q_{prev} \leftarrow [0, 4)^2, q←[0,2)2q \leftarrow [0, 2)^2
3 2 [0,2)2[0, 2)^2 u1u_1 1 1<31 < 3: return [0,4)2[0, 4)^2

The service receives the square [0,4)2[0, 4)^2, which holds four users. For this snapshot, u2u_2, u3u_3 and u4u_4 also get [0,4)2[0, 4)^2, and u5u_5 to u8u_8 get the whole area [0,8)2[0, 8)^2, because each of their quadrants [4,8)×[0,4)[4, 8) \times [0, 4), [4,8)2[4, 8)^2 and [0,4)×[4,8)[0, 4) \times [4, 8) holds fewer than 3 users. Each region is sent by at least 3 users, so here an attacker who knows the positions still has at least 3 candidates. That is not always so; see Pitfalls.

Correctness

The cells that hold pp form a chain A=C0⊃C1⊃⋯⊃ChA = C_0 \supset C_1 \supset \cdots \supset C_h, with CdC_d of side 2h−d2^{h - d}, and the number of users in CdC_d does not grow with dd. Before each test of line L5, q=Cdq = C_d, VV is the set of users in CdC_d, and, if d≥1d \ge 1, qprev=Cd−1q_{prev} = C_{d-1} holds at least kk users. Line L8 keeps the quadrant that holds pp, so qq stays in the chain, and line L9 keeps exactly the users of the new cell. So line L5 returns Cd−1C_{d-1} for the first dd with fewer than kk users, and line L6 returns ChC_h if all the cells have at least kk. In both cases the result is the deepest cell of the chain with at least kk users, which is the smallest cell that holds pp and kk users. Line L3 makes sure that line L5 never returns qprevq_{prev} at d=0d = 0. The full proof is in proofs/correctness.md.

The privacy claim is weaker than it looks. For one request seen alone, the region holds at least kk users who could have sent it. The claim says nothing about an attacker who knows the positions of all users and the algorithm, about a series of requests from the same user, or about the anonymity server itself, which sees everything.

Proof: CLOAK returns the smallest quadtree cell that holds the requester and at least k usersfull

Let A=[0,2h)2A = [0, 2^h)^2, let UU be a finite multiset of integer points in AA, let p∈Up \in U and let k≥1k \ge 1. For 0≤d≤h0 \le d \le h let CdC_d be the cell of side 2h−d2^{h-d} that holds pp, that is [x,x+s)×[y,y+s)[x, x + s) \times [y, y + s) with s=2h−ds = 2^{h-d}, x=s⌊px/s⌋x = s \lfloor p_x / s \rfloor and y=s⌊py/s⌋y = s \lfloor p_y / s \rfloor. Let NdN_d be the number of users of UU in CdC_d.

Claim 1 (the chain). C0=AC_0 = A, each Cd+1C_{d+1} is the quadrant of CdC_d that holds pp, and N0≥N1≥⋯≥Nh≥1N_0 \ge N_1 \ge \cdots \ge N_h \ge 1.

Proof. The four quadrants of CdC_d are the cells of side 2h−d−12^{h-d-1} inside it, and they partition it, so exactly one holds pp, and it is Cd+1C_{d+1}. Since Cd+1⊆CdC_{d+1} \subseteq C_d, Nd+1≤NdN_{d+1} \le N_d. Every CdC_d holds pp, which is in UU, so Nh≥1N_h \ge 1. □\square

The cells that hold pp are exactly C0,…,ChC_0, \ldots, C_h, and they are nested. So the smallest cell that holds pp and at least kk users is Cd∗C_{d^*} for the largest d∗d^* with Nd∗≥kN_{d^*} \ge k, if N0≥kN_0 \ge k.

Claim 2 (invariant). Each time line L5 runs, q=Cdq = C_d, VV is the multiset of users of UU in CdC_d, and if d≥1d \ge 1 then qprev=Cd−1q_{prev} = C_{d-1} and Nd−1≥kN_{d-1} \ge k.

Proof. At the first test, lines L1 and L2 give d=0d = 0, q=A=C0q = A = C_0 and V=UV = U. Suppose the claim holds at a test that neither line L5 nor line L6 ends. Then ∣V∣=Nd≥k|V| = N_d \ge k and d<hd < h. Line L7 sets qprev=Cdq_{prev} = C_d, line L8 sets qq to the quadrant of CdC_d that holds pp, which is Cd+1C_{d+1} by Claim 1, line L9 keeps the users of VV in Cd+1C_{d+1}, which are all the users of UU in Cd+1C_{d+1} since Cd+1⊆CdC_{d+1} \subseteq C_d, and line L10 adds 1 to dd. So the claim holds at the next test, with Nd≥kN_d \ge k for the old dd. □\square

Claim 3 (the result). CLOAK returns NONE if ∣U∣<k|U| < k, and otherwise Cd∗C_{d^*}.

Proof. If ∣U∣<k|U| < k, line L3 returns NONE. Otherwise N0≥kN_0 \ge k, so line L5 does not return at d=0d = 0. The loop stops by d=hd = h at the latest, since dd grows by one per pass. By Claim 2 and Claim 1, the tests at line L5 see N0,N1,…N_0, N_1, \ldots in turn. If line L5 returns at some d≥1d \ge 1, then Nd<k≤Nd−1N_d < k \le N_{d-1}, and by Claim 1 every later NN is also below kk, so d∗=d−1d^* = d - 1 and the result qprev=Cd−1q_{prev} = C_{d-1} is correct. If instead line L6 returns at d=hd = h, then Nh≥kN_h \ge k, so d∗=hd^* = h and the result is q=Chq = C_h. ■\blacksquare

Reciprocity is not implied. The result depends on pp through line L8. The four users (0,0)(0, 0), (1,0)(1, 0), (0,1)(0, 1) and (3,3)(3, 3) with h=2h = 2 and k=3k = 3 give Cd∗=[0,2)2C_{d^*} = [0, 2)^2 for the first three and AA for the last, so the region AA is returned for only one user. The test file checks this case and compares the reciprocity check with an oracle.

Cost. By Claim 3 the loop makes at most h+1h + 1 passes, and each pass scans VV, which has at most nn users, once. The time is O(nh)O(n h), and VV needs O(n)O(n) extra space.

Complexity

  • Each pass of lines L5 to L10 scans VV once, and there are at most h+1h + 1 passes, so a request takes O(nh)O(n h) time in the worst case, when the users are packed near pp. If the number of users halves at each level, the total is O(n)O(n).
  • With a region quadtree of the positions that stores the number of users in each node, lines L8 and L9 become one step down the tree and one read of a count, so a request takes O(h)O(h) time. Keeping the counts up to date costs O(h)O(h) per position update.
  • The extra space is O(n)O(n) for VV, or O(nh)O(n h) for the tree in the worst case.

Properties

  • Adaptive. The region size follows the density of users, so the same kk gives small regions in dense areas and large ones in sparse areas.
  • Coarse steps. The region area changes by a factor of 4 at each level, so the region often holds many more than kk users. In the paper's simulation with kmin=5k_{min} = 5, the mean number of users in a region was about 10 (a measurement, Section 7).
  • Deterministic. The region depends only on pp, the snapshot and kk.
  • Not reciprocal. Different users in the same region may get different regions; this is the weak point of the method.

Variants and improvements

  • Temporal cloaking. Gruteser and Grunwald also give a version that fixes a spatial resolution, waits until kmink_{min} different users have visited the chosen area, and returns the area with a time interval that ends at the current time and starts a random amount before the request (Section 5.2). It trades delay for spatial accuracy.
  • Reciprocal cloaking. Kalnis et al. require that every user inside a region would get the same region. Then an attacker who knows the positions and the algorithm still has at least kk candidates. Their Hilbert Cloak orders the users along a Hilbert curve, cuts the order into groups of kk users, and returns the bounding rectangle of the requester's group, so every member of a group gets the same region.
  • Personal kk and minimum area. Later systems let each user choose kk and a minimum region size, since a region with kk users inside one building can still reveal the building.
  • Without a trusted server. Geo-indistinguishability adds planar Laplace noise on the user's device and gives a local differential privacy style guarantee that does not depend on where the other users are. Private information retrieval hides the query from the service by cryptography.

When to use it

Use spatial cloaking when a trusted party already knows all positions, such as a network operator, and the service only needs a coarse location, for example points of interest or local traffic. The paper's simulation gives an idea of the cost: with kmin=5k_{min} = 5, the median region side was 30 and 65 metres in two areas with many expressways and 125 and 250 metres in two areas with mostly collector streets (a measurement, Section 7). Do not use it when the attacker can learn the positions of the other users, or when one user sends many requests along a route. Use a reciprocal variant in the first case and a local, noise-based method in the second.

Pitfalls

  • Returning qq instead of qprevq_{prev} at line L5. The current cell has just been found to hold fewer than kk users. Returning it reveals a region with as few as one user, the requester.
  • No depth limit at line L6. If kk users stand at the same point, every quadrant that holds pp holds all of them, and the loop never stops. Real coordinates have a finite resolution; stop there.
  • No check at line L3. Table 2 of the paper returns the previous quadrant when the count is too small, and on the first pass the previous quadrant is the whole area. If fewer than kk users are known in total, the result is a region with fewer than kk users. Refuse the request instead.
  • Choosing the quadrant by pp at line L8 lets the region identify pp. Take A=[0,4)2A = [0, 4)^2 with h=2h = 2, k=3k = 3, three users at (0,0)(0, 0), (1,0)(1, 0) and (0,1)(0, 1), and one user uu at (3,3)(3, 3). The three users each get [0,2)2[0, 2)^2. The user uu is alone in [2,4)2[2, 4)^2, so uu gets the whole area. The whole area holds four users, but only uu would ever send it. An attacker who knows the positions and the algorithm learns that uu sent the request. Kalnis et al. call the missing property reciprocity, and the code has a check for it.
  • Tracking across requests. Each request is cloaked against a fresh snapshot at line L2. A user who sends requests while driving gives a series of regions; the intersection with a road map and with the next region can shrink the candidate set below kk. The paper names location tracking as a threat (Section 4.1) but the algorithm does not stop it.
  • A region that is all one place. A region with kk users inside one hospital tells the service that the requester is in the hospital. k-anonymity of identity is not privacy of location.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

spatial_cloaking.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/spatial-cloaking/. The authors are listed in prov/JKVDFZ.jsonl.

from collections.abc import Sequence

Point = tuple[int, int]
Cell = tuple[int, int, int]  # (x, y, side): the half-open square [x, x + side) x [y, y + side)


def cloak(p: Point, users: Sequence[Point], k: int, h: int) -> Cell | None:
    """Return the smallest quadtree cell that holds p and at least k users, or None.

    users lists every known position, the requester included, with integer
    coordinates in [0, 2^h). Precondition: k >= 1, h >= 0 and p is in users.
    Cost: O(n h) time and O(n) extra space for n users.
    """
    q: Cell = (0, 0, 1 << h)  # @line L1
    q_prev = q
    d = 0
    v = list(users)  # @line L2
    if len(v) < k:  # @line L3
        return None
    while True:  # @line L4
        if len(v) < k:  # @line L5
            return q_prev
        if d == h:  # @line L6
            return q
        q_prev = q  # @line L7
        x, y, side = q
        half = side // 2
        q = (x + half if p[0] >= x + half else x, y + half if p[1] >= y + half else y, half)  # @line L8
        v = [u for u in v if q[0] <= u[0] < q[0] + half and q[1] <= u[1] < q[1] + half]  # @line L9
        d += 1  # @line L10


def reciprocity_holds(users: Sequence[Point], k: int, h: int) -> bool:
    """Return True if every region that CLOAK returns for some user is returned for at least k users.

    Precondition: as for cloak, and at least k users. Cost: O(n^2 h).
    """
    senders: dict[Cell | None, int] = {}
    for u in users:
        r = cloak(u, users, k, h)
        senders[r] = senders.get(r, 0) + 1
    return all(c >= k for c in senders.values())
test_spatial_cloaking.py 74 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/spatial-cloaking/. The authors are listed in prov/JKVDFZ.jsonl.

import random

from spatial_cloaking import cloak, reciprocity_holds

USERS = [(1, 1), (3, 2), (2, 3), (0, 3), (5, 1), (6, 6), (7, 5), (2, 6)]


def test_example() -> None:
    assert cloak((1, 1), USERS, 3, 3) == (0, 0, 4)
    for u in USERS[:4]:
        assert cloak(u, USERS, 3, 3) == (0, 0, 4)
    for u in USERS[4:]:
        assert cloak(u, USERS, 3, 3) == (0, 0, 8)
    assert reciprocity_holds(USERS, 3, 3)


def test_not_reciprocal() -> None:
    users = [(0, 0), (1, 0), (0, 1), (3, 3)]
    assert cloak((0, 0), users, 3, 2) == (0, 0, 2)
    assert cloak((3, 3), users, 3, 2) == (0, 0, 4)
    assert not reciprocity_holds(users, 3, 2)


def test_edges() -> None:
    assert cloak((0, 0), [(0, 0)], 2, 3) is None
    assert cloak((0, 0), [(0, 0)], 1, 3) == (0, 0, 1)
    assert cloak((5, 5), [(5, 5)] * 4, 3, 3) == (5, 5, 1)
    assert cloak((0, 0), [(0, 0), (0, 0)], 2, 0) == (0, 0, 1)
    # with k = 1 the requester alone is enough, so the cell of side 1 comes back
    assert cloak((6, 2), USERS + [(6, 2)], 1, 3) == (6, 2, 1)


def oracle(p: tuple[int, int], users: list[tuple[int, int]], k: int, h: int) -> tuple[int, int, int] | None:
    best = None
    for d in range(h + 1):
        side = 1 << (h - d)
        x, y = p[0] // side * side, p[1] // side * side
        inside = sum(1 for u in users if u[0] // side * side == x and u[1] // side * side == y)
        if inside >= k:
            best = (x, y, side)
    return best


def test_against_oracle() -> None:
    rng = random.Random(11)
    for _ in range(600):
        h = rng.randint(0, 5)
        size = 1 << h
        n = rng.randint(1, 30)
        users = [(rng.randrange(size), rng.randrange(size)) for _ in range(n)]
        k = rng.randint(1, 8)
        p = rng.choice(users)
        got = cloak(p, users, k, h)
        assert got == oracle(p, users, k, h)
        if got is not None:
            x, y, side = got
            assert x <= p[0] < x + side and y <= p[1] < y + side
            assert sum(1 for u in users if x <= u[0] < x + side and y <= u[1] < y + side) >= k


def test_reciprocity_against_oracle() -> None:
    rng = random.Random(12)
    for _ in range(500):
        h = rng.randint(1, 4)
        size = 1 << h
        k = rng.randint(1, 4)
        n = rng.randint(k, 12)
        users = [(rng.randrange(size), rng.randrange(size)) for _ in range(n)]
        regions = [oracle(u, users, k, h) for u in users]
        want = all(regions.count(r) >= k for r in regions)
        assert reciprocity_holds(users, k, h) == want

History

Gruteser and Grunwald presented the anonymity server, k-anonymous location information and the adaptive-interval and temporal cloaking algorithms at MobiSys 2003, borrowing the idea of k-anonymity from published tables. Their evaluation used simulated traffic on road maps of Denver. In 2007 Kalnis, Ghinita, Mouratidis and Papadias showed that the quadtree cloak can reveal the requester when the attacker knows the positions, and defined reciprocity. Later work moved towards methods that need no trusted server, such as geo-indistinguishability.

References

  1. Marco Gruteser and Dirk Grunwald, Anonymous usage of location-based services through spatial and temporal cloaking, MobiSys 2003, pages 31-42, Section 4.1 (restricted space identification, observation identification and location tracking), Section 5 (the trusted anonymity server), Section 5.1 (k-anonymous location information), Section 5.2 (Table 2, the adaptive-interval cloaking algorithm, and temporal cloaking), Sections 6 and 7 (k_min = 5 and the measured resolutions)
  2. Panos Kalnis, Gabriel Ghinita, Kyriakos Mouratidis and Dimitris Papadias, Preventing location-based identity inference in anonymous spatial queries, IEEE Transactions on Knowledge and Data Engineering 19(12), 2007, pages 1719-1733, the reciprocity property as a sufficient condition for spatial k-anonymity, the attack on cloaking regions that do not have it, and the Nearest Neighbor Cloak and Hilbert Cloak algorithms

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • at least three sources
  • a scene preset for the example