void dump_abort (mpz_t dividend, mpz_t divisor) { fprintf (stderr, "ERROR\n"); fprintf (stderr, "dividend = "); debug_mp (dividend, -16); fprintf (stderr, "divisor = "); debug_mp (divisor, -16); abort(); }
void dump_abort (int testnr, mpz_t op1, mpz_t op2) { fprintf (stderr, "ERROR in test %d\n", testnr); fprintf (stderr, "op1 = "); debug_mp (op1, -16); fprintf (stderr, "op2 = "); debug_mp (op2, -16); abort(); }
void dump_abort (mpz_t x2, mpz_t x, mpz_t rem) { fprintf (stderr, "ERROR\n"); fprintf (stderr, "x2 = "); debug_mp (x2, -16); fprintf (stderr, "x = "); debug_mp (x, -16); fprintf (stderr, "remainder = "); debug_mp (rem, -16); abort(); }
void dump_abort (mpz_t dividend, mpz_t divisor) { fprintf (stderr, "ERROR\n"); printf("nn = %ld, dn = %ld\n", mpz_size(dividend), mpz_size(divisor)); fprintf (stderr, "dividend = "); debug_mp (dividend, -16); fprintf (stderr, "divisor = "); debug_mp (divisor, -16); abort(); }
void dump_abort (int i, const char *s, mpz_t op1, mpz_t op2) { fprintf (stderr, "ERROR: %s in test %d\n", s, i); fprintf (stderr, "op1 = "); debug_mp (op1, -16); fprintf (stderr, "op2 = "); debug_mp (op2, -16); abort(); }
void dump_abort (int i, char *s, mpz_t op1, mpz_t op2, mpz_t product, mpz_t ref_product) { fprintf (stderr, "ERROR: %s in test %d\n", s, i); fprintf (stderr, "op1 = "); debug_mp (op1); fprintf (stderr, "op2 = "); debug_mp (op2); fprintf (stderr, " product = "); debug_mp (product); fprintf (stderr, "ref_product = "); debug_mp (ref_product); abort(); }
/* See that mpz_tstbit matches a twos complement calculated explicitly, for various low zeros. */ void check_tstbit (void) { #define MAX_ZEROS 3 #define NUM_LIMBS 3 mp_limb_t pos[1+NUM_LIMBS+MAX_ZEROS]; mp_limb_t neg[1+NUM_LIMBS+MAX_ZEROS]; mpz_t z; unsigned long i; int zeros, low1; int got, want; mpz_init (z); for (zeros = 0; zeros <= MAX_ZEROS; zeros++) { MPN_ZERO (pos, numberof(pos)); mpn_random2 (pos+zeros, NUM_LIMBS); for (low1 = 0; low1 <= 1; low1++) { if (low1) pos[0] |= 1; refmpn_neg_n (neg, pos, numberof(neg)); mpz_set_n (z, neg, numberof(neg)); mpz_neg (z, z); for (i = 0; i < numberof(pos)*GMP_NUMB_BITS; i++) { got = mpz_tstbit (z, i); want = refmpn_tstbit (pos, i); if (got != want) { printf ("wrong at bit %lu, with %d zeros\n", i, zeros); printf ("z neg "); debug_mp (z, -16); mpz_set_n (z, pos, numberof(pos)); printf ("pos "); debug_mp (z, -16); mpz_set_n (z, neg, numberof(neg)); printf ("neg "); debug_mp (z, -16); exit (1); } } } } mpz_clear (z); }
void dump_abort (char *str, mpz_t dividend, unsigned long divisor) { fprintf (stderr, "ERROR: %s\n", str); fprintf (stderr, "dividend = "); debug_mp (dividend, -16); fprintf (stderr, "divisor = %lX\n", divisor); abort(); }
void dump_abort (int i, char *s, mpz_t op1, mpz_t op2, mpz_t product, mpz_t ref_product) { mp_size_t b, e; fprintf (stderr, "ERROR: %s in test %d\n", s, i); fprintf (stderr, "op1 = "); debug_mp (op1); fprintf (stderr, "op2 = "); debug_mp (op2); fprintf (stderr, " product = "); debug_mp (product); fprintf (stderr, "ref_product = "); debug_mp (ref_product); for (b = 0; b < ABSIZ(ref_product); b++) if (PTR(ref_product)[b] != PTR(product)[b]) break; for (e = ABSIZ(ref_product) - 1; e >= 0; e--) if (PTR(ref_product)[e] != PTR(product)[e]) break; printf ("ERRORS in %ld--%ld\n", b, e); abort(); }
void special_tests(void) { mpz_t n, d, q, q2, r; mpz_init(n); mpz_init(d); mpz_init(q); mpz_init(q2); mpz_init(r); gmp_sscanf("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF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FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "%Zx", n); gmp_sscanf("20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "%Zx", d); mpz_tdiv_q(q, n, d); mpz_tdiv_qr(q2, r, n, d); if (mpz_cmp(q, q2) != 0) { fprintf (stderr, "ERROR\n"); fprintf (stderr, "dividend = "); debug_mp (n, -16); fprintf (stderr, "divisor = "); debug_mp (d, -16); fprintf (stderr, "q1 = "); debug_mp (q, -16); fprintf (stderr, "q2 = "); debug_mp (q2, -16); abort(); } gmp_sscanf("3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF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FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "%Zx", n); gmp_sscanf("400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "%Zx", d); mpz_tdiv_q(q, n, d); mpz_tdiv_qr(q2, r, n, d); if (mpz_cmp(q, q2) != 0) { fprintf (stderr, "ERROR\n"); fprintf (stderr, "dividend = "); debug_mp (n, -16); fprintf (stderr, "divisor = "); debug_mp (d, -16); fprintf (stderr, "q1 = "); debug_mp (q, -16); fprintf (stderr, "q2 = "); debug_mp (q2, -16); abort(); } mpz_clear(n); mpz_clear(d); mpz_clear(q); mpz_clear(q2); mpz_clear(r); }
static int hgcd_appr_valid_p (mpz_t a, mpz_t b, mp_size_t res0, struct hgcd_ref *ref, mpz_t ref_r0, mpz_t ref_r1, mp_size_t res1, struct hgcd_matrix *hgcd) { mp_size_t n = MAX (mpz_size (a), mpz_size (b)); mp_size_t s = n/2 + 1; mp_bitcnt_t dbits, abits, margin; mpz_t appr_r0, appr_r1, t, q; struct hgcd_ref appr; if (!res0) { if (!res1) return 1; fprintf (stderr, "mpn_hgcd_appr returned 1 when no reduction possible.\n"); return 0; } /* NOTE: No *_clear calls on error return, since we're going to abort anyway. */ mpz_init (t); mpz_init (q); hgcd_ref_init (&appr); mpz_init (appr_r0); mpz_init (appr_r1); if (mpz_size (ref_r0) <= s) { fprintf (stderr, "ref_r0 too small!!!: "); debug_mp (ref_r0, 16); return 0; } if (mpz_size (ref_r1) <= s) { fprintf (stderr, "ref_r1 too small!!!: "); debug_mp (ref_r1, 16); return 0; } mpz_sub (t, ref_r0, ref_r1); dbits = mpz_sizeinbase (t, 2); if (dbits > s*GMP_NUMB_BITS) { fprintf (stderr, "ref |r0 - r1| too large!!!: "); debug_mp (t, 16); return 0; } if (!res1) { mpz_set (appr_r0, a); mpz_set (appr_r1, b); } else { unsigned i; for (i = 0; i<2; i++) { unsigned j; for (j = 0; j<2; j++) { mp_size_t mn = hgcd->n; MPN_NORMALIZE (hgcd->p[i][j], mn); mpz_realloc (appr.m[i][j], mn); MPN_COPY (PTR (appr.m[i][j]), hgcd->p[i][j], mn); SIZ (appr.m[i][j]) = mn; } } mpz_mul (appr_r0, appr.m[1][1], a); mpz_mul (t, appr.m[0][1], b); mpz_sub (appr_r0, appr_r0, t); if (mpz_sgn (appr_r0) <= 0 || mpz_size (appr_r0) <= s) { fprintf (stderr, "appr_r0 too small: "); debug_mp (appr_r0, 16); return 0; } mpz_mul (appr_r1, appr.m[1][0], a); mpz_mul (t, appr.m[0][0], b); mpz_sub (appr_r1, t, appr_r1); if (mpz_sgn (appr_r1) <= 0 || mpz_size (appr_r1) <= s) { fprintf (stderr, "appr_r1 too small: "); debug_mp (appr_r1, 16); return 0; } } mpz_sub (t, appr_r0, appr_r1); abits = mpz_sizeinbase (t, 2); if (abits < dbits) { fprintf (stderr, "|r0 - r1| too small: "); debug_mp (t, 16); return 0; } /* We lose one bit each time we discard the least significant limbs. For the lehmer code, that can happen at most s * (GMP_NUMB_BITS) / (GMP_NUMB_BITS - 1) times. For the dc code, we lose an entire limb (or more?) for each level of recursion. */ margin = (n/2+1) * GMP_NUMB_BITS / (GMP_NUMB_BITS - 1); { mp_size_t rn; for (rn = n; ABOVE_THRESHOLD (rn, HGCD_APPR_THRESHOLD); rn = (rn + 1)/2) margin += GMP_NUMB_BITS; } if (verbose_flag && abits > dbits) fprintf (stderr, "n = %u: sbits = %u: ref #(r0-r1): %u, appr #(r0-r1): %u excess: %d, margin: %u\n", (unsigned) n, (unsigned) s*GMP_NUMB_BITS, (unsigned) dbits, (unsigned) abits, (int) abits - s * GMP_NUMB_BITS, (unsigned) margin); if (abits > s*GMP_NUMB_BITS + margin) { fprintf (stderr, "appr |r0 - r1| much larger than minimal (by %u bits, margin = %u bits)\n", (unsigned) (abits - s*GMP_NUMB_BITS), (unsigned) margin); return 0; } while (mpz_cmp (appr_r0, ref_r0) > 0 || mpz_cmp (appr_r1, ref_r1) > 0) { ASSERT (mpz_size (appr_r0) > s); ASSERT (mpz_size (appr_r1) > s); if (mpz_cmp (appr_r0, appr_r1) > 0) { if (!sdiv_qr (q, appr_r0, s, appr_r0, appr_r1)) break; mpz_addmul (appr.m[0][1], q, appr.m[0][0]); mpz_addmul (appr.m[1][1], q, appr.m[1][0]); } else { if (!sdiv_qr (q, appr_r1, s, appr_r1, appr_r0)) break; mpz_addmul (appr.m[0][0], q, appr.m[0][1]); mpz_addmul (appr.m[1][0], q, appr.m[1][1]); } } if (mpz_cmp (appr_r0, ref_r0) != 0 || mpz_cmp (appr_r1, ref_r1) != 0 || !hgcd_ref_equal (ref, &appr)) { fprintf (stderr, "appr_r0: "); debug_mp (appr_r0, 16); fprintf (stderr, "ref_r0: "); debug_mp (ref_r0, 16); fprintf (stderr, "appr_r1: "); debug_mp (appr_r1, 16); fprintf (stderr, "ref_r1: "); debug_mp (ref_r1, 16); return 0; } mpz_clear (t); mpz_clear (q); hgcd_ref_clear (&appr); mpz_clear (appr_r0); mpz_clear (appr_r1); return 1; }
void check_one (mpz_t root1, mpz_t x2, unsigned long nth, int i) { mpz_t temp, temp2; mpz_t root2, rem2; mpz_init (root2); mpz_init (rem2); mpz_init (temp); mpz_init (temp2); MPZ_CHECK_FORMAT (root1); mpz_rootrem (root2, rem2, x2, nth); MPZ_CHECK_FORMAT (root2); MPZ_CHECK_FORMAT (rem2); mpz_pow_ui (temp, root1, nth); MPZ_CHECK_FORMAT (temp); mpz_add (temp2, temp, rem2); /* Is power of result > argument? */ if (mpz_cmp (root1, root2) != 0 || mpz_cmp (x2, temp2) != 0 || mpz_cmpabs (temp, x2) > 0) { fprintf (stderr, "ERROR after test %d\n", i); debug_mp (x2, 10); debug_mp (root1, 10); debug_mp (root2, 10); fprintf (stderr, "nth: %lu\n", nth); abort (); } if (nth > 1 && mpz_cmp_ui (temp, 1L) > 0 && ! mpz_perfect_power_p (temp)) { fprintf (stderr, "ERROR in mpz_perfect_power_p after test %d\n", i); debug_mp (temp, 10); debug_mp (root1, 10); fprintf (stderr, "nth: %lu\n", nth); abort (); } if (nth <= 10000 && mpz_sgn(x2) > 0) /* skip too expensive test */ { mpz_add_ui (temp2, root1, 1L); mpz_pow_ui (temp2, temp2, nth); MPZ_CHECK_FORMAT (temp2); /* Is square of (result + 1) <= argument? */ if (mpz_cmp (temp2, x2) <= 0) { fprintf (stderr, "ERROR after test %d\n", i); debug_mp (x2, 10); debug_mp (root1, 10); fprintf (stderr, "nth: %lu\n", nth); abort (); } } mpz_clear (root2); mpz_clear (rem2); mpz_clear (temp); mpz_clear (temp2); }
int main (int argc, char **argv) { mpz_t op1, op2, temp1, temp2; int i, j, chain_len; gmp_randstate_ptr rands; mpz_t bs; unsigned long size_range; tests_start (); rands = RANDS; mpz_init (bs); mpz_init (op1); mpz_init (op2); mpz_init (temp1); mpz_init (temp2); for (i = 0; hgcd_values[i].res >= 0; i++) { mp_size_t res; mpz_set_str (op1, hgcd_values[i].a, 0); mpz_set_str (op2, hgcd_values[i].b, 0); res = one_test (op1, op2, -1-i); if (res != hgcd_values[i].res) { fprintf (stderr, "ERROR in test %d\n", -1-i); fprintf (stderr, "Bad return code from hgcd\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "expected: %d\n", hgcd_values[i].res); fprintf (stderr, "hgcd: %d\n", (int) res); abort (); } } for (i = 0; i < 15; i++) { /* Generate plain operands with unknown gcd. These types of operands have proven to trigger certain bugs in development versions of the gcd code. The "hgcd->row[3].rsize > M" ASSERT is not triggered by the division chain code below, but that is most likely just a result of that other ASSERTs are triggered before it. */ mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 13 + 2; mpz_urandomb (bs, rands, size_range); mpz_urandomb (op1, rands, mpz_get_ui (bs) + MIN_OPERAND_SIZE); mpz_urandomb (bs, rands, size_range); mpz_urandomb (op2, rands, mpz_get_ui (bs) + MIN_OPERAND_SIZE); if (mpz_cmp (op1, op2) < 0) mpz_swap (op1, op2); if (mpz_size (op1) > 0) one_test (op1, op2, i); /* Generate a division chain backwards, allowing otherwise unlikely huge quotients. */ mpz_set_ui (op1, 0); mpz_urandomb (bs, rands, 32); mpz_urandomb (bs, rands, mpz_get_ui (bs) % 16 + 1); mpz_rrandomb (op2, rands, mpz_get_ui (bs)); mpz_add_ui (op2, op2, 1); #if 0 chain_len = 1000000; #else mpz_urandomb (bs, rands, 32); chain_len = mpz_get_ui (bs) % (GMP_NUMB_BITS * GCD_DC_THRESHOLD / 256); #endif for (j = 0; j < chain_len; j++) { mpz_urandomb (bs, rands, 32); mpz_urandomb (bs, rands, mpz_get_ui (bs) % 12 + 1); mpz_rrandomb (temp2, rands, mpz_get_ui (bs) + 1); mpz_add_ui (temp2, temp2, 1); mpz_mul (temp1, op2, temp2); mpz_add (op1, op1, temp1); /* Don't generate overly huge operands. */ if (SIZ (op1) > 3 * GCD_DC_THRESHOLD) break; mpz_urandomb (bs, rands, 32); mpz_urandomb (bs, rands, mpz_get_ui (bs) % 12 + 1); mpz_rrandomb (temp2, rands, mpz_get_ui (bs) + 1); mpz_add_ui (temp2, temp2, 1); mpz_mul (temp1, op1, temp2); mpz_add (op2, op2, temp1); /* Don't generate overly huge operands. */ if (SIZ (op2) > 3 * GCD_DC_THRESHOLD) break; } if (mpz_cmp (op1, op2) < 0) mpz_swap (op1, op2); if (mpz_size (op1) > 0) one_test (op1, op2, i); } mpz_clear (bs); mpz_clear (op1); mpz_clear (op2); mpz_clear (temp1); mpz_clear (temp2); tests_end (); exit (0); }
static mp_size_t one_test (mpz_t a, mpz_t b, int i) { struct hgcd_matrix hgcd; struct hgcd_ref ref; mpz_t ref_r0; mpz_t ref_r1; mpz_t hgcd_r0; mpz_t hgcd_r1; mp_size_t res[2]; mp_size_t asize; mp_size_t bsize; mp_size_t hgcd_init_scratch; mp_size_t hgcd_scratch; mp_ptr hgcd_init_tp; mp_ptr hgcd_tp; asize = a->_mp_size; bsize = b->_mp_size; ASSERT (asize >= bsize); hgcd_init_scratch = MPN_HGCD_MATRIX_INIT_ITCH (asize); hgcd_init_tp = refmpn_malloc_limbs (hgcd_init_scratch); mpn_hgcd_matrix_init (&hgcd, asize, hgcd_init_tp); hgcd_scratch = mpn_hgcd_itch (asize); hgcd_tp = refmpn_malloc_limbs (hgcd_scratch); #if 0 fprintf (stderr, "one_test: i = %d asize = %d, bsize = %d\n", i, a->_mp_size, b->_mp_size); gmp_fprintf (stderr, "one_test: i = %d\n" " a = %Zx\n" " b = %Zx\n", i, a, b); #endif hgcd_ref_init (&ref); mpz_init_set (ref_r0, a); mpz_init_set (ref_r1, b); res[0] = hgcd_ref (&ref, ref_r0, ref_r1); mpz_init_set (hgcd_r0, a); mpz_init_set (hgcd_r1, b); if (bsize < asize) { _mpz_realloc (hgcd_r1, asize); MPN_ZERO (hgcd_r1->_mp_d + bsize, asize - bsize); } res[1] = mpn_hgcd (hgcd_r0->_mp_d, hgcd_r1->_mp_d, asize, &hgcd, hgcd_tp); if (res[0] != res[1]) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "Different return value from hgcd and hgcd_ref\n"); fprintf (stderr, "op1="); debug_mp (a, -16); fprintf (stderr, "op2="); debug_mp (b, -16); fprintf (stderr, "hgcd_ref: %ld\n", (long) res[0]); fprintf (stderr, "mpn_hgcd: %ld\n", (long) res[1]); abort (); } if (res[0] > 0) { if (!hgcd_ref_equal (&hgcd, &ref) || !mpz_mpn_equal (ref_r0, hgcd_r0->_mp_d, res[1]) || !mpz_mpn_equal (ref_r1, hgcd_r1->_mp_d, res[1])) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpn_hgcd and hgcd_ref returned different values\n"); fprintf (stderr, "op1="); debug_mp (a, -16); fprintf (stderr, "op2="); debug_mp (b, -16); abort (); } } refmpn_free_limbs (hgcd_init_tp); refmpn_free_limbs (hgcd_tp); hgcd_ref_clear (&ref); mpz_clear (ref_r0); mpz_clear (ref_r1); mpz_clear (hgcd_r0); mpz_clear (hgcd_r1); return res[0]; }
void one_test (mpz_t op1, mpz_t op2, mpz_t ref, int i) { /* printf ("%ld %ld %ld\n", SIZ (op1), SIZ (op2), SIZ (ref)); fflush (stdout); */ /* fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); */ mpz_gcdext (gcd1, s, NULL, op1, op2); if (ref && mpz_cmp (ref, gcd1) != 0) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcdext returned incorrect result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "expected result:\n"); debug_mp (ref, -16); fprintf (stderr, "mpz_gcdext returns:\n");debug_mp (gcd1, -16); abort (); } if (!gcdext_valid_p(op1, op2, gcd1, s)) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcdext returned invalid result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "mpz_gcdext returns:\n");debug_mp (gcd1, -16); abort (); } mpz_gcd (gcd2, op1, op2); if (mpz_cmp (gcd2, gcd1) != 0) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcd returned incorrect result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "expected result:\n"); debug_mp (gcd1, -16); fprintf (stderr, "mpz_gcd returns:\n"); debug_mp (gcd2, -16); abort (); } /* This should probably move to t-gcd_ui.c */ if (mpz_fits_ulong_p (op1) || mpz_fits_ulong_p (op2)) { if (mpz_fits_ulong_p (op1)) mpz_gcd_ui (gcd2, op2, mpz_get_ui (op1)); else mpz_gcd_ui (gcd2, op1, mpz_get_ui (op2)); if (mpz_cmp (gcd2, gcd1)) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcd_ui returned incorrect result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "expected result:\n"); debug_mp (gcd1, -16); fprintf (stderr, "mpz_gcd_ui returns:\n"); debug_mp (gcd2, -16); abort (); } } mpz_gcdext (gcd2, temp1, temp2, op1, op2); mpz_mul (temp1, temp1, op1); mpz_mul (temp2, temp2, op2); mpz_add (temp1, temp1, temp2); if (mpz_cmp (gcd1, gcd2) != 0 || mpz_cmp (gcd2, temp1) != 0) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "mpz_gcdext returned incorrect result\n"); fprintf (stderr, "op1="); debug_mp (op1, -16); fprintf (stderr, "op2="); debug_mp (op2, -16); fprintf (stderr, "expected result:\n"); debug_mp (gcd1, -16); fprintf (stderr, "mpz_gcdext returns:\n");debug_mp (gcd2, -16); abort (); } }
static mp_size_t one_test (mpz_t a, mpz_t b, int i) { struct hgcd_matrix hgcd; struct hgcd_ref ref; mpz_t ref_r0; mpz_t ref_r1; mpz_t hgcd_r0; mpz_t hgcd_r1; int res[2]; mp_size_t asize; mp_size_t bsize; mp_size_t hgcd_init_scratch; mp_size_t hgcd_scratch; mp_ptr hgcd_init_tp; mp_ptr hgcd_tp; mp_limb_t marker[4]; asize = a->_mp_size; bsize = b->_mp_size; ASSERT (asize >= bsize); hgcd_init_scratch = MPN_HGCD_MATRIX_INIT_ITCH (asize); hgcd_init_tp = refmpn_malloc_limbs (hgcd_init_scratch + 2) + 1; mpn_hgcd_matrix_init (&hgcd, asize, hgcd_init_tp); hgcd_scratch = mpn_hgcd_appr_itch (asize); hgcd_tp = refmpn_malloc_limbs (hgcd_scratch + 2) + 1; mpn_random (marker, 4); hgcd_init_tp[-1] = marker[0]; hgcd_init_tp[hgcd_init_scratch] = marker[1]; hgcd_tp[-1] = marker[2]; hgcd_tp[hgcd_scratch] = marker[3]; #if 0 fprintf (stderr, "one_test: i = %d asize = %d, bsize = %d\n", i, a->_mp_size, b->_mp_size); gmp_fprintf (stderr, "one_test: i = %d\n" " a = %Zx\n" " b = %Zx\n", i, a, b); #endif hgcd_ref_init (&ref); mpz_init_set (ref_r0, a); mpz_init_set (ref_r1, b); res[0] = hgcd_ref (&ref, ref_r0, ref_r1); mpz_init_set (hgcd_r0, a); mpz_init_set (hgcd_r1, b); if (bsize < asize) { _mpz_realloc (hgcd_r1, asize); MPN_ZERO (hgcd_r1->_mp_d + bsize, asize - bsize); } res[1] = mpn_hgcd_appr (hgcd_r0->_mp_d, hgcd_r1->_mp_d, asize, &hgcd, hgcd_tp); if (hgcd_init_tp[-1] != marker[0] || hgcd_init_tp[hgcd_init_scratch] != marker[1] || hgcd_tp[-1] != marker[2] || hgcd_tp[hgcd_scratch] != marker[3]) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "scratch space overwritten!\n"); if (hgcd_init_tp[-1] != marker[0]) gmp_fprintf (stderr, "before init_tp: %Mx\n" "expected: %Mx\n", hgcd_init_tp[-1], marker[0]); if (hgcd_init_tp[hgcd_init_scratch] != marker[1]) gmp_fprintf (stderr, "after init_tp: %Mx\n" "expected: %Mx\n", hgcd_init_tp[hgcd_init_scratch], marker[1]); if (hgcd_tp[-1] != marker[2]) gmp_fprintf (stderr, "before tp: %Mx\n" "expected: %Mx\n", hgcd_tp[-1], marker[2]); if (hgcd_tp[hgcd_scratch] != marker[3]) gmp_fprintf (stderr, "after tp: %Mx\n" "expected: %Mx\n", hgcd_tp[hgcd_scratch], marker[3]); abort (); } if (!hgcd_appr_valid_p (a, b, res[0], &ref, ref_r0, ref_r1, res[1], &hgcd)) { fprintf (stderr, "ERROR in test %d\n", i); fprintf (stderr, "Invalid results for hgcd and hgcd_ref\n"); fprintf (stderr, "op1="); debug_mp (a, -16); fprintf (stderr, "op2="); debug_mp (b, -16); fprintf (stderr, "hgcd_ref: %ld\n", (long) res[0]); fprintf (stderr, "mpn_hgcd_appr: %ld\n", (long) res[1]); abort (); } refmpn_free_limbs (hgcd_init_tp - 1); refmpn_free_limbs (hgcd_tp - 1); hgcd_ref_clear (&ref); mpz_clear (ref_r0); mpz_clear (ref_r1); mpz_clear (hgcd_r0); mpz_clear (hgcd_r1); return res[0]; }
int main (int argc, char **argv) { mpq_t op1, op2; mp_size_t size; int i; int reps = 10000; FILE *fp; int base; gmp_randstate_ptr rands; mpz_t bs; unsigned long bsi, size_range; size_t nread; tests_start (); rands = RANDS; mpz_init (bs); if (argc == 2) reps = atoi (argv[1]); mpq_init (op1); mpq_init (op2); fp = fopen (FILENAME, "w+"); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 10 + 2; mpz_urandomb (bs, rands, size_range); size = mpz_get_ui (bs); mpz_errandomb (mpq_numref(op1), rands, 512L); mpz_errandomb_nonzero (mpq_denref(op1), rands, 512L); mpq_canonicalize (op1); mpz_urandomb (bs, rands, 1); bsi = mpz_get_ui (bs); if ((bsi & 1) != 0) mpq_neg (op1, op1); mpz_urandomb (bs, rands, 16); bsi = mpz_get_ui (bs); base = bsi % 36 + 1; if (base == 1) base = 0; rewind (fp); if (mpq_out_str (fp, base, op1) == 0 || putc (' ', fp) == EOF || fflush (fp) != 0) { printf ("mpq_out_str write error\n"); abort (); } rewind (fp); nread = mpq_inp_str (op2, fp, base); if (nread == 0) { if (ferror (fp)) printf ("mpq_inp_str stream read error\n"); else printf ("mpq_inp_str data conversion error\n"); abort (); } if (nread != ftell(fp)) { printf ("mpq_inp_str nread doesn't match ftell\n"); printf (" nread %lu\n", (unsigned long) nread); printf (" ftell %ld\n", ftell(fp)); abort (); } if (mpq_cmp (op1, op2)) { printf ("ERROR\n"); printf ("op1 = "); debug_mp (op1, -16); printf ("op2 = "); debug_mp (op2, -16); printf ("base = %d\n", base); abort (); } } fclose (fp); unlink (FILENAME); mpz_clear (bs); mpq_clear (op1); mpq_clear (op2); tests_end (); exit (0); }
int main (int argc, char **argv) { mpz_t base, exp, mod; mpz_t r1, r2, base2; mp_size_t base_size, exp_size, mod_size; unsigned long int exp2; int i; int reps = 100; gmp_randstate_ptr rands; mpz_t bs; unsigned long bsi, size_range; tests_start (); rands = RANDS; mpz_init (bs); if (argc == 2) reps = atoi (argv[1]); mpz_init (base); mpz_init (exp); mpz_init (mod); mpz_init (r1); mpz_init (r2); mpz_init (base2); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 18 + 2; do /* Loop until mathematically well-defined. */ { mpz_urandomb (bs, rands, size_range); base_size = mpz_get_ui (bs); mpz_rrandomb (base, rands, base_size); mpz_urandomb (bs, rands, 6L); exp_size = mpz_get_ui (bs); mpz_rrandomb (exp, rands, exp_size); exp2 = mpz_getlimbn (exp, (mp_size_t) 0); } while (mpz_cmp_ui (base, 0) == 0 && exp2 == 0); do { mpz_urandomb (bs, rands, size_range); mod_size = mpz_get_ui (bs); mpz_rrandomb (mod, rands, mod_size); } while (mpz_cmp_ui (mod, 0) == 0); mpz_urandomb (bs, rands, 2); bsi = mpz_get_ui (bs); if ((bsi & 1) != 0) mpz_neg (base, base); /* printf ("%ld %ld\n", SIZ (base), SIZ (mod)); */ #if 0 putc ('\n', stderr); debug_mp (base, -16); debug_mp (mod, -16); #endif mpz_powm_ui (r1, base, exp2, mod); MPZ_CHECK_FORMAT (r1); mpz_set_ui (r2, 1); mpz_set (base2, base); mpz_mod (r2, r2, mod); /* needed when exp==0 and mod==1 */ while (exp2 != 0) { if (exp2 % 2 != 0) { mpz_mul (r2, r2, base2); mpz_mod (r2, r2, mod); } mpz_mul (base2, base2, base2); mpz_mod (base2, base2, mod); exp2 = exp2 / 2; } #if 0 debug_mp (r1, -16); debug_mp (r2, -16); #endif if (mpz_cmp (r1, r2) != 0) { fprintf (stderr, "\ntest %d: Incorrect results for operands:\n", i); debug_mp (base, -16); debug_mp (exp, -16); debug_mp (mod, -16); fprintf (stderr, "mpz_powm_ui result:\n"); debug_mp (r1, -16); fprintf (stderr, "reference result:\n"); debug_mp (r2, -16); abort (); } } mpz_clear (bs); mpz_clear (base); mpz_clear (exp); mpz_clear (mod); mpz_clear (r1); mpz_clear (r2); mpz_clear (base2); tests_end (); exit (0); }
int main (int argc, char **argv) { mpz_t op1, op2; mp_size_t size; int i; int reps = 10000; char *str, *buf; int base; gmp_randstate_ptr rands; mpz_t bs; unsigned long bsi, size_range; tests_start (); rands = RANDS; mpz_init (bs); if (argc == 2) reps = atoi (argv[1]); mpz_init (op1); mpz_init (op2); for (i = 0; i < reps; i++) { /* 1. Generate random mpz_t and convert to a string and back to mpz_t again. */ mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 12 + 2; /* 2..13 */ mpz_urandomb (bs, rands, size_range); /* 3..8191 bits */ size = mpz_get_ui (bs); mpz_rrandomb (op1, rands, size); mpz_urandomb (bs, rands, 1); bsi = mpz_get_ui (bs); if ((bsi & 1) != 0) mpz_neg (op1, op1); mpz_urandomb (bs, rands, 32); bsi = mpz_get_ui (bs); base = bsi % 62 + 1; if (base == 1) base = 0; str = mpz_get_str ((char *) 0, base, op1); mpz_set_str_or_abort (op2, str, base); if (mpz_cmp (op1, op2)) { fprintf (stderr, "ERROR, op1 and op2 different in test %d\n", i); fprintf (stderr, "str = %s\n", str); fprintf (stderr, "base = %d\n", base); fprintf (stderr, "op1 = "); debug_mp (op1, -16); fprintf (stderr, "op2 = "); debug_mp (op2, -16); abort (); } (*__gmp_free_func) (str, strlen (str) + 1); #if 0 /* 2. Generate random string and convert to mpz_t and back to a string again. */ mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 10 + 2; /* 2..11 */ mpz_urandomb (bs, rands, size_range); /* 3..2047 bits */ len = mpz_get_ui (bs); buf = (*__gmp_allocate_func) (len + 1); string_urandomb (buf, len, base); mpz_set_str_or_abort (op1, buf, base); str = mpz_get_str ((char *) 0, base, op1); if (strcmp (str, buf) != 0) { fprintf (stderr, "ERROR, str and buf different\n"); fprintf (stderr, "str = %s\n", str); fprintf (stderr, "buf = %s\n", buf); fprintf (stderr, "base = %d\n", base); fprintf (stderr, "op1 = "); debug_mp (op1, -16); abort (); } (*__gmp_free_func) (buf, len + 1); (*__gmp_free_func) (str, strlen (str) + 1); #endif } mpz_clear (bs); mpz_clear (op1); mpz_clear (op2); tests_end (); exit (0); }
int main (int argc, char **argv) { mpz_t base, exp, mod; mpz_t r1, r2, t1, exp2, base2; mp_size_t base_size, exp_size, mod_size; int i; int reps = 100; gmp_randstate_ptr rands; mpz_t bs; unsigned long bsi, size_range; tests_start (); rands = RANDS; mpz_init (bs); if (argc == 2) reps = atoi (argv[1]); mpz_init (base); mpz_init (exp); mpz_init (mod); mpz_init (r1); mpz_init (r2); mpz_init (t1); mpz_init (exp2); mpz_init (base2); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 13 + 2; do /* Loop until mathematically well-defined. */ { mpz_urandomb (bs, rands, size_range); base_size = mpz_get_ui (bs); mpz_rrandomb (base, rands, base_size); mpz_urandomb (bs, rands, 7L); exp_size = mpz_get_ui (bs); mpz_rrandomb (exp, rands, exp_size); } while (mpz_cmp_ui (base, 0) == 0 && mpz_cmp_ui (exp, 0) == 0); do { mpz_urandomb (bs, rands, size_range); mod_size = mpz_get_ui (bs); mpz_rrandomb (mod, rands, mod_size); } while (mpz_cmp_ui (mod, 0) == 0); mpz_urandomb (bs, rands, 2); bsi = mpz_get_ui (bs); if ((bsi & 1) != 0) mpz_neg (base, base); /* printf ("%ld %ld %ld\n", SIZ (base), SIZ (exp), SIZ (mod)); */ mpz_powm (r1, base, exp, mod); mpz_set_ui (r2, 1); mpz_set (base2, base); mpz_set (exp2, exp); mpz_mod (r2, r2, mod); /* needed when exp==0 and mod==1 */ while (mpz_cmp_ui (exp2, 0) != 0) { mpz_mod_ui (t1, exp2, 2); if (mpz_cmp_ui (t1, 0) != 0) { mpz_mul (r2, r2, base2); mpz_mod (r2, r2, mod); } mpz_mul (base2, base2, base2); mpz_mod (base2, base2, mod); mpz_div_ui (exp2, exp2, 2); } if (mpz_cmp (r1, r2) != 0) { fprintf (stderr, "\nIncorrect results for operands:\n"); debug_mp (base, -16); debug_mp (exp, -16); debug_mp (mod, -16); fprintf (stderr, "mpz_powm result:\n"); debug_mp (r1, -16); fprintf (stderr, "reference result:\n"); debug_mp (r2, -16); abort (); } } mpz_clear (bs); mpz_clear (base); mpz_clear (exp); mpz_clear (mod); mpz_clear (r1); mpz_clear (r2); mpz_clear (t1); mpz_clear (exp2); mpz_clear (base2); tests_end (); exit (0); }