Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Bech32 Algorithm

by fragi
March 1, 2022
in Algorithms, Crypto
Share on FacebookShare on Twitter

Bech32 is a checksummed base32 format. It is being used in cryptocurrencies development (bitcoin, cardano,…) https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki, https://cips.cardano.org/cips/cip19/.

Let’s explore the algorithm.

A Bech32 is consist of three parts, the human readable part(hrp) the separator (always “1”) and the data part. Our algorithm will have two inputs the hrp and the input data. The output of the algorithm will be hrp + 1  + Transformed(input data + checksum) .

Once we calculate the checksum (will tackle it in a bit) we map the values as keys to the following array of characters qpzry9x8gf2tvdw0s3jn54khce6mua7l .

Let’s now calculate the checksum. The checksum is always 6 bytes.  In order to calculate it we have to follow the following algorithm:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)]
 
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]

To write he algorithm in Swift is straight forward but we have to be careful with the operators precedence. You can find an algorithm in Swift here: https://github.com/arnot-project/swift-bech32 along side with test cases so you can practise TDD while you implement the algorithm.

fragi

fragi

Related Posts

Swift – Disjoint Set (Union-Find)
Algorithms

Swift – Disjoint Set (Union-Find)

February 21, 2024

Disjoint Set is an algorithm that can help us solve many graph problems. There are many variations/improvements on the base...

Blake2b Algorithm
Algorithms

Operators precedence

March 2, 2022

Operators precedence matters when we have many operators in one expression. The operator with the highest precedence will be evaluated...

Blake2b Algorithm
Algorithms

Blake2b Algorithm

February 28, 2022

Blake2 is a cryptographic hash function. https://www.blake2.net/ Let's now explore how to implement this algorithm in Swift. The first option...

Memoization
Algorithms

Memoization

February 15, 2022

Memoization is a technique to increase the time performance of an algorithm by using some extra memory. According to Wikipedia:...

Performance testing in Xcode
Algorithms

Performance testing in Xcode

February 15, 2022

The performance is most of the times very important. In this post we will explore we can do performance testing...

How to wrap c code in a swift package
Crypto

How to wrap c code in a swift package

February 12, 2022

Sometimes we need to add c code in a Swift Package so we can use it in swift. One of...

Next Post
Blake2b Algorithm

Operators precedence

Delete Derived Data – zsh command

Delete Derived Data - zsh command

Swift – Disjoint Set (Union-Find)

Swift - Disjoint Set (Union-Find)

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.