Posts  / #POST-214283
REDDIT

Is a 10x speedup over GMP for 2-adic inversion unusual? Looking for context.

M
Dec 11, 2025 · 22:20

Hi,

Go easy on me because I'm new to Reddit.

I've been experimenting with specialized 2-adic modular inversion (computing d^(-1) mod 2^n for odd integers) using fixed-width big (unsigned) integers.

I'm seeing significantly better performance than I expected, and I'd appreciate some context from people who know the landscape better than I do.

On x86-64 (old notebook) using gcc I'm getting ~180ns per 256-bit inverse and ~470ns per 512-bit inverse.

GMP mpz_invert is taking ~1900ns at 256-bit and ~3400ns at 512-bit.

Results are verified by comparing against GMP for many random odd inputs.

This isn't general modular inverse. It's just the special case modulo 2^n using fixed width arithmetic.

I'm still cleaning up the implementation and not ready to share details yet, but before I keep going, I wanted to ask if GMP is supposed to have a highly optimized path for 2-adic inversion at these sizes? Maybe it's just not needed enough?

If you know about mpn-level routines, I'm wondering if GMP is competitive in this domain or if a 10x speedup isn't surprising.

Thanks in advance, and apologies if this is too early for a detailed discussion. I'm just trying to get a sense if this is new or a case where GMP hasn't optimized.

Edited to add:

"2-adic" here just means the numerator is a power of two. That's exactly the situation for Barrett inversion, so the restriction isn't a practical limitation.

Edited to add again:

This thread has been very helpful. My plan to complete the implementation is now concrete, and I expect to be able to share further details soon.