From e4113a5e5a260bae6ad8a69d8eb245c02386bb18 Mon Sep 17 00:00:00 2001 From: William Hart Date: Wed, 29 Jul 2020 02:41:00 +0200 Subject: [PATCH 1/3] Add sqrt and issquare to fq and fq_nmod. --- docs/src/finitefield.md | 10 ++++++++++ src/flint/FlintTypes.jl | 2 ++ src/flint/fq.jl | 23 ++++++++++++++++++++++- src/flint/fq_nmod.jl | 12 ++++++++++++ test/flint/fq-test.jl | 4 ++++ test/flint/fq_nmod-test.jl | 4 ++++ 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/docs/src/finitefield.md b/docs/src/finitefield.md index a4ebfa33d..6be9d91f6 100644 --- a/docs/src/finitefield.md +++ b/docs/src/finitefield.md @@ -131,6 +131,14 @@ norm(::fq) frobenius(::fq, ::Int) ``` +```@docs +sqrt(::fq) +``` + +```@docs +issquare(::fq) +``` + ```@docs pth_root(::fq) ``` @@ -147,4 +155,6 @@ c = norm(a) d = frobenius(a) f = frobenius(a, 3) g = pth_root(a) +h = sqrt(a^2) +j = issquare(a) ``` diff --git a/src/flint/FlintTypes.jl b/src/flint/FlintTypes.jl index 266c7417b..f13201046 100644 --- a/src/flint/FlintTypes.jl +++ b/src/flint/FlintTypes.jl @@ -1600,6 +1600,7 @@ mutable struct FqNmodFiniteField <: FinField ninv :: Int norm :: Int sparse_modulus :: Int + is_conway::Int a :: Ptr{Nothing} j :: Ptr{Nothing} len :: Int @@ -1753,6 +1754,7 @@ end mutable struct FqFiniteField <: FinField p::Int # fmpz sparse_modulus::Int + is_conway::Int a::Ptr{Nothing} j::Ptr{Nothing} len::Int diff --git a/src/flint/fq.jl b/src/flint/fq.jl index 1833a6f36..6e4dbb83a 100644 --- a/src/flint/fq.jl +++ b/src/flint/fq.jl @@ -405,9 +405,30 @@ end # ############################################################################### +@doc Markdown.doc""" + sqrt(x::fq) +> Return the square root of $x$ in the finite field. If $x$ is not a square +> an exception is raised. +""" +function sqrt(x::fq) + z = parent(x)() + ccall((:fq_sqrt, libflint), Nothing, + (Ref{fq}, Ref{fq}, Ref{FqFiniteField}), z, x, x.parent) + return z +end + +@doc Markdown.doc""" + issquare(x::fq) +> Returns `true` if $x$ is a square in the finite field (includes zero). +""" +function issquare(x::fq) + ccall((:fq_is_square, libflint), Bool, + (Ref{fq}, Ref{FqFiniteField}), x, x.parent) +end + @doc Markdown.doc""" pth_root(x::fq) -> Return the $p$-th root of $a$ in the finite field of characteristic $p$. This +> Return the $p$-th root of $x$ in the finite field of characteristic $p$. This > is the inverse operation to the Frobenius map $\sigma_p$. """ function pth_root(x::fq) diff --git a/src/flint/fq_nmod.jl b/src/flint/fq_nmod.jl index 95d9ae128..e958b11ae 100644 --- a/src/flint/fq_nmod.jl +++ b/src/flint/fq_nmod.jl @@ -348,6 +348,18 @@ divexact(x::fmpz, y::fq_nmod) = divexact(parent(y)(x), y) # ############################################################################### +function sqrt(x::fq_nmod) + z = parent(x)() + ccall((:fq_nmod_sqrt, libflint), Nothing, + (Ref{fq_nmod}, Ref{fq_nmod}, Ref{FqNmodFiniteField}), z, x, x.parent) + return z +end + +function issquare(x::fq_nmod) + ccall((:fq_nmod_is_square, libflint), Bool, + (Ref{fq_nmod}, Ref{FqNmodFiniteField}), x, x.parent) +end + function pth_root(x::fq_nmod) z = parent(x)() ccall((:fq_nmod_pth_root, libflint), Nothing, diff --git a/test/flint/fq-test.jl b/test/flint/fq-test.jl index f060d8eb1..be5b7ce46 100644 --- a/test/flint/fq-test.jl +++ b/test/flint/fq-test.jl @@ -182,6 +182,10 @@ end @test frobenius(a, 3) == 3*x^4+3*x^3+3*x^2+x+4 @test pth_root(a) == 4*x^4+3*x^3+4*x^2+5*x+2 + + @test sqrt(a^2) == a + + @test issquare(a^2) end @testset "fq.rand..." begin diff --git a/test/flint/fq_nmod-test.jl b/test/flint/fq_nmod-test.jl index 0bde52983..dd1b8b729 100644 --- a/test/flint/fq_nmod-test.jl +++ b/test/flint/fq_nmod-test.jl @@ -182,4 +182,8 @@ end @test frobenius(a, 3) == 3*x^4+3*x^3+3*x^2+x+4 @test pth_root(a) == 4*x^4+3*x^3+4*x^2+5*x+2 + + @test sqrt(a^2) == a + + @test issquare(a^2) end From 34471bd02b81a55babf162ed2a4108ba75a5da38 Mon Sep 17 00:00:00 2001 From: William Hart Date: Thu, 30 Jul 2020 18:55:30 +0200 Subject: [PATCH 2/3] Int -> Cint for two fields. --- src/flint/FlintTypes.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/flint/FlintTypes.jl b/src/flint/FlintTypes.jl index f13201046..74528c911 100644 --- a/src/flint/FlintTypes.jl +++ b/src/flint/FlintTypes.jl @@ -1599,8 +1599,8 @@ mutable struct FqNmodFiniteField <: FinField n :: Int ninv :: Int norm :: Int - sparse_modulus :: Int - is_conway::Int + sparse_modulus :: Cint + is_conway::Cint a :: Ptr{Nothing} j :: Ptr{Nothing} len :: Int @@ -1753,8 +1753,8 @@ end mutable struct FqFiniteField <: FinField p::Int # fmpz - sparse_modulus::Int - is_conway::Int + sparse_modulus::Cint + is_conway::Cint a::Ptr{Nothing} j::Ptr{Nothing} len::Int From c71d64a83f5bb5a674771595bff05e3912429ad5 Mon Sep 17 00:00:00 2001 From: William Hart Date: Thu, 30 Jul 2020 20:54:14 +0200 Subject: [PATCH 3/3] Throw exception if not a square. --- src/flint/fq.jl | 3 ++- src/flint/fq_nmod.jl | 3 ++- test/flint/fq-test.jl | 2 ++ test/flint/fq_nmod-test.jl | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/flint/fq.jl b/src/flint/fq.jl index 6e4dbb83a..d5c725157 100644 --- a/src/flint/fq.jl +++ b/src/flint/fq.jl @@ -412,8 +412,9 @@ end """ function sqrt(x::fq) z = parent(x)() - ccall((:fq_sqrt, libflint), Nothing, + res = ccall((:fq_sqrt, libflint), Bool, (Ref{fq}, Ref{fq}, Ref{FqFiniteField}), z, x, x.parent) + res || error("Not a square in sqrt") return z end diff --git a/src/flint/fq_nmod.jl b/src/flint/fq_nmod.jl index e958b11ae..6b74a3459 100644 --- a/src/flint/fq_nmod.jl +++ b/src/flint/fq_nmod.jl @@ -350,8 +350,9 @@ divexact(x::fmpz, y::fq_nmod) = divexact(parent(y)(x), y) function sqrt(x::fq_nmod) z = parent(x)() - ccall((:fq_nmod_sqrt, libflint), Nothing, + res = ccall((:fq_nmod_sqrt, libflint), Bool, (Ref{fq_nmod}, Ref{fq_nmod}, Ref{FqNmodFiniteField}), z, x, x.parent) + res || error("Not a square in sqrt") return z end diff --git a/test/flint/fq-test.jl b/test/flint/fq-test.jl index be5b7ce46..3e8d7c3c8 100644 --- a/test/flint/fq-test.jl +++ b/test/flint/fq-test.jl @@ -185,6 +185,8 @@ end @test sqrt(a^2) == a + @test_throws ErrorException sqrt(x*a^2) + @test issquare(a^2) end diff --git a/test/flint/fq_nmod-test.jl b/test/flint/fq_nmod-test.jl index dd1b8b729..bf82d902f 100644 --- a/test/flint/fq_nmod-test.jl +++ b/test/flint/fq_nmod-test.jl @@ -185,5 +185,7 @@ end @test sqrt(a^2) == a + @test_throws ErrorException sqrt(x*a^2) + @test issquare(a^2) end