pub struct Polynomial<C> { /* private fields */ }
alloc
only.Expand description
Polynomial $f(x) = \sum_i a_i x^i$ defined as a list of coefficients $[a_0, \dots, a_{\text{degree}}]$
Polynomial is generic over type of coefficients C
, it can be Scalar<E>
, NonZero<Scalar<E>>
, SecretScalar<E>
, Point<E>
,
or any other type that implements necessary traits.
Implementations§
Source§impl<C: IsZero> Polynomial<C>
impl<C: IsZero> Polynomial<C>
Sourcepub fn from_coefs(coefs: Vec<C>) -> Self
pub fn from_coefs(coefs: Vec<C>) -> Self
Constructs a polynomial from its coefficients
coefs[i]
is coefficient of x^i
term. Resulting polynomial will be
$f(x) = \sum_i \text{coefs}_i \cdot x^i$
§Example
use generic_ec::{Scalar, curves::Secp256k1};
use generic_ec_zkp::polynomial::Polynomial;
let coefs: [Scalar<Secp256k1>; 3] = [
Scalar::random(&mut OsRng),
Scalar::random(&mut OsRng),
Scalar::random(&mut OsRng),
];
let polynomial = Polynomial::from_coefs(coefs.to_vec());
let x = Scalar::random(&mut OsRng);
assert_eq!(
coefs[0] + x * coefs[1] + x * x * coefs[2],
polynomial.value::<_, Scalar<_>>(&x),
);
Source§impl<C> Polynomial<C>
impl<C> Polynomial<C>
Sourcepub fn degree(&self) -> usize
pub fn degree(&self) -> usize
Returns polynomial degree
Polynomial degree is index of most significant non-zero coefficient. Polynomial $f(x) = 0$ considered to have degree $deg(f) = 0$.
$$ \begin{dcases} deg(f(x) = \sum_i a_i \cdot x^i) &= n \text{, where } a_n \ne 0 \land n \to max \\ deg(f(x) = 0) &= 0 \end{dcases} $$
Sourcepub fn into_coefs(self) -> Vec<C>
pub fn into_coefs(self) -> Vec<C>
Destructs polynomial, returns its coefficients
Source§impl<C: Samplable> Polynomial<C>
impl<C: Samplable> Polynomial<C>
Sourcepub fn sample(rng: &mut impl RngCore, degree: usize) -> Self
pub fn sample(rng: &mut impl RngCore, degree: usize) -> Self
Samples a random polynomial with specified degree
Sourcepub fn sample_with_const_term(
rng: &mut impl RngCore,
degree: usize,
const_term: C,
) -> Self
pub fn sample_with_const_term( rng: &mut impl RngCore, degree: usize, const_term: C, ) -> Self
Samples a random polynomial with specified degree and constant term
Constant term determines value of polynomial at point zero: $f(0) = \text{const\_term}$
§Example
use generic_ec::{Scalar, curves::Secp256k1};
use generic_ec_zkp::polynomial::Polynomial;
let const_term = Scalar::<Secp256k1>::from(1234);
let polynomial = Polynomial::sample_with_const_term(&mut OsRng, 3, const_term);
assert_eq!(const_term, polynomial.value::<_, Scalar<_>>(&Scalar::zero()));
Source§impl<C> Polynomial<C>
impl<C> Polynomial<C>
Sourcepub fn value<P, O>(&self, point: &P) -> O
pub fn value<P, O>(&self, point: &P) -> O
Evaluates polynomial value at given point: $f(\text{point})$
Polynomial coefficients, point, and output can all be differently typed.
§Example: polynomial with coefficients typed as non-zero scalars vs elliptic points
Let $f(x) = a_1 \cdot x + a_0$ and $F(x) = G \cdot f(x)$. Coefficients
of $f(x)$ are typed as NonZero<Scalar<E>>
, and $F(x)$ has coefficients typed as NonZero<Point<E>>
.
When we evaluate $f(x)$, we have coefficients C
of type NonZero<Scalar<E>>
, and both point P
and output O
of type Scalar<E>
.
On other hand, when $F(x)$ is evaluated, coefficients C
have type NonZero<Point<E>>
, point P
has
type Scalar<E>
, and output O
is of type Point<E>
.
use generic_ec::{Point, Scalar, NonZero, curves::Secp256k1};
use generic_ec_zkp::polynomial::Polynomial;
let f: Polynomial<NonZero<Scalar<Secp256k1>>> = Polynomial::sample(&mut OsRng, 1);
let F: Polynomial<NonZero<Point<_>>> = &f * &Point::generator();
let x = Scalar::random(&mut OsRng);
assert_eq!(
f.value::<_, Scalar<_>>(&x) * Point::generator(),
F.value::<_, Point<_>>(&x)
);
Trait Implementations§
Source§impl<C> Add<&Polynomial<C>> for Polynomial<C>
impl<C> Add<&Polynomial<C>> for Polynomial<C>
Source§type Output = Polynomial<C>
type Output = Polynomial<C>
+
operator.Source§impl<C> AddAssign<&Polynomial<C>> for Polynomial<C>
impl<C> AddAssign<&Polynomial<C>> for Polynomial<C>
Source§fn add_assign(&mut self, rhs: &Polynomial<C>)
fn add_assign(&mut self, rhs: &Polynomial<C>)
+=
operation. Read moreSource§impl<C: Clone> Clone for Polynomial<C>
impl<C: Clone> Clone for Polynomial<C>
Source§fn clone(&self) -> Polynomial<C>
fn clone(&self) -> Polynomial<C>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<C: Debug> Debug for Polynomial<C>
impl<C: Debug> Debug for Polynomial<C>
Source§impl<'de, C> Deserialize<'de> for Polynomial<C>where
C: Deserialize<'de> + IsZero,
impl<'de, C> Deserialize<'de> for Polynomial<C>where
C: Deserialize<'de> + IsZero,
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<C> Digestable for Polynomial<C>where
C: Digestable,
impl<C> Digestable for Polynomial<C>where
C: Digestable,
Source§fn unambiguously_encode<B>(&self, encoder: EncodeValue<'_, B>)where
B: Buffer,
fn unambiguously_encode<B>(&self, encoder: EncodeValue<'_, B>)where
B: Buffer,
Source§impl<B, C, O> Mul<&B> for &Polynomial<C>
Multiplies polynomial $F(x)$ at $k$ returning resulting polynomial
$F’(x) = k \cdot F(x)$, resulting polynomial is allocated at heap
impl<B, C, O> Mul<&B> for &Polynomial<C>
Multiplies polynomial $F(x)$ at $k$ returning resulting polynomial $F’(x) = k \cdot F(x)$, resulting polynomial is allocated at heap
$k$ can be any type as long as it can be multiplied at C
Source§impl<C> Mul<&C> for Polynomial<C>
Multiplies polyinomial $F(x)$ at $k$ returning resulting polyinomial
$F’(x) = k \cdot F(x)$ without allocations
impl<C> Mul<&C> for Polynomial<C>
Multiplies polyinomial $F(x)$ at $k$ returning resulting polyinomial $F’(x) = k \cdot F(x)$ without allocations
$k$ needs to be of type C
.
Source§impl<C> Serialize for Polynomial<C>where
C: Serialize,
impl<C> Serialize for Polynomial<C>where
C: Serialize,
Source§impl<'a, C> Sum<&'a Polynomial<C>> for Polynomial<C>
impl<'a, C> Sum<&'a Polynomial<C>> for Polynomial<C>
Source§fn sum<I: Iterator<Item = &'a Polynomial<C>>>(iter: I) -> Self
fn sum<I: Iterator<Item = &'a Polynomial<C>>>(iter: I) -> Self
Self
from the elements by “summing up”
the items.