Short answer: scrypt stretches a password by filling a table and walking it at random. PBKDF2-HMAC-SHA-256 expands the password and salt into p blocks. Each block is mixed by ROMix. ROMix stores N successive BlockMix outputs, then walks N more steps, using the last 64 bytes as an index into the table. BlockMix runs Salsa20/8 on 64-byte chunks and writes the even outputs before the odd outputs. A final PBKDF2 call produces the derived key.
What the problem actually asks
PBKDF2 makes a guess take time and almost no memory. scrypt makes a guess store the ROMix table. N is the number of table entries and has to be a power of two. r sets the block size to 128r bytes. p is the number of independent tables. The fragment uses N = 16, which is a test size, not a password-store cost. Salsa20/8 is the mixer inside BlockMix. Its quarter-round rotates by 7, 9, 13, and 18. That is not the ChaCha20 quarter-round.
What the algorithm guarantees
The same password, salt, N, r, p, and length reproduce the same bytes. During the walk, the index is the last 64 bytes interpreted as a little-endian integer, reduced modulo N. A guesser who wants to avoid the table has to recompute the missing entries. The honest verifier stores N blocks per parallel copy. The empty password in the fragment is a test input. A store still uses a unique salt.
The working steps
- Expand the password and salt with one iteration of PBKDF2-HMAC-SHA-256.
- For each of the
pblocks, fill a table ofNBlockMix outputs. - Walk
Nmore times. At each step, mix the current block with the table entry named by its last 64 bytes. - Pass the joined results through PBKDF2-HMAC-SHA-256 once more to obtain the derived key.
Approaches that look simpler and fail
| Approach | Why it fails when the table is smaller than the cost parameter |
|---|---|
Use N = 16 in a password store |
Sixteen entries are the test size. A store chooses an N that the verifier can afford and a guesser must store. |
| Write the BlockMix outputs in the order they were produced | The published BlockMix writes the even chunks first and the odd chunks second. |
| Use the ChaCha20 quarter-round inside BlockMix | Salsa20/8 rotates by 7, 9, 13, and 18. ChaCha20 is a different permutation. |
| Drop the salt because the table is large | Two equal passwords still need different salts, or they store the same derived key. |
PBKDF2 is the outer function. scrypt is that function wrapped around the ROMix table.
A working example
This Python fragment runs scrypt with N = 16, r = 1, and p = 1 on an empty password and an empty salt. It prints 77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906.
import hashlib
import struct
def _salsa20_8(block: bytes) -> bytes:
w = list(struct.unpack("<16I", block))
def rot(x: int, n: int) -> int:
return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF
def quarter(a, b, c, d):
b ^= rot((a + d) & 0xFFFFFFFF, 7)
c ^= rot((b + a) & 0xFFFFFFFF, 9)
d ^= rot((c + b) & 0xFFFFFFFF, 13)
a ^= rot((d + c) & 0xFFFFFFFF, 18)
return a, b, c, d
x = w[:]
for _ in range(4):
x[0], x[4], x[8], x[12] = quarter(x[0], x[4], x[8], x[12])
x[5], x[9], x[13], x[1] = quarter(x[5], x[9], x[13], x[1])
x[10], x[14], x[2], x[6] = quarter(x[10], x[14], x[2], x[6])
x[15], x[3], x[7], x[11] = quarter(x[15], x[3], x[7], x[11])
x[0], x[1], x[2], x[3] = quarter(x[0], x[1], x[2], x[3])
x[5], x[6], x[7], x[4] = quarter(x[5], x[6], x[7], x[4])
x[10], x[11], x[8], x[9] = quarter(x[10], x[11], x[8], x[9])
x[15], x[12], x[13], x[14] = quarter(x[15], x[12], x[13], x[14])
return struct.pack("<16I", *[(x[i] + w[i]) & 0xFFFFFFFF for i in range(16)])
def _block_mix(data: bytes) -> bytes:
blocks = [data[i : i + 64] for i in range(0, len(data), 64)]
x = blocks[-1]
out = []
for block in blocks:
x = _salsa20_8(bytes(a ^ b for a, b in zip(x, block)))
out.append(x)
mixed = out[::2] + out[1::2]
return b"".join(mixed)
def scrypt(password, salt, n, r, p, length):
size = 128 * r
xy = hashlib.pbkdf2_hmac("sha256", password, salt, 1, dklen=p * size)
chunks = []
for i in range(p):
v = []
x = xy[i * size:(i + 1) * size]
for _ in range(n):
v.append(x)
x = _block_mix(x)
for _ in range(n):
j = int.from_bytes(x[-64:], "little") % n
x = _block_mix(bytes(a ^ b for a, b in zip(x, v[j])))
chunks.append(x)
return hashlib.pbkdf2_hmac("sha256", password, b"".join(chunks), 1, dklen=length)
derived = scrypt(b"", b"", 16, 1, 1, 64)
assert derived.hex() == "77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906"
print(derived.hex())
Where it is used
scrypt is the memory-hard password hash from Tarsnap, and it is the proof-of-work function of Litecoin. The ROMix table is the cost a guess has to store. PBKDF2 repeats a hash and does not fill that table.
When you can drop this algorithm
Drop the test parameters as a store cost. Keep scrypt where the verifier can hold the ROMix table and each password has its own salt. The parameters N, r, and p are stored with the salt.
Why this stays published
We publish this so a search for scrypt returns ROMix, Salsa20/8, and the reason the table is the cost. scrypt fills a table of Salsa20/8 mixes and walks that table, so a password guess has to store the table or recompute it.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
