remove rustc-serialize feature and bump version (#22)

* remove rustc-serialize

* bump version to 0.6.0

* remove bincode
This commit is contained in:
Andronik Ordian
2021-04-21 17:06:02 +02:00
committed by GitHub
parent 47d6e4f919
commit 63f8c58735
8 changed files with 8 additions and 30366 deletions
+2 -7
View File
@@ -1,6 +1,6 @@
[package]
name = "substrate-bn"
version = "0.5.0"
version = "0.6.0"
authors = ["Sean Bowe <ewillbefull@gmail.com>", "Parity Technologies <admin@parity.io>"]
description = "Pairing cryptography with the Barreto-Naehrig curve"
keywords = ["pairing","crypto","cryptography"]
@@ -11,14 +11,13 @@ license = "MIT OR Apache-2.0"
edition = "2018"
[features]
default = ["rustc-serialize"]
default = []
[[bench]]
name = "api"
[dependencies]
rand = { version = "0.8.3", default-features = false }
rustc-serialize = { version = "0.3", optional = true }
byteorder = { version = "1.0", features = ["i128"], default-features = false }
crunchy = "0.2.1"
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
@@ -27,7 +26,3 @@ rustc-hex = { version = "2", default-features = false }
[dev-dependencies]
rand = { version = "0.8.3", features = ["std_rng"] }
[dev-dependencies.bincode]
version = "0.6"
default-features = false
features = ["rustc-serialize"]
-38
View File
@@ -1,8 +1,6 @@
#![feature(test)]
use substrate_bn::*;
use bincode::SizeLimit::Infinite;
use bincode::rustc_serialize::{decode, encode};
const SAMPLES: usize = 30;
@@ -26,42 +24,6 @@ macro_rules! benchmark(
)
);
benchmark!(g1_serialization,
input(rng) = G1::random(rng);
encode(input, Infinite).unwrap()
);
benchmark!(g1_serialization_normalized,
input(rng) = {let mut tmp = G1::random(rng); tmp.normalize(); tmp};
encode(input, Infinite).unwrap()
);
benchmark!(g2_serialization,
input(rng) = G2::random(rng);
encode(input, Infinite).unwrap()
);
benchmark!(g2_serialization_normalized,
input(rng) = {let mut tmp = G2::random(rng); tmp.normalize(); tmp};
encode(input, Infinite).unwrap()
);
benchmark!(g1_deserialization,
input(rng) = {encode(&G1::random(rng), Infinite).unwrap()};
decode::<G1>(input).unwrap()
);
benchmark!(g2_deserialization,
input(rng) = {encode(&G2::random(rng), Infinite).unwrap()};
decode::<G2>(input).unwrap()
);
benchmark!(fr_addition,
input(rng) = (Fr::random(rng), Fr::random(rng));
+1 -62
View File
@@ -1,8 +1,7 @@
use core::cmp::Ordering;
use rand::Rng;
use crunchy::unroll;
#[cfg(feature = "rustc-serialize")]
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use byteorder::{BigEndian, ByteOrder};
/// 256-bit, stack allocated biginteger for use in prime field
@@ -148,66 +147,6 @@ 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; 4 * 16];
for (l, i) in (0..4).rev().zip((0..4).map(|i| i * 16)) {
BigEndian::write_u128(&mut buf[i..], self.0[l]);
}
for i in 0..(4 * 16) {
s.emit_u8(buf[i])?;
}
Ok(())
}
}
#[cfg(feature = "rustc-serialize")]
impl Decodable for U512 {
fn decode<S: Decoder>(s: &mut S) -> Result<U512, S::Error> {
let mut buf = [0; 4 * 16];
for i in 0..(4 * 16) {
buf[i] = s.read_u8()?;
}
Ok(U512::interpret(&buf))
}
}
#[cfg(feature = "rustc-serialize")]
impl Encodable for U256 {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let mut buf = [0; 2 * 16];
for (l, i) in (0..2).rev().zip((0..2).map(|i| i * 16)) {
BigEndian::write_u128(&mut buf[i..], self.0[l]);
}
for i in 0..(2 * 16) {
s.emit_u8(buf[i])?;
}
Ok(())
}
}
#[cfg(feature = "rustc-serialize")]
impl Decodable for U256 {
fn decode<S: Decoder>(s: &mut S) -> Result<U256, S::Error> {
let mut buf = [0; 2 * 16];
for i in 0..(2 * 16) {
buf[i] = s.read_u8()?;
}
U256::from_slice(&buf).map_err(|_| s.error("Invalid input length; Also unreachable;"))
}
}
impl Ord for U512 {
#[inline]
fn cmp(&self, other: &U512) -> Ordering {
+1 -20
View File
@@ -4,9 +4,6 @@ use rand::Rng;
use crate::fields::FieldElement;
use crate::arith::{U256, U512};
#[cfg(feature = "rustc-serialize")]
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
macro_rules! field_impl {
($name:ident, $modulus:expr, $rsquared:expr, $rcubed:expr, $one:expr, $inv:expr) => {
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -22,22 +19,6 @@ 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);
normalized.encode(s)
}
}
#[cfg(feature = "rustc-serialize")]
impl Decodable for $name {
fn decode<S: Decoder>(s: &mut S) -> Result<$name, S::Error> {
$name::new(U256::decode(s)?).ok_or_else(|| s.error("integer is not less than modulus"))
}
}
impl $name {
pub fn from_str(s: &str) -> Option<Self> {
let ints: Vec<_> = {
@@ -240,7 +221,7 @@ field_impl!(
0x9ede7d651eca6ac987d20782e4866389
);
lazy_static! {
lazy_static::lazy_static! {
static ref FQ: U256 = U256::from([
0x3c208c16d87cfd47,
+1 -26
View File
@@ -3,9 +3,6 @@ use rand::Rng;
use crate::fields::{const_fq, FieldElement, Fq};
use crate::arith::{U256, U512};
#[cfg(feature = "rustc-serialize")]
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
#[inline]
fn fq_non_residue() -> Fq {
// (q - 1) is a quadratic nonresidue in Fq
@@ -43,28 +40,6 @@ 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();
let c1: U256 = self.c1.into();
U512::new(&c1, &c0, &Fq::modulus()).encode(s)
}
}
#[cfg(feature = "rustc-serialize")]
impl Decodable for Fq2 {
fn decode<S: Decoder>(s: &mut S) -> Result<Fq2, S::Error> {
let combined = U512::decode(s)?;
match combined.divrem(&Fq::modulus()) {
(Some(c1), c0) => Ok(Fq2::new(Fq::new(c0).unwrap(), Fq::new(c1).unwrap())),
_ => Err(s.error("integer not less than modulus squared")),
}
}
}
impl Fq2 {
pub fn new(c0: Fq, c1: Fq) -> Self {
Fq2 { c0: c0, c1: c1 }
@@ -206,7 +181,7 @@ impl Neg for Fq2 {
}
}
lazy_static! {
lazy_static::lazy_static! {
static ref FQ: U256 = U256::from([
0x3c208c16d87cfd47,
0x97816a916871ca8d,
+2 -57
View File
@@ -3,9 +3,8 @@ use crate::arith::U256;
use core::{fmt, ops::{Add, Mul, Neg, Sub}};
use rand::Rng;
use alloc::vec::Vec;
#[cfg(feature = "rustc-serialize")]
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
#[cfg(test)]
use alloc::vec;
// This is the NAF version of ate_loop_count. Entries are all mod 4, so 3 = -1
// n.b. ate_loop_count = 0x19d797039be763ba8
@@ -33,9 +32,6 @@ pub trait GroupElement
}
pub trait GroupParams: Sized + fmt::Debug {
#[cfg(feature = "rustc-serialize")]
type Base: FieldElement + Decodable + Encodable;
#[cfg(not(feature = "rustc-serialize"))]
type Base: FieldElement;
fn name() -> &'static str;
@@ -230,57 +226,6 @@ 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() {
let l: u8 = 0;
l.encode(s)
} else {
let l: u8 = 4;
l.encode(s)?;
self.to_affine().unwrap().encode(s)
}
}
}
#[cfg(feature = "rustc-serialize")]
impl<P: GroupParams> Encodable for AffineG<P> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
self.x.encode(s)?;
self.y.encode(s)?;
Ok(())
}
}
#[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 = u8::decode(s)?;
if l == 0 {
Ok(G::zero())
} else if l == 4 {
Ok(AffineG::decode(s)?.to_jacobian())
} else {
Err(s.error("invalid leading byte for uncompressed group element"))
}
}
}
#[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 = P::Base::decode(s)?;
let y = P::Base::decode(s)?;
Self::new(x, y).map_err(|e| match e {
Error::NotOnCurve => s.error("point is not on the curve"),
Error::NotInSubgroup => s.error("point is not in the subgroup"),
})
}
}
impl<P: GroupParams> GroupElement for G<P> {
fn zero() -> Self {
G {
+1 -16
View File
@@ -1,14 +1,6 @@
#![no_std]
#[cfg_attr(test, macro_use)]
extern crate alloc;
extern crate byteorder;
#[macro_use]
extern crate crunchy;
extern crate rand;
#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize;
#[macro_use] extern crate lazy_static;
pub mod arith;
mod fields;
@@ -22,7 +14,6 @@ use core::ops::{Add, Mul, Neg, Sub};
use rand::Rng;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct Fr(fields::Fr);
@@ -132,7 +123,6 @@ impl From<FieldError> for CurveError {
pub use crate::groups::Error as GroupError;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct Fq(fields::Fq);
@@ -330,7 +320,6 @@ pub trait Group
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct G1(groups::G1);
@@ -442,7 +431,6 @@ impl Mul<Fr> for G1 {
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct AffineG1(groups::AffineG1);
@@ -479,7 +467,6 @@ impl From<AffineG1> for G1 {
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct G2(groups::G2);
@@ -646,7 +633,6 @@ pub fn miller_loop_batch(pairs: &[(G2, G1)]) -> Result<Gt, CurveError> {
}
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcDecodable, RustcEncodable))]
#[repr(C)]
pub struct AffineG2(groups::AffineG2);
@@ -684,12 +670,11 @@ impl From<AffineG2> for G2 {
#[cfg(test)]
mod tests {
extern crate rustc_hex as hex;
use alloc::vec::Vec;
use super::{G1, Fq, G2, Fq2};
fn hex(s: &'static str) -> Vec<u8> {
use self::hex::FromHex;
use rustc_hex::FromHex;
s.from_hex().unwrap()
}
-30140
View File
File diff suppressed because it is too large Load Diff