Skip to content

Commit b32ce86

Browse files
committed
Remove use of std::num::Int
1 parent 8b90988 commit b32ce86

File tree

10 files changed

+26
-45
lines changed

10 files changed

+26
-45
lines changed

src/aessafe.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ finite field which allows for efficient computation of the AES S-Boxes. See [7]
124124
*/
125125

126126
use std::ops::{BitAnd, BitXor, Not};
127-
use std::num::Int;
128127
use std::default::Default;
129128

130129
use cryptoutil::{read_u32v_le, write_u32_le};

src/blake2b.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// except according to those terms.
66

77
use std::iter::repeat;
8-
use std::num::Int;
98
use cryptoutil::{read_u64v_le, write_u64v_le};
109
use std::slice::bytes::{copy_memory};
1110
use std::intrinsics::volatile_set_memory;

src/cryptoutil.rs

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use std;
1212
use std::{io, mem};
13-
use std::num::Int;
1413
use std::ptr;
1514
use std::slice::bytes::{MutableByteVector, copy_memory};
1615

@@ -101,7 +100,7 @@ pub fn read_u64v_be(dst: &mut[u64], input: &[u8]) {
101100
for _ in (0..dst.len()) {
102101
let mut tmp: u64 = mem::uninitialized();
103102
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 8);
104-
*x = Int::from_be(tmp);
103+
*x = u64::from_be(tmp);
105104
x = x.offset(1);
106105
y = y.offset(8);
107106
}
@@ -117,7 +116,7 @@ pub fn read_u64v_le(dst: &mut[u64], input: &[u8]) {
117116
for _ in (0..dst.len()) {
118117
let mut tmp: u64 = mem::uninitialized();
119118
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 8);
120-
*x = Int::from_le(tmp);
119+
*x = u64::from_le(tmp);
121120
x = x.offset(1);
122121
y = y.offset(8);
123122
}
@@ -133,7 +132,7 @@ pub fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
133132
for _ in (0..dst.len()) {
134133
let mut tmp: u32 = mem::uninitialized();
135134
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 4);
136-
*x = Int::from_be(tmp);
135+
*x = u32::from_be(tmp);
137136
x = x.offset(1);
138137
y = y.offset(4);
139138
}
@@ -149,7 +148,7 @@ pub fn read_u32v_le(dst: &mut[u32], input: &[u8]) {
149148
for _ in (0..dst.len()) {
150149
let mut tmp: u32 = mem::uninitialized();
151150
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 4);
152-
*x = Int::from_le(tmp);
151+
*x = u32::from_le(tmp);
153152
x = x.offset(1);
154153
y = y.offset(4);
155154
}
@@ -162,7 +161,7 @@ pub fn read_u32_le(input: &[u8]) -> u32 {
162161
unsafe {
163162
let mut tmp: u32 = mem::uninitialized();
164163
ptr::copy_nonoverlapping(input.get_unchecked(0), &mut tmp as *mut _ as *mut u8, 4);
165-
Int::from_le(tmp)
164+
u32::from_le(tmp)
166165
}
167166
}
168167

@@ -172,7 +171,7 @@ pub fn read_u32_be(input: &[u8]) -> u32 {
172171
unsafe {
173172
let mut tmp: u32 = mem::uninitialized();
174173
ptr::copy_nonoverlapping(input.get_unchecked(0), &mut tmp as *mut _ as *mut u8, 4);
175-
Int::from_be(tmp)
174+
u32::from_be(tmp)
176175
}
177176
}
178177

@@ -243,24 +242,18 @@ pub fn symm_enc_or_dec<S: SynchronousStreamCipher, R: ReadBuffer, W: WriteBuffer
243242
}
244243
}
245244

