Explanation of blockchain ecc signature algorithm

The signature algorithm of Ethereum (Bitcoin) is the ECC signature, a brief introduction to the ECC signature

1. Principle of ecc signature


The ECC algorithm can be encoded with such a simple understanding, and there are many details that are not introduced.

A few concepts to understand:

1. The signature algorithm has the difference between private key and public key. The value of d in ecc is the private key, and the value of Q is the public key. Compared with rsa, there is one more concept of base point. In the figure, P is the base point.

2. The private key cannot be forged. The public key is used to verify the signature. Passing the verification can prove that it is the signature of the holder of the private key. Others cannot forge it.

3. The calculated signature value is R and S. Simply splicing R+S together is called a naked signature, and the blockchain uses naked signatures.

4. There is a sm2 variant of ecc in China, that is, the national secret algorithm. Currently, the ecc algorithm is not supported in China, only the sm2 algorithm is supported.


Second, ECC signature go language implementation

package main


import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/md5"
"crypto/rand"
"fmt"
"hash"
"io""os")func main() {curve := elliptic.P256()  privatekey := new(ecdsa.PrivateKey)privatekey, err := ecdsa.GenerateKey(curve, rand.Reader) // this generates a public & private key pairif err != nil {fmt.Println(err)os.Exit(1)}var pubkey ecdsa.PublicKeypubkey = privatekey.PublicKeyfmt.Println("Private Key :")fmt.Printf("%x \n", privatekey)fmt.Println("Public Key :")fmt.Printf("%x \n", pubkey)fmt.Println("GX GY:")



 



























fmt.Printf("%x \n", privatekey.Params().Gx)
fmt.Printf("%x \n", privatekey.Params().Gy)
fmt.Println("")
// Sign ecdsa style


var h hash.Hash
h = md5.New()
io.WriteString(h, "This is a message to be signed and verified by ECDSA!")
signhash := h.Sum(nil)


r, s, serr := ecdsa.Sign(rand.Reader, privatekey, signhash)
if serr != nil {
fmt.Println(err)
os.Exit(1)
}


// signature := r.Bytes()
// signature = append(signature, s.Bytes()...)
//
// fmt.Printf("Signature : %x\n", signature)fmt.Printf("%x\n", r)fmt.Printf("%x\n", s)// Verify






verifystatus := ecdsa.Verify(&pubkey, signhash, r, s)
fmt.Println(verifystatus) // should be true
}

3. Application of Ethereum

  Ethereum stores the private key in your account, and then encrypts it with your password. For each transaction, take out the private key to make a signature, and attach the signature value to the content of the transaction, namely r , the value of s, used to ensure the non-repudiation of the transaction





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325972726&siteId=291194637