Merge pull request #7 from tomaka/rustc-ser-opt

Make rustc-serialize optional
This commit is contained in:
Pierre Krieger
2018-03-26 15:38:13 +02:00
committed by GitHub
8 changed files with 62 additions and 9 deletions
+1
View File
@@ -2,3 +2,4 @@ language: rust
script:
- cargo test --release
- cargo test --release --no-default-features
+5 -2
View File
@@ -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]
+5
View File
@@ -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};
@@ -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)];
@@ -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)];
@@ -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)];
@@ -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)];
+3
View File
@@ -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};
@@ -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);
@@ -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"))
+3
View File
@@ -4,6 +4,7 @@ use rand::Rng;
use arith::{U256, U512};
#[cfg(feature = "rustc-serialize")]
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
#[inline]
@@ -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();
@@ -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));
+8
View File
@@ -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 +
@@ -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>;
@@ -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() {
@@ -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));
@@ -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));
@@ -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));
+35 -7
View File
@@ -1,4 +1,5 @@
extern crate rand;
#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize;
extern crate byteorder;
@@ -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);
@@ -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);
@@ -144,10 +147,10 @@ impl Fq2 {
}
}
#[cfg(feature = "rustc-serialize")]
pub trait Group:
rustc_serialize::Encodable +
rustc_serialize::Decodable +
'static +
Send +
Sync +
Copy +
@@ -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);
@@ -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);
@@ -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);
@@ -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);
+2
View File
@@ -1,3 +1,5 @@
#![cfg(feature = "rustc-serialize")]
extern crate rand;
extern crate bn;
extern crate bincode;