<p>Pieter Wuille's comment gives a nice summary:</p>

<blockquote>
  <p>basically: take a bech32 string, xor a 1 into the last character, then push or pop as many 'q's as you like, and then xor a 1 into the last character again... should always give you a valid new bech32 string</p>
</blockquote>

<p>Checksum code taken from Bitcoin Core is:</p>

<pre><code>uint32_t PolyMod(const data&amp; v)
{
    uint32_t c = 1;
    for (const auto v_i : v) {
        uint8_t c0 = c &gt;&gt; 25;

        c = ((c &amp; 0x1ffffff) &lt;&lt; 5) ^ v_i;

        if (c0 &amp; 1)  c ^= 0x3b6a57b2;
        if (c0 &amp; 2)  c ^= 0x26508e6d;
        if (c0 &amp; 4)  c ^= 0x1ea119fa;
        if (c0 &amp; 8)  c ^= 0x3d4233dd;
        if (c0 &amp; 16) c ^= 0x2a1462b3;
    }
    return c;
}
</code></pre>

<p>Basically, the checksum function has an internal variable that is modified with every 5-bit character, similar to SHA without padding and different sizes. What you should notice is that it both this and SHA are prone to length extension attacks, which means someone who doesn't know <code>x</code> but knows <code>H(x)</code> can calculate <code>H(x || A)</code>, where <code>A</code> is any data sequence and <code>||</code> is the concat operator.</p>

<p>Called from:</p>

<pre><code>data CreateChecksum(const std::string&amp; hrp, const data&amp; values)
{
    data enc = Cat(ExpandHRP(hrp), values);
    enc.resize(enc.size() + 6); // Append 6 zeroes
    uint32_t mod = PolyMod(enc) ^ 1; // Determine what to XOR into those 6 zeroes.
    data ret(6);
    for (size_t i = 0; i &lt; 6; ++i) {
        // Convert the 5-bit groups in mod to checksum values.
        ret[i] = (mod &gt;&gt; (5 * (5 - i))) &amp; 31;
    }
    return ret;
}
</code></pre>

<p>In this function, simply the result of PolyMod is serialized, but the checksum's least significant bit is XOR'ed with 1. If you undo that, you can extend the checksum by feeding the PolyMod function (or the Initialize-Update-Finalize version of it) a number of five-bit zeros more, and if you XOR the least significant bit with 1 again, you'll get a valid checksum.</p>

<p>Why? Because a zero byte does not cause any <em>if</em> branch to run in the PolyMod function loop. c was zero, and it stays zero.</p>

<h3>You can now skip to the bottom to my more clear explanation</h3>

<p>The reason XOR-1 was a part of the specification is (from my understanding) was to prevent adding an additional character at the end in a way that the checksum is still correct. Otherwise,</p>

<ul>
<li><code>ii2134hk2xmat79tqq</code></li>
<li><code>ii2134hk2xmat79tqqq</code></li>
<li><code>ii2134hk2xmat79tqqqq</code></li>
</ul>

<p>would all be correct, which is worse (even though there are length checks...)</p>

<p>In the CreateChecksum function, you'll see that to create a signature, 6 empty five-bits are appended (because it's how BCH works, checksum is set to empty before being calculated). Because PolyMod has no knowledge if the zeros are part of the input or the empty checksum, they're treated the same. Therefore you can:</p>

<p>1) Take any Bech32 address as a five-bit byte sequence</p>

<p>2) Xor the last byte with 1</p>

<p>3) Append arbitrary numbers</p>

<p>4) Add 6 empty bytes</p>

<p>5) Calculate PolyMod result</p>

<p>6) Xor the last byte with 1</p>

<p>7) Encode</p>

<p>The quirk described in the link given is when those "arbitrary numbers" in step 3 are zeros. The PolyMod requests a payload with 6 zeros appended to be checksummed (and trailing zeros make a difference in this case as c is nonzero), and the checksum should come just after the payload, if we want the checksum to be correct. You can add zeros to the end, and it'll stay correct. Only the XOR-1 thing has to be done.</p>

<hr>

<h2>Alternative explanation without distracting details</h2>

<p>Here's how the BCH checksum works and why it's unique:</p>

<p>If you give the PolyMod function a payload and 6 empty bytes (which is the length of the signature) it'll give you a value.</p>

