TExcelTAD
Home › Streaming, Sketching & Sublinear Algorithms › Linearity Testing: BLR

Linearity Testing: BLR

A function f:F2k→F2f : \mathbb{F}_2^k \to \mathbb{F}_2 is linear if f(x+y)=f(x)+f(y)f(x + y) = f(x) + f(y) for all x,yx, y (addition is XOR). The linear functions are exactly the 2k2^k parities χS(x)=∑i∈Sxi\chi_S(x) = \sum_{i \in S} x_i. Blum, Luby and Rubinfeld (1993) proposed the most natural test imaginable: pick xx and yy at random and check the defining identity once. Proving that this works, that a function passing the check with high probability is close to a linear function, turned out to be subtle, and the cleanest proof, by Fourier analysis (Bellare, Coppersmith, Håstad, Kiwi and Sudan, 1996), is a classic. The BLR test is the seed of the PCP theorem and of much of property testing; with it come self-correction and the general idea that a few random consistency checks certify global structure.

The test

text
BLR linearity test
1  repeat t = ⌈2/ε⌉ times:
2      choose x, y uniformly in F_2^k
3      if f(x) + f(y) ≠ f(x + y): reject
4  accept

A linear function always passes. The question is what passing says about ff. Let dist⁡(f)=min⁡SPr⁡x[f(x)≠χS(x)]\operatorname{dist}(f) = \min_S \Pr_x[f(x) \ne \chi_S(x)] be the distance to the nearest linear function, and ρ(f)=Pr⁡x,y[f(x)+f(y)≠f(x+y)]\rho(f) = \Pr_{x,y}[f(x) + f(y) \ne f(x+y)] the rejection probability of a single check.

Theorem (Bellare, Coppersmith, Håstad, Kiwi and Sudan, 1996). ρ(f)≥dist⁡(f)\rho(f) \ge \operatorname{dist}(f) for every ff. Hence a function that is ε\varepsilon-far from linear fails one check with probability at least ε\varepsilon, and t=⌈2/ε⌉t = \lceil 2/\varepsilon \rceil checks reject it with probability at least 1−e−21 - e^{-2}: an ε\varepsilon-tester with O(1/ε)O(1/\varepsilon) queries, independent of kk.

Blum, Luby and Rubinfeld's original proof gave a weaker relation, dist⁡(f)≤2ρ(f)\operatorname{dist}(f) \le 2\rho(f) when ρ(f)\rho(f) is below a small constant (29\frac{2}{9}), by a combinatorial argument that defines the "majority vote" function g(x)=maj⁡y(f(x+y)+f(y))g(x) = \operatorname{maj}_y \bigl(f(x + y) + f(y)\bigr) and shows that gg is linear and close to ff. The Fourier proof is shorter and exact.

Proof by Fourier analysis

Work with F(x)=(−1)f(x)∈{±1}F(x) = (-1)^{f(x)} \in \{\pm 1\} and the characters ψS(x)=(−1)χS(x)\psi_S(x) = (-1)^{\chi_S(x)}, which form an orthonormal basis for the inner product ⟨F,G⟩=Ex[F(x)G(x)]\langle F, G \rangle = \mathbb{E}_x[F(x)G(x)]. Write F=∑SF^(S)ψSF = \sum_S \hat F(S) \psi_S with F^(S)=⟨F,ψS⟩\hat F(S) = \langle F, \psi_S \rangle; Parseval gives ∑SF^(S)2=1\sum_S \hat F(S)^2 = 1.

Distance. Pr⁡x[f(x)≠χS(x)]=1−F^(S)2\Pr_x[f(x) \ne \chi_S(x)] = \frac{1 - \hat F(S)}{2}, so dist⁡(f)=1−max⁡SF^(S)2\operatorname{dist}(f) = \frac{1 - \max_S \hat F(S)}{2}.

Acceptance. The check passes exactly when F(x)F(y)F(x+y)=1F(x)F(y)F(x+y) = 1, so

