Skip to content

Commit e22271c

Browse files
bors[bot]cuviper
andcommitted
Merge #38
38: Remove big_digit::* from the public API r=cuviper a=cuviper The *idea* of `big_digit` and its type aliases is that we may someday use something other than `u32` in the representation, perhaps even different sizes for different targets. That's still a possibility, but I think it's not really feasible to expose this variation in the public API. Calling `BigUint::from_slice([1, 2, 3])` is only meaningful if you know what that size is, and users can't really alternate this kind of thing based on a type definition. So for now, we just commit to `u32` units in the public API, no matter what we may do internally. This removal is a breaking change, part of the 0.2 semver bump. If I'm wrong and somebody can show a compelling use case for `big_digit`, we can always add things back into the public API later. Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents 7424bbe + a849284 commit e22271c

9 files changed

Lines changed: 108 additions & 112 deletions

File tree

src/algorithms.rs

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,7 @@ use bigint::BigInt;
1212
use bigint::Sign;
1313
use bigint::Sign::{Minus, NoSign, Plus};
1414

15-
#[allow(non_snake_case)]
16-
pub mod big_digit {
17-
/// A `BigDigit` is a `BigUint`'s composing element.
18-
pub type BigDigit = u32;
19-
20-
/// A `DoubleBigDigit` is the internal type used to do the computations. Its
21-
/// size is the double of the size of `BigDigit`.
22-
pub type DoubleBigDigit = u64;
23-
24-
/// A `SignedDoubleBigDigit` is the signed version of `DoubleBigDigit`.
25-
pub type SignedDoubleBigDigit = i64;
26-
27-
pub const ZERO_BIG_DIGIT: BigDigit = 0;
28-
29-
// `DoubleBigDigit` size dependent
30-
pub const BITS: usize = 32;
31-
32-
pub const BASE: DoubleBigDigit = 1 << BITS;
33-
const LO_MASK: DoubleBigDigit = (-1i32 as DoubleBigDigit) >> BITS;
34-
35-
#[inline]
36-
fn get_hi(n: DoubleBigDigit) -> BigDigit {
37-
(n >> BITS) as BigDigit
38-
}
39-
#[inline]
40-
fn get_lo(n: DoubleBigDigit) -> BigDigit {
41-
(n & LO_MASK) as BigDigit
42-
}
43-
44-
/// Split one `DoubleBigDigit` into two `BigDigit`s.
45-
#[inline]
46-
pub fn from_doublebigdigit(n: DoubleBigDigit) -> (BigDigit, BigDigit) {
47-
(get_hi(n), get_lo(n))
48-
}
49-
50-
/// Join two `BigDigit`s into one `DoubleBigDigit`
51-
#[inline]
52-
pub fn to_doublebigdigit(hi: BigDigit, lo: BigDigit) -> DoubleBigDigit {
53-
(lo as DoubleBigDigit) | ((hi as DoubleBigDigit) << BITS)
54-
}
55-
}
56-
57-
use big_digit::{BigDigit, DoubleBigDigit, SignedDoubleBigDigit};
15+
use big_digit::{self, BigDigit, DoubleBigDigit, SignedDoubleBigDigit};
5816

5917
// Generic functions for add/subtract/multiply with carry/borrow:
6018

