algorithms.rs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. use std::borrow::Cow;
  2. use std::cmp;
  3. use std::cmp::Ordering::{self, Less, Greater, Equal};
  4. use std::iter::repeat;
  5. use std::mem;
  6. use traits;
  7. use traits::{Zero, One};
  8. use biguint::BigUint;
  9. use bigint::BigInt;
  10. use bigint::Sign;
  11. use bigint::Sign::{Minus, NoSign, Plus};
  12. #[allow(non_snake_case)]
  13. pub mod big_digit {
  14. /// A `BigDigit` is a `BigUint`'s composing element.
  15. pub type BigDigit = u32;
  16. /// A `DoubleBigDigit` is the internal type used to do the computations. Its
  17. /// size is the double of the size of `BigDigit`.
  18. pub type DoubleBigDigit = u64;
  19. pub const ZERO_BIG_DIGIT: BigDigit = 0;
  20. // `DoubleBigDigit` size dependent
  21. pub const BITS: usize = 32;
  22. pub const BASE: DoubleBigDigit = 1 << BITS;
  23. const LO_MASK: DoubleBigDigit = (-1i32 as DoubleBigDigit) >> BITS;
  24. #[inline]
  25. fn get_hi(n: DoubleBigDigit) -> BigDigit {
  26. (n >> BITS) as BigDigit
  27. }
  28. #[inline]
  29. fn get_lo(n: DoubleBigDigit) -> BigDigit {
  30. (n & LO_MASK) as BigDigit
  31. }
  32. /// Split one `DoubleBigDigit` into two `BigDigit`s.
  33. #[inline]
  34. pub fn from_doublebigdigit(n: DoubleBigDigit) -> (BigDigit, BigDigit) {
  35. (get_hi(n), get_lo(n))
  36. }
  37. /// Join two `BigDigit`s into one `DoubleBigDigit`
  38. #[inline]
  39. pub fn to_doublebigdigit(hi: BigDigit, lo: BigDigit) -> DoubleBigDigit {
  40. (lo as DoubleBigDigit) | ((hi as DoubleBigDigit) << BITS)
  41. }
  42. }
  43. use big_digit::{BigDigit, DoubleBigDigit};
  44. // Generic functions for add/subtract/multiply with carry/borrow:
  45. // Add with carry:
  46. #[inline]
  47. fn adc(a: BigDigit, b: BigDigit, carry: &mut BigDigit) -> BigDigit {
  48. let (hi, lo) = big_digit::from_doublebigdigit((a as DoubleBigDigit) + (b as DoubleBigDigit) +
  49. (*carry as DoubleBigDigit));
  50. *carry = hi;
  51. lo
  52. }
  53. // Subtract with borrow:
  54. #[inline]
  55. fn sbb(a: BigDigit, b: BigDigit, borrow: &mut BigDigit) -> BigDigit {
  56. let (hi, lo) = big_digit::from_doublebigdigit(big_digit::BASE + (a as DoubleBigDigit) -
  57. (b as DoubleBigDigit) -
  58. (*borrow as DoubleBigDigit));
  59. // hi * (base) + lo == 1*(base) + ai - bi - borrow
  60. // => ai - bi - borrow < 0 <=> hi == 0
  61. *borrow = (hi == 0) as BigDigit;
  62. lo
  63. }
  64. #[inline]
  65. pub fn mac_with_carry(a: BigDigit, b: BigDigit, c: BigDigit, carry: &mut BigDigit) -> BigDigit {
  66. let (hi, lo) = big_digit::from_doublebigdigit((a as DoubleBigDigit) +
  67. (b as DoubleBigDigit) * (c as DoubleBigDigit) +
  68. (*carry as DoubleBigDigit));
  69. *carry = hi;
  70. lo
  71. }
  72. #[inline]
  73. pub fn mul_with_carry(a: BigDigit, b: BigDigit, carry: &mut BigDigit) -> BigDigit {
  74. let (hi, lo) = big_digit::from_doublebigdigit((a as DoubleBigDigit) * (b as DoubleBigDigit) +
  75. (*carry as DoubleBigDigit));
  76. *carry = hi;
  77. lo
  78. }
  79. /// Divide a two digit numerator by a one digit divisor, returns quotient and remainder:
  80. ///
  81. /// Note: the caller must ensure that both the quotient and remainder will fit into a single digit.
  82. /// This is _not_ true for an arbitrary numerator/denominator.
  83. ///
  84. /// (This function also matches what the x86 divide instruction does).
  85. #[inline]
  86. fn div_wide(hi: BigDigit, lo: BigDigit, divisor: BigDigit) -> (BigDigit, BigDigit) {
  87. debug_assert!(hi < divisor);
  88. let lhs = big_digit::to_doublebigdigit(hi, lo);
  89. let rhs = divisor as DoubleBigDigit;
  90. ((lhs / rhs) as BigDigit, (lhs % rhs) as BigDigit)
  91. }
  92. pub fn div_rem_digit(mut a: BigUint, b: BigDigit) -> (BigUint, BigDigit) {
  93. let mut rem = 0;
  94. for d in a.data.iter_mut().rev() {
  95. let (q, r) = div_wide(rem, *d, b);
  96. *d = q;
  97. rem = r;
  98. }
  99. (a.normalized(), rem)
  100. }
  101. // Only for the Add impl:
  102. #[must_use]
  103. #[inline]
  104. pub fn __add2(a: &mut [BigDigit], b: &[BigDigit]) -> BigDigit {
  105. debug_assert!(a.len() >= b.len());
  106. let mut carry = 0;
  107. let (a_lo, a_hi) = a.split_at_mut(b.len());
  108. for (a, b) in a_lo.iter_mut().zip(b) {
  109. *a = adc(*a, *b, &mut carry);
  110. }
  111. if carry != 0 {
  112. for a in a_hi {
  113. *a = adc(*a, 0, &mut carry);
  114. if carry == 0 { break }
  115. }
  116. }
  117. carry
  118. }
  119. /// /Two argument addition of raw slices:
  120. /// a += b
  121. ///
  122. /// The caller _must_ ensure that a is big enough to store the result - typically this means
  123. /// resizing a to max(a.len(), b.len()) + 1, to fit a possible carry.
  124. pub fn add2(a: &mut [BigDigit], b: &[BigDigit]) {
  125. let carry = __add2(a, b);
  126. debug_assert!(carry == 0);
  127. }
  128. pub fn sub2(a: &mut [BigDigit], b: &[BigDigit]) {
  129. let mut borrow = 0;
  130. let len = cmp::min(a.len(), b.len());
  131. let (a_lo, a_hi) = a.split_at_mut(len);
  132. let (b_lo, b_hi) = b.split_at(len);
  133. for (a, b) in a_lo.iter_mut().zip(b_lo) {
  134. *a = sbb(*a, *b, &mut borrow);
  135. }
  136. if borrow != 0 {
  137. for a in a_hi {
  138. *a = sbb(*a, 0, &mut borrow);
  139. if borrow == 0 { break }
  140. }
  141. }
  142. // note: we're _required_ to fail on underflow
  143. assert!(borrow == 0 && b_hi.iter().all(|x| *x == 0),
  144. "Cannot subtract b from a because b is larger than a.");
  145. }
  146. pub fn sub2rev(a: &[BigDigit], b: &mut [BigDigit]) {
  147. debug_assert!(b.len() >= a.len());
  148. let mut borrow = 0;
  149. let len = cmp::min(a.len(), b.len());
  150. let (a_lo, a_hi) = a.split_at(len);
  151. let (b_lo, b_hi) = b.split_at_mut(len);
  152. for (a, b) in a_lo.iter().zip(b_lo) {
  153. *b = sbb(*a, *b, &mut borrow);
  154. }
  155. assert!(a_hi.is_empty());
  156. // note: we're _required_ to fail on underflow
  157. assert!(borrow == 0 && b_hi.iter().all(|x| *x == 0),
  158. "Cannot subtract b from a because b is larger than a.");
  159. }
  160. pub fn sub_sign(a: &[BigDigit], b: &[BigDigit]) -> (Sign, BigUint) {
  161. // Normalize:
  162. let a = &a[..a.iter().rposition(|&x| x != 0).map_or(0, |i| i + 1)];
  163. let b = &b[..b.iter().rposition(|&x| x != 0).map_or(0, |i| i + 1)];
  164. match cmp_slice(a, b) {
  165. Greater => {
  166. let mut a = a.to_vec();
  167. sub2(&mut a, b);
  168. (Plus, BigUint::new(a))
  169. }
  170. Less => {
  171. let mut b = b.to_vec();
  172. sub2(&mut b, a);
  173. (Minus, BigUint::new(b))
  174. }
  175. _ => (NoSign, Zero::zero()),
  176. }
  177. }
  178. /// Three argument multiply accumulate:
  179. /// acc += b * c
  180. fn mac_digit(acc: &mut [BigDigit], b: &[BigDigit], c: BigDigit) {
  181. if c == 0 {
  182. return;
  183. }
  184. let mut b_iter = b.iter();
  185. let mut carry = 0;
  186. for ai in acc.iter_mut() {
  187. if let Some(bi) = b_iter.next() {
  188. *ai = mac_with_carry(*ai, *bi, c, &mut carry);
  189. } else if carry != 0 {
  190. *ai = mac_with_carry(*ai, 0, c, &mut carry);
  191. } else {
  192. break;
  193. }
  194. }
  195. assert!(carry == 0);
  196. }
  197. /// Three argument multiply accumulate:
  198. /// acc += b * c
  199. fn mac3(acc: &mut [BigDigit], b: &[BigDigit], c: &[BigDigit]) {
  200. let (x, y) = if b.len() < c.len() {
  201. (b, c)
  202. } else {
  203. (c, b)
  204. };
  205. // Karatsuba multiplication is slower than long multiplication for small x and y:
  206. //
  207. if x.len() <= 16 {
  208. for (i, xi) in x.iter().enumerate() {
  209. mac_digit(&mut acc[i..], y, *xi);
  210. }
  211. } else if x.len() <= 300 {
  212. /*
  213. * Karatsuba multiplication:
  214. *
  215. * The idea is that we break x and y up into two smaller numbers that each have about half
  216. * as many digits, like so (note that multiplying by b is just a shift):
  217. *
  218. * x = x0 + x1 * b
  219. * y = y0 + y1 * b
  220. *
  221. * With some algebra, we can compute x * y with three smaller products, where the inputs to
  222. * each of the smaller products have only about half as many digits as x and y:
  223. *
  224. * x * y = (x0 + x1 * b) * (y0 + y1 * b)
  225. *
  226. * x * y = x0 * y0
  227. * + x0 * y1 * b
  228. * + x1 * y0 * b
  229. * + x1 * y1 * b^2
  230. *
  231. * Let p0 = x0 * y0 and p2 = x1 * y1:
  232. *
  233. * x * y = p0
  234. * + (x0 * y1 + x1 * y0) * b
  235. * + p2 * b^2
  236. *
  237. * The real trick is that middle term:
  238. *
  239. * x0 * y1 + x1 * y0
  240. *
  241. * = x0 * y1 + x1 * y0 - p0 + p0 - p2 + p2
  242. *
  243. * = x0 * y1 + x1 * y0 - x0 * y0 - x1 * y1 + p0 + p2
  244. *
  245. * Now we complete the square:
  246. *
  247. * = -(x0 * y0 - x0 * y1 - x1 * y0 + x1 * y1) + p0 + p2
  248. *
  249. * = -((x1 - x0) * (y1 - y0)) + p0 + p2
  250. *
  251. * Let p1 = (x1 - x0) * (y1 - y0), and substitute back into our original formula:
  252. *
  253. * x * y = p0
  254. * + (p0 + p2 - p1) * b
  255. * + p2 * b^2
  256. *
  257. * Where the three intermediate products are:
  258. *
  259. * p0 = x0 * y0
  260. * p1 = (x1 - x0) * (y1 - y0)
  261. * p2 = x1 * y1
  262. *
  263. * In doing the computation, we take great care to avoid unnecessary temporary variables
  264. * (since creating a BigUint requires a heap allocation): thus, we rearrange the formula a
  265. * bit so we can use the same temporary variable for all the intermediate products:
  266. *
  267. * x * y = p2 * b^2 + p2 * b
  268. * + p0 * b + p0
  269. * - p1 * b
  270. *
  271. * The other trick we use is instead of doing explicit shifts, we slice acc at the
  272. * appropriate offset when doing the add.
  273. */
  274. /*
  275. * When x is smaller than y, it's significantly faster to pick b such that x is split in
  276. * half, not y:
  277. */
  278. let b = x.len() / 2;
  279. let (x0, x1) = x.split_at(b);
  280. let (y0, y1) = y.split_at(b);
  281. /*
  282. * We reuse the same BigUint for all the intermediate multiplies and have to size p
  283. * appropriately here: x1.len() >= x0.len and y1.len() >= y0.len():
  284. */
  285. let len = x1.len() + y1.len() + 1;
  286. let mut p = BigUint { data: vec![0; len] };
  287. // p2 = x1 * y1
  288. mac3(&mut p.data[..], x1, y1);
  289. // Not required, but the adds go faster if we drop any unneeded 0s from the end:
  290. p.normalize();
  291. add2(&mut acc[b..], &p.data[..]);
  292. add2(&mut acc[b * 2..], &p.data[..]);
  293. // Zero out p before the next multiply:
  294. p.data.truncate(0);
  295. p.data.extend(repeat(0).take(len));
  296. // p0 = x0 * y0
  297. mac3(&mut p.data[..], x0, y0);
  298. p.normalize();
  299. add2(&mut acc[..], &p.data[..]);
  300. add2(&mut acc[b..], &p.data[..]);
  301. // p1 = (x1 - x0) * (y1 - y0)
  302. // We do this one last, since it may be negative and acc can't ever be negative:
  303. let (j0_sign, j0) = sub_sign(x1, x0);
  304. let (j1_sign, j1) = sub_sign(y1, y0);
  305. match j0_sign * j1_sign {
  306. Plus => {
  307. p.data.truncate(0);
  308. p.data.extend(repeat(0).take(len));
  309. mac3(&mut p.data[..], &j0.data[..], &j1.data[..]);
  310. p.normalize();
  311. sub2(&mut acc[b..], &p.data[..]);
  312. },
  313. Minus => {
  314. mac3(&mut acc[b..], &j0.data[..], &j1.data[..]);
  315. },
  316. NoSign => (),
  317. }
  318. } else {
  319. let i = y.len()/3 + 1;
  320. let x0_len = cmp::min(x.len(), i);
  321. let x1_len = cmp::min(x.len() - x0_len, i);
  322. let y0_len = i;
  323. let y1_len = cmp::min(y.len() - y0_len, i);
  324. let x0 = BigInt::from_slice(Plus, &x[..x0_len]);
  325. let x1 = BigInt::from_slice(Plus, &x[x0_len..x0_len + x1_len]);
  326. let x2 = BigInt::from_slice(Plus, &x[x0_len + x1_len..]);
  327. let y0 = BigInt::from_slice(Plus, &y[..y0_len]);
  328. let y1 = BigInt::from_slice(Plus, &y[y0_len..y0_len + y1_len]);
  329. let y2 = BigInt::from_slice(Plus, &y[y0_len + y1_len..]);
  330. let r0 = &x0 * &y0;
  331. let r4 = &x2 * &y2;
  332. let r1 = (&x0 + &x1 + &x2) * (&y0 + &y1 + &y2);
  333. let r2 = (&x0 - &x1 + &x2) * (&y0 - &y1 + &y2);
  334. let r3 = (x0 - x1*2 + x2*4) * (y0 - y1*2 + y2*4);
  335. let mut comp3: BigInt = (&r3 - &r1) / 3;
  336. let mut comp1: BigInt = (&r1 - &r2) / 2;
  337. let mut comp2: BigInt = &r2 - &r0;
  338. comp3 = (&comp2 - &comp3)/2 + &r4*2;
  339. comp2 = comp2 + &comp1 - &r4;
  340. comp1 = comp1 - &comp3;
  341. let result = r0 + (comp1 << 32*i) + (comp2 << 2*32*i) + (comp3 << 3*32*i) + (r4 << 4*32*i);
  342. let result_pos = result.to_biguint().unwrap();
  343. add2(&mut acc[..], &result_pos.data);
  344. }
  345. }
  346. pub fn mul3(x: &[BigDigit], y: &[BigDigit]) -> BigUint {
  347. let len = x.len() + y.len() + 1;
  348. let mut prod = BigUint { data: vec![0; len] };
  349. mac3(&mut prod.data[..], x, y);
  350. prod.normalized()
  351. }
  352. pub fn scalar_mul(a: &mut [BigDigit], b: BigDigit) -> BigDigit {
  353. let mut carry = 0;
  354. for a in a.iter_mut() {
  355. *a = mul_with_carry(*a, b, &mut carry);
  356. }
  357. carry
  358. }
  359. pub fn div_rem(u: &BigUint, d: &BigUint) -> (BigUint, BigUint) {
  360. if d.is_zero() {
  361. panic!()
  362. }
  363. if u.is_zero() {
  364. return (Zero::zero(), Zero::zero());
  365. }
  366. if *d == One::one() {
  367. return (u.clone(), Zero::zero());
  368. }
  369. // Required or the q_len calculation below can underflow:
  370. match u.cmp(d) {
  371. Less => return (Zero::zero(), u.clone()),
  372. Equal => return (One::one(), Zero::zero()),
  373. Greater => {} // Do nothing
  374. }
  375. // This algorithm is from Knuth, TAOCP vol 2 section 4.3, algorithm D:
  376. //
  377. // First, normalize the arguments so the highest bit in the highest digit of the divisor is
  378. // set: the main loop uses the highest digit of the divisor for generating guesses, so we
  379. // want it to be the largest number we can efficiently divide by.
  380. //
  381. let shift = d.data.last().unwrap().leading_zeros() as usize;
  382. let mut a = u << shift;
  383. let b = d << shift;
  384. // The algorithm works by incrementally calculating "guesses", q0, for part of the
  385. // remainder. Once we have any number q0 such that q0 * b <= a, we can set
  386. //
  387. // q += q0
  388. // a -= q0 * b
  389. //
  390. // and then iterate until a < b. Then, (q, a) will be our desired quotient and remainder.
  391. //
  392. // q0, our guess, is calculated by dividing the last few digits of a by the last digit of b
  393. // - this should give us a guess that is "close" to the actual quotient, but is possibly
  394. // greater than the actual quotient. If q0 * b > a, we simply use iterated subtraction
  395. // until we have a guess such that q0 * b <= a.
  396. //
  397. let bn = *b.data.last().unwrap();
  398. let q_len = a.data.len() - b.data.len() + 1;
  399. let mut q = BigUint { data: vec![0; q_len] };
  400. // We reuse the same temporary to avoid hitting the allocator in our inner loop - this is
  401. // sized to hold a0 (in the common case; if a particular digit of the quotient is zero a0
  402. // can be bigger).
  403. //
  404. let mut tmp = BigUint { data: Vec::with_capacity(2) };
  405. for j in (0..q_len).rev() {
  406. /*
  407. * When calculating our next guess q0, we don't need to consider the digits below j
  408. * + b.data.len() - 1: we're guessing digit j of the quotient (i.e. q0 << j) from
  409. * digit bn of the divisor (i.e. bn << (b.data.len() - 1) - so the product of those
  410. * two numbers will be zero in all digits up to (j + b.data.len() - 1).
  411. */
  412. let offset = j + b.data.len() - 1;
  413. if offset >= a.data.len() {
  414. continue;
  415. }
  416. /* just avoiding a heap allocation: */
  417. let mut a0 = tmp;
  418. a0.data.truncate(0);
  419. a0.data.extend(a.data[offset..].iter().cloned());
  420. /*
  421. * q0 << j * big_digit::BITS is our actual quotient estimate - we do the shifts
  422. * implicitly at the end, when adding and subtracting to a and q. Not only do we
  423. * save the cost of the shifts, the rest of the arithmetic gets to work with
  424. * smaller numbers.
  425. */
  426. let (mut q0, _) = div_rem_digit(a0, bn);
  427. let mut prod = &b * &q0;
  428. while cmp_slice(&prod.data[..], &a.data[j..]) == Greater {
  429. let one: BigUint = One::one();
  430. q0 = q0 - one;
  431. prod = prod - &b;
  432. }
  433. add2(&mut q.data[j..], &q0.data[..]);
  434. sub2(&mut a.data[j..], &prod.data[..]);
  435. a.normalize();
  436. tmp = q0;
  437. }
  438. debug_assert!(a < b);
  439. (q.normalized(), a >> shift)
  440. }
  441. /// Find last set bit
  442. /// fls(0) == 0, fls(u32::MAX) == 32
  443. pub fn fls<T: traits::PrimInt>(v: T) -> usize {
  444. mem::size_of::<T>() * 8 - v.leading_zeros() as usize
  445. }
  446. pub fn ilog2<T: traits::PrimInt>(v: T) -> usize {
  447. fls(v) - 1
  448. }
  449. #[inline]
  450. pub fn biguint_shl(n: Cow<BigUint>, bits: usize) -> BigUint {
  451. let n_unit = bits / big_digit::BITS;
  452. let mut data = match n_unit {
  453. 0 => n.into_owned().data,
  454. _ => {
  455. let len = n_unit + n.data.len() + 1;
  456. let mut data = Vec::with_capacity(len);
  457. data.extend(repeat(0).take(n_unit));
  458. data.extend(n.data.iter().cloned());
  459. data
  460. }
  461. };
  462. let n_bits = bits % big_digit::BITS;
  463. if n_bits > 0 {
  464. let mut carry = 0;
  465. for elem in data[n_unit..].iter_mut() {
  466. let new_carry = *elem >> (big_digit::BITS - n_bits);
  467. *elem = (*elem << n_bits) | carry;
  468. carry = new_carry;
  469. }
  470. if carry != 0 {
  471. data.push(carry);
  472. }
  473. }
  474. BigUint::new(data)
  475. }
  476. #[inline]
  477. pub fn biguint_shr(n: Cow<BigUint>, bits: usize) -> BigUint {
  478. let n_unit = bits / big_digit::BITS;
  479. if n_unit >= n.data.len() {
  480. return Zero::zero();
  481. }
  482. let mut data = match n_unit {
  483. 0 => n.into_owned().data,
  484. _ => n.data[n_unit..].to_vec(),
  485. };
  486. let n_bits = bits % big_digit::BITS;
  487. if n_bits > 0 {
  488. let mut borrow = 0;
  489. for elem in data.iter_mut().rev() {
  490. let new_borrow = *elem << (big_digit::BITS - n_bits);
  491. *elem = (*elem >> n_bits) | borrow;
  492. borrow = new_borrow;
  493. }
  494. }
  495. BigUint::new(data)
  496. }
  497. pub fn cmp_slice(a: &[BigDigit], b: &[BigDigit]) -> Ordering {
  498. debug_assert!(a.last() != Some(&0));
  499. debug_assert!(b.last() != Some(&0));
  500. let (a_len, b_len) = (a.len(), b.len());
  501. if a_len < b_len {
  502. return Less;
  503. }
  504. if a_len > b_len {
  505. return Greater;
  506. }
  507. for (&ai, &bi) in a.iter().rev().zip(b.iter().rev()) {
  508. if ai < bi {
  509. return Less;
  510. }
  511. if ai > bi {
  512. return Greater;
  513. }
  514. }
  515. return Equal;
  516. }
  517. #[cfg(test)]
  518. mod algorithm_tests {
  519. use {BigDigit, BigUint, BigInt};
  520. use Sign::Plus;
  521. use traits::Num;
  522. #[test]
  523. fn test_sub_sign() {
  524. use super::sub_sign;
  525. fn sub_sign_i(a: &[BigDigit], b: &[BigDigit]) -> BigInt {
  526. let (sign, val) = sub_sign(a, b);
  527. BigInt::from_biguint(sign, val)
  528. }
  529. let a = BigUint::from_str_radix("265252859812191058636308480000000", 10).unwrap();
  530. let b = BigUint::from_str_radix("26525285981219105863630848000000", 10).unwrap();
  531. let a_i = BigInt::from_biguint(Plus, a.clone());
  532. let b_i = BigInt::from_biguint(Plus, b.clone());
  533. assert_eq!(sub_sign_i(&a.data[..], &b.data[..]), &a_i - &b_i);
  534. assert_eq!(sub_sign_i(&b.data[..], &a.data[..]), &b_i - &a_i);
  535. }
  536. }