Trait zksnark::field::Polynomial
source · pub trait Polynomial<T>: From<Vec<T>> + Deref<Target = [T]>where
T: Field,{
fn coefficients(&self) -> Vec<T> { ... }
fn degree(&self) -> usize { ... }
fn evaluate(&self, x: T) -> T { ... }
fn remove_leading_zeros(&mut self) { ... }
}
Expand description
A line, Polynomial
, represented as a vector of Field
elements where the position in the
vector determines the power of the exponent.
Remarks
If you want examples of how to implement a Polynomial
go to the Z251
module.
The polynomial is represented as a list of coefficients where the powers of “x” are implicit.
For Example: [1, 3, 0, 5] is f(x) = 1 + 3x + 5x^3
Note
use zksnark::field::z251::Z251;
use zksnark::field::*;
// The (*) is overloaded to give you back an array
let tmp = vec![1,2,0,4].into_iter()
.map(Z251::from)
.collect::<Vec<_>>();
assert_eq!(*tmp, [1.into(),2.into(),0.into(),4.into()]);
RunProvided Methods§
sourcefn coefficients(&self) -> Vec<T>
fn coefficients(&self) -> Vec<T>
This defines how to turn a Polynomial
into a vector of Field
. In
other words, it gives you back the coefficients of the Polynomial
.
However, since Deref is required for Polynomial
you may prefer to get
the coefficients through an iterator.
Examples
use zksnark::field::z251::Z251;
use zksnark::field::*;
// Get coefficients through an iterator
let poly = vec![1,2,0,4].into_iter()
.map(Z251::from)
.collect::<Vec<_>>();
let mut iter = poly.iter();
assert_eq!(iter.next(), Some(&Z251::from(1)));
Runsourcefn degree(&self) -> usize
fn degree(&self) -> usize
Returns the highest exponent of the polynomial.
Examples
Basic usage:
use zksnark::field::z251::Z251;
use zksnark::field::*;
// [1, 2, 0, 4] would be f(x) = 1 + 2x + 0x^2 + 4x^3
// Thus the degree is 3
assert_eq!(
vec![1, 2, 0, 4]
.into_iter()
.map(Z251::from)
.collect::<Vec<_>>()
.degree(),
3
);
// [1, 1, 1, 1, 9] would be f(x) = 1 + x + x^2 + x^3 + 9x^4
// Thus the degree is 4
assert_eq!(
vec![1, 1, 1, 1, 9]
.into_iter()
.map(Z251::from)
.collect::<Vec<_>>()
.degree(),
4
);
Runsourcefn evaluate(&self, x: T) -> T
fn evaluate(&self, x: T) -> T
Takes the polynomial and evaluates it at the specified value.
Examples
Basic usage:
use zksnark::field::z251::Z251;
use zksnark::field::*;
// [1, 1, 1] would be f(x) = 1 + x + x^2 thus f(2) = 1 + 2 + 2^2
assert_eq!(
vec![1, 1, 1]
.into_iter()
.map(Z251::from)
.collect::<Vec<_>>()
.evaluate(Z251::from(2)),
Z251::from(7)
);
// [1, 1, 4] would be f(x) = 1 + x + 4x^2 thus f(2) = 1 + 2 + 4*2^2
assert_eq!(
vec![1, 1, 4]
.into_iter()
.map(Z251::from)
.collect::<Vec<_>>()
.evaluate(Z251::from(2)),
Z251::from(19)
);
// (1, 2, 3, 4) would be f(x) = 1 + 2x + 3x^2 + 4x^3
// thus f(3) = 1 + 2 * 3 + 3 * 3^2 + 4 * 3^3
assert_eq!(
(1..5)
.map(Z251::from)
.collect::<Vec<_>>()
.evaluate(Z251::from(3)),
Z251::from(142)
);
Run