Home >Backend Development >Golang >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:
<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!