Skip to content

Commit

Permalink
bls: support gen pub key
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign committed Sep 8, 2021
1 parent 3ce58b7 commit 6233ef1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
23 changes: 23 additions & 0 deletions bls/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

curve12381 "github.com/drand/kyber-bls12381"
"github.com/drand/kyber/sign/bls" // nolint:staticcheck
"github.com/filecoin-project/go-address"
)

// Sign creates a BLS signature of a message using a private key.
Expand Down Expand Up @@ -40,3 +41,25 @@ func Verify(pubkey, msg, sig []byte) bool {

return true
}

// GetPubKey returns the public key from the private key.
func GetPubKey(pk []byte) (address.Address, error) {
var pkrev [32]byte
for i := 0; i < 32; i++ {
pkrev[i] = pk[32-i-1]
}
scalar := curve12381.NewKyberScalar().SetBytes(pkrev[:])
pubPoint := curve12381.NewGroupG1().Point()
pubPoint.Mul(scalar, nil)

buf, err := pubPoint.MarshalBinary()
if err != nil {
return address.Address{}, fmt.Errorf("marshaling pub: %s", err)
}
addr, err := address.NewBLSAddress(buf)
if err != nil {
return address.Undef, fmt.Errorf("generating public key: %s", err)
}

return addr, nil
}
3 changes: 1 addition & 2 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ func PublicKey(lotusExportedPK string) (address.Address, error) {
case types.KTSecp256k1:
return secp256k1.GetPubKey(ki.PrivateKey)
default:
// TODO: support BLS.
return address.Undef, fmt.Errorf("signature type not supported")
return bls.GetPubKey(ki.PrivateKey)
}
}

Expand Down
8 changes: 7 additions & 1 deletion wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestSecp256k1(t *testing.T) {
t.Run("gen-pubkey", func(t *testing.T) {
addr, err := PublicKey(privateKeyHex)
require.NoError(t, err)
require.Equal(t, "f1fib3pv7jua2ockdugtz7viz3cyy6lkhh7rfx3sa", addr.String())
require.Equal(t, publicAddr, addr.String())
})
}

Expand All @@ -88,6 +88,12 @@ func TestBLSSign(t *testing.T) {
require.Equal(t, hexSignature, genHexSignature)
})

t.Run("gen-pubkey", func(t *testing.T) {
addr, err := PublicKey(privateKeyHex)
require.NoError(t, err)
require.Equal(t, publicAddr, addr.String())
})

t.Run("verify", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 6233ef1

Please sign in to comment.