BeginPackage["CSM`Modular`"]

mod::usage = "mod[n, p] is a representation of the number n mod p."


Begin["`Private`"]


(* Normal Forms *)

mod[n_, p_] := mod[ Mod[n, p], p ] /; n < 0 || n >= p

mod[0, _] := 0

(* Arithmetic *)

mod/: mod[a_,p_] + mod[b_,p_] := mod[ a + b, p ]
mod/: mod[a_,p_] * mod[b_,p_] := mod[ a * b, p ]
mod/: -mod[a_, p_] := mod[-a, p]
mod/: mod[a_,p_] ^ n_Integer := mod[ PowerMod[a, n, p], p ]

End[]

EndPackage[]