<p>If you serialize this value in the place of the last 6 empty bytes and call the PolyMod function, it'll return zero. Whenever the checksum is correct, it returns zero. You can then discard the last 6 bytes and use the rest while decoding</p>

<p>Consider the PolyMod function's state after the payload and the checksum is processed. It returns zero, which is the value of the c variable (you can look at the code at the top). Same for c0. Since then, as long as v_i equals zero, c will stay zero. Therefore you can append zeros at the end without breaking the checksum. Or the converse, if you have a valid address, you can remove zeros at the end.</p>

<p>Plus some Xor-ing with 1, as I explained in the middle of the post. In Bech32 alphabet, p ^ 1 = q and q ^ 1 = p. </p>

CC BY-SA 4.0
2019-11-10T20:45:17.353Z
2019-11-11T00:31:20.373Z
2019-11-11T00:31:20.373Z
MCCCS
<p>I'm going to provide a more algebraic explanation of the issue.</p>
<p>Every valid (and invalid) Bech32 string can be seen as a polynomial in the variable <em>x</em>, where the coefficients are elements of GF(32). I'm going to ignore the GF(32) part in what follows, so it suffices to say that they're simply numbers from 0 to 31 inclusive, with weird addition and multiplication rules that guarantee every operation always stays within the range 0-31.</p>
<p>The data part (which means the string excluding the human-readable part (HRP) and the &quot;1&quot; that follows it, but including the 6 checksum characters at the end) uses the character set that's included in BIP173 in table form. For example, a &quot;q&quot; means 0, &quot;p&quot; means 1, &quot;3&quot; means 17, and &quot;l&quot; means 31. Each of these characters forms the coefficient of the term of our polynomial. I'm going to ignore the human-readable part as well in what follows as it doesn't matter for the issue, but for completeness the HRP also encodes coefficients of the polynomial, but using a different (and slightly more complicated) translation.</p>
<p>For example, the string &quot;3qlp&quot; corresponds to the polynomial <em>17x<sup>3</sup> + 31x + 1</em>. As &quot;q&quot; represents 0, there is no <em>x<sup>2</sup></em> term.</p>
<p>Now, every <strong>valid</strong> Bech32 string corresponds to a polynomial <em>p(x)</em> that can be written as <em>p(x) = f(x)⋅g(x) + 1</em> for some <em>f(x)</em>, where <em>g(x) = x<sup>6</sup> + 29x<sup>5</sup> + 22x<sup>4</sup> + 20x<sup>3</sup> + 21x<sup>2</sup> + 29x + 18</em>.</p>
<p>Why is that <em>+ 1</em> there? If it wasn't, then for every valid <em>p(x) = f(x)⋅g(x)</em>, <em>x⋅f(x)⋅g(x) = x⋅p(x)</em> would also be a valid polynomial. As multiplying by <em>x</em> is simply adding a 0 (or &quot;q&quot;) at the end, this would let anyone accidentally mutate valid Bech32 strings into distinct valid Bech32 strings by simply appending a &quot;q&quot;.</p>
<p>However, it turns out that adding the <em>+ 1</em> does not fully solve this sort of issue. It isn't possible anymore to simply multiply by <em>x</em>, but it's still the case that for every valid <em>p(x)</em>, <em>x<sup>i</sup>⋅(p(x) - 1) + 1</em> for any <em>i &gt; 0</em> is also valid. If <em>p(x)</em> happens to end in a &quot;p&quot;, it can be written as <em>x⋅e(x) + 1</em> for some <em>e(x)</em>. In this case, <em>x<sup>i</sup>⋅e(x) + 1</em> is also a valid string, for every value of <em>i</em>. In string form, this corresponds to inserting &quot;q&quot; characters in the penultimate position (just before the final &quot;p&quot;).</p>
<p>Similarly, if <em>p(x)</em> happens to end in a &quot;qp&quot;, it can be written as <em>x<sup>i</sup>⋅e(x) + 1</em> for some <em>e(x)</em> and <em>i &gt; 1</em>. In this case, <em>xj⋅e(x) + 1</em> where <em>j &lt; i</em> is also a valid string. In string form, this corresponds to removing &quot;q&quot; character in the penultimate position (just before the final &quot;p&quot;).</p>

