Module zksnark::field

source ·
Expand description

Defines the Field trait along with other utility functions for working with fields.

Usually you won’t need to dive into this module unless you want to define a new type of with either Field or Polynomial traits.

The z251 module is an implementation of a Field, useful for testing and getting to understand how many of the functions work. As such most of the examples use z251 in them.

Also, the examples in this module all use a Vec since there is an implementation of Polynomial for Vec, but in the code that uses this module uses CoefficentPoly.

Examples

Basic usage:

use zksnark::field::z251::Z251;
use zksnark::field::*;

// `evaluate` a `Polynomial`
//
// [1, 1, 1] would be f(x) = 1 + x + x^2 thus f(2) = 1 + 2 + 2^2
// Thus the evaluation would be 7
let poly_eval = vec![1, 1, 1]
    .into_iter()
    .map(Z251::from)
    .collect::<Vec<_>>();

assert_eq!(poly_eval.evaluate(Z251::from(2)), Z251::from(7));

// `polynomial_division`
//
let poly: Vec<Z251> = vec![1, 0, 3, 1].into_iter().map(Z251::from).collect();
let polyDividend: Vec<Z251> = vec![0, 0, 9, 1].into_iter().map(Z251::from).collect();

let num: Vec<Z251> = vec![1].into_iter().map(Z251::from).collect();
let den: Vec<Z251> = vec![1, 0, 245].into_iter().map(Z251::from).collect();

assert_eq!(polynomial_division(poly, polyDividend), (num, den));
Run

Modules

Traits

A Field here has the same classical mathematical definition of a field.
FieldIdentity only makes sense when defined with a Field. The reason this trait is not a part of Field is to provide a “zero” element and a “one” element to types that cannot define a multiplicative inverse to be a Field. Currently this includes: isize and is used in z251.
A line, Polynomial, represented as a vector of Field elements where the position in the vector determines the power of the exponent.

Functions

Discrete Fourier Transformation
Inverse Discrete Fourier Transformation
The devision of two Polynomial
Yields an infinite list of powers of x starting from x^0.