Prime

Trait Prime 

Source
pub trait Prime:
    Debug
    + Clone
    + Copy
    + Display
    + Hash
    + PartialEq
    + Eq
    + PartialEq<u32>
    + PartialOrd<u32>
    + Add<u32, Output = u32>
    + Sub<u32, Output = u32>
    + Mul<u32, Output = u32>
    + Div<u32, Output = u32>
    + Rem<u32, Output = u32>
    + Shl<u32, Output = u32>
    + Shr<u32, Output = u32>
    + Serialize
    + for<'de> Deserialize<'de>
    + MaybeArbitrary<Option<NonZeroU32>>
    + 'static {
    // Required methods
    fn as_i32(self) -> i32;
    fn to_dyn(self) -> ValidPrime;

    // Provided methods
    fn as_u32(self) -> u32 { ... }
    fn as_usize(self) -> usize { ... }
    fn sum(self, n1: u32, n2: u32) -> u32 { ... }
    fn product(self, n1: u32, n2: u32) -> u32 { ... }
    fn inverse(self, k: u32) -> u32 { ... }
    fn pow(self, exp: u32) -> u32 { ... }
    fn pow_mod(self, b: u32, e: u32) -> u32 { ... }
}
Expand description

A trait that represents a prime number. There are currently two kinds of structs that implement this trait: static primes and ValidPrime, the dynamic prime.

The methods in this trait take a self receiver so that the dynamic prime ValidPrime can implement it. We could also have a &self receiver, but since Prime is a supertrait of Copy, the two are equivalent. Using self might also be useful in the future if we ever want to play with autoref specialization.

The fact that e.g. P2::to_u32 is hardcoded to return 2 means that a condition like p.to_u32() == 2 (or even better, just p == 2) will reduce to true at compile time, and allow the compiler to eliminate an entire branch, while also leaving that check in for when the prime is chosen at runtime.

Required Methods§

Source

fn as_i32(self) -> i32

Source

fn to_dyn(self) -> ValidPrime

Provided Methods§

Source

fn as_u32(self) -> u32

Source

fn as_usize(self) -> usize

Source

fn sum(self, n1: u32, n2: u32) -> u32

Computes the sum mod p. This takes care of overflow.

Source

fn product(self, n1: u32, n2: u32) -> u32

Computes the product mod p. This takes care of overflow.

Source

fn inverse(self, k: u32) -> u32

Source

fn pow(self, exp: u32) -> u32

Source

fn pow_mod(self, b: u32, e: u32) -> u32

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§