supranational / blst

Multilingual BLS12-381 signature library
Apache License 2.0
454 stars 171 forks source link

The same private key but different public key results #205

Closed vincentiss closed 4 months ago

vincentiss commented 4 months ago

Dear all,

Below is my test code. I used this library.

e2types "github.com/wealdtech/go-eth2-types/v2"
blst "github.com/supranational/blst/bindings/go"
        skbytes, _ := hex.DecodeString("6c92261bccf2511762f89e3ae7698ec74c365dc3ed1671d8ba839e80a3a45278")
    sk := blst.KeyGen(skbytes)
    pub := new(PublicKey).From(sk)
    fmt.Println(hex.EncodeToString(pub.Compress()))

    e2types.InitBLS()
    e2sk, _ := e2types.BLSPrivateKeyFromBytes(skbytes)
    fmt.Println(hex.EncodeToString(e2sk.PublicKey().Marshal()))

Print results is

b52d4cc94a63acbb29f370827c865a0d7a7a06f1c6513a02a13e6213be5d2204871e65655e3b510d506b4ac1573d732a

b35f5a41f73748a3ecc313028743a32b41956d7024f7a78cddf5a216f0b4db96b49a3159e127a003fb418a8ed75d991e

Why are the results different? And the correct result is "b35f5a41f73748a3ecc313028743a32b41956d7024f7a78cddf5a216f0b4db96b49a3159e127a003fb418a8ed75d991e".

Did I use it wrong?

Thank you very much.

dot-asm commented 4 months ago

Do e2types use the same key derivation algorithm? blst.KeyGen() implements one specified in "BLS Signatures" spec, in accordance to which it even accepts an optional parameter referred to as key_info. In other words if e2types uses the same algorithm, you'd be required to pass the same key_info parameter. And if it doesn't use the same algorithm, then blst.KeyGen() is simply not the way to go. What is right is more likely for you to figure out.

vincentiss commented 4 months ago

Use this way new(blst.SecretKey).FromBEndian(sk) will be same with https://github.com/herumi/bls-eth-go-binary