Skip to content

Commit

Permalink
composite
Browse files Browse the repository at this point in the history
  • Loading branch information
tnasu committed Oct 13, 2021
1 parent c196924 commit 6d25ceb
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 244 deletions.
34 changes: 30 additions & 4 deletions crypto/composite/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ type PubKey struct {
VrfKey crypto.PubKey `json:"vrf"`
}

func PubKeyFromBytes(bz []byte) PubKey {
if len(bz) != bls.PubKeySize+ed25519.PubKeySize {
panic(fmt.Sprintf("Wrong PubKey bytes size: %d", len(bz)))
}
sign := bls.PubKey{}
copy(sign[:], bz[:bls.PubKeySize])
vrf := ed25519.PubKey(make([]byte, ed25519.PubKeySize))
copy(vrf, bz[bls.PubKeySize:])
return PubKey{SignKey: sign, VrfKey: vrf}
}

func (pk *PubKey) Identity() crypto.PubKey {
return pk.VrfKey
}
Expand All @@ -44,9 +55,9 @@ func (pk PubKey) Address() crypto.Address {
}

func (pk PubKey) Bytes() []byte {
msg := bytes.NewBuffer(pk.SignKey.Bytes())
msg.Write(pk.VrfKey.Bytes())
return msg.Bytes()
bz := bytes.NewBuffer(pk.SignKey.Bytes())
bz.Write(pk.VrfKey.Bytes())
return bz.Bytes()
}

