mirror of
https://github.com/exfinen/zk-toolkit.git
synced 2026-01-09 12:07:57 -05:00
use GT for paring output
This commit is contained in:
39
src/building_block/curves/bls12_381/gt_point.rs
Normal file
39
src/building_block/curves/bls12_381/gt_point.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use crate::building_block::curves::bls12_381::fq12::Fq12;
|
||||
use std::ops::Mul;
|
||||
|
||||
pub struct GTPoint {
|
||||
e: Fq12,
|
||||
}
|
||||
|
||||
impl GTPoint {
|
||||
pub fn new(e: &Fq12) -> Self {
|
||||
GTPoint {
|
||||
e: e.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_mul {
|
||||
($rhs: ty, $target: ty) => {
|
||||
impl Mul<$rhs> for $target {
|
||||
type Output = GTPoint;
|
||||
|
||||
fn mul(self, rhs: $rhs) -> Self::Output {
|
||||
let e = &self.e * &rhs.e;
|
||||
GTPoint::new(&e)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
impl_mul!(GTPoint, GTPoint);
|
||||
impl_mul!(GTPoint, >Point);
|
||||
impl_mul!(>Point, GTPoint);
|
||||
impl_mul!(>Point, >Point);
|
||||
|
||||
impl PartialEq for GTPoint {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
&self.e == &other.e
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for GTPoint {}
|
||||
@@ -6,6 +6,7 @@ pub mod fq_test_helper;
|
||||
pub mod g1_point;
|
||||
pub mod g2_point;
|
||||
pub mod g12_point;
|
||||
pub mod gt_point;
|
||||
pub mod pairing;
|
||||
pub mod params;
|
||||
pub mod private_key;
|
||||
|
||||
@@ -2,6 +2,7 @@ use crate::building_block::{
|
||||
curves::bls12_381::{
|
||||
g1_point::G1Point,
|
||||
g2_point::G2Point,
|
||||
gt_point::GTPoint,
|
||||
fq12::Fq12,
|
||||
params::Params as P,
|
||||
rational_function::RationalFunction,
|
||||
@@ -71,17 +72,18 @@ impl Pairing {
|
||||
Pairing { l_bits }
|
||||
}
|
||||
|
||||
pub fn weil(&self, p1: &G1Point, p2: &G2Point) -> Fq12 {
|
||||
pub fn weil(&self, p1: &G1Point, p2: &G2Point) -> GTPoint {
|
||||
println!("Started Weil pairing");
|
||||
println!("Running Miller loop G1-G2...");
|
||||
|
||||
let num = self.calc_g1_g2(p1, p2);
|
||||
println!("Running Miller loop G2-G1...");
|
||||
let deno = self.calc_g2_g1(p2, p1);
|
||||
num * deno.inv()
|
||||
let e = num * deno.inv();
|
||||
GTPoint::new(&e)
|
||||
}
|
||||
|
||||
pub fn tate(&self, p1: &G1Point, p2: &G2Point) -> Fq12 {
|
||||
pub fn tate(&self, p1: &G1Point, p2: &G2Point) -> GTPoint {
|
||||
println!("Started Tate pairing");
|
||||
println!("Running Miller loop G1-G2...");
|
||||
|
||||
@@ -93,7 +95,8 @@ impl Pairing {
|
||||
let q_to_12 = P::base_prime_field().order_ref().pow(P::embedding_degree());
|
||||
let r = P::subgroup().order();
|
||||
let exp = (q_to_12 - one) / r;
|
||||
intmed.pow(&exp)
|
||||
let e = intmed.pow(&exp);
|
||||
GTPoint::new(&e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +106,7 @@ mod tests {
|
||||
|
||||
fn test(
|
||||
pairing: &Pairing,
|
||||
pair: &dyn Fn(&Pairing, &G1Point, &G2Point) -> Fq12,
|
||||
pair: &dyn Fn(&Pairing, &G1Point, &G2Point) -> GTPoint,
|
||||
p1: &G1Point,
|
||||
p2: &G2Point,
|
||||
) -> bool {
|
||||
@@ -120,7 +123,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn test_with_generators(
|
||||
pair: &dyn Fn(&Pairing, &G1Point, &G2Point) -> Fq12,
|
||||
pair: &dyn Fn(&Pairing, &G1Point, &G2Point) -> GTPoint,
|
||||
) {
|
||||
let pairing = &Pairing::new();
|
||||
let p1 = G1Point::g();
|
||||
@@ -130,7 +133,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn test_with_random_points(
|
||||
pair: &dyn Fn(&Pairing, &G1Point, &G2Point) -> Fq12,
|
||||
pair: &dyn Fn(&Pairing, &G1Point, &G2Point) -> GTPoint,
|
||||
) {
|
||||
let mut errors = 0;
|
||||
let num_tests = 1;
|
||||
@@ -147,7 +150,8 @@ mod tests {
|
||||
assert!(errors == 0);
|
||||
}
|
||||
|
||||
fn test_plus_to_mul(pair: &dyn Fn(&Pairing, &G1Point, &G2Point) -> Fq12,
|
||||
fn test_plus_to_mul(
|
||||
pair: &dyn Fn(&Pairing, &G1Point, &G2Point) -> GTPoint,
|
||||
) {
|
||||
let pairing = &Pairing::new();
|
||||
let one = &G2Point::g();
|
||||
|
||||
@@ -2,13 +2,8 @@ use mcl_rust::*;
|
||||
use std::{
|
||||
convert::From,
|
||||
fmt,
|
||||
ops::{Add,
|
||||
Sub,
|
||||
Mul,
|
||||
Neg,
|
||||
},
|
||||
ops::Mul,
|
||||
};
|
||||
use num_traits::Zero;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MclGT {
|
||||
@@ -26,22 +21,6 @@ impl MclGT {
|
||||
GT::inv(&mut v, &self.v);
|
||||
MclGT::from(&v)
|
||||
}
|
||||
|
||||
pub fn sq(&self) -> Self {
|
||||
let mut v = GT::zero();
|
||||
GT::sqr(&mut v, &self.v);
|
||||
MclGT::from(&v)
|
||||
}
|
||||
}
|
||||
|
||||
impl Zero for MclGT {
|
||||
fn is_zero(&self) -> bool {
|
||||
self.v.is_zero()
|
||||
}
|
||||
|
||||
fn zero() -> Self {
|
||||
MclGT::from(>::zero())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i32> for MclGT {
|
||||
@@ -71,58 +50,6 @@ impl fmt::Display for MclGT {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_neg {
|
||||
($target: ty) => {
|
||||
impl Neg for $target {
|
||||
type Output = MclGT;
|
||||
|
||||
fn neg(self) -> Self::Output {
|
||||
let mut v = GT::zero();
|
||||
GT::neg(&mut v, &self.v);
|
||||
MclGT::from(&v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl_neg!(MclGT);
|
||||
impl_neg!(&MclGT);
|
||||
|
||||
macro_rules! impl_add {
|
||||
($rhs: ty, $target: ty) => {
|
||||
impl Add<$rhs> for $target {
|
||||
type Output = MclGT;
|
||||
|
||||
fn add(self, rhs: $rhs) -> Self::Output {
|
||||
let mut v = GT::zero();
|
||||
GT::add(&mut v, &self.v, &rhs.v);
|
||||
MclGT::from(&v)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
impl_add!(MclGT, MclGT);
|
||||
impl_add!(&MclGT, MclGT);
|
||||
impl_add!(MclGT, &MclGT);
|
||||
impl_add!(&MclGT, &MclGT);
|
||||
|
||||
macro_rules! impl_sub {
|
||||
($rhs: ty, $target: ty) => {
|
||||
impl Sub<$rhs> for $target {
|
||||
type Output = MclGT;
|
||||
|
||||
fn sub(self, rhs: $rhs) -> Self::Output {
|
||||
let mut v = GT::zero();
|
||||
GT::sub(&mut v, &self.v, &rhs.v);
|
||||
MclGT::from(&v)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
impl_sub!(MclGT, MclGT);
|
||||
impl_sub!(&MclGT, MclGT);
|
||||
impl_sub!(MclGT, &MclGT);
|
||||
impl_sub!(&MclGT, &MclGT);
|
||||
|
||||
macro_rules! impl_mul {
|
||||
($rhs: ty, $target: ty) => {
|
||||
impl Mul<$rhs> for $target {
|
||||
@@ -146,28 +73,6 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::building_block::mcl::mcl_initializer::MclInitializer;
|
||||
|
||||
#[test]
|
||||
fn test_add() {
|
||||
MclInitializer::init();
|
||||
|
||||
let n3 = MclGT::from(3i32);
|
||||
let n9 = MclGT::from(9i32);
|
||||
let exp = MclGT::from(12i32);
|
||||
let act = n3 + n9;
|
||||
assert_eq!(exp, act);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sub() {
|
||||
MclInitializer::init();
|
||||
|
||||
let n9 = MclGT::from(9i32);
|
||||
let n3 = MclGT::from(3i32);
|
||||
let exp = MclGT::from(6i32);
|
||||
let act = n9 - n3;
|
||||
assert_eq!(exp, act);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mul() {
|
||||
MclInitializer::init();
|
||||
@@ -179,22 +84,14 @@ mod tests {
|
||||
assert_eq!(exp, act);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_inv() {
|
||||
// MclInitializer::init();
|
||||
//
|
||||
// let n1 = MclGT::from(1i32);
|
||||
// let n9 = MclGT::from(9i32);
|
||||
// let inv9 = n9.inv();
|
||||
//
|
||||
// assert_eq!(n9 * inv9, n1);
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn test_neg() {
|
||||
fn test_inv() {
|
||||
MclInitializer::init();
|
||||
|
||||
let n9 = &MclGT::from(9i32);
|
||||
assert_eq!(n9 + -n9, MclGT::zero());
|
||||
let n1 = MclGT::from(1i32);
|
||||
let n9 = MclGT::from(9i32);
|
||||
let inv9 = n9.inv();
|
||||
|
||||
assert_eq!(n9 * inv9, n1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::{
|
||||
curves::bls12_381::{
|
||||
g1_point::G1Point,
|
||||
g2_point::G2Point,
|
||||
fq12::Fq12,
|
||||
gt_point::GTPoint,
|
||||
pairing::Pairing,
|
||||
},
|
||||
field::prime_field::PrimeField,
|
||||
@@ -32,7 +32,7 @@ pub struct G2 {
|
||||
}
|
||||
|
||||
pub struct GT {
|
||||
pub alpha_beta: Fq12,
|
||||
pub alpha_beta: GTPoint,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
|
||||
Reference in New Issue
Block a user