Polynomial Fingerprints
Map each symbol to a nonzero integer. For $S=s_0s_1\dots s_{n-1}$, define $H(S)=(s_0b^{n-1}+s_1b^{n-2}+\dots+s_{n-1})\bmod p$, where $b$ is a base and $p$ is a prime modulus. Positional powers make order significant.
For a length-$m$ window, remove the outgoing highest-power term, shift the remaining polynomial by multiplying by $b$, and append the incoming symbol:
$$H_{i+1}=((H_i-t_i b^{m-1})b+t_{i+m})\bmod p.$$
Normalize negative residues in languages where remainder may stay negative. A left-to-right prefix recurrence gives position-independent substring hashes without inverses. A global-power convention is also valid but requires inverse powers or exponent alignment; mixing formulas is incorrect. Avoid bases $0$ and $1$, use nonzero symbol encodings, and randomize parameters when relying on probabilistic collision bounds.
Prefix Hashes and Substring Queries
Use $P[0]=0$ and $P[i+1]=(P[i]b+s_i)\bmod p$. For the half-open substring $S[l\dots r)$ of length $k=r-l$,
$$H(l,r)=(P[r]-P[l]b^{r-l})\bmod p.$$
Precomputed powers make extraction $O(1)$ after $O(n)$ preprocessing. The result already uses local powers, so no modular inverse is needed. Always compare length alongside the fingerprint. Two independently parameterized hashes reduce collision probability but do not make equality mathematically certain. Concatenation is composable through $H(XY)=H(X)b^{|Y|}+H(Y)\pmod p$.
Collision Analysis
For unequal strings of maximum length $L$, a collision means their nonzero difference polynomial evaluates to zero modulo $p$. Its degree is at most $L-1$, so if the base is sampled uniformly after the strings are fixed, one comparison collides with probability at most $(L-1)/p$.
Across at most $Q$ comparisons, the union bound gives $\Pr[\text{any collision}]\le Q(L-1)/p$ without assuming independence. Fixed public bases do not provide the same adversarial guarantee. Randomize parameters per process or dataset, use a keyed hash at security boundaries, and obtain a product-style double-hash bound only when parameter choices justify independence.
Hash-only equality is Monte Carlo. Verifying the original substrings after a hash match makes the result deterministic and uses the hash only as a fast rejection filter.
Collision Budget
Suppose a deployment performs $Q=10^8$ substring comparisons, each of length at most $L=10^4$, using a randomly selected base modulo a prime near $2^{61}$.
One comparison has collision probability below $10^4/(2^{61}-1)\approx4.34\times10^{-15}$. The workload union bound is below $10^{12}/(2^{61}-1)\approx4.34\times10^{-7}$, about one failure budget per $2.3$ million deployments.
With two independently parameterized $61$-bit fingerprints, the workload bound is approximately $10^8(4.34\times10^{-15})^2\approx1.88\times10^{-21}$. If probabilistic equality is still unacceptable, compare the source symbols whenever both hashes match.
Dynamic and Multidimensional Extensions
A segment tree can store (length, forward hash, reverse hash) at each node. The concatenation identity recomputes parents after point updates in $O(\log n)$; comparing forward and reverse hashes gives a probabilistic palindrome test. Implicit balanced trees and ropes extend the same aggregate to insertion, deletion, split, join, and persistence.
A Fenwick tree can maintain globally weighted terms for fixed-length text with point updates, but ranges at different positions must be shifted to a common exponent. In a grid, independent row and column bases define a 2D polynomial; 2D prefixes or separable row-then-column hashes extract rectangles efficiently. Compare dimensions with residues and use independent parameter pairs to reduce structural collisions.
Persistent path copying creates one root per version while sharing unchanged subtrees, supporting historical substring comparisons. In every extension, a hash remains a compact probabilistic certificate rather than a proof.
Monte Carlo mode accepts equal-length substrings when their fingerprints match and carries an explicit collision probability. Verified mode uses hashes only to reject unequal candidates, then compares the original bytes, code points, or tokens before returning equality; this preserves the speed benefit while making the final answer deterministic.