如何从 secp256k1 中提取未压缩的公钥? [英] How to extract uncompressed public key from secp256k1?

查看:115
本文介绍了如何从 secp256k1 中提取未压缩的公钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题经过大量编辑以缩小问题的焦点

我正在尝试从 secp256k1::key::PublicKey 结构中提取未压缩的公钥.

I'm trying to extract an uncompressed public key from the secp256k1::key::PublicKey struct.

根据我使用的打印格式,我会获得压缩或未压缩的密钥.

Depending on which print formatting I use I either get the compressed or uncompressed key.

正如这个问题所观察到的,解压后的key是十六进制(或字节)反转的(可以看到压缩版的反转是解压版的开始).

As observed by this question, the uncompressed key is hex- (or byte-)reversed (you can see that the reverse of the compressed version is the start of the uncompressed version).

我不知道如何从结构中提取 64 字节(反向)的键来操作它.如果我尝试在公钥上使用 .serialize() 方法,它将返回压缩密钥的 33 字节切片,而不是公钥.

I don't know how to extract the 64 byte (reversed) key from the struct in order to manipulate it. If I try to use the .serialize() method on the public key it returns the 33-byte slice of the compressed key, not the public key.

有谁知道如何从结构中提取和操作 64 字节的密钥?

Does anyone know how to extract and manipulate the 64-byte key from the struct?

以下代码片段:

//Calculate BIP32 Extended Public Key and Public Key
let ex_pub_key = ExtendedPubKey::from_private(&secp, &ex_priv_key);
    println!("Compressed Public Key: {}", ex_pub_key.public_key.key);
    //returns the 33-byte: 
    //03a593ae00ed6b402c801d2a2edca9f5c057f2be7327e3d81c5747df058e169cd5
    
    println!("Uncompressed Public Key: {:?}", ex_pub_key.public_key.key);  
    //returns the 64-byte struct:
 //PublicKey(d59c168e05df47571cd8e32773bef257c0f5a9dc2e2a1d802c406bed00ae93a57d95dc0fa554e33bda9c7cd87e4b8e8b788b759c0bb1160bb803240468d0e559)
    
//Serializing the key returns the compressed version of the key
let serialized_key = public_key.serialize();
    println!("Serialized Key: {:?}", serialized_key);
    //returns the 33-byte slice:
    //[3, 165, 147, 174, 0, 237, 107, 64, 44, 128, 29, 42, 46, 220, 169, 245, 192, 87, 242, 190, 115, 39, 227, 216, 28, 87, 71, 223, 5, 142, 22, 156, 213]

推荐答案

经过大量研究,我发现有一种简单的方法可以通过使用 serialize_uncompressed() 上的方法来提取未压缩的公钥公钥,它是同一个 secp256k1 crate 的一部分.

After much research I have discovered that there is a simple way to extract the uncompressed public key by using the method serialize_uncompressed() on the public key, which is part of the same secp256k1 crate.

这提供了一个 65 字节的输出,其中第一个字节应该被删除以创建正确的 64 字节公钥.

This provides a 65-byte output, where the first byte should be dropped to create the correct 64-byte public key.

这篇关于如何从 secp256k1 中提取未压缩的公钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