dc4bc/airgapped/bls.go

136 lines
4.1 KiB
Go
Raw 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-08-26 07:12:24 -07:00
client "github.com/depools/dc4bc/client/types"
"github.com/depools/dc4bc/fsm/state_machines/signing_proposal_fsm"
"github.com/depools/dc4bc/fsm/types/requests"
"github.com/depools/dc4bc/fsm/types/responses"
2020-08-18 11:52:04 -07:00
)
2020-08-26 08:15:38 -07:00
func (am *AirgappedMachine) 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
}
func (am *AirgappedMachine) handleStateSigningAwaitPartialSigns(o *client.Operation) error {
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
}
func (am *AirgappedMachine) reconstructThresholdSignature(o *client.Operation) error {
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)
}
fmt.Println(reconstructedSignature)
return nil
}
2020-08-18 11:52:04 -07:00
func (am *AirgappedMachine) createPartialSign(msg []byte, dkgIdentifier string) ([]byte, error) {
blsKeyring, err := am.loadBLSKeyring(dkgIdentifier)
if err != nil {
return nil, fmt.Errorf("failed to load blsKeyring: %w", err)
}
2020-08-30 09:23:48 -07:00
return tbls.Sign(am.suite.(pairing.Suite), blsKeyring.Share, msg)
2020-08-18 11:52:04 -07:00
}
2020-08-28 06:31:31 -07:00
func (am *AirgappedMachine) 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.suite.(pairing.Suite), blsKeyring.PubPoly, msg, sigShares, t, n)
2020-08-18 11:52:04 -07:00
}
func (am *AirgappedMachine) verifySign(msg []byte, fullSignature []byte, dkgIdentifier string) error {
blsKeyring, err := am.loadBLSKeyring(dkgIdentifier)
if err != nil {
return fmt.Errorf("failed to load blsKeyring: %w", err)
}
2020-08-30 09:23:48 -07:00
return bls.Verify(am.suite.(pairing.Suite), blsKeyring.PubPoly.Commit(), msg, fullSignature)
2020-08-18 11:52:04 -07:00
}