///|
/// Frobenius constants for F_{p^6} = F_{p^2}[v] / (v^3 - xi), where xi = 9 + u.
let fq6_frob1_c1 : Fq2 = {
c0: Fp::from_raw(
U256::new(
0xb5773b104563ab30UL, 0x347f91c8a9aa6454UL, 0x7a007127242e0991UL, 0x1956bcd8118214ecUL,
),
),
c1: Fp::from_raw(
U256::new(
0x6e849f1ea0aa4757UL, 0xaa1c7b6d89f89141UL, 0xb6e713cdfae0ca3aUL, 0x26694fbb4e82ebc3UL,
),
),
}
///|
let fq6_frob1_c2 : Fq2 = {
c0: Fp::from_raw(
U256::new(
0x7361d77f843abe92UL, 0xa5bb2bd3273411fbUL, 0x9c941f314b3e2399UL, 0x15df9cddbb9fd3ecUL,
),
),
c1: Fp::from_raw(
U256::new(
0x5dddfd154bd8c949UL, 0x62cb29a5a4445b60UL, 0x37bc870a0c7dd2b9UL, 0x24830a9d3171f0fdUL,
),
),
}
///|
let fq6_frob2_c1 : Fp = Fp::from_raw(
U256::new(
0x3350c88e13e80b9cUL, 0x7dce557cdb5e56b9UL, 0x6001b4b8b615564aUL, 0x2682e617020217e0UL,
),
)
///|
let fq6_frob2_c2 : Fp = Fp::from_raw(
U256::new(
0x71930c11d782e155UL, 0xa6bb947cffbe3323UL, 0xaa303344d4741444UL, 0x2c3b3f0d26594943UL,
),
)
///|
let fq6_frob3_c1 : Fq2 = {
c0: Fp::from_raw(
U256::new(
0xc9af22f716ad6badUL, 0xb311782a4aa662b2UL, 0x19eeaf64e248c7f4UL, 0x20273e77e3439f82UL,
),
),
c1: Fp::from_raw(
U256::new(
0xacc02860f7ce93acUL, 0x3933d5817ba76b4cUL, 0x69e6188b446c8467UL, 0x0a46036d4417cc55UL,
),
),
}
///|
let fq6_frob3_c2 : Fq2 = {
c0: Fp::from_raw(
U256::new(
0x448a93a57b6762dfUL, 0xbfd62df528fdeadfUL, 0xd858f5d00e9bd47aUL, 0x06b03d4d3476ec58UL,
),
),
c1: Fp::from_raw(
U256::new(
0x2b19daf4bcc936d1UL, 0xa1a54e7a56f4299fUL, 0xb533eee05adeaef1UL, 0x170c812b84dda0b2UL,
),
),
}
///|
/// Cubic extension field F_{p^6} = F_{p^2}[v] / (v^3 - (9 + u)).
/// Elements are represented as c0 + c1*v + c2*v^2.
#derive(Debug, Eq, Hash)
pub struct Fq6 {
c0 : Fq2
c1 : Fq2
c2 : Fq2
}
///|
pub extend Fq6 with @debug.Debug::{to_repr}
///|
pub extend Fq6 with Eq::{not_equal, equal}
///|
pub extend Fq6 with Hash::{hash, hash_combine}
///|
pub fn Fq6::new(c0 : Fq2, c1 : Fq2, c2 : Fq2) -> Fq6 {
{ c0, c1, c2, }
}
///|
pub fn Fq6::zero() -> Fq6 {
{ c0: Fq2::zero(), c1: Fq2::zero(), c2: Fq2::zero(), }
}
///|
pub fn Fq6::one() -> Fq6 {
{ c0: Fq2::one(), c1: Fq2::zero(), c2: Fq2::zero(), }
}
///|
pub fn Fq6::is_zero(self : Fq6) -> Bool {
self.c0.is_zero() && self.c1.is_zero() && self.c2.is_zero()
}
///|
pub fn Fq6::add(self : Fq6, other : Fq6) -> Fq6 {
{
c0: self.c0.add(other.c0),
c1: self.c1.add(other.c1),
c2: self.c2.add(other.c2),
}
}
///|
pub fn Fq6::sub(self : Fq6, other : Fq6) -> Fq6 {
{
c0: self.c0.sub(other.c0),
c1: self.c1.sub(other.c1),
c2: self.c2.sub(other.c2),
}
}
///|
pub fn Fq6::neg(self : Fq6) -> Fq6 {
{ c0: self.c0.neg(), c1: self.c1.neg(), c2: self.c2.neg(), }
}
///|
pub fn Fq6::double(self : Fq6) -> Fq6 {
{ c0: self.c0.double(), c1: self.c1.double(), c2: self.c2.double(), }
}
///|
/// Multiply by the non-residue v in F_{p^6}: (c0 + c1*v + c2*v^2)*v = (xi*c2) + c0*v + c1*v^2.
pub fn Fq6::mul_by_nonresidue(self : Fq6) -> Fq6 {
{ c0: self.c2.mul_by_nonresidue(), c1: self.c0, c2: self.c1, }
}
///|
/// Karatsuba multiplication in F_{p^6}.
pub fn Fq6::mul(self : Fq6, other : Fq6) -> Fq6 {
let v0 = self.c0.mul(other.c0)
let v1 = self.c1.mul(other.c1)
let v2 = self.c2.mul(other.c2)
let c0 = self.c1
.add(self.c2)
.mul(other.c1.add(other.c2))
.sub(v1)
.sub(v2)
.mul_by_nonresidue()
.add(v0)
let c1 = self.c0
.add(self.c1)
.mul(other.c0.add(other.c1))
.sub(v0)
.sub(v1)
.add(v2.mul_by_nonresidue())
let c2 = self.c0
.add(self.c2)
.mul(other.c0.add(other.c2))
.sub(v0)
.sub(v2)
.add(v1)
{ c0, c1, c2, }
}
///|
/// Chung-Hasan SQR2 squaring in F_{p^6}.
pub fn Fq6::square(self : Fq6) -> Fq6 {
let s0 = self.c0.square()
let ab = self.c0.mul(self.c1)
let s1 = ab.double()
let s2 = self.c0.sub(self.c1).add(self.c2).square()
let bc = self.c1.mul(self.c2)
let s3 = bc.double()
let s4 = self.c2.square()
let c0 = s3.mul_by_nonresidue().add(s0)
let c1 = s4.mul_by_nonresidue().add(s1)
let c2 = s1.add(s2).add(s3).sub(s0).sub(s4)
{ c0, c1, c2, }
}
///|
/// Sparse multiplication by (b0 + b1*v).
pub fn Fq6::mul_by_01(self : Fq6, b0 : Fq2, b1 : Fq2) -> Fq6 {
let a_a = self.c0.mul(b0)
let b_b = self.c1.mul(b1)
let t1 = self.c1.add(self.c2).mul(b1).sub(b_b).mul_by_nonresidue().add(a_a)
let t2 = self.c0.add(self.c1).mul(b0.add(b1)).sub(a_a).sub(b_b)
let t3 = self.c2.mul(b0).add(b_b)
{ c0: t1, c1: t2, c2: t3, }
}
///|
/// Sparse multiplication by (b1*v).
pub fn Fq6::mul_by_1(self : Fq6, b1 : Fq2) -> Fq6 {
{
c0: self.c2.mul(b1).mul_by_nonresidue(),
c1: self.c0.mul(b1),
c2: self.c1.mul(b1),
}
}
///|
/// Frobenius map x -> x^(p^k) for k in {1, 2, 3}.
pub fn Fq6::frobenius_map(self : Fq6, power : Int32) -> Fq6 {
let p = (power % 6 + 6) % 6
match p {
0 => self
1 =>
{
c0: self.c0.conjugate(),
c1: self.c1.conjugate().mul(fq6_frob1_c1),
c2: self.c2.conjugate().mul(fq6_frob1_c2),
}
2 =>
{
c0: self.c0,
c1: self.c1.mul_fp(fq6_frob2_c1),
c2: self.c2.mul_fp(fq6_frob2_c2),
}
3 =>
{
c0: self.c0.conjugate(),
c1: self.c1.conjugate().mul(fq6_frob3_c1),
c2: self.c2.conjugate().mul(fq6_frob3_c2),
}
_ => {
let mut acc = self
for _i = 0; _i < p; _i = _i + 1 {
acc = acc.frobenius_map(1)
}
acc
}
}
}
///|
/// Multiplicative inverse in F_{p^6}. Returns None for 0.
pub fn Fq6::inv(self : Fq6) -> Fq6? {
let c0 = self.c0.square().sub(self.c1.mul(self.c2).mul_by_nonresidue())
let c1 = self.c2.square().mul_by_nonresidue().sub(self.c0.mul(self.c1))
let c2 = self.c1.square().sub(self.c0.mul(self.c2))
let norm = self.c2
.mul(c1)
.add(self.c1.mul(c2))
.mul_by_nonresidue()
.add(self.c0.mul(c0))
match norm.inv() {
None => None
Some(t) => Some({ c0: c0.mul(t), c1: c1.mul(t), c2: c2.mul(t), })
}
}
///|
pub impl Add for Fq6 with fn add(self, other) {
self.add(other)
}
///|
pub impl Sub for Fq6 with fn sub(self, other) {
self.sub(other)
}
///|
pub impl Mul for Fq6 with fn mul(self, other) {
self.mul(other)
}
///|
pub impl Neg for Fq6 with fn neg(self) {
self.neg()
}