mod2k

Crates.iomod2k
lib.rsmod2k
version0.1.1
created_at2025-12-22 06:00:07.268173+00
updated_at2025-12-22 20:31:51.519238+00
descriptionFast arithmetic modulo `2^k`, `2^k - 1`, and `2^k - d`.
homepage
repositoryhttps://github.com/purplesyringa/mod2k
max_upload_size
id1999142
size106,319
Alisa Sireneva (purplesyringa)

documentation

README

mod2k

License Version docs.rs Tests

A #![no_std] Rust crate for fast arithmetic modulo 2^k, 2^k - 1, and 2^k - d.

Modular arithmetic is useful for fast hashing, verification of polynomial operations, and as a ring balancing the cost of division with the cost of other operations. Different moduli make different quality vs performance tradeoffs. This crate provides a uniform interface for fast implementations of multiple moduli, allowing you to tune the balance without rewriting your code to adopt faster algorithms:

  • "Big" primes: 2^8 - 5, 2^16 - 15, 2^32 - 5, 2^64 - 59. Slowest (~12 insn per multiplication, ~7 insn per addition, slow shifts and inversions), excellent quality.
  • Primes: 2^7 - 1, 2^13 - 1, 2^31 - 1, 2^61 - 1. Medium performance (~10 insn per multiplication, ~5 insn per addition), excellent quality (but slightly fewer bits of precision compared to "big" primes).
  • "Fast": 2^8 - 1, 2^16 - 1, 2^32 - 1, 2^64 - 1. Great performance (~4 insn per multiplication, ~2 insn per addition), medium quality (the moduli have relatively big prime factors).
  • Powers of two: 2^8, 2^16, 2^32, 2^64. Fastest (1 insn per multiplication and addition), but have seed-independent collisions and do not support division by 2.

mod2k is estimated to be ~2x faster than general-purpose modular arithmetic libraries on average, and more specific performance information is available.

Check the docs for more information.

Commit count: 0

cargo fmt