Add `Group` trait, more methods to API, repr(C) everything.

This commit is contained in:
Sean Bowe 2016-09-11 21:30:38 -06:00
parent f695ff29fd
commit 03521652c5
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
7 changed files with 28 additions and 9 deletions

View File

@ -1,6 +1,6 @@
extern crate bn;
extern crate rand;
use bn::{Fr, G1, G2, pairing};
use bn::{Group, Fr, G1, G2, pairing};
fn main() {
let rng = &mut rand::thread_rng();

View File

@ -17,6 +17,7 @@ pub trait FpParams {
fn one() -> U256;
}
#[repr(C)]
pub struct Fp<P: FpParams>(U256, PhantomData<P>);
impl<P: FpParams> Copy for Fp<P> { }
impl<P: FpParams> Clone for Fp<P> {

View File

@ -24,6 +24,7 @@ fn frobenius_coeffs_c1(power: usize) -> Fq2 {
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct Fq12 {
c0: Fq6,
c1: Fq6

View File

@ -20,6 +20,7 @@ pub fn fq2_nonresidue() -> Fq2 {
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct Fq2 {
c0: Fq,
c1: Fq

View File

@ -40,6 +40,7 @@ fn frobenius_coeffs_c2(n: usize) -> Fq2 {
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct Fq6 {
pub c0: Fq2,
pub c1: Fq2,

View File

@ -32,6 +32,7 @@ pub trait GroupParams: Sized {
fn coeff_b() -> Self::Base;
}
#[repr(C)]
pub struct G<P: GroupParams> {
x: P::Base,
y: P::Base,

View File

@ -13,6 +13,7 @@ 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 {
@ -22,6 +23,7 @@ impl Fr {
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 {
@ -48,13 +50,22 @@ impl Mul for 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 G1 {
pub fn zero() -> Self { G1(groups::G1::zero()) }
pub fn one() -> Self { G1(groups::G1::one()) }
pub fn random<R: Rng>(rng: &mut R) -> Self { G1(groups::G1::random(rng)) }
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 {
@ -82,12 +93,14 @@ impl Mul<Fr> for G1 {
}
#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[repr(C)]
pub struct G2(groups::G2);
impl G2 {
pub fn zero() -> Self { G2(groups::G2::zero()) }
pub fn one() -> Self { G2(groups::G2::one()) }
pub fn random<R: Rng>(rng: &mut R) -> Self { G2(groups::G2::random(rng)) }
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 {
@ -115,6 +128,7 @@ impl Mul<Fr> for G2 {
}
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct Gt(fields::Fq12);
impl Gt {