Series, volume 2

The cryptographic hash function mechanism as seen in the online mahjong site "Tenhou"

Hello, my name is Mirumi, and I run a blog called miru memo (https://mirumi.me). My main profession is as a software engineer, and I spend my days either programming or writing for my blog. For some time, I will be contributing articles on a wide range of topics, primarily focusing on IT technology. Please treat me kindly.

Let's consider playing shogi and mahjong on computers. Our goal is to enjoy fair games without cheating, but unfortunately, this may be difficult for one of these two types of table games because there is a fundamental difference between them.

The question is whether there's a "behind-the-scenes" aspect that we can't see. In shogi, everything visible on the board – all the pieces – constitutes the game itself. The only unpredictable element, besides our own moves, is the behavior of our opponent. However, this unpredictability is inherent to the nature of the game, and there's nothing inherently wrong with that.

Mahjong is different. The contents of the wall (the stack of tiles) are hidden from all players, and no one knows what tile they will draw on their next turn. This implies that there's a behind-the-scenes element, suggesting that someone we don't know might be secretly manipulating things, perhaps thinking, "Hehe, let me subtly switch out the tile for the next draw!"

The online mahjong site "Tenhou" attempts to solve these issues using fundamental technologies such as software, cryptography, and mathematics. [Image 1]

1. The online mahjong website "Tenhou" (https://tenhou.net). Amateur players from all over Japan enjoy playing games on this site, and it is also frequently used as a venue for professional online tournaments.

Many of the recent mahjong matching services also employ a similar approach, and it can be said to be a pioneer in ensuring fairness in online mahjong.

In this article, we will introduce the "behind-the-scenes" of how Tenhou proves that it is free from cheating.

Publish the questions beforehand and then check the answers later

The fundamental concept underlying problem-solving is the "hash function." To understand its purpose and effect, let's tackle a problem like this:

Alice will send a love letter to Bob on February 14th. However, she wants to avoid any delays in sending it, so she decided to send the letter to Bob a little earlier. But if she does that, Bob will inevitably open the letter and see its contents before February 14th. Therefore, it seems necessary to consider a method where "the love letter is given beforehand, but the content of the confession remains undisclosed until February 14th."

This is where hash functions come in. A hash function has 1 input and 1 output, and it can be thought of like a vending machine: you put something in, and something comes out. The rules for this "vending machine" are as follows:

  • The system returns nonsensical output that has absolutely nothing to do with the input provided
  • For the same input, it will always return the same output
  • It is absolutely impossible to reverse the process and return the output back to its original input form

The image below illustrates this concept. [Image 2]

2

This process of converting data into a fixed-size string of characters is often referred to as hashing, or simply creating a hash.

Let's see how a hash function can help Alice solve her Valentine's Day problem.

Alice first writes the hashed message on a letter and sends it to Bob so that it arrives before February 14th. At this point, Bob can verify the content of the message, but according to rule 3, he cannot know what the original content was. Then, after February 14th, Alice shows Bob the "hashed version" of her confession message, stating that she is hashing it again in front of him. According to rule 2, if the same confession text is used, the same random output will always be generated. Therefore, by verifying the consistency between the hashed result shown to Bob and the message already sent to him, Alice can prove that "the seemingly random message I had previously sent was actually a love letter." If even one character of the message being hashed differs from the original message, rule 1 dictates that a completely different result will be output, so there will be no match.

Alice must have sent a love letter to Bob on or before February 14th, as she undoubtedly had written it by that date.

This turned out to be quite an unusual love confession, but don't you think this approach could potentially be used in a similar way to prove the generation of card arrangements in Mahjong?

All of the hashed deck data has been made publicly available

In Tenhou, a large number of tile stacks are generated long before the start of each game, and these are publicly available in a hashed format. With each game that begins, tile stacks are "consumed" from this pool. [Image 3]

3. This can be viewed from here: https://tenhou.net/stat/rand/?00000-000b. This page shows a "Shiban South" game using a deck of cards specifically designed for that hand, and you can see that each time a card is used, the "USED" indicator on the left side is added.

After each game, we repeatedly perform the same hashing operation on the actual tile data and can always verify that it matches the publicly available tile arrangement data. This means that we can continuously prove that no tile manipulation has occurred during the game.

Because it's impossible to know when and by whom a generated card stack will be used, it would realistically be impossible to "stack" cards specifically for a particular player (and there are also issues related to seating order).

More robust cryptographic hash functions

Regarding hash functions, the previous example with Alice and Bob only touched upon the basic aspect that "when hashed, completely random data is output, and it cannot be reversed." However, when using hashing in scenarios where security is paramount, a higher level of sophistication is required.

This is defined as a "cryptographic hash function," and it possesses the following 3 characteristics.

  • Original image calculation difficulty
  • Secondary source calculation difficulty
  • High impact resistance

Please note that these rules do not directly correspond to the previously mentioned vending machine regulations. If I were to make a connection, rule 3 has the strongest relationship with characteristic 1. I will briefly introduce each of these characteristics.

"Computational infeasibility" primarily refers to the fundamental property of hash functions known as "one-wayness." This characteristic indicates how difficult it is to reverse a hashed value and recover the original message. It's often compared to a mixer; once something has been mixed, it's impossible to return it to its original state. However, it's important to note that this concept is based on the idea that it is "technically extremely difficult (impractical) to do," meaning that if one had an enormous amount of time and computing power, this property could be overcome (and the same applies to the other two characteristics). In modern cryptography, almost all aspects are based on this principle. It's like saying, "Even an apple crushed in a mixer can theoretically be returned to its original shape if you try hard enough."

"Second preimage resistance" is a property that requires it to be difficult to find another input that produces the same hash value as a given input. This is similar to "strong collision resistance," which will be introduced next, but the meaning differs in that it describes the difficulty of searching for an alternative input when the original input and its corresponding hash value are known.

"Strong collision resistance" refers to the property that it is impossible to find 2 different inputs that produce the same hash value. This means that even if you are free to choose any pair of inputs, it is difficult to find a combination that results in the same hash value (i.e., a collision). It is also known as "collision resistance."

These characteristics become very easy to understand when you actually put yourself in the position of carrying out an attack. [Image 4]

Here is a diagram showing which of the individual characteristics can address different types of attacks. When all 3 are combined, they guarantee properties such as "the original message cannot be tampered with without changing the hashed result."

The cryptographic hash function used in Tenhou is called "SHA-512," which is one of the most well-known cryptographic hash functions. It is particularly known for its robustness in terms of security.

Actually, this process is also publicly available, and you can check it here (http://blog.tenhou.net/article/30503297.html). It's evident from the code of the published program that SHA-512 is also being used (the SHA-512 used in this source code is actually for the random processing mentioned later, but it's stated here (https://tenhou.net/stat/rand/) that SHA-512 is also used for the generated tile stacks).

The fundamental question is whether the deck of cards was properly "shuffled"

We have resolved the issues that could arise after the generation of the card deck, so let's also address the question of whether a sufficiently random card deck is being generated in the first place.

From the perspective of participants in the game of mahjong, when someone feels unlucky, they are more likely to question whether the tiles are truly being shuffled randomly, rather than suspecting any manipulation. In reality, computers inherently struggle with the concept of randomness, and it is extremely difficult for software alone to "generate truly random results from nothing."

To address this issue, modern CPU architectures now include a "hardware random number generator," which can obtain truly random entropy from sources such as device thermal noise. Furthermore, APIs that generate random numbers at the operating system level by utilizing user actions like mouse movements are also common; for example, the Windows CryptGenRandom function is a representative example.

Based on the powerful random output generated here, it is used as input for a simple-to-execute random function (also known as a pseudo-random number generator) within software. This process, as mentioned earlier, is also clearly described within the card arrangement generation algorithm that was previously presented.

In summary, "the pile of cards is generated through a random process that completely lacks predictable relationships or patterns between input and output, and which involves sufficient mixing."

However, even though something may be technically proven, there are still many situations that don't feel right. As someone who becomes frustrated by simply knowing the theory, I have experienced this firsthand.

(Written by Miru)

(Featured in the Autumn issue of Up&Coming '23)



Previous page
    
Index
    
Next page

Up&Coming

LOADING