Hi all,<br><br>As the doc says:<br><br>mod_exp(N, P, M) -> Result<br><br>Types:<br>N, P, M, Result = Mpint<br>Mpint = binary()<br><br>This function performs the exponentiation N ^ P mod M, using the crypto library.<br><br>
Now, assume: N = -2, P = 3, M = 3<br>Then: N ^ P mod M = (-2) ^ 3 mod 3<br>                  = (-8) mod 3<br>                  = (-3) * 3 + 1<br>               or = (-3) * 2 + (-2)<br>So: the remainder should be 1 or -2<br>
(Remainder, From Wikipedia, <a href="http://en.wikipedia.org/wiki/Remainder">http://en.wikipedia.org/wiki/Remainder</a>)<br><br>But I got a TWO from crypto:mod_exp/3... Is there some wrong...?<br>And I did more tests with erlang, python and ruby.<br>
The result:<br><br>Erlang R14B02 (erts-5.8.3) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]<br><br>Eshell V5.8.3  (abort with ^G)<br>1> crypto:mod_exp(-2, 3, 3).<br>2<br>2> crypto:mod_exp(2, 3, 3).<br>
2<br>3> crypto:mod_exp(-2, 3, -3).<br>1<br>4> crypto:mod_exp(2, 3, -3).<br>8<br><br>Python 2.7.1 (r271:86832, Mar 25 2011, 15:07:46)<br><br>In [1]: pow(-2, 3, 3)<br>Out[1]: 1<br><br>In [2]: pow(2, 3, 3)<br>Out[2]: 2<br>
<br>In [3]: pow(-2, 3, -3)<br>Out[3]: -2<br><br>In [4]: pow(2, 3, -3)<br>Out[4]: -1<br><br>Welcome to IRB. You are using ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]. Have fun ;)<br>irb(main):001:0> (-2) ** 3 % 3<br>
1<br>irb(main):002:0> 2 ** 3 % 3<br>2<br>irb(main):003:0> (-2) ** 3 % (-3)<br>-2<br>irb(main):004:0> 2 ** 3 % (-3)<br>-1<br><br><br>Regards,<br>Hanfei<br>