{"id":503,"date":"2026-11-13T13:39:52","date_gmt":"2026-11-13T05:39:52","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-scrypt-stretches-a-password-with-a-large-table\/"},"modified":"2026-11-13T21:59:17","modified_gmt":"2026-11-13T13:59:17","slug":"how-scrypt-stretches-a-password-with-a-large-table","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-scrypt-stretches-a-password-with-a-large-table\/","title":{"rendered":"How scrypt stretches a password with a large table"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>scrypt<\/strong> stretches a password by filling a table and walking it at random. PBKDF2-HMAC-SHA-256 expands the password and salt into <code>p<\/code> blocks. Each block is mixed by ROMix. ROMix stores <code>N<\/code> successive BlockMix outputs, then walks <code>N<\/code> 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">PBKDF2 makes a guess take time and almost no memory. scrypt makes a guess store the ROMix table. <code>N<\/code> is the number of table entries and has to be a power of two. <code>r<\/code> sets the block size to <code>128r<\/code> bytes. <code>p<\/code> is the number of independent tables. The fragment uses <code>N = 16<\/code>, 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The same password, salt, <code>N<\/code>, <code>r<\/code>, <code>p<\/code>, and length reproduce the same bytes. During the walk, the index is the last 64 bytes interpreted as a little-endian integer, reduced modulo <code>N<\/code>. A guesser who wants to avoid the table has to recompute the missing entries. The honest verifier stores <code>N<\/code> blocks per parallel copy. The empty password in the fragment is a test input. A store still uses a unique salt.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Expand the password and salt with one iteration of PBKDF2-HMAC-SHA-256.<\/li>\n<li>For each of the <code>p<\/code> blocks, fill a table of <code>N<\/code> BlockMix outputs.<\/li>\n<li>Walk <code>N<\/code> more times. At each step, mix the current block with the table entry named by its last 64 bytes.<\/li>\n<li>Pass the joined results through PBKDF2-HMAC-SHA-256 once more to obtain the derived key.<\/li>\n<\/ol>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the table is smaller than the cost parameter<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Use <code>N = 16<\/code> in a password store<\/td>\n<td>Sixteen entries are the test size. A store chooses an <code>N<\/code> that the verifier can afford and a guesser must store.<\/td>\n<\/tr>\n<tr>\n<td>Write the BlockMix outputs in the order they were produced<\/td>\n<td>The published BlockMix writes the even chunks first and the odd chunks second.<\/td>\n<\/tr>\n<tr>\n<td>Use the ChaCha20 quarter-round inside BlockMix<\/td>\n<td>Salsa20\/8 rotates by 7, 9, 13, and 18. ChaCha20 is a different permutation.<\/td>\n<\/tr>\n<tr>\n<td>Drop the salt because the table is large<\/td>\n<td>Two equal passwords still need different salts, or they store the same derived key.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">PBKDF2 is the outer function. scrypt is that function wrapped around the ROMix table.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment runs scrypt with <code>N = 16<\/code>, <code>r = 1<\/code>, and <code>p = 1<\/code> on an empty password and an empty salt. It prints <code>77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import hashlib\n\nimport struct\n\ndef _salsa20_8(block: bytes) -&gt; bytes:\n    w = list(struct.unpack(&quot;&lt;16I&quot;, block))\n\n    def rot(x: int, n: int) -&gt; int:\n        return ((x &lt;&lt; n) | (x &gt;&gt; (32 - n))) &amp; 0xFFFFFFFF\n\n    def quarter(a, b, c, d):\n        b ^= rot((a + d) &amp; 0xFFFFFFFF, 7)\n        c ^= rot((b + a) &amp; 0xFFFFFFFF, 9)\n        d ^= rot((c + b) &amp; 0xFFFFFFFF, 13)\n        a ^= rot((d + c) &amp; 0xFFFFFFFF, 18)\n        return a, b, c, d\n\n    x = w[:]\n    for _ in range(4):\n        x[0], x[4], x[8], x[12] = quarter(x[0], x[4], x[8], x[12])\n        x[5], x[9], x[13], x[1] = quarter(x[5], x[9], x[13], x[1])\n        x[10], x[14], x[2], x[6] = quarter(x[10], x[14], x[2], x[6])\n        x[15], x[3], x[7], x[11] = quarter(x[15], x[3], x[7], x[11])\n        x[0], x[1], x[2], x[3] = quarter(x[0], x[1], x[2], x[3])\n        x[5], x[6], x[7], x[4] = quarter(x[5], x[6], x[7], x[4])\n        x[10], x[11], x[8], x[9] = quarter(x[10], x[11], x[8], x[9])\n        x[15], x[12], x[13], x[14] = quarter(x[15], x[12], x[13], x[14])\n    return struct.pack(&quot;&lt;16I&quot;, *[(x[i] + w[i]) &amp; 0xFFFFFFFF for i in range(16)])\n\ndef _block_mix(data: bytes) -&gt; bytes:\n    blocks = [data[i : i + 64] for i in range(0, len(data), 64)]\n    x = blocks[-1]\n    out = []\n    for block in blocks:\n        x = _salsa20_8(bytes(a ^ b for a, b in zip(x, block)))\n        out.append(x)\n    mixed = out[::2] + out[1::2]\n    return b&quot;&quot;.join(mixed)\n\ndef scrypt(password, salt, n, r, p, length):\n    size = 128 * r\n    xy = hashlib.pbkdf2_hmac(&quot;sha256&quot;, password, salt, 1, dklen=p * size)\n    chunks = []\n    for i in range(p):\n        v = []\n        x = xy[i * size:(i + 1) * size]\n        for _ in range(n):\n            v.append(x)\n            x = _block_mix(x)\n        for _ in range(n):\n            j = int.from_bytes(x[-64:], &quot;little&quot;) % n\n            x = _block_mix(bytes(a ^ b for a, b in zip(x, v[j])))\n        chunks.append(x)\n    return hashlib.pbkdf2_hmac(&quot;sha256&quot;, password, b&quot;&quot;.join(chunks), 1, dklen=length)\n\nderived = scrypt(b&quot;&quot;, b&quot;&quot;, 16, 1, 1, 64)\nassert derived.hex() == &quot;77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906&quot;\nprint(derived.hex())\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>scrypt<\/strong> 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>N<\/code>, <code>r<\/code>, and <code>p<\/code> are stored with the salt.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for scrypt returns ROMix, Salsa20\/8, and the reason the table is the cost. <strong>scrypt fills a table of Salsa20\/8 mixes and walks that table, so a password guess has to store the table or recompute it.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,49],"tags":[],"class_list":["post-503","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cryptography"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=503"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":715,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/503\/revisions\/715"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}