Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elliptic-curve: add LinearCombination trait #832

Merged
merged 1 commit into from Dec 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions elliptic-curve/src/ops.rs
Expand Up @@ -5,6 +5,9 @@ pub use core::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
use crypto_bigint::{ArrayEncoding, ByteArray, Integer};
use subtle::CtOption;

#[cfg(feature = "arithmetic")]
use crate::ProjectiveArithmetic;

/// Perform an inversion on a field element (i.e. base field element or scalar)
pub trait Invert {
/// Field element type
Expand All @@ -23,6 +26,26 @@ impl<F: ff::Field> Invert for F {
}
}

/// Linear combination.
///
/// This trait enables crates to provide an optimized implementation of
/// linear combinations (e.g. Shamir's Trick), or otherwise provides a default
/// non-optimized implementation.
// TODO(tarcieri): replace this with a trait from the `group` crate? (see zkcrypto/group#25)
#[cfg(feature = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait LinearCombination: ProjectiveArithmetic {
/// Calculates `x * k + y * l`.
fn lincomb(
x: &Self::ProjectivePoint,
k: &Self::Scalar,
y: &Self::ProjectivePoint,
l: &Self::Scalar,
) -> Self::ProjectivePoint {
(*x * k) + (*y * l)
}
}

/// Modular reduction.
pub trait Reduce<UInt: Integer + ArrayEncoding>: Sized {
/// Perform a modular reduction, returning a field element.
Expand Down