Skip to content

Commit

Permalink
Merge pull request #7 from tomaka/rustc-ser-opt
Browse files Browse the repository at this point in the history
Make rustc-serialize optional
  • Loading branch information
tomaka authored Mar 26, 2018
2 parents f3936c0 + 86f751e commit 964b48f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ language: rust

script:
- cargo test --release
- cargo test --release --no-default-features
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ homepage = "https://github.com/zcash/bn"
repository = "https://github.com/zcash/bn"
license = "MIT OR Apache-2.0"

[features]
default = ["rustc-serialize"]

[[bench]]
name = "api"

[dependencies]
rand = "0.3"
rustc-serialize = "0.3"
rand = "0.4"
rustc-serialize = { version = "0.3", optional = true }
byteorder = "1.0"

[dev-dependencies.bincode]
Expand Down
5 changes: 5 additions & 0 deletions src/arith.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cmp::Ordering;
use rand::Rng;

#[cfg(feature = "rustc-serialize")]
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
use byteorder::{ByteOrder, BigEndian};

Expand Down Expand Up @@ -97,6 +98,7 @@ impl U512 {
}
}

#[cfg(feature = "rustc-serialize")]
impl Encodable for U512 {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let mut buf = [0; (8 * 8)];
Expand All @@ -113,6 +115,7 @@ impl Encodable for U512 {
}
}

