#![allow(clippy::op_ref)]
use ff::Field;
pub type AffinePoint<Fe> = (Fe, Fe);
pub type ProjectivePoint<Fe> = (Fe, Fe, Fe);
#[inline(always)]
pub fn add<Fe>(
(ax, ay, az): ProjectivePoint<Fe>,
(bx, by, bz): ProjectivePoint<Fe>,
curve_equation_b: Fe,
) -> ProjectivePoint<Fe>
where
Fe: Field,
{
let xx = ax * bx; let yy = ay * by; let zz = az * bz; let xy_pairs = ((ax + ay) * &(bx + by)) - &(xx + &yy); let yz_pairs = ((ay + az) * &(by + bz)) - &(yy + &zz); let xz_pairs = ((ax + az) * &(bx + bz)) - &(xx + &zz); let bzz_part = xz_pairs - &(curve_equation_b * &zz); let bzz3_part = bzz_part.double() + &bzz_part; let yy_m_bzz3 = yy - &bzz3_part; let yy_p_bzz3 = yy + &bzz3_part; let zz3 = zz.double() + &zz; let bxz_part = (curve_equation_b * &xz_pairs) - &(zz3 + &xx); let bxz3_part = bxz_part.double() + &bxz_part; let xx3_m_zz3 = xx.double() + &xx - &zz3; (
(yy_p_bzz3 * &xy_pairs) - &(yz_pairs * &bxz3_part), (yy_p_bzz3 * &yy_m_bzz3) + &(xx3_m_zz3 * &bxz3_part), (yy_m_bzz3 * &yz_pairs) + &(xy_pairs * &xx3_m_zz3), )
}
#[inline(always)]
pub fn add_mixed<Fe>(
(ax, ay, az): ProjectivePoint<Fe>,
(bx, by): AffinePoint<Fe>,
curve_equation_b: Fe,
) -> ProjectivePoint<Fe>
where
Fe: Field,
{
let xx = ax * &bx; let yy = ay * &by; let xy_pairs = ((ax + &ay) * &(bx + &by)) - &(xx + &yy); let yz_pairs = (by * &az) + &ay; let xz_pairs = (bx * &az) + &ax; let bz_part = xz_pairs - &(curve_equation_b * &az); let bz3_part = bz_part.double() + &bz_part; let yy_m_bzz3 = yy - &bz3_part; let yy_p_bzz3 = yy + &bz3_part; let z3 = az.double() + &az; let bxz_part = (curve_equation_b * &xz_pairs) - &(z3 + &xx); let bxz3_part = bxz_part.double() + &bxz_part; let xx3_m_zz3 = xx.double() + &xx - &z3; (
(yy_p_bzz3 * &xy_pairs) - &(yz_pairs * &bxz3_part), (yy_p_bzz3 * &yy_m_bzz3) + &(xx3_m_zz3 * &bxz3_part), (yy_m_bzz3 * &yz_pairs) + &(xy_pairs * &xx3_m_zz3), )
}
#[inline(always)]
pub fn double<Fe>((x, y, z): ProjectivePoint<Fe>, curve_equation_b: Fe) -> ProjectivePoint<Fe>
where
Fe: Field,
{
let xx = x.square(); let yy = y.square(); let zz = z.square(); let xy2 = (x * &y).double(); let xz2 = (x * &z).double(); let bzz_part = (curve_equation_b * &zz) - &xz2; let bzz3_part = bzz_part.double() + &bzz_part; let yy_m_bzz3 = yy - &bzz3_part; let yy_p_bzz3 = yy + &bzz3_part; let y_frag = yy_p_bzz3 * &yy_m_bzz3; let x_frag = yy_m_bzz3 * &xy2; let zz3 = zz.double() + &zz; let bxz2_part = (curve_equation_b * &xz2) - &(zz3 + &xx); let bxz6_part = bxz2_part.double() + &bxz2_part; let xx3_m_zz3 = xx.double() + &xx - &zz3; let dy = y_frag + &(xx3_m_zz3 * &bxz6_part); let yz2 = (y * &z).double(); let dx = x_frag - &(bxz6_part * &yz2); let dz = (yz2 * &yy).double().double(); (dx, dy, dz)
}