diff --git a/examples/joux.rs b/examples/joux.rs index ba1a6e0..88cb53b 100644 --- a/examples/joux.rs +++ b/examples/joux.rs @@ -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(); diff --git a/src/fields/fp.rs b/src/fields/fp.rs index 3c47b9f..b881f52 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -17,6 +17,7 @@ pub trait FpParams { fn one() -> U256; } +#[repr(C)] pub struct Fp(U256, PhantomData

); impl Copy for Fp

{ } impl Clone for Fp

{ diff --git a/src/fields/fq12.rs b/src/fields/fq12.rs index cb11e2e..d8d0dbc 100644 --- a/src/fields/fq12.rs +++ b/src/fields/fq12.rs @@ -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 diff --git a/src/fields/fq2.rs b/src/fields/fq2.rs index 958a53c..551842e 100644 --- a/src/fields/fq2.rs +++ b/src/fields/fq2.rs @@ -20,6 +20,7 @@ pub fn fq2_nonresidue() -> Fq2 { } #[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[repr(C)] pub struct Fq2 { c0: Fq, c1: Fq diff --git a/src/fields/fq6.rs b/src/fields/fq6.rs index 810e3da..39b91ed 100644 --- a/src/fields/fq6.rs +++ b/src/fields/fq6.rs @@ -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, diff --git a/src/groups/mod.rs b/src/groups/mod.rs index bdf5889..6fef217 100644 --- a/src/groups/mod.rs +++ b/src/groups/mod.rs @@ -32,6 +32,7 @@ pub trait GroupParams: Sized { fn coeff_b() -> Self::Base; } +#[repr(C)] pub struct G { x: P::Base, y: P::Base, diff --git a/src/lib.rs b/src/lib.rs index 255f122..c28f555 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { fields::Fr::from_str(s).map(|e| Fr(e)) } pub fn inverse(&self) -> Option { self.0.inverse().map(|e| Fr(e)) } + pub fn is_zero(&self) -> bool { self.0.is_zero() } } impl Add 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 + Sub + Neg + Mul { + fn zero() -> Self; + fn one() -> Self; + fn random(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(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(rng: &mut R) -> Self { G1(groups::G1::random(rng)) } + fn is_zero(&self) -> bool { self.0.is_zero() } } impl Add for G1 { @@ -82,12 +93,14 @@ impl Mul 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(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(rng: &mut R) -> Self { G2(groups::G2::random(rng)) } + fn is_zero(&self) -> bool { self.0.is_zero() } } impl Add for G2 { @@ -115,6 +128,7 @@ impl Mul for G2 { } #[derive(Copy, Clone, PartialEq, Eq)] +#[repr(C)] pub struct Gt(fields::Fq12); impl Gt {