[][src]Struct zerocaf::backend::u64::field::FieldElement

pub struct FieldElement(pub [u64; 5]);

A FieldElement represents an element of the field which has order of 2^252 + 27742317777372353535851937790883648493

In the 64-bit backend implementation, the FieldElement is represented in radix 2^52

Methods

impl FieldElement[src]

pub const fn zero() -> FieldElement[src]

Construct zero.

pub const fn one() -> FieldElement[src]

Construct one.

pub const fn minus_one() -> FieldElement[src]

Construct -1 (mod l).

pub fn is_even(self) -> bool[src]

Evaluate if a FieldElement is even or not.

pub fn is_positive(&self) -> Choice[src]

Checks if a ´FieldElement` is considered negative following the Decaf paper criteria.

The criteria says: Non-negative field elements. Let p > 2 be prime. Define a residue x ∈ F =Z/pZ to be “non-negative” if the least absolute residue for x is in [0,(p−1)/2], and “negative” otherwise.

Returns:

  • Choice(1) if pos.
  • Choice(0) if neg.

pub fn from_bytes(bytes: &[u8; 32]) -> FieldElement[src]

Load a FieldElement from the low 253b bits of a 256-bit input. So Little Endian representation in bytes of a FieldElement.

pub fn to_bytes(self) -> [u8; 32][src]

Serialize this FieldElement to a 32-byte array. The encoding is canonical.

pub fn two_pow_k(exp: u64) -> FieldElement[src]

Given a k: u64, compute 2^k giving the resulting result as a FieldElement.

See that the input must be between the range => 0..253.

NOTE: This function implements an assert! statement that checks the correctness of the exponent provided as param.

pub fn half_without_mod(self) -> FieldElement[src]

Returns the half of an EVEN FieldElement.

This function performs almost 4x faster than the Half implementation but SHOULD be used carefully.

Panics

When the FieldElement provided is not even.

pub fn legendre_symbol(&self) -> Choice[src]

Given a FieldElement, this function evaluates if it is a quadratic residue (mod l).

See: https://en.wikipedia.org/wiki/Legendre_symbol.

Returns:

-1 -> Non-quadratic residue (mod l) == Choice(0).

1 -> Quadratic residue (mod l) == Choice(1).

0 -> Input (mod l) == 0. Not implemented since you can't pass an input which is multiple of FIELD_L.

pub fn inverse(&self) -> FieldElement[src]

Compute a^-1 (mod l) using the the Savas & Koç modular inverse algorithm. It's an optimization of the Kalinski modular inversion algorithm that extends the Binary GCD algorithm to perform the modular inverse operation.

The PhaseII it's substituded by 1 or 2 Montgomery Multiplications, what makes the second part compute in almost ConstTime.

Panics

It is not possible to invert 0 by obvious reasons. So an the function panics when trying to invert zero.

Special issue on Montgomery arithmetic. Montgomery inversion - Erkay Sava ̧s & Çetin Kaya Koç J Cryptogr Eng (2018) 8:201–210 https://doi.org/10.1007/s13389-017-0161-x.

Trait Implementations

impl<'a, 'b> Add<&'b FieldElement> for &'a FieldElement[src]

type Output = FieldElement

The resulting type after applying the + operator.

fn add(self, b: &'b FieldElement) -> FieldElement[src]

Compute a + b (mod l).

impl Add<FieldElement> for FieldElement[src]

type Output = FieldElement

The resulting type after applying the + operator.

fn add(self, b: FieldElement) -> FieldElement[src]

Compute a + b (mod l).

impl Clone for FieldElement[src]

impl Copy for FieldElement[src]

impl Debug for FieldElement[src]

impl Default for FieldElement[src]

fn default() -> FieldElement[src]

Returns the default value for a FieldElement = Zero.

impl Display for FieldElement[src]

impl<'a, 'b> Div<&'a FieldElement> for &'b FieldElement[src]

type Output = FieldElement

The resulting type after applying the / operator.

fn div(self, _rhs: &'a FieldElement) -> FieldElement[src]

Performs the op: x / y (mod l).

Since on modular fields we don't divide, the equivalent op is: x * (y^-1 (mod l)), which is equivalent to the naive division but for Finite Fields.

impl Div<FieldElement> for FieldElement[src]

type Output = FieldElement

The resulting type after applying the / operator.

fn div(self, _rhs: FieldElement) -> FieldElement[src]

Performs the op: x / y (mod l).

Since on modular fields we don't divide, the equivalent op is: x * (y^-1 (mod l)), which is equivalent to the naive division but for Finite Fields.

impl Eq for FieldElement[src]

impl<'a> From<&'a Scalar> for FieldElement[src]

fn from(origin: &'a Ristretto255Scalar) -> FieldElement[src]

Given a Ristretto255Scalar on canonical bytes representation get it's FieldElement equivalent value as 5 limbs and radix-52.

impl From<u128> for FieldElement[src]

fn from(_inp: u128) -> FieldElement[src]

Performs the conversion.

impl From<u16> for FieldElement[src]

fn from(_inp: u16) -> FieldElement[src]

Performs the conversion.

impl From<u32> for FieldElement[src]

fn from(_inp: u32) -> FieldElement[src]

Performs the conversion.

impl From<u64> for FieldElement[src]

fn from(_inp: u64) -> FieldElement[src]

Performs the conversion.

impl From<u8> for FieldElement[src]

fn from(_inp: u8) -> FieldElement[src]

Performs the conversion.

impl<'a> Half for &'a FieldElement[src]

type Output = FieldElement

fn half(self) -> FieldElement[src]

Give the half of the FieldElement value (mod l).

impl Identity for FieldElement[src]

fn identity() -> FieldElement[src]

Returns the Identity element over the finite field modulo 2^252 + 27742317777372353535851937790883648493.

It is defined as 1 on FieldElement format, and is therefore written as: [1, 0, 0, 0, 0].

impl Index<usize> for FieldElement[src]

type Output = u64

The returned type after indexing.

impl IndexMut<usize> for FieldElement[src]

impl Into<Scalar> for FieldElement[src]

fn into(self) -> Ristretto255Scalar[src]

Given a FieldElement reference get it's Ristretto255Scalar Equivalent on it's canonical bytes representation.

impl<'_> InvSqrt for &'_ FieldElement[src]

type Output = (Choice, FieldElement)

fn inv_sqrt(self) -> (Choice, FieldElement)[src]

This is a convenience wrapper function over the SqrtRatioI trait implementation when self = 1: Computes sqrt(1/self).

This function always returns the non-negative result of the sqrt.

Returns:

  • (Choice(1), +sqrt(1/self)) if self is a nonzero square;
  • (Choice(0), zero) if self is zero;
  • (Choice(0), +sqrt(i/self)) if self is a nonzero nonsquare;

impl<'a> ModSqrt for &'a FieldElement[src]

type Output = Option<FieldElement>

fn mod_sqrt(self, sign: Choice) -> Option<FieldElement>[src]

Performs the op: sqrt(a) (mod l).

Tonelli-Shanks prime modular square root algorithm implementation for FieldElement.

Conditionally selects and returns the positive or the negative result of the mod_sqrt by analyzing the Choice sent as input:

For Choice(0) -> Negative result. For Choice(1) -> Positive result.

Daniel Shanks. Five Number Theoretic Algorithms. Proceedings of the Second Manitoba Conference on Numerical Mathematics. Pp. 51–70. 1973.

This algorithm was translated from the python impl found in: https://codereview.stackexchange.com/questions/43210/tonelli-shanks-algorithm-implementation-of-prime-modular-square-root

impl<'a, 'b> Mul<&'b FieldElement> for &'a FieldElement[src]

type Output = FieldElement

The resulting type after applying the * operator.

fn mul(self, _rhs: &'b FieldElement) -> FieldElement[src]

This Mul implementation returns a double precision result.

The result of the standard mul is stored on a [u128; 9].

Then, we apply the Montgomery Reduction function to perform the modulo and the reduction to the FieldElement format: [u64; 5].

impl Mul<FieldElement> for FieldElement[src]

type Output = FieldElement

The resulting type after applying the * operator.

fn mul(self, _rhs: FieldElement) -> FieldElement[src]

This Mul implementation returns a double precision result.

The result of the standard mul is stored on a [u128; 9].

Then, we apply the Montgomery Reduction function to perform the modulo and the reduction to the FieldElement format: [u64; 5].

impl<'a> Neg for &'a FieldElement[src]

type Output = FieldElement

The resulting type after applying the - operator.

fn neg(self) -> FieldElement[src]

Computes -self (mod l). Compute the negated value that corresponds to the complement of the two, of the input FieldElement.

impl Neg for FieldElement[src]

type Output = FieldElement

The resulting type after applying the - operator.

fn neg(self) -> FieldElement[src]

Computes -self (mod l).

Compute the negated value that corresponds to the two's complement of the input FieldElement.

impl Ord for FieldElement[src]

impl PartialEq<FieldElement> for FieldElement[src]

impl PartialOrd<FieldElement> for FieldElement[src]

impl<'a, 'b> Pow<&'b FieldElement> for &'a FieldElement[src]

type Output = FieldElement

fn pow(self, exp: &'b FieldElement) -> FieldElement[src]

Performs the op: a^b (mod l).

Exponentiation by squaring classical algorithm implementation for FieldElement.

Schneier, Bruce (1996). Applied Cryptography: Protocols, Algorithms, and Source Code in C, Second Edition (2nd ed.).

impl<'_> SqrtRatioI<&'_ FieldElement> for FieldElement[src]

type Output = (Choice, FieldElement)

fn sqrt_ratio_i(&self, v: &FieldElement) -> (Choice, FieldElement)[src]

The first part of the return value signals whether u/v was square, and the second part contains a square root. Specifically, it returns:

  • (true, +sqrt(u/v)) if v is nonzero and u/v is square;
  • (true, zero) if u is zero;
  • (false, zero) if v is zero and u is nonzero;
  • (false, +sqrt(i*u/v)) if u/v is nonsquare (so iu/v is square).

impl<'a> Square for &'a FieldElement[src]

type Output = FieldElement

fn square(self) -> FieldElement[src]

Compute a^2 (mod l).

This Square implementation returns a double precision result. The result of the standard square is stored on a [u128; 9].

Then, we apply the Montgomery Reduction function to perform the modulo and the reduction to the FieldElement format: [u64; 5].

impl StructuralEq for FieldElement[src]

impl<'a, 'b> Sub<&'b FieldElement> for &'a FieldElement[src]

type Output = FieldElement

The resulting type after applying the - operator.

fn sub(self, b: &'b FieldElement) -> FieldElement[src]

Compute a - b (mod l)

impl Sub<FieldElement> for FieldElement[src]

type Output = FieldElement

The resulting type after applying the - operator.

fn sub(self, b: FieldElement) -> FieldElement[src]

Compute a + b (mod l).

Auto Trait Implementations

impl RefUnwindSafe for FieldElement

impl Send for FieldElement

impl Sync for FieldElement

impl Unpin for FieldElement

impl UnwindSafe for FieldElement

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Clear for T where
    T: InitializableFromZeroed + ?Sized

impl<T> ConditionallyNegatable for T where
    T: ConditionallySelectable,
    &'a T: Neg,
    <&'a T as Neg>::Output == T, 

impl<T> From<T> for T[src]

impl<T> InitializableFromZeroed for T where
    T: Default

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,