use core::ops::{Add, Sub, Mul, Neg};
use cryptix_bigint::property::IsBigInt;
use cryptix_field::field::montgomery::Montgomery;
use cryptix_field::group::*;
use cryptix_field::ring::*;
use cryptix_field::field::*;
use cryptix_field::field::montgomery::MontgomeryOps;
use super::{U256, BN254, FpElement};
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Fp2Element(pub FpElement, pub FpElement);
impl core::fmt::Debug for Fp2Element {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "fp2!(\n \"")?;
self.0.fmt(f)?;
write!(f, "\", \n \"")?;
self.1.fmt(f)?;
write!(f, "\"\n)\n ")
}
}
impl AbelianGroup for Fp2Element { }
impl Group for Fp2Element { }
impl Add for Fp2Element {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0, self.1 + rhs.1)
}
}
impl Sub for Fp2Element {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self(self.0 - rhs.0, self.1 - rhs.1)
}
}
impl AddIdentity for Fp2Element {
const ADD_IDENTITY: Self = Self(FpElement::ZERO, FpElement::ZERO);
}
impl Neg for Fp2Element {
type Output = Self;
fn neg(self) -> Self::Output {
Self(-self.0, -self.1)
}
}
impl AssociativeAdd for Fp2Element { }
impl CommunicativeAdd for Fp2Element { }
impl Ring for Fp2Element { }
impl Mul for Fp2Element {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
self.mont_mul(rhs).mont_form()
}
}
impl AssociativeMul for Fp2Element { }
impl DistributiveMul for Fp2Element { }
impl MulIdentity for Fp2Element {
const MUL_IDENTITY: Self = Self(FpElement::ONE, FpElement::ZERO);
}
impl CommunicativeMul for Fp2Element { }
impl MulInverse for Fp2Element {
fn mul_inv(self) -> Self {
self.mont_inv().mont_mul_fp(BN254::R_INV_P)
}
}
impl Field for Fp2Element {
fn hlv(self) -> Self {
Self(self.0.hlv(), self.1.hlv())
}
fn is_zero(&self) -> bool {
self.0.is_zero() && self.1.is_zero()
}
}
impl From<FpElement> for Fp2Element {
fn from(value: FpElement) -> Self {
Self(value, FpElement::ZERO)
}
}
impl MontgomeryOps<U256, BN254> for Fp2Element {
fn mont_mul(self, rhs: Self) -> Self {
let (a0, b0) = (self.0, self.1);
let (a1, b1) = (rhs.0, rhs.1);
let a0a1 = a0.mont_mul(a1);
let b0b1 = b0.mont_mul(b1);
Self(
a0a1 - b0b1,
(a0 + b0).mont_mul(a1 + b1) - a0a1 - b0b1
)
}
fn mont_inv(self) -> Self {
let t = (self.0.mont_sqr() + self.1.mont_sqr()).mont_inv();
Self(self.0.mont_mul(t), -self.1.mont_mul(t))
}
fn mont_mul_fp(self, rhs: FpElement) -> Self {
Self(
self.0.mont_mul(rhs),
self.1.mont_mul(rhs)
)
}
fn mont_rdc(self) -> Self {
Self(self.0.mont_rdc(), self.1.mont_rdc())
}
}
impl Fp2Element {
pub fn mul_xi(self) -> Self {
Self(self.0 - self.1, self.0 + self.1)
}
pub fn mul_3b(self) -> Self {
let t = self.mont_mul(BN254::B_DIV_XI_MONT);
t + t + t
}
pub fn map_frob(self) -> Self {
self.conjugate()
}
pub fn conjugate(self) -> Self {
Self(self.0, -self.1)
}
}
#[cfg(feature = "rand")]
impl Fp2Element {
pub fn rand(rng: &mut impl rand_core::CryptoRngCore) -> Self {
Self(FpElement::rand(rng), FpElement::rand(rng))
}
}
impl From<Fp2Element> for [u8; U256::BYTE_LEN * 2] {
fn from(val: Fp2Element) -> Self {
let mut buf = [0_u8; U256::BYTE_LEN * 2];
let tmp: [u8; U256::BYTE_LEN] = val.0.into();
buf[..U256::BYTE_LEN].copy_from_slice(&tmp);
let tmp: [u8; U256::BYTE_LEN] = val.1.into();
buf[U256::BYTE_LEN..].copy_from_slice(&tmp);
buf
}
}