From Functions and Set Theory to Asymmetric Cryptography

A gentle mathematical introduction with diagrams, worked examples and an explanation of what changes when we move from abstract bijections to practical crypto-systems.

All mathematical notation uses LaTeX ($...$ and $$...$$). The illustrations are supplied as local PNG files in diagrams/. Keep that folder alongside this Markdown file. The document works best in a Markdown viewer that supports LaTeX.

1. Sets, functions and mappings

A set is a collection of elements. For instance,

(1)

A function associates each input for which it is defined with exactly one output. We write a total function as

(2)

Here, is the domain, the codomain, and the output for an input . The image (sometimes called the range) is the set of outputs actually reached:

(3)

Where means is possibly equal to or is a subset of. The codomain and image need not be identical. This distinction will matter when discussing surjectivity.

1.1 Total functions

A function is total when every element of its declared domain has exactly one output:

(4)

The symbol means there exists exactly one. The mapping below is total.

A total function: all three inputs mapped

For example, defined by is total: every real input has a real square.

1.2 Partial functions

A partial function may be undefined for some elements of its declared input set. A common notation is

(5)

A partial function: the third input is undefined

Consider , described as a map . This is partial because is undefined. However, if we restrict its domain,

(6)

then it is total. Totality is determined by the declared domain as well as the rule.

For a partial function, define its domain of definition:

(7)

The restriction is total. We can use this restricted total function to reason about the properties below.

2. Injection: no two different inputs collide

A function is injective, or one-to-one, when

(8)

Equivalently, distinct inputs always have distinct outputs. Injection does not require every element of to be reached.

Injection: three distinct outputs, with one unused codomain element

Example. The total function , , is injective. If , then .

Counterexample. The function on is not injective because .

For a partial function, injection is usually checked only over its domain of definition : if two defined inputs yield the same output, they must be equal.

3. Surjection: every codomain element is reached

A total function is surjective, or onto, when

(9)

Equivalently, . Several inputs may share an output.

Surjection: four inputs reach both outputs, with collisions

The function , , is surjective because every non-negative real number has a real square root. The same formula with codomain is not surjective, because it never produces a negative number.

For a partial map , we can ask whether every element of is reached by some element of . This means its restriction is surjective; terminology varies by author, so the intended domain should be stated explicitly.

4. Bijection: an exact pairing

A bijection is a total function that is both injective and surjective. Thus every has exactly one preimage in :

(10)

Bijection: every input pairs with one distinct output, and every output is used

The important consequence is that a bijection has an inverse:

(11)

For example,

(12)

A partial map can be bijective on its domain of definition , but it is not a bijection from all of to unless .

4.1 Summary table

PropertyQuestionFormal intuition
TotalDoes every permitted input have an output?, one output exists
PartialMay a declared input lack an output?
InjectiveCan different defined inputs have the same output?No collisions
SurjectiveIs every codomain element reached?
BijectiveIs there exactly one input for each output?Total + injective + surjective

Existence and uniqueness provide a useful memory aid. Totality is about the existence of an output for each input. Functionality is about its uniqueness. Surjectivity is about the existence of a preimage for each output. Injectivity is about the uniqueness of that preimage.

How does a bijection differ from a total function?

The fundamental difference is that a total function guarantees that every input has an output, whereas a bijection guarantees a unique, reversible correspondence between every input and output.

Every bijection is a total function, but not every total function is a bijection.

1. Total function

A total function:

(13)

requires that every element of maps to exactly one element of .

Formally:

(14)

However, two different inputs are allowed to produce the same output, and some elements of might never be reached.

Screenshot 2026-11-19 at 17.33.34

2. Bijective function

A bijection imposes two additional requirements on a total function:

Mathematically:

(15)

Notice what's changed: now we're saying that every output has exactly one corresponding input.

Screenshot 2026-11-19 at 17.35.14

3. Compare them using actual functions

Consider these two functions: Function 1: Total, but not bijective

(16)

Every real number has a square, so the function is total.

But:

(17)

This violates injectivity. Also, negative numbers never appear as outputs, violating surjectivity.

Function 2: Total and bijective

(18)

Every real input produces an output, no two inputs share the same output, and every real output can be reached.

We can reverse it:

(19)
  1. The relationship in one table Property Total function Bijective function Every input has an output Yes Yes Each input has only one output Yes Yes Different inputs have different outputs Not necessarily Yes Every codomain element is reached Not necessarily Yes Has a unique inverse function (B\to A) Not necessarily Yes

  2. Why this matters for cryptography Suppose an encryption function maps plaintexts to ciphertexts:

(20)

A total function guarantees that every valid plaintext can be encrypted.

But totality alone does not guarantee that ciphertexts can be uniquely decrypted.

Imagine:

(21)
(22)

Given ciphertext 42, how would you know which plaintext was originally encrypted?

You couldn't.

For deterministic encryption with exact decryption, encryption must at least be injective: distinct plaintexts cannot produce the same ciphertext.

If encryption is also surjective onto its declared ciphertext set, then it is bijective, and a mathematical inverse exists:

(23)

So:

(24)