1−2ρ(f)=Ex,y[F(x)F(y)F(x+y)]=∑S,T,UF^(S)F^(T)F^(U) Ex,y[ψS(x)ψT(y)ψU(x+y)]=∑SF^(S)3,1 - 2\rho(f) = \mathbb{E}_{x,y}\bigl[F(x)F(y)F(x+y)\bigr] = \sum_{S,T,U} \hat F(S)\hat F(T)\hat F(U)\, \mathbb{E}_{x,y}\bigl[\psi_S(x)\psi_T(y)\psi_U(x+y)\bigr] = \sum_S \hat F(S)^3 ,

because ψU(x+y)=ψU(x)ψU(y)\psi_U(x + y) = \psi_U(x)\psi_U(y) and the expectation over independent x,yx, y is 1 when S=T=US = T = U and 0 otherwise.

Conclusion. ∑SF^(S)3≤max⁡SF^(S)⋅∑SF^(S)2=max⁡SF^(S)\sum_S \hat F(S)^3 \le \max_S \hat F(S) \cdot \sum_S \hat F(S)^2 = \max_S \hat F(S), so 1−2ρ(f)≤1−2dist⁡(f)1 - 2\rho(f) \le 1 - 2\operatorname{dist}(f). □\square

The program computes everything exactly for k=8k = 8: the Fourier coefficients by the fast Walsh-Hadamard transform, the distance from the largest coefficient, and the rejection probability of one check over all 2162^{16} pairs (x,y)(x, y), next to the formula ρ=12−12∑SF^(S)3\rho = \frac{1}{2} - \frac{1}{2}\sum_S \hat F(S)^3 from the proof.

python · try it yourself
import random

k = 8
N = 1 << k
def parity(v):
    return bin(v).count("1") & 1

def walsh(f):
    """Fourier coefficients of F(x) = (-1)^f(x): hat F(S) = average of F(x) (-1)^(S.x), by the fast transform."""
    a = [1 - 2 * v for v in f]
    h = 1
    while h < N:
        for i in range(0, N, 2 * h):
            for j in range(i, i + h):
                a[j], a[j + h] = a[j] + a[j + h], a[j] - a[j + h]
        h *= 2
    return [v / N for v in a]

def exact_rejection(f):
    return sum(f[x] ^ f[y] ^ f[x ^ y] for x in range(N) for y in range(N)) / N ** 2

rng = random.Random(1993)
S = 0b10110010
lin = [parity(S & x) for x in range(N)]
noisy = [v ^ (rng.random() < 0.05) for v in lin]
quad = [((x & 1) & (x >> 1 & 1)) ^ ((x >> 2 & 1) & (x >> 3 & 1)) ^ (x >> 5 & 1) for x in range(N)]
rand = [rng.randrange(2) for _ in range(N)]
print("function                    distance to linear   Pr[BLR rejects] (exact)   1/2 - 1/2 sum hat^3")
for name, f in [("linear chi_S", lin), ("linear, 5% of values flipped", noisy),
                ("quadratic x1x2 + x3x4 + x6", quad), ("random", rand)]:
    F = walsh(f)
    dist = (1 - max(F)) / 2                       # closest linear function: the largest coefficient
    rej = exact_rejection(f)
    print(f"{name:28} {dist:18.4f}   {rej:23.4f}   {0.5 - 0.5 * sum(c ** 3 for c in F):19.4f}")
function                    distance to linear   Pr[BLR rejects] (exact)   1/2 - 1/2 sum hat^3
linear chi_S                             0.0000                    0.0000                0.0000
linear, 5% of values flipped             0.0312                    0.0879                0.0879
quadratic x1x2 + x3x4 + x6               0.3750                    0.4688                0.4688
random                                   0.4102                    0.4950                0.4950

The identity ρ=12−12∑SF^(S)3\rho = \frac{1}{2} - \frac{1}{2}\sum_S \hat F(S)^3 holds exactly, and ρ≥dist⁡\rho \ge \operatorname{dist} in every row. Corrupting 3% of a linear function's values makes about 9% of the checks fail: each check reads three values, and any one of them being corrupted is enough. A random function fails half of the checks.

