[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[341696] | 2 | /* $Id$ */ |
---|
[2dd068] | 3 | |
---|
[e4fe2b] | 4 | #include "config.h" |
---|
[b973c0] | 5 | |
---|
[650f2d8] | 6 | #include "cf_assert.h" |
---|
[b973c0] | 7 | |
---|
[2dd068] | 8 | #include "cf_defs.h" |
---|
| 9 | #include "cf_random.h" |
---|
| 10 | #include "cf_util.h" |
---|
| 11 | #include "fac_cantzass.h" |
---|
| 12 | #include "fac_sqrfree.h" |
---|
| 13 | #include "gmpext.h" |
---|
| 14 | |
---|
[ee668e] | 15 | #include <factory/cf_gmp.h> |
---|
[daa556] | 16 | |
---|
[2dd068] | 17 | static CanonicalForm randomPoly( int n, const Variable & x, const CFRandom & gen ); |
---|
| 18 | |
---|
| 19 | static CFFList CantorZassenhausFactorFFGF( const CanonicalForm & f, int d, int q, const CFRandom & ); |
---|
| 20 | |
---|
[a52291] | 21 | static CFFList CantorZassenhausFactorExt( const CanonicalForm & g, int s, mpz_t q, const CFRandom & gen ); |
---|
[2dd068] | 22 | |
---|
| 23 | static CFFList distinctDegreeFactorFFGF ( const CanonicalForm & f, int q ); |
---|
| 24 | |
---|
| 25 | static CFFList distinctDegreeFactorExt ( const CanonicalForm & f, int p, int n ); |
---|
| 26 | |
---|
| 27 | // calculates f^m % d |
---|
| 28 | |
---|
| 29 | static CanonicalForm powerMod( const CanonicalForm & f, int m, const CanonicalForm & d ); |
---|
| 30 | |
---|
| 31 | // calculates f^(p^s) % d |
---|
| 32 | |
---|
| 33 | static CanonicalForm powerMod( const CanonicalForm & f, int p, int s, const CanonicalForm & d ); |
---|
| 34 | |
---|
| 35 | // calculates f^((p^s-1)/2) % d |
---|
| 36 | |
---|
| 37 | static CanonicalForm powerMod2( const CanonicalForm & f, int p, int s, const CanonicalForm & d ); |
---|
| 38 | |
---|
[a52291] | 39 | static CanonicalForm powerMod2( const CanonicalForm & f, mpz_t q, int s, const CanonicalForm & d ); |
---|
[2dd068] | 40 | |
---|
[9c9e2a4] | 41 | CFFList FpFactorizeUnivariateCZ( const CanonicalForm& f, bool issqrfree, int numext, const Variable alpha, const Variable beta ) |
---|
[2dd068] | 42 | { |
---|
| 43 | CFFList F, G, H, HH; |
---|
| 44 | CanonicalForm fac; |
---|
| 45 | ListIterator<CFFactor> i, j, k; |
---|
| 46 | int d, q, n = 0; |
---|
| 47 | bool galoisfield = getGFDegree() > 1; |
---|
[a52291] | 48 | mpz_t qq; |
---|
[2dd068] | 49 | |
---|
| 50 | if ( galoisfield ) |
---|
[cd86ac] | 51 | q = ipower( getCharacteristic(), getGFDegree() ); |
---|
[2dd068] | 52 | else |
---|
[cd86ac] | 53 | q = getCharacteristic(); |
---|
[2dd068] | 54 | if ( numext > 0 ) { |
---|
[cd86ac] | 55 | if ( numext == 1 ) |
---|
| 56 | n = getMipo( alpha ).degree(); |
---|
| 57 | else |
---|
| 58 | n = getMipo( alpha ).degree() * getMipo( beta ).degree(); |
---|
[a52291] | 59 | mpz_init( qq ); |
---|
| 60 | mpz_ui_pow_ui ( qq, q, n ); |
---|
[2dd068] | 61 | } |
---|
| 62 | if ( LC( f ).isOne() ) |
---|
[cd86ac] | 63 | if ( issqrfree ) |
---|
| 64 | F.append( CFFactor( f, 1 ) ); |
---|
| 65 | else |
---|
| 66 | F = sqrFreeFp( f ); |
---|
[2dd068] | 67 | else { |
---|
[cd86ac] | 68 | if ( issqrfree ) |
---|
| 69 | F.append( CFFactor( f / LC( f ), 1 ) ); |
---|
| 70 | else |
---|
| 71 | F = sqrFreeFp( f / LC( f ) ); |
---|
| 72 | H.append( LC( f ) ); |
---|
[2dd068] | 73 | } |
---|
| 74 | for ( i = F; i.hasItem(); ++i ) { |
---|
[cd86ac] | 75 | d = i.getItem().exp(); |
---|
| 76 | if ( numext > 0 ) |
---|
| 77 | G = distinctDegreeFactorExt( i.getItem().factor(), q, n ); |
---|
| 78 | else |
---|
| 79 | G = distinctDegreeFactorFFGF( i.getItem().factor(), q ); |
---|
| 80 | for ( j = G; j.hasItem(); ++j ) { |
---|
| 81 | if ( numext > 0 ) { |
---|
[0509dde] | 82 | if ( numext == 1 ) { |
---|
| 83 | AlgExtRandomF tmpalpha( alpha ); |
---|
[a52291] | 84 | HH = CantorZassenhausFactorExt( j.getItem().factor(), j.getItem().exp(), qq, tmpalpha ); |
---|
[0509dde] | 85 | } |
---|
| 86 | else { |
---|
| 87 | AlgExtRandomF tmpalphabeta( alpha, beta ); |
---|
[a52291] | 88 | HH = CantorZassenhausFactorExt( j.getItem().factor(), j.getItem().exp(), qq, tmpalphabeta ); |
---|
[0509dde] | 89 | } |
---|
[cd86ac] | 90 | } |
---|
| 91 | else if ( galoisfield ) |
---|
| 92 | HH = CantorZassenhausFactorFFGF( j.getItem().factor(), j.getItem().exp(), q, GFRandom() ); |
---|
| 93 | else |
---|
| 94 | HH = CantorZassenhausFactorFFGF( j.getItem().factor(), j.getItem().exp(), q, FFRandom() ); |
---|
| 95 | for ( k = HH; k.hasItem(); ++k ) { |
---|
| 96 | fac = k.getItem().factor(); |
---|
| 97 | H.append( CFFactor( fac / LC( fac ), d ) ); |
---|
| 98 | } |
---|
| 99 | } |
---|
[2dd068] | 100 | } |
---|
| 101 | if ( numext > 0 ) |
---|
[a52291] | 102 | mpz_clear( qq ); |
---|
[2dd068] | 103 | return H; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | CFFList distinctDegreeFactorFFGF ( const CanonicalForm & f, int q ) |
---|
| 107 | { |
---|
| 108 | int i; |
---|
| 109 | Variable x = f.mvar(); |
---|
| 110 | CanonicalForm g = f, h, r = x; |
---|
| 111 | CFFList F; |
---|
| 112 | i = 1; |
---|
| 113 | while ( g.degree(x) > 0 && i <= g.degree(x) ) { |
---|
[cd86ac] | 114 | r = powerMod( r, q, g ); |
---|
| 115 | h = gcd( g, r - x ); |
---|
| 116 | if ( h.degree(x) > 0 ) { |
---|
| 117 | F.append( CFFactor( h, i ) ); |
---|
| 118 | g /= h; |
---|
| 119 | } |
---|
| 120 | i++; |
---|
[2dd068] | 121 | } |
---|
| 122 | ASSERT( g.degree(x) == 0, "fatal fatal" ); |
---|
| 123 | return F; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | CFFList distinctDegreeFactorExt ( const CanonicalForm & f, int p, int n ) |
---|
| 127 | { |
---|
| 128 | int i; |
---|
| 129 | Variable x = f.mvar(); |
---|
| 130 | CanonicalForm g = f, h, r = x; |
---|
| 131 | CFFList F; |
---|
| 132 | i = 1; |
---|
| 133 | while ( g.degree(x) > 0 && i <= g.degree(x) ) { |
---|
[cd86ac] | 134 | r = powerMod( r, p, n, g ); |
---|
| 135 | h = gcd( g, r - x ); |
---|
| 136 | if ( h.degree(x) > 0 ) { |
---|
| 137 | F.append( CFFactor( h, i ) ); |
---|
| 138 | g /= h; |
---|
| 139 | } |
---|
| 140 | i++; |
---|
[2dd068] | 141 | } |
---|
| 142 | ASSERT( g.degree(x) == 0, "fatal fatal" ); |
---|
| 143 | return F; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | CFFList CantorZassenhausFactorFFGF( const CanonicalForm & g, int s, int q, const CFRandom & gen ) |
---|
| 147 | { |
---|
| 148 | CanonicalForm f = g; |
---|
| 149 | CanonicalForm b, f1; |
---|
| 150 | int d, d1; |
---|
| 151 | Variable x = f.mvar(); |
---|
[b973c0] | 152 | |
---|
[2dd068] | 153 | if ( (d=f.degree(x)) == s ) |
---|
[cd86ac] | 154 | return CFFactor( f, 1 ); |
---|
[2dd068] | 155 | else while ( 1 ) { |
---|
[cd86ac] | 156 | b = randomPoly( d, x, gen ); |
---|
| 157 | f1 = gcd( b, f ); |
---|
| 158 | if ( (d1 = f1.degree(x)) > 0 && d1 < d ) { |
---|
| 159 | CFFList firstFactor = CantorZassenhausFactorFFGF( f1, s, q, gen ); |
---|
| 160 | CFFList secondFactor = CantorZassenhausFactorFFGF( f/f1, s, q, gen ); |
---|
| 161 | return Union( firstFactor, secondFactor ); |
---|
| 162 | } else { |
---|
| 163 | f1 = gcd( f, powerMod2( b, q, s, f ) - 1 ); |
---|
| 164 | if ( (d1 = f1.degree(x)) > 0 && d1 < d ) { |
---|
| 165 | CFFList firstFactor = CantorZassenhausFactorFFGF( f1, s, q, gen ); |
---|
| 166 | CFFList secondFactor = CantorZassenhausFactorFFGF( f/f1, s, q, gen ); |
---|
| 167 | return Union( firstFactor, secondFactor ); |
---|
| 168 | } |
---|
| 169 | } |
---|
[2dd068] | 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
[a52291] | 173 | CFFList CantorZassenhausFactorExt( const CanonicalForm & g, int s, mpz_t q, const CFRandom & gen ) |
---|
[2dd068] | 174 | { |
---|
| 175 | CanonicalForm f = g; |
---|
| 176 | CanonicalForm b, f1; |
---|
| 177 | int d, d1; |
---|
| 178 | Variable x = f.mvar(); |
---|
[b973c0] | 179 | |
---|
[2dd068] | 180 | if ( (d=f.degree(x)) == s ) |
---|
[cd86ac] | 181 | return CFFactor( f, 1 ); |
---|
[2dd068] | 182 | else while ( 1 ) { |
---|
[cd86ac] | 183 | b = randomPoly( d, x, gen ); |
---|
| 184 | f1 = gcd( b, f ); |
---|
| 185 | if ( (d1 = f1.degree(x)) > 0 && d1 < d ) { |
---|
| 186 | CFFList firstFactor = CantorZassenhausFactorExt( f1, s, q, gen ); |
---|
| 187 | CFFList secondFactor = CantorZassenhausFactorExt( f/f1, s, q, gen ); |
---|
| 188 | return Union( firstFactor, secondFactor ); |
---|
| 189 | } else { |
---|
| 190 | f1 = gcd( f, powerMod2( b, q, s, f ) - 1 ); |
---|
| 191 | if ( (d1 = f1.degree(x)) > 0 && d1 < d ) { |
---|
| 192 | CFFList firstFactor = CantorZassenhausFactorExt( f1, s, q, gen ); |
---|
| 193 | CFFList secondFactor = CantorZassenhausFactorExt( f/f1, s, q, gen ); |
---|
| 194 | return Union( firstFactor, secondFactor ); |
---|
| 195 | } |
---|
| 196 | } |
---|
[2dd068] | 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | CanonicalForm randomPoly( int d, const Variable & x, const CFRandom & g ) |
---|
| 201 | { |
---|
| 202 | CanonicalForm result = 0; |
---|
| 203 | for ( int i = 0; i < d; i++ ) |
---|
[cd86ac] | 204 | result += power( x, i ) * g.generate(); |
---|
[2dd068] | 205 | result += power( x, d ); |
---|
| 206 | return result; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | CanonicalForm powerMod( const CanonicalForm & f, int m, const CanonicalForm & d ) |
---|
| 210 | { |
---|
| 211 | CanonicalForm prod = 1; |
---|
| 212 | CanonicalForm b = f % d; |
---|
| 213 | |
---|
| 214 | while ( m != 0 ) { |
---|
[cd86ac] | 215 | if ( m % 2 != 0 ) |
---|
| 216 | prod = (prod * b) % d; |
---|
| 217 | m /= 2; |
---|
| 218 | if ( m != 0 ) |
---|
| 219 | b = (b * b) % d; |
---|
[2dd068] | 220 | } |
---|
| 221 | return prod; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | CanonicalForm powerMod( const CanonicalForm & f, int p, int s, const CanonicalForm & d ) |
---|
| 225 | { |
---|
| 226 | CanonicalForm prod = 1; |
---|
| 227 | CanonicalForm b = f % d; |
---|
| 228 | int odd; |
---|
| 229 | |
---|
[a52291] | 230 | mpz_t m; |
---|
[b973c0] | 231 | |
---|
[a52291] | 232 | mpz_init( m ); |
---|
| 233 | mpz_ui_pow_ui ( m, p, s ); |
---|
| 234 | while ( mpz_cmp_si( m, 0 ) != 0 ) |
---|
[cacfb6] | 235 | { |
---|
[a52291] | 236 | odd = mpz_fdiv_q_ui( m, m, 2 ); |
---|
[cd86ac] | 237 | if ( odd != 0 ) |
---|
| 238 | prod = (prod * b) % d; |
---|
[a52291] | 239 | if ( mpz_cmp_si( m, 0 ) != 0 ) |
---|
[cd86ac] | 240 | b = (b*b) % d; |
---|
[2dd068] | 241 | } |
---|
[a52291] | 242 | mpz_clear( m ); |
---|
[2dd068] | 243 | return prod; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | CanonicalForm powerMod2( const CanonicalForm & f, int p, int s, const CanonicalForm & d ) |
---|
| 247 | { |
---|
| 248 | CanonicalForm prod = 1; |
---|
| 249 | CanonicalForm b = f % d; |
---|
| 250 | int odd; |
---|
| 251 | |
---|
[a52291] | 252 | mpz_t m; |
---|
[b973c0] | 253 | |
---|
[a52291] | 254 | mpz_init( m ); |
---|
| 255 | mpz_ui_pow_ui ( m, p, s ); |
---|
| 256 | mpz_sub_ui( m, m, 1 ); |
---|
| 257 | mpz_fdiv_q_ui( m, m, 2 ); |
---|
| 258 | while ( mpz_cmp_si( m, 0 ) != 0 ) |
---|
[cacfb6] | 259 | { |
---|
[a52291] | 260 | odd = mpz_fdiv_q_ui( m, m, 2 ); |
---|
[cd86ac] | 261 | if ( odd != 0 ) |
---|
| 262 | prod = (prod * b) % d; |
---|
[a52291] | 263 | if ( mpz_cmp_si( m, 0 ) != 0 ) |
---|
[cd86ac] | 264 | b = (b*b) % d; |
---|
[2dd068] | 265 | } |
---|
[a52291] | 266 | mpz_clear( m ); |
---|
[2dd068] | 267 | return prod; |
---|
| 268 | } |
---|
| 269 | |
---|
[a52291] | 270 | CanonicalForm powerMod2( const CanonicalForm & f, mpz_t q, int s, const CanonicalForm & d ) |
---|
[2dd068] | 271 | { |
---|
| 272 | CanonicalForm prod = 1; |
---|
| 273 | CanonicalForm b = f % d; |
---|
| 274 | int odd; |
---|
| 275 | |
---|
[a52291] | 276 | mpz_t m; |
---|
[b973c0] | 277 | |
---|
[a52291] | 278 | mpz_init( m ); |
---|
| 279 | mpz_pow_ui( m, q, s ); |
---|
| 280 | mpz_sub_ui( m, m, 1 ); |
---|
| 281 | mpz_fdiv_q_ui( m, m, 2 ); |
---|
| 282 | while ( mpz_cmp_si( m, 0 ) != 0 ) |
---|
[cacfb6] | 283 | { |
---|
[a52291] | 284 | odd = mpz_fdiv_q_ui( m, m, 2 ); |
---|
[cd86ac] | 285 | if ( odd != 0 ) |
---|
| 286 | prod = (prod * b) % d; |
---|
[a52291] | 287 | if ( mpz_cmp_si( m, 0 ) != 0 ) |
---|
[cd86ac] | 288 | b = (b*b) % d; |
---|
[2dd068] | 289 | } |
---|
[a52291] | 290 | mpz_clear( m ); |
---|
[2dd068] | 291 | return prod; |
---|
| 292 | } |
---|