# Crate galois_field

Expand description

Library for finite body calculations

finitefields provides utilities to perform operations between Galois Field and between polynomials that have Galois Field as coefficients.

## Quick Start

### Case 1: Prime Field

``````let char: u32 = 5;
let x:FiniteField = FiniteField{
char: char,
element:Element::PrimeField{element:0} // 0 in F_5
};
let y:FiniteField = FiniteField{
char: char,
element:Element::PrimeField{element:1} // 1 in F_5
};
println!("x + y = {:?}", (x.clone() + y.clone()).element); // ->1
println!("x - y = {:?}", (x.clone() - y.clone()).element); // -> 4
println!("x * y = {:?}", (x.clone() * y.clone()).element); // -> 0
println!("x / y = {:?}", (x.clone() / y.clone()).element); // -> 0``````

### Case 2: Galois Field

Remark:

• The source of GF(p^n) is represented by a polynomial basis.

i.e. 3 = x+1 -> [1,1] over GF(2^4)

13 = x^3+x^2+1 -> [1,0,1,1] over GF(2^4)

``````use galois_field::*;
fn main(){
// consider GF(2^4)
let char: u32 = 2;
let n = 4;
let primitive_polynomial = Polynomial::get_primitive_polynomial(char, n);
let x:FiniteField = FiniteField{
char: char,
element:Element::GaloisField{element:vec![0,1],primitive_polynomial:primitive_polynomial.clone()} // i.e. [0,1] = x -> 2 over GF(2^4)
};
let y:FiniteField = FiniteField{
char: char,
element:Element::GaloisField{element:vec![0,0,1,1],primitive_polynomial:primitive_polynomial.clone()} // i.e. [0,0,1,1] = x^3 + x^2 -> 12 over GF(2^4)
};
println!("x + y = {:?}", (x.clone() + y.clone()).element);
println!("x - y = {:?}", (x.clone() - y.clone()).element);
println!("x * y = {:?}", (x.clone() * y.clone()).element);
println!("x / y = {:?}", (x.clone() / y.clone()).element);
}``````

### Case 3: Polynomial over Prime Field

``````use galois_field::*;
// This is a test of the elementary F_p, Galois GF(p^n), polynomial quadrature.
// character
let char: u32 = 2;
let element0:FiniteField = FiniteField{
char: char,
element:Element::PrimeField{element:0} // 0 in F_5
};
let element1:FiniteField = FiniteField{
char: char,
element:Element::PrimeField{element:1} // 1 in F_5
};
// operations between GaloisField elements
// we need primitive_polynomial
let primitive_polynomial = Polynomial::get_primitive_polynomial(char,4);
let f: Polynomial = Polynomial {
coef: vec![element1.clone(),element0.clone(),element0.clone(),element0.clone(),element1.clone()]
};
let g: Polynomial = Polynomial {
coef: vec![element1.clone(),element0.clone(),element0.clone(),element1.clone(),element1.clone()]
};
println!("f + g = {:?}", (f.clone()+g.clone()).coef);
println!("f - g = {:?}", (f.clone()-g.clone()).coef);
println!("f * g = {:?}", (f.clone()*g.clone()).coef);
println!("f / g = {:?}", (f.clone()/g.clone()).coef);
println!("f % g = {:?}", (f.clone()%g.clone()).coef);``````

same as above.

### Case 5: Matrix over Prime Field

``````use galois_field::*;
let char = 3;
let element0: FiniteField = FiniteField {
char: char,
element: Element::PrimeField { element: 0 },
};
let element1: FiniteField = FiniteField {
char: char,
element: Element::PrimeField { element: 1 },
};
let element2: FiniteField = FiniteField {
char: char,
element: Element::PrimeField { element: 2 },
};
let mut matrix_element:Vec<Vec<FiniteField>> = vec![
vec![element0.clone(),element1.clone(), element0.clone()],
vec![element2.clone(),element2.clone(), element1.clone()],
vec![element1.clone(),element0.clone(), element1.clone()]
];
let mut m = Matrix{
element: matrix_element,
};
println!("m+m = {:?}", m.clone()+m.clone());
println!("m*m = {:?}", m.clone()*m.clone());
let mut sweep_matrix = m.sweep_method();
println!("{:?}", sweep_matrix);``````

## Structs

FiniteField
Matrix over FiniteField.
Polynomial over FiniteField.

## Enums

Element of finite field. enum Element has two variants: PrimeField and GaloisField.