Montgomery

Struct Montgomery 

Source
pub struct Montgomery<T> { /* private fields */ }
Expand description

Provides a method to perform Montgomery Reduction.

Montgomery::convert can convert an integer to a Montgomery representation, and Montgomery::reduce can restore a normal integer representation.
None of the other methods do the conversion internally.

use modutil::Montgomery;

const M: Montgomery<u8> = Montgomery::<u8>::new(17);

let a = M.convert(3);
assert_eq!(M.multiply(a, a), M.convert(9));
assert_eq!(M.pow(a, 3), M.convert(10));
assert_eq!(M.reduce(a), 3);

Implementations§

Source§

impl Montgomery<u8>

Source

pub const fn reduce(&self, val: u8) -> u8

Convert Montgomery representation to a normal integer representation.

Source

pub const fn multiply(&self, lhs: u8, rhs: u8) -> u8

Multiply lhs and rhs. lhs and rhs must be Montgomery representations.

Source

pub const fn convert(&self, val: u8) -> u8

Convert a normal integer representation to Montgomery representation.

To restore, you can use Montgomery::reduce.

§Examples
use modutil::Montgomery;

const M: Montgomery<u8> = Montgomery::<u8>::new(17);
assert_eq!(M.reduce(M.convert(3)), 3);
Source

pub const fn pow(&self, val: u8, exp: u8) -> u8

Calculate val^exp (mod modulo).

val must be Montgomery representation, but exp must be a normal integer representation.

Source

pub const fn new(modulo: u8) -> Self

Constructor.

§Panics
  • modulo must be odd.
Source

pub const fn zero(&self) -> u8

Return 0 in Montgomery representation.

Source

pub const fn one(&self) -> u8

Return 1 in Montgomery representation.

Source

pub const fn modulus(&self) -> u8

Return the modulus.

Source§

impl Montgomery<u16>

Source

pub const fn reduce(&self, val: u16) -> u16

Convert Montgomery representation to a normal integer representation.

Source

pub const fn multiply(&self, lhs: u16, rhs: u16) -> u16

Multiply lhs and rhs. lhs and rhs must be Montgomery representations.

Source

pub const fn convert(&self, val: u16) -> u16

Convert a normal integer representation to Montgomery representation.

To restore, you can use Montgomery::reduce.

§Examples
use modutil::Montgomery;

const M: Montgomery<u8> = Montgomery::<u8>::new(17);
assert_eq!(M.reduce(M.convert(3)), 3);
Source

pub const fn pow(&self, val: u16, exp: u16) -> u16

Calculate val^exp (mod modulo).

val must be Montgomery representation, but exp must be a normal integer representation.

Source

pub const fn new(modulo: u16) -> Self

Constructor.

§Panics
  • modulo must be odd.
Source

pub const fn zero(&self) -> u16

Return 0 in Montgomery representation.

Source

pub const fn one(&self) -> u16

Return 1 in Montgomery representation.

Source

pub const fn modulus(&self) -> u16

Return the modulus.

Source§

impl Montgomery<u32>

Source

pub const fn reduce(&self, val: u32) -> u32

Convert Montgomery representation to a normal integer representation.

Source

pub const fn multiply(&self, lhs: u32, rhs: u32) -> u32

Multiply lhs and rhs. lhs and rhs must be Montgomery representations.

Source

pub const fn convert(&self, val: u32) -> u32

Convert a normal integer representation to Montgomery representation.

To restore, you can use Montgomery::reduce.

§Examples
use modutil::Montgomery;

const M: Montgomery<u8> = Montgomery::<u8>::new(17);
assert_eq!(M.reduce(M.convert(3)), 3);
Source

pub const fn pow(&self, val: u32, exp: u32) -> u32

Calculate val^exp (mod modulo).

val must be Montgomery representation, but exp must be a normal integer representation.

Source

pub const fn new(modulo: u32) -> Self

Constructor.

§Panics
  • modulo must be odd.
Source

pub const fn zero(&self) -> u32

Return 0 in Montgomery representation.

Source

pub const fn one(&self) -> u32

Return 1 in Montgomery representation.

Source

pub const fn modulus(&self) -> u32

Return the modulus.

Source§

impl Montgomery<u64>

Source

pub const fn reduce(&self, val: u64) -> u64

Convert Montgomery representation to a normal integer representation.

Source

pub const fn multiply(&self, lhs: u64, rhs: u64) -> u64

Multiply lhs and rhs. lhs and rhs must be Montgomery representations.

Source

pub const fn convert(&self, val: u64) -> u64

Convert a normal integer representation to Montgomery representation.

To restore, you can use Montgomery::reduce.

§Examples
use modutil::Montgomery;

const M: Montgomery<u8> = Montgomery::<u8>::new(17);
assert_eq!(M.reduce(M.convert(3)), 3);
Source

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

Calculate val^exp (mod modulo).

val must be Montgomery representation, but exp must be a normal integer representation.

Source

pub const fn new(modulo: u64) -> Self

Constructor.

§Panics
  • modulo must be odd.
Source

pub const fn zero(&self) -> u64

Return 0 in Montgomery representation.

Source

pub const fn one(&self) -> u64

Return 1 in Montgomery representation.

Source

pub const fn modulus(&self) -> u64

Return the modulus.

Trait Implementations§

Source§

impl<T: Clone> Clone for Montgomery<T>

Source§

fn clone(&self) -> Montgomery<T>

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<T: Debug> Debug for Montgomery<T>

Source§

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

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

impl<T: Copy> Copy for Montgomery<T>

Auto Trait Implementations§

§

impl<T> Freeze for Montgomery<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Montgomery<T>
where T: RefUnwindSafe,

§

impl<T> Send for Montgomery<T>
where T: Send,

§

impl<T> Sync for Montgomery<T>
where T: Sync,

§

impl<T> Unpin for Montgomery<T>
where T: Unpin,

§

impl<T> UnwindSafe for Montgomery<T>
where T: UnwindSafe,

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> 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, 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.