[proofplan]
The strategy is local-to-global: replace the global geometric statement by a statement about a finitely generated module over the discrete valuation ring $A := \mathcal{O}_{D,q}$. The pullback $\varphi^* : A \to B$ realises the semilocalisation $B := \bigcap_{i} \mathcal{O}_{C, p_i}$ at the preimages $p_1, \ldots, p_k$ of $q$ as a finitely generated, torsion-free, hence free $A$-module. The rank of $B$ over $A$ is computed in two ways: (i) by tensoring with the fraction field $K(D)$, the rank equals the degree $[K(C) : K(D)] = \deg \varphi$; (ii) by reducing mod the maximal ideal $\mathfrak{m}_q \subset A$, the quotient $B/\mathfrak{m}_q B$ decomposes via the *Chinese Remainder Theorem* as a direct product of $\mathcal{O}_{C, p_i}/\mathfrak{m}_{p_i}^{e_{p_i}}$, whose total $k$-dimension is $\sum_i e_{p_i}$. Equating the two computations of the rank gives the formula.
[/proofplan]
[step:Set up the semilocal ring at the fibre and identify it as a finitely generated $A$-module]
\textbf{Finiteness of $\varphi$.} We first verify that $\varphi : C \to D$ is a finite morphism. Since $C$ is projective and $D$ is separated, $\varphi$ is proper. Since $\varphi$ is nonconstant, its image is a closed irreducible subset of $D$ of dimension $\geq 1$; the only such subset of the curve $D$ is $D$ itself, so $\varphi$ is surjective and dominant. For any closed point $q \in D$, the fibre $\varphi^{-1}(q)$ is a closed subscheme of $C$, proper over $\operatorname{Spec}\kappa(q)$, and — being a proper subset of the irreducible curve $C$ (since $\varphi$ is dominant, $\varphi^{-1}(q) \neq C$) — has dimension $0$. Hence $\varphi$ is quasi-finite. We invoke the standard result that a proper quasi-finite morphism of noetherian schemes is finite; this gives that $\varphi$ is finite. In particular, for any affine open $V \subset D$, the preimage $\varphi^{-1}(V) \subset C$ is affine, and the pullback $\mathcal{O}_D(V) \to \mathcal{O}_C(\varphi^{-1}(V))$ is a finite ring extension.
\textbf{Fibre at $q$.} Since $\varphi$ is finite, the fibre
\begin{align*}
\varphi^{-1}(q) = \{p_1, \ldots, p_k\} \subset C
\end{align*}
is a finite (non-empty, by surjectivity) set of closed points.
\textbf{Local rings and semilocalisation.} Set
\begin{align*}
A &:= \mathcal{O}_{D, q}, & B &:= \bigcap_{i=1}^{k} \mathcal{O}_{C, p_i} \subset K(C),
\end{align*}
where the intersection is taken inside the function field $K(C)$. The ring $A$ is a discrete valuation ring because $D$ is a smooth curve and $q \in D$ is a closed point. Each $\mathcal{O}_{C, p_i}$ is a discrete valuation ring for the same reason.
\textbf{Description of $B$ as a semilocalisation.} Choose an affine open $V \subset D$ with $q \in V$, and set $U := \varphi^{-1}(V) \subset C$. Since $\varphi$ is finite, $U$ is affine and $\varphi^{-1}(q) = \{p_1, \ldots, p_k\} \subset U$. Write $R := \mathcal{O}_D(V)$ and $S := \mathcal{O}_C(U)$, so that the pullback gives a finite ring extension $\varphi^\# : R \hookrightarrow S$ (injectivity from dominance). Let $\mathfrak{q} \subset R$ be the maximal ideal at $q$ and $\mathfrak{p}_i \subset S$ the maximal ideals at $p_i$; since $\varphi^{-1}(q) = \{p_1, \ldots, p_k\}$, the $\mathfrak{p}_i$ are precisely the primes of $S$ lying over $\mathfrak{q}$. Localise at the multiplicative set $R \setminus \mathfrak{q} \subset R \subset S$ (which inverts only the elements of $R$ \emph{outside} $\mathfrak{q}$, \emph{not} all of $R \setminus \{0\}$):
\begin{align*}
A = R_\mathfrak{q}, \qquad B = S[(R \setminus \mathfrak{q})^{-1}] = \bigcap_{i=1}^{k} S_{\mathfrak{p}_i} = \bigcap_{i=1}^{k} \mathcal{O}_{C, p_i}.
\end{align*}
The second equality is the standard identification of the semilocalisation of $S$ at the (finitely many) primes $\mathfrak{p}_i$ above $\mathfrak{q}$ with the intersection of the localisations $S_{\mathfrak{p}_i}$ inside $\operatorname{Frac}(S) = K(C)$: an element $b \in K(C)$ lies in the semilocalisation iff it is regular at each $p_i$. This is what the description '$B$ is the semilocalisation at $\{p_1, \ldots, p_k\}$' means. \emph{We do not localise at $R \setminus \{0\}$} — doing so would invert \emph{all} non-zero elements of $R$ and produce $K(C)$ itself (a field, not a Dedekind domain with $k$ maximal ideals).
Since $S$ is the integral closure of $R$ in $K(C)$ (because $C$ is smooth, hence normal, and $S$ is the ring of regular functions on the affine open $U$ of the normal variety $C$, which equals the integral closure of $R$ in $K(C)$), $B$ is the integral closure of $A$ in $K(C)$. The ring $B$ is a semilocal Dedekind domain with exactly $k$ maximal ideals $\mathfrak{m}_i$ (the images of $\mathfrak{p}_i$ in $B$), corresponding to the preimages $p_i$.
\textbf{Finite generation of $B$ over $A$.} Since $R \hookrightarrow S$ is a finite ring extension, $S$ is finitely generated as an $R$-module. Localisation of modules preserves finite generation: $B = S \otimes_R R_\mathfrak{q} = S \otimes_R A$, and the tensor product of a finitely generated $R$-module with the ring $A$ over $R$ is a finitely generated $A$-module. Hence $B$ is a finitely generated $A$-module.
\textbf{Pullback homomorphism.} The pullback induces a ring homomorphism
\begin{align*}
\varphi^* : A &\to B \\
g &\mapsto g \circ \varphi,
\end{align*}
compatible with the global pullback $R \hookrightarrow S$ via localisation.
[/step]
[step:Conclude that $B$ is a free $A$-module of finite rank]
Since $A$ is a discrete valuation ring (in particular a [principal ideal domain](/page/Principal%20Ideal%20Domain)), and $B$ is a finitely generated $A$-module, the *Structure Theorem for Finitely Generated Modules over a PID* decomposes $B$ as
\begin{align*}
B \cong A^{\oplus r} \oplus T,
\end{align*}
where $T$ is the torsion submodule. We claim $T = 0$. Indeed, $B$ is a subring of $K(C)$, hence has no nonzero zero divisors. Any $A$-torsion element $b \in B$ satisfies $a b = 0$ for some nonzero $a \in A$, but $\varphi^*(a) \neq 0$ in $B$ (the pullback of a nonzero rational function on $D$ is a nonzero rational function on $C$, since $\varphi$ is dominant), so $a b = 0$ in $B$ forces $b = 0$. Hence $T = 0$ and
\begin{align*}
B \cong A^{\oplus r}
\end{align*}
as $A$-modules, for some integer $r \geq 1$.
[guided]
The classification of finitely generated modules over a PID says that any such module is isomorphic to a direct sum of a free part and cyclic torsion summands $A/(a_i)$. To conclude $B$ is free, we must rule out torsion.
The argument uses two facts about $B$. First, $B \subset K(C)$ is an integral domain (it is a subring of a field), so it has no nonzero zero divisors when viewed as a ring. Second, $\varphi^* : A \to B$ is injective: a nonzero rational function $a \in A \subset K(D)$ pulls back to $\varphi^* a \in K(C)$ which is nonzero, because $\varphi$ is nonconstant — equivalently, $\varphi$ is dominant onto $D$ as a morphism of irreducible varieties, so the pullback on rational functions is injective.
Putting these together: if $b \in B$ is $A$-torsion, then there is a nonzero $a \in A$ with $\varphi^*(a) \cdot b = 0$ in $B$. Since $\varphi^*(a) \neq 0$ in the integral domain $B$, we conclude $b = 0$. Hence the torsion submodule $T$ vanishes and $B \cong A^{\oplus r}$ for some $r \geq 1$.
Why is $r \geq 1$? Because $B \neq 0$ — for instance, $1 \in B$.
[/guided]
[/step]
[step:Compute the rank by tensoring with the fraction field]
Tensoring $B \cong A^{\oplus r}$ with the fraction field $K(D) = \operatorname{Frac}(A)$ over $A$:
\begin{align*}
B \otimes_A K(D) \cong K(D)^{\oplus r}.
\end{align*}
On the other hand, $B \otimes_A K(D)$ is the localisation of $B$ at $A \setminus \{0\}$. Since $B \subset K(C)$ and every nonzero element of $A$ becomes invertible in $K(C)$, the localisation is identified with $K(C)$ itself: every element of $K(C)$ can be written as $b/a$ with $b \in B$ and $a \in A \setminus \{0\}$ (clear denominators in a sufficiently small affine open by multiplying by a regular function on $D$ that is nonzero at $q$), and conversely every $b/a$ with $b \in B$, $a \in A \setminus \{0\}$ is in $K(C)$. Hence
\begin{align*}
B \otimes_A K(D) \cong K(C).
\end{align*}
Comparing dimensions over $K(D)$:
\begin{align*}
r = \dim_{K(D)} K(C) = [K(C) : K(D)] = \deg \varphi,
\end{align*}
where the last equality is the definition of the degree of a finite morphism of curves as the degree of the induced field extension.
[/step]
[step:Compute the rank by reducing modulo $\mathfrak{m}_q$]
Reducing $B \cong A^{\oplus r}$ modulo $\mathfrak{m}_q$ — that is, tensoring with the residue field $\kappa(q) = A/\mathfrak{m}_q = k$ (using that $k$ is algebraically closed and $q$ is a $k$-point) — we obtain
\begin{align*}
B \otimes_A k = B / \mathfrak{m}_q B \cong k^{\oplus r}.
\end{align*}
We compute the left-hand side directly. Let $\mathfrak{m}_i := \mathfrak{m}_{p_i} \cap B$ be the maximal ideals of $B$. The maximal ideals of $B$ are exactly the $\mathfrak{m}_i$, $i = 1, \ldots, k$, and they are pairwise coprime (any two distinct maximal ideals of a commutative ring are coprime).
\textbf{Identify $\mathfrak{m}_q B$ as a principal ideal.} Let $t \in \mathfrak{m}_q$ be a uniformiser of $A$, so that $\mathfrak{m}_q = (t)$ as an ideal of $A$ (this uses that $A$ is a DVR, hence $\mathfrak{m}_q$ is principal). Then $\mathfrak{m}_q B = (\varphi^* t)$ in $B$: an ideal extension from a principal ideal is principal, generated by the image of the generator.
\textbf{Order of $\varphi^* t$ at each $p_i$.} In the localisation $B_{\mathfrak{m}_i} = \mathcal{O}_{C, p_i}$, the ramification index is defined by
\begin{align*}
\operatorname{ord}_{p_i}(\varphi^* t) = e_{p_i},
\end{align*}
by the very definition of the ramification index (the statement of the theorem). Hence in $\mathcal{O}_{C, p_i}$, the element $\varphi^* t$ generates the ideal $\mathfrak{m}_{p_i}^{e_{p_i}}$, i.e.\ $(\varphi^* t) \mathcal{O}_{C, p_i} = \mathfrak{m}_{p_i}^{e_{p_i}}$.
\textbf{Factor $(\varphi^* t)$ in the semilocal Dedekind ring $B$.} Since $B$ is a semilocal Dedekind domain with maximal ideals $\mathfrak{m}_1, \ldots, \mathfrak{m}_k$, every nonzero ideal of $B$ factors uniquely as a product of prime powers. The ideal $(\varphi^* t)$ has, locally at $\mathfrak{m}_i$, valuation $e_{p_i}$, so its unique factorisation is
\begin{align*}
(\varphi^* t) = \prod_{i=1}^{k} \mathfrak{m}_i^{e_{p_i}} = \bigcap_{i=1}^{k} \mathfrak{m}_i^{e_{p_i}},
\end{align*}
where the second equality holds because the $\mathfrak{m}_i^{e_{p_i}}$ are pairwise coprime (distinct maximal ideals are coprime, and powers of coprime ideals are coprime — see the guided block).
\textbf{Apply CRT.} We invoke the *Chinese Remainder Theorem* for commutative rings: if $I_1, \ldots, I_k$ are pairwise coprime ideals of a commutative ring $R$, then the natural map $R / \bigcap_i I_i \to \prod_i R / I_i$ is a ring isomorphism. Hypotheses: $B$ is a commutative ring, and the ideals $\mathfrak{m}_i^{e_{p_i}}$ are pairwise coprime. Hence
\begin{align*}
B / \mathfrak{m}_q B = B / (\varphi^* t) = B / \bigcap_{i=1}^{k} \mathfrak{m}_i^{e_{p_i}} \cong \prod_{i=1}^{k} B/\mathfrak{m}_i^{e_{p_i}}.
\end{align*}
\textbf{Localisation is harmless modulo $\mathfrak{m}_i^{e_{p_i}}$.} The localisation map $\lambda_i : B \to B_{\mathfrak{m}_i} = \mathcal{O}_{C, p_i}$ descends to a map $\bar\lambda_i : B/\mathfrak{m}_i^{e_{p_i}} \to \mathcal{O}_{C, p_i}/\mathfrak{m}_{p_i}^{e_{p_i}}$. We verify it is an isomorphism:
\emph{Surjectivity.} Take any $[\alpha/\beta] \in \mathcal{O}_{C, p_i}/\mathfrak{m}_{p_i}^{e_{p_i}}$ with $\alpha \in B$, $\beta \in B \setminus \mathfrak{m}_i$. Since $\beta \notin \mathfrak{m}_i$, its image $\bar\beta \in B/\mathfrak{m}_i^{e_{p_i}}$ is a unit: the ring $B/\mathfrak{m}_i^{e_{p_i}}$ is local with maximal ideal $\mathfrak{m}_i/\mathfrak{m}_i^{e_{p_i}}$, so an element is a unit iff it is not in $\mathfrak{m}_i$. Hence there exists $\bar\gamma \in B/\mathfrak{m}_i^{e_{p_i}}$ with $\bar\beta \bar\gamma = 1$, and $\bar\alpha \bar\gamma \in B/\mathfrak{m}_i^{e_{p_i}}$ maps to $[\alpha/\beta]$.
\emph{Injectivity.} If $\bar\lambda_i([x]) = 0$ for some $x \in B$, then $x \in \mathfrak{m}_{p_i}^{e_{p_i}} \cap B$. Since $B_{\mathfrak{m}_i} = \mathcal{O}_{C, p_i}$, $\mathfrak{m}_{p_i}^{e_{p_i}} = \mathfrak{m}_i^{e_{p_i}} B_{\mathfrak{m}_i}$. An element of $B$ lies in $\mathfrak{m}_i^{e_{p_i}} B_{\mathfrak{m}_i} \cap B$ iff it lies in $\mathfrak{m}_i^{e_{p_i}}$ (using that $B$ is a Dedekind domain and the primary decomposition of $(x)$ at $\mathfrak{m}_i$ has $\mathfrak{m}_i$-part contained in $\mathfrak{m}_i^{e_{p_i}}$). Hence $x \in \mathfrak{m}_i^{e_{p_i}}$, i.e.\ $[x] = 0$.
Combining:
\begin{align*}
B / \mathfrak{m}_q B \cong \prod_{i=1}^{k} B/\mathfrak{m}_i^{e_{p_i}} \cong \prod_{i=1}^{k} \mathcal{O}_{C, p_i}/\mathfrak{m}_{p_i}^{e_{p_i}}.
\end{align*}
The $k$-vector space $\mathcal{O}_{C, p_i} / \mathfrak{m}_{p_i}^{e_{p_i}}$ has dimension $e_{p_i}$: a basis is given by $1, s_i, s_i^2, \ldots, s_i^{e_{p_i} - 1}$, where $s_i \in \mathfrak{m}_{p_i}$ is a uniformiser, by the structure theorem for the residue ring of a discrete valuation ring modulo $\mathfrak{m}_{p_i}^{e_{p_i}}$. Summing:
\begin{align*}
\dim_k (B / \mathfrak{m}_q B) = \sum_{i=1}^{k} e_{p_i}.
\end{align*}
[guided]
This step is where the ramification indices appear. The bridge from the algebraic rank $r$ to the geometric quantities $e_{p_i}$ goes through the residue ring $B/\mathfrak{m}_q B$.
The first key observation: tensoring the free $A$-module $B \cong A^{\oplus r}$ with $k = A/\mathfrak{m}_q$ gives $k^{\oplus r}$, so $\dim_k B/\mathfrak{m}_q B = r$. This is just commutative algebra.
The second key observation: $\mathfrak{m}_q B$ is the ideal of $B$ generated by the pullback of a uniformiser of $A$. Why? Because $\mathfrak{m}_q = (t)$ is principal as an ideal of $A$, so $\mathfrak{m}_q B = \varphi^*(\mathfrak{m}_q) B = (\varphi^* t)$. Now $\varphi^* t$ is a regular function on $C$ near each $p_i$, with $\operatorname{ord}_{p_i}(\varphi^* t) = e_{p_i}$ — this is literally the definition of $e_{p_i}$.
To compute $B/(\varphi^* t)$ we need to factor the ideal $(\varphi^* t)$ into a product (or intersection) of prime power ideals. The maximal ideals of $B$ are $\mathfrak{m}_1, \ldots, \mathfrak{m}_k$ (one for each preimage $p_i$), and locally at $\mathfrak{m}_i$ we have $(\varphi^* t) = \mathfrak{m}_i^{e_{p_i}} \mathcal{O}_{C, p_i}$. Globally on the semilocal ring $B$:
\begin{align*}
(\varphi^* t) = \prod_{i=1}^{k} \mathfrak{m}_i^{e_{p_i}} = \bigcap_{i=1}^{k} \mathfrak{m}_i^{e_{p_i}}
\end{align*}
(the product equals the intersection because the $\mathfrak{m}_i^{e_{p_i}}$ are pairwise coprime).
The *Chinese Remainder Theorem* requires (i) a commutative ring and (ii) pairwise coprime ideals. Both are satisfied: $B$ is a commutative integral domain, and distinct maximal ideals of any commutative ring are coprime. Powers of coprime ideals are coprime: if $1 = u + v$ with $u \in \mathfrak{m}_i$, $v \in \mathfrak{m}_j$, then $1 = (u+v)^N$ expanded by the binomial theorem lies in $\mathfrak{m}_i^{N - e_{p_j} + 1} + \mathfrak{m}_j^{e_{p_j}} \subset \mathfrak{m}_i^{e_{p_i}} + \mathfrak{m}_j^{e_{p_j}}$ for $N$ sufficiently large.
The CRT then gives
\begin{align*}
B/(\varphi^* t) = B / \bigcap_i \mathfrak{m}_i^{e_{p_i}} \cong \prod_i B/\mathfrak{m}_i^{e_{p_i}}.
\end{align*}
Since $\mathfrak{m}_i$ is a maximal ideal of $B$, the localisation map $B \to B_{\mathfrak{m}_i} = \mathcal{O}_{C, p_i}$ induces an isomorphism $B/\mathfrak{m}_i^{e_{p_i}} \cong \mathcal{O}_{C, p_i} / \mathfrak{m}_{p_i}^{e_{p_i}}$ (any element of $B$ outside $\mathfrak{m}_i$ becomes a unit in the quotient by $\mathfrak{m}_i^{e_{p_i}}$, so localisation is harmless).
Finally, $\mathcal{O}_{C, p_i} / \mathfrak{m}_{p_i}^{e_{p_i}}$ is the standard residue ring of a DVR modulo the $e$-th power of the maximal ideal: it has $k$-dimension $e_{p_i}$ (basis: $1, s_i, \ldots, s_i^{e_{p_i} - 1}$ with $s_i$ a uniformiser), because the filtration $\mathcal{O}_{C, p_i} \supset \mathfrak{m}_{p_i} \supset \mathfrak{m}_{p_i}^2 \supset \cdots \supset \mathfrak{m}_{p_i}^{e_{p_i}}$ has successive quotients $\mathfrak{m}_{p_i}^j / \mathfrak{m}_{p_i}^{j+1} \cong k$, each one-dimensional.
Summing the dimensions across the factors gives $\dim_k B/\mathfrak{m}_q B = \sum_i e_{p_i}$.
[/guided]
[/step]
[step:Equate the two computations of the rank]
Combining the two computations:
\begin{align*}
\deg \varphi = r = \dim_k (B / \mathfrak{m}_q B) = \sum_{i=1}^{k} e_{p_i} = \sum_{p \in \varphi^{-1}(q)} e_p.
\end{align*}
The first equality is Step 3, the second equality is the rank formula $B \otimes_A k \cong k^{\oplus r}$, and the third is Step 4. This completes the proof.
[/step]