use super::fq::FROBENIUS_COEFF_FQ12_C1;
use super::fq2::Fq2;
use super::fq6::Fq6;
use ff::Field;
use rand_core::RngCore;
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use subtle::{Choice, ConditionallySelectable, CtOption};

/// An element of Fq12, represented by c0 + c1 * w.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Fq12 {
    pub c0: Fq6,
    pub c1: Fq6,
}

impl ::std::fmt::Display for Fq12 {
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
        write!(f, "Fq12({} + {} * w)", self.c0, self.c1)
    }
}

impl Fq12 {
    pub fn conjugate(&mut self) {
        self.c1 = self.c1.neg();
    }

    pub fn mul_by_014(&mut self, c0: &Fq2, c1: &Fq2, c4: &Fq2) {
        let mut aa = self.c0;
        aa.mul_by_01(c0, c1);
        let mut bb = self.c1;
        bb.mul_by_1(c4);
        let mut o = *c1;
        o.add_assign(c4);
        self.c1.add_assign(&self.c0);
        self.c1.mul_by_01(c0, &o);
        self.c1.sub_assign(&aa);
        self.c1.sub_assign(&bb);
        self.c0 = bb;
        self.c0.mul_by_nonresidue();
        self.c0.add_assign(&aa);
    }
}

impl ConditionallySelectable for Fq12 {
    fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
        Fq12 {
            c0: Fq6::conditional_select(&a.c0, &b.c0, choice),
            c1: Fq6::conditional_select(&a.c1, &b.c1, choice),
        }
    }
}

impl Neg for Fq12 {
    type Output = Self;

    fn neg(self) -> Self {
        Fq12 {
            c0: self.c0.neg(),
            c1: self.c1.neg(),
        }
    }
}

impl<'r> Add<&'r Fq12> for Fq12 {
    type Output = Self;

    fn add(self, other: &Self) -> Self {
        Fq12 {
            c0: self.c0 + other.c0,
            c1: self.c1 + other.c1,
        }
    }
}

impl Add for Fq12 {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        self.add(&other)
    }
}

impl<'r> AddAssign<&'r Fq12> for Fq12 {
    fn add_assign(&mut self, other: &'r Self) {
        self.c0.add_assign(&other.c0);
        self.c1.add_assign(&other.c1);
    }
}

impl AddAssign for Fq12 {
    fn add_assign(&mut self, other: Self) {
        self.add_assign(&other);
    }
}

impl<'r> Sub<&'r Fq12> for Fq12 {
    type Output = Self;

    fn sub(self, other: &Self) -> Self {
        Fq12 {
            c0: self.c0 - other.c0,
            c1: self.c1 - other.c1,
        }
    }
}

impl Sub for Fq12 {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        self.sub(&other)
    }
}

impl<'r> SubAssign<&'r Fq12> for Fq12 {
    fn sub_assign(&mut self, other: &'r Self) {
        self.c0.sub_assign(&other.c0);
        self.c1.sub_assign(&other.c1);
    }
}

impl SubAssign for Fq12 {
    fn sub_assign(&mut self, other: Self) {
        self.sub_assign(&other);
    }
}

impl<'r> Mul<&'r Fq12> for Fq12 {
    type Output = Self;

    fn mul(self, other: &Self) -> Self {
        let mut ret = self;
        ret.mul_assign(other);
        ret
    }
}

impl Mul for Fq12 {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        self.mul(&other)
    }
}

impl<'r> MulAssign<&'r Fq12> for Fq12 {
    fn mul_assign(&mut self, other: &Self) {
        let mut aa = self.c0;
        aa.mul_assign(&other.c0);
        let mut bb = self.c1;
        bb.mul_assign(&other.c1);
        let mut o = other.c0;
        o.add_assign(&other.c1);
        self.c1.add_assign(&self.c0);
        self.c1.mul_assign(&o);
        self.c1.sub_assign(&aa);
        self.c1.sub_assign(&bb);
        self.c0 = bb;
        self.c0.mul_by_nonresidue();
        self.c0.add_assign(&aa);
    }
}

impl MulAssign for Fq12 {
    fn mul_assign(&mut self, other: Self) {
        self.mul_assign(&other);
    }
}

impl Field for Fq12 {
    fn random<R: RngCore + ?std::marker::Sized>(rng: &mut R) -> Self {
        Fq12 {
            c0: Fq6::random(rng),
            c1: Fq6::random(rng),
        }
    }

    fn zero() -> Self {
        Fq12 {
            c0: Fq6::zero(),
            c1: Fq6::zero(),
        }
    }

    fn one() -> Self {
        Fq12 {
            c0: Fq6::one(),
            c1: Fq6::zero(),
        }
    }

    fn is_zero(&self) -> bool {
        self.c0.is_zero() && self.c1.is_zero()
    }

    fn double(&self) -> Self {
        Fq12 {
            c0: self.c0.double(),
            c1: self.c1.double(),
        }
    }

    fn frobenius_map(&mut self, power: usize) {
        self.c0.frobenius_map(power);
        self.c1.frobenius_map(power);

        self.c1.c0.mul_assign(&FROBENIUS_COEFF_FQ12_C1[power % 12]);
        self.c1.c1.mul_assign(&FROBENIUS_COEFF_FQ12_C1[power % 12]);
        self.c1.c2.mul_assign(&FROBENIUS_COEFF_FQ12_C1[power % 12]);
    }

    fn square(&self) -> Self {
        let mut ab = self.c0;
        ab.mul_assign(&self.c1);
        let mut c0c1 = self.c0;
        c0c1.add_assign(&self.c1);
        let mut c0 = self.c1;
        c0.mul_by_nonresidue();
        c0.add_assign(&self.c0);
        c0.mul_assign(&c0c1);
        c0.sub_assign(&ab);
        let mut c1 = ab;
        c1.add_assign(&ab);
        ab.mul_by_nonresidue();
        c0.sub_assign(&ab);
        Fq12 { c0, c1 }
    }

    fn invert(&self) -> CtOption<Self> {
        let mut c0s = self.c0.square();
        let mut c1s = self.c1.square();
        c1s.mul_by_nonresidue();
        c0s.sub_assign(&c1s);

        c0s.invert().map(|t| Fq12 {
            c0: t.mul(&self.c0),
            c1: t.mul(&self.c1).neg(),
        })
    }
}

#[cfg(test)]
use rand_core::SeedableRng;
#[cfg(test)]
use rand_xorshift::XorShiftRng;

#[test]
fn test_fq12_mul_by_014() {
    let mut rng = XorShiftRng::from_seed([
        0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc,
        0xe5,
    ]);

    for _ in 0..1000 {
        let c0 = Fq2::random(&mut rng);
        let c1 = Fq2::random(&mut rng);
        let c5 = Fq2::random(&mut rng);
        let mut a = Fq12::random(&mut rng);
        let mut b = a;

        a.mul_by_014(&c0, &c1, &c5);
        b.mul_assign(&Fq12 {
            c0: Fq6 {
                c0,
                c1,
                c2: Fq2::zero(),
            },
            c1: Fq6 {
                c0: Fq2::zero(),
                c1: c5,
                c2: Fq2::zero(),
            },
        });

        assert_eq!(a, b);
    }
}

#[test]
fn fq12_field_tests() {
    use ff::PrimeField;

    crate::tests::field::random_field_tests::<Fq12>();
    crate::tests::field::random_frobenius_tests::<Fq12, _>(super::fq::Fq::char(), 13);
}
