A decades-old bug in Knuth's long division
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
A decades-old bug in Knuth’s long division
A long division story 13 Aug, 2026 How I received a theorem in Knuth’s “The Art of Computer Programming” after finding a decades-old bug in Algorithm D (+ a “bug” in llvm)
关于高纳德(Knuth)长除法中存在数十年之久的漏洞的故事,2026年8月13日。在发现高纳德《计算机程序设计艺术》中算法 D 的一个数十年之久的漏洞(以及 LLVM 中的一个“漏洞”)后,我如何获得了一个定理。
I was implementing Algorithm D, the well-known long division algorithm from Knuth’s “The Art of Computer Programming”, and I stumbled upon an issue that I couldn’t let go. The correctness of the algorithm relied on Theorem B, and its proof bugged me. It felt unnatural, it took a very convoluted path to proving a simple statement, and it isolated a special case which was not a corner case and that seemed unrelated to the problem at hand.
我当时正在实现算法 D,这是高纳德《计算机程序设计艺术》中著名的长除法算法,我偶然发现了一个让我无法释怀的问题。该算法的正确性依赖于定理 B,但其证明过程让我感到困扰。它显得很不自然,用极其复杂的路径去证明一个简单的陈述,并且孤立出了一个并非边界情况的特殊情况,这似乎与手头的问题无关。
There was something odd about it, so I tried to prove the theorem myself, and I failed. However, the failure handed me a counterexample to Algorithm D that had passed as correct for decades, and with it a theorem on the correctness of the algorithm carrying my name.
这其中有些古怪,所以我尝试自己证明这个定理,但失败了。然而,这次失败却让我得到了算法 D 的一个反例,这个算法几十年来一直被认为是正确的,随之而来的还有一个以我名字命名的关于该算法正确性的定理。
In this post I’ll give some background, then cover long division from scratch for those who want it, share my thoughts on how the bug came to be and how it stayed hidden so long, and finish with a preview of more modern ways to implement long division. In the process of writing this blog I also found a “bug” in this algorithm’s implementation in llvm, and I will expand on that too.
在这篇文章中,我将提供一些背景信息,然后为有需要的人从零开始介绍长除法,分享我对这个漏洞是如何产生以及为何隐藏如此之久的看法,最后预览一些实现长除法的更现代的方法。在撰写此博客的过程中,我还发现了 LLVM 中该算法实现的一个“漏洞”,我也会对此进行详细说明。
If you’re only interested in the bug you can jump directly to The bug. Contents How I got here Long division from scratch Multiprecision integers in hardware Reducing long division to medium division Reducing medium division to small division Normalisation The bug How did it stay hidden for decades Can it be exploited The llvm “bug” AI didn’t find it The check A little trit more Stronger bounds Doubling the quotient limbs Division by a constant Discussions
如果你只对漏洞感兴趣,可以直接跳转到“漏洞”部分。目录:我如何走到这一步、从零开始的长除法、硬件中的多精度整数、将长除法简化为中除法、将中除法简化为小除法、归一化、漏洞、它为何隐藏了数十年、它能被利用吗、LLVM“漏洞”、AI 未能发现它、检查、多一点点、更强的界限、商位加倍、除以常数、讨论。
How I got here Preparing for an interview, I decided to do a small project: build a little library for arithmetic over prime fields. This meant fixed-size multiprecision integers, arithmetic operations, some field operations, constant-time, constant memory access, all in all a starting point for modern cryptographic protocols.
我如何走到这一步:在准备面试时,我决定做一个小项目:构建一个用于素域算术的小型库。这意味着固定大小的多精度整数、算术运算、一些域运算、恒定时间、恒定内存访问,总而言之,这是现代加密协议的起点。
As I worked on implementing it, the process turned into a game with one rule: avoid division at all costs. You can get almost all the way there, and to the best of my knowledge cryptographic libraries never execute the division instruction at runtime. Whenever a divide is needed, it is generally substituted by a multiplication followed by a bit of shuffling.
在实现过程中,这个过程变成了一个只有一条规则的游戏:不惜一切代价避免除法。你几乎可以做到这一点,据我所知,加密库从不在运行时执行除法指令。每当需要除法时,通常会用乘法加上一些位移操作来代替。
Why do we go so far out of our way to avoid division? Well, multiplication is very simple, in fact it can be thought of as an axiom of the natural numbers. Division is far more complicated. Firstly, it’s not everywhere defined: we can’t divide by zero. But we can’t divide 5 by 2 either!
我们为什么要费尽心思去避免除法呢?嗯,乘法非常简单,事实上它可以被认为是自然数的一个公理。除法要复杂得多。首先,它并非在任何地方都有定义:我们不能除以零。但我们也无法将 5 除以 2!
What we actually have is “division with remainder”, a more complicated operation which returns two answers: the quotient and the remainder, the smallest non-negative difference between the dividend and a multiple of the divisor. The issue hides in what “smallest” exactly means, why we choose this particular definition, and why the notion of size enters the picture at all.
我们实际上拥有的是“带余除法”,这是一种更复杂的操作,它返回两个答案:商和余数,即被除数与除数倍数之间最小的非负差值。问题隐藏在“最小”的确切含义、我们为何选择这种特定定义,以及为什么大小的概念会出现在这里。
One may choose differently, say zero-centred remainders. But we could go further and choose a different size function, which gives rise to a different division algorithm altogether. With all that in mind it’s no wonder that the theoretical complication transfers into practice. A multiply instruction costs a cycle or two on modern machines and fully pipelines, while a divide can cost up to twenty cycles and usually doesn’t pipeline.
人们可以选择不同的方式,比如以零为中心的余数。但我们可以更进一步,选择不同的度量函数,这会产生完全不同的除法算法。考虑到这一切,理论上的复杂性转化为实践也就不足为奇了。在现代机器上,乘法指令只需一两个周期且完全流水线化,而除法可能需要多达二十个周期,且通常无法流水线化。
Eventually the only gap left in the multiprecision implementation was the multiprecision division algorithm. So I did the obvious thing and sat down to implement long division, and for reference I used Donald Knuth’s “The Art of Computer Programming” Vol. II, Third Edition, Algorithm 4.3.1D. Let’s look at how long division actually works.
最终,多精度实现中唯一剩下的空白就是多精度除法算法。所以我做了显而易见的事情,坐下来实现长除法,并参考了唐纳德·高纳德的《计算机程序设计艺术》第二卷第三版中的算法 4.3.1D。让我们看看长除法实际上是如何工作的。
Long division from scratch Multiprecision integers in hardware Cryptographic integers run to hundreds or thousands of bits, well past a single register, so we store them in base b, one limb per machine word: x=(xn−1,…,x0)b=∑i=0n−1xibi,0≤xi<b.
从零开始的长除法:硬件中的多精度整数。加密整数运行在数百或数千位,远远超过单个寄存器,因此我们以基数 b 存储它们,每个机器字存储一个“肢”(limb):x=(xn−1,…,x0)b=∑i=0n−1xibi,0≤xi<b。
The main building blocks of multiprecision arithmetic algorithms are the four primitive instructions that operate over single/double limbs: addc: x, y -> s, carry # s = (x+y) mod b, carry = 1 iff overflow subc: x, y -> d, borrow # d = (x−y) mod b, borrow = 1 iff underflow mul: x, y -> (hi, lo) # x·y = hi·b + lo div: (hi, lo), y -> (q1, q0), r # hi·b + lo = q·y + r, 0 ≤ r < y, q = q1·b + q0
多精度算术算法的主要构建块是四个在单/双“肢”上操作的原始指令:addc(加法进位)、subc(减法借位)、mul(乘法)、div(除法)。
The first three are unremarkable, but the division stands as the odd one out. While multiplying two single-limb multiplicands always returns a two-limb product, the quotient of a two-limb dividend over a one-limb divisor does not always fit in a single limb, so we use a two-limb quotient. In addition to that, the remainder shows up as a necessary byproduct. And in division by zero we assume undefined behaviour, i.e. that q and r may take any value, although some architectures treat this case differently as we will see later.
前三个指令平淡无奇,但除法却显得格格不入。虽然将两个单“肢”乘数相乘总是返回一个双“肢”乘积,但双“肢”被除数除以单“肢”除数的商并不总是能放入单个“肢”中,因此我们使用双“肢”商。此外,余数作为必要的副产品出现。在除以零的情况下,我们假设其行为未定义,即 q 和 r 可以取任何值,尽管正如我们稍后将看到的,某些架构对这种情况的处理方式不同。
Reducing long division to medium division Our task is to divide a multiprecision integer u by v, that is, find integers q and r such that u=q·v+r and 0≤r<v. Assume without loss of generality that the divisor v is an n-limb integer with non-zero top limb vn−1, and pad the dividend u with leading zeros until it has strictly more limbs than v; write n+m+1 for the number of limbs of u.
将长除法简化为中除法:我们的任务是将多精度整数 u 除以 v,即找到整数 q 和 r,使得 u=q·v+r 且 0≤r<v。在不失一般性的前提下,假设除数 v 是一个 n“肢”整数,且最高位肢 vn−1 不为零,并在被除数 u 前补零,直到其“肢”数严格多于 v;记 u 的“肢”数为 n+m+1。
We have another requirement which will prove natural in what follows: the highest n limbs of u, read as an n-limb integer, must be strictly smaller than v, (un+m,…,um+1)b<(vn−1,…,v0)b, equivalently ⌊ubm+1⌋<v. If that is not already the case, appending a further zero to u will guarantee it. This assumption pins down the size of the quotient to m+1 limbs: ⌊ubm+1⌋<v⟺u<vbm+1⟺q=⌊uv⌋<bm+1. With the padding in place every operand has a fixed shape.
我们还有另一个在后续证明中显得自然的要求:u 的最高 n 个“肢”(读取为一个 n“肢”整数)必须严格小于 v,即 (un+m,…,um+1)b<(vn−1,…,v0)b,等价于 ⌊ubm+1⌋<v。如果情况并非如此,在 u 前再补一个零即可保证这一点。这一假设将商的大小固定为 m+1 个“肢”:⌊ubm+1⌋<v⟺u<vbm+1⟺q=⌊uv⌋<bm+1。补零后,每个操作数都有固定的形状。