public class IntElementaryModMath extends Object
int
data.Modular addition and subtraction are trivial, when the modulus is less than 231 and overflow can be detected easily.
Modular multiplication is more complicated, and since it is usually the single most time consuming operation in the whole program execution, the very core of the Number Theoretic Transform (NTT), it should be carefully optimized.
The obvious (but not very efficient) algorithm for multiplying two
int
s and taking the remainder is
(int) ((long) a * b % modulus)
The first observation is that since the modulus is practically constant, it should be more efficient to calculate (once) the inverse of the modulus, and then subsequently multiply by the inverse modulus instead of dividing by the modulus.
The second observation is that to get the remainder of the division, we don't necessarily need the actual result of the division (we just want the remainder). So, we should discard the topmost 32 bits of the full 64-bit result whenever possible, to save a few operations.
The basic approach is to get some approximation of a * b / modulus
.
The approximation should be within +1 or -1 of the correct result. Then
calculate a * b - approximateDivision * modulus
to get
the remainder. This calculation needs to use only the lowest 32 bits. As
the modulus is less than 231 it is easy to detect the case
when the approximate division was off by one (and the remainder is
±modulus
off).
There are different algorithms to calculate the approximate division
a * b / modulus
. This implementation simply converts all
the operands to double
and performs the mulciplications.
This requires that converting between integer types and floating point
types is efficient. On some platforms this may not be true; in that
case it can be more efficient to perform the multiplications using
64-bit integer arithmetic.
To simplify the operations, we calculate the inverse modulus as
1.0 / (modulus + 0.5)
. Since the modulus is assumed to be
prime, and a double
has more bits for precision than an
int
, the approximate result of a * b / modulus
will always be either correct or one too small (but never one too big).
Constructor and Description |
---|
IntElementaryModMath()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
getModulus()
Get the modulus.
|
int |
modAdd(int a,
int b)
Modular addition.
|
int |
modMultiply(int a,
int b)
Modular multiplication.
|
int |
modSubtract(int a,
int b)
Modular subtraction.
|
void |
setModulus(int modulus)
Set the modulus.
|
public final int modMultiply(int a, int b)
a
- First operand.b
- Second operand.a * b % modulus
public final int modAdd(int a, int b)
a
- First operand.b
- Second operand.(a + b) % modulus
public final int modSubtract(int a, int b)
a
- First operand.b
- Second operand.(a - b + modulus) % modulus
public final int getModulus()
public final void setModulus(int modulus)
modulus
- The modulus.Copyright © 2017. All rights reserved.