\begin{lemma}[Exact decimal digit growth under a left shift]
Let a decimal sequence contain $n\le768$ stored decimal digits, let $I$ be
their base-ten fold, and let $0\le k\le60$.  Suppose $h$ satisfies
\[
                  10^{h-1}<2^k<10^h.
\]
Then the extracted decimal left-shift digit-count routine returns normally,
and its result is
\[
 \begin{cases}
 h,&10^{n+h-1}\le I2^k,\\
 h-1,&I2^k<10^{n+h-1}.
 \end{cases}
\]
The Lean statement records the same choice by an \texttt{if} expression.
\end{lemma}
\begin{proof}
The shift is at most 60, so masking it with 63 in
\noderef{num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift}
does not change it.  Apply
\noderef{DecimalLeftShiftTableCertificate} at the finite index $k$.  For
$k=0$ its table field is zero and its comparison slice is empty; the routine
returns zero.  Since $2^0=1$ and an $n$-digit integer satisfies $I<10^n$, the
displayed test is false and its right side is $h-1=0$.

Assume henceforth $k>0$.  Write $d_0\ldots d_{\ell-1}$ for the certified
table slice.  The certificate gives
\[
 D:=\sum_{i<\ell}d_i10^{\ell-i-1}=5^k,\qquad
 h+\ell=k+1,                                           \tag{1}
\]
and $d_0$ is nonzero.  All delimiter calculations are ordered and within the
1308-cell table, so both table indexing operations, the slice construction,
the iterator construction, and subtraction of its endpoints return normally.
The encoded high field is $h$.

The loop compares the sequence digits with $d_0,\ldots,d_{\ell-1}$ in order.
At the first unequal position it returns $h$ when the sequence digit is larger
and $h-1$ when it is smaller.  If all available sequence digits agree and the
table slice still has a digit, the branch \texttt{$i\ge n$} returns $h-1$.
Thus the loop is exactly a lexicographic comparison of the first $\ell$
digits of the sequence after padding the sequence on the right with zeroes.
No array access fails: an input digit is read only in the branch $i<n$, and
$n\le768$ is the physical array length.

We now translate that comparison into the arithmetic test in the statement.
If $n\ge\ell$, write
\[
 I=Q10^{n-\ell}+S,\qquad 0\le S<10^{n-\ell}.
\]
The compared prefix is the $\ell$-digit integer $Q$, so the routine selects
$h$ exactly when $Q\ge5^k$.  Equivalently,
\[
 I10^\ell\ge5^k10^n.                                  \tag{2}
\]
Indeed $Q<5^k$ makes the left side strictly smaller after bounding $S$, while
$Q\ge5^k$ makes it at least the right side.

If $n<\ell$, the comparison is not automatically smaller.  The available
word may differ upward before it is exhausted.  Its zero-padded comparison
integer is exactly $I10^{\ell-n}$, so this branch selects $h$ precisely when
\[
                  I10^{\ell-n}\ge5^k.                  \tag{3}
\]
This includes both possibilities omitted by the former argument: an earlier
larger digit makes (3) true, whereas equality through all $n$ positions and
exhaustion makes it false because $5^k$ has nonzero leading digit.  For
example, $k=3$, $n=2$, and $I=99$ compares $990$ with $125$ and therefore
selects the larger growth.

Multiplying (3) by $10^n$, or using (2) directly, gives in both cases
\[
                       I10^\ell\ge5^k10^n.              \tag{4}
\]
By (1), $\ell=k+1-h$.  Multiply the desired threshold
$10^{n+h-1}\le I2^k$ by the positive integer $5^k$ and use
$2^k5^k=10^k$; after cancelling the common positive power of ten, the result
is exactly (4).  Therefore the loop returns $h$ exactly under the statement's
test and $h-1$ otherwise, as required.
\end{proof}
