{"id":485,"date":"2026-11-13T13:39:18","date_gmt":"2026-11-13T05:39:18","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-blowfish-builds-its-state-from-the-digits-of-pi\/"},"modified":"2026-11-13T21:58:48","modified_gmt":"2026-11-13T13:58:48","slug":"how-blowfish-builds-its-state-from-the-digits-of-pi","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-blowfish-builds-its-state-from-the-digits-of-pi\/","title":{"rendered":"How Blowfish builds its state from the digits of pi"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Blowfish<\/strong> is a 16-round Feistel cipher on 8-byte blocks. Eighteen P-words and four S-boxes of 256 words start as the fractional hexadecimal digits of pi. The key is xored into the P-array, and the cipher then encrypts a running block to replace every P-word and every S-box word. The round function splits a word into four bytes and combines the four S-box entries by addition and xor. The all-zero key and the all-zero block encrypt to <code>4ef997456198dd78<\/code>.<\/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\">A Feistel cipher needs a round function and a key schedule. Blowfish fills both from one idea: start from the digits of pi, fold the key into the P-array, and then use the cipher itself to replace the tables so the key affects every lookup. The digits are generated here by Machin&#8217;s formula, <code>16 arctan(1\/5) - 4 arctan(1\/239)<\/code>, so the fragment does not paste 1,042 constants. The first word is <code>0x243f6a88<\/code>. The block is still 8 bytes.<\/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\">For a key whose length is a multiple of 4, the schedule produces one permutation of 8-byte blocks. The example key is 8 bytes. Decryption uses the same tables with the P-array applied in reverse. The 8-byte block, not the key length, bounds how much data one key should encrypt. The construction does not add a nonce or a tag.<\/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>Compute the fractional hex digits of pi and split them into the 18 P-words and the four S-boxes.<\/li>\n<li>Xor each P-word with the next four key bytes, wrapping through the key.<\/li>\n<li>Encrypt a running 8-byte block, starting from zero, and write each ciphertext back over the next two P-words and then over the S-boxes.<\/li>\n<li>Encrypt the message block with sixteen Feistel rounds, then swap the halves and apply the last two P-words.<\/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 as the cipher for a long message<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Paste a random-looking table that is not the digits of pi<\/td>\n<td>The published initial state is those digits. A different table is a different cipher.<\/td>\n<\/tr>\n<tr>\n<td>Xor the key in and skip the self-encryption of the tables<\/td>\n<td>The key schedule is not finished until the running block has replaced the P-array and the S-boxes.<\/td>\n<\/tr>\n<tr>\n<td>Encrypt a long message under one key because the key can be long<\/td>\n<td>The block is 8 bytes. The birthday bound is the block size.<\/td>\n<\/tr>\n<tr>\n<td>Use the same key and no authenticator<\/td>\n<td>Blowfish is a block permutation. A modified ciphertext is not rejected.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">AES uses a 16-byte block and a public S-box. Blowfish is the Feistel cipher whose tables start from pi.<\/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 derives the P-array and the S-boxes from pi, expands the all-zero key, and encrypts the all-zero block. It prints <code>4ef997456198dd78<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import struct\n\ndef _pi_words(count: int) -&gt; list[int]:\n    digits = count * 8 + 4\n\n    def arctan_inv(x: int) -&gt; int:\n        scale = 16 ** digits\n        term = scale \/\/ x\n        total = term\n        x2 = x * x\n        k = 1\n        while term:\n            term \/\/= x2\n            piece = term \/\/ (2 * k + 1)\n            total += -piece if k &amp; 1 else piece\n            k += 1\n            if piece == 0:\n                break\n        return total\n\n    scaled = 16 * arctan_inv(5) - 4 * arctan_inv(239)\n    fractional = scaled % (16 ** digits)\n    text = f&quot;{fractional:x}&quot;.zfill(digits)[: count * 8]\n    return [int(text[i : i + 8], 16) for i in range(0, count * 8, 8)]\n\ndef _blowfish_state(key: bytes) -&gt; tuple[list[int], list[list[int]]]:\n    words = _pi_words(18 + 4 * 256)\n    p = words[:18]\n    s = [words[18 + i * 256 : 18 + (i + 1) * 256] for i in range(4)]\n    for i in range(18):\n        chunk = key[(4 * i) % len(key) : (4 * i) % len(key) + 4]\n        if len(chunk) &lt; 4:\n            chunk = (key + key)[(4 * i) % len(key) : (4 * i) % len(key) + 4]\n        p[i] ^= int.from_bytes(chunk, &quot;big&quot;)\n\n    def encrypt_pair(left: int, right: int) -&gt; tuple[int, int]:\n        for i in range(16):\n            left ^= p[i]\n            a, b, c, d = left.to_bytes(4, &quot;big&quot;)\n            f = (s[0][a] + s[1][b]) &amp; 0xFFFFFFFF\n            f = (f ^ s[2][c]) &amp; 0xFFFFFFFF\n            f = (f + s[3][d]) &amp; 0xFFFFFFFF\n            right ^= f\n            left, right = right, left\n        left, right = right, left\n        right ^= p[16]\n        left ^= p[17]\n        return left, right\n\n    block = 0\n    for i in range(0, 18, 2):\n        left, right = encrypt_pair(block &gt;&gt; 32, block &amp; 0xFFFFFFFF)\n        p[i], p[i + 1] = left, right\n        block = (left &lt;&lt; 32) | right\n    for box in s:\n        for i in range(0, 256, 2):\n            left, right = encrypt_pair(block &gt;&gt; 32, block &amp; 0xFFFFFFFF)\n            box[i], box[i + 1] = left, right\n            block = (left &lt;&lt; 32) | right\n    return p, s\n\ndef blowfish_encrypt(block: bytes, key: bytes) -&gt; bytes:\n    p, s = _blowfish_state(key)\n    left, right = struct.unpack(&quot;&gt;II&quot;, block)\n    for i in range(16):\n        left ^= p[i]\n        a, b, c, d = left.to_bytes(4, &quot;big&quot;)\n        f = (s[0][a] + s[1][b]) &amp; 0xFFFFFFFF\n        f = (f ^ s[2][c]) &amp; 0xFFFFFFFF\n        f = (f + s[3][d]) &amp; 0xFFFFFFFF\n        right ^= f\n        left, right = right, left\n    left, right = right, left\n    right ^= p[16]\n    left ^= p[17]\n    return struct.pack(&quot;&gt;II&quot;, left, right)\n\ncipher = blowfish_encrypt(bytes(8), bytes(8))\nassert cipher.hex() == &quot;4ef997456198dd78&quot;\nprint(cipher.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\">Blowfish remains in legacy file and session ciphers. The surviving descendant is <strong>bcrypt<\/strong>, whose EksBlowfish setup is this Feistel network with a cost parameter. New designs use AES or ChaCha20.<\/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 Blowfish for a new message. The 8-byte block is the limit. Keep the schedule as the example of a key-dependent table built from a public constant.<\/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 Blowfish returns the pi-digit tables and the all-zero ciphertext. <strong>Blowfish fills a 16-round Feistel cipher from the hex digits of pi, folds the key into that state, and encrypts 8-byte blocks.<\/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: Blowfish is a 16-round Feistel cipher on 8-byte blocks. Eighteen P-words and four S-boxes of 256 words start as the fractional hexadecimal digits of pi. The key is xored into the P-array, and the cipher then encrypts a running block to replace every P-word and every S-box&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-485","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\/485","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=485"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/485\/revisions"}],"predecessor-version":[{"id":697,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/485\/revisions\/697"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}