1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/* This file is part of the gf2x library.
Copyright 2007, 2008, 2009
Richard Brent, Pierrick Gaudry, Emmanuel Thome', Paul Zimmermann
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along
with this program; see the file COPYING. If not, write to the Free
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02111-1307, USA.
*/
/* Subquadratic gcd over GF(2)[x]. */
// #define BOGONG // Selects routines that run well on bogong,
// a 2.2 Ghz AMD Opteron 275 at ANU
#undef STATS // If defined, statistics will be gathered
// and printed on stdout
#include <NTL/GF2X.h>
#include "halfgcd.hpp"
/* This threshold controls the internal calls to HalfGCD. */
#define NTL_GF2X_HalfGCD_CROSSOVER (4*NTL_BITS_PER_LONG)
/* this threshold controls the calls in FastGCD, thus is less sensitive */
#define NTL_GF2X_GCD_CROSSOVER (5*NTL_GF2X_HalfGCD_CROSSOVER)
#ifdef STATS
long mulktm = 0x3fffff; // mask for mul1kt, mul2kt
long addktm = 0xffff; // mask for Mul1kt, Add1kt
long mul1kt = 0; // counts calls to mul1 or mul1rpb
long mul2kt = 0; // counts calls to mul2 or mul2t
long Mul1kt = 0; // counts calls to mul_1_n
long Mul1kts = 0; // sum of sizes sb in calls to mul_1_n
long Add1kt = 0; // counts calls to addmul_1_n
long Add1kts = 0; // sum of sizes sb in calls to addmul_1_n
#endif
#include "gf2x.h"
using namespace NTL;
class GF2XMatrix {
private:
GF2XMatrix(const GF2XMatrix&); // disable
GF2X elts[2][2];
public:
GF2XMatrix() { }
~GF2XMatrix() { }
void operator=(const GF2XMatrix&);
GF2X& operator() (long i, long j) { return elts[i][j]; }
const GF2X& operator() (long i, long j) const { return elts[i][j]; }
};
void mul_gen(GF2X& c, const GF2X& a, const GF2X& b)
{
long sa = a.xrep.length();
long sb = b.xrep.length();
if (sa <= 0 || sb <= 0) {
clear(c);
return;
}
c.xrep.SetLength(sa+sb);
/* gf2x_mul now properly handles possible aliasing a==c and b==c */
gf2x_mul(c.xrep.elts(), a.xrep.elts(), sa, b.xrep.elts(), sb);
c.normalize();
}
void
mul (GF2X& U, GF2X& V, const GF2XMatrix& M)
// (U, V)^T = M*(U, V)^T
{
GF2X RU, RV, R1, R2;
// RU = U
// RV = V
// R1 = M(0,0)
mul_gen(R1, M(0,0), U);
// R2 = M(0,1)
mul_gen(R2, M(0,1), V);
add(R2, R1, R2);
// R1 = M(1,0)
mul_gen(R1, M(1,0), U);
U = R2; // previous value
// R2 = M(1,1)
mul_gen(R2, M(1,1), V);
add(V, R1, R2);
}
void
mul (GF2XMatrix& A, GF2XMatrix& B, GF2XMatrix& C)
// A = B*C, B and C are destroyed
{
GF2X B00, B01, B10, B11, C0, C1, T1, T2;
mul_gen(T1, B(0,0), C(0,0));
mul_gen(T2, B(0,1), C(1,0));
add(A(0,0), T1, T2);
mul_gen(T1, B(1,0), C(0,0));
mul_gen(T2, B(1,1), C(1,0));
add(A(1,0), T1, T2);
C(0,0).kill();
C(1,0).kill();
mul_gen(T1, B(0,0), C(0,1));
mul_gen(T2, B(0,1), C(1,1));
add(A(0,1), T1, T2);
mul_gen(T1, B(1,0), C(0,1));
mul_gen(T2, B(1,1), C(1,1));
add(A(1,1), T1, T2);
C(0,1).kill();
C(1,1).kill();
B(0,0).kill();
B(1,0).kill();
B(0,1).kill();
B(1,1).kill();
}
void
strassen_mul (GF2XMatrix& C, GF2XMatrix& A, GF2XMatrix& B)
// C = A*B, A and B are destroyed
/* we follow the code from SAGE 1.6, file strassen.pyx */
{
GF2X S0, T0, S1, T1, S2, T2, S3, T3, P0, P1, P2, P3, P4, P5, P6;
add (S0, A(1,0), A(1,1));
add (T0, B(0,1), B(0,0));
add (S1, S0, A(0,0));
add (T1, B(1,1), T0);
add (S2, A(0,0), A(1,0));
add (T2, B(1,1), B(0,1));
add (S3, A(0,1), S1);
add (T3, B(1,0), T1);
mul_gen (P0, A(0,0), B(0,0));
mul_gen (P1, A(0,1), B(1,0));
mul_gen (P2, S0, T0);
mul_gen (P3, S1, T1);
mul_gen (P4, S2, T2);
mul_gen (P5, S3, B(1,1));
mul_gen (P6, A(1,1), T3);
add (C(0,0), P0, P1); /* U0 */
add (C(0,1), P0, P3); /* U1 */
add (C(1,1), C(0,1), P4); /* U2 */
add (C(1,0), C(1,1), P6); /* U3 */
add (C(1,1), C(1,1), P2); /* U4 */
add (C(0,1), C(0,1), P2); /* U5 */
add (C(0,1), C(0,1), P5); /* U6 */
A(0,0).kill();
A(1,0).kill();
A(0,1).kill();
A(1,1).kill();
B(0,0).kill();
B(1,0).kill();
B(0,1).kill();
B(1,1).kill();
}
void
IterHalfGCD (GF2XMatrix& M_out, GF2X& U, GF2X& V, long d_red)
{
M_out(0,0).SetMaxLength(d_red);
M_out(0,1).SetMaxLength(d_red);
M_out(1,0).SetMaxLength(d_red);
M_out(1,1).SetMaxLength(d_red);
set(M_out(0,0)); clear(M_out(0,1));
clear(M_out(1,0)); set(M_out(1,1));
long goal = deg(U) - d_red;
if (deg(V) <= goal)
return;
GF2X Q, t(INIT_SIZE, d_red);
while (deg(V) > goal) {
/* For the gcd of two polynomials of degree < 74207281,
this PlainDivRem() call takes about 20% of the total gcd time.
Here U and V have degree less than about 2*NTL_GF2X_HalfGCD_CROSSOVER,
and usually deg(V) = deg(U) - <small value>. */
PlainDivRem(Q, U, U, V);
swap(U, V);
mul_gen(t, Q, M_out(1,0));
add(t, M_out(0,0), t); // add=sub over GF2
M_out(0,0) = M_out(1,0);
M_out(1,0) = t;
mul_gen(t, Q, M_out(1,1));
add(t, M_out(0,1), t); // add=sub over GF2
M_out(0,1) = M_out(1,1);
M_out(1,1) = t;
}
}
void GF2XMatrix::operator=(const GF2XMatrix& M)
{
elts[0][0] = M.elts[0][0];
elts[0][1] = M.elts[0][1];
elts[1][0] = M.elts[1][0];
elts[1][1] = M.elts[1][1];
}
// c <- a mod x^n, with n >= 0
void
RightShiftRem (GF2X& c, const GF2X& a, long n)
{
if (deg(a) < n) // covers a=0 too
{
c = a;
return;
}
long sa = a.xrep.length(); // number of words of a
long wn = n / NTL_BITS_PER_LONG;
long bn = n - wn * NTL_BITS_PER_LONG; // #bits of ap[wn]
if (wn >= sa)
{
wn = sa;
bn = 0;
}
c.xrep.SetLength (wn + (bn != 0));
_ntl_ulong *cp = c.xrep.elts();
const _ntl_ulong *ap = a.xrep.elts();
long i;
for (i = 0; i < wn; i++)
cp[i] = ap[i];
if (bn)
cp[wn] = ap[wn] & ((1UL << bn) - 1UL);
c.normalize();
}
// same as HalfGCD, except U and V are replaced by U' and V'
// if extended is zero, the matrix M_out is not computed, and the inputs are
// fully reduced.
// We want to reduce deg(V) to <= deg(U) - d_red
// Usually if deg(U) = m, then d_red = m/2
void
HalfGCD2 (GF2XMatrix& M_out, GF2X& U, GF2X& V, long d_red, int extended)
{
long degU = deg(U);
if (IsZero(V) || deg(V) <= degU - d_red)
{
if (extended != 0)
{
set(M_out(0,0)); clear(M_out(0,1));
clear(M_out(1,0)); set(M_out(1,1));
}
return;
}
if (d_red <= NTL_GF2X_HalfGCD_CROSSOVER)
{
IterHalfGCD(M_out, U, V, d_red);
return;
}
long d1 = (d_red + 1) / 2; /* d1 is about m/4 */
if (extended == 0) d1 = deg(U) / 4;
if (d1 < 1) d1 = 1;
if (d1 >= d_red) d1 = d_red - 1;
long n = degU - 2 * d1 + 2; /* n is the ignored part, about m/2 */
if (n < 0) n = 0;
GF2X U1, V1;
if (n != 0)
{
RightShiftRem (U1, U, n); /* U1 = U mod x^n: m/2 bits */
RightShiftRem (V1, V, n); /* V1 = V mod x^n: m/2 bits */
RightShift (U, U, n); /* U = U div x^n has about m/2 bits */
RightShift (V, V, n); /* V = V div x^n has about m/2 bits */
}
GF2XMatrix M1;
HalfGCD2 (M1, U, V, d1, extended || (n != 0));
/* the entries of M1 have m/4 bits, and U,V have been reduced to m/4 bits */
if (n != 0)
{
LeftShift (U, U, n);
LeftShift (V, V, n);
mul (U1, V1, M1); /* U1,V1:m/2 M1:m/4 cost: 4 M(m/2,m/4) */
add (U, U, U1);
add (V, V, V1);
}
/* now U, V have 3m/4 bits */
// FIXME (24 June 2007): shouldn't it be deg(U) instead of deg(V) below?
long d2 = deg(V) - (degU - d_red); /* should be about m/2 */
if (IsZero(V) || d2 <= 0)
{
if (extended != 0)
M_out = M1;
return;
}
GF2X Q;
GF2XMatrix M2;
/* this PlainDivRem() call takes negligible time */
PlainDivRem (Q, U, U, V);
swap (U, V);
if (extended)
{
GF2X t;
mul_gen (t, Q, M1(1,0));
add (t, M1(0,0), t);
swap (M1(0,0), M1(1,0));
swap (M1(1,0), t);
mul_gen (t, Q, M1(1,1));
add (t, M1(0,1), t);
swap (M1(0,1), M1(1,1));
swap (M1(1,1), t);
}
else
d2 = deg(U); // fully reduce V
if (IsZero(V) || deg(V) <= degU - d_red)
{
if (extended)
M_out = M1;
return;
}
n = deg(U) - 2 * d2 + 2; /* should be about m/4 */
if (n < 0 || extended == 0) n = 0;
if (n != 0)
{
RightShiftRem (U1, U, n); /* U1,V1 have m/4 bits */
RightShiftRem (V1, V, n);
RightShift(U, U, n); /* U,V have m/2 bits */
RightShift(V, V, n);
}
HalfGCD2 (M2, U, V, d2, extended || (n != 0));
/* now U,V have m/4 bits, like the entries of M2 */
if (n != 0)
{
LeftShift (U, U, n);
LeftShift (V, V, n);
mul (U1, V1, M2); /* 4 M(m/4) */
add (U, U, U1);
add (V, V, V1);
}
if (extended)
strassen_mul (M_out, M2, M1); /* 8 M(m/4) */
}
/* in the general case, we have deg(u) and deg(v) about the same here */
void FastGCD(GF2X& d, const GF2X& u, const GF2X& v)
{
GF2X u1, v1;
GF2XMatrix M1;
u1 = u;
v1 = v;
if (deg(u1) == deg(v1)) {
if (IsZero(u1)) {
clear(d);
return;
}
rem(v1, v1, u1);
}
else if (deg(u1) < deg(v1)) {
swap(u1, v1);
}
// deg(u1) > deg(v1)
/* in practice this while loop will be performed only once,
since HalfGCD fully reduces the inputs */
while (deg(u1) > NTL_GF2X_GCD_CROSSOVER && !IsZero(v1))
{
HalfGCD2 (M1, u1, v1, deg (u1), 0);
if (!IsZero(v1))
{
rem(u1, u1, v1);
swap(u1, v1);
}
}
// base case.
GCD(d, u1, v1);
}
|