In C++

For k=20k = 20 (about a million points), the distance is computed with the same transform in O(k 2k)O(k\, 2^k) operations, and the tester runs with ε=0.05\varepsilon = 0.05: 40 checks, 120 queries, regardless of kk.

cpp · try it yourself
#include <algorithm>
#include <bit>
#include <cstdint>
#include <cstdio>
#include <random>
#include <vector>

using Table = std::vector<std::uint8_t>;                     // f : {0,1}^k -> {0,1} as a table

// Distance to the nearest linear function chi_S, from the fast Walsh-Hadamard transform.
double distance_to_linear(const Table& f) {
    std::size_t N = f.size();
    std::vector<std::int64_t> a(N);
    for (std::size_t x = 0; x < N; ++x) a[x] = f[x] ? -1 : 1;
    for (std::size_t h = 1; h < N; h *= 2)
        for (std::size_t i = 0; i < N; i += 2 * h)
            for (std::size_t j = i; j < i + h; ++j) { auto u = a[j], v = a[j + h]; a[j] = u + v; a[j + h] = u - v; }
    std::int64_t best = *std::max_element(a.begin(), a.end());
    return (1.0 - double(best) / N) / 2;
}

// BLR: t random triples (x, y, x+y); reject on f(x) + f(y) != f(x + y).
bool blr(const Table& f, int t, std::mt19937_64& rng) {
    for (int i = 0; i < t; ++i) {
        std::size_t x = rng() % f.size(), y = rng() % f.size();
        if (f[x] ^ f[y] ^ f[x ^ y]) return false;
    }
    return true;
}

int main() {
    const int k = 20;
    const std::size_t N = std::size_t(1) << k;
    std::mt19937_64 rng(1993);
    std::uint64_t S = rng() % N;
    Table lin(N), noisy(N), cubic(N);
    for (std::size_t x = 0; x < N; ++x) {
        lin[x] = std::popcount(S & x) & 1;
        noisy[x] = lin[x] ^ (rng() % 100 < 2);              // 2% of the values flipped
        cubic[x] = ((x & 1) & (x >> 1) & (x >> 2) & 1) ^ lin[x];   // a cubic term added
    }
    const double eps = 0.05;
    const int t = int(2 / eps);
    const struct { const char* name; const Table* f; } fs[] = {{"linear", &lin}, {"linear, 2% flipped", &noisy}, {"linear + x1 x2 x3", &cubic}};
    std::printf("k = %d (%zu points), %d BLR checks per run (3 queries each)\n", k, N, t);
    for (auto& [name, f] : fs) {
        int rejected = 0;
        for (int r = 0; r < 1000; ++r) rejected += !blr(*f, t, rng);
        std::printf("%-20s distance %.4f, rejected in %4d of 1000 runs\n", name, distance_to_linear(*f), rejected);
    }
}
k = 20 (1048576 points), 40 BLR checks per run (3 queries each)
linear               distance 0.0000, rejected in    0 of 1000 runs
linear, 2% flipped   distance 0.0202, rejected in  888 of 1000 runs
linear + x1 x2 x3    distance 0.1250, rejected in 1000 of 1000 runs

The function with 2% of its values flipped is only 0.020.02-far, closer than ε=0.05\varepsilon = 0.05, so the tester may accept it; it rejects most of the time anyway, since each check catches a corruption with probability about 3×0.023 \times 0.02.

Self-correction

If ff is δ\delta-close to a linear function gg with δ<14\delta < \frac{1}{4}, the values of gg can be recovered at any point xx, even at points where the table of ff is wrong:

g(x)=f(x+r)+f(r)with probability at least 1−2δ over a uniform r,g(x) = f(x + r) + f(r) \quad \text{with probability at least } 1 - 2\delta \text{ over a uniform } r ,

