alga2 0.1.0

A modern abstract-algebra hierarchy for Rust — the successor to alga, powered by batch-impl
Documentation
//! The integers modulo `P` — a finite field when `P` is prime.
//!
//! `ModN<P>` is the canonical inhabitant of the integral-domain ladder
//! (`Z/pZ` is a field, hence an integral domain / UFD / PID — unlike the
//! crate's `Z/2^N` numerics, which have zero divisors such as
//! `16 · 16 ≡ 0 (mod 256)`) and of `FiniteField` (characteristic = order =
//! P). The tower impls live in `crate::impls::modn` (generated by
//! batch-impl); field-level structures assume `P` prime (the multiplicative
//! inverse panics for a non-invertible residue, which exists exactly when
//! `P` is composite).

use core::fmt;

/// The integers modulo `P`, represented canonically in `0..P`.
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ModN<const P: usize>(usize);

impl<const P: usize> ModN<P> {
    /// The canonical representative of `n` in `0..P`.
    pub const fn new(n: usize) -> Self {
        Self(n % P)
    }

    /// The residue `0..P`.
    pub const fn value(&self) -> usize {
        self.0
    }
}

impl<const P: usize> fmt::Debug for ModN<P> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ModN<{}>({})", P, self.0)
    }
}