{"action":"created","issue":{"url":"https://api.github.com/repos/peterolson/BigInteger.js/issues/46","labels_url":"https://api.github.com/repos/peterolson/BigInteger.js/issues/46/labels{/name}","comments_url":"https://api.github.com/repos/peterolson/BigInteger.js/issues/46/comments","events_url":"https://api.github.com/repos/peterolson/BigInteger.js/issues/46/events","html_url":"https://github.com/peterolson/BigInteger.js/issues/46","id":104437509,"number":46,"title":"modPow isn't working well ?","user":{"login":"zarmack","id":13377900,"avatar_url":"https://avatars.githubusercontent.com/u/13377900?v=3","gravatar_id":"","url":"https://api.github.com/users/zarmack","html_url":"https://github.com/zarmack","followers_url":"https://api.github.com/users/zarmack/followers","following_url":"https://api.github.com/users/zarmack/following{/other_user}","gists_url":"https://api.github.com/users/zarmack/gists{/gist_id}","starred_url":"https://api.github.com/users/zarmack/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zarmack/subscriptions","organizations_url":"https://api.github.com/users/zarmack/orgs","repos_url":"https://api.github.com/users/zarmack/repos","events_url":"https://api.github.com/users/zarmack/events{/privacy}","received_events_url":"https://api.github.com/users/zarmack/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-09-02T09:26:05Z","updated_at":"2015-09-07T08:21:05Z","closed_at":null,"body":"Hi,\r\n\r\nI'm porting a SRP library to support biginteger, but I've detected an error in the modPow method.\r\nThere is bit of code that reproduces the error.\r\n\r\n\r\n```javascript\r\n'use strict';\r\n\r\nvar bigInt = require('big-integer');\r\nvar bignum = require('bignum');\r\n\r\n\r\nfunction h(s) {\r\n  return bigInt(s.split(/\\s/).join(''), 16);\r\n}\r\n\r\nvar test = [\r\n  {\r\n    g: h('02'),\r\n    x: h('b5200337cc 3f3f926cdd dae0b2d310 29c069936a 844aff5877' +\r\n         '9a545be89d 0abe'),\r\n    N: h('ac6bdb4132 4a9a9bf166 de5e138958 2faf72b665 1987ee07fc' +\r\n         '3192943db5 6050a37329 cbb4a099ed 8193e07577 67a13dd523' +\r\n         '12ab4b0331 0dcd7f48a9 da04fd50e8 083969edb7 67b0cf6095' +\r\n         '179a163ab3 661a05fbd5 faaae82918 a9962f0b93 b855f97993' +\r\n         'ec975eeaa8 0d740adbf4 ff747359d0 41d5c33ea7 1d281e446b' +\r\n         '14773bca97 b43a23fb80 1676bd207a 436c6481f1 d2b9078717' +\r\n         '461a5b9d32 e688f87748 544523b524 b0d57d5ea7 7a2775d2ec' +\r\n         'fa032cfbdb f52fb37861 60279004e5 7ae6af874e 7303ce5329' +\r\n         '9ccc041c7b c308d82a56 98f3a8d0c3 8271ae35f8 e9dbfbb694' +\r\n         'b5c803d89f 7ae435de23 6d525f5475 9b65e372fc d68ef20fa7' +\r\n         '111f9e4aff 73'),\r\n    k: h('5b9e8ef059 c6b32ea59f c1d322d37f 04aa30bae5 aa9003b832' +\r\n         '1e21ddb04e 300'),\r\n    a: h('f200000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '0000000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '0000000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '0000000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '0000000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '0000000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '0000000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '0000000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '0000000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '0000000000 0000000000 0000000000 0000000000 0000000000' +\r\n         '000000d3d7'),\r\n    u: h('b284aa1064 e8775150da 6b5e2147b4 7ca7df505b ed94a6f4bb' +\r\n         '2ad873332a d732'),\r\n    B: h('22ce5a7b9d 81277172ca a20b0f1efb 4643b3becc 5356647395' +\r\n         '9b07b790d3 c3f08650d5 531c19ad30 ebb67bdb48 1d1d9cf61b' +\r\n         'f272f84398 48fdda58a4 e6abc5abb2 ac496da509 8d5cbf90e2' +\r\n         '9b4b110e4e 2c033c70af 73925fa374 57ee13ea3e 8fde4ab516' +\r\n         'dff1c2ae8e 57a6b264fb 9db637eeea e9b5e43dfa ba9b329d3b' +\r\n         '8770ce8988 8709e02627 0e474eef82 2436e63975 62f2847786' +\r\n         '73a1a7bc12 b6883d1c21 fbc27ffb3d beb85efda2 79a69a1941' +\r\n         '4969113f10 451603065f 0a01266664 5651dde44a 52f4d8de11' +\r\n         '3e2131321d f1bf4369d2 585364f9e5 36c39a4dce 33221be57d' +\r\n         '50ddccb438 4e3612bbfd 03a268a36e 4f7e01de65 1401e108cc' +\r\n         '247db50392'),\r\n    e: h('92aaf0f527 906aa5e860 1f5d707907 a03137e1b6 01e04b5a1d' +\r\n         'eb02a981f4 be037b3982 9a27dba50f 1b27545ff2 e28729c2b7' +\r\n         '9dcbdd32c9 d6b20d340a ffab91a626 a8075806c2 6fe39df91d' +\r\n         '0ad979f9b2 ee8aad1bc7 83e7097407 b63bfe58d9 118b9b0b2a' +\r\n         '7c5c4cdeba f8e9a460f4 bf6247b0da 34b760a59f ac891757dd' +\r\n         'edcaf08eed 823b090586 c63009b2d7 40cc9f5397 be89a2c32c' +\r\n         'dcfe6d6251 ce11e44e6e cbdd9b6d93 f30e90896d 2527564c7e' +\r\n         'b9ff70aa91 acc0bac174 0a11cd184f fb989554ab 58117c2196' +\r\n         'b353d70c35 6160100ef5 f4c28d19f6 e59ea2508e 8e8aac6001' +\r\n         '497c27f362 edbafb25e0 f045bfdf9f b02db9c908 f10340a639' +\r\n         'fe84c31b27')\r\n  }\r\n]\r\n\r\ntest.forEach(function(o, i, a) {\r\n\r\n  // Starting the test with bigInt()\r\n  var bi_step1 = o.g.modPow(o.x, o.N);\r\n  var bi_step2 = o.B.subtract(o.k.multiply(o.g.modPow(o.x, o.N)));\r\n  var bi_step3 = o.a.add(o.u.multiply(o.x));\r\n  var bi_step4 = bi_step2.modPow(bi_step3, o.N);\r\n\r\n\r\n  // Now with bignum !\r\n  // Converting the numbers to bignum() from bigInt()\r\n  var bn_g = bignum(o.g.toString(16), 16);\r\n  var bn_x = bignum(o.x.toString(16), 16);\r\n  var bn_k = bignum(o.k.toString(16), 16);\r\n  var bn_a = bignum(o.a.toString(16), 16)\r\n  var bn_u = bignum(o.u.toString(16), 16)\r\n  var bn_B = bignum(o.B.toString(16), 16);\r\n  var bn_N = bignum(o.N.toString(16), 16);\r\n\r\n  var bn_step1 = bn_g.powm(bn_x, bn_N);\r\n  var bn_step2 = bn_B.sub(bn_k.mul(bn_g.powm(bn_x, bn_N)));\r\n  var bn_step3 = bn_a.add(bn_u.mul(bn_x))\r\n  var bn_step4 = bn_step2.powm(bn_step3, bn_N)\r\n\r\n  // Comparing the results\r\n  if(bn_step4.toString(16) != bi_step4.toString(16)) {\r\n    console.log();\r\n    console.log(\"Step 4 result doesn't match !!!\");\r\n    console.log();\r\n    console.log(\" bigInt step4\");\r\n    console.log(\"--------------\");\r\n    console.log(bi_step4.toString(16));\r\n    console.log();\r\n    console.log(\" bignum step4\");\r\n    console.log(\"--------------\");\r\n    console.log(bn_step4.toString(16));\r\n    console.log();\r\n    console.log(\" expected step4\");\r\n    console.log(\"----------------\");\r\n    console.log(o.e.toString(16));\r\n    console.log();\r\n  }\r\n});\r\n```\r\n\r\nBest regards,\r\nZarmack\r\n"},"comment":{"url":"https://api.github.com/repos/peterolson/BigInteger.js/issues/comments/138229900","html_url":"https://github.com/peterolson/BigInteger.js/issues/46#issuecomment-138229900","issue_url":"https://api.github.com/repos/peterolson/BigInteger.js/issues/46","id":138229900,"user":{"login":"zarmack","id":13377900,"avatar_url":"https://avatars.githubusercontent.com/u/13377900?v=3","gravatar_id":"","url":"https://api.github.com/users/zarmack","html_url":"https://github.com/zarmack","followers_url":"https://api.github.com/users/zarmack/followers","following_url":"https://api.github.com/users/zarmack/following{/other_user}","gists_url":"https://api.github.com/users/zarmack/gists{/gist_id}","starred_url":"https://api.github.com/users/zarmack/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zarmack/subscriptions","organizations_url":"https://api.github.com/users/zarmack/orgs","repos_url":"https://api.github.com/users/zarmack/repos","events_url":"https://api.github.com/users/zarmack/events{/privacy}","received_events_url":"https://api.github.com/users/zarmack/received_events","type":"User","site_admin":false},"created_at":"2015-09-07T08:21:05Z","updated_at":"2015-09-07T08:21:05Z","body":"Hi Peter,\r\n\r\nI don't know if it's a bug, but I'm sure other libraries gives a diferent result for the ```BigInteger's modPow()``` For now, I've tested [Tom Wu's jsbn](http://www-cs-students.stanford.edu/~tjw/jsbn/), [node-bignum](https://github.com/justmoon/node-bignum), and some others and all give the same output that differs from BigInteger result.\r\n\r\nWhen I make the [node-srp](https://github.com/mozilla/node-srp) test with you library, on the PiCl section, only one of the tests is passed. But with other libraries all of them are pased. [BN from OpenSSL](https://www.openssl.org/docs/manmaster/crypto/bn.html) also outputs diferent than BigInteger.\r\n\r\nI'm no mathematician, I do not know which is the correct result, what I know is that the result of BigInteger is different from all other libraries I've tested.\r\n\r\nRegards,\r\nZarmack"}}