1 unstable release

new 0.1.0 Feb 15, 2025

#866 in Math

Download history 139/week @ 2025-02-11

139 downloads per month
Used in rsa_heapless

Apache-2.0

72KB
2K SLoC

Modular math functions

Yet another mod math implementation, but written for traits. All functions are free functions that are constrainted by core::ops:: and num_traits:: traits.

Implements:

  • Unsigned modular addition and subtraction
  • Unsigned modular multiplication
  • Unsigned modular exponentiation
  • Unsigned modular inverse

The code isn't intended to be fast or efficient, just as generic as possible to work with multiple implementations.

Note: While const traits are not yet stable and commonplace, this cannot be verify efficient. In almost all real world code you'll want to direcyl use crates that implement big integers with const functions.

Tested with

Tested with num-bigint, crypto-bigint, bnum, ibig and fixed-bigint crates.


lib.rs:

Modular math implemented with traits.

This crate provides modular arithmetic implemented not for any particular type, but for any type that implements minimal set of core::ops:: and num_traits:: traits.

All provided functions are simply free functions.

There are three verions of each: basic that has least amount of constraints, but requires Copy to be implemented for the type. constrained requires Clone. strict requires neither, but has most other constaints to be able to operate with references and Overflowing arithmetic. Tested with builtin integers and num-bigint, crypto-bigint, bnum, ibig and fixed-bigint crates. basic versions of functions wont work with num-bigint and ibig as both require heap allocation.

Dependencies

~150KB