///|
/// Frobenius constants for F_{p^12} = F_{p^6}[w] / (w^2 - v).
let fq12_frob1_w : Fq2 = {
c0: Fp::from_raw(
U256::new(
0xaf9ba69633144907UL, 0xca6b1d7387afb78aUL, 0x11bded5ef08a2087UL, 0x02f34d751a1f3a7cUL,
),
),
c1: Fp::from_raw(
U256::new(
0xa222ae234c492d72UL, 0xd00f02a4565de15bUL, 0xdc2ff3a253dfc926UL, 0x10a75716b3899551UL,
),
),
}
///|
let fq12_frob2_w : Fp = Fp::from_raw(
U256::new(
0xca8d800500fa1bf2UL, 0xf0c5d61468b39769UL, 0x0e201271ad0d4418UL, 0x04290f65bad856e6UL,
),
)
///|
let fq12_frob3_w : Fq2 = {
c0: Fp::from_raw(
U256::new(
0x365316184e46d97dUL, 0x0af7129ed4c96d9fUL, 0x659da72fca1009b5UL, 0x08116d8983a20d23UL,
),
),
c1: Fp::from_raw(
U256::new(
0xb1df4af7c39c1939UL, 0x3d9f02878a73bf7fUL, 0x9b2220928caf0ae0UL, 0x26684515eff054a6UL,
),
),
}
///|
/// Degree-12 extension field F_{p^12} = F_{p^6}[w] / (w^2 - v), target group GT of BN254.
/// Elements are represented as c0 + c1*w.
#derive(Debug, Eq, Hash)
pub struct Fq12 {
c0 : Fq6
c1 : Fq6
}
///|
pub extend Fq12 with @debug.Debug::{to_repr}
///|
pub extend Fq12 with Eq::{not_equal, equal}
///|
pub extend Fq12 with Hash::{hash, hash_combine}
///|
pub fn Fq12::new(c0 : Fq6, c1 : Fq6) -> Fq12 {
{ c0, c1, }
}
///|
pub fn Fq12::zero() -> Fq12 {
{ c0: Fq6::zero(), c1: Fq6::zero(), }
}
///|
pub fn Fq12::one() -> Fq12 {
{ c0: Fq6::one(), c1: Fq6::zero(), }
}
///|
pub fn Fq12::is_zero(self : Fq12) -> Bool {
self.c0.is_zero() && self.c1.is_zero()
}
///|
pub fn Fq12::is_one(self : Fq12) -> Bool {
self == Fq12::one()
}
///|
pub fn Fq12::add(self : Fq12, other : Fq12) -> Fq12 {
{ c0: self.c0.add(other.c0), c1: self.c1.add(other.c1), }
}
///|
pub fn Fq12::sub(self : Fq12, other : Fq12) -> Fq12 {
{ c0: self.c0.sub(other.c0), c1: self.c1.sub(other.c1), }
}
///|
pub fn Fq12::neg(self : Fq12) -> Fq12 {
{ c0: self.c0.neg(), c1: self.c1.neg(), }
}
///|
/// Conjugation in F_{p^12} (equal to x^(p^6), and equal to inverse in the cyclotomic subgroup).
pub fn Fq12::conjugate(self : Fq12) -> Fq12 {
{ c0: self.c0, c1: self.c1.neg(), }
}
///|
/// Karatsuba multiplication in F_{p^12}.
pub fn Fq12::mul(self : Fq12, other : Fq12) -> Fq12 {
let aa = self.c0.mul(other.c0)
let bb = self.c1.mul(other.c1)
let c0 = bb.mul_by_nonresidue().add(aa)
let c1 = self.c0.add(self.c1).mul(other.c0.add(other.c1)).sub(aa).sub(bb)
{ c0, c1, }
}
///|
/// Complex squaring in F_{p^12}.
pub fn Fq12::square(self : Fq12) -> Fq12 {
let ab = self.c0.mul(self.c1)
let c0 = self.c0
.add(self.c1)
.mul(self.c0.add(self.c1.mul_by_nonresidue()))
.sub(ab)
.sub(ab.mul_by_nonresidue())
let c1 = ab.double()
{ c0, c1, }
}
///|
/// Helper for Granger-Scott cyclotomic squaring: compute (a + b*w)^2 in F_{p^4} = F_{p^2}[t]/(t^2 - xi).
fn fp4_square(a : Fq2, b : Fq2) -> (Fq2, Fq2) {
let t0 = a.square()
let t1 = b.square()
let r0 = t1.mul_by_nonresidue().add(t0)
let r1 = a.add(b).square().sub(t0).sub(t1)
(r0, r1)
}
///|
/// Fast Granger-Scott squaring for elements in the cyclotomic subgroup G_{Phi_6}(F_{p^2}).
pub fn Fq12::cyclotomic_square(self : Fq12) -> Fq12 {
let z0 = self.c0.c0
let z4 = self.c0.c1
let z3 = self.c0.c2
let z2 = self.c1.c0
let z1 = self.c1.c1
let z5 = self.c1.c2
let (t0, t1) = fp4_square(z0, z1)
let (t2, t3) = fp4_square(z2, z3)
let (t4, t5) = fp4_square(z4, z5)
let out_c0_c0 = t0.sub(z0).double().add(t0)
let out_c0_c1 = t2.sub(z4).double().add(t2)
let out_c0_c2 = t4.sub(z3).double().add(t4)
let t5_xi = t5.mul_by_nonresidue()
let out_c1_c0 = t5_xi.add(z2).double().add(t5_xi)
let out_c1_c1 = t1.add(z1).double().add(t1)
let out_c1_c2 = t3.add(z5).double().add(t3)
{
c0: Fq6::new(out_c0_c0, out_c0_c1, out_c0_c2),
c1: Fq6::new(out_c1_c0, out_c1_c1, out_c1_c2),
}
}
///|
/// Sparse multiplication by a Miller-loop line evaluation of the form
/// (c0 + 0*v + 0*v^2) + (c3 + c4*v + 0*v^2)*w.
pub fn Fq12::mul_by_034(self : Fq12, c0 : Fq2, c3 : Fq2, c4 : Fq2) -> Fq12 {
let a = Fq6::new(self.c0.c0.mul(c0), self.c0.c1.mul(c0), self.c0.c2.mul(c0))
let b = self.c1.mul_by_01(c3, c4)
let e = self.c0.add(self.c1).mul_by_01(c0.add(c3), c4)
let new_c1 = e.sub(a).sub(b)
let new_c0 = b.mul_by_nonresidue().add(a)
{ c0: new_c0, c1: new_c1, }
}
///|
/// Frobenius map x -> x^(p^k) in F_{p^12}.
pub fn Fq12::frobenius_map(self : Fq12, power : Int32) -> Fq12 {
let p = (power % 12 + 12) % 12
match p {
0 => self
1 => {
let c0 = self.c0.frobenius_map(1)
let c1_raw = self.c1.frobenius_map(1)
let c1 = Fq6::new(
c1_raw.c0.mul(fq12_frob1_w),
c1_raw.c1.mul(fq12_frob1_w),
c1_raw.c2.mul(fq12_frob1_w),
)
{ c0, c1, }
}
2 => {
let c0 = self.c0.frobenius_map(2)
let c1_raw = self.c1.frobenius_map(2)
let c1 = Fq6::new(
c1_raw.c0.mul_fp(fq12_frob2_w),
c1_raw.c1.mul_fp(fq12_frob2_w),
c1_raw.c2.mul_fp(fq12_frob2_w),
)
{ c0, c1, }
}
3 => {
let c0 = self.c0.frobenius_map(3)
let c1_raw = self.c1.frobenius_map(3)
let c1 = Fq6::new(
c1_raw.c0.mul(fq12_frob3_w),
c1_raw.c1.mul(fq12_frob3_w),
c1_raw.c2.mul(fq12_frob3_w),
)
{ c0, c1, }
}
6 => self.conjugate()
_ => {
let mut acc = self
for _i = 0; _i < p; _i = _i + 1 {
acc = acc.frobenius_map(1)
}
acc
}
}
}
///|
/// Multiplicative inverse in F_{p^12}. Returns None for 0.
pub fn Fq12::inv(self : Fq12) -> Fq12? {
let norm = self.c0.square().sub(self.c1.square().mul_by_nonresidue())
match norm.inv() {
None => None
Some(t) => Some({ c0: self.c0.mul(t), c1: self.c1.mul(t).neg(), })
}
}
///|
/// Exponentiation by a 64-bit unsigned integer (used in final exponentiation by BN254 parameter x).
pub fn Fq12::cyclotomic_pow_u64(self : Fq12, exp : UInt64) -> Fq12 {
let mut res = Fq12::one()
let mut started = false
for i = 63; i >= 0; i = i - 1 {
if started {
res = res.cyclotomic_square()
}
if ((exp >> i) & 1UL) == 1UL {
res = res.mul(self)
started = true
}
}
res
}
///|
/// General exponentiation by U256.
pub fn Fq12::pow(self : Fq12, exp : U256) -> Fq12 {
let mut res = Fq12::one()
let mut base = self
let bits = exp.bit_length()
for i = 0; i < bits; i = i + 1 {
if exp.bit(i) {
res = res.mul(base)
}
base = base.square()
}
res
}
///|
pub impl Add for Fq12 with fn add(self, other) {
self.add(other)
}
///|
pub impl Sub for Fq12 with fn sub(self, other) {
self.sub(other)
}
///|
pub impl Mul for Fq12 with fn mul(self, other) {
self.mul(other)
}
///|
pub impl Neg for Fq12 with fn neg(self) {
self.neg()
}