Add `Gt` type to API.

This commit is contained in:
Sean Bowe 2016-07-04 09:18:27 -06:00
parent bb4b97ccec
commit 38f1f7d21d
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
3 changed files with 43 additions and 6 deletions

33
src/groups/gt.rs Normal file
View File

@ -0,0 +1,33 @@
use ::Fq12;
use ::Scalar;
use std::ops::{BitXor,Mul};
use fields::Field;
use std::cmp::{PartialEq, Eq};
#[derive(Debug,Eq,PartialEq)]
pub struct Gt(Fq12);
impl Gt {
pub fn new(a: Fq12) -> Gt {
return Gt(a);
}
}
impl<'a, 'b> Mul<&'a Gt> for &'b Gt {
type Output = Gt;
fn mul(self, other: &Gt) -> Gt {
Gt(&self.0 * &other.0)
}
}
impl<'a, 'b> BitXor<&'a Scalar> for &'b Gt {
type Output = Gt;
fn bitxor(self, other: &Scalar) -> Gt {
Gt(self.0.pow(other))
}
}
forward_all_binop_to_ref_ref!(impl() Mul for Gt, mul, Gt);
forward_all_binop_to_ref_ref!(impl() BitXor for Gt, bitxor, Scalar);

View File

@ -12,6 +12,9 @@ pub mod tests;
#[macro_use]
mod macros;
mod gt;
pub use self::gt::Gt;
pub trait GroupParams: Sized {
type Base: Field;

View File

@ -6,6 +6,7 @@ mod params;
mod groups;
pub use groups::Gt;
pub use fields::fp::Fp;
pub use fields::fp2::Fp2;
pub use fields::fp6::Fp6;
@ -347,11 +348,11 @@ pub fn final_exponentiation(elt: &Fq12) -> Fq12 {
final_exponentiation_last_chunk(&final_exponentiation_first_chunk(elt))
}
pub fn pairing(p: &G1, q: &G2) -> Fq12 {
pub fn pairing(p: &G1, q: &G2) -> Gt {
let p = p.to_affine();
let q = G2Precomp::new(q);
final_exponentiation(&miller_loop(&p, &q))
Gt::new(final_exponentiation(&miller_loop(&p, &q)))
}
#[test]
@ -361,7 +362,7 @@ fn test_reduced_pairing() {
let gt = pairing(&g1, &g2);
let expected = Fq12::new(
let expected = Gt::new(Fq12::new(
Fq6::new(
Fq2::new(Fq::from("7520311483001723614143802378045727372643587653754534704390832890681688842501"), Fq::from("20265650864814324826731498061022229653175757397078253377158157137251452249882")),
Fq2::new(Fq::from("11942254371042183455193243679791334797733902728447312943687767053513298221130"), Fq::from("759657045325139626991751731924144629256296901790485373000297868065176843620")),
@ -372,7 +373,7 @@ fn test_reduced_pairing() {
Fq2::new(Fq::from("17897835398184801202802503586172351707502775171934235751219763553166796820753"), Fq::from("1344517825169318161285758374052722008806261739116142912817807653057880346554")),
Fq2::new(Fq::from("11123896897251094532909582772961906225000817992624500900708432321664085800838"), Fq::from("17453370448280081813275586256976217762629631160552329276585874071364454854650"))
)
);
));
assert_eq!(expected, gt);
}
@ -390,7 +391,7 @@ fn test_binlinearity() {
let sp = &p * &s;
let sq = &q * &s;
let a = pairing(&p, &q).pow(&s);
let a = pairing(&p, &q) ^ &s;
let b = pairing(&sp, &q);
let c = pairing(&p, &sq);
@ -399,6 +400,6 @@ fn test_binlinearity() {
let t = Fr::zero().sub(&Fr::one());
assert_eq!(a.pow(&t).mul(&a), Fq12::one());
assert_eq!((&a ^ t) * &a, Gt::new(Fq12::one()));
}
}