Skip to content

Commit

Permalink
implement legacy soft signer
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Jun 20, 2023
1 parent 01f462a commit 839f66c
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 257 deletions.
86 changes: 43 additions & 43 deletions signer/cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,61 +66,61 @@ type CosignerSignResponse struct {
Signature []byte
}

type CosignerEphemeralSecretPart struct {
SourceID int
DestinationID int
SourceEphemeralSecretPublicKey []byte
EncryptedSharePart []byte
SourceSig []byte
}

func (secretPart *CosignerEphemeralSecretPart) toProto() *proto.EphemeralSecretPart {
return &proto.EphemeralSecretPart{
SourceID: int32(secretPart.SourceID),
DestinationID: int32(secretPart.DestinationID),
SourceEphemeralSecretPublicKey: secretPart.SourceEphemeralSecretPublicKey,
EncryptedSharePart: secretPart.EncryptedSharePart,
SourceSig: secretPart.SourceSig,
type CosignerNonce struct {
SourceID int
DestinationID int
SourcePubKey []byte
EncryptedSharePart []byte
SourceSig []byte
}

func (secretPart *CosignerNonce) toProto() *proto.Nonce {
return &proto.Nonce{
SourceID: int32(secretPart.SourceID),
DestinationID: int32(secretPart.DestinationID),
SourcePubKey: secretPart.SourcePubKey,
EncryptedSharePart: secretPart.EncryptedSharePart,
SourceSig: secretPart.SourceSig,
}
}

type CosignerEphemeralSecretParts []CosignerEphemeralSecretPart
type CosignerNonces []CosignerNonce

func (secretParts CosignerEphemeralSecretParts) toProto() (out []*proto.EphemeralSecretPart) {
func (secretParts CosignerNonces) toProto() (out []*proto.Nonce) {
for _, secretPart := range secretParts {
out = append(out, secretPart.toProto())
}
return
}

func CosignerEphemeralSecretPartFromProto(secretPart *proto.EphemeralSecretPart) CosignerEphemeralSecretPart {
return CosignerEphemeralSecretPart{
SourceID: int(secretPart.SourceID),
DestinationID: int(secretPart.DestinationID),
SourceEphemeralSecretPublicKey: secretPart.SourceEphemeralSecretPublicKey,
EncryptedSharePart: secretPart.EncryptedSharePart,
SourceSig: secretPart.SourceSig,
func CosignerNonceFromProto(secretPart *proto.Nonce) CosignerNonce {
return CosignerNonce{
SourceID: int(secretPart.SourceID),
DestinationID: int(secretPart.DestinationID),
SourcePubKey: secretPart.SourcePubKey,
EncryptedSharePart: secretPart.EncryptedSharePart,
SourceSig: secretPart.SourceSig,
}
}

func CosignerEphemeralSecretPartsFromProto(
secretParts []*proto.EphemeralSecretPart) (out []CosignerEphemeralSecretPart) {
func CosignerNoncesFromProto(
secretParts []*proto.Nonce) (out []CosignerNonce) {
for _, secretPart := range secretParts {
out = append(out, CosignerEphemeralSecretPartFromProto(secretPart))
out = append(out, CosignerNonceFromProto(secretPart))
}
return
}

type CosignerSetEphemeralSecretPartRequest struct {
ChainID string
SourceID int
SourceEphemeralSecretPublicKey []byte
EncryptedSharePart []byte
SourceSig []byte
Height int64
Round int64
Step int8
Timestamp time.Time
type CosignerSetNonceRequest struct {
ChainID string
SourceID int
SourcePubKey []byte
EncryptedSharePart []byte
SourceSig []byte
Height int64
Round int64
Step int8
Timestamp time.Time
}

type CosignerSignBlockRequest struct {
Expand All @@ -132,13 +132,13 @@ type CosignerSignBlockResponse struct {
Signature []byte
}

type CosignerEphemeralSecretPartsResponse struct {
EncryptedSecrets []CosignerEphemeralSecretPart
type CosignerNoncesResponse struct {
EncryptedSecrets []CosignerNonce
}

type CosignerSetEphemeralSecretPartsAndSignRequest struct {
type CosignerSetNoncesAndSignRequest struct {
ChainID string
EncryptedSecrets []CosignerEphemeralSecretPart
EncryptedSecrets []CosignerNonce
HRST HRSTKey
SignBytes []byte
}
Expand All @@ -159,8 +159,8 @@ type Cosigner interface {
VerifySignature(chainID string, payload, signature []byte) bool

// Get ephemeral secret part for all cosigner shards
GetEphemeralSecretParts(chainID string, hrst HRSTKey) (*CosignerEphemeralSecretPartsResponse, error)
GetNonces(chainID string, hrst HRSTKey) (*CosignerNoncesResponse, error)

// Sign the requested bytes
SetEphemeralSecretPartsAndSign(req CosignerSetEphemeralSecretPartsAndSignRequest) (*CosignerSignResponse, error)
SetNoncesAndSign(req CosignerSetNoncesAndSignRequest) (*CosignerSignResponse, error)
}
24 changes: 12 additions & 12 deletions signer/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func (rpc *GRPCServer) SignBlock(
}, nil
}

func (rpc *GRPCServer) SetEphemeralSecretPartsAndSign(
func (rpc *GRPCServer) SetNoncesAndSign(
_ context.Context,
req *proto.CosignerGRPCSetEphemeralSecretPartsAndSignRequest,
) (*proto.CosignerGRPCSetEphemeralSecretPartsAndSignResponse, error) {
res, err := rpc.cosigner.SetEphemeralSecretPartsAndSign(CosignerSetEphemeralSecretPartsAndSignRequest{
req *proto.CosignerGRPCSetNoncesAndSignRequest,
) (*proto.CosignerGRPCSetNoncesAndSignResponse, error) {
res, err := rpc.cosigner.SetNoncesAndSign(CosignerSetNoncesAndSignRequest{
ChainID: req.ChainID,
EncryptedSecrets: CosignerEphemeralSecretPartsFromProto(req.GetEncryptedSecrets()),
EncryptedSecrets: CosignerNoncesFromProto(req.GetEncryptedSecrets()),
HRST: HRSTKeyFromProto(req.GetHrst()),
SignBytes: req.GetSignBytes(),
})
Expand All @@ -66,25 +66,25 @@ func (rpc *GRPCServer) SetEphemeralSecretPartsAndSign(
"round", req.Hrst.Round,
"step", req.Hrst.Step,
)
return &proto.CosignerGRPCSetEphemeralSecretPartsAndSignResponse{
return &proto.CosignerGRPCSetNoncesAndSignResponse{
Timestamp: res.Timestamp.UnixNano(),
Signature: res.Signature,
}, nil
}

func (rpc *GRPCServer) GetEphemeralSecretParts(
func (rpc *GRPCServer) GetNonces(
_ context.Context,
req *proto.CosignerGRPCGetEphemeralSecretPartsRequest,
) (*proto.CosignerGRPCGetEphemeralSecretPartsResponse, error) {
res, err := rpc.cosigner.GetEphemeralSecretParts(
req *proto.CosignerGRPCGetNoncesRequest,
) (*proto.CosignerGRPCGetNoncesResponse, error) {
res, err := rpc.cosigner.GetNonces(
req.ChainID,
HRSTKeyFromProto(req.GetHrst()),
)
if err != nil {
return nil, err
}
return &proto.CosignerGRPCGetEphemeralSecretPartsResponse{
EncryptedSecrets: CosignerEphemeralSecretParts(res.EncryptedSecrets).toProto(),
return &proto.CosignerGRPCGetNoncesResponse{
EncryptedSecrets: CosignerNonces(res.EncryptedSecrets).toProto(),
}, nil
}

Expand Down
76 changes: 43 additions & 33 deletions signer/local_cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ type ChainState struct {
// CosignerMetadata holds the share and the ephermeral secret public key
// Moved from Local cosigner to threshold_ed25519
type CosignerMetadata struct {
Shares [][]byte
EphemeralSecretPublicKey []byte
Shares [][]byte
PubKey []byte
}

func (ccs *ChainState) combinedNonces(myID int, threshold uint8, hrst HRSTKey) ([]Nonce, error) {
Expand Down Expand Up @@ -114,7 +114,7 @@ type CosignerRSAPubKey struct {
PublicKey rsa.PublicKey
}

type CosignerGetEphemeralSecretPartRequest struct {
type CosignerGetNonceRequest struct {
ChainID string
ID int
Height int64
Expand Down Expand Up @@ -208,6 +208,16 @@ func (cosigner *LocalCosigner) GetPubKey(chainID string) (cometcrypto.PubKey, er
return cometcryptoed25519.PubKey(ccs.signer.PubKey()), nil
}

// CombineSignatures takes
func (cosigner *LocalCosigner) CombineSignatures(chainID string, signatures []PartialSignature) ([]byte, error) {
ccs, err := cosigner.getChainState(chainID)
if err != nil {
return nil, err
}

return ccs.signer.CombineSignatures(signatures)
}

// VerifySignature validates a signed payload against the public key.
// Implements Cosigner interface
func (cosigner *LocalCosigner) VerifySignature(chainID string, payload, signature []byte) bool {
Expand Down Expand Up @@ -294,7 +304,7 @@ func (cosigner *LocalCosigner) sign(req CosignerSignRequest) (CosignerSignRespon
return res, nil
}

func (cosigner *LocalCosigner) dealShares(req CosignerGetEphemeralSecretPartRequest) ([]Nonces, error) {
func (cosigner *LocalCosigner) dealShares(req CosignerGetNonceRequest) ([]Nonces, error) {
chainID := req.ChainID

ccs, err := cosigner.getChainState(chainID)
Expand Down Expand Up @@ -355,18 +365,18 @@ func reverseBytes(inBytes []byte) []byte {
return outBytes
}

func (cosigner *LocalCosigner) GetEphemeralSecretParts(
func (cosigner *LocalCosigner) GetNonces(
chainID string,
hrst HRSTKey,
) (*CosignerEphemeralSecretPartsResponse, error) {
) (*CosignerNoncesResponse, error) {
metricsTimeKeeper.SetPreviousLocalEphemeralShare(time.Now())

if err := cosigner.LoadSignStateIfNecessary(chainID); err != nil {
return nil, err
}

res := &CosignerEphemeralSecretPartsResponse{
EncryptedSecrets: make([]CosignerEphemeralSecretPart, len(cosigner.rsaPubKeys)-1),
res := &CosignerNoncesResponse{
EncryptedSecrets: make([]CosignerNonce, len(cosigner.rsaPubKeys)-1),
}

id := cosigner.GetID()
Expand All @@ -380,7 +390,7 @@ func (cosigner *LocalCosigner) GetEphemeralSecretParts(
pubKey := pubKey

eg.Go(func() error {
secretPart, err := cosigner.getEphemeralSecretPart(CosignerGetEphemeralSecretPartRequest{
secretPart, err := cosigner.getNonce(CosignerGetNonceRequest{
ChainID: chainID,
ID: pubKey.ID,
Height: hrst.Height,
Expand Down Expand Up @@ -425,7 +435,7 @@ func (cosigner *LocalCosigner) dealSharesIfNecessary(chainID string, hrst HRSTKe
return nonces, nil
}

newNonces, err := cosigner.dealShares(CosignerGetEphemeralSecretPartRequest{
newNonces, err := cosigner.dealShares(CosignerGetNonceRequest{
ChainID: chainID,
Height: hrst.Height,
Round: hrst.Round,
Expand All @@ -443,11 +453,11 @@ func (cosigner *LocalCosigner) dealSharesIfNecessary(chainID string, hrst HRSTKe

// Get the ephemeral secret part for an ephemeral share
// The ephemeral secret part is encrypted for the receiver
func (cosigner *LocalCosigner) getEphemeralSecretPart(
req CosignerGetEphemeralSecretPartRequest,
) (CosignerEphemeralSecretPart, error) {
func (cosigner *LocalCosigner) getNonce(
req CosignerGetNonceRequest,
) (CosignerNonce, error) {
chainID := req.ChainID
res := CosignerEphemeralSecretPart{}
res := CosignerNonce{}

hrst := HRSTKey{
Height: req.Height,
Expand Down Expand Up @@ -480,7 +490,7 @@ func (cosigner *LocalCosigner) getEphemeralSecretPart(
}

res.SourceID = id
res.SourceEphemeralSecretPublicKey = ourCosignerMeta.PubKey
res.SourcePubKey = ourCosignerMeta.PubKey
res.EncryptedSharePart = encrypted

// sign the response payload with our private key
Expand All @@ -506,7 +516,7 @@ func (cosigner *LocalCosigner) getEphemeralSecretPart(
}

// Store an ephemeral secret share part provided by another cosigner
func (cosigner *LocalCosigner) setEphemeralSecretPart(req CosignerSetEphemeralSecretPartRequest) error {
func (cosigner *LocalCosigner) setNonce(req CosignerSetNonceRequest) error {
chainID := req.ChainID

ccs, err := cosigner.getChainState(chainID)
Expand All @@ -519,10 +529,10 @@ func (cosigner *LocalCosigner) setEphemeralSecretPart(req CosignerSetEphemeralSe
return errors.New("SourceSig field is required")
}

digestMsg := CosignerEphemeralSecretPart{
SourceID: req.SourceID,
SourceEphemeralSecretPublicKey: req.SourceEphemeralSecretPublicKey,
EncryptedSharePart: req.EncryptedSharePart,
digestMsg := CosignerNonce{
SourceID: req.SourceID,
SourcePubKey: req.SourcePubKey,
EncryptedSharePart: req.EncryptedSharePart,
}

digestBytes, err := cometjson.Marshal(digestMsg)
Expand Down Expand Up @@ -576,12 +586,12 @@ func (cosigner *LocalCosigner) setEphemeralSecretPart(req CosignerSetEphemeralSe
nonces[req.SourceID-1].Shares = make([][]byte, len(cosigner.rsaPubKeys))
}
nonces[req.SourceID-1].Shares[cosigner.GetID()-1] = sharePart
nonces[req.SourceID-1].PubKey = req.SourceEphemeralSecretPublicKey
nonces[req.SourceID-1].PubKey = req.SourcePubKey
return nil
}

func (cosigner *LocalCosigner) SetEphemeralSecretPartsAndSign(
req CosignerSetEphemeralSecretPartsAndSignRequest) (*CosignerSignResponse, error) {
func (cosigner *LocalCosigner) SetNoncesAndSign(
req CosignerSetNoncesAndSignRequest) (*CosignerSignResponse, error) {
chainID := req.ChainID

if err := cosigner.LoadSignStateIfNecessary(chainID); err != nil {
Expand All @@ -593,16 +603,16 @@ func (cosigner *LocalCosigner) SetEphemeralSecretPartsAndSign(
for _, secretPart := range req.EncryptedSecrets {
secretPart := secretPart
eg.Go(func() error {
return cosigner.setEphemeralSecretPart(CosignerSetEphemeralSecretPartRequest{
ChainID: chainID,
SourceID: secretPart.SourceID,
SourceEphemeralSecretPublicKey: secretPart.SourceEphemeralSecretPublicKey,
EncryptedSharePart: secretPart.EncryptedSharePart,
SourceSig: secretPart.SourceSig,
Height: req.HRST.Height,
Round: req.HRST.Round,
Step: req.HRST.Step,
Timestamp: time.Unix(0, req.HRST.Timestamp),
return cosigner.setNonce(CosignerSetNonceRequest{
ChainID: chainID,
SourceID: secretPart.SourceID,
SourcePubKey: secretPart.SourcePubKey,
EncryptedSharePart: secretPart.EncryptedSharePart,
SourceSig: secretPart.SourceSig,
Height: req.HRST.Height,
Round: req.HRST.Round,
Step: req.HRST.Step,
Timestamp: time.Unix(0, req.HRST.Timestamp),
})
})
}
Expand Down
12 changes: 6 additions & 6 deletions signer/local_cosigner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ func TestLocalCosignerSign2of2(t *testing.T) {
Timestamp: now.UnixNano(),
}

ephemeralSharesFor2, err := cosigner1.GetEphemeralSecretParts(testChainID, hrst)
ephemeralSharesFor2, err := cosigner1.GetNonces(testChainID, hrst)
require.NoError(t, err)

publicKeys = append(publicKeys, ephemeralSharesFor2.EncryptedSecrets[0].SourceEphemeralSecretPublicKey)
publicKeys = append(publicKeys, ephemeralSharesFor2.EncryptedSecrets[0].SourcePubKey)

ephemeralSharesFor1, err := cosigner2.GetEphemeralSecretParts(testChainID, hrst)
ephemeralSharesFor1, err := cosigner2.GetNonces(testChainID, hrst)
require.NoError(t, err)

t.Logf("Shares from 2: %d", len(ephemeralSharesFor1.EncryptedSecrets))

publicKeys = append(publicKeys, ephemeralSharesFor1.EncryptedSecrets[0].SourceEphemeralSecretPublicKey)
publicKeys = append(publicKeys, ephemeralSharesFor1.EncryptedSecrets[0].SourcePubKey)

t.Logf("public keys: %x", publicKeys)
// pack a vote into sign bytes
Expand All @@ -189,15 +189,15 @@ func TestLocalCosignerSign2of2(t *testing.T) {

signBytes := comet.VoteSignBytes("chain-id", &vote)

sigRes1, err := cosigner1.SetEphemeralSecretPartsAndSign(CosignerSetEphemeralSecretPartsAndSignRequest{
sigRes1, err := cosigner1.SetNoncesAndSign(CosignerSetNoncesAndSignRequest{
ChainID: testChainID,
EncryptedSecrets: ephemeralSharesFor1.EncryptedSecrets,
HRST: hrst,
SignBytes: signBytes,
})
require.NoError(t, err)

sigRes2, err := cosigner2.SetEphemeralSecretPartsAndSign(CosignerSetEphemeralSecretPartsAndSignRequest{
sigRes2, err := cosigner2.SetNoncesAndSign(CosignerSetNoncesAndSignRequest{
ChainID: testChainID,
EncryptedSecrets: ephemeralSharesFor2.EncryptedSecrets,
HRST: hrst,
Expand Down
Loading

0 comments on commit 839f66c

Please sign in to comment.