Study on Improving the Performance of BigInteger.ModPow #110297
Replies: 1 comment
-
As has been covered in many other posts, the primary issue with There are additional things that can be improved as well, but the primary thing is a much needed rewrite of the general type to use Such a rewrite is incredibly expensive and involved, its something that needs top down approval and dedicated time to be worked on by the team. -- CC. @jeffhandley |
Beta Was this translation helpful? Give feedback.
-
In my opinion, the performance of
BigInteger.ModPow
is too bad. Therefore, I have studied the current implementation and tried to find ways to improve it. I would like to share my findings in this discussion. I found an article that mentions some modular exponential algorithms. The current implementation is also included in the article as the right-to-left (LSB-to-MSB) method of modular exponentiation by squaring. Here is the current implementation in BigIntegerCalculator.ModPow.csI have tried to rewrite the method to implement other algorithms mentioned in the article and carry out testing on them.
Here is the binary MSB-to-LSB method:
Here is the k-ary MSB-to-LSB method,
k
can be set to different values (k
should be a factor of 32 so that all the shifting operations can be done within auint
):After some experiments, I found that the binary MSB-to-LSB method runs faster with a smaller
value
, a greaterexponent
or a greatermodulus
than the current implementation. However, I cannot find the exact threshold. Regarding the k-ary MSB-to-LSB method, its performance is similar to that of the binary MSB-to-LSB method. Maybe I have made an incorrect implementation. Although the MSB-to-LSB algorithm is faster in some cases, the wholeModPow
operation is still too slow compared with other languages. e.g. ~10 times slower than Java. I think further improvements can be made by replacing theMultiplySelf
/SquareSelf
+Reduce
parts with other algorithms such as Montgomery modular multiplication.Beta Was this translation helpful? Give feedback.
All reactions