///|
/// Quadratic extension field F_{p^2} = F_p[u] / (u^2 + 1).
/// Elements are represented as c0 + c1 * u.
#derive(Debug, Eq, Hash)
pub struct Fq2 {
  c0 : Fp
  c1 : Fp
}

///|
pub extend Fq2 with @debug.Debug::{to_repr}

///|
pub extend Fq2 with Eq::{not_equal, equal}

///|
pub extend Fq2 with Hash::{hash, hash_combine}

///|
pub fn Fq2::new(c0 : Fp, c1 : Fp) -> Fq2 {
  { c0, c1, }
}

///|
pub fn Fq2::zero() -> Fq2 {
  { c0: Fp::zero(), c1: Fp::zero(), }
}

///|
pub fn Fq2::one() -> Fq2 {
  { c0: Fp::one(), c1: Fp::zero(), }
}

///|
pub fn Fq2::is_zero(self : Fq2) -> Bool {
  self.c0.is_zero() && self.c1.is_zero()
}

///|
pub fn Fq2::add(self : Fq2, other : Fq2) -> Fq2 {
  { c0: self.c0.add(other.c0), c1: self.c1.add(other.c1), }
}

///|
pub fn Fq2::sub(self : Fq2, other : Fq2) -> Fq2 {
  { c0: self.c0.sub(other.c0), c1: self.c1.sub(other.c1), }
}

///|
pub fn Fq2::neg(self : Fq2) -> Fq2 {
  { c0: self.c0.neg(), c1: self.c1.neg(), }
}

///|
pub fn Fq2::double(self : Fq2) -> Fq2 {
  { c0: self.c0.double(), c1: self.c1.double(), }
}

///|
/// Karatsuba multiplication in F_{p^2}: (a0 + a1*u)(b0 + b1*u) = (a0*b0 - a1*b1) + ((a0+a1)(b0+b1) - a0*b0 - a1*b1)*u.
pub fn Fq2::mul(self : Fq2, other : Fq2) -> Fq2 {
  let aa = self.c0.mul(other.c0)
  let bb = self.c1.mul(other.c1)
  let c0 = aa.sub(bb)
  let c1 = self.c0.add(self.c1).mul(other.c0.add(other.c1)).sub(aa).sub(bb)
  { c0, c1, }
}

///|
/// Optimized squaring in F_{p^2}: (a0 + a1*u)^2 = (a0+a1)(a0-a1) + (2*a0*a1)*u.
pub fn Fq2::square(self : Fq2) -> Fq2 {
  let ab = self.c0.mul(self.c1)
  let c0 = self.c0.add(self.c1).mul(self.c0.sub(self.c1))
  let c1 = ab.double()
  { c0, c1, }
}

///|
/// Multiply by scalar in F_p.
pub fn Fq2::mul_fp(self : Fq2, s : Fp) -> Fq2 {
  { c0: self.c0.mul(s), c1: self.c1.mul(s), }
}

///|
/// Multiply by the cubic/sextic non-residue xi = 9 + u in F_{p^2}.
/// (c0 + c1*u)(9 + u) = (9*c0 - c1) + (c0 + 9*c1)*u.
pub fn Fq2::mul_by_nonresidue(self : Fq2) -> Fq2 {
  let c0_2 = self.c0.double()
  let c0_4 = c0_2.double()
  let c0_8 = c0_4.double()
  let c0_9 = c0_8.add(self.c0)

  let c1_2 = self.c1.double()
  let c1_4 = c1_2.double()
  let c1_8 = c1_4.double()
  let c1_9 = c1_8.add(self.c1)

  { c0: c0_9.sub(self.c1), c1: self.c0.add(c1_9), }
}

///|
/// Frobenius endomorphism (conjugation): (c0 + c1*u)^p = c0 - c1*u.
pub fn Fq2::conjugate(self : Fq2) -> Fq2 {
  { c0: self.c0, c1: self.c1.neg(), }
}

///|
/// Frobenius map x -> x^(p^k) in F_{p^2}.
pub fn Fq2::frobenius_map(self : Fq2, power : Int32) -> Fq2 {
  if (power & 1) == 1 {
    self.conjugate()
  } else {
    self
  }
}

///|
/// Multiplicative inverse in F_{p^2}. Returns None for 0.
pub fn Fq2::inv(self : Fq2) -> Fq2? {
  let norm = self.c0.square().add(self.c1.square())
  match norm.inv() {
    None => None
    Some(t) => Some({ c0: self.c0.mul(t), c1: self.c1.mul(t).neg(), })
  }
}

///|
pub impl Show for Fq2 with fn output(self, logger) {
  logger.write_string("(\{self.c0}, \{self.c1})")
}

///|
pub extend Fq2 with Show::{to_string, output}

///|
pub impl Add for Fq2 with fn add(self, other) {
  self.add(other)
}

///|
pub impl Sub for Fq2 with fn sub(self, other) {
  self.sub(other)
}

///|
pub impl Mul for Fq2 with fn mul(self, other) {
  self.mul(other)
}

///|
pub impl Neg for Fq2 with fn neg(self) {
  self.neg()
}