2016-06-28 22:50:38 -07:00
|
|
|
extern crate rand;
|
2016-09-07 00:20:21 -07:00
|
|
|
extern crate rustc_serialize;
|
|
|
|
extern crate byteorder;
|
2016-06-28 22:50:38 -07:00
|
|
|
|
2016-08-28 10:30:40 -07:00
|
|
|
mod arith;
|
2016-07-01 12:50:55 -07:00
|
|
|
mod fields;
|
2016-08-28 21:04:46 -07:00
|
|
|
mod groups;
|
2016-07-01 20:41:47 -07:00
|
|
|
|
2016-09-11 01:19:11 -07:00
|
|
|
use fields::FieldElement;
|
|
|
|
use groups::GroupElement;
|
|
|
|
|
|
|
|
use std::ops::{Add, Sub, Mul, Neg};
|
|
|
|
use rand::Rng;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
|
2016-09-11 20:30:38 -07:00
|
|
|
#[repr(C)]
|
2016-09-11 01:19:11 -07:00
|
|
|
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)) }
|
2016-09-11 20:30:38 -07:00
|
|
|
pub fn is_zero(&self) -> bool { self.0.is_zero() }
|
2016-09-18 10:22:53 -07:00
|
|
|
pub fn interpret(buf: &[u8; 64]) -> Fr {
|
|
|
|
Fr(fields::Fr::interpret(buf))
|
|
|
|
}
|
2016-09-11 01:19:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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) }
|
|
|
|
}
|
|
|
|
|
2016-09-12 13:02:21 -07:00
|
|
|
pub trait Group:
|
|
|
|
rustc_serialize::Encodable +
|
|
|
|
rustc_serialize::Decodable +
|
|
|
|
'static +
|
|
|
|
Send +
|
2016-09-14 13:38:17 -07:00
|
|
|
Sync +
|
2016-09-12 13:02:21 -07:00
|
|
|
Copy +
|
|
|
|
Clone +
|
|
|
|
PartialEq +
|
|
|
|
Eq +
|
|
|
|
Sized +
|
|
|
|
Add<Self, Output=Self> +
|
|
|
|
Sub<Self, Output=Self> +
|
|
|
|
Neg<Output=Self> +
|
|
|
|
Mul<Fr, Output=Self>
|
|
|
|
{
|
2016-09-11 20:30:38 -07:00
|
|
|
fn zero() -> Self;
|
|
|
|
fn one() -> Self;
|
|
|
|
fn random<R: Rng>(rng: &mut R) -> Self;
|
|
|
|
fn is_zero(&self) -> bool;
|
2016-10-14 11:24:16 -07:00
|
|
|
fn normalize(&mut self);
|
2016-09-11 20:30:38 -07:00
|
|
|
}
|
|
|
|
|
2016-09-11 01:19:11 -07:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
|
2016-09-11 20:30:38 -07:00
|
|
|
#[repr(C)]
|
2016-09-11 01:19:11 -07:00
|
|
|
pub struct G1(groups::G1);
|
|
|
|
|
2016-09-11 20:30:38 -07:00
|
|
|
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() }
|
2016-10-14 11:24:16 -07:00
|
|
|
fn normalize(&mut self) {
|
|
|
|
let new = match self.0.to_affine() {
|
|
|
|
Some(a) => a,
|
|
|
|
None => return
|
|
|
|
};
|
|
|
|
|
|
|
|
self.0 = new.to_jacobian();
|
|
|
|
}
|
2016-09-11 01:19:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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)]
|
2016-09-11 20:30:38 -07:00
|
|
|
#[repr(C)]
|
2016-09-11 01:19:11 -07:00
|
|
|
pub struct G2(groups::G2);
|
|
|
|
|
2016-09-11 20:30:38 -07:00
|
|
|
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() }
|
2016-10-14 11:24:16 -07:00
|
|
|
fn normalize(&mut self) {
|
|
|
|
let new = match self.0.to_affine() {
|
|
|
|
Some(a) => a,
|
|
|
|
None => return
|
|
|
|
};
|
|
|
|
|
|
|
|
self.0 = new.to_jacobian();
|
|
|
|
}
|
2016-09-11 01:19:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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) }
|
|
|
|
}
|
|
|
|
|
2018-06-07 23:09:21 -07:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)]
|
2016-09-11 20:30:38 -07:00
|
|
|
#[repr(C)]
|
2016-09-11 01:19:11 -07:00
|
|
|
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()) }
|
|
|
|
}
|
|
|
|
|
2018-06-07 23:09:21 -07:00
|
|
|
pub trait SerializableGt:
|
|
|
|
rustc_serialize::Encodable +
|
|
|
|
rustc_serialize::Decodable +
|
|
|
|
'static +
|
|
|
|
Copy +
|
|
|
|
Clone +
|
|
|
|
PartialEq +
|
|
|
|
Eq
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializableGt for Gt {}
|
|
|
|
|
2016-09-11 01:19:11 -07:00
|
|
|
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))
|
2016-09-10 11:21:32 -07:00
|
|
|
}
|