Search

Search papers and lessons

One-Way Function

Level 3 · 9 min read

A one-way function is easy to compute in the forward direction and infeasible to invert, and it is the basic building block of passwords, signatures and key exchange.

On this page

  • Colour = topic mix
  • Discrete math
  • Algorithms
  • Security & cryptography
  • Sign in to see your progress

TL;DR

A one-way function is a function ff where computing f(x)f(x) takes a moment for any input xx, but given an output yy, finding any xx with f(x)=yf(x) = y takes so long that it is infeasible in practice. The asymmetry is the point: the forward direction is cheap, and the reverse direction is hard.

A trapdoor one-way function adds a secret piece of information that makes inversion easy again for whoever holds it.

Where you'll see it: in password storage, where the system keeps only f(password)f(\text{password}); in digital signatures and key exchange, where the public key is a one-way image of a secret; and in the hash functions that protect data integrity.

This lesson explains a mathematical idea in cryptography. It is not advice about how to choose or store passwords or keys.

Intuition

Mixing paint

Mixing two colours is easy. Given a bucket of paint, you can add a drop of one colour and stir. Getting the original colours back out of the mix is effectively impossible: the mixed colour does not record how much of each went in or in what order. The forward step is cheap, and the reverse step destroys information.

A one-way function is a computer version of that. It must be easy to run and must throw away enough information that the input cannot be recovered by working backwards.

Why the idea exists

Two problems push cryptography toward one-way functions. First, we need to store or share something that proves we know a secret without revealing the secret. If the system keeps f(password)f(\text{password}) rather than the password, a leaked database does not hand over the password, because the attacker has to invert ff to get it. Second, in public-key cryptography, a public value must be easy to derive from a private value but hard to go back from. The public key is shared openly, so going from it to the private key must be infeasible.

The difficulty is on the attacker's side. The defender does the easy direction, and the attacker must do the hard direction. Security is the gap between the two costs.

The math

Let f:X→Yf : X \to Y be a function. We call ff one-way if:

  1. There is a polynomial-time algorithm that computes f(x)f(x) for every x∈Xx \in X.
  2. For every probabilistic polynomial-time algorithm AA, the probability that AA outputs some x′x' with f(x′)=f(x)f(x') = f(x), when given y=f(x)y = f(x) for a uniformly random xx, is negligible in the input size nn.

"Polynomial time" means the cost grows as a power of the input length, and "negligible" means the success probability shrinks faster than any polynomial. The definition is asymptotic (see Big-O Notation): it says nothing about a particular input, but about how hard the problem is as the size grows.

A trapdoor one-way function is a family of one-way functions indexed by a key (fpk,t)(f_{\text{pk}}, t) where tt is a secret. Inversion is hard without tt and easy with tt.

Modular exponentiation as a candidate

The standard candidate uses Modular Arithmetic. Fix a prime pp and a generator gg. Define

f(x)=gx mod p.f(x) = g^{x} \bmod p.

Computing f(x)f(x) is fast even when xx has hundreds of digits, because repeated squaring needs about log⁡2x\log_2 x multiplications. Inverting it means solving gx≡y(modp)g^{x} \equiv y \pmod p for xx, which is the Discrete Logarithm Problem. No classical algorithm is known that does this in polynomial time for well-chosen parameters.

The forward direction takes about log⁡2x\log_2 x steps. The best known inversion methods take sub-exponential time in log⁡2p\log_2 p, which is the gap that cryptographic parameters exploit.

Why the trapdoor works (RSA, briefly)

Let n=pqn = pq where pp and qq are large primes. The function f(m)=me mod nf(m) = m^{e} \bmod n is believed to be one-way when pp and qq are secret, because inverting it requires factoring nn or finding a root in an unknown group. Knowing the factorisation gives a secret exponent dd with ed≡1(mod(p−1)(q−1))ed \equiv 1 \pmod{(p-1)(q-1)}, and then (me)d≡m(modn)(m^{e})^{d} \equiv m \pmod n inverts the function directly. The factorisation is the trapdoor.

Worked examples

Example
A tiny exponentiation example

Take p=17p = 17, g=3g = 3, and the function f(x)=3x mod 17f(x) = 3^{x} \bmod 17.

  1. Forward: compute f(5)f(5). Successive powers of 3 mod 17 are 3,9,27 mod 17=10,30 mod 17=13,39 mod 17=53, 9, 27 \bmod 17 = 10, 30 \bmod 17 = 13, 39 \bmod 17 = 5. So f(5)=5f(5) = 5.
  2. Inverse: given y=5y = 5, find xx with 3x≡5(mod17)3^{x} \equiv 5 \pmod{17}. Brute force tries x=1,2,3,4,5x = 1, 2, 3, 4, 5 and stops at x=5x = 5.
  3. For a harder-looking target, take y=7y = 7. Continuing the powers gives 36=153^{6} = 15, 37=113^{7} = 11, 38=163^{8} = 16, 39=143^{9} = 14, 310=83^{10} = 8, 311=24 mod 17=73^{11} = 24 \bmod 17 = 7, so x=11x = 11. Notice that the powers jump around with no visible pattern, so the answer cannot be read off from yy; you must search.
  4. For 17 the search costs at most 16 steps, so inversion is trivial. The example shows the mechanics, not the hardness.
  5. Now replace 17 with a prime of 600 decimal digits. Forward evaluation still takes roughly 2,000 squarings and multiplications (since log⁡2x\log_2 x is about 2,000 for an exponent of that size). Brute force inversion would need on the order of pp tries, which is about 1060010^{600} steps, far beyond any computer.

