#![cfg_attr(feature = "cargo-clippy", deny(warnings))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::inline_always))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::too_many_arguments))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::many_single_char_names))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::write_literal))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::missing_safety_doc))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::cognitive_complexity))]
#![deny(missing_debug_implementations)]
extern crate digest;
extern crate ff_zeroize as ff;
extern crate rand_core;
extern crate rand_xorshift;
#[cfg(test)]
extern crate sha2;
#[cfg(test)]
extern crate sha3;
#[macro_use]
extern crate zeroize;
#[cfg(test)]
pub mod tests;
pub mod bls12_381;
pub mod hash_to_curve;
pub mod hash_to_field;
pub mod serdes;
pub mod signum;
mod wnaf;
pub use self::wnaf::Wnaf;
use ff::{Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, ScalarEngine, SqrtField};
use std::error::Error;
use std::fmt;
pub trait Engine: ScalarEngine {
type G1: CurveProjective<Engine = Self, Base = Self::Fq, Scalar = Self::Fr, Affine = Self::G1Affine>
+ From<Self::G1Affine>;
type G1Affine: CurveAffine<
Engine = Self,
Base = Self::Fq,
Scalar = Self::Fr,
Projective = Self::G1,
Pair = Self::G2Affine,
PairingResult = Self::Fqk,
> + From<Self::G1>;
type G2: CurveProjective<Engine = Self, Base = Self::Fqe, Scalar = Self::Fr, Affine = Self::G2Affine>
+ From<Self::G2Affine>;
type G2Affine: CurveAffine<
Engine = Self,
Base = Self::Fqe,
Scalar = Self::Fr,
Projective = Self::G2,
Pair = Self::G1Affine,
PairingResult = Self::Fqk,
> + From<Self::G2>;
type Fq: PrimeField + SqrtField;
type Fqe: SqrtField;
type Fqk: Field;
fn miller_loop<'a, I>(i: I) -> Self::Fqk
where
I: IntoIterator<
Item = &'a (
&'a <Self::G1Affine as CurveAffine>::Prepared,
&'a <Self::G2Affine as CurveAffine>::Prepared,
),
>;
fn final_exponentiation(&Self::Fqk) -> Option<Self::Fqk>;
fn pairing<G1, G2>(p: G1, q: G2) -> Self::Fqk
where
G1: Into<Self::G1Affine>,
G2: Into<Self::G2Affine>,
{
Self::final_exponentiation(&Self::miller_loop(
[(&(p.into().prepare()), &(q.into().prepare()))].iter(),
))
.unwrap()
}
fn pairing_product<G1, G2>(p1: G1, q1: G2, p2: G1, q2: G2) -> Self::Fqk
where
G1: Into<Self::G1Affine>,
G2: Into<Self::G2Affine>,
{
Self::final_exponentiation(&Self::miller_loop(
[
(&(p1.into().prepare()), &(q1.into().prepare())),
(&(p2.into().prepare()), &(q2.into().prepare())),
]
.iter(),
))
.unwrap()
}
fn pairing_multi_product(p: &[Self::G1Affine], q: &[Self::G2Affine]) -> Self::Fqk {
let prep_p: Vec<<Self::G1Affine as CurveAffine>::Prepared> =
p.iter().map(|v| v.prepare()).collect();
let prep_q: Vec<<Self::G2Affine as CurveAffine>::Prepared> =
q.iter().map(|v| v.prepare()).collect();
let mut pairs = Vec::with_capacity(p.len());
for i in 0..p.len() {
pairs.push((&prep_p[i], &prep_q[i]));
}
let t = Self::miller_loop(&pairs);
Self::final_exponentiation(&t).unwrap()
}
}
pub trait CurveProjective:
PartialEq
+ Eq
+ Sized
+ Copy
+ Clone
+ Send
+ Sync
+ fmt::Debug
+ fmt::Display
+ 'static
{
type Engine: Engine<Fr = Self::Scalar>;
type Scalar: PrimeField + SqrtField;
type Base: SqrtField;
type Affine: CurveAffine<Projective = Self, Scalar = Self::Scalar>;
fn random<R: rand_core::RngCore>(rng: &mut R)-> Self;
fn zero() -> Self;
fn one() -> Self;
fn is_zero(&self) -> bool;
fn batch_normalization(v: &mut [Self]);
fn is_normalized(&self) -> bool;
fn double(&mut self);
fn add_assign(&mut self, other: &Self);
fn sub_assign(&mut self, other: &Self) {
let mut tmp = *other;
tmp.negate();
self.add_assign(&tmp);
}
fn add_assign_mixed(&mut self, other: &Self::Affine);
fn sub_assign_mixed(&mut self, other: &Self::Affine) {
let mut tmp = *other;
tmp.negate();
self.add_assign_mixed(&tmp);
}
fn negate(&mut self);
fn mul_assign<S: Into<<Self::Scalar as PrimeField>::Repr>>(&mut self, other: S);
fn into_affine(&self) -> Self::Affine;
fn recommended_wnaf_for_scalar(scalar: <Self::Scalar as PrimeField>::Repr) -> usize;
fn recommended_wnaf_for_num_scalars(num_scalars: usize) -> usize;
fn as_tuple(&self) -> (&Self::Base, &Self::Base, &Self::Base);
unsafe fn as_tuple_mut(&mut self) -> (&mut Self::Base, &mut Self::Base, &mut Self::Base);
}
pub trait CurveAffine:
Copy + Clone + Sized + Send + Sync + fmt::Debug + fmt::Display + PartialEq + Eq + 'static
{
type Engine: Engine<Fr = Self::Scalar>;
type Scalar: PrimeField + SqrtField;
type Base: SqrtField;
type Projective: CurveProjective<Affine = Self, Scalar = Self::Scalar>;
type Prepared: Clone + Send + Sync + 'static;
type Uncompressed: EncodedPoint<Affine = Self>;
type Compressed: EncodedPoint<Affine = Self>;
type Pair: CurveAffine<Pair = Self>;
type PairingResult: Field;
fn zero() -> Self;
fn one() -> Self;
fn is_zero(&self) -> bool;
fn negate(&mut self);
fn mul<S: Into<<Self::Scalar as PrimeField>::Repr>>(&self, other: S) -> Self::Projective;
fn prepare(&self) -> Self::Prepared;
fn pairing_with(&self, other: &Self::Pair) -> Self::PairingResult;
fn into_projective(&self) -> Self::Projective;
fn into_compressed(&self) -> Self::Compressed {
<Self::Compressed as EncodedPoint>::from_affine(*self)
}
fn into_uncompressed(&self) -> Self::Uncompressed {
<Self::Uncompressed as EncodedPoint>::from_affine(*self)
}
fn as_tuple(&self) -> (&Self::Base, &Self::Base);
unsafe fn as_tuple_mut(&mut self) -> (&mut Self::Base, &mut Self::Base);
fn sum_of_products(bases: &[Self], scalars: &[&[u64; 4]]) -> Self::Projective;
fn find_pippinger_window(num_components: usize) -> usize;
fn find_pippinger_window_via_estimate(num_components: usize) -> usize;
fn sum_of_products_pippinger(
bases: &[Self],
scalars: &[&[u64; 4]],
window: usize,
) -> Self::Projective;
fn sum_of_products_precomp_256(
bases: &[Self],
scalars: &[&[u64; 4]],
pre: &[Self],
) -> Self::Projective;
fn precomp_3(&self, pre: &mut [Self]);
fn mul_precomp_3<S: Into<<Self::Scalar as PrimeField>::Repr>>(
&self,
other: S,
pre: &[Self],
) -> Self::Projective;
fn precomp_256(&self, pre: &mut [Self]);
fn mul_precomp_256<S: Into<<Self::Scalar as PrimeField>::Repr>>(
&self,
other: S,
pre: &[Self],
) -> Self::Projective;
}
pub trait EncodedPoint:
Sized + Send + Sync + AsRef<[u8]> + AsMut<[u8]> + Clone + Copy + 'static
{
type Affine: CurveAffine;
fn empty() -> Self;
fn size() -> usize;
fn into_affine(&self) -> Result<Self::Affine, GroupDecodingError>;
fn into_affine_unchecked(&self) -> Result<Self::Affine, GroupDecodingError>;
fn from_affine(affine: Self::Affine) -> Self;
}
pub trait SubgroupCheck {
fn in_subgroup(&self) -> bool;
}
#[derive(Debug)]
pub enum GroupDecodingError {
NotOnCurve,
NotInSubgroup,
CoordinateDecodingError(&'static str, PrimeFieldDecodingError),
UnexpectedCompressionMode,
UnexpectedInformation,
}
impl Error for GroupDecodingError {
fn description(&self) -> &str {
match *self {
GroupDecodingError::NotOnCurve => "coordinate(s) do not lie on the curve",
GroupDecodingError::NotInSubgroup => "the element is not part of an r-order subgroup",
GroupDecodingError::CoordinateDecodingError(..) => "coordinate(s) could not be decoded",
GroupDecodingError::UnexpectedCompressionMode => {
"encoding has unexpected compression mode"
}
GroupDecodingError::UnexpectedInformation => "encoding has unexpected information",
}
}
}
impl fmt::Display for GroupDecodingError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
GroupDecodingError::CoordinateDecodingError(description, ref err) => {
write!(f, "{} decoding error: {}", description, err)
}
_ => write!(f, "{}", self.to_string()),
}
}
}