Reading — step 1 of 5
Read
~1 min readBits, Bytes & Endianness
Bit Operations Refresher
SHA-256 is a flurry of bit operations on 32-bit unsigned integers.
| Op | Symbol | What it does |
|---|---|---|
| AND | a & b | bits set in both |
| OR | `a | b` |
| XOR | a ^ b | bits differing |
| NOT | ~a | flip all bits |
| Left shift | a << n | shift bits left, fill with 0s |
| Right shift | a >> n | shift bits right, fill with 0s |
| Rotate right | ROTR(a,n) | bits that fall off the right come around |
In SHA-256, ROTR is critical:
ROTR(x, n) = (x >> n) | (x << (32 - n)) (mod 2^32)
Two derived functions used in the spec:
Σ0(x) = ROTR(x,2) ^ ROTR(x,13) ^ ROTR(x,22)
Σ1(x) = ROTR(x,6) ^ ROTR(x,11) ^ ROTR(x,25)
σ0(x) = ROTR(x,7) ^ ROTR(x,18) ^ (x >> 3)
σ1(x) = ROTR(x,17) ^ ROTR(x,19) ^ (x >> 10)
Plus two "majority/choice" functions:
Ch(x,y,z) = (x & y) ^ (~x & z) # if x then y else z, bitwise
Maj(x,y,z) = (x & y) ^ (x & z) ^ (y & z) # majority of three bits
All arithmetic is mod 2^32 — wrap on overflow.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…