mirror of
https://github.com/pezkuwichain/bizinikiwi-bn.git
synced 2026-06-13 22:11:12 +00:00
Pairing cryptography operations
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
use ::Fr;
|
||||
use ::Fq2;
|
||||
use ::Fq6;
|
||||
use ::Fq12;
|
||||
use fields::fp::PrimeFieldParams;
|
||||
use fields::fp6::Fp6Params;
|
||||
use params::{FrParams,Fq6Params};
|
||||
use rand::Rng;
|
||||
use fields::Field;
|
||||
use std::ops::{Mul,Add,Sub,Neg};
|
||||
@@ -10,6 +15,7 @@ use std::fmt;
|
||||
pub trait Fp12Params {
|
||||
fn non_residue() -> Fq2;
|
||||
fn name() -> &'static str;
|
||||
fn frobenius_coeffs_c1(n: usize) -> Fq2;
|
||||
}
|
||||
|
||||
pub struct Fp12<P: Fp12Params> {
|
||||
@@ -26,6 +32,167 @@ impl<P: Fp12Params> Fp12<P> {
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
pub fn frobenius_map(&self, power: usize) -> Self {
|
||||
Fp12 {
|
||||
a: self.a.frobenius_map(power),
|
||||
b: &self.b.frobenius_map(power) * &P::frobenius_coeffs_c1(power % 12),
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Fq12 {
|
||||
pub fn unitary_inverse(&self) -> Fq12 {
|
||||
Fp12 {
|
||||
a: self.a.clone(),
|
||||
b: -&self.b,
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cyclotomic_exp(&self, exp: &Fr) -> Self {
|
||||
let mut res = Self::one();
|
||||
|
||||
let mut found_one = false;
|
||||
|
||||
for i in (0..FrParams::bits()).rev() {
|
||||
if found_one {
|
||||
res = res.cyclotomic_squared();
|
||||
}
|
||||
|
||||
if exp.test_bit(i) {
|
||||
found_one = true;
|
||||
res = self * &res;
|
||||
}
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
pub fn cyclotomic_squared(&self) -> Self {
|
||||
let z0 = &self.a.a;
|
||||
let z4 = &self.a.b;
|
||||
let z3 = &self.a.c;
|
||||
let z2 = &self.b.a;
|
||||
let z1 = &self.b.b;
|
||||
let z5 = &self.b.c;
|
||||
|
||||
let tmp = z0 * z1;
|
||||
let t0 = (z0 + z1) * (z1 * Fq6Params::non_residue() + z0) - &tmp - &tmp * Fq6Params::non_residue();
|
||||
let t1 = &tmp + &tmp;
|
||||
|
||||
let tmp = z2 * z3;
|
||||
let t2 = (z2 + z3) * (z3 * Fq6Params::non_residue() + z2) - &tmp - &tmp * Fq6Params::non_residue();
|
||||
let t3 = &tmp + &tmp;
|
||||
|
||||
let tmp = z4 * z5;
|
||||
let t4 = (z4 + z5) * (z5 * Fq6Params::non_residue() + z4) - &tmp - &tmp * Fq6Params::non_residue();
|
||||
let t5 = &tmp + &tmp;
|
||||
|
||||
let z0 = &t0 - z0;
|
||||
let z0 = &z0 + &z0;
|
||||
let z0 = &z0 + &t0;
|
||||
|
||||
let z1 = &t1 + z1;
|
||||
let z1 = &z1 + &z1;
|
||||
let z1 = &z1 + &t1;
|
||||
|
||||
let tmp = &t5 * Fq6Params::non_residue();
|
||||
let z2 = &tmp + z2;
|
||||
let z2 = &z2 + &z2;
|
||||
let z2 = &z2 + &tmp;
|
||||
|
||||
let z3 = &t4 - z3;
|
||||
let z3 = &z3 + &z3;
|
||||
let z3 = &z3 + &t4;
|
||||
|
||||
let z4 = &t2 - z4;
|
||||
let z4 = &z4 + &z4;
|
||||
let z4 = &z4 + &t2;
|
||||
|
||||
let z5 = &t3 + z5;
|
||||
let z5 = &z5 + &z5;
|
||||
let z5 = &z5 + &t3;
|
||||
|
||||
Fp12 {
|
||||
a: Fq6::new(z0, z4, z3),
|
||||
b: Fq6::new(z2, z1, z5),
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mul_by_024(&self,
|
||||
ell_0: &Fq2,
|
||||
ell_VW: Fq2,
|
||||
ell_VV: Fq2) -> Fq12 {
|
||||
let z0 = &self.a.a;
|
||||
let z1 = &self.a.b;
|
||||
let z2 = &self.a.c;
|
||||
let z3 = &self.b.a;
|
||||
let z4 = &self.b.b;
|
||||
let z5 = &self.b.c;
|
||||
|
||||
let x0 = ell_0;
|
||||
let x2 = &ell_VV;
|
||||
let x4 = &ell_VW;
|
||||
|
||||
let d0 = z0 * x0;
|
||||
let d2 = z2 * x2;
|
||||
let d4 = z4 * x4;
|
||||
let t2 = z0 + z4;
|
||||
let t1 = z0 + z2;
|
||||
let s0 = z1 + z3 + z5;
|
||||
|
||||
let s1 = z1 * x2;
|
||||
let t3 = &s1 + &d4;
|
||||
let t4 = &t3 * Fq6Params::non_residue() + &d0;
|
||||
let z0 = t4;
|
||||
|
||||
let t3 = z5 * x4;
|
||||
let s1 = &s1 + &t3;
|
||||
let t3 = &t3 + &d2;
|
||||
let t4 = &t3 * Fq6Params::non_residue();
|
||||
let t3 = z1 * x0;
|
||||
let s1 = &s1 + &t3;
|
||||
let t4 = &t4 + &t3;
|
||||
let z1 = t4;
|
||||
|
||||
let t0 = x0 + x2;
|
||||
let t3 = &t1 * &t0 - &d0 - &d2;
|
||||
let t4 = z3 * x4;
|
||||
let s1 = &s1 + &t4;
|
||||
let t3 = &t3 + &t4;
|
||||
|
||||
let t0 = z2 + z4;
|
||||
let z2 = t3;
|
||||
|
||||
let t1 = x2 + x4;
|
||||
let t3 = &t0 * &t1 - &d2 - &d4;
|
||||
let t4 = &t3 * Fq6Params::non_residue();
|
||||
let t3 = z3 * x0;
|
||||
let s1 = &s1 + &t3;
|
||||
let t4 = &t4 + &t3;
|
||||
let z3 = t4;
|
||||
|
||||
let t3 = z5 * x2;
|
||||
let s1 = &s1 + &t3;
|
||||
let t4 = &t3 * Fq6Params::non_residue();
|
||||
let t0 = x0 + x4;
|
||||
let t3 = &t2 * &t0 - &d0 - &d4;
|
||||
let t4 = &t4 + &t3;
|
||||
let z4 = t4;
|
||||
|
||||
let t0 = x0 + x2 + x4;
|
||||
let t3 = &s0 * &t0 - &s1;
|
||||
let z5 = t3;
|
||||
|
||||
Fq12 {
|
||||
a: Fq6::new(z0, z1, z2),
|
||||
b: Fq6::new(z3, z4, z5),
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Fp12Params> fmt::Debug for Fp12<P> {
|
||||
|
||||
@@ -9,6 +9,7 @@ use std::fmt;
|
||||
pub trait Fp2Params {
|
||||
fn non_residue() -> Fq;
|
||||
fn name() -> &'static str;
|
||||
fn frobenius_coeffs_c1(usize) -> Fq;
|
||||
}
|
||||
|
||||
pub struct Fp2<P: Fp2Params> {
|
||||
@@ -25,6 +26,14 @@ impl<P: Fp2Params> Fp2<P> {
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
pub fn frobenius_map(&self, power: usize) -> Self {
|
||||
Fp2 {
|
||||
a: self.a.clone(),
|
||||
b: &self.b * P::frobenius_coeffs_c1(power % 2),
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Fp2Params> fmt::Debug for Fp2<P> {
|
||||
|
||||
+48
-3
@@ -9,12 +9,14 @@ use std::fmt;
|
||||
pub trait Fp6Params {
|
||||
fn non_residue() -> Fq2;
|
||||
fn name() -> &'static str;
|
||||
fn frobenius_coeffs_c1(usize) -> Fq2;
|
||||
fn frobenius_coeffs_c2(usize) -> Fq2;
|
||||
}
|
||||
|
||||
pub struct Fp6<P: Fp6Params> {
|
||||
a: Fq2,
|
||||
b: Fq2,
|
||||
c: Fq2,
|
||||
pub a: Fq2,
|
||||
pub b: Fq2,
|
||||
pub c: Fq2,
|
||||
_marker: PhantomData<P>
|
||||
}
|
||||
|
||||
@@ -27,6 +29,15 @@ impl<P: Fp6Params> Fp6<P> {
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
pub fn frobenius_map(&self, power: usize) -> Self {
|
||||
Fp6 {
|
||||
a: self.a.frobenius_map(power),
|
||||
b: self.b.frobenius_map(power) * P::frobenius_coeffs_c1(power % 6),
|
||||
c: self.c.frobenius_map(power) * P::frobenius_coeffs_c2(power % 6),
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Fp6Params> fmt::Debug for Fp6<P> {
|
||||
@@ -114,6 +125,27 @@ impl<P: Fp6Params> Field for Fp6<P> {
|
||||
}
|
||||
}
|
||||
|
||||
fn squared(&self) -> Self {
|
||||
let a = &self.a;
|
||||
let b = &self.b;
|
||||
let c = &self.c;
|
||||
|
||||
let s0 = a.squared();
|
||||
let ab = a * b;
|
||||
let s1 = &ab + &ab;
|
||||
let s2 = (a - b + c).squared();
|
||||
let bc = b * c;
|
||||
let s3 = &bc + &bc;
|
||||
let s4 = c.squared();
|
||||
|
||||
Fp6 {
|
||||
a: &s0 + &s3 * P::non_residue(),
|
||||
b: &s1 + &s4 * P::non_residue(),
|
||||
c: &s1 + &s2 + &s3 - &s0 - &s4,
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
fn sub(&self, other: &Self) -> Self {
|
||||
Fp6 {
|
||||
a: &self.a - &other.a,
|
||||
@@ -144,4 +176,17 @@ impl<P: Fp6Params> Fp6<P> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, P: Fp6Params> Mul<&'a Fq2> for &'b Fp6<P> {
|
||||
type Output = Fp6<P>;
|
||||
|
||||
fn mul(self, other: &Fq2) -> Fp6<P> {
|
||||
Fp6 {
|
||||
a: &self.a * other,
|
||||
b: &self.b * other,
|
||||
c: &self.c * other,
|
||||
_marker: PhantomData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
forward_ops_to_field_ops!(impl(P: Fp6Params) Fp6<P>);
|
||||
|
||||
Reference in New Issue
Block a user