pub struct Zp { /* private fields */ }Expand description
Element of finite field Z_p (integers modulo prime p)
This type provides efficient modular arithmetic with automatic normalization.
All operations maintain the invariant that value < modulus.
§Memory Layout
The struct is 16 bytes (two u64), fitting well in registers and cache lines.
§Thread Safety
Zp is Copy, Send, and Sync, making it safe for parallel computation.
§Examples
use mathhook_core::algebra::Zp;
// Create field elements mod 7
let a = Zp::new(3, 7);
let b = Zp::new(5, 7);
// Arithmetic
let sum = a + b; // 3 + 5 = 8 ≡ 1 (mod 7)
assert_eq!(sum.value(), 1);
let product = a * b; // 3 * 5 = 15 ≡ 1 (mod 7)
assert_eq!(product.value(), 1);Implementations§
Source§impl Zp
impl Zp
Sourcepub fn from_signed(value: i64, modulus: u64) -> Self
pub fn from_signed(value: i64, modulus: u64) -> Self
Create a new finite field element from a signed integer
Handles negative values correctly using symmetric representation.
§Arguments
value- The signed integer valuemodulus- The prime modulus p
§Examples
use mathhook_core::algebra::Zp;
let a = Zp::from_signed(-3, 7);
assert_eq!(a.value(), 4); // -3 ≡ 4 (mod 7)Sourcepub fn inverse(&self) -> FiniteFieldResult<Self>
pub fn inverse(&self) -> FiniteFieldResult<Self>
Compute the multiplicative inverse using extended Euclidean algorithm
Uses Fermat’s little theorem: a^(-1) ≡ a^(p-2) (mod p) for prime p. However, extended GCD is faster for single inversions.
§Returns
Ok(inverse) if the element is non-zero, Err otherwise.
§Examples
use mathhook_core::algebra::Zp;
let a = Zp::new(3, 7);
let inv = a.inverse().unwrap();
assert_eq!((a * inv).value(), 1); // 3 * 5 = 15 ≡ 1 (mod 7)Sourcepub fn to_symmetric(&self) -> i64
pub fn to_symmetric(&self) -> i64
Convert to signed representation in [-p/2, p/2]
This is the symmetric representation, useful for CRT reconstruction.
§Examples
use mathhook_core::algebra::Zp;
let a = Zp::new(6, 7);
assert_eq!(a.to_symmetric(), -1); // 6 ≡ -1 (mod 7)Trait Implementations§
impl Copy for Zp
impl Eq for Zp
impl StructuralPartialEq for Zp
Auto Trait Implementations§
impl Freeze for Zp
impl RefUnwindSafe for Zp
impl Send for Zp
impl Sync for Zp
impl Unpin for Zp
impl UnwindSafe for Zp
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more