ark_ec/models/twisted_edwards/
group.rsuse ark_serialize::{
CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate,
};
use ark_std::{
borrow::Borrow,
fmt::{Display, Formatter, Result as FmtResult},
hash::{Hash, Hasher},
io::{Read, Write},
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
rand::{
distributions::{Distribution, Standard},
Rng,
},
vec::*,
One, Zero,
};
use ark_ff::{fields::Field, AdditiveGroup, PrimeField, ToConstraintField, UniformRand};
use educe::Educe;
use zeroize::Zeroize;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use super::{Affine, MontCurveConfig, TECurveConfig};
use crate::{
scalar_mul::{variable_base::VariableBaseMSM, ScalarMul},
AffineRepr, CurveGroup, PrimeGroup,
};
#[derive(Educe)]
#[educe(Copy, Clone, Eq(bound(P: TECurveConfig)), Debug)]
#[must_use]
pub struct Projective<P: TECurveConfig> {
pub x: P::BaseField,
pub y: P::BaseField,
pub t: P::BaseField,
pub z: P::BaseField,
}
impl<P: TECurveConfig> PartialEq<Affine<P>> for Projective<P> {
fn eq(&self, other: &Affine<P>) -> bool {
*self == other.into_group()
}
}
impl<P: TECurveConfig> Display for Projective<P> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{}", Affine::from(*self))
}
}
impl<P: TECurveConfig> PartialEq for Projective<P> {
fn eq(&self, other: &Self) -> bool {
if self.is_zero() {
return other.is_zero();
}
if other.is_zero() {
return false;
}
(self.x * &other.z) == (other.x * &self.z) && (self.y * &other.z) == (other.y * &self.z)
}
}
impl<P: TECurveConfig> Hash for Projective<P> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.into_affine().hash(state)
}
}
impl<P: TECurveConfig> Distribution<Projective<P>> for Standard {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Projective<P> {
loop {
let y = P::BaseField::rand(rng);
let greatest = rng.gen();
if let Some(p) = Affine::get_point_from_y_unchecked(y, greatest) {
return p.mul_by_cofactor_to_group();
}
}
}
}
impl<P: TECurveConfig> Default for Projective<P> {
#[inline]
fn default() -> Self {
Self::zero()
}
}
impl<P: TECurveConfig> Projective<P> {
pub const fn new_unchecked(
x: P::BaseField,
y: P::BaseField,
t: P::BaseField,
z: P::BaseField,
) -> Self {
Self { x, y, t, z }
}
pub fn new(x: P::BaseField, y: P::BaseField, t: P::BaseField, z: P::BaseField) -> Self {
let p = Self::new_unchecked(x, y, t, z).into_affine();
assert!(p.is_on_curve());
assert!(p.is_in_correct_subgroup_assuming_on_curve());
p.into()
}
}
impl<P: TECurveConfig> Zeroize for Projective<P> {
fn zeroize(&mut self) {
self.x.zeroize();
self.y.zeroize();
self.t.zeroize();
self.z.zeroize();
}
}
impl<P: TECurveConfig> Zero for Projective<P> {
fn zero() -> Self {
Self::new_unchecked(
P::BaseField::zero(),
P::BaseField::one(),
P::BaseField::zero(),
P::BaseField::one(),
)
}
fn is_zero(&self) -> bool {
self.x.is_zero() && self.y == self.z && !self.y.is_zero() && self.t.is_zero()
}
}
impl<P: TECurveConfig> AdditiveGroup for Projective<P> {
type Scalar = P::ScalarField;
const ZERO: Self = Self::new_unchecked(
P::BaseField::ZERO,
P::BaseField::ONE,
P::BaseField::ZERO,
P::BaseField::ONE,
);
fn double_in_place(&mut self) -> &mut Self {
let a = self.x.square();
let b = self.y.square();
let c = self.z.square().double();
let d = P::mul_by_a(a);
let e = (self.x + &self.y).square() - &a - &b;
let g = d + &b;
let f = g - &c;
let h = d - &b;
self.x = e * &f;
self.y = g * &h;
self.t = e * &h;
self.z = f * &g;
self
}
}
impl<P: TECurveConfig> PrimeGroup for Projective<P> {
type ScalarField = P::ScalarField;
fn generator() -> Self {
Affine::generator().into()
}
#[inline]
fn mul_bigint(&self, other: impl AsRef<[u64]>) -> Self {
P::mul_projective(self, other.as_ref())
}
}
impl<P: TECurveConfig> CurveGroup for Projective<P> {
type Config = P;
type BaseField = P::BaseField;
type Affine = Affine<P>;
type FullGroup = Affine<P>;
fn normalize_batch(v: &[Self]) -> Vec<Self::Affine> {
let mut z_s = v.iter().map(|g| g.z).collect::<Vec<_>>();
ark_ff::batch_inversion(&mut z_s);
ark_std::cfg_iter!(v)
.zip(z_s)
.map(|(g, z)| match g.is_zero() {
true => Affine::zero(),
false => {
let x = g.x * &z;
let y = g.y * &z;
Affine::new_unchecked(x, y)
},
})
.collect()
}
}
impl<P: TECurveConfig> Neg for Projective<P> {
type Output = Self;
fn neg(mut self) -> Self {
self.x = -self.x;
self.t = -self.t;
self
}
}
impl<P: TECurveConfig, T: Borrow<Affine<P>>> AddAssign<T> for Projective<P> {
fn add_assign(&mut self, other: T) {
let other = other.borrow();
let a = self.x * &other.x;
let b = self.y * &other.y;
let c = P::COEFF_D * &self.t * &other.x * &other.y;
let d = self.z;
let e = (self.x + &self.y) * &(other.x + &other.y) - &a - &b;
let f = d - &c;
let g = d + &c;
let h = b - &P::mul_by_a(a);
self.x = e * &f;
self.y = g * &h;
self.t = e * &h;
self.z = f * &g;
}
}
impl<P: TECurveConfig, T: Borrow<Affine<P>>> Add<T> for Projective<P> {
type Output = Self;
fn add(mut self, other: T) -> Self {
let other = other.borrow();
self += other;
self
}
}
impl<P: TECurveConfig, T: Borrow<Affine<P>>> SubAssign<T> for Projective<P> {
fn sub_assign(&mut self, other: T) {
*self += -(*other.borrow());
}
}
impl<P: TECurveConfig, T: Borrow<Affine<P>>> Sub<T> for Projective<P> {
type Output = Self;
fn sub(mut self, other: T) -> Self {
self -= other.borrow();
self
}
}
ark_ff::impl_additive_ops_from_ref!(Projective, TECurveConfig);
impl<'a, P: TECurveConfig> Add<&'a Self> for Projective<P> {
type Output = Self;
fn add(mut self, other: &'a Self) -> Self {
self += other;
self
}
}
impl<'a, P: TECurveConfig> Sub<&'a Self> for Projective<P> {
type Output = Self;
fn sub(mut self, other: &'a Self) -> Self {
self -= other;
self
}
}
impl<'a, P: TECurveConfig> AddAssign<&'a Self> for Projective<P> {
fn add_assign(&mut self, other: &'a Self) {
let a = self.x * &other.x;
let b = self.y * &other.y;
let c = P::COEFF_D * &self.t * &other.t;
let d = self.z * &other.z;
let h = b - &P::mul_by_a(a);
let e = (self.x + &self.y) * &(other.x + &other.y) - &a - &b;
let f = d - &c;
let g = d + &c;
self.x = e * &f;
self.y = g * &h;
self.t = e * &h;
self.z = f * &g;
}
}
impl<'a, P: TECurveConfig> SubAssign<&'a Self> for Projective<P> {
fn sub_assign(&mut self, other: &'a Self) {
*self += -(*other);
}
}
impl<P: TECurveConfig, T: Borrow<P::ScalarField>> MulAssign<T> for Projective<P> {
fn mul_assign(&mut self, other: T) {
*self = self.mul_bigint(other.borrow().into_bigint())
}
}
impl<P: TECurveConfig, T: Borrow<P::ScalarField>> Mul<T> for Projective<P> {
type Output = Self;
#[inline]
fn mul(mut self, other: T) -> Self {
self *= other;
self
}
}
impl<P: TECurveConfig, T: Borrow<Affine<P>>> ark_std::iter::Sum<T> for Projective<P> {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = T>,
{
iter.fold(Self::zero(), |acc, x| acc + x.borrow())
}
}
impl<P: TECurveConfig> From<Affine<P>> for Projective<P> {
fn from(p: Affine<P>) -> Projective<P> {
Self::new_unchecked(p.x, p.y, p.x * &p.y, P::BaseField::one())
}
}
#[derive(Educe)]
#[educe(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct MontgomeryAffine<P: MontCurveConfig> {
pub x: P::BaseField,
pub y: P::BaseField,
}
impl<P: MontCurveConfig> Display for MontgomeryAffine<P> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "MontgomeryAffine(x={}, y={})", self.x, self.y)
}
}
impl<P: MontCurveConfig> MontgomeryAffine<P> {
pub fn new(x: P::BaseField, y: P::BaseField) -> Self {
Self { x, y }
}
}
impl<P: TECurveConfig> CanonicalSerialize for Projective<P> {
#[allow(unused_qualifications)]
#[inline]
fn serialize_with_mode<W: Write>(
&self,
writer: W,
compress: Compress,
) -> Result<(), SerializationError> {
let aff = Affine::<P>::from(*self);
P::serialize_with_mode(&aff, writer, compress)
}
#[inline]
fn serialized_size(&self, compress: Compress) -> usize {
P::serialized_size(compress)
}
}
impl<P: TECurveConfig> Valid for Projective<P> {
fn check(&self) -> Result<(), SerializationError> {
self.into_affine().check()
}
fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
where
Self: 'a,
{
let batch = batch.copied().collect::<Vec<_>>();
let batch = Self::normalize_batch(&batch);
Affine::batch_check(batch.iter())
}
}
impl<P: TECurveConfig> CanonicalDeserialize for Projective<P> {
#[allow(unused_qualifications)]
fn deserialize_with_mode<R: Read>(
reader: R,
compress: Compress,
validate: Validate,
) -> Result<Self, SerializationError> {
let aff = P::deserialize_with_mode(reader, compress, validate)?;
Ok(aff.into())
}
}
impl<M: TECurveConfig, ConstraintF: Field> ToConstraintField<ConstraintF> for Projective<M>
where
M::BaseField: ToConstraintField<ConstraintF>,
{
#[inline]
fn to_field_elements(&self) -> Option<Vec<ConstraintF>> {
Affine::from(*self).to_field_elements()
}
}
impl<P: TECurveConfig> ScalarMul for Projective<P> {
type MulBase = Affine<P>;
const NEGATION_IS_CHEAP: bool = true;
fn batch_convert_to_mul_base(bases: &[Self]) -> Vec<Self::MulBase> {
Self::normalize_batch(bases)
}
}
impl<P: TECurveConfig> VariableBaseMSM for Projective<P> {
fn msm(bases: &[Self::MulBase], bigints: &[Self::ScalarField]) -> Result<Self, usize> {
P::msm(bases, bigints)
}
}