1

The original reason

Ethereum digital signature and bitcoin

Ethereum digital signature, which almost completely follows bitcoin’s digital signature algorithm ECDSA-SECP256K1. Only hashes are generated differently, and we’ll talk about that later. ECDSA- SECP256K1 is an asymmetric encryption algorithm.

What is the ECDSA

Ethereum digital signature algorithm uses elliptic curve digital signature algorithm, ECDSA for short. EC stands for “elliptic curve” and DSA for “Digital signature Algorithm”.

What is a secp256k1

Elliptic curve algorithm is simply to draw a curve with X and Y coordinates. How to draw this curve requires many parameters. Ethereum uses a set of parameters called secP256k1 to determine the shape of the ellipse. So, the full name of ethereum’s signature algorithm is ECDSA-SECP256K1.

What is asymmetric encryption

What is symmetric encryption and what is asymmetric encryption? To put it simply, there is only one key, symmetric encryption, encryption and decryption with it. Two keys, asymmetric encryption, encryption with one key, decryption with the other. Compared with symmetric encryption algorithm, asymmetric encryption has the advantage of no need to expose the encryption key on the network, and is more secure from mechanism. The disadvantage is that the encryption efficiency is much lower than symmetric encryption. Therefore, asymmetric encryption is generally only used for small data encryption operations such as digital signatures.

Common asymmetric encryption algorithms in addition to elliptic encryption algorithm, there is the famous RSA. The differences between elliptic encryption and RSA are as follows:

  • Elliptic encryption has a shorter key

  • Elliptic encryption is faster and equally secure

  • RSA private key and public key can be decrypted interchangeably, but elliptic encryption can only encrypt private key and public key.

The private key

The ethereum private key is a 32-byte number ranging from 1 to 0xFFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4140. This number can be generated by a pseudo-random algorithm (PRNG). In fact, 0 is also a legal private key, but it is a special private key, and ethereum’s creation block is generated by this private key.

The public key

Ethereum’s uncompressed public key is a 65-byte number that is inherited from Bitcoin. But ethereum only uses 64 of these bytes, one of these 64 bytes, 32 bytes represent the X coordinate of the elliptic curve, 32 bytes represent the Y coordinate of the elliptic curve. The XY coordinates are derived by the private key ecdSA-secP256K1. Therefore, the public key of elliptic curve algorithm is calculated by the private key. On the other hand, deducing a private key from a public key is almost impossible with existing computers, which is what Ethereum and Bitcoin are based on. If there were a big leap in computing technology, such as quantum computers, the private keys of all accounts on the existing chain would be exposed. Of course, blockchain technology itself will certainly continue to evolve.

The hash

Hashes can also be figuratively called abstracts. A fixed-length “digest” can be generated regardless of the size of the message, and this “digest” can be used to verify that the message has not been tampered with. Validation of the digest fails as soon as the message is modified by one byte.

Bitcoin’s hash algorithm uses SHA2-256. In contrast to SHA1, SHA2 simply extends the number of bytes in the hash. SHA1 has been breached, and it is only a matter of time before SHA2 is breached.

Ethereum uses the new SHA3-256 hash algorithm. Unlike SHA1 and SHA2, SHA3 does not simply expand the number of bytes, but adopts the new Keccak algorithm. SHA3 with the same byte width is more secure than SHA2.

address

The address of Ethereum is a string encoded in Base58 by the public key through a series of hashes and transformations. The process is not described. Base58 encoding is similar to Base64, which uses “readable symbols” to represent binary data. Base58 removes some letters and numbers that are easy to cause visual confusion relative to Base64.

The signature

Signing is simply encrypting the hash of a message with a private key. When an Ethereum node sends a message to another node, it signs the message’s hash with its private key, and then sends the signature and the message itself to the other node.

The process is shown as follows:

Check the signature

After receiving the message and signature from the peer, the node performs an “Recover” operation and derives the public key of the peer using the message and signature. Then through the public key, signature, message hash value to calculate a value called “R”, this R is a part of the signature, the verification signature is calculated by r and r carried in the signature line comparison, if the consistency is verified.

The process is shown as follows:

2

Code implementation

Ethereum project GEth has two sets of ecDSA-SECP256P1 implementations. One is pure GO and one is based on the C library. None of the underlying algorithms were written by ethereum developers, using the principles of the open source world.

Go to realize

Interface source in crypto/signature_nocgo.go. This file is just a wrapper, called the real implementation of a third party currency go project, source in vendor//github.com/bitcsuite/bitcd.

C implementation

Interface source code in crypto/signature_cgo.go. This is also a wrapper and crypto/secp256k1/ is called further down. This is also the wrapper, and below that is the libsecp256K1 library for the Bitcoin C language project.

API and its functions

The interfaces of signature_nocgo.go and signature_cgo.go are the same, as follows:

// Recover the public key by hashing and signing the message

func Ecrecover(hash, sig []byte) ([]byte, error) {}

// Compute the ECDSA signature with the hash and private key

func Sign(hash []byte, prv *ecdsa.PrivateKey) {}

// Hash the signature through the public key

func VerifySignature(pubkey, hash, signature []byte) bool {}

// Decompress the 33-byte public key into a 65-byte public key

func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {}

// Compress a 65-byte uncompressed public key into a 33-byte compressed public key

func CompressPubkey(pubkey *ecdsa.PublicKey) {}

HiBlock blockchain community ** “Read code together” ** Partner — Ocean life

Originally published in Jane’s Book

To learn about and join “Doing Things Together”, please see the details at the end of the article ~

Course recommended

Course Topic: Unlocking the Mysteries of Digital Currency Trading — 2 lessons on building a digital currency exchange

Identify the qr code below to add wechat, reply “006” to register.

Click “Read the article” to go to the GitHub page of HiBlock