This is the mathematical foundation of reversible transformations used in cryptography. Modern public-key encryption adds considerations such as randomisation, keys and computational security.

The key distinction to remember: totality tells you whether a function can process every input. Bijection tells you whether you can move uniquely in both directions between its domain and codomain.

5. Why these ideas lead naturally to cryptography

Suppose we want to transform a message into a ciphertext , then recover the message later. The simplest mathematical abstraction is

(25)

where is the message space and is the ciphertext space. Correctness requires

(26)

This single equation already tells us something surprisingly strong: must be injective.

5.1 A short proof

Assume . Apply decryption to both sides:

(27)

By correctness,

(28)

Therefore encryption is injective. Two different plaintexts cannot give the same ciphertext if a single deterministic decryption operation must recover them both without error.

Does that make encryption bijective? Not necessarily. might include values that no valid encryption produces. In that case encryption is injective but not surjective onto . However, it is bijective from to its image .

This is the first bridge from set theory to cryptographic design: the ability to decrypt uniquely constrains the mathematical shape of encryption.

6. Symmetric versus asymmetric cryptography

In symmetric encryption, both sides use a shared secret key . We can write the deterministic idealisation

(29)

In asymmetric (public-key) cryptography, there are two related keys:

The basic goal is that anybody can use to encrypt, but only somebody with can efficiently decrypt:

(30)

The key distinction is not that the mathematical encryption function is impossible to invert. Rather, encryption is designed so that an authorised party can invert it efficiently using a secret trapdoor, while inversion without that trapdoor is computationally infeasible under suitable assumptions.

An inverse may exist mathematically without being feasible to compute in practice. That distinction is fundamental to public-key cryptography.

7. Trapdoor one-way permutations

A permutation of a set is a bijection . A trapdoor one-way permutation is a special family of permutations for which:

  1. Computing is efficient.
  2. Inverting on a randomly chosen input is believed to be computationally hard without secret information.
  3. The trapdoor makes inversion efficient.

The word one-way refers to computational difficulty, not the mathematical absence of an inverse. Because is bijective, its inverse exists.

RSA as a trapdoor permutation

These conditions describe an idealised cryptographic primitive. Not every public-key scheme uses a trapdoor permutation, and a trapdoor permutation by itself is not a complete secure encryption scheme.

8. RSA: a concrete example of a public permutation and private inverse

RSA gives an elegant example of this idea. Begin with two distinct primes and , and compute

(31)

Work with residues modulo , the set

(32)

Choose a positive integer such that

(33)

where is the Carmichael function for a product of two distinct primes. Then compute satisfying

(34)

The public map is

(35)

The private inverse is

(36)

With these conditions, for every ,

(37)

Therefore is a bijection of and is its inverse. This holds for all residues, including non-units, when is a product of distinct primes and the exponents are chosen as above. One can prove it by applying Fermat's little theorem separately modulo and , then combining the results using the Chinese remainder theorem.

8.1 A small, fully worked RSA example

Take deliberately tiny values (these are not secure):

(38)
(39)

Choose and . They satisfy

(40)

For the toy message ,

(41)

And decryption gives

(42)

Thus we have both the forward computation and its inverse. The public information is ; the private exponent is derived from secret information about the factors of .

8.2 What exactly makes this asymmetric?

The public map is easy to evaluate using modular exponentiation. The inverse is also easy if is known. Deriving from public information alone is believed to be difficult for appropriately chosen large RSA parameters. The security of inverting the RSA map is formalised by the RSA assumption; its precise relationship to integer factorisation should not be overstated as a proven equivalence.

Important: the example above is textbook RSA. It is deterministic and insecure as an encryption scheme. It reveals equality of plaintexts and lacks semantic security. Real systems must use a secure encoding or key-encapsulation construction, such as RSA-OAEP for encryption under suitable implementation and parameter choices. One must also handle padding errors and side channels carefully.

9. Why modern public-key encryption is not simply a bijection

Practical encryption is ordinarily randomised. Instead of considering as one fixed ciphertext, we make the random coins explicit:

(43)

Here is the random choice used by the encryptor. Correctness says

(44)

Modern randomised public-key encryption

The same plaintext can now produce many ciphertexts because changes. Therefore, when randomness is not treated as an explicit argument, encryption is better viewed as a randomised algorithm or a mapping from a message to a ciphertext distribution, not as a deterministic function .

We can nevertheless recover an important injection property. Define the set of possible ciphertexts for message :

(45)

Perfect correctness implies that for any distinct messages ,

(46)

Why? If the same ciphertext could represent two different plaintexts, deterministic decryption of that ciphertext could not recover both correctly. For a fixed value of , the map is consequently injective under the same correctness assumptions. However, the map need not be injective in the randomness coordinate: different random choices may generate the same ciphertext for the same message.

Probabilistic encryption must additionally prevent adversaries from distinguishing encryptions of chosen messages. This is an indistinguishability requirement, commonly formalised by IND-CPA or the stronger IND-CCA notion. Those security properties are not implied by injection or bijection.

10. What about partial functions in cryptography?

