Add `Group` trait, more methods to API, repr(C) everything.
This commit is contained in:
parent
f695ff29fd
commit
03521652c5
|
@ -1,6 +1,6 @@
|
||||||
extern crate bn;
|
extern crate bn;
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
use bn::{Fr, G1, G2, pairing};
|
use bn::{Group, Fr, G1, G2, pairing};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let rng = &mut rand::thread_rng();
|
let rng = &mut rand::thread_rng();
|
||||||
|
|
|
@ -17,6 +17,7 @@ pub trait FpParams {
|
||||||
fn one() -> U256;
|
fn one() -> U256;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
pub struct Fp<P: FpParams>(U256, PhantomData<P>);
|
pub struct Fp<P: FpParams>(U256, PhantomData<P>);
|
||||||
impl<P: FpParams> Copy for Fp<P> { }
|
impl<P: FpParams> Copy for Fp<P> { }
|
||||||
impl<P: FpParams> Clone for Fp<P> {
|
impl<P: FpParams> Clone for Fp<P> {
|
||||||
|
|
|
@ -24,6 +24,7 @@ fn frobenius_coeffs_c1(power: usize) -> Fq2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
|
#[repr(C)]
|
||||||
pub struct Fq12 {
|
pub struct Fq12 {
|
||||||
c0: Fq6,
|
c0: Fq6,
|
||||||
c1: Fq6
|
c1: Fq6
|
||||||
|
|
|
@ -20,6 +20,7 @@ pub fn fq2_nonresidue() -> Fq2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
|
#[repr(C)]
|
||||||
pub struct Fq2 {
|
pub struct Fq2 {
|
||||||
c0: Fq,
|
c0: Fq,
|
||||||
c1: Fq
|
c1: Fq
|
||||||
|
|
|
@ -40,6 +40,7 @@ fn frobenius_coeffs_c2(n: usize) -> Fq2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
|
#[repr(C)]
|
||||||
pub struct Fq6 {
|
pub struct Fq6 {
|
||||||
pub c0: Fq2,
|
pub c0: Fq2,
|
||||||
pub c1: Fq2,
|
pub c1: Fq2,
|
||||||
|
|
|
@ -32,6 +32,7 @@ pub trait GroupParams: Sized {
|
||||||
fn coeff_b() -> Self::Base;
|
fn coeff_b() -> Self::Base;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
pub struct G<P: GroupParams> {
|
pub struct G<P: GroupParams> {
|
||||||
x: P::Base,
|
x: P::Base,
|
||||||
y: P::Base,
|
y: P::Base,
|
||||||
|
|
30
src/lib.rs
30
src/lib.rs
|
@ -13,6 +13,7 @@ use std::ops::{Add, Sub, Mul, Neg};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
|
#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
|
||||||
|
#[repr(C)]
|
||||||
pub struct Fr(fields::Fr);
|
pub struct Fr(fields::Fr);
|
||||||
|
|
||||||
impl Fr {
|
impl Fr {
|
||||||
|
@ -22,6 +23,7 @@ impl Fr {
|
||||||
pub fn pow(&self, exp: Fr) -> Self { Fr(self.0.pow(exp.0)) }
|
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 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 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 {
|
impl Add<Fr> for Fr {
|
||||||
|
@ -48,13 +50,22 @@ impl Mul for Fr {
|
||||||
fn mul(self, other: Fr) -> Fr { Fr(self.0 * other.0) }
|
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)]
|
#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
|
||||||
|
#[repr(C)]
|
||||||
pub struct G1(groups::G1);
|
pub struct G1(groups::G1);
|
||||||
|
|
||||||
impl G1 {
|
impl Group for G1 {
|
||||||
pub fn zero() -> Self { G1(groups::G1::zero()) }
|
fn zero() -> Self { G1(groups::G1::zero()) }
|
||||||
pub fn one() -> Self { G1(groups::G1::one()) }
|
fn one() -> Self { G1(groups::G1::one()) }
|
||||||
pub fn random<R: Rng>(rng: &mut R) -> Self { G1(groups::G1::random(rng)) }
|
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 {
|
impl Add<G1> for G1 {
|
||||||
|
@ -82,12 +93,14 @@ impl Mul<Fr> for G1 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
|
#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
|
||||||
|
#[repr(C)]
|
||||||
pub struct G2(groups::G2);
|
pub struct G2(groups::G2);
|
||||||
|
|
||||||
impl G2 {
|
impl Group for G2 {
|
||||||
pub fn zero() -> Self { G2(groups::G2::zero()) }
|
fn zero() -> Self { G2(groups::G2::zero()) }
|
||||||
pub fn one() -> Self { G2(groups::G2::one()) }
|
fn one() -> Self { G2(groups::G2::one()) }
|
||||||
pub fn random<R: Rng>(rng: &mut R) -> Self { G2(groups::G2::random(rng)) }
|
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 {
|
impl Add<G2> for G2 {
|
||||||
|
@ -115,6 +128,7 @@ impl Mul<Fr> for G2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
|
#[repr(C)]
|
||||||
pub struct Gt(fields::Fq12);
|
pub struct Gt(fields::Fq12);
|
||||||
|
|
||||||
impl Gt {
|
impl Gt {
|
||||||
|
|
Loading…
Reference in New Issue