Interpretation. The gap between about 2,000 operations and 1060010^{600} is the security margin. Real systems use parameters that keep the best known attacks far beyond practical reach.

Example
Multiplication is easy, factoring is not (at small scale)

Consider f(p,q)=p×qf(p, q) = p \times q with pp and qq prime.

  1. Forward: 101×103=10403101 \times 103 = 10403. One multiplication.
  2. Inverse: given N=10403N = 10403, find a factor. Trial division tries d=2,3,…,101d = 2, 3, \dots, 101. Divisibility by d=101d = 101 succeeds, giving 10403=101×10310403 = 101 \times 103.
  3. For NN of this size, the search is about 100 divisions. Doubling the digit count of NN does not double this cost; the best known factoring methods grow far faster.
  4. For a 2048-bit modulus N=pqN = pq with p,qp, q each about 1024 bits, the best known classical methods remain infeasible. Multiplication of the same two primes still takes microseconds.

Interpretation. Factoring is the basis of RSA's one-way behaviour, and the gap in cost is what the system relies on. (The 2048-bit claim reflects current best practice, not a proof that factoring is hard.)

02468101201000200030004000input size in bitsoperations (illustrative)
  • inverting by exhaustive search
  • computing the function
Forward cost grows polynomially, while exhaustive inversion grows exponentially. The curves are illustrative, not measured.

Tradeoffs and failure modes

SituationProblemBetter choice
Storing passwordsFast hashes are cheap to brute-force after a leakUse a slow, memory-hard password hashing function with a salt
Choosing parametersToo small an exponent or modulus is breakable as hardware improvesUse parameters that current standards recommend and revisit them over time
Relying on the assumptionNo one has proved one-way functions exist, so every construction is an unproven hardness assumptionPrefer schemes with well-studied assumptions and wide review
Quantum computersShor's algorithm inverts discrete logarithms and factoring efficiently on a large quantum computerPost-quantum schemes based on different hard problems
Trapdoor leakWhoever learns the secret can invert everythingProtect the trapdoor, and rotate keys

When to use it. Use one-way functions wherever the system must check knowledge of a secret without holding the secret itself, or wherever a public value must be derived from a private one.

When not to. A one-way function does not protect data that is low-entropy. If the input space is small, an attacker simply tries every input and checks the output, whatever the function's hardness.

Alternatives. For confidentiality, Public-Key Cryptography and symmetric encryption use these functions as building blocks. For integrity, cryptographic hash functions are one-way in a weaker sense (preimage resistance) and are often used directly.

History

The concept was made explicit in cryptography by Whitfield Diffie and Martin Hellman (1976), in New Directions in Cryptography, which proposed public-key distribution based on the apparent hardness of discrete logarithms. Ralph Merkle developed related ideas about hard-to-invert puzzles at about the same time. Ron Rivest, Adi Shamir and Leonard Adleman (1977) built on the trapdoor version with RSA, which uses factoring as its one-way step.

The idea caught on because it made secure communication without a shared secret possible. Over time the notion was formalised with the definitions above, and hash functions and password hashing adopted the same concept. Whether one-way functions exist at all is still an open question in complexity theory; cryptography proceeds on the assumption that they do.

Check yourself

QuestionRecall: what is the difference between a one-way function and a trapdoor one-way function?Show answer

A one-way function is easy to compute but hard to invert for everyone. A trapdoor one-way function has a secret trapdoor that makes inversion easy for whoever knows it, while still hard for everyone else. RSA's trapdoor is the factorisation of the modulus.

QuestionComputation: using f(x)=2x mod 11f(x) = 2^{x} \bmod 11, compute f(5)f(5).Show answer

25=322^{5} = 32, and 32=2×11+1032 = 2 \times 11 + 10, so f(5)=10f(5) = 10.

QuestionWhy: why does storing f(password)f(\text{password}) protect users better than storing the password, even if the attacker can run ff freely?Show answer

Storing f(password)f(\text{password}) means a leaked database reveals only images of the passwords, and the attacker must invert ff for each one. Running ff freely lets the attacker test guesses, but they must still guess correctly and cannot read passwords directly. The protection comes from the cost of inversion for strong passwords. For weak passwords it fails, because guessing is cheap. That is why password hashing also uses salts and slow functions.

QuestionApplied: a designer proposes f(x)=x2 mod 1000f(x) = x^{2} \bmod 1000 as a one-way function for storing short codes. What is wrong with this proposal?Show answer

The function is not one-way in practice. Its input space is small: xx can be any number up to 999, and any output has only a handful of preimages (at most about ten). An attacker can simply try all 1000 inputs and find every preimage in a moment. Security needs a large input space and a function whose inversion is infeasible, not merely a function that is awkward to reverse by hand.

Sources