From be48dbf90bcf43831a0716fb300a59fb9f3e84d8 Mon Sep 17 00:00:00 2001 From: Tommy Hofmann Date: Mon, 6 Jan 2025 20:04:35 +0100 Subject: [PATCH 1/2] fix: make invmod more robust for non-prime moduli (#1986) (before it was giving silently wrong results) --- src/HeckeMoreStuff.jl | 3 ++- test/flint/fmpz_mod_poly-test.jl | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/HeckeMoreStuff.jl b/src/HeckeMoreStuff.jl index 994514333b..7abe92c401 100644 --- a/src/HeckeMoreStuff.jl +++ b/src/HeckeMoreStuff.jl @@ -400,7 +400,8 @@ end function invmod(f::ZZModPolyRingElem, M::ZZModPolyRingElem) if !is_unit(f) r = parent(f)() - i = @ccall libflint.fmpz_mod_poly_invmod(r::Ref{ZZModPolyRingElem}, f::Ref{ZZModPolyRingElem}, M::Ref{ZZModPolyRingElem}, f.parent.base_ring.ninv::Ref{fmpz_mod_ctx_struct})::Int + ff = ZZ() + i = @ccall libflint.fmpz_mod_poly_invmod_f(ff::Ref{ZZRingElem}, r::Ref{ZZModPolyRingElem}, f::Ref{ZZModPolyRingElem}, M::Ref{ZZModPolyRingElem}, f.parent.base_ring.ninv::Ref{fmpz_mod_ctx_struct})::Int if iszero(i) error("not yet implemented") else diff --git a/test/flint/fmpz_mod_poly-test.jl b/test/flint/fmpz_mod_poly-test.jl index b4db6f5776..c6f8b729c2 100644 --- a/test/flint/fmpz_mod_poly-test.jl +++ b/test/flint/fmpz_mod_poly-test.jl @@ -343,6 +343,12 @@ end @test powermod(f, -10, g) == 78305338116088931412*x+91239060941924718463 @test powermod(f, -ZZRingElem(10), g) == 78305338116088931412*x+91239060941924718463 + + R, = residue_ring(ZZ, ZZ(2809)) + Rx, x = R[:x] + f = 32*x^9 + 28*x^8 + 2497*x^7 + 2443*x^6 + 708*x^5 + 996*x^4 + 2557*x^3 + 2104*x^2 + 2517*x + 2752 + g = x^10 + 2798*x^8 + 2806*x^7 + 37*x^6 + 14*x^5 + 2761*x^4 + 2787*x^3 + 20*x^2 + 12*x + 1 + @test_throws ErrorException invmod(f, g) end @testset "ZZModPolyRingElem.euclidean_division" begin From fc77b13721c353d8d8b6682e749f65381e1458f2 Mon Sep 17 00:00:00 2001 From: Tommy Hofmann Date: Tue, 7 Jan 2025 18:31:51 +0100 Subject: [PATCH 2/2] fix: aliasing bug in mul! (#1987) --- src/flint/gfp_fmpz_elem.jl | 11 +++++++---- test/flint/gfp_fmpz-test.jl | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/flint/gfp_fmpz_elem.jl b/src/flint/gfp_fmpz_elem.jl index d4fa418d72..7a89b1117f 100644 --- a/src/flint/gfp_fmpz_elem.jl +++ b/src/flint/gfp_fmpz_elem.jl @@ -364,10 +364,13 @@ end function mul!(z::FpFieldElem, x::FpFieldElem, y::ZZRingElem) R = parent(x) - @ccall libflint.fmpz_mod(z.data::Ref{ZZRingElem}, y::Ref{ZZRingElem}, R.n::Ref{ZZRingElem})::Nothing - - @ccall libflint.fmpz_mod_mul(z.data::Ref{ZZRingElem}, x.data::Ref{ZZRingElem}, z.data::Ref{ZZRingElem}, R.ninv::Ref{fmpz_mod_ctx_struct})::Nothing - return z + if z !== x + @ccall libflint.fmpz_mod(z.data::Ref{ZZRingElem}, y::Ref{ZZRingElem}, R.n::Ref{ZZRingElem})::Nothing + @ccall libflint.fmpz_mod_mul(z.data::Ref{ZZRingElem}, x.data::Ref{ZZRingElem}, z.data::Ref{ZZRingElem}, R.ninv::Ref{fmpz_mod_ctx_struct})::Nothing + return z + else + return mul!(z, x, R(y)) + end end function add!(z::FpFieldElem, x::FpFieldElem, y::FpFieldElem) diff --git a/test/flint/gfp_fmpz-test.jl b/test/flint/gfp_fmpz-test.jl index 1216be268e..29c1b418f8 100644 --- a/test/flint/gfp_fmpz-test.jl +++ b/test/flint/gfp_fmpz-test.jl @@ -532,3 +532,10 @@ end R = Native.GF(ZZ(19)) @test R([5]) == R(5) end + +@testset "gfp_fmpz.bug" begin + R = Native.GF(ZZRingElem(123456789012345678949)) + z = R(1) + mul!(z, z, ZZ(10)) + @test z == 10 +end