func (pk PubKey) VerifySignature(msg []byte, sig []byte) bool {
Expand Down Expand Up @@ -80,12 +91,27 @@ func NewPrivKeyComposite(sign crypto.PrivKey, vrf crypto.PrivKey) *PrivKey {
return &PrivKey{SignKey: sign, VrfKey: vrf}
}

// PrivKeyFromBytes depends on PrivKey.Bytes
// See PrivKey.Bytes
func PrivKeyFromBytes(bz []byte) *PrivKey {
if len(bz) != bls.PrivKeySize+ed25519.PrivateKeySize {
panic(fmt.Sprintf("Wrong PrivKey bytes size: %d", len(bz)))
}
sign := bls.PrivKey{}
copy(sign[:], bz[:bls.PrivKeySize])
vrf := ed25519.PrivKey(make([]byte, ed25519.PrivateKeySize))
copy(vrf, bz[bls.PrivKeySize:])
return &PrivKey{SignKey: sign, VrfKey: vrf}
}

func (sk PrivKey) Identity() crypto.PrivKey {
return sk.VrfKey
}

func (sk PrivKey) Bytes() []byte {
return sk.Identity().Bytes()
bz := bytes.NewBuffer(sk.SignKey.Bytes())
bz.Write(sk.VrfKey.Bytes())
return bz.Bytes()
}

func (sk PrivKey) Sign(msg []byte) ([]byte, error) {
Expand Down
30 changes: 26 additions & 4 deletions crypto/composite/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,33 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"

tmjson "github.com/line/ostracon/libs/json"

"github.com/line/ostracon/crypto/bls"
"github.com/line/ostracon/crypto/composite"
"github.com/line/ostracon/crypto/ed25519"
)

func TestGenPrivKey(t *testing.T) {
sk := composite.GenPrivKey()
sign := sk.SignKey
vrf := sk.VrfKey
compositeKey := composite.NewPrivKeyComposite(sign, vrf)
assert.Equal(t, sk, compositeKey)
bz := compositeKey.Bytes()
assert.Equal(t, sk, composite.PrivKeyFromBytes(bz))
}

func TestPrivKeyComposite_Bytes(t *testing.T) {
sign := bls.GenPrivKey()
vrf := ed25519.GenPrivKey()
sk := composite.NewPrivKeyComposite(sign, vrf)
sk.Bytes()
compositeKey := composite.PrivKeyFromBytes(sk.Bytes())
assert.Equal(t, sign, compositeKey.SignKey)
assert.Equal(t, vrf, compositeKey.VrfKey)
assert.Equal(t, sk, compositeKey)
}

func TestPrivKeyComposite_Equals(t *testing.T) {
Expand Down Expand Up @@ -56,7 +71,11 @@ func TestPrivKeyComposite_PubKey(t *testing.T) {
sign := bls.GenPrivKey()
vrf := ed25519.GenPrivKey()
sk := composite.NewPrivKeyComposite(sign, vrf)
sk.PubKey()
pk := sk.PubKey()
compositeKey := composite.PubKeyFromBytes(pk.Bytes())
assert.Equal(t, sign.PubKey(), compositeKey.SignKey)
assert.Equal(t, vrf.PubKey(), compositeKey.VrfKey)
assert.Equal(t, pk, compositeKey)
}

func TestPrivKeyComposite_Sign(t *testing.T) {
Expand Down Expand Up @@ -105,7 +124,10 @@ func TestPubKeyComposite_Bytes(t *testing.T) {
vrf := ed25519.GenPrivKey()
sk := composite.NewPrivKeyComposite(sign, vrf)
pk := sk.PubKey().(composite.PubKey)
pk.Bytes()
compositeKey := composite.PubKeyFromBytes(pk.Bytes())
assert.Equal(t, sign.PubKey(), compositeKey.SignKey)
assert.Equal(t, vrf.PubKey(), compositeKey.VrfKey)
assert.Equal(t, pk, compositeKey)
}

func TestPubKeyComposite_Equals(t *testing.T) {
Expand Down Expand Up @@ -135,7 +157,7 @@ func TestPubKeyComposite_Identity(t *testing.T) {
vrf := ed25519.GenPrivKey()
sk := composite.NewPrivKeyComposite(sign, vrf)
pk := sk.PubKey().(composite.PubKey)
pk.Identity()
assert.Equal(t, vrf.PubKey(), pk.Identity())
}

func TestPubKeyComposite_VerifyBytes(t *testing.T) {
Expand Down
18 changes: 10 additions & 8 deletions crypto/encoding/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func init() {
json.RegisterType((*pc.PublicKey)(nil), "ostracon.crypto.PublicKey")
json.RegisterType((*pc.PublicKey_Ed25519)(nil), "ostracon.crypto.PublicKey_Ed25519")
json.RegisterType((*pc.PublicKey_Secp256K1)(nil), "ostracon.crypto.PublicKey_Secp256K1")
json.RegisterType((*pc.PublicKey_Composite)(nil), "ostracon.crypto.PublicKey_Composite")
json.RegisterType((*pc.PublicKey_Bls12)(nil), "ostracon.crypto.PublicKey_Bls12")
}

// PubKeyToProto takes crypto.PubKey and transforms it to a protobuf Pubkey
Expand Down Expand Up @@ -81,14 +83,6 @@ func PubKeyFromProto(k *pc.PublicKey) (crypto.PubKey, error) {
VrfKey: vrf,
}
return pk, nil
case *pc.PublicKey_Ed25519:
if len(k.Ed25519) != ed25519.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
len(k.Ed25519), ed25519.PubKeySize)
}
pk := make(ed25519.PubKey, ed25519.PubKeySize)
copy(pk, k.Ed25519)
return pk, nil
case *pc.PublicKey_Bls12:
if len(k.Bls12) != bls.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeyBls12. Got %d, expected %d",
Expand All @@ -97,6 +91,14 @@ func PubKeyFromProto(k *pc.PublicKey) (crypto.PubKey, error) {
pk := bls.PubKey{}
copy(pk[:], k.Bls12)
return pk, nil
case *pc.PublicKey_Ed25519:
if len(k.Ed25519) != ed25519.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
len(k.Ed25519), ed25519.PubKeySize)
}
pk := make(ed25519.PubKey, ed25519.PubKeySize)
copy(pk, k.Ed25519)
return pk, nil
case *pc.PublicKey_Secp256K1:
if len(k.Secp256K1) != secp256k1.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeySecp256k1. Got %d, expected %d",
Expand Down
Loading

0 comments on commit 6d25ceb

Please sign in to comment.