I am now developing a code which makes use of a huge number of modulo arithmetic.
For a better readability, I tried to define custom types Modulus
and ModNum
.
Modulus
contains some values which is essential for the modulo arithmetic (for example, the value of the modulus, Q, or some pre-computed values for montgomery reduction, etc.) and ModNum
stores the value and modulus. Namely,
mutable struct Modulus
Q::UInt64
Qinv::UInt64
...
end
struct ModNum
val::UInt64
Q::Modulus
end
. And then, I could define overloading operations such as Base.+
, Base.-
or Base.*
for two ModNum
s with the same field value of Q. However, one downside with this approach is that it copies Q whenever new ModNum is generated, and it slows down the code and also consumes more memory. (Using UInt64 is twice as fast as this approach, but the readability and usability is a bit poor.) Will there be any good way to keep the structure while avoiding memory consumption from copying Q?