我在我的 C++ 程序中编写了 ECDSA 函数。当我测试我的签名时,它在我的 C++ ECDSA 验证函数中工作正常,但不是每个测试都通过了 GO 语言。
因此,我尝试将我的公钥(从我的 C++ 中)作为缓冲区的十六进制字符串(33 字节)导出到 GO 语言。然后,我使用ellipitc.UnmarshalCompressed在 GO 程序上检索我的公钥。后来,我发现使用标量乘法从密钥生成公钥时,Unmarshal 公钥具有不同的 Y 坐标。我把代码放在下面。
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"math/big"
"os"
)
func main() {
/* UnmarshalCompressed public key from C++ buffer (hex string) */
publicKeyBufferFromCplusplus, err := hex.DecodeString("02d36b0e521ca9a28cd6f2ddc56dc0973215702f6f67ed0670b9bc9a98c28d473b")
if err != nil {
fmt.Println("Unable to convert hex to byte. ", err)
}
pk := new(ecdsa.PublicKey)
pk.Curve = elliptic.P256()
pk.X, pk.Y = elliptic.UnmarshalCompressed(elliptic.P256(), publicKeyBufferFromCplusplus[:])
/* Generate the key pair in GO, using the private key (as decimal) from C++ */
expect_sk := new(ecdsa.PrivateKey)
expect_sk.D, _ = new(big.Int).SetString("50228957095953179898827503463423289296009712707225507368245266147079499081684", 10)
expect_sk.PublicKey.Curve = elliptic.P256()
expect_sk.PublicKey.X, expect_sk.PublicKey.Y = expect_sk.PublicKey.Curve.ScalarBaseMult(expect_sk.D.Bytes())
expect_pk := expect_sk.PublicKey
/* compare the two public keys, the X coordinate is the same, but Y is different */
fmt.Printf("pk_x:\t\t%d\n", pk.X)
fmt.Printf("expect pk_x:\t%d\n\n", expect_pk.X)
fmt.Printf("pk_y:\t\t%d\n", pk.Y)
fmt.Printf("expect pk_y:\t%d\n", expect_pk.Y)
}
这是终端的结果
pk_x: 95627162525183504786576659676808415919520991299985517290103803735976207796027
expect pk_x: 95627162525183504786576659676808415919520991299985517290103803735976207796027
pk_y: 106312815215663533204607583749797836088594130128596587441436180287153537381066
expect pk_y: 9479273994692715558089863199609737441492013286693726754097451021713560472885
请注意,不同之处在于 Y 坐标,而 X 坐标是相同的。
翻过高山走不出你
相关分类