Skip to content

Commit

Permalink
Merge pull request rust-num#6 from dignifiedquire/zeroize
Browse files Browse the repository at this point in the history
feat: add zeroize feature
  • Loading branch information
dignifiedquire committed Mar 16, 2019
2 parents 666cdc6 + 5f60f4b commit 30d11de
Show file tree
Hide file tree
Showing 8 changed files with 594 additions and 549 deletions.
12 changes: 8 additions & 4 deletions Cargo.toml
Expand Up @@ -44,15 +44,16 @@ version = "0.5"
default-features = false
features = ["std"]

[dependencies.zeroize]
version = "0.5"
optional = true

[dependencies.serde]
optional = true
version = "1.0"
default-features = false
features = ["std"]

[dev-dependencies.serde_test]
version = "1.0"

[dependencies.lazy_static]
version = "1.2.0"

Expand All @@ -63,9 +64,12 @@ version = "1.2.7"
criterion = "0.2"
rand_chacha = "0.1"

[dev-dependencies.serde_test]
version = "1.0"

[features]
default = ["std", "i128", "u64_digit"]
i128 = ["num-integer/i128", "num-traits/i128"]
std = ["num-integer/std", "num-traits/std", "smallvec/std"]
u64_digit = []
prime = ["rand"]
prime = ["rand"]
2 changes: 1 addition & 1 deletion src/algorithms/cmp.rs
Expand Up @@ -22,7 +22,7 @@ pub fn cmp_slice(a: &[BigDigit], b: &[BigDigit]) -> Ordering {
return Greater;
}
}
return Equal;
Equal
}

#[cfg(test)]
Expand Down
9 changes: 5 additions & 4 deletions src/algorithms/mac.rs
Expand Up @@ -297,16 +297,17 @@ fn toom3(acc: &mut [BigDigit], x: &[BigDigit], y: &[BigDigit]) {
//
// Evaluate at w(t) where t is our given base to get the result.
add2(acc, r0.digits());
add2(acc, (comp1 << BITS * 1 * i).digits());
add2(acc, (comp2 << BITS * 2 * i).digits());
add2(acc, (comp3 << BITS * 3 * i).digits());
add2(acc, (r4 << BITS * 4 * i).digits());
add2(acc, (comp1 << (BITS * 1 * i)).digits());
add2(acc, (comp2 << (BITS * 2 * i)).digits());
add2(acc, (comp3 << (BITS * 3 * i)).digits());
add2(acc, (r4 << (BITS * 4 * i)).digits());
}

#[cfg(test)]
mod tests {
use super::*;

#[cfg(feature = "u64_digit")]
#[test]
fn test_mac3_regression() {
let b: Vec<BigDigit> = vec![
Expand Down
2 changes: 2 additions & 0 deletions src/algorithms/mod.rs
@@ -1,3 +1,5 @@
#![allow(clippy::many_single_char_names)]

mod add;
mod bits;
mod cmp;
Expand Down
59 changes: 38 additions & 21 deletions src/bigint.rs
@@ -1,22 +1,26 @@
#![allow(clippy::suspicious_arithmetic_impl)]
#[allow(deprecated, unused_imports)]
use std::borrow::Cow;
use std::cmp::Ordering::{self, Equal, Greater, Less};
use std::default::Default;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter::{Product, Sum};
use std::mem;
use std::ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
};
use std::str::{self, FromStr};
use std::{fmt, mem};
#[cfg(has_i128)]
use std::{i128, u128};
use std::{i64, u64};

#[cfg(feature = "serde")]
use serde;

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

use integer::{Integer, Roots};
use num_traits::{
CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow, Signed,
Expand Down Expand Up @@ -113,12 +117,21 @@ impl<'de> serde::Deserialize<'de> for Sign {
}

/// A big signed integer type.
#[derive(Clone, Debug, Hash)]
#[derive(Clone, Debug)]
pub struct BigInt {
pub(crate) sign: Sign,
pub(crate) data: BigUint,
}

#[cfg(feature = "zeroize")]
impl Zeroize for BigInt {
fn zeroize(&mut self) {
// TODO: Figure out how to better clear the sign.
self.sign = Sign::NoSign;
self.data.zeroize();
}
}

/// Return the magnitude of a `BigInt`.
///
/// This is in a private module, pseudo pub(crate)
Expand All @@ -144,6 +157,13 @@ impl PartialEq for BigInt {

impl Eq for BigInt {}

impl Hash for BigInt {
fn hash<H: Hasher>(&self, state: &mut H) {
self.sign.hash(state);
self.data.hash(state);
}
}

impl PartialOrd for BigInt {
#[inline]
fn partial_cmp(&self, other: &BigInt) -> Option<Ordering> {
Expand Down Expand Up @@ -824,9 +844,7 @@ impl Signed for BigInt {
fn powsign<T: Integer>(sign: Sign, other: &T) -> Sign {
if other.is_zero() {
Plus
} else if sign != Minus {
sign
} else if other.is_odd() {
} else if sign != Minus || other.is_odd() {
sign
} else {
-sign
Expand Down Expand Up @@ -2147,31 +2165,32 @@ impl<'a> Neg for &'a BigInt {
impl CheckedAdd for BigInt {
#[inline]
fn checked_add(&self, v: &BigInt) -> Option<BigInt> {
return Some(self.add(v));
Some(self.add(v))
}
}

impl CheckedSub for BigInt {
#[inline]
fn checked_sub(&self, v: &BigInt) -> Option<BigInt> {
return Some(self.sub(v));
Some(self.sub(v))
}
}

impl CheckedMul for BigInt {
#[inline]
fn checked_mul(&self, v: &BigInt) -> Option<BigInt> {
return Some(self.mul(v));
Some(self.mul(v))
}
}

impl CheckedDiv for BigInt {
#[inline]
fn checked_div(&self, v: &BigInt) -> Option<BigInt> {
if v.is_zero() {
return None;
None
} else {
Some(self.div(v))
}
return Some(self.div(v));
}
}

Expand Down Expand Up @@ -2245,7 +2264,7 @@ impl Integer for BigInt {
/// Deprecated, use `is_multiple_of` instead.
#[inline]
fn divides(&self, other: &BigInt) -> bool {
return self.is_multiple_of(other);
self.is_multiple_of(other)
}

/// Returns `true` if the number is a multiple of `other`.
Expand Down Expand Up @@ -2682,10 +2701,7 @@ impl BigInt {
sign = NoSign;
}

BigInt {
sign: sign,
data: data,
}
BigInt { sign, data }
}

/// Creates and initializes a `BigInt`.
Expand Down Expand Up @@ -3044,25 +3060,26 @@ impl BigInt {

#[inline]
pub fn checked_add(&self, v: &BigInt) -> Option<BigInt> {
return Some(self.add(v));
Some(self.add(v))
}

#[inline]
pub fn checked_sub(&self, v: &BigInt) -> Option<BigInt> {
return Some(self.sub(v));
Some(self.sub(v))
}

#[inline]
pub fn checked_mul(&self, v: &BigInt) -> Option<BigInt> {
return Some(self.mul(v));
Some(self.mul(v))
}

#[inline]
pub fn checked_div(&self, v: &BigInt) -> Option<BigInt> {
if v.is_zero() {
return None;
None
} else {
Some(self.div(v))
}
return Some(self.div(v));
}

/// Returns `(self ^ exponent) mod modulus`
Expand Down

0 comments on commit 30d11de

Please sign in to comment.