1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
extern crate rand;
extern crate rustc_serialize;
extern crate byteorder;

mod arith;
mod fields;
mod groups;

use fields::FieldElement;
use groups::GroupElement;

use std::ops::{Add, Sub, Mul, Neg};
use rand::Rng;

#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[repr(C)]
pub struct Fr(fields::Fr);

impl Fr {
    pub fn zero() -> Self { Fr(fields::Fr::zero()) }
    pub fn one() -> Self { Fr(fields::Fr::one()) }
    pub fn random<R: Rng>(rng: &mut R) -> Self { Fr(fields::Fr::random(rng)) }
    pub fn pow(&self, exp: Fr) -> Self { Fr(self.0.pow(exp.0)) }
    pub fn from_str(s: &str) -> Option<Self> { fields::Fr::from_str(s).map(|e| Fr(e)) }
    pub fn inverse(&self) -> Option<Self> { self.0.inverse().map(|e| Fr(e)) }
    pub fn is_zero(&self) -> bool { self.0.is_zero() }
}

impl Add<Fr> for Fr {
    type Output = Fr;

    fn add(self, other: Fr) -> Fr { Fr(self.0 + other.0) }
}

impl Sub<Fr> for Fr {
    type Output = Fr;

    fn sub(self, other: Fr) -> Fr { Fr(self.0 - other.0) }
}

impl Neg for Fr {
    type Output = Fr;

    fn neg(self) -> Fr { Fr(-self.0) }
}

impl Mul for Fr {
    type Output = Fr;

    fn mul(self, other: Fr) -> Fr { Fr(self.0 * other.0) }
}

pub trait Group: Copy + Clone + PartialEq + Eq + Sized + Add<Self> + Sub<Self> + Neg + Mul<Fr> {
    fn zero() -> Self;
    fn one() -> Self;
    fn random<R: Rng>(rng: &mut R) -> Self;
    fn is_zero(&self) -> bool;
}

#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[repr(C)]
pub struct G1(groups::G1);

impl Group for G1 {
    fn zero() -> Self { G1(groups::G1::zero()) }
    fn one() -> Self { G1(groups::G1::one()) }
    fn random<R: Rng>(rng: &mut R) -> Self { G1(groups::G1::random(rng)) }
    fn is_zero(&self) -> bool { self.0.is_zero() }
}

impl Add<G1> for G1 {
    type Output = G1;

    fn add(self, other: G1) -> G1 { G1(self.0 + other.0) }
}

impl Sub<G1> for G1 {
    type Output = G1;

    fn sub(self, other: G1) -> G1 { G1(self.0 - other.0) }
}

impl Neg for G1 {
    type Output = G1;

    fn neg(self) -> G1 { G1(-self.0) }
}

impl Mul<Fr> for G1 {
    type Output = G1;

    fn mul(self, other: Fr) -> G1 { G1(self.0 * other.0) }
}

#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[repr(C)]
pub struct G2(groups::G2);

impl Group for G2 {
    fn zero() -> Self { G2(groups::G2::zero()) }
    fn one() -> Self { G2(groups::G2::one()) }
    fn random<R: Rng>(rng: &mut R) -> Self { G2(groups::G2::random(rng)) }
    fn is_zero(&self) -> bool { self.0.is_zero() }
}

impl Add<G2> for G2 {
    type Output = G2;

    fn add(self, other: G2) -> G2 { G2(self.0 + other.0) }
}

impl Sub<G2> for G2 {
    type Output = G2;

    fn sub(self, other: G2) -> G2 { G2(self.0 - other.0) }
}

impl Neg for G2 {
    type Output = G2;

    fn neg(self) -> G2 { G2(-self.0) }
}

impl Mul<Fr> for G2 {
    type Output = G2;

    fn mul(self, other: Fr) -> G2 { G2(self.0 * other.0) }
}

#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct Gt(fields::Fq12);

impl Gt {
    pub fn one() -> Self { Gt(fields::Fq12::one()) }
    pub fn pow(&self, exp: Fr) -> Self { Gt(self.0.pow(exp.0)) }
    pub fn inverse(&self) -> Self { Gt(self.0.inverse().unwrap()) }
}

impl Mul<Gt> for Gt {
    type Output = Gt;

    fn mul(self, other: Gt) -> Gt { Gt(self.0 * other.0) }
}

pub fn pairing(p: G1, q: G2) -> Gt {
    Gt(groups::pairing(&p.0, &q.0))
}