Post · 3 min read· pp. 258–259

NP relations

tl;dr: An NP relation R(x;w)R(\mathbf{x}; \mathbf{w}) is a formalization of an algorithm RR that verifies a solution w\mathbf{w} to a problem x\mathbf{x} (in time \poly(∣x∣+∣w∣)\poly(|\mathbf{x}|+|\mathbf{w}|). For example, Sudoku(x;w)\mathsf{Sudoku}(\mathbf{x}; \mathbf{w}) verifies if w\mathbf{w} is a valid solution to the Sudoku puzzle x\mathbf{x}. NP relations are extremely helpful when formalizing zero-knowledge proofs as a way of “proving knowledge of a witness w\mathbf{w} such that R(x;w)=1R(\mathbf{x}; \mathbf{w}) = 1 for a public statement x\mathbf{x}”.

$ $

$ \def\stmt{\mathbf{x}} \def\witn{\mathbf{w}} % \def\td{\mathsf{td}} % \def\zkpSetup{\mathsf{ZKP}.\mathsf{Setup}} \def\zkpProve{\mathsf{ZKP}.\mathsf{Prove}} \def\zkpVerify{\mathsf{ZKP}.\mathsf{Verify}} \def\zkpSim{\mathsf{ZKP}.\mathsf{Sim}} $

What is an NP relation?

An NP relation R(\stmt;\witn)R(\stmt; \witn) is a deterministic algorithm that runs in polynomial time in the length of the statement \stmt\stmt and the witness \witn\witn and outputs either 0 or 1, based on custom checks it performs on \stmt\stmt or \witn\witn.

NP relations are a useful mathematical formalization of hard problems (\stmt\stmt) whose solutions (\witn\witn) are easy to verify (i.e., in polynomial time).

We often denote R(\stmt;\witn)=1R(\stmt;\witn) = 1 as (\stmt,\witn)∈R(\stmt, \witn)\in R.

Examples

Factoring

Factors(n;p,q)\mathsf{Factors}(n; p, q) is an NP relation that checks if nn factors into prime factors pp and qq: i.e., it outputs 1 if n,p,q∈N∗n,p,q\in \mathbb{N}^* and n=pqn=pq and p≠1p\ne 1 and q≠1q\ne 1, or outputs 0 otherwise.

Factors(n;p,q)={1if n,p,q∈N∗,p≠1,q≠1, and n=pq0otherwise\begin{align*} \mathsf{Factors}(n; p, q) = \begin{cases} 1 & \text{if } n, p, q \in \mathbb{N}^*, p \ne 1, q \ne 1, \text{ and } n = pq \\ 0 & \text{otherwise} \end{cases} \end{align*}

For a sufficiently large composite number nn, it is very hard to find its factors pp and qq. In fact, there is no known polynomial-time algorithm (in the bit-length of nn) for this problem. In this sense, factoring perfectly illustrates the “hard-to-solve-but-easy-to-verify” nature of NP relations.

Merkle tree membership

IsMember(r,vi;i,π)\mathsf{IsMember}(r, v_i; i, \pi) is an NP relation that outputs 1 if the Merkle proof π\pi correctly attests that viv_i is the value of the iith leaf in the Merkle tree with root hash rr. Otherwise, it outputs 0.

Note that a zero-knowledge proof for this relation would allow the prover to hide the position ii of the leaf and its Merkle proof π\pi, while convincing the verifier that the value viv_i is (somewhere) in the tree. This can be very useful. For example, anonymous payment schemes like Zcash are built on top of such a relation!

Merkle proof aggregation

AreMembers(r,{vj}j∈[n];(ij,πj)j∈[n])\mathsf{AreMembers}\left(r, \{v_j\}_{j \in [n]}; (i_j, \pi_j)_{j\in [n]}\right) is an NP relation that outputs 1 if, for all j∈[n]j\in[n], the Merkle proof πj\pi_j correctly attests that vjv_j is the value of the iji_jth leaf in the Merkle tree with root hash rr. Otherwise, it outputs 0.

A succinct (not necessarily zero-knowledge) proof for this relation would allow the prover to “compress” the nn proofs πj\pi_j for the nn values. For an example of such a succinct proof when the Merkle tree is algebraic, see our Hyperproofs paper.