0

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

3
  • Forgive me if I'm wrong, but isn't >>> in Javascript the same operator as >> in python? In javascript >> copies the most significant bit, but >>> simply inserts 0; >> in python uses the latter behavior. Thus you should be able to just replace it, right? Commented Dec 1, 2018 at 3:13
  • Using x = 0b11111111111111111111111111111111: in my JS console, x >> 1 = -1 and x >>> 1 = 2147483647. Meanwhile, in python, x >> 1 = 2147483647. Commented Dec 1, 2018 at 3:18
  • in Javascript 7195273490 >>> 10 returns 2832330 but in python 7195273490 >> 10 returns 7026634 Commented Dec 2, 2018 at 11:59

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ctypes.c_int32 and filling the bits with 0 for the >>> shift fixed my errors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.