2

In this piece of pseudocode from the MD5 Wikipedia website, fully available here, there is a pseudofunction leftrotate().

for each 512-bit chunk of message
    break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15
//Initialize hash value for this chunk:
    var int A := a0
    var int B := b0
    var int C := c0
    var int D := d0
//Main loop:
    for i from 0 to 63
        if 0 ≤ i ≤ 15 then
            F := (B and C) or ((not B) and D)
            g := i
        else if 16 ≤ i ≤ 31
            F := (D and B) or ((not D) and C)
            g := (5×i + 1) mod 16
        else if 32 ≤ i ≤ 47
            F := B xor C xor D
            g := (3×i + 5) mod 16
        else if 48 ≤ i ≤ 63
            F := C xor (B or (not D))
            g := (7×i) mod 16
        dTemp := D
        D := C
        C := B
        B := B + leftrotate((A + F + K[i] + M[g]), s[i])
        A := dTemp
    end for
//Add this chunk's hash to result so far:
    a0 := a0 + A
    b0 := b0 + B
    c0 := c0 + C
    d0 := d0 + D
end for

var char digest[16] := a0 append b0 append c0 append d0 //(Output is in little-endian)

//leftrotate function definition
leftrotate (x, c)
    return (x << c) binary or (x >> (32-c));

However, is the leftrotate() function a logical rotate or a circular rotate? As when I looked up the function on the bitwise operations wikipedia I saw different leftrotations. Which one does the MD5 hashfunction use?

The rotation is defined on the first Wikipedia as:

leftrotate (x, c)
    return (x << c) binary or (x >> (32-c));

On RFC 1321 the function is formulated differently, like so:

a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s)

Where s is the shift, but still I don't know what kind of leftrotate it is.

Community
  • 1
  • 1
Thomas Wagenaar
  • 6,489
  • 5
  • 30
  • 73
  • For my own curiosity, what other kind of rotate of there? Your question, the answer, and wikipedia, all document the only kind of rotate of a 32-bit integer that I've ever heard of. What else is there? – Ian Boyd Oct 04 '15 at 12:23
  • There is only one type of left rotate, namely a rotating rotate (wut?). There is no such thing as "logical rotate". – harold Oct 04 '15 at 12:31
  • There are different ones, ofcourse they all rotate (duhhh), however there are different types (Logical shift, Circular shift, Through carry) and I was wondering which one. – Thomas Wagenaar Oct 04 '15 at 12:38
  • Rotate through carry exists only in assembly, logical shift is not even a rotate – harold Oct 04 '15 at 13:23

1 Answers1

2

Found the answer while googling some other questions here

A bit rotation is also known as a circular shift rotation, extra information can be found here: - Circular shift - Bitwise operations

A circular shift shifts the numbers to left by an x amount, appending the extra ones back at the end.

enter image description here

Community
  • 1
  • 1
Thomas Wagenaar
  • 6,489
  • 5
  • 30
  • 73