Skip to content

Commit

Permalink
Fix promotion with large integers, rework is_prime
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsch committed Dec 28, 2022
1 parent 6a4e610 commit 35d33ff
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/base/primefield.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Return `true` if `n` is a prime number.
"""
Base.@pure function is_prime(n::Int)
function is_prime(n::Int)
if iseven(n) || n < 2
return n == 2
else
Expand Down Expand Up @@ -81,10 +81,13 @@ Base.zero(::Type{Mod{M}}) where {M} = Mod{M}(0, false)
Base.one(::Type{Mod{M}}) where {M} = Mod{M}(1, false)
Base.sign(i::M) where {M<:Mod} = ifelse(iszero(i), zero(M), one(M))

Base.promote_rule(::Type{Mod{M}}, ::Type{<:Integer}) where {M} = Mod{M}
Base.promote_rule(::Type{Mod{M}}, ::Type{<:Mod}) where {M} = Union{}
function Base.promote_rule(::Type{Mod{M}}, ::Type{N}) where {M,N<:Integer}
return Base.promote_type(N, Int) === Int ? Mod{M} : Union{}
function Base.promote_rule(::Type{Mod{M}}, ::Type{I}) where {M,I<:Integer}
if Base.promote_type(I, Int128) === Int128 || Base.promote_type(I, Int128) === UInt128
return Mod{M}
else
return Union{}
end
end

Base.inv(i::Mod{M}) where {M} = Mod{M}(invmod(Int(i), M), false)

0 comments on commit 35d33ff

Please sign in to comment.