Zp

Struct Zp 

Source
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

Source

pub fn new(value: u64, modulus: u64) -> Self

Create a new finite field element

The value is automatically reduced modulo p.

§Arguments
  • value - The integer value (will be reduced mod p)
  • modulus - The prime modulus p
§Examples
use mathhook_core::algebra::Zp;

let a = Zp::new(10, 7);
assert_eq!(a.value(), 3);  // 10 mod 7 = 3
Source

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 value
  • modulus - 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)
Source

pub fn value(&self) -> u64

Get the underlying value

Source

pub fn modulus(&self) -> u64

Get the modulus

Source

pub fn is_zero(&self) -> bool

Check if this element is zero

Source

pub fn is_one(&self) -> bool

Check if this element is one

Source

pub fn zero(modulus: u64) -> Self

Get the zero element of this field

Source

pub fn one(modulus: u64) -> Self

Get the multiplicative identity (one) of this field

Source

pub fn negate(&self) -> Self

Compute the additive inverse (-a mod p)

Source

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)
Source

pub fn pow(&self, exp: u64) -> Self

Compute a^n mod p using binary exponentiation

This is O(log n) multiplications.

§Arguments
  • exp - The exponent
§Examples
use mathhook_core::algebra::Zp;

let a = Zp::new(2, 7);
let result = a.pow(3);
assert_eq!(result.value(), 1);  // 2^3 = 8 ≡ 1 (mod 7)
Source

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§

Source§

impl Add for Zp

Source§

type Output = Zp

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl Clone for Zp

Source§

fn clone(&self) -> Zp

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Zp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Zp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div for Zp

Source§

type Output = Zp

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self

Performs the / operation. Read more
Source§

impl Hash for Zp

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul for Zp

Source§

type Output = Zp

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
Source§

impl Neg for Zp

Source§

type Output = Zp

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Zp

Source§

fn eq(&self, other: &Zp) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub for Zp

Source§

type Output = Zp

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl Copy for Zp

Source§

impl Eq for Zp

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.