1. What is Bitcoin?
Bitcoin is an electronic currency (digital currency), and Bitcoin is a currency based on cryptography. On November 1, 2008, Satoshi Nakamoto (I don’t know whether it is a human or an AI, an organization or an individual?) proposed the concept of Bitcoin. He published a paper on Bitcoin, which is now the white paper of Bitcoin.
In this whiter paper, a decentralized electronic bookkeeping system is proposed. The traditional electronic cash is used by the bank to keep accounts, because the bank is behind the country’s credit, and the decentralized electronic bookkeeping system is shared by the participants. Account. People obtain bitcoins through mining, and complete payment through public accounting.
2. The principle of blockchain
(1). Merkel Tree
Hash tree or Merkel tree is a tree in which each leaf node is marked with the password hash of the data block, and each non-leaf node is marked with the password of its child node label. Greek mark. . Hash trees allow effective and safe verification of the contents of large data structures. Hash tree is a generalization of hash list and hash chain. In order to prove that a leaf node is part of a given binary hash tree, it is necessary to calculate the number of hashes that is proportional to the logarithm of the number of leaf nodes of the tree. This is the opposite of the hash list, and the number of the hash list is the same as the leaf node itself. Is directly proportional to the number.
This is an example of a binary hash tree. Hash 0-0 and 0-1 are the hash values of data blocks L1 and L2, respectively, and hash 0 is the concatenation of hashes 0-0 and 0-1.
(2). Block and blockchain
There are many blocks in blockchain, these blocks are encrypted using links, and each block contains a block header and transaction information (usually ** Merkel tree ** means), the header information contains the hash sequence and the timestamp. According to the design, the blockchain can resist the modification of its data. This is because once recorded, the data in any given block cannot be changed retrospectively without changing all subsequent blocks.
Logically speaking, the blockchain can be seen as consisting of 5 layers:
- Infrastructure (hardware)
- Network (node discovery, information dissemination and verification)
- Consensus (proof of work, certificate of shareholding)
- Data (blocks, transactions)
- Apps (dApps)
Because the data in any given block cannot be changed retrospectively, without changing all subsequent blocks.
The formation of the blockchain. The main chain (black) consists of the longest series of blocks from the founding block (green) to the current block. There are isolated blocks (purple) outside the main chain.
Allow participants to verify and review transactions independently and relatively cheaply. Use peer-to-peer networks and distributed timestamp servers to independently manage blockchain databases. They certify that they carry collective self-interest through large-scale collaboration. Such a design promotes a robust workflow in which participants’ uncertainty about data security is negligible. The use of blockchain eliminates the infinite reproducibility of digital assets. It confirmed that each unit of value was transferred only once, thus solving the long-standing double-spending problem. Blockchain has been described as a value exchange protocol. Blockchain can maintain ownership because when properly set up to detail the exchange agreement, it can provide a record of mandatory offers and acceptances.
A simple example: A transaction was initiated among the four persons on ABCD. Among them, A transferred 100 bitcoins to B. Because this is a decentralized accounting method. Therefore, each of these four people will record this transaction on their own ledger, and will record the transfer of 100 Bitcoins from A to B. This is a piece of transaction information that will be recorded in the block ** Each block is 1MB in size and can store about 4k pieces of information. ** In this section, why would I use A as the standard to notify others? Who will we use in normal trading? And why should we keep accounts? Is it good for us? These are all problems encountered in design.
3. Reasons for using blockchain for accounting
As it is said that there is no benefit, Satoshi Nakamoto mentioned the incentive scheme in his paper on the design of blockchain. The person who keeps the book will get two kinds of benefits: the first is rewards for handling fees, and the second is rewards for packaging blocks (rewards from the system). The initiation of each transaction will charge the user a small fee, and these fees will be rewarded to the users who book the package. In the second reward, the system rewards the person who booked the package. In Satoshi Nakamoto’s paper, he wrote that this system will generate a block every ten minutes, and every time a block is generated, a certain amount of Bitcoin will be rewarded. Since 2008, it has been 50 bitcoins, which will decay by half every four years. By analogy, the number of bitcoins obtained will decrease. According to this algorithm, we can calculate that there are about 21 million Bitcoins in the world, so Bitcoin will become more and more difficult to mine over time.
4. Who is the so-called “center” in each block
(1). Obtain the power of packaging through the proof-of-work mechanism
Satoshi Nakamoto also wrote in the paper that each user or node will compete for the power of bookkeeping through a proof-of-work mechanism. The first user that can be calculated for each block is the producer of the next block. The only way to select users through proof of work is to let users solve “mathematics” problems. This mathematical problem is based on the SHA256 algorithm, so the only way is to try one by one. Whoever gets the answer to this question first will be eligible for packaging. This is also called the so-called ** Mining**.
(2). SHA256 algorithm
The Secure Hash (SHA) algorithm is a series of cryptographic hash functions issued by the National Institute of Standards and Technology as the U.S. Federal Information Processing Standard. SHA stands for Secure Hash Algorithm. SHA-1 and SHA-2 are two different versions of this algorithm. They differ in structure (the way the resulting hash is created from the original data) and the bit length of the signature. SHA-2 is regarded as the successor to SHA-1 because it is an overall improvement. First of all, people regard the position length as an important difference. SHA-1 is a 160-bit hash. SHA-2 is actually a “hash” series, and has various lengths, the most popular is 256-bit (SHA256), The final output is a 256-bit binary number. The following is the pseudo code of the SHA256 algorithm:
1 | Initialize hash values: |
(3). SHA256 core algorithm code formed by C++ language
1 |
|
5. Blockchain and Bitcoin security
Are blockchain and Bitcoin safe? How do they prevent counterfeiting, tampering and double payment? Identity authentication technology can be face recognition, signature, fingerprint, etc. in life. But once they are digitized, they can be forged by copying, so Bitcoin uses the electronic signature method. Electronic signature technology mainly uses asymmetric encryption. First, Bitcoin will generate a random number, this technology will generate a private key (only the user knows) through a random number, encrypt it with a private key, and then generate a public key through the private key. Public) can be decrypted by the public key, and finally a public address will be generated, and the user can use the address to conduct anonymous transactions. The typical algorithm is RSA, and Bitcoin uses an elliptic curve encryption algorithm.
Conversion process
First use a random number generator to generate a private key, which is a 256-bit binary number. The private key cannot be made public and is equivalent to the password of a bank card.
The private key generates a public key through the SECP256K1 algorithm. SECP256K1 is an elliptic curve encryption algorithm. Its function is similar to the RSA algorithm. A public key is generated through a known private key, but the private key cannot be deduced from the public key.
Like the SHA256 algorithm, RIPEMD160 is also a HASH algorithm. The hash value of the public key can be obtained from the public key, but the public key cannot be derived from the hash value.
Connect the one-byte version number to the header of the public key hash, and then perform two SHA256 operations on it, and use the first 4 bytes of the result as the check value of the public key hash and connect it at the end.
Use BASE58 to encode the result of the previous step to get the wallet address (equivalent to a bank account). For example, A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
(1). Solve forged transaction records
The figure shows the event that A gives B1 bitcoins. After obtaining the digest through the hash operation, the digest is encrypted with the private key to obtain the password. Due to the uniqueness of the private key, the digest is unique. Through broadcasting, the messages delivered by A are: A transfers to B 1 bitcoin, A’s public key, and A’s password.
Of course, we can assume that the message that A transferred 1 Bitcoin to B is false, and the digest 1 is obtained based on this hash operation. And using A’s public key to decrypt the password to get Digest 2. At this time Digest 1 and Digest 2 are different. Obviously, “A gives B 1 bitcoin” is a forged message.
(2). Prevent tampering with transaction records
In order to prevent tampering events, the blockchain will protect the entire blockchain according to the longest chain principle:
When the block chain branches, that is, more than one person dug up the next block in almost the same time, and the chain direction has branched. Generally speaking, the longest chain principle is used for selection. Suppose that user group A chooses to go on the chain to continue mining, and user group B chooses to go off the chain to continue mining. If group A digs out the next mine first and adds a new block to the chain, group B will continue mining after moving to the new block. Normally, the down chain is discarded.
That is to say, where there is a branch in the blockchain, it will compare the upper and lower chains who dig out the second block first (whose chain becomes the long branch first), keep the long chain, and give up the short chain.
Therefore, according to the longest chain principle, if someone wants to tamper with the information of a block on the block chain, he must lead a branch at that block and create a new chain to make the new chain exceed the length of the original chain. That is to say, the computing power of the mining machine controlled by him alone exceeds the computing power of the remaining mining machines in the world ** (faster than anyone else). The probability of this realization is very small. For example, a person controls 90% of the mining machines in the world to tamper with the transaction records of a chain. Why doesn’t he use so many mining machines to mine seriously?
(3). Prevent double payment transactions
When a double payment event occurs, for example, A has only 100 bitcoins, but at the same time broadcasts the message “A to B 100 bitcoins” (recorded as message b) and “A to C
100 Bitcoins” (recorded as message c), group D will not confirm message c if it receives message b first. Similarly, group E will not confirm message b if it receives message c first. At this time It depends on who in groups D and E can calculate the math problem first, and the one who digs the mine first can write the confirmed message in the new block, while the other message is invalid.
Copyright Notice
This article is the original content of Junhao except the referenced content below, and the final interpretation right belongs to the original author. If there is any infringement, please contact to delete. Without my authorization, please do not reprint it privately.
6. References
[1]. BMoney http://www.weidai.com/bmoney.txt
[2]. Wikipedia https://en.wikipedia.org/wiki/Bitcoin
[3]. Wikipedia https://en.wikipedia.org/wiki/SHA-2
[4]. Wikipedia https://en.wikipedia.org/wiki/Blockchain
[5]. Youtube https://www.youtube.com/watch?v=g_fSistU3MQ&t=11s
[6]. BitcoinOrg https://bitcoin.org/bitcoin.pdf