CC BY-SA 4.0
2019-11-11T09:45:01.510Z
It might be worth mentioning that addresses that end with q('s) and then p can equally have q's removed.  You also might want to point out the invalid padding constraint. Also that v0 is immune due length restrictions.
G. Maxwell
CC BY-SA 4.0
2019-11-11T06:01:01.983Z
2021-03-10T19:49:40.870Z
2021-03-10T19:49:40.870Z
Pieter Wuille
<p>A bech32 address ending with <code>p</code> can be modified be inserting or removing <code>q</code> characters immediately before the final <code>p</code> character to make a new valid bech32 address. Why does this work?</p>

<p>Are there any other similar mutation weaknesses in bech32?</p>

<p>References:</p>

<ul>
<li><a href="https://github.com/sipa/bech32/issues/51" rel="noreferrer">issue raised against bech32 repo</a></li>
<li><a href="https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-November/017443.html" rel="noreferrer">mailing list post</a></li>
</ul>

<bech32-address><bip350-bech32m>
How does the bech32 length-extension mutation weakness work?
jnewbery
# How does the bech32 length-extension mutation weakness work?

(Asked by: username_0 on 2019-11-10)

A bech32 address ending with `p` can be modified be inserting or removing `q` characters immediately before the final `p` character to make a new valid bech32 address. Why does this work?

Are there any other similar mutation weaknesses in bech32?

References:

* [issue raised against bech32 repo](https://github.com/sipa/bech32/issues/51)
* [mailing list post](https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-November/017443.html)

**Tags:** bech32-address, bip350-bech32m


---

## 2 answers

### Answer by username_2 on 2019-11-11. Score: 10 | Accepted answer

I'm going to provide a more algebraic explanation of the issue.

Every valid (and invalid) Bech32 string can be seen as a polynomial in the variable *x*, where the coefficients are elements of GF(32). I'm going to ignore the GF(32) part in what follows, so it suffices to say that they're simply numbers from 0 to 31 inclusive, with weird addition and multiplication rules that guarantee every operation always stays within the range 0-31.

The data part (which means the string excluding the human-readable part (HRP) and the "1" that follows it, but including the 6 checksum characters at the end) uses the character set that's included in BIP173 in table form. For example, a "q" means 0, "p" means 1, "3" means 17, and "l" means 31. Each of these characters forms the coefficient of the term of our polynomial. I'm going to ignore the human-readable part as well in what follows as it doesn't matter for the issue, but for completeness the HRP also encodes coefficients of the polynomial, but using a different (and slightly more complicated) translation.

For example, the string "3qlp" corresponds to the polynomial *17x3 + 31x + 1*. As "q" represents 0, there is no *x2* term.

Now, every **valid** Bech32 string corresponds to a polynomial *p(x)* that can be written as *p(x) = f(x)⋅g(x) + 1* for some *f(x)*, where *g(x) = x6 + 29x5 + 22x4 + 20x3 + 21x2 + 29x + 18*.

Why is that *+ 1* there? If it wasn't, then for every valid *p(x) = f(x)⋅g(x)*, *x⋅f(x)⋅g(x) = x⋅p(x)* would also be a valid polynomial. As multiplying by *x* is simply adding a 0 (or "q") at the end, this would let anyone accidentally mutate valid Bech32 strings into distinct valid Bech32 strings by simply appending a "q".

However, it turns out that adding the *+ 1* does not fully solve this sort of issue. It isn't possible anymore to simply multiply by *x*, but it's still the case that for every valid *p(x)*, *xi⋅(p(x) - 1) + 1* for any *i > 0* is also valid. If *p(x)* happens to end in a "p", it can be written as *x⋅e(x) + 1* for some *e(x)*. In this case, *xi⋅e(x) + 1* is also a valid string, for every value of *i*. In string form, this corresponds to inserting "q" characters in the penultimate position (just before the final "p").

Similarly, if *p(x)* happens to end in a "qp", it can be written as *xi⋅e(x) + 1* for some *e(x)* and *i > 1*. In this case, *xj⋅e(x) + 1* where *j < i* is also a valid string. In string form, this corresponds to removing "q" character in the penultimate position (just before the final "p").

> **Comments:**
> **username_3**: It might be worth mentioning that addresses that end with q('s) and then p can equally have q's removed.  You also might want to point out the invalid padding constraint. Also that v0 is immune due length restrictions.  

---

### Answer by username_1 on 2019-11-10. Score: 5

username_2 username_2's comment gives a nice summary:

> basically: take a bech32 string, xor a 1 into the last character, then push or pop as many 'q's as you like, and then xor a 1 into the last character again... should always give you a valid new bech32 string

Checksum code taken from Bitcoin Core is:

```
uint32_t PolyMod(const data& v)
{
    uint32_t c = 1;
    for (const auto v_i : v) {
        uint8_t c0 = c >> 25;

        c = ((c & 0x1ffffff) << 5) ^ v_i;

        if (c0 & 1)  c ^= 0x3b6a57b2;
        if (c0 & 2)  c ^= 0x26508e6d;
        if (c0 & 4)  c ^= 0x1ea119fa;
        if (c0 & 8)  c ^= 0x3d4233dd;
        if (c0 & 16) c ^= 0x2a1462b3;
    }
    return c;
}

```

Basically, the checksum function has an internal variable that is modified with every 5-bit character, similar to SHA without padding and different sizes. What you should notice is that it both this and SHA are prone to length extension attacks, which means someone who doesn't know `x` but knows `H(x)` can calculate `H(x || A)`, where `A` is any data sequence and `||` is the concat operator.

Called from:

```
data CreateChecksum(const std::string& hrp, const data& values)
{
    data enc = Cat(ExpandHRP(hrp), values);
    enc.resize(enc.size() + 6); // Append 6 zeroes
    uint32_t mod = PolyMod(enc) ^ 1; // Determine what to XOR into those 6 zeroes.
    data ret(6);
    for (size_t i = 0; i < 6; ++i) {
        // Convert the 5-bit groups in mod to checksum values.
        ret[i] = (mod >> (5 * (5 - i))) & 31;
    }
    return ret;
}

```

In this function, simply the result of PolyMod is serialized, but the checksum's least significant bit is XOR'ed with 1. If you undo that, you can extend the checksum by feeding the PolyMod function (or the Initialize-Update-Finalize version of it) a number of five-bit zeros more, and if you XOR the least significant bit with 1 again, you'll get a valid checksum.

Why? Because a zero byte does not cause any *if* branch to run in the PolyMod function loop. c was zero, and it stays zero.

### You can now skip to the bottom to my more clear explanation

The reason XOR-1 was a part of the specification is (from my understanding) was to prevent adding an additional character at the end in a way that the checksum is still correct. Otherwise,

* `ii2134hk2xmat79tqq`
* `ii2134hk2xmat79tqqq`
* `ii2134hk2xmat79tqqqq`

would all be correct, which is worse (even though there are length checks...)

In the CreateChecksum function, you'll see that to create a signature, 6 empty five-bits are appended (because it's how BCH works, checksum is set to empty before being calculated). Because PolyMod has no knowledge if the zeros are part of the input or the empty checksum, they're treated the same. Therefore you can:

1) Take any Bech32 address as a five-bit byte sequence

2) Xor the last byte with 1