@@ -647,7 +605,8 @@ pub fn cmp_slice(a: &[BigDigit], b: &[BigDigit]) -> Ordering {
647605

648606
#[cfg(test)]
649607
mod algorithm_tests {
650-
use {BigDigit, BigUint, BigInt};
608+
use big_digit::BigDigit;
609+
use {BigUint, BigInt};
651610
use Sign::Plus;
652611
use traits::Num;
653612

src/bigint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use traits::{ToPrimitive, FromPrimitive, Num, CheckedAdd, CheckedSub,
2525
use self::Sign::{Minus, NoSign, Plus};
2626

2727
use super::ParseBigIntError;
28-
use super::big_digit::{self, BigDigit, DoubleBigDigit};
28+
use big_digit::{self, BigDigit, DoubleBigDigit};
2929
use biguint;
3030
use biguint::to_str_radix_reversed;
3131
use biguint::{BigUint, IntDigits};
@@ -1868,7 +1868,7 @@ impl BigInt {
18681868
///
18691869
/// The digits are in little-endian base 2<sup>32</sup>.
18701870
#[inline]
1871-
pub fn new(sign: Sign, digits: Vec<BigDigit>) -> BigInt {
1871+
pub fn new(sign: Sign, digits: Vec<u32>) -> BigInt {
18721872
BigInt::from_biguint(sign, BigUint::new(digits))
18731873
}
18741874

@@ -1891,13 +1891,13 @@ impl BigInt {
18911891

18921892
/// Creates and initializes a `BigInt`.
18931893
#[inline]
1894-
pub fn from_slice(sign: Sign, slice: &[BigDigit]) -> BigInt {
1894+
pub fn from_slice(sign: Sign, slice: &[u32]) -> BigInt {
18951895
BigInt::from_biguint(sign, BigUint::from_slice(slice))
18961896
}
18971897

18981898
/// Reinitializes a `BigInt`.
18991899
#[inline]
1900-
pub fn assign_from_slice(&mut self, sign: Sign, slice: &[BigDigit]) {
1900+
pub fn assign_from_slice(&mut self, sign: Sign, slice: &[u32]) {
19011901
if sign == NoSign {
19021902
self.data.assign_from_slice(&[]);
19031903
self.sign = NoSign;

src/biguint.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ use integer::Integer;
2121
use traits::{ToPrimitive, FromPrimitive, Float, Num, Unsigned, CheckedAdd, CheckedSub, CheckedMul,
2222
CheckedDiv, Zero, One};
2323

24+
use big_digit::{self, BigDigit, DoubleBigDigit};
25+
2426
#[path = "algorithms.rs"]
2527
mod algorithms;
2628
#[path = "monty.rs"]
2729
mod monty;
28-
pub use self::algorithms::big_digit;
29-
pub use self::big_digit::{BigDigit, DoubleBigDigit, ZERO_BIG_DIGIT};
3030

3131
use self::algorithms::{mac_with_carry, mul3, scalar_mul, div_rem, div_rem_digit};
3232
use self::algorithms::{__add2, __sub2rev, add2, sub2, sub2rev};
@@ -39,9 +39,6 @@ use UsizePromotion;
3939
use ParseBigIntError;
4040

4141
/// A big unsigned integer type.
42-
///
43-
/// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number
44-
/// `(a + b * big_digit::BASE + c * big_digit::BASE^2)`.
4542
#[derive(Clone, Debug, Hash)]
4643
pub struct BigUint {
4744
data: Vec<BigDigit>,
@@ -1379,23 +1376,23 @@ impl BigUint {
13791376
///
13801377
/// The digits are in little-endian base 2<sup>32</sup>.
13811378
#[inline]
1382-
pub fn new(digits: Vec<BigDigit>) -> BigUint {
1379+
pub fn new(digits: Vec<u32>) -> BigUint {
13831380
BigUint { data: digits }.normalized()
13841381
}
13851382

13861383
/// Creates and initializes a `BigUint`.
13871384
///
13881385
/// The digits are in little-endian base 2<sup>32</sup>.
13891386
#[inline]
1390-
pub fn from_slice(slice: &[BigDigit]) -> BigUint {
1387+
pub fn from_slice(slice: &[u32]) -> BigUint {
13911388
BigUint::new(slice.to_vec())
13921389
}
13931390

13941391
/// Assign a value to a `BigUint`.
13951392
///
13961393
/// The digits are in little-endian base 2<sup>32</sup>.
13971394
#[inline]
1398-
pub fn assign_from_slice(&mut self, slice: &[BigDigit]) {
1395+
pub fn assign_from_slice(&mut self, slice: &[u32]) {
13991396
self.data.resize(slice.len(), 0);
14001397
self.data.clone_from_slice(slice);
14011398
self.normalize();

src/lib.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,46 @@ mod bigint;
152152

153153
pub use biguint::BigUint;
154154
pub use biguint::ToBigUint;
155-
pub use biguint::big_digit;
156-
pub use biguint::big_digit::{BigDigit, DoubleBigDigit, ZERO_BIG_DIGIT};
157155

158156
pub use bigint::Sign;
159157
pub use bigint::BigInt;
160158
pub use bigint::ToBigInt;
161159
pub use bigint::RandBigInt;
160+
161+
mod big_digit {
162+
/// A `BigDigit` is a `BigUint`'s composing element.
163+
pub type BigDigit = u32;
164+
165+
/// A `DoubleBigDigit` is the internal type used to do the computations. Its
166+
/// size is the double of the size of `BigDigit`.
167+
pub type DoubleBigDigit = u64;
168+
169+
/// A `SignedDoubleBigDigit` is the signed version of `DoubleBigDigit`.
170+
pub type SignedDoubleBigDigit = i64;
171+
172+
// `DoubleBigDigit` size dependent
173+
pub const BITS: usize = 32;
174+
175+
const LO_MASK: DoubleBigDigit = (-1i32 as DoubleBigDigit) >> BITS;
176+
177+
#[inline]
178+
fn get_hi(n: DoubleBigDigit) -> BigDigit {
179+
(n >> BITS) as BigDigit
180+
}
181+
#[inline]
182+
fn get_lo(n: DoubleBigDigit) -> BigDigit {
183+
(n & LO_MASK) as BigDigit
184+
}
185+
186+
/// Split one `DoubleBigDigit` into two `BigDigit`s.
187+
#[inline]
188+
pub fn from_doublebigdigit(n: DoubleBigDigit) -> (BigDigit, BigDigit) {
189+
(get_hi(n), get_lo(n))
190+
}
191+
192+
/// Join two `BigDigit`s into one `DoubleBigDigit`
193+
#[inline]
194+
pub fn to_doublebigdigit(hi: BigDigit, lo: BigDigit) -> DoubleBigDigit {
195+
(lo as DoubleBigDigit) | ((hi as DoubleBigDigit) << BITS)
196+
}
197+
}

tests/bigint.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate num_integer;
33
extern crate num_traits;
44
extern crate rand;
55

6-
use num_bigint::{BigDigit, BigUint, big_digit};
6+
use num_bigint::BigUint;
77
use num_bigint::{BigInt, ToBigInt};
88
use num_bigint::Sign::{Minus, NoSign, Plus};
99

@@ -163,7 +163,7 @@ fn test_from_signed_bytes_be() {
163163

164164
#[test]
165165
fn test_cmp() {
166-
let vs: [&[BigDigit]; 4] = [&[2 as BigDigit], &[1, 1], &[2, 1], &[1, 1, 1]];
166+
let vs: [&[u32]; 4] = [&[2 as u32], &[1, 1], &[2, 1], &[1, 1, 1]];
167167
let mut nums = Vec::new();
168168
for s in vs.iter().rev() {
169169
nums.push(BigInt::from_slice(Minus, *s));
@@ -244,7 +244,7 @@ fn test_convert_i64() {
244244
None);
245245

246246
assert_eq!(BigInt::from_biguint(Minus,
247-
BigUint::new(vec![1, 0, 0, 1 << (big_digit::BITS - 1)]))
247+
BigUint::new(vec![1, 0, 0, 1 << 31]))
248248
.to_i64(),
249249
None);
250250

@@ -458,11 +458,11 @@ fn test_convert_from_uint() {
458458
}
459459
}
460460

461-
check!(u8, BigInt::from_slice(Plus, &[u8::MAX as BigDigit]));
462-
check!(u16, BigInt::from_slice(Plus, &[u16::MAX as BigDigit]));
463-
check!(u32, BigInt::from_slice(Plus, &[u32::MAX as BigDigit]));
461+
check!(u8, BigInt::from_slice(Plus, &[u8::MAX as u32]));
462+
check!(u16, BigInt::from_slice(Plus, &[u16::MAX as u32]));
463+
check!(u32, BigInt::from_slice(Plus, &[u32::MAX]));
464464
check!(u64,
465-
BigInt::from_slice(Plus, &[u32::MAX as BigDigit, u32::MAX as BigDigit]));
465+
BigInt::from_slice(Plus, &[u32::MAX, u32::MAX]));
466466
check!(usize, BigInt::from(usize::MAX as u64));
467467
}
468468

@@ -482,16 +482,16 @@ fn test_convert_from_int() {
482482

483483
check!(i8,
484484
BigInt::from_slice(Minus, &[1 << 7]),
485-
BigInt::from_slice(Plus, &[i8::MAX as BigDigit]));
485+
BigInt::from_slice(Plus, &[i8::MAX as u32]));
486486
check!(i16,
487487
BigInt::from_slice(Minus, &[1 << 15]),
488-
BigInt::from_slice(Plus, &[i16::MAX as BigDigit]));
488+
BigInt::from_slice(Plus, &[i16::MAX as u32]));
489489
check!(i32,
490490
BigInt::from_slice(Minus, &[1 << 31]),
491-
BigInt::from_slice(Plus, &[i32::MAX as BigDigit]));
491+
BigInt::from_slice(Plus, &[i32::MAX as u32]));
492492
check!(i64,
493493
BigInt::from_slice(Minus, &[0, 1 << 31]),
494-
BigInt::from_slice(Plus, &[u32::MAX as BigDigit, i32::MAX as BigDigit]));
494+
BigInt::from_slice(Plus, &[u32::MAX, i32::MAX as u32]));
495495
check!(isize,
496496
BigInt::from(isize::MIN as i64),
497497
BigInt::from(isize::MAX as i64));

tests/bigint_bitwise.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
extern crate num_bigint;
22
extern crate num_traits;
33

4-
use num_bigint::{BigDigit, BigInt, Sign, ToBigInt};
4+
use num_bigint::{BigInt, Sign, ToBigInt};
55
use num_traits::ToPrimitive;
66
use std::{i32, i64, u32};
77

88
enum ValueVec {
99
N,
10-
P(&'static [BigDigit]),
11-
M(&'static [BigDigit]),
10+
P(&'static [u32]),
11+
M(&'static [u32]),
1212
}
1313

1414
use ValueVec::*;

tests/bigint_scalar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate num_bigint;
22
extern crate num_traits;
33

4-
use num_bigint::{BigDigit, BigInt};
4+
use num_bigint::BigInt;
55
use num_bigint::Sign::Plus;
66
use num_traits::{Zero, Signed, ToPrimitive};
77

@@ -88,7 +88,7 @@ fn test_scalar_mul() {
8888

8989
#[test]
9090
fn test_scalar_div_rem() {
91-
fn check_sub(a: &BigInt, b: BigDigit, ans_q: &BigInt, ans_r: &BigInt) {
91+
fn check_sub(a: &BigInt, b: u32, ans_q: &BigInt, ans_r: &BigInt) {
9292
let (q, r) = (a / b, a % b);
9393
if !r.is_zero() {
9494
assert_eq!(r.sign(), a.sign());
@@ -109,7 +109,7 @@ fn test_scalar_div_rem() {
109109
}
110110
}
111111

112-
fn check(a: &BigInt, b: BigDigit, q: &BigInt, r: &BigInt) {
112+
fn check(a: &BigInt, b: u32, q: &BigInt, r: &BigInt) {
113113
check_sub(a, b, q, r);
114114
check_sub(&a.neg(), b, &q.neg(), &r.neg());
115115
}

tests/biguint.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate num_traits;
44
extern crate rand;
55

66
use num_integer::Integer;
7-
use num_bigint::{BigDigit, BigUint, ToBigUint, big_digit};
7+
use num_bigint::{BigUint, ToBigUint};
88
use num_bigint::{BigInt, ToBigInt};
99
use num_bigint::Sign::Plus;
1010

@@ -146,19 +146,25 @@ fn test_hash() {
146146
assert!(hash(&d) != hash(&e));
147147
}
148148

149-
const BIT_TESTS: &'static [(&'static [BigDigit],
150-
&'static [BigDigit],
151-
&'static [BigDigit],
152-
&'static [BigDigit],
153-
&'static [BigDigit])] = &[// LEFT RIGHT AND OR XOR
154-
(&[], &[], &[], &[], &[]),
155-
(&[1, 0, 1], &[1, 1], &[1], &[1, 1, 1], &[0, 1, 1]),
156-
(&[1, 0, 1], &[0, 1, 1], &[0, 0, 1], &[1, 1, 1], &[1, 1]),
157-
(&[268, 482, 17],
158-
&[964, 54],
159-
&[260, 34],
160-
&[972, 502, 17],
161-
&[712, 468, 17])];
149+
// LEFT, RIGHT, AND, OR, XOR
150+
const BIT_TESTS: &'static [(
151+
&'static [u32],
152+
&'static [u32],
153+
&'static [u32],
154+
&'static [u32],
155+
&'static [u32],
156+
)] = &[
157+
(&[], &[], &[], &[], &[]),
158+
(&[1, 0, 1], &[1, 1], &[1], &[1, 1, 1], &[0, 1, 1]),
159+
(&[1, 0, 1], &[0, 1, 1], &[0, 0, 1], &[1, 1, 1], &[1, 1]),
160+
(
161+
&[268, 482, 17],
162+
&[964, 54],
163+
&[260, 34],
164+
&[972, 502, 17],
165+
&[712, 468, 17],
166+
),
167+
];
162168

163169
#[test]
164170
fn test_bitand() {
@@ -473,9 +479,9 @@ fn test_convert_i64() {
473479
check(i64::MAX.to_biguint().unwrap(), i64::MAX);
474480

475481
check(BigUint::new(vec![]), 0);
476-
check(BigUint::new(vec![1]), 1 << (0 * big_digit::BITS));
477-
check(BigUint::new(vec![N1]), (1 << (1 * big_digit::BITS)) - 1);
478-
check(BigUint::new(vec![0, 1]), 1 << (1 * big_digit::BITS));
482+
check(BigUint::new(vec![1]), 1);
483+
check(BigUint::new(vec![N1]), (1 << 32) - 1);
484+
check(BigUint::new(vec![0, 1]), 1 << 32);
479485
check(BigUint::new(vec![N1, N1 >> 1]), i64::MAX);
480486

481487
assert_eq!(i64::MIN.to_biguint(), None);
@@ -499,9 +505,9 @@ fn test_convert_u64() {
499505
check(u64::MAX.to_biguint().unwrap(), u64::MAX);
500506

501507
check(BigUint::new(vec![]), 0);
502-
check(BigUint::new(vec![1]), 1 << (0 * big_digit::BITS));
503-
check(BigUint::new(vec![N1]), (1 << (1 * big_digit::BITS)) - 1);
504-
check(BigUint::new(vec![0, 1]), 1 << (1 * big_digit::BITS));
508+
check(BigUint::new(vec![1]), 1);
509+
check(BigUint::new(vec![N1]), (1 << 32) - 1);
510+
check(BigUint::new(vec![0, 1]), 1 << 32);
505511
check(BigUint::new(vec![N1, N1]), u64::MAX);
506512

507513
assert_eq!(BigUint::new(vec![0, 0, 1]).to_u64(), None);
@@ -667,8 +673,8 @@ fn test_convert_from_uint() {
667673
}
668674
}
669675

670-
check!(u8, BigUint::from_slice(&[u8::MAX as BigDigit]));
671-
check!(u16, BigUint::from_slice(&[u16::MAX as BigDigit]));
676+
check!(u8, BigUint::from_slice(&[u8::MAX as u32]));
677+
check!(u16, BigUint::from_slice(&[u16::MAX as u32]));
672678
check!(u32, BigUint::from_slice(&[u32::MAX]));
673679
check!(u64, BigUint::from_slice(&[u32::MAX, u32::MAX]));
674680
check!(usize, BigUint::from(usize::MAX as u64));
@@ -915,7 +921,7 @@ fn test_is_even() {
915921
}
916922

917923
fn to_str_pairs() -> Vec<(BigUint, Vec<(u32, String)>)> {
918-
let bits = big_digit::BITS;
924+
let bits = 32;
919925
vec![(Zero::zero(),
920926
vec![(2, "0".to_string()), (3, "0".to_string())]),
921927
(BigUint::from_slice(&[0xff]),

0 commit comments

Comments
 (0)