246-
pub trait ToBits {
247-
/// Convert the value in bytes to the number of bits, a tuple where the 1st item is the
248-
/// high-order value and the 2nd item is the low order value.
249-
fn to_bits(self) -> (Self, Self);
250-
}
251-
252-
impl ToBits for u64 {
253-
fn to_bits(self) -> (u64, u64) {
254-
(self >> 61, self << 3)
255-
}
245+
/// Convert the value in bytes to the number of bits, a tuple where the 1st item is the
246+
/// high-order value and the 2nd item is the low order value.
247+
fn to_bits(x: u64) -> (u64, u64) {
248+
(x >> 61, x << 3)
256249
}
257250

258251
/// Adds the specified number of bytes to the bit count. panic!() if this would cause numeric
259252
/// overflow.
260-
pub fn add_bytes_to_bits<T: Int + ToBits>(bits: T, bytes: T) -> T {
261-
let (new_high_bits, new_low_bits) = bytes.to_bits();
253+
pub fn add_bytes_to_bits(bits: u64, bytes: u64) -> u64 {
254+
let (new_high_bits, new_low_bits) = to_bits(bytes);
262255

263-
if new_high_bits > Int::zero() {
256+
if new_high_bits > 0 {
264257
panic!("Numeric overflow occured.")
265258
}
266259

@@ -270,17 +263,16 @@ pub fn add_bytes_to_bits<T: Int + ToBits>(bits: T, bytes: T) -> T {
270263
/// Adds the specified number of bytes to the bit count, which is a tuple where the first element is
271264
/// the high order value. panic!() if this would cause numeric overflow.
272265
pub fn add_bytes_to_bits_tuple
273-
<T: Int + ToBits>
274-
(bits: (T, T), bytes: T) -> (T, T) {
275-
let (new_high_bits, new_low_bits) = bytes.to_bits();
266+
(bits: (u64, u64), bytes: u64) -> (u64, u64) {
267+
let (new_high_bits, new_low_bits) = to_bits(bytes);
276268
let (hi, low) = bits;
277269

278270
// Add the low order value - if there is no overflow, then add the high order values
279271
// If the addition of the low order values causes overflow, add one to the high order values
280272
// before adding them.
281273
match low.checked_add(new_low_bits) {
282274
Some(x) => {
283-
if new_high_bits == Int::zero() {
275+
if new_high_bits == 0 {
284276
// This is the fast path - every other alternative will rarely occur in practice
285277
// considering how large an input would need to be for those paths to be used.
286278
return (hi, x);
@@ -292,8 +284,7 @@ pub fn add_bytes_to_bits_tuple
292284
}
293285
},
294286
None => {
295-
let one: T = Int::one();
296-
let z = match new_high_bits.checked_add(one) {
287+
let z = match new_high_bits.checked_add(1) {
297288
Some(w) => w,
298289
None => panic!("Numeric overflow occured.")
299290
};
@@ -496,8 +487,8 @@ impl <T: FixedBuffer> StandardPadding for T {
496487

497488
#[cfg(test)]
498489
pub mod test {
490+
use std;
499491
use std::iter::repeat;
500-
use std::num::Int;
501492

502493
use rand::IsaacRng;
503494
use rand::distributions::{IndependentSample, Range};
@@ -532,47 +523,47 @@ pub mod test {
532523
// A normal addition - no overflow occurs
533524
#[test]
534525
fn test_add_bytes_to_bits_ok() {
535-
assert!(add_bytes_to_bits::<u64>(100, 10) == 180);
526+
assert!(add_bytes_to_bits(100, 10) == 180);
536527
}
537528

538529
// A simple failure case - adding 1 to the max value
539530
#[test]
540531
#[should_panic]
541532
fn test_add_bytes_to_bits_overflow() {
542-
add_bytes_to_bits::<u64>(Int::max_value(), 1);
533+
add_bytes_to_bits(std::u64::MAX, 1);
543534
}
544535

545536
// A normal addition - no overflow occurs (fast path)
546537
#[test]
547538
fn test_add_bytes_to_bits_tuple_ok() {
548-
assert!(add_bytes_to_bits_tuple::<u64>((5, 100), 10) == (5, 180));
539+
assert!(add_bytes_to_bits_tuple((5, 100), 10) == (5, 180));
549540
}
550541

551542
// The low order value overflows into the high order value
552543
#[test]
553544
fn test_add_bytes_to_bits_tuple_ok2() {
554-
assert!(add_bytes_to_bits_tuple::<u64>((5, Int::max_value()), 1) == (6, 7));
545+
assert!(add_bytes_to_bits_tuple((5, std::u64::MAX), 1) == (6, 7));
555546
}
556547

557548
// The value to add is too large to be converted into bits without overflowing its type
558549
#[test]
559550
fn test_add_bytes_to_bits_tuple_ok3() {
560-
assert!(add_bytes_to_bits_tuple::<u64>((5, 0), 0x4000000000000001) == (7, 8));
551+
assert!(add_bytes_to_bits_tuple((5, 0), 0x4000000000000001) == (7, 8));
561552
}
562553

563554
// A simple failure case - adding 1 to the max value
564555
#[test]
565556
#[should_panic]
566557
fn test_add_bytes_to_bits_tuple_overflow() {
567-
add_bytes_to_bits_tuple::<u64>((Int::max_value(), Int::max_value()), 1);
558+
add_bytes_to_bits_tuple((std::u64::MAX, std::u64::MAX), 1);
568559
}
569560

570561
// The value to add is too large to convert to bytes without overflowing its type, but the high
571562
// order value from this conversion overflows when added to the existing high order value
572563
#[test]
573564
#[should_panic]
574565
fn test_add_bytes_to_bits_tuple_overflow2() {
575-
let value: u64 = Int::max_value();
576-
add_bytes_to_bits_tuple::<u64>((value - 1, 0), 0x8000000000000000);
566+
let value: u64 = std::u64::MAX;
567+
add_bytes_to_bits_tuple((value - 1, 0), 0x8000000000000000);
577568
}
578569
}

src/hc128.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use buffer::{BufferResult, RefReadBuffer, RefWriteBuffer};
99
use symmetriccipher::{Encryptor, Decryptor, SynchronousStreamCipher, SymmetricCipherError};
1010
use cryptoutil::{read_u32_le, symm_enc_or_dec, write_u32_le};
1111

12-
use std::num::Int;
1312
use std::ptr;
1413

1514

src/hkdf.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//! Derivation Function as specified by https://tools.ietf.org/html/rfc5869.
99
1010
use std::iter::repeat;
11-
use std::num::Int;
1211
use std::slice::bytes::copy_memory;
1312

1413
use digest::Digest;

src/md5.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::num::Int;
12-
1311
use cryptoutil::{write_u32_le, read_u32v_le, FixedBuffer, FixedBuffer64, StandardPadding};
1412
use digest::Digest;
1513

src/pbkdf2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use std::iter::repeat;
1313
use std::io;
14-
use std::num::Int;
1514
use std::slice::bytes::copy_memory;
1615

1716
use rand::{OsRng, Rng};

src/sha1.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Some of these functions are commonly found in all hash digest
6060
algorithms, but some, like "parity" is only found in SHA-1.
6161
*/
6262

63-
use std::num::Int;
6463
use digest::Digest;
6564
use cryptoutil::{write_u32_be, read_u32v_be, add_bytes_to_bits, FixedBuffer, FixedBuffer64, StandardPadding};
6665
use simd::u32x4;

src/sha2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ assert_eq!(hex,
7070
7171
*/
7272

73-
use std::num::Int;
7473
use digest::Digest;
7574
use cryptoutil::{write_u32_be, read_u32v_be,
7675
write_u64_be, read_u64v_be,

src/sosemanuk.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use buffer::{BufferResult, RefReadBuffer, RefWriteBuffer};
99
use symmetriccipher::{Encryptor, Decryptor, SynchronousStreamCipher, SymmetricCipherError};
1010
use cryptoutil::{read_u32_le, symm_enc_or_dec, write_u32v_le};
1111

12-
use std::num::Int;
1312
use std::slice::bytes::copy_memory;
1413

1514

0 commit comments

Comments
 (0)