Further reorganization

This commit is contained in:
Sean Bowe 2016-08-02 09:17:38 -06:00
parent 04c0064a5c
commit 731860e477
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
5 changed files with 131 additions and 153 deletions

View File

@ -23,23 +23,23 @@ extern "C" void bnwrap_init() {
// Fr
extern "C" FieldT bnwrap_fr_from(const char *a) {
extern "C" FieldT bnwrap_Fr_from(const char *a) {
return FieldT(a);
}
extern "C" FieldT bnwrap_fr_add(const char *a, const char *b) {
extern "C" FieldT bnwrap_Fr_add(const char *a, const char *b) {
return *a + *b;
}
extern "C" FieldT bnwrap_fr_sub(const char *a, const char *b) {
extern "C" FieldT bnwrap_Fr_sub(const char *a, const char *b) {
return *a - *b;
}
extern "C" FieldT bnwrap_fr_mul(const char *a, const char *b) {
extern "C" FieldT bnwrap_Fr_mul(const char *a, const char *b) {
return *a * *b;
}
extern "C" FieldT bnwrap_fr_neg(const char *a) {
extern "C" FieldT bnwrap_Fr_neg(const char *a) {
return -(*a);
}

View File

@ -8,11 +8,11 @@ use std::ffi::CString;
pub struct Fr([u64; 4]);
extern "C" {
fn bnwrap_fr_from(s: *const c_char) -> Fr;
fn bnwrap_fr_add(a: *const Fr, b: *const Fr) -> Fr;
fn bnwrap_fr_mul(a: *const Fr, b: *const Fr) -> Fr;
fn bnwrap_fr_sub(a: *const Fr, b: *const Fr) -> Fr;
fn bnwrap_fr_neg(a: *const Fr) -> Fr;
fn bnwrap_Fr_from(s: *const c_char) -> Fr;
fn bnwrap_Fr_add(a: *const Fr, b: *const Fr) -> Fr;
fn bnwrap_Fr_mul(a: *const Fr, b: *const Fr) -> Fr;
fn bnwrap_Fr_sub(a: *const Fr, b: *const Fr) -> Fr;
fn bnwrap_Fr_neg(a: *const Fr) -> Fr;
}
impl Fr {
@ -34,7 +34,7 @@ impl Fr {
let s = CString::new(s).unwrap();
unsafe { bnwrap_fr_from(s.as_ptr()) }
unsafe { bnwrap_Fr_from(s.as_ptr()) }
}
}
@ -42,7 +42,7 @@ impl Add for Fr {
type Output = Fr;
fn add(self, other: Fr) -> Fr {
unsafe { bnwrap_fr_add(&self, &other) }
unsafe { bnwrap_Fr_add(&self, &other) }
}
}
@ -50,7 +50,7 @@ impl Mul for Fr {
type Output = Fr;
fn mul(self, other: Fr) -> Fr {
unsafe { bnwrap_fr_mul(&self, &other) }
unsafe { bnwrap_Fr_mul(&self, &other) }
}
}
@ -58,7 +58,7 @@ impl Sub for Fr {
type Output = Fr;
fn sub(self, other: Fr) -> Fr {
unsafe { bnwrap_fr_sub(&self, &other) }
unsafe { bnwrap_Fr_sub(&self, &other) }
}
}
@ -66,6 +66,6 @@ impl Neg for Fr {
type Output = Fr;
fn neg(self) -> Fr {
unsafe { bnwrap_fr_neg(&self) }
unsafe { bnwrap_Fr_neg(&self) }
}
}

View File

@ -1,3 +1,4 @@
use std::ops::{Add,Sub,Mul,Neg};
use super::{Fr,GroupElement};
#[derive(Copy, Clone)]
@ -22,6 +23,12 @@ extern "C" {
fn bnwrap_G1_scalarmul(p: *const G1, s: *const Fr) -> G1;
}
impl PartialEq for G1 {
fn eq(&self, other: &G1) -> bool {
unsafe { bnwrap_G1_is_equal(self, other) }
}
}
impl GroupElement for G1 {
fn zero() -> G1 {
unsafe { bnwrap_G1_zero() }
@ -31,10 +38,6 @@ impl GroupElement for G1 {
unsafe { bnwrap_G1_one() }
}
fn is_equal(&self, other: &Self) -> bool {
unsafe { bnwrap_G1_is_equal(self, other) }
}
fn random() -> G1 {
unsafe { bnwrap_G1_random() }
}
@ -42,20 +45,36 @@ impl GroupElement for G1 {
fn is_zero(&self) -> bool {
unsafe { bnwrap_G1_is_zero(self) }
}
}
fn arith_neg(&self) -> Self {
unsafe { bnwrap_G1_neg(self) }
}
impl Add for G1 {
type Output = G1;
fn arith_add(&self, other: &Self) -> Self {
unsafe { bnwrap_G1_add(self, other) }
}
fn arith_sub(&self, other: &Self) -> Self {
unsafe { bnwrap_G1_sub(self, other) }
}
fn arith_mul(&self, other: &Fr) -> Self {
unsafe { bnwrap_G1_scalarmul(self, other) }
fn add(self, other: G1) -> G1 {
unsafe { bnwrap_G1_add(&self, &other) }
}
}
impl Mul<Fr> for G1 {
type Output = G1;
fn mul(self, other: Fr) -> G1 {
unsafe { bnwrap_G1_scalarmul(&self, &other) }
}
}
impl Sub for G1 {
type Output = G1;
fn sub(self, other: G1) -> G1 {
unsafe { bnwrap_G1_sub(&self, &other) }
}
}
impl Neg for G1 {
type Output = G1;
fn neg(self) -> G1 {
unsafe { bnwrap_G1_neg(&self) }
}
}

View File

@ -1,3 +1,4 @@
use std::ops::{Add,Sub,Mul,Neg};
use super::{Fr,GroupElement};
#[derive(Copy, Clone)]
@ -22,6 +23,12 @@ extern "C" {
fn bnwrap_G2_scalarmul(p: *const G2, s: *const Fr) -> G2;
}
impl PartialEq for G2 {
fn eq(&self, other: &G2) -> bool {
unsafe { bnwrap_G2_is_equal(self, other) }
}
}
impl GroupElement for G2 {
fn zero() -> G2 {
unsafe { bnwrap_G2_zero() }
@ -31,10 +38,6 @@ impl GroupElement for G2 {
unsafe { bnwrap_G2_one() }
}
fn is_equal(&self, other: &Self) -> bool {
unsafe { bnwrap_G2_is_equal(self, other) }
}
fn random() -> G2 {
unsafe { bnwrap_G2_random() }
}
@ -42,20 +45,36 @@ impl GroupElement for G2 {
fn is_zero(&self) -> bool {
unsafe { bnwrap_G2_is_zero(self) }
}
}
fn arith_neg(&self) -> Self {
unsafe { bnwrap_G2_neg(self) }
}
impl Add for G2 {
type Output = G2;
fn arith_add(&self, other: &Self) -> Self {
unsafe { bnwrap_G2_add(self, other) }
}
fn arith_sub(&self, other: &Self) -> Self {
unsafe { bnwrap_G2_sub(self, other) }
}
fn arith_mul(&self, other: &Fr) -> Self {
unsafe { bnwrap_G2_scalarmul(self, other) }
fn add(self, other: G2) -> G2 {
unsafe { bnwrap_G2_add(&self, &other) }
}
}
impl Mul<Fr> for G2 {
type Output = G2;
fn mul(self, other: Fr) -> G2 {
unsafe { bnwrap_G2_scalarmul(&self, &other) }
}
}
impl Sub for G2 {
type Output = G2;
fn sub(self, other: G2) -> G2 {
unsafe { bnwrap_G2_sub(&self, &other) }
}
}
impl Neg for G2 {
type Output = G2;
fn neg(self) -> G2 {
unsafe { bnwrap_G2_neg(&self) }
}
}

View File

@ -8,9 +8,8 @@ mod gt;
pub use self::fr::Fr;
pub use self::gt::Gt;
pub type G1 = G<g1::G1>;
pub type G2 = G<g2::G2>;
pub use self::g1::G1;
pub use self::g2::G2;
extern "C" {
fn bnwrap_init();
@ -30,97 +29,28 @@ pub fn initialize() {
}
}
pub trait GroupElement: Sized + Copy + Clone {
pub trait GroupElement: Sized +
Copy +
Clone +
Mul<Fr, Output=Self> +
Add<Output=Self> +
Sub<Output=Self> +
Neg<Output=Self> +
PartialEq {
fn zero() -> Self;
fn one() -> Self;
fn random() -> Self;
fn is_equal(&self, other: &Self) -> bool;
fn is_zero(&self) -> bool;
fn arith_neg(&self) -> Self;
fn arith_add(&self, other: &Self) -> Self;
fn arith_sub(&self, other: &Self) -> Self;
fn arith_mul(&self, other: &Fr) -> Self;
}
#[derive(Copy, Clone)]
pub struct G<T: GroupElement>(T);
impl<T: GroupElement> G<T> {
pub fn zero() -> Self {
G(T::zero())
}
pub fn one() -> Self {
G(T::one())
}
pub fn random() -> Self {
G(T::random())
}
pub fn is_zero(&self) -> bool {
self.0.is_zero()
}
}
impl<T: GroupElement> PartialEq for G<T> {
fn eq(&self, other: &Self) -> bool {
self.0.is_equal(&other.0)
}
}
impl<T: GroupElement> Neg for G<T> {
type Output = G<T>;
fn neg(self) -> G<T> {
G(self.0.arith_neg())
}
}
impl<T: GroupElement> Add for G<T> {
type Output = G<T>;
fn add(self, other: G<T>) -> G<T> {
G(self.0.arith_add(&other.0))
}
}
impl<T: GroupElement> Sub for G<T> {
type Output = G<T>;
fn sub(self, other: G<T>) -> G<T> {
G(self.0.arith_sub(&other.0))
}
}
impl<T: GroupElement> Mul<Fr> for G<T> {
type Output = G<T>;
fn mul(self, other: Fr) -> G<T> {
G(self.0.arith_mul(&other))
}
}
mod test_groups {
use super::{G, Fr, g1, g2, initialize, GroupElement};
use super::{Fr, G1, G2, initialize, GroupElement};
fn test_allocations_and_moves<Group: GroupElement>() {
let a: Vec<G<Group>> = (0..100)
.map(|i| (G::one() * Fr::from_str(&format!("{}", i))))
.collect();
let b = a.into_iter().fold(G::zero(), |a, b| a + b);
assert!(b == G::one() * Fr::from_str("4950"));
}
fn test_associative<Group: GroupElement>() {
fn test_associative<G: GroupElement>() {
for _ in 0..50 {
let a = G::<Group>::random();
let b = G::<Group>::random();
let c = G::<Group>::random();
let a = G::random();
let b = G::random();
let c = G::random();
let x = (a + b) + c;
let y = (a + c) + b;
@ -129,11 +59,19 @@ mod test_groups {
}
}
fn test_scalar_mul<Group: GroupElement>() {
let r = G::<Group>::random();
fn test_primitives<G: GroupElement>() {
let a = G::zero();
let b = G::one();
assert_eq!(a.is_zero(), true);
assert_eq!(b.is_zero(), false);
}
fn test_scalar_mul<G: GroupElement>() {
let r = G::random();
let res = r * Fr::from_str("16");
let mut acc = G::<Group>::zero();
let mut acc = G::zero();
for _ in 0..16 {
acc = acc + r;
@ -142,16 +80,16 @@ mod test_groups {
assert!(acc == res);
}
fn test_addition<Group: GroupElement>() {
fn test_addition<G: GroupElement>() {
{
let a = G::<Group>::random();
let a = G::random();
let b = -(a);
let c = a + b;
assert!(c.is_zero());
}
{
let a = G::<Group>::random();
let a = G::random();
let b = -(a);
let c = a - b;
let d = a * Fr::from_str("2");
@ -160,33 +98,35 @@ mod test_groups {
}
}
fn test_primitives<Group: GroupElement>() {
let a = G::<Group>::zero();
let b = G::<Group>::one();
fn test_allocations_and_moves<G: GroupElement>() {
let a: Vec<G> = (0..100)
.map(|i| (G::one() * Fr::from_str(&format!("{}", i))))
.collect();
assert_eq!(a.is_zero(), true);
assert_eq!(b.is_zero(), false);
let b = a.iter().fold(G::zero(), |a, b| a + *b);
assert!(b == G::one() * Fr::from_str("4950"));
}
fn test_group_ops<Group: GroupElement>() {
test_associative::<Group>();
test_primitives::<Group>();
test_scalar_mul::<Group>();
test_addition::<Group>();
test_allocations_and_moves::<Group>();
fn test_group_ops<G: GroupElement>() {
test_associative::<G>();
test_primitives::<G>();
test_scalar_mul::<G>();
test_addition::<G>();
test_allocations_and_moves::<G>();
}
#[test]
fn test_g1() {
initialize();
test_group_ops::<g1::G1>();
test_group_ops::<G1>();
}
#[test]
fn test_g2() {
initialize();
test_group_ops::<g2::G2>();
test_group_ops::<G2>();
}
}