Algorithm Search⌘K

Proofs: Scaled remainder tree

These are the human proofs of Scaled remainder tree, at full length.

The scaled remainder tree returns every remainder exactly full

This proof shows: correctness, termination.

Let x≥0x \ge 0 and m1,…,mk≥1m_1, \ldots, m_k \ge 1. In the product tree every internal node vv has two children and QvQ_v is the product of the moduli at the leaves below it; let dd be its height. Write λ(Q)\lambda(Q) for the binary length of Q≥1Q \ge 1, so 2λ(Q)−1≤Q<2λ(Q)2^{\lambda(Q) - 1} \le Q < 2^{\lambda(Q)}. Let G≥1G \ge 1 with 2G≥d+12^G \ge d + 1 and pv=λ(Qv)+Gp_v = \lambda(Q_v) + G. For a real zz write {z}=z−⌊z⌋∈[0,1)\{z\} = z - \lfloor z \rfloor \in [0, 1).

Theorem. SCALED-REMAINDERS terminates and returns x mod mix \bmod m_i at the leaf of mim_i, for every ii.

Termination. The tree has 2k−12k - 1 nodes, line L3 runs once, line L5 once per node other than the root, and line L6 once per leaf; each is a finite computation on integers.

1. Precisions decrease. If cc is a child of vv with sibling ss, then Qv=QcQs≥QcQ_v = Q_c Q_s \ge Q_c, so λ(Qc)≤λ(Qv)\lambda(Q_c) \le \lambda(Q_v) and pc≤pvp_c \le p_v; the shift in line L5 is by a nonnegative amount.

2. The error invariant. Let yv={x/Qv}y_v = \{x / Q_v\}. For every node vv there is a real δv\delta_v with

yv≡Yv2pv+δv(mod1),0≤Qvδv<(depth(v)+1) 2−G.y_v \equiv \frac{Y_v}{2^{p_v}} + \delta_v \pmod 1, \qquad 0 \le Q_v \delta_v < (\mathrm{depth}(v) + 1) \, 2^{-G} .

Proof. By induction on the depth. At the root, x/P≡(x mod P)/P(mod1)x / P \equiv (x \bmod P)/P \pmod 1, and line L3 sets Y=⌊(x mod P)2p/P⌋Y = \lfloor (x \bmod P) 2^{p} / P \rfloor, so δroot=(x mod P)/P−Y/2p∈[0,2−p)\delta_{\text{root}} = (x \bmod P)/P - Y/2^{p} \in [0, 2^{-p}), and Pδroot<P 2−λ(P)−G<2−GP \delta_{\text{root}} < P \, 2^{-\lambda(P) - G} < 2^{-G}.

Let cc be a child of vv with sibling ss. Since x/Qc=Qs⋅x/Qvx/Q_c = Q_s \cdot x/Q_v and QsQ_s is an integer, yc≡Qsyv(mod1)y_c \equiv Q_s y_v \pmod 1, and by the hypothesis

yc≡QsYv2pv+Qsδv(mod1).y_c \equiv \frac{Q_s Y_v}{2^{p_v}} + Q_s \delta_v \pmod 1 .

Let Z=(QsYv) mod 2pvZ = (Q_s Y_v) \bmod 2^{p_v}; then QsYv/2pv≡Z/2pv(mod1)Q_s Y_v / 2^{p_v} \equiv Z / 2^{p_v} \pmod 1. Line L5 sets Yc=⌊Z/2pv−pc⌋Y_c = \lfloor Z / 2^{p_v - p_c} \rfloor, so Z/2pv=Yc/2pc+τZ / 2^{p_v} = Y_c / 2^{p_c} + \tau with 0≤τ<2−pc0 \le \tau < 2^{-p_c}. Hence yc≡Yc/2pc+δcy_c \equiv Y_c / 2^{p_c} + \delta_c with δc=τ+Qsδv≥0\delta_c = \tau + Q_s \delta_v \ge 0, and

Qcδc=Qcτ+Qvδv<Qc2−λ(Qc)−G+(depth(v)+1)2−G<(depth(v)+2)2−G,Q_c \delta_c = Q_c \tau + Q_v \delta_v < Q_c 2^{-\lambda(Q_c) - G} + (\mathrm{depth}(v) + 1) 2^{-G} < (\mathrm{depth}(v) + 2) 2^{-G},

using QcQs=QvQ_c Q_s = Q_v and Qc<2λ(Qc)Q_c < 2^{\lambda(Q_c)}. Since depth(c)=depth(v)+1\mathrm{depth}(c) = \mathrm{depth}(v) + 1, the invariant holds at cc. □\square

3. The leaves. Let a leaf have modulus mm, value YY and precision pp, at depth at most dd, and let r=x mod mr = x \bmod m. Then y={x/m}=r/my = \{x/m\} = r/m, and by part 2, r/m=Y/2p+δ+jr/m = Y/2^p + \delta + j for an integer jj, with 0≤mδ<(d+1)2−G≤10 \le m \delta < (d + 1) 2^{-G} \le 1. Multiplying by mm,

mY2p=r−mj−mδ,0≤mδ<1.\frac{mY}{2^p} = r - mj - m\delta, \qquad 0 \le m \delta < 1 .

Since r−mjr - mj is an integer, ⌈mY/2p⌉=r−mj\lceil mY / 2^p \rceil = r - mj, and reducing modulo mm gives rr, because 0≤r<m0 \le r < m. So line L6 returns x mod mx \bmod m at every leaf. ■\blacksquare

Back to Scaled remainder tree