because x+rx + r and rr are each uniformly distributed, so each is a corrupted point with probability at most δ\delta, and if neither is, f(x+r)+f(r)=g(x+r)+g(r)=g(x)f(x + r) + f(r) = g(x + r) + g(r) = g(x). A majority vote over O(log⁡1η)O(\log \frac{1}{\eta}) independent choices of rr is wrong with probability at most η\eta. This is self-correction (Blum, Luby and Rubinfeld, 1993), the reason linear (and more generally low-degree polynomial) codes are locally decodable. The program corrupts 10% of a linear function's table for k=16k = 16 and decodes it.

python · try it yourself
import random

k = 16
N = 1 << k
def parity(v):
    return bin(v).count("1") & 1

rng = random.Random(1990)
S = rng.randrange(N)
f = [parity(S & x) for x in range(N)]                  # a linear function ...
corrupt = rng.sample(range(N), N // 10)                # ... with 10% of its table corrupted
for x in corrupt:
    f[x] ^= 1

def self_correct(x, reps):
    """Majority of f(x + r) + f(r) over random r: each vote is right with probability >= 1 - 2 * 0.1."""
    votes = sum(f[x ^ r] ^ f[r] for r in (rng.randrange(N) for _ in range(reps)))
    return int(2 * votes > reps)

bad = corrupt[:2000]                                   # points where the table itself is wrong
print("votes   wrong on random points   wrong on corrupted points (reading f directly: 1.000)")
for reps in [1, 3, 9, 27]:
    xs = [rng.randrange(N) for _ in range(2000)]
    on_random = sum(self_correct(x, reps) != parity(S & x) for x in xs) / len(xs)
    on_bad = sum(self_correct(x, reps) != parity(S & x) for x in bad) / len(bad)
    print(f"{reps:5}   {on_random:22.3f}   {on_bad:25.3f}")
votes   wrong on random points   wrong on corrupted points (reading f directly: 1.000)
    1                    0.172                       0.183
    3                    0.080                       0.087
    9                    0.011                       0.013
   27                    0.000                       0.001

A single vote is wrong with probability about 2×0.12 \times 0.1, as the bound says; 27 votes are almost always right, on random points and, crucially, on the corrupted points, where reading the table directly is always wrong.

Where BLR leads

  • PCPs. The Hadamard code (the table of all χS\chi_S) with BLR testing and self-correction gives the first, exponential-size probabilistically checkable proofs for NP; the PCP theorem replaces it by low-degree polynomials and composition.
  • Low-degree testing generalizes the test to polynomials of degree dd over larger fields (Rubinfeld and Sudan, 1996), with O(d)O(d)-type query bounds.
  • Testing by implicit learning: many properties of Boolean functions (juntas, small decision trees, sparse polynomials) can be tested with a number of queries independent of kk (Diakonikolas et al., 2007).

Test yourself

What does one BLR check query and test?

  1. f(0)=0f(0) = 0
  2. Three values f(x)f(x), f(y)f(y), f(x+y)f(x + y) for random x,yx, y, testing f(x)+f(y)=f(x+y)f(x) + f(y) = f(x + y)
  3. All values of ff
  4. f(x)=f(−x)f(x) = f(-x)

What is the exact relation between the rejection probability ρ\rho of one check and the Fourier coefficients F^(S)\hat F(S) of F=(−1)fF = (-1)^f?

  1. ρ=max⁡SF^(S)\rho = \max_S \hat F(S)
  2. 1−2ρ=∑SF^(S)31 - 2\rho = \sum_S \hat F(S)^3
  3. ρ=∑SF^(S)2\rho = \sum_S \hat F(S)^2
  4. ρ=1−F^(∅)\rho = 1 - \hat F(\emptyset)

Why does self-correction f(x+r)+f(r)f(x + r) + f(r) work even at a point xx where f(x)f(x) itself is corrupted?

  1. Because rr is chosen to avoid xx
  2. Because it never reads f(x)f(x): x+rx + r and rr are each uniformly random, so each is corrupted with probability at most δ\delta
  3. Because corrupted points are rare near xx
  4. It does not work there