3) Append arbitrary numbers

4) Add 6 empty bytes

5) Calculate PolyMod result

6) Xor the last byte with 1

7) Encode

The quirk described in the link given is when those "arbitrary numbers" in step 3 are zeros. The PolyMod requests a payload with 6 zeros appended to be checksummed (and trailing zeros make a difference in this case as c is nonzero), and the checksum should come just after the payload, if we want the checksum to be correct. You can add zeros to the end, and it'll stay correct. Only the XOR-1 thing has to be done.

---

## Alternative explanation without distracting details

Here's how the BCH checksum works and why it's unique:

If you give the PolyMod function a payload and 6 empty bytes (which is the length of the signature) it'll give you a value.

If you serialize this value in the place of the last 6 empty bytes and call the PolyMod function, it'll return zero. Whenever the checksum is correct, it returns zero. You can then discard the last 6 bytes and use the rest while decoding

Consider the PolyMod function's state after the payload and the checksum is processed. It returns zero, which is the value of the c variable (you can look at the code at the top). Same for c0. Since then, as long as v\_i equals zero, c will stay zero. Therefore you can append zeros at the end without breaking the checksum. Or the converse, if you have a valid address, you can remove zeros at the end.

Plus some Xor-ing with 1, as I explained in the middle of the post. In Bech32 alphabet, p ^ 1 = q and q ^ 1 = p.


---