Partiality occurs naturally when a mathematical operation has a restricted domain or an implementation rejects malformed input.

10.1 Modular inverse

An integer has a multiplicative inverse modulo if and only if

(47)

Thus the modular inverse operation

(48)

is partial on all residues: for example, has no multiplicative inverse modulo . But on the group of units

(49)

inversion is a total bijection. In fact, it is its own inverse:

(50)

This makes a subtle point: a mathematical expression can be partial on one set and total and bijective when properly restricted to another.

10.2 Decryption and invalid ciphertexts

A cryptographic decoder may reject malformed encodings, invalid padding or invalid group elements. Mathematically, we could describe it as a partial map

(51)

In real APIs, it is often safer and clearer to make the behaviour total by including an explicit failure result:

(52)

where means failure. This does not make decryption surjective onto all possible output values in every design; it simply models rejection explicitly. It also does not itself guarantee side-channel safety: implementations must avoid exposing sensitive distinctions through timing and error behaviour.

Asymmetric cryptography includes key agreement, not just encryption. Diffie–Hellman is a classic example.

Take a cyclic group of prime order , generated by . Alice chooses a secret exponent and publishes . Bob chooses and publishes . Their shared value is

(53)

Diffie-Hellman key agreement

A useful set-theoretic observation is that, with of prime order and a generator, the map

(54)

is a bijection. Yet public-key secrecy does not follow from bijectivity. It relies on the computational difficulty of recovering the secret exponent (the discrete logarithm problem) and, for common shared-secret security formulations, assumptions such as computational or decisional Diffie–Hellman. Real protocols additionally require validation, authentication as appropriate, and a key-derivation function.

Not every modern elliptic-curve implementation exposes a perfect bijection between arbitrary encoded private scalars and public keys: clamping, equivalent scalar representations, cofactors and encoding rules affect such claims. Always specify the exact domain, codomain and algorithm before asserting injectivity or bijectivity.

12. Signatures are not encryption run backwards

A digital-signature scheme usually has

(55)

Verification is a predicate, not an inverse that reconstructs an original message. Signature algorithms may be randomised or deterministic; signature verification does not require a bijection between messages and signatures. Textbook descriptions of RSA sometimes suggest that signing is merely "private-key encryption". That is not a sound description of modern signature schemes such as RSA-PSS, which use explicit hashing, encoding and verification rules.

13. A compact comparison

ConstructionSimplified mathematical formProperty that mattersWhat provides security?
Deterministic reversible map, must be injectiveNot guaranteed by reversibility
Trapdoor permutationBijection with an inverseInversion assumed difficult without trapdoor
Textbook RSA mapPermutation of under key conditionsRSA inversion assumption; textbook encryption still insecure
Randomised encryptionDifferent messages have disjoint sets of valid ciphertexts under perfect correctnessIndistinguishability and robust implementation
Modular inversePartial, but total on Algebraic primitive, not security by itself
Diffie–Hellman agreementGroup operations and matching shared valuesDiscrete-log/DH assumptions, plus protocol safeguards

14. Exercises

Exercise 1: domain restrictions. Is bijective as a function ? What happens if you restrict its domain to and its codomain to ?

Exercise 2: prove injectivity. Suppose for every message . Prove that is injective. Which part of the proof depends on the existence of a deterministic decryption function?

Exercise 3: partial inverse. List the elements of that have multiplicative inverses modulo . Show that inversion is total on but partial on .

Exercise 4: randomised encryption. Let denote all valid ciphertexts that might encrypt . Under perfect decryption correctness, prove that for distinct messages and . Must the map be injective?

Exercise 5: assess a claim. A developer says: "My cipher is bijective, so it is secure." Explain why this conclusion does not follow. Give the identity map as a counterexample.

Answers and hints

  1. Over , it is neither injective nor surjective. Over , it is bijective, with inverse .
  2. If , apply and use correctness to get . A deterministic decoder cannot assign two different results to one ciphertext.
  3. ; their respective inverses are .
  4. Any shared ciphertext would have to decrypt to two different messages, contradicting deterministic correctness. Distinct random coins may still encrypt the same message to the same ciphertext.
  5. The identity is a bijection but hides nothing. Security involves computational indistinguishability, not merely a reversible correspondence.

15. Conclusion

Set theory provides a language for expressing exactly what a cryptographic construction must do. Totality asks whether an operation is defined. Injectivity explains why a uniquely decryptable deterministic encoding cannot collapse two distinct messages into the same result. Surjectivity distinguishes the full codomain from outputs actually reached. Bijection establishes the existence of a unique inverse.

Asymmetric cryptography builds on this algebra but introduces a fundamentally different idea: computational asymmetry. A transformation may have an inverse that exists mathematically and is efficient to compute with a private key, yet infeasible to recover without it. RSA exhibits this through a trapdoor permutation. Modern public-key encryption additionally uses randomness and formal security properties that no bare bijection can supply. The mathematical theory of functions is therefore a useful foundation, but the security argument begins only after we introduce computational assumptions, randomisation, adversarial models and careful protocol design.


Suggested further reading

All numerical RSA examples in this document are instructional only and must not be used for real encryption.