int test__ZmodF_mul_fft_split() { int success = 1; mpz_t x, y, z; mpz_init(x); mpz_init(y); mpz_init(z); mp_limb_t buf[300]; for (unsigned long n = 1; n < 200 && success; n++) { for (unsigned long depth = 0; ((n*FLINT_BITS) % (1 << depth) == 0) && success; depth++) { unsigned long bits = (n*FLINT_BITS) >> depth; unsigned long m = (bits-1)/FLINT_BITS + 1; ZmodF_poly_t poly; ZmodF_poly_init(poly, depth, m, 1); #if DEBUG printf("n = %d, depth = %d, m = %d\n", n, depth, m); #endif for (unsigned long trial = 0; trial < 120; trial++) { random_limbs(buf, n); buf[n] = 0; mpn_to_mpz(x, buf, n); _ZmodF_mul_fft_split(poly, buf, n); for (unsigned long i = 0; i < (1 << depth); i++) { mpz_tdiv_r_2exp(y, x, bits); mpz_tdiv_q_2exp(x, x, bits); mpn_to_mpz(z, poly->coeffs[i], m+1); if (mpz_cmp(z, y)) success = 0; } } ZmodF_poly_clear(poly); } } mpz_clear(x); mpz_clear(y); mpz_clear(z); return success; }
int test_F_mpn_splitcombine_bits() { mp_limb_t * int1, * int2; ZmodF_poly_t poly; int result = 1; unsigned long count; for (count = 0; (count < 30000) && (result == 1); count++) { unsigned long limbs = randint(300)+1; unsigned long bits = randint(500)+1; unsigned long coeff_limbs = randint(100) + (bits-1)/FLINT_BITS + 1; unsigned long length = (FLINT_BITS*limbs - 1)/bits + 1; unsigned long log_length = 0; while ((1L << log_length) < length) log_length++; #if DEBUG printf("limbs = %ld, bits = %ld, coeff_limbs = %ld\n", limbs, bits, coeff_limbs); #endif int1 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs); int2 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs); ZmodF_poly_init(poly, log_length, coeff_limbs, 0); mpn_random2(int1, limbs); F_mpn_FFT_split_bits(poly, int1, limbs, bits, coeff_limbs); F_mpn_clear(int2, limbs); F_mpn_FFT_combine_bits(int2, poly, bits, coeff_limbs, limbs); #if DEBUG F_mpn_printx(int1, limbs); printf("\n\n"); unsigned long i; for (i = 0; i < length; i++) { F_mpn_printx(poly->coeffs[i], coeff_limbs); printf("\n");} printf("\n"); F_mpn_printx(int2, limbs); printf("\n\n"); #endif unsigned long j; for (j = 0; j < limbs; j++) { if (int1[j] != int2[j]) result = 0; } ZmodF_poly_clear(poly); free(int2); free(int1); } return result; }
int test__ZmodF_mul_fft_combine() { int success = 1; mpz_t x, y, p, q, r, s, total; mpz_init(x); mpz_init(y); mpz_init(s); mpz_init(r); mpz_init(q); mpz_init(p); mpz_init(total); mp_limb_t buf[300]; for (unsigned long n = 1; n < 80 && success; n++) { for (unsigned long depth = 0; ((n*FLINT_BITS) % (1 << depth) == 0) && success; depth++) { for (unsigned long m = 1; m < n/4 && success; m++) { for (unsigned long k = 0; k < 5 && success; k++) { #if DEBUG printf("n = %ld, depth = %ld, m = %ld, k = %ld\n", n, depth, m, k); #endif ZmodF_poly_t poly; ZmodF_poly_init(poly, depth, m+k, 1); // p := B^n + 1 mpz_set_ui(p, 1); mpz_mul_2exp(p, p, n*FLINT_BITS); mpz_add_ui(p, p, 1); // q := (B^m + 1)*B^k mpz_set_ui(q, 1); mpz_mul_2exp(q, q, m*FLINT_BITS); mpz_add_ui(q, q, 1); mpz_mul_2exp(q, q, k*FLINT_BITS); // r := B^(m+k) - 1 mpz_set_ui(r, 1); mpz_mul_2exp(r, r, (m+k)*FLINT_BITS); mpz_sub_ui(r, r, 1); // s := B^(m+k)/2 mpz_set_ui(s, 1); mpz_mul_2exp(s, s, (m+k)*FLINT_BITS - 1); for (unsigned long trial = 0; trial < 20 && success; trial++) { mpz_set_ui(total, 0); for (long i = (1 << depth) - 1; i >= 0; i--) { // select random x in (0, B^(m+k)) mpz_set_ui(x, 0); while (!mpz_sgn(x)) { mpz_rrandomb(x, randstate, (m+k)*FLINT_BITS); if (random_ulong(2)) // to get high bit 0 sometimes mpz_sub(x, r, x); } // push it down to (-B^(m+k)/2, B^(m+k)/2) mpz_sub(x, x, s); // add it to running total mpz_mul_2exp(total, total, (n*FLINT_BITS) >> depth); mpz_add(total, total, x); // normalise it into [0, q), and store in polynomial mpz_mod(x, x, q); mpz_to_mpn(poly->coeffs[i], m+k+1, x); } // compare result to target function _ZmodF_mul_fft_combine(buf, poly, m, k, n); ZmodF_normalise(buf, n); mpn_to_mpz(y, buf, n+1); mpz_mod(total, total, p); if (mpz_cmp(total, y)) success = 0; } ZmodF_poly_clear(poly); } } } } mpz_clear(x); mpz_clear(y); mpz_clear(s); mpz_clear(r); mpz_clear(q); mpz_clear(p); mpz_clear(total); return success; }