dc4bc/airgapped/bls.go

155 lines
5.2 KiB
Go
Raw Permalink Normal View History

2020-08-18 11:52:04 -07:00
package airgapped
import (
2020-08-26 07:12:24 -07:00
"encoding/json"
2020-08-18 11:52:04 -07:00
"fmt"
2020-08-30 09:23:48 -07:00
"github.com/corestario/kyber/pairing"
"github.com/corestario/kyber/sign/bls"
"github.com/corestario/kyber/sign/tbls"
2020-11-05 01:43:54 -08:00
client "github.com/lidofinance/dc4bc/client/types"
"github.com/lidofinance/dc4bc/fsm/state_machines/signing_proposal_fsm"
"github.com/lidofinance/dc4bc/fsm/types/requests"
"github.com/lidofinance/dc4bc/fsm/types/responses"
2020-08-18 11:52:04 -07:00
)
2020-09-22 06:15:18 -07:00
// handleStateSigningAwaitConfirmations returns a confirmation of participation to create a threshold signature for a data
2020-10-05 08:00:54 -07:00
func (am *Machine) handleStateSigningAwaitConfirmations(o *client.Operation) error {
2020-08-26 07:12:24 -07:00
var (
payload responses.SigningProposalParticipantInvitationsResponse
err error
)
if err = json.Unmarshal(o.Payload, &payload); err != nil {
return fmt.Errorf("failed to unmarshal payload: %w", err)
}
2020-08-26 08:15:38 -07:00
participantID, err := am.getParticipantID(o.DKGIdentifier)
if err != nil {
return fmt.Errorf("failed to get paricipant id: %w", err)
}
req := requests.SigningProposalParticipantRequest{
SigningId: payload.SigningId,
ParticipantId: participantID,
CreatedAt: o.CreatedAt,
}
reqBz, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("failed to generate fsm request: %w", err)
}
o.Event = signing_proposal_fsm.EventConfirmSigningConfirmation
o.ResultMsgs = append(o.ResultMsgs, createMessage(*o, reqBz))
return nil
}
2020-09-22 06:15:18 -07:00
// handleStateSigningAwaitPartialSigns takes a data to sign as payload and returns a partial sign for the data to broadcast
2020-10-05 08:00:54 -07:00
func (am *Machine) handleStateSigningAwaitPartialSigns(o *client.Operation) error {
2020-08-26 08:15:38 -07:00
var (
payload responses.SigningPartialSignsParticipantInvitationsResponse
err error
)
if err = json.Unmarshal(o.Payload, &payload); err != nil {
return fmt.Errorf("failed to unmarshal payload: %w", err)
}
2020-08-26 07:12:24 -07:00
partialSign, err := am.createPartialSign(payload.SrcPayload, o.DKGIdentifier)
if err != nil {
return fmt.Errorf("failed to create partialSign for msg: %w", err)
}
participantID, err := am.getParticipantID(o.DKGIdentifier)
if err != nil {
return fmt.Errorf("failed to get paricipant id: %w", err)
}
2020-09-01 01:45:19 -07:00
req := requests.SigningProposalPartialSignRequest{
2020-08-26 08:15:38 -07:00
SigningId: payload.SigningId,
2020-08-26 07:12:24 -07:00
ParticipantId: participantID,
PartialSign: partialSign,
CreatedAt: o.CreatedAt,
}
reqBz, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("failed to generate fsm request: %w", err)
}
2020-09-01 01:45:19 -07:00
o.Event = signing_proposal_fsm.EventSigningPartialSignReceived
2020-08-26 07:12:24 -07:00
o.ResultMsgs = append(o.ResultMsgs, createMessage(*o, reqBz))
return nil
}
2020-09-22 06:15:18 -07:00
// reconstructThresholdSignature takes broadcasted partial signs from the previous step and reconstructs a full signature
2020-10-05 08:00:54 -07:00
func (am *Machine) reconstructThresholdSignature(o *client.Operation) error {
2020-08-26 07:12:24 -07:00
var (
payload responses.SigningProcessParticipantResponse
err error
)
if err = json.Unmarshal(o.Payload, &payload); err != nil {
return fmt.Errorf("failed to unmarshal payload: %w", err)
}
partialSignatures := make([][]byte, 0, len(payload.Participants))
for _, participant := range payload.Participants {
partialSignatures = append(partialSignatures, participant.PartialSign)
}
2020-08-28 06:31:31 -07:00
dkgInstance, ok := am.dkgInstances[o.DKGIdentifier]
if !ok {
return fmt.Errorf("dkg instance with identifier %s does not exist", o.DKGIdentifier)
}
reconstructedSignature, err := am.recoverFullSign(payload.SrcPayload, partialSignatures, dkgInstance.Threshold,
dkgInstance.N, o.DKGIdentifier)
2020-08-26 07:12:24 -07:00
if err != nil {
return fmt.Errorf("failed to reconsruct full signature for msg: %w", err)
}
response := client.ReconstructedSignature{
2020-11-02 05:17:01 -08:00
SigningID: payload.SigningId,
2020-11-02 02:56:02 -08:00
SrcPayload: payload.SrcPayload,
Signature: reconstructedSignature,
DKGRoundID: o.DKGIdentifier,
}
respBz, err := json.Marshal(response)
if err != nil {
return fmt.Errorf("failed to generate reconstructed signature response: %w", err)
}
o.Event = client.SignatureReconstructed
o.ResultMsgs = append(o.ResultMsgs, createMessage(*o, respBz))
2020-08-26 07:12:24 -07:00
return nil
}
2020-08-18 11:52:04 -07:00
2020-09-22 06:15:18 -07:00
// createPartialSign returns a partial sign of a given message
// with using of a private part of the reconstructed DKG key of a given DKG round
2020-10-05 08:00:54 -07:00
func (am *Machine) createPartialSign(msg []byte, dkgIdentifier string) ([]byte, error) {
2020-08-18 11:52:04 -07:00
blsKeyring, err := am.loadBLSKeyring(dkgIdentifier)
if err != nil {
return nil, fmt.Errorf("failed to load blsKeyring: %w", err)
}
return tbls.Sign(am.baseSuite.(pairing.Suite), blsKeyring.Share, msg)
2020-08-18 11:52:04 -07:00
}
2020-09-22 06:15:18 -07:00
// recoverFullSign recovers full threshold signature for a message
// with using of a reconstructed public DKG key of a given DKG round
2020-10-05 08:00:54 -07:00
func (am *Machine) recoverFullSign(msg []byte, sigShares [][]byte, t, n int, dkgIdentifier string) ([]byte, error) {
2020-08-18 11:52:04 -07:00
blsKeyring, err := am.loadBLSKeyring(dkgIdentifier)
if err != nil {
return nil, fmt.Errorf("failed to load blsKeyring: %w", err)
}
return tbls.Recover(am.baseSuite.(pairing.Suite), blsKeyring.PubPoly, msg, sigShares, t, n)
2020-08-18 11:52:04 -07:00
}
2020-09-22 06:15:18 -07:00
// verifySign verifies a signature of a message
2020-10-05 08:00:54 -07:00
func (am *Machine) VerifySign(msg []byte, fullSignature []byte, dkgIdentifier string) error {
2020-08-18 11:52:04 -07:00
blsKeyring, err := am.loadBLSKeyring(dkgIdentifier)
if err != nil {
return fmt.Errorf("failed to load blsKeyring: %w", err)
}
return bls.Verify(am.baseSuite.(pairing.Suite), blsKeyring.PubPoly.Commit(), msg, fullSignature)
2020-08-18 11:52:04 -07:00
}