different results between javascript and python when using bitwise operators

廉价感情. 提交于 2021-01-29 16:59:49

问题


I am trying to translate some js code into python and having trouble converting bitwise operators. I already introduced ctypes.c_int in python but the results still do not match. For the >>> in js I used as suggested here.

A minimal example of my (not working) code:

Javascript:

let R = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
let K = [3614090360, 3905402710, 606105819, 3250441966, 4118548399, 1200080426, 2821735955, 4249261313, 1770035416, 2336552879, 4294925233, 2304563134, 1804603682, 4254626195, 2792965006, 1236535329, 4129170786, 3225465664, 643717713, 3921069994, 3593408605, 38016083, 3634488961, 3889429448, 568446438, 3275163606, 4107603335, 1163531501, 2850285829, 4243563512, 1735328473, 2368359562, 4294588738, 2272392833, 1839030562, 4259657740, 2763975236, 1272893353, 4139469664, 3200236656, 681279174, 3936430074, 3572445317, 76029189, 3654602809, 3873151461, 530742520, 3299628645, 4096336452, 1126891415, 2878612391, 4237533241, 1700485571, 2399980690, 4293915773, 2240044497, 1873313359, 4264355552, 2734768916, 1309151649, 4149444226, 3174756917, 718787259, 3951481745]
let blocks = [1819043144, 1919899503, 8414316, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0];

let h0 = 1732584193, h1 = 4023233417, h2 = 2562383102, h3 = 271733878, length = blocks.length;
for (let i = 0; length > i; i += 16) {
    var f, g, tmp, x, y, a = h0, b = h1, c = h2, d = h3;
    for (let j = 0; 64 > j; ++j) {
        if (16 > j) {
            f = d ^ b & (c ^ d);
            g = j;
            tmp = d;
            d = c;
            c = b;
            x = a + f + K[j] + blocks[i + g];
            y = R[j];
            b += x << y | x >>> 32 - y;
            a = tmp;
        }
    }
}

Python:

import ctypes

R = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
K = [3614090360, 3905402710, 606105819, 3250441966, 4118548399, 1200080426, 2821735955, 4249261313, 1770035416, 2336552879, 4294925233, 2304563134, 1804603682, 4254626195, 2792965006, 1236535329, 4129170786, 3225465664, 643717713, 3921069994, 3593408605, 38016083, 3634488961, 3889429448, 568446438, 3275163606, 4107603335, 1163531501, 2850285829, 4243563512, 1735328473, 2368359562, 4294588738, 2272392833, 1839030562, 4259657740, 2763975236, 1272893353, 4139469664, 3200236656, 681279174, 3936430074, 3572445317, 76029189, 3654602809, 3873151461, 530742520, 3299628645, 4096336452, 1126891415, 2878612391, 4237533241, 1700485571, 2399980690, 4293915773, 2240044497, 1873313359, 4264355552, 2734768916, 1309151649, 4149444226, 3174756917, 718787259, 3951481745]
blocks = [1819043144, 1919899503, 8414316, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0]

h0 = 1732584193
h1 = 4023233417
h2 = 2562383102
h3 = 271733878
length = len(blocks)

i = 0
while length > i:
    a = h0
    b = h1
    c = h2
    d = h3

    j = 0
    while 64 > j:

        if 16 > j:
            f = ctypes.c_int(c ^ d & (b ^ c)).value
            g = j
            tmp = d
            d = c
            c = b
            x = a + f + K[j] + blocks[i + g]
            y = R[j]
            b += ctypes.c_int(x << y | ((x & 0xffffffff) >> 32 - y)).value # was >>>
            a = tmp

        j += 1

    i += 16


print('a', a)
print('b', b)

js returns 7727443648 and python returns 4225785507 for a


回答1:


Your values are outside JS limits :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

The numbers -2147483648 and 2147483647 are the minimum and the maximum integers representable through a 32bit signed number.



来源:https://stackoverflow.com/questions/53567349/different-results-between-javascript-and-python-when-using-bitwise-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!