Linearity Testing: BLR
A function is linear if for all (addition is XOR). The linear functions are exactly the parities . Blum, Luby and Rubinfeld (1993) proposed the most natural test imaginable: pick and 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
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 acceptA linear function always passes. The question is what passing says about . Let be the distance to the nearest linear function, and the rejection probability of a single check.
Theorem (Bellare, Coppersmith, Håstad, Kiwi and Sudan, 1996). for every . Hence a function that is -far from linear fails one check with probability at least , and checks reject it with probability at least : an -tester with queries, independent of .
Blum, Luby and Rubinfeld's original proof gave a weaker relation, when is below a small constant (), by a combinatorial argument that defines the "majority vote" function and shows that is linear and close to . The Fourier proof is shorter and exact.
Proof by Fourier analysis
Work with and the characters , which form an orthonormal basis for the inner product . Write with ; Parseval gives .
Distance. , so .
Acceptance. The check passes exactly when , so
because and the expectation over independent is 1 when and 0 otherwise.
Conclusion. , so .
The program computes everything exactly for : the Fourier coefficients by the fast Walsh-Hadamard transform, the distance from the largest coefficient, and the rejection probability of one check over all pairs , next to the formula from the proof.
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 holds exactly, and 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 (about a million points), the distance is computed with the same transform in operations, and the tester runs with : 40 checks, 120 queries, regardless of .
#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 -far, closer than , so the tester may accept it; it rejects most of the time anyway, since each check catches a corruption with probability about .
Self-correction
If is -close to a linear function with , the values of can be recovered at any point , even at points where the table of is wrong:
because and are each uniformly distributed, so each is a corrupted point with probability at most , and if neither is, . A majority vote over independent choices of is wrong with probability at most . 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 and decodes it.
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 , 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 ) 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 over larger fields (Rubinfeld and Sudan, 1996), with -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 (Diakonikolas et al., 2007).
Test yourself
What does one BLR check query and test?
- Three values , , for random , testing
- All values of
What is the exact relation between the rejection probability of one check and the Fourier coefficients of ?
Why does self-correction work even at a point where itself is corrupted?
- Because is chosen to avoid
- Because it never reads : and are each uniformly random, so each is corrupted with probability at most
- Because corrupted points are rare near
- It does not work there