Struct g2poly::G2Poly

source ·
pub struct G2Poly(pub u64);
Expand description

Main type exported by this library

The polynomial is represented as the bits of the inner u64. The least significant bit represents c_0 in c_n * x^n + c_(n-1) * x^(n-1) + ... + c_0 * x^0, the next bit c_1 and so on.

assert_eq!(format!("{}", G2Poly(0b101)), "G2Poly { x^2 + 1 }");

3 main operations +, - and * are implemented, as well as % for remainder calculation. Note that multiplication generates a G2PolyProd so there is no risk of overflow.

Division is left out as there is generally not needed for common use cases. This may change in a later release.

Tuple Fields§

§0: u64

Implementations§

The constant 1 polynomial.

This is the multiplicative identity. (a * UNIT = a)

The constant 0 polynomial

This is the additive identity (a + ZERO = a)

The x polynomial.

Useful for quickly generating x^n values.

Quickly calculate p^n mod m

Uses square-and-multiply to quickly exponentiate a polynomial.

Example
let p = G2Poly(0b1011);
assert_eq!(p.pow_mod(127, G2Poly(0b1101)), G2Poly(0b110));

Determine if the given polynomial is irreducible.

Irreducible polynomials not be expressed as the product of other irreducible polynomials (except 1 and itself). This uses Rabin’s tests to check if the given polynomial is irreducible.

Example
let p = G2Poly(0b101);
assert!(!p.is_irreducible()); // x^2 + 1 == (x + 1)^2 in GF(2)
let p = G2Poly(0b111);
assert!(p.is_irreducible());

Get the degree of the polynomial

Returns None for the 0 polynomial (which has degree -infinity), otherwise is guaranteed to return Some(d) with d the degree.

let z = G2Poly::ZERO;
assert_eq!(z.degree(), None);
let s = G2Poly(0b101);
assert_eq!(s.degree(), Some(2));

Checks if a polynomial generates the multiplicative group mod m.

The field GF(2^p) can be interpreted as all polynomials of degree < p, with all operations carried over from polynomials. Multiplication is done mod m, where m is some irreducible polynomial of degree p. The multiplicative group is cyclic, so there is an element a so that all elements != can be expressed as a^n for some n < 2^p - 1.

This checks if the given polynomial is such a generator element mod m.

Example
let m = G2Poly(0b10011101);
// The element `x` generates the whole group.
assert!(G2Poly::X.is_generator(m));

Trait Implementations§

The resulting type after applying the + operator.
Performs the + operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.