0173 - Final - Informational - March 20, 2017 (9 years, 4 months ago)
20
2017
BIP: 173
Layer: Applications
Title: Base32 address format for native v0-16 witness outputs
Author: Pieter Wuille <[email protected]>
Greg Maxwell <[email protected]>
Comments-Summary: No comments yet.
Comments-URI: https://github.com/bitcoin/bips/wiki/Comments:BIP 173
Status: Final
Type: Informational
Created: 2017-03-20
License: BSD-2-Clause
Replaces: 142
Superseded-By: 350
This document proposes a checksummed base32 format, "Bech32", and a standard for native segregated witness output addresses using it.
This BIP is licensed under the 2-clause BSD license.
For most of its history, Bitcoin has relied on base58 addresses with a truncated double-SHA256 checksum. They were part of the original software and their scope was extended in BIP 13 for Pay-to-script-hash ( P2SH ). However, both the character set and the checksum algorithm have limitations:
Included in the Segregated Witness proposal are a new class of outputs (witness programs, see BIP 141 ), and two instances of it ("P2WPKH" and "P2WSH", see BIP 143 ). Their functionality is available indirectly to older clients by embedding in P2SH outputs, but for optimal efficiency and security it is best to use it directly. In this document we propose a new address format for native witness outputs (current and future versions).
This replaces BIP 142 , and was previously discussed here (summarized here ).
All examples use public key
0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
.
The P2WSH examples use
key OP_CHECKSIG
as script.
bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4
tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx
bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3
tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7
We first describe the general checksummed base32 1 format called Bech32 and then define Segregated Witness addresses using it.
A Bech32 2 string is at most 90 characters long and consists of:
part is unambiguously separated from the data part, avoiding potential collisions with other human-readable parts that share a prefix. It also allows us to avoid having character-set restrictions on the human-readable part. The separator is 1 because using a non-alphanumeric character would complicate copy-pasting of addresses (with no double-click selection in several applications). Therefore an alphanumeric character outside the normal character set was chosen.
.
The character set is chosen to minimize ambiguity according to this visual similarity data, and the ordering is chosen to minimize the number of pairs of similar characters (according to the same data) that differ in more than 1 bit. As the checksum is chosen to maximize detection capabilities for low numbers of bit errors, this choice improves its performance under some error models.
.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
|
+0 |
q |
p |
z |
r |
y |
9 |
x |
8 |
|
+8 |
g |
f |
2 |
t |
v |
d |
w |
0 |
|
+16 |
s |
3 |
j |
n |
5 |
4 |
k |
h |
|
+24 |
c |
e |
6 |
m |
u |
a |
7 |
l |
Checksum
The last six characters of the data part form a checksum and contain
no information. Valid strings MUST pass the criteria for validity
specified by the Python3 code snippet below. The function
bech32_verify_checksum
must return true when its arguments
are:
hrp
: the human-readable part as a string
data
: the data part as a list of integers representing
the characters after conversion using the table above
def bech32_polymod(values):
GEN = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
chk = 1
for v in values:
b = (chk >> 25)
chk = (chk & 0x1ffffff) << 5 ^ v
for i in range(5):
chk ^= GEN[i] if ((b >> i) & 1) else 0
return chk
def bech32_hrp_expand(s):
return [ord(x) >> 5 for x in s] + [0] + [ord(x) & 31 for x in s]
def bech32_verify_checksum(hrp, data):
return bech32_polymod(bech32_hrp_expand(hrp) + data) == 1
This implements a BCH code that guarantees detection of any error affecting at most 4 characters and has less than a 1 in 10 9 chance of failing to detect more errors. More details about the properties can be found in the Checksum Design appendix. The human-readable part is processed by first feeding the higher bits of each character's US-ASCII value into the checksum calculation followed by a zero and then the lower bits of each 3 .
To construct a valid checksum given the human-readable part and (non-checksum) values of the data-part characters, the code below can be used:
def bech32_create_checksum(hrp, data):
values = bech32_hrp_expand(hrp) + data
polymod = bech32_polymod(values + [0,0,0,0,0,0]) ^ 1
return [(polymod >> 5 * (5 - i)) & 31 for i in range(6)]
Error correction
One of the properties of these BCH codes is that they can be used for error correction. An unfortunate side effect of error correction is that it erodes error detection: correction changes invalid inputs into valid inputs, but if more than a few errors were made then the valid input may not be the correct input. Use of an incorrect but valid input can cause funds to be lost irrecoverably. Because of this, implementations SHOULD NOT implement correction beyond potentially suggesting to the user where in the string an error might be found, without suggesting the correction to make.
Uppercase/lowercase
The lowercase form is used when determining a character's value for checksum purposes.
Encoders MUST always output an all lowercase Bech32 string. If an uppercase version of the encoding result is desired, (e.g.- for presentation purposes, or QR code use), then an uppercasing procedure can be performed external to the encoding process.
Decoders MUST NOT accept strings where some characters are uppercase and some are lowercase (such strings are referred to as mixed case strings).
For presentation, lowercase is usually preferable, but inside QR codes uppercase SHOULD be used, as those permit the use of alphanumeric mode , which is 45% more compact than the normal byte mode .
A segwit address 4 is a Bech32 encoding of:
be of the same length as the mainnet counterpart (to simplify implementations' assumptions about lengths), but still be visually distinct.
for testnet.
Decoding
Software interpreting a segwit address:
Decoders SHOULD enforce known-length restrictions on witness programs. For example, BIP 141 specifies If the version byte is 0, but the witness program is neither 20 nor 32 bytes, the script must fail.
As a result of the previous rules, addresses are always between 14 and 74 characters long, and their length modulo 8 cannot be 0, 3, or 5. Version 0 witness addresses are always 42 or 62 characters, but implementations MUST allow the use of any version.
Implementations should take special care when converting the address to a scriptPubkey, where witness version n is stored as OP_n . OP_0 is encoded as 0x00, but OP_1 through OP_16 are encoded as 0x51 though 0x60 (81 to 96 in decimal). If a bech32 address is converted to an incorrect scriptPubKey the result will likely be either unspendable or insecure.
Only new software will be able to use these addresses, and only for receivers with segwit-enabled new software. In all other cases, P2SH or P2PKH addresses can be used.
SatoshiLabs maintains a full list of registered human-readable parts for other cryptocurrencies:
SLIP-0173 : Registered human-readable parts for BIP 173
The following strings are valid Bech32:
A12UEL5L
a12uel5l
an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs
abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw
11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j
split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w
?1ezyfcl
WARNING: During conversion to US-ASCII some
encoders may set unmappable characters to a valid US-ASCII character,
such as '?'. For example:
>>> bech32_encode('\x80'.encode('ascii', 'replace').decode('ascii'), [])
'?1ezyfcl'
The following string are not valid Bech32 (with reason for invalidity):
1nwldj5
: HRP character out of range
1axkwrx
: HRP character out of range
1eym55h
: HRP character out of range
an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx
:
overall max length exceeded
pzry9x0s0muk
: No separator character
1pzry9x0s0muk
: Empty HRP
x1b4n0q5v
: Invalid data character
li1dgmt3
: Too short checksum
de1lg7wt
+ 0xFF: Invalid character in checksum
A1G7SGD8
: checksum calculated with uppercase form of
HRP
10a06t8
: empty HRP
1qzzfhee
: empty HRP
The following list gives valid segwit addresses and the scriptPubKey that they translate to in hex.
BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4
:
0014751e76e8199196d454941c45d1b3a323f1433bd6
tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7
:
00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262
bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx
:
5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6
BC1SW50QA3JX3S
:
6002751e
bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj
:
5210751e76e8199196d454941c45d1b3a323
tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy
:
0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433
The following list gives invalid segwit addresses and the reason for their invalidity.
tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty
: Invalid
human-readable part
bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5
: Invalid
checksum
BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2
: Invalid
witness version
bc1rw5uspcuh
: Invalid program length
bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90
:
Invalid program length
BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P
: Invalid program
length for witness version 0 (per BIP 141)
tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7
:
Mixed case
bc1zw508d6qejxtdg4y5r3zarvaryvqyzf3du
: zero padding of
more than 4 bits
tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv
:
Non-zero padding in 8-to-5 conversion
bc1gmk9yu
: Empty data section
Design choices
BCH codes can be constructed over any prime-power alphabet and can be chosen to have a good trade-off between size and error-detection capabilities. While most work around BCH codes uses a binary alphabet, that is not a requirement. This makes them more appropriate for our use case than CRC codes . Unlike Reed-Solomon codes , they are not restricted in length to one less than the alphabet size. While they also support efficient error correction, the implementation of just error detection is very simple.
We pick 6 checksum characters as a trade-off between length of the addresses and the error-detection capabilities, as 6 characters is the lowest number sufficient for a random failure chance below 1 per billion. For the length of data we're interested in protecting (up to 71 bytes for a potential future 40-byte witness program), BCH codes can be constructed that guarantee detecting up to 4 errors.
Selected properties
Many of these codes perform badly when dealing with more errors than they are designed to detect, but not all. For that reason, we consider codes that are designed to detect only 3 errors as well as 4 errors, and analyse how well they perform in practice.
The specific code chosen here is the result of:
As a naive search would require over 6.5 * 10 19 checksum evaluations, a collision-search approach was used for analysis. The code can be found here .
Properties
The following table summarizes the chances for detection failure (as multiples of 1 in 10 9 ).
| Window length | Number of wrong characters | ||||||
|---|---|---|---|---|---|---|---|
|
Length |
Description |
≤4 |
5 |
6 |
7 |
8 |
≥9 |
|
8 |
Longest detecting 6 errors |
0 |
1.127 |
0.909 |
n/a |
||
|
18 |
Longest detecting 5 errors |
0 |
0.965 |
0.929 |
0.932 |
0.931 |
|
|
19 |
Worst case for 6 errors |
0 |
0.093 |
0.972 |
0.928 |
0.931 |
|
|
39 |
Length for a P2WPKH address |
0 |
0.756 |
0.935 |
0.932 |
0.931 |
|
|
59 |
Length for a P2WSH address |
0 |
0.805 |
0.933 |
0.931 |
||
|
71 |
Length for a 40-byte program address |
0 |
0.830 |
0.934 |
0.931 |
||
|
89 |
Longest detecting 4 errors |
0 |
0.867 |
0.933 |
0.931 |
||
This means that when 5 changed characters occur randomly distributed in the 39 characters of a P2WPKH address, there is a chance of 0.756 per billion that it will go undetected. When those 5 changes occur randomly within a 19-character window, that chance goes down to 0.093 per billion . As the number of errors goes up, the chance converges towards 1 in 2 30 = 0.931 per billion .
Even though the chosen code performs reasonably well up to 1023 characters, other designs are preferable for lengths above 89 characters (excluding the separator).
This document is inspired by the address proposal by Rusty Russell, the base32 proposal by Mark Friedenbach, and had input from Luke Dashjr, Johnson Lau, Eric Lombrozo, Peter Todd, and various other reviewers.
Due to an oversight in the design of bech32, this checksum scheme is not always robust against the insertion and deletion of fewer than 5 consecutive characters . Due to this weakness, BIP 350 proposes using the scheme described in this BIP only for Native Segwit v0 outputs.