From 43584c4c0350e099c7530d10eaf095d52ed94521 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 26 Mar 2018 12:41:49 +0200 Subject: [PATCH 1/2] Make rustc-serialize optional --- .travis.yml | 1 + Cargo.toml | 7 +++++-- src/arith.rs | 5 +++++ src/fields/fp.rs | 3 +++ src/fields/fq2.rs | 3 +++ src/groups/mod.rs | 8 ++++++++ src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++------ tests/serialization.rs | 2 ++ 8 files changed, 63 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4d15cb9d..b93286b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,3 +2,4 @@ language: rust script: - cargo test --release + - cargo test --release --no-default-features diff --git a/Cargo.toml b/Cargo.toml index 21162b08..225504ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/arith.rs b/src/arith.rs index 89d09f30..b1fecf2a 100644 --- a/src/arith.rs +++ b/src/arith.rs @@ -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(&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: &mut S) -> Result { let mut buf = [0; (8 * 8)]; @@ -125,6 +128,7 @@ impl Decodable for U512 { } } +#[cfg(feature = "rustc-serialize")] impl Encodable for U256 { fn encode(&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: &mut S) -> Result { let mut buf = [0; (4 * 8)]; diff --git a/src/fields/fp.rs b/src/fields/fp.rs index 625be0c0..0e093edf 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -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(&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: &mut S) -> Result<$name, S::Error> { $name::new(try!(U256::decode(s))).ok_or_else(|| s.error("integer is not less than modulus")) diff --git a/src/fields/fq2.rs b/src/fields/fq2.rs index 73eb1751..d99bda7b 100644 --- a/src/fields/fq2.rs +++ b/src/fields/fq2.rs @@ -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(&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: &mut S) -> Result { let combined = try!(U512::decode(s)); diff --git a/src/groups/mod.rs b/src/groups/mod.rs index dc8da3b8..74b5eecc 100644 --- a/src/groups/mod.rs +++ b/src/groups/mod.rs @@ -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; @@ -223,6 +227,7 @@ impl AffineG

{ } } +#[cfg(feature = "rustc-serialize")] impl Encodable for G

{ fn encode(&self, s: &mut S) -> Result<(), S::Error> { if self.is_zero() { @@ -236,6 +241,7 @@ impl Encodable for G

{ } } +#[cfg(feature = "rustc-serialize")] impl Encodable for AffineG

{ fn encode(&self, s: &mut S) -> Result<(), S::Error> { try!(self.x.encode(s)); @@ -245,6 +251,7 @@ impl Encodable for AffineG

{ } } +#[cfg(feature = "rustc-serialize")] impl Decodable for G

{ fn decode(s: &mut S) -> Result, S::Error> { let l = try!(u8::decode(s)); @@ -258,6 +265,7 @@ impl Decodable for G

{ } } +#[cfg(feature = "rustc-serialize")] impl Decodable for AffineG

{ fn decode(s: &mut S) -> Result, S::Error> { let x = try!(P::Base::decode(s)); diff --git a/src/lib.rs b/src/lib.rs index 4f6036ca..f48a4dd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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,6 +147,7 @@ impl Fq2 { } } +#[cfg(feature = "rustc-serialize")] pub trait Group: rustc_serialize::Encodable + rustc_serialize::Decodable + @@ -167,7 +171,30 @@ pub trait Group: fn normalize(&mut self); } -#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)] +#[cfg(not(feature = "rustc-serialize"))] +pub trait Group: + 'static + + Send + + Sync + + Copy + + Clone + + PartialEq + + Eq + + Sized + + Add + + Sub + + Neg + + Mul +{ + fn zero() -> Self; + fn one() -> Self; + fn random(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 +267,8 @@ impl Mul 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 +304,8 @@ impl From 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 +398,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); diff --git a/tests/serialization.rs b/tests/serialization.rs index 88c928e2..801306e1 100644 --- a/tests/serialization.rs +++ b/tests/serialization.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "rustc-serialize")] + extern crate rand; extern crate bn; extern crate bincode; From 86f751ed0b5eb13691c50cbbdc2633760fc2a1f2 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 26 Mar 2018 15:20:59 +0200 Subject: [PATCH 2/2] Remove 'static requirement --- src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f48a4dd7..d583e02d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -151,7 +151,6 @@ impl Fq2 { pub trait Group: rustc_serialize::Encodable + rustc_serialize::Decodable + - 'static + Send + Sync + Copy + @@ -173,7 +172,6 @@ pub trait Group: #[cfg(not(feature = "rustc-serialize"))] pub trait Group: - 'static + Send + Sync + Copy +