Merge pull request #8 from ebfull/api-changes

Add `Group` trait, more methods to API, repr(C) everything.
This commit is contained in:
ebfull
2016-09-11 21:42:45 -06:00
committed by GitHub
9 changed files with 30 additions and 11 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "bn"
version = "0.2.0"
version = "0.2.1"
authors = ["Sean Bowe <ewillbefull@gmail.com>"]
description = "Pairing cryptography with the Barreto-Naehrig curve"
keywords = ["pairing","crypto","cryptography"]
+1 -1
View File
@@ -12,7 +12,7 @@ Add the `bn` crate to your dependencies in `Cargo.toml`...
```toml
[dependencies]
bn = "0.2.*"
bn = "0.2.1"
```
...and add an `extern crate` declaration to your crate root:
+1 -1
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();
+1
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> {
+1
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
+1
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
+1
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,
+1
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,
+22 -8
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 {