MyNixOS website logo
Description

Fast type-safe modular arithmetic.

Modular arithmetic, promoting moduli to the type level, with an emphasis on performance. Originally part of the arithmoi package.

mod Hackage Stackage LTS Stackage Nightly

Modular arithmetic, promoting moduli to the type level, with an emphasis on performance. Originally a part of the arithmoi package.

> :set -XDataKinds
> 4 + 5 :: Mod 7
2
> 4 - 5 :: Mod 7
6
> 4 * 5 :: Mod 7
6
> 4 / 5 :: Mod 7
5
> 4 ^ 5 :: Mod 7
2

Competitors

There are other Haskell packages, employing the very same idea of moduli on the type level, namely modular, modular-arithmetic and finite-field. One can also use finite-typelits, which covers some elementary modular arithmetic as well. Unfortunately, all of them fall behind in terms of performance. Here is a brief comparison:

Disciplinemodmodularmodular-arithmeticfinite-typelitsfinite-field
AdditionFastSlowSlowSlowSlow
Small (*)FastSlowSlowSlowSlow
InversionFastN/ASlowN/ASlow
PowerFastSlowSlowSlowSlow
OverflowsSafeSafeUnsafeSafeSafe
  • Addition. All competing implementations of the modular addition involve divisions, while mod completely avoids this costly operation. This makes a difference even for small numbers; e. g., sum [1..10^7] becomes 5x faster. For larger integers the speed up is even more significant, because the computational complexity of division is not linear.

  • Small (*). When a modulus fits in a machine word (which is quite a common case on 64-bit architectures), mod implements the modular multiplication as a couple of CPU instructions and neither allocates intermediate arbitrary-precision values, nor calls libgmp at all. For computations like product [1..10^7] this gives a 3x boost to performance in comparison to other libraries.

  • Inversion. This package relies on libgmp for modular inversions. Even for small arguments it is about 5x faster than the native implementation of modular inversion in modular-arithmetic.

  • Power. This package relies on libgmp for modular exponentiation. Even for small arguments it is about 2x faster than competitors.

  • Overflows. At first glance modular-arithmetic is more flexible than mod, because it allows to specify the underlying representation of a modular residue, e. g., Mod Integer 100, Mod Int 100, Mod Word8 100. We argue that this is a dangerous freedom, vulnerable to overflows. For instance, 20 ^ 2 :: Mod Word8 100 returns 44 instead of the expected 0. Even less expected is that 50 :: Mod Word8 300 appears to be 6 (remember that type-level numbers are always Natural).

What is the difference between mod and finite-typelits?

mod is specifically designed to represent modular residues for mathematical applications (wrapping-around finite numbers) and provides modular inversion and exponentiation.

The main focus of finite-typelits is on non-wrapping-around finite numbers, like indices of arrays in vector-sized. It features a Num instance only for the sake of overloading numeric literals. There is no lawful way to define Num except modular arithmetic, but from finite-typelits' viewpoint this is a by-product.

Citius, altius, fortius!

If you are looking for an ultimate performance and your moduli fit into Word, try Data.Mod.Word, which is a drop-in replacement of Data.Mod, offering better performance and much less allocations.

Benchmarks

Here are some relative benchmarks (less is better), which can be reproduced by running cabal bench.

DisciplineData.Mod.WordData.Modmodularmodular-arithmeticfinite-typelitsfinite-field
Sum0.44x1x16.6x8.9x14.7x14.2x
Product0.95x1x7.8x4.5x7.0x7.0x
Inversion0.54x1xN/A3.2xN/A1.8x
Power0.29x1x2.0x1.2x1.4x1.5x

What's next?

This package was cut out of arithmoi to provide modular arithmetic with a light dependency footprint. This goal certainly limits the scope of the API to the bare minimum. If you need more advanced tools (the Chinese remainder theorem, cyclic groups, modular equations, etc.) please refer to the Math.NumberTheory.Moduli module.

Metadata

Version

0.2.0.1

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows