Home >Backend Development >Golang >Why Does My Go ed25519 Public Key Differ From the Expected BEP Data?

Why Does My Go ed25519 Public Key Differ From the Expected BEP Data?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-31 07:23:291035browse

Why Does My Go ed25519 Public Key Differ From the Expected BEP Data?

Generating Public Key Matching BEP for ed25519 Key

Problem:
Using the Go crypto/ed25519 package, the generated public key differs from the expected one provided in a test case using the BEP data.

Cause:
Differing private key formats exist for ed25519. The BEP data uses a 64-byte hashed private key, while the Go package uses a 32-byte hashed private key (prefixed by the 32-byte seed).

Solution:
While it's not possible to convert the BEP private keys to the Go format, a modified version of the Go library can be created to handle the hashed private keys used in the BEP.

Code Modification:

The modified code includes the following functions:

  1. getPublicKey: Generates the public key from a 64-byte hashed private key.
  2. sign: Computes the signature using the hashed private key, public key, and message.
<code class="go">func getPublicKey(privateKey []byte) []byte {
    // ... (extraction of 32-byte hash from 64-byte hashed private key and publicKey generation) ...
}

func sign(privateKey, publicKey, message []byte) []byte {
    // ... (extraction of 32-byte hash from 64-byte hashed private key and signature computation) ...
}</code>

Usage:

With this modified code, the test case can be run, demonstrating that the generated public key and signature match the expected BEP data:

<code class="go">// Using the modified code...

fmt.Printf("Calculated key: %x\n", publicKey)
fmt.Printf("Expected key:   %s\n", expectedPublicKey)
keyMatches := expectedPublicKey == hex.EncodeToString(publicKey)
fmt.Printf("Public key matches expected: %v\n", keyMatches)

// Similarly, check for signature match</code>

The above is the detailed content of Why Does My Go ed25519 Public Key Differ From the Expected BEP Data?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn