peterolson / BigInteger.js

An arbitrary length integer library for Javascript
The Unlicense
1.12k stars 187 forks source link

modPow give bad result #111

Closed USideOfficial closed 7 years ago

USideOfficial commented 7 years ago

Hello i don't known if it's a bug but i'm getting weird result

Variable A : 0x-a86c02360e7c2eb14db3b5dc0ce0d3bd7589b75e24168b8631c6c5c5cf44bd7bf4a0881b3ceb6c27e1c40ab3629b1f8f2f08372f8586405905d9ee2b827e1101630fe8189d9a2119c2ee01fe09ba293a11657e43f8c3577496dadaa770a13bedf2ba3f6229187996e9dac6a2f4d62ac187ddf86761ea6e2da85899428728e129d05e2669c42213ccd54efb3013b1de04fea841c1924285fe32bf1edad3bee58

Variable B : 0x1c387eb7edaa35f307e1362ddf858a06341557cb1523c45141e7acd8fbf01e70f0e17f63c244be7b90feb445248fd86dc2f76e155186573f4fb8a9d19d58e870afc5045e36fa2b4603ffc0d0e8aa18de7cddef8dd8ddbad763f2c955418dca6734480f583e3d876b0fbdb8fa8b543b05ceb39300c1356b94ae6e37c6004fbc1d

Variable C : 0xc037c37588b4329887e61c2da3324b1ba4b81a63f9748fed2d8a410c2fc21b1232f0d3bfa024276cfd88448197aae486a63bfca7b8bf7754dfb327c7201f6fd17fd7fd74158bd31ce772c9f5f8ab584548a99a759b5a2c0532162b7b6218e8f142bce2c30d7784689a483e095e701618437913a8c39c3dd0d4ca3c500b885fe3

So with js i do A.ModPow(B, C) and give me a bad result : -53d5e6215e3aac1f83a311ab1bc6bb8abeafd73d73ffc91b306b10a565f42d545d109a9001e9907d21938d600e8921022ad24dab1da1431f0aa16721750ca5248c8866c62a65bce753a136ca2ffc5610253c57de082799c6afab4c500ed29a4c456091283f0a0a795a997126f6e7750dde5fa8b1c0cf986903c385b39384a5b0

But the python version give me the correct result : 0x6c61dd542a79867904430a82876b8f90e60843268574c6d1fd1f3066c9cdedbdd5e0392f9e3a96efdbf4b7218921c3847b69aefc9b1e3435d511c0a5ab12caacf34f96adeb26163593d1932bc8af0235236d42979332923e826adf2b53464ea4fd5c519ace6d79ef3faecce26788a10a65196af702cca567d106b69c7803ba33

peterolson commented 7 years ago

@Yaffle Do you have any idea about this one? I checked with the jsbn library which gives the same result as the python result above.

Yaffle commented 7 years ago

The result is correct, but negative. A.modPow(B, C).add(C) gives the positive one.

You could add something like:

        if (base.isNegative()) {
          base = base.add(mod);
        }
USideOfficial commented 7 years ago

Ok ty