1 /* mpn_trialdiv -- find small factors of an mpn number using trial division. 2 3 Contributed to the GNU project by Torbjorn Granlund. 4 5 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY 6 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST 7 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE. 8 9 Copyright 2009, 2010, 2012 Free Software Foundation, Inc. 10 11 This file is part of the GNU MP Library. 12 13 The GNU MP Library is free software; you can redistribute it and/or modify 14 it under the terms of the GNU Lesser General Public License as published by 15 the Free Software Foundation; either version 3 of the License, or (at your 16 option) any later version. 17 18 The GNU MP Library is distributed in the hope that it will be useful, but 19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 20 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 21 License for more details. 22 23 You should have received a copy of the GNU Lesser General Public License 24 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 25 26 /* 27 This function finds the first (smallest) factor represented in 28 trialdivtab.h. It does not stop the factoring effort just because it has 29 reached some sensible limit, such as the square root of the input number. 30 31 The caller can limit the factoring effort by passing NPRIMES. The function 32 will then divide until that limit, or perhaps a few primes more. A position 33 which only mpn_trialdiv can make sense of is returned in the WHERE 34 parameter. It can be used for restarting the factoring effort; the first 35 call should pass 0 here. 36 37 Input: 1. A non-negative number T = {tp,tn} 38 2. NPRIMES as described above, 39 3. *WHERE as described above. 40 Output: 1. *WHERE updated as described above. 41 2. Return value is non-zero if we found a factor, else zero 42 To get the actual prime factor, compute the mod B inverse 43 of the return value. 44 */ 45 46 #include "gmp.h" 47 #include "gmp-impl.h" 48 49 struct gmp_primes_dtab { 50 mp_limb_t binv; 51 mp_limb_t lim; 52 }; 53 54 struct gmp_primes_ptab { 55 mp_limb_t ppp; /* primes, multiplied together */ 56 mp_limb_t cps[7]; /* ppp values pre-computed for mpn_mod_1s_4p */ 57 unsigned int idx:24; /* index of first primes in dtab */ 58 unsigned int np :8; /* number of primes related to this entry */ 59 }; 60 61 #define P(p,inv,lim) {inv,lim} 62 63 #include "trialdivtab.h" 64 65 #define PTAB_LINES (sizeof (gmp_primes_ptab) / sizeof (gmp_primes_ptab[0])) 66 67 /* FIXME: We could optimize out one of the outer loop conditions if we 68 had a final ptab entry with a huge nd field. */ 69 mp_limb_t 70 mpn_trialdiv (mp_srcptr tp, mp_size_t tn, mp_size_t nprimes, int *where) 71 { 72 mp_limb_t ppp; 73 mp_limb_t *cps; 74 struct gmp_primes_dtab *dp; 75 long i, j, idx, np; 76 mp_limb_t r, q; 77 78 ASSERT (tn >= 1); 79 80 for (i = *where; i < PTAB_LINES; i++) 81 { 82 ppp = gmp_primes_ptab[i].ppp; 83 cps = gmp_primes_ptab[i].cps; 84 85 r = mpn_mod_1s_4p (tp, tn, ppp << cps[1], cps); 86 87 idx = gmp_primes_ptab[i].idx; 88 np = gmp_primes_ptab[i].np; 89 90 /* Check divisibility by individual primes. */ 91 dp = &gmp_primes_dtab[idx] + np; 92 for (j = -np; j < 0; j++) 93 { 94 q = r * dp[j].binv; 95 if (q <= dp[j].lim) 96 { 97 *where = i; 98 return dp[j].binv; 99 } 100 } 101 102 nprimes -= np; 103 if (nprimes <= 0) 104 return 0; 105 } 106 return 0; 107 } 108