From 3f544907a9f2a721161bc122dfa88edd94442750 Mon Sep 17 00:00:00 2001 From: Giacomo Pope Date: Mon, 19 Aug 2024 15:56:52 +0100 Subject: [PATCH 1/4] introduce fq_default_ctx_init_randtest --- doc/source/fq_default.rst | 9 ++++++--- src/fq_default.h | 1 + src/fq_default/ctx.c | 19 +++++++++++++++++++ src/fq_default/test/t-init.c | 16 ++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/doc/source/fq_default.rst b/doc/source/fq_default.rst index f74f136e9b..467132b10c 100644 --- a/doc/source/fq_default.rst +++ b/doc/source/fq_default.rst @@ -104,10 +104,13 @@ Context Management Prints the context information to ``stdout``. -.. function:: void fq_default_ctx_randtest(fq_default_ctx_t ctx) +.. function:: void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state, int type) - Initializes ``ctx`` to a random finite field. Assumes that - ``fq_default_ctx_init`` has not been called on ``ctx`` already. + Initializes ``ctx`` to a random finite field where the prime and degree is + set according to ``type``. + Assumes that ``fq_default_ctx_init`` has not been called on ``ctx`` already. + To see what prime and degrees may be output, see + ``type`` in :func:`_nmod_poly_conway_rand`. .. function:: void fq_default_get_coeff_fmpz(fmpz_t c, fq_default_t op, slong n, const fq_default_ctx_t ctx) diff --git a/src/fq_default.h b/src/fq_default.h index 6406358f99..9d2e82d67e 100644 --- a/src/fq_default.h +++ b/src/fq_default.h @@ -229,6 +229,7 @@ int fq_default_ctx_fprint(FILE * file, const fq_default_ctx_t ctx); #endif void fq_default_ctx_print(const fq_default_ctx_t ctx); +void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state, int type); /* Memory management *********************************************************/ diff --git a/src/fq_default/ctx.c b/src/fq_default/ctx.c index 778bb3df15..0422367589 100644 --- a/src/fq_default/ctx.c +++ b/src/fq_default/ctx.c @@ -199,3 +199,22 @@ void fq_default_ctx_modulus(fmpz_mod_poly_t p, const fq_default_ctx_t ctx) fmpz_mod_poly_set(p, FQ_DEFAULT_CTX_FQ(ctx)->modulus, mod); } } + +/* FIXME: fq_ctx_init_randtest only tests nmod sized primes, so that's + all we'll do here too? */ +void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state, int type) +{ + ulong prime; + slong degree; + fmpz_t p; + + /* select a prime and degree to create the finite field */ + prime = _nmod_poly_conway_rand(°ree, state, type); + + /* fq_default initialisation wants an fmpz_t for the prime */ + fmpz_init(p); + fmpz_set_ui(p, prime); + + /* Initialise the context using the prime and degree selected above */ + fq_default_ctx_init(ctx, p, degree, "x"); +} diff --git a/src/fq_default/test/t-init.c b/src/fq_default/test/t-init.c index ae29d4d802..af8ec96aa1 100644 --- a/src/fq_default/test/t-init.c +++ b/src/fq_default/test/t-init.c @@ -81,5 +81,21 @@ TEST_FUNCTION_START(fq_default_init, state) fmpz_clear(p); } + for (i = 0; i < 100 * flint_test_multiplier(); i++) + { + fq_default_ctx_t ctx; + fq_default_t fq; + + /* Sets the type used internally for prime selection. + See _nmod_poly_conway_rand for more info. */ + int type = n_randint(state, 4); + + fq_default_ctx_init_randtest(ctx, state, type); + fq_default_init(fq, ctx); + fq_default_clear(fq, ctx); + fq_default_randtest(fq, state, ctx); + fq_default_ctx_clear(ctx); + } + TEST_FUNCTION_END(state); } From ed59d18c92d7fb2d7fdb62f46bb03e9687647474 Mon Sep 17 00:00:00 2001 From: Giacomo Pope Date: Fri, 23 Aug 2024 15:48:08 +0100 Subject: [PATCH 2/4] swap order of function calls --- src/fq_default/test/t-init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fq_default/test/t-init.c b/src/fq_default/test/t-init.c index af8ec96aa1..43035492f8 100644 --- a/src/fq_default/test/t-init.c +++ b/src/fq_default/test/t-init.c @@ -92,8 +92,8 @@ TEST_FUNCTION_START(fq_default_init, state) fq_default_ctx_init_randtest(ctx, state, type); fq_default_init(fq, ctx); - fq_default_clear(fq, ctx); fq_default_randtest(fq, state, ctx); + fq_default_clear(fq, ctx); fq_default_ctx_clear(ctx); } From 1195800be4c0d07244ee3723e49390142d22fe72 Mon Sep 17 00:00:00 2001 From: Giacomo Pope Date: Mon, 2 Sep 2024 10:26:53 +0100 Subject: [PATCH 3/4] random selection from one of the five internal representations --- doc/source/fq_default.rst | 7 ++--- src/fq_default.h | 2 +- src/fq_default/ctx.c | 53 +++++++++++++++++++++++++----------- src/fq_default/test/t-init.c | 6 +--- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/doc/source/fq_default.rst b/doc/source/fq_default.rst index 467132b10c..190da0c868 100644 --- a/doc/source/fq_default.rst +++ b/doc/source/fq_default.rst @@ -104,13 +104,10 @@ Context Management Prints the context information to ``stdout``. -.. function:: void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state, int type) +.. function:: void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state) - Initializes ``ctx`` to a random finite field where the prime and degree is - set according to ``type``. + Initializes ``ctx`` to a random finite field using one of the five internal representations. Assumes that ``fq_default_ctx_init`` has not been called on ``ctx`` already. - To see what prime and degrees may be output, see - ``type`` in :func:`_nmod_poly_conway_rand`. .. function:: void fq_default_get_coeff_fmpz(fmpz_t c, fq_default_t op, slong n, const fq_default_ctx_t ctx) diff --git a/src/fq_default.h b/src/fq_default.h index 9d2e82d67e..3acad8f666 100644 --- a/src/fq_default.h +++ b/src/fq_default.h @@ -229,7 +229,7 @@ int fq_default_ctx_fprint(FILE * file, const fq_default_ctx_t ctx); #endif void fq_default_ctx_print(const fq_default_ctx_t ctx); -void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state, int type); +void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state); /* Memory management *********************************************************/ diff --git a/src/fq_default/ctx.c b/src/fq_default/ctx.c index 0422367589..9a053b86b6 100644 --- a/src/fq_default/ctx.c +++ b/src/fq_default/ctx.c @@ -200,21 +200,42 @@ void fq_default_ctx_modulus(fmpz_mod_poly_t p, const fq_default_ctx_t ctx) } } -/* FIXME: fq_ctx_init_randtest only tests nmod sized primes, so that's - all we'll do here too? */ -void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state, int type) +/* Create a random fq_default_ctx picked from the five internal representations */ +void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state) { - ulong prime; - slong degree; - fmpz_t p; - - /* select a prime and degree to create the finite field */ - prime = _nmod_poly_conway_rand(°ree, state, type); - - /* fq_default initialisation wants an fmpz_t for the prime */ - fmpz_init(p); - fmpz_set_ui(p, prime); - - /* Initialise the context using the prime and degree selected above */ - fq_default_ctx_init(ctx, p, degree, "x"); + fmpz_t prime; + slong deg; + + /* Select a context type [1,...,5] */ + int ctx_type = 1 + n_randint(state, 5); + switch (ctx_type) + { + /* Create GF(p^d) for FQ_ZECH context */ + case FQ_DEFAULT_FQ_ZECH: + fmpz_randprime(prime, state, 2 + n_randint(state, 4), 1); + deg = 1 + n_randint(state, 4); + break; + /* Create GF(p^d) for FQ_NMOD context */ + case FQ_DEFAULT_FQ_NMOD: + fmpz_randprime(prime, state, 2 + n_randint(state, 29), 1); + deg = 1 + n_randint(state, 15); + break; + /* Create GF(p^d) for FQ context */ + case FQ_DEFAULT_FQ: + fmpz_randprime(prime, state, 2 + n_randint(state, 62), 1); + deg = 1 + n_randint(state, 7); + break; + /* Create GF(p) for NMOD context */ + case FQ_DEFAULT_NMOD: + fmpz_randprime(prime, state, 2 + n_randint(state, 29), 1); + deg = 1; + break; + /* Create GF(p) for FMPZ_MOD context */ + case FQ_DEFAULT_FMPZ_MOD: + fmpz_randprime(prime, state, 2 + n_randint(state, 62), 1); + deg = 1; + break; + default: FLINT_UNREACHABLE; + } + fq_default_ctx_init_type(ctx, prime, deg, "a", ctx_type); } diff --git a/src/fq_default/test/t-init.c b/src/fq_default/test/t-init.c index 43035492f8..a470dbf810 100644 --- a/src/fq_default/test/t-init.c +++ b/src/fq_default/test/t-init.c @@ -86,11 +86,7 @@ TEST_FUNCTION_START(fq_default_init, state) fq_default_ctx_t ctx; fq_default_t fq; - /* Sets the type used internally for prime selection. - See _nmod_poly_conway_rand for more info. */ - int type = n_randint(state, 4); - - fq_default_ctx_init_randtest(ctx, state, type); + fq_default_ctx_init_randtest(ctx, state); fq_default_init(fq, ctx); fq_default_randtest(fq, state, ctx); fq_default_clear(fq, ctx); From 009a93f763e0364645d84358b106e5c43aff3f79 Mon Sep 17 00:00:00 2001 From: Giacomo Pope Date: Mon, 2 Sep 2024 12:07:23 +0100 Subject: [PATCH 4/4] shrink p^d for zech testing --- src/fq_default/ctx.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/fq_default/ctx.c b/src/fq_default/ctx.c index 9a053b86b6..f6dc693018 100644 --- a/src/fq_default/ctx.c +++ b/src/fq_default/ctx.c @@ -205,15 +205,15 @@ void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state) { fmpz_t prime; slong deg; - + /* Select a context type [1,...,5] */ int ctx_type = 1 + n_randint(state, 5); switch (ctx_type) { /* Create GF(p^d) for FQ_ZECH context */ case FQ_DEFAULT_FQ_ZECH: - fmpz_randprime(prime, state, 2 + n_randint(state, 4), 1); - deg = 1 + n_randint(state, 4); + fmpz_randprime(prime, state, 2 + n_randint(state, 3), 1); + deg = 1 + n_randint(state, 3); break; /* Create GF(p^d) for FQ_NMOD context */ case FQ_DEFAULT_FQ_NMOD: