Skip to content

Issue with 64 bit integers in CRT for Windows #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions src/reconstruction/crt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,44 @@
###
# CRT

# Auxiliary functions allowing to avoid overflow
# on Windows for numbers between 32 and 64 bits
function my_set_ui!(a::BigInt, b::UInt64)
@static if Culong == UInt64

Check warning on line 9 in src/reconstruction/crt.jl

View check run for this annotation

Codecov / codecov/patch

src/reconstruction/crt.jl#L9

Added line #L9 was not covered by tests
Base.GMP.MPZ.set_ui!(a, b)
else
if b < 2^32
Base.GMP.MPZ.set_ui!(a, b)
else
Base.GMP.MPZ.set!(a, BigInt(b))
end
end
end

function my_mul_ui!(a::BigInt, b::BigInt, c::UInt64)
@static if Culong == UInt64

Check warning on line 21 in src/reconstruction/crt.jl

View check run for this annotation

Codecov / codecov/patch

src/reconstruction/crt.jl#L21

Added line #L21 was not covered by tests
Base.GMP.MPZ.mul_ui!(a, b, c)
else
if c < 2^32
Base.GMP.MPZ.mul_ui!(a, b, c)
else
Base.GMP.MPZ.mul!(a, b, BigInt(c))
end
end
end

function my_mul_ui!(a::BigInt, b::UInt64)
@static if Culong == UInt64

Check warning on line 33 in src/reconstruction/crt.jl

View check run for this annotation

Codecov / codecov/patch

src/reconstruction/crt.jl#L33

Added line #L33 was not covered by tests
Base.GMP.MPZ.mul_ui!(a, b)
else
if b < 2^32
Base.GMP.MPZ.mul_ui!(a, b)
else
Base.GMP.MPZ.mul!(a, BigInt(b))
end
end
end

"""
crt!

Expand All @@ -23,9 +61,9 @@
function crt!(M::BigInt, buf::BigInt, n1::BigInt, n2::BigInt, ai::Vector{UInt}, ci::Vector{BigInt})
@invariant length(ai) == length(ci)

Base.GMP.MPZ.set_ui!(n1, UInt(0))
my_set_ui!(n1, UInt(0))
for i in 1:length(ai)
Base.GMP.MPZ.mul_ui!(n2, ci[i], ai[i])
my_mul_ui!(n2, ci[i], ai[i])
Base.GMP.MPZ.add!(n1, n2)
end

Expand All @@ -52,13 +90,13 @@
@invariant length(ci) == length(moduli)

n3, n4 = BigInt(), BigInt()
@inbounds Base.GMP.MPZ.set_ui!(M, moduli[1])
@inbounds my_set_ui!(M, moduli[1])
@inbounds for i in 2:length(moduli)
Base.GMP.MPZ.mul_ui!(M, moduli[i])
my_mul_ui!(M, moduli[i])
end

@inbounds for i in 1:length(moduli)
Base.GMP.MPZ.set_ui!(n2, moduli[i])
my_set_ui!(n2, moduli[i])
Base.GMP.MPZ.tdiv_q!(ci[i], M, n2)
Base.GMP.MPZ.gcdext!(n2, n3, n4, ci[i], n2)
Base.GMP.MPZ.mul!(ci[i], n3)
Expand Down
43 changes: 43 additions & 0 deletions test/crt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@testset "CRT" begin
cases = []

push!(
cases,
Dict(
:moduli => Vector{UInt64}([1099511627791, 3518437208889]),
:ai => Vector{UInt64}([8590035092, 8589936485])
),
Dict(
:moduli => Vector{UInt64}([1099511627791, 17, 3518437208889]),
:ai => Vector{UInt64}([8590035092, 3, 8589936485])
),
Dict(
:moduli => Vector{UInt64}([1099511627791, 2^30 + 3, 3518437208889]),
:ai => Vector{UInt64}([8590035092, 42, 0])
),
Dict(
:moduli => Vector{UInt64}([1099511627791, 2^30 + 3, 3518437208889]),
:ai => Vector{UInt64}([0, 0, 0])
)
)

for c in cases
ai = c[:ai]
moduli = c[:moduli]
M = prod(Vector{BigInt}(moduli))
ci = Vector{BigInt}([0 for _ in moduli])
n1, n2 = BigInt(0), BigInt(0)
Groebner.crt_precompute!(M, n1, n2, ci, moduli)
for i in 1:length(ci)
pii = div(M, moduli[i])
@test ci[i] % pii == 0
@test (ci[i] - 1) % moduli[i] == 0
end
buf = BigInt(0)
Groebner.crt!(M, buf, n1, n2, ai, ci)
@test 0 <= buf < M
for i in 1:length(ci)
@test (buf - ai[i]) % moduli[i] == 0
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ end

@time @testset "All tests" verbose = true begin
@time include("arithmetic.jl")
@time include("crt.jl")

# Different implementations of a monomial
@time include("monoms/exponentvector.jl")
Expand Down
Loading