Our num-bigint gcd cost was 64x too low
We metered big-integer work to stop denial of service, then read num-bigint's gcd source and found we had charged about 64 times too little for it.
Contents
We wanted a hard limit on how much work a parser can do on untrusted numbers. Our rational
number type charges a budget for every big-integer operation before it runs, and refuses with
ResourceExhausted once the budget is gone. A limit like that is only as good as its price
list. When we read the source of num-bigint 0.4.6 to check one line of it, the price for gcd
turned out to be wrong by roughly a factor of 64. This post is about how we found it, what the
fix charges, and the other unmetered work we found on the way.
What the charge looked like
Normalizing a fraction divides numerator and denominator by their gcd. Our code charged the
work as the square of the word counts: (wn + wd)^2, with wn and wd the number of 64-bit
words in each side. That is a natural guess. Schoolbook division and multiplication are
quadratic in the word count, and it is easy to assume gcd is too.
What the library does
BigUint::gcd in this version uses Stein's binary algorithm. It does not divide. Each round
subtracts, then shifts out factors of two. The number of rounds is bounded by the bit lengths,
at most bits(n) + bits(d) + 1. Each round touches every word of the operands.
That is bits times words, not words times words. A word has 64 bits, so for the same input the real work can be up to 64 times the charge. For small numbers nobody notices. For an attacker who picks inputs to hit the worst case, the budget would have allowed 64 times more CPU than we said it would.
The new charge is built from the bound itself: (bn + bd + 1) * 4 * max(words), plus the cost
of the two exact divisions that follow. The derivation sits next to the code as a short proof.
Checking the charge against the real loop
A formula in a comment is a claim. To test it we wrote an instrumented copy of the gcd loop
that counts what it really does, and a test in tests/resource_bounds.rs that checks, for
each operation, that the amount charged is at least the work measured. Other cases in that
file cover cancellation and extreme exponents.
One limit we should state: the multiplication and division prices are modeled, not proven. We derived them from how the library works and tested them against measurement, which is good evidence but not a proof.
The other things that cost nothing
Once we were pricing things from the source and not from guesses, the audit turned up work we had not metered at all:
- the re-format into canonical form after each operation
- clones of the operands
- shifts
- decimal formatting
- building the powers
10^eand5^b
Each of these is linear in the number size or worse. A chain of cheap-looking operations could run them over and over while the budget barely moved. They are metered now.
A gcd we did not need
The audit also removed work. On the decimal path the denominator has the form 2^a * 5^b,
and in the cases we checked the numerator is already coprime to it, so a gcd there changes
nothing. We dropped that call. The fraction code also skips gcd when the denominator is 1 or
the numerator is 0, because gcd(n, 1) = 1 and the result is known without running anything.
Fewer operations is both faster and easier to price.
Why under-charging is the dangerous direction
A price that is too high only makes the limit trip early, so honest inputs get refused. That is annoying, and you notice it quickly because users complain. A price that is too low fails silently. The budget says 10 percent used while the CPU is already busy for far longer than you allowed, and nothing in the normal test suite shows it, because normal inputs are nowhere near the worst case. This is why we wrote the test as "charge is at least the measured cost" and not as "charge equals cost". The one-sided check is the one that matters.
How to price a resource limit
- Charge before you compute. The check has to come first. A limit that notices after the work is done only reports the damage.
- Read the dependency. The complexity you remember from a textbook may not be the one the crate uses. Our assumption of quadratic cost was the textbook answer for a different algorithm.
- Test charge against measurement. For every operation, assert that charge is at least the observed cost. This is the test that would have failed on the original gcd line.
- List what is not charged. Formatting, cloning and allocation are work too.
- Pin the version. A price taken from one release of a library can go stale in the next.
Ours is tied to
0.4.6, and an update should send you back to the source.
Result
The gcd price now matches the loop. The test suite checks the charge for every operation, cancellation, and extreme exponents. The finding came from reading a short piece of a dependency, which is a cheap review for a limit whose whole job is to be trustworthy.