1. Mathematical Formalism of Functions
In computer science, functions are not merely subroutines that accept arguments and return outputs—they are formal mathematical mappings that model transformations, data serialization, compiler semantics, and cryptography.
Formal Definition
Let $A$ and $B$ be non-empty sets. A function (or mapping) $f$ from $A$ to $B$, denoted $f: A \to B$, is a subset of the Cartesian product $A \times B$ such that for every element $a \in A$, there exists a unique element $b \in B$ such that $(a, b) \in f$.
We write $f(a) = b$, where:
- $A$ is the Domain ($\text{dom}(f)$).
- $B$ is the Codomain ($\text{codom}(f)$).
- $b$ is the image of $a$ under $f$, and $a$ is a pre-image of $b$.
- The Range (or Image) of $f$ is the set of all elements in $B$ that are actually mapped to: $$\text{Im}(f) = f(A) = { b \in B : \exists a \in A \text{ such that } f(a) = b } \subseteq B$$
text
2. Fundamental Function Properties
The structural properties of functions govern whether transformations preserve uniqueness, cover target spaces, or can be run in reverse.
text
2.1 Injective Functions (One-to-One)
A function $f: A \to B$ is injective if distinct elements in the domain map to distinct elements in the codomain:
$$\forall a_1, a_2 \in A, \quad a_1 \neq a_2 \implies f(a_1) \neq f(a_2)$$
Equivalently by contraposition:
$$\forall a_1, a_2 \in A, \quad f(a_1) = f(a_2) \implies a_1 = a_2$$
CS Example: In a database table, primary key assignment is an injective mapping from database entities to integers. Cryptographic hash functions strive for pseudo-injectivity (collision resistance).
2.2 Surjective Functions (Onto)
A function $f: A \to B$ is surjective if its range equals its entire codomain:
$$\forall b \in B, \quad \exists a \in A \text{ such that } f(a) = b$$
CS Example: A hash function mapping 64-bit strings to 32-bit buckets is surjective (every bucket can be reached).
2.3 Bijective Functions (One-to-One Correspondence)
A function $f: A \to B$ is bijective if it is both injective and surjective.
If $A$ and $B$ are finite sets, a bijection exists if and only if $|A| = |B|$.
CS Examples:
- Symmetric cryptography (AES-256 block cipher encryption $E_K(P) = C$) is a bijection over 128-bit blocks; if it were not a bijection, decryption would either be ambiguous or impossible!
- Lossless compression and decompression (gzip, zstd) are mutually inverse bijections.
3. Inverse Functions
A function $f: A \to B$ has an inverse function $f^{-1}: B \to A$ if and only if $f$ is bijective.
Why Inverses Require Bijectivity:
- If $f$ is not injective: There exist $a_1 \neq a_2$ such that $f(a_1) = f(a_2) = b$. Then $f^{-1}(b)$ would have to map to both $a_1$ and $a_2$, violating the deterministic requirement of a function.
- If $f$ is not surjective: There exists some $b \in B$ with no pre-image in $A$. Then $f^{-1}(b)$ would be undefined, violating the exhaustive requirement of a function.
When $f$ is bijective, its inverse satisfies:
$$f^{-1}(f(a)) = a \quad (\forall a \in A), \qquad f(f^{-1}(b)) = b \quad (\forall b \in B)$$
$$f^{-1} \circ f = \text{id}_A, \qquad f \circ f^{-1} = \text{id}_B$$
4. Function Composition
Let $f: A \to B$ and $g: B \to C$. The composite function $g \circ f: A \to C$ is defined by:
$$(g \circ f)(a) = g(f(a))$$
text
4.1 Algebraic Properties of Composition
- Associative: For $f: A \to B$, $g: B \to C$, and $h: C \to D$: $$h \circ (g \circ f) = (h \circ g) \circ f$$
- Non-Commutative: In general, $g \circ f \neq f \circ g$.
- Preservation of Properties:
- If $f$ and $g$ are injective, then $g \circ f$ is injective.
- If $f$ and $g$ are surjective, then $g \circ f$ is surjective.
- If $f$ and $g$ are bijective, then $g \circ f$ is bijective, and its inverse is: $$(g \circ f)^{-1} = f^{-1} \circ g^{-1}$$ (Note the reversed order: socks first, then shoes $\implies$ undo shoes first, then socks!)
5. Hash Functions & Non-Injectivity
In software engineering, a Hash Function maps a potentially unbounded domain (arbitrary byte strings $\mathcal{U} = {0, 1}^*$) to a fixed-size codomain of $b$ bits ($\mathcal{B} = {0, 1}^b$):
$$h: {0, 1}^* \to {0, 1}^b$$
The Inevitability of Collisions
Because $|\mathcal{U}| = \infty$ and $|\mathcal{B}| = 2^b < \infty$, by the Pigeonhole Principle, $h$ cannot be injective! There exist infinitely many pairs of distinct strings $x \neq y$ such that:
$$h(x) = h(y) \quad (\text{Hash Collision})$$
Cryptographic Resistance Properties
While injectivity is mathematically impossible, cryptographic hash functions (SHA-256) enforce computational hardness guarantees:
- Pre-image Resistance (One-Way): Given $y \in \mathcal{B}$, it is computationally intractable to find any $x$ such that $h(x) = y$ (Complexity: $O(2^b)$).
- Second Pre-image Resistance (Weak Collision Resistance): Given $x$, it is intractable to find $x' \neq x$ such that $h(x) = h(x')$ (Complexity: $O(2^b)$).
- Collision Resistance (Strong Collision Resistance): It is intractable to find any arbitrary pair $x \neq y$ such that $h(x) = h(y)$ (Complexity: $O(2^{b/2})$ due to the Birthday Bound).
6. Python Implementation: Verifying Function Properties & Inverses
python
7. Key Takeaways
- Functions are Deterministic Mappings: Every domain element must map to exactly one codomain element.
- Invertibility Requires Bijectivity: Only functions that are both one-to-one (injective) and onto (surjective) possess an inverse.
- Reversed Inverses: $(g \circ f)^{-1} = f^{-1} \circ g^{-1}$.
- Hash Non-Injectivity: Pigeonhole theorem dictates that any mapping from a larger domain to a smaller codomain must suffer collisions.