Struct Polynomial

Source
pub struct Polynomial<C> { /* private fields */ }
Available on crate feature 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>

Source

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>

Source

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} $$

Source

pub fn coefs(&self) -> &[C]

Returns polynomial coefficients

Source

pub fn into_coefs(self) -> Vec<C>

Destructs polynomial, returns its coefficients

Source§

impl<C: Samplable> Polynomial<C>

Source

pub fn sample(rng: &mut impl RngCore, degree: usize) -> Self

Samples a random polynomial with specified degree

Source

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>

Source

pub fn value<P, O>(&self, point: &P) -> O
where for<'a> O: Zero + Mul<&'a P, Output = O> + Add<&'a C, Output = 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>
where C: Clone + for<'a> AddAssign<&'a C>,

Source§

type Output = Polynomial<C>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Polynomial<C>) -> Self::Output

Performs the + operation. Read more
Source§

impl<C> AddAssign<&Polynomial<C>> for Polynomial<C>
where C: Clone + for<'a> AddAssign<&'a C>,

Source§

fn add_assign(&mut self, rhs: &Polynomial<C>)

Performs the += operation. Read more
Source§

impl<C: Clone> Clone for Polynomial<C>

Source§

fn clone(&self) -> Polynomial<C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C: Debug> Debug for Polynomial<C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<C> Digestable for Polynomial<C>
where C: Digestable,

Source§

fn unambiguously_encode<B>(&self, encoder: EncodeValue<'_, B>)
where B: Buffer,

Unambiguously encodes the value
Source§

impl<B, C, O> Mul<&B> for &Polynomial<C>
where for<'a> &'a C: Mul<&'a B, Output = O>,

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§

type Output = Polynomial<O>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &B) -> Self::Output

Performs the * operation. Read more
Source§

impl<C> Mul<&C> for Polynomial<C>
where for<'a> &'a C: Mul<&'a C, Output = 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§

type Output = Polynomial<C>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &C) -> Self::Output

Performs the * operation. Read more
Source§

impl<C> Serialize for Polynomial<C>
where C: Serialize,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'a, C> Sum<&'a Polynomial<C>> for Polynomial<C>
where C: Clone + 'a + for<'c> AddAssign<&'c C>,

Source§

fn sum<I: Iterator<Item = &'a Polynomial<C>>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<C> Sum for Polynomial<C>
where C: for<'a> AddAssign<&'a C> + Clone,

Source§

fn sum<I: Iterator<Item = Polynomial<C>>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.

Auto Trait Implementations§

§

impl<C> Freeze for Polynomial<C>

§

impl<C> RefUnwindSafe for Polynomial<C>
where C: RefUnwindSafe,

§

impl<C> Send for Polynomial<C>
where C: Send,

§

impl<C> Sync for Polynomial<C>
where C: Sync,

§

impl<C> Unpin for Polynomial<C>
where C: Unpin,

§

impl<C> UnwindSafe for Polynomial<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,