#[cfg(feature = "rustc-serialize")]
impl Decodable for U512 {
fn decode<S: Decoder>(s: &mut S) -> Result<U512, S::Error> {
let mut buf = [0; (8 * 8)];
Expand All @@ -125,6 +128,7 @@ impl Decodable for U512 {
}
}

#[cfg(feature = "rustc-serialize")]
impl Encodable for U256 {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let mut buf = [0; (4 * 8)];
Expand All @@ -141,6 +145,7 @@ impl Encodable for U256 {
}
}

#[cfg(feature = "rustc-serialize")]
impl Decodable for U256 {
fn decode<S: Decoder>(s: &mut S) -> Result<U256, S::Error> {
let mut buf = [0; (4 * 8)];
Expand Down
3 changes: 3 additions & 0 deletions src/fields/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rand::Rng;
use std::ops::{Add, Sub, Mul, Neg};
use super::FieldElement;

#[cfg(feature = "rustc-serialize")]
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};

use arith::{U512, U256};
Expand All @@ -21,6 +22,7 @@ macro_rules! field_impl {
}
}

#[cfg(feature = "rustc-serialize")]
impl Encodable for $name {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let normalized = U256::from(*self);
Expand All @@ -29,6 +31,7 @@ macro_rules! field_impl {
}
}

#[cfg(feature = "rustc-serialize")]
impl Decodable for $name {
fn decode<S: Decoder>(s: &mut S) -> Result<$name, S::Error> {
$name::new(try!(U256::decode(s))).ok_or_else(|| s.error("integer is not less than modulus"))
Expand Down
3 changes: 3 additions & 0 deletions src/fields/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rand::Rng;

use arith::{U256, U512};

#[cfg(feature = "rustc-serialize")]
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};

#[inline]
Expand All @@ -28,6 +29,7 @@ pub struct Fq2 {
c1: Fq
}

#[cfg(feature = "rustc-serialize")]
impl Encodable for Fq2 {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let c0: U256 = self.c0.into();
Expand All @@ -37,6 +39,7 @@ impl Encodable for Fq2 {
}
}

#[cfg(feature = "rustc-serialize")]
impl Decodable for Fq2 {
fn decode<S: Decoder>(s: &mut S) -> Result<Fq2, S::Error> {
let combined = try!(U512::decode(s));
Expand Down
8 changes: 8 additions & 0 deletions src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use arith::U256;
use std::fmt;
use rand::Rng;

#[cfg(feature = "rustc-serialize")]
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};

pub trait GroupElement: Sized +
Expand All @@ -25,7 +26,10 @@ pub trait GroupElement: Sized +
}

pub trait GroupParams: Sized {
#[cfg(feature = "rustc-serialize")]
type Base: FieldElement + Decodable + Encodable;
#[cfg(not(feature = "rustc-serialize"))]
type Base: FieldElement;

fn name() -> &'static str;
fn one() -> G<Self>;
Expand Down Expand Up @@ -223,6 +227,7 @@ impl<P: GroupParams> AffineG<P> {
}
}

#[cfg(feature = "rustc-serialize")]
impl<P: GroupParams> Encodable for G<P> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
if self.is_zero() {
Expand All @@ -236,6 +241,7 @@ impl<P: GroupParams> Encodable for G<P> {
}
}

#[cfg(feature = "rustc-serialize")]
impl<P: GroupParams> Encodable for AffineG<P> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
try!(self.x.encode(s));
Expand All @@ -245,6 +251,7 @@ impl<P: GroupParams> Encodable for AffineG<P> {
}
}

#[cfg(feature = "rustc-serialize")]
impl<P: GroupParams> Decodable for G<P> {
fn decode<S: Decoder>(s: &mut S) -> Result<G<P>, S::Error> {
let l = try!(u8::decode(s));
Expand All @@ -258,6 +265,7 @@ impl<P: GroupParams> Decodable for G<P> {
}
}

#[cfg(feature = "rustc-serialize")]
impl<P: GroupParams> Decodable for AffineG<P> {
fn decode<S: Decoder>(s: &mut S) -> Result<AffineG<P>, S::Error> {
let x = try!(P::Base::decode(s));
Expand Down
42 changes: 35 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate rand;
#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize;
extern crate byteorder;

Expand All @@ -12,7 +13,8 @@ use groups::GroupElement;
use std::ops::{Add, Sub, Mul, Neg};
use rand::Rng;

#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct Fr(fields::Fr);

Expand Down Expand Up @@ -70,7 +72,8 @@ pub enum FieldError {

pub use groups::Error as GroupError;

#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct Fq(fields::Fq);

Expand Down Expand Up @@ -144,10 +147,10 @@ impl Fq2 {
}
}

#[cfg(feature = "rustc-serialize")]
pub trait Group:
rustc_serialize::Encodable +
rustc_serialize::Decodable +
'static +
Send +
Sync +
Copy +
Expand All @@ -167,7 +170,29 @@ pub trait Group:
fn normalize(&mut self);
}

#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[cfg(not(feature = "rustc-serialize"))]
pub trait Group:
Send +
Sync +
Copy +
Clone +
PartialEq +
Eq +
Sized +
Add<Self, Output=Self> +
Sub<Self, Output=Self> +
Neg<Output=Self> +
Mul<Fr, Output=Self>
{
fn zero() -> Self;
fn one() -> Self;
fn random<R: Rng>(rng: &mut R) -> Self;
fn is_zero(&self) -> bool;
fn normalize(&mut self);
}

#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct G1(groups::G1);

Expand Down Expand Up @@ -240,7 +265,8 @@ impl Mul<Fr> for G1 {
fn mul(self, other: Fr) -> G1 { G1(self.0 * other.0) }
}

#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct AffineG1(groups::AffineG1);

Expand Down Expand Up @@ -276,7 +302,8 @@ impl From<AffineG1> for G1 {
}
}

#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct G2(groups::G2);

Expand Down Expand Up @@ -369,7 +396,8 @@ pub fn pairing(p: G1, q: G2) -> Gt {
Gt(groups::pairing(&p.0, &q.0))
}

#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)]
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct AffineG2(groups::AffineG2);

Expand Down
2 changes: 2 additions & 0 deletions tests/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "rustc-serialize")]

extern crate rand;
extern crate bn;
extern crate bincode;
Expand Down

0 comments on commit 964b48f

Please sign in to comment.