-
Notifications
You must be signed in to change notification settings - Fork 13
/
crypto.go
81 lines (68 loc) · 2.19 KB
/
crypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Package crypto provides an implementation of the MirModule module.
// It supports RSA and ECDSA signatures.
package crypto
import (
"fmt"
"github.com/filecoin-project/mir/pkg/events"
"github.com/filecoin-project/mir/pkg/modules"
"github.com/filecoin-project/mir/pkg/pb/eventpb"
t "github.com/filecoin-project/mir/pkg/types"
)
type MirModule struct {
crypto Crypto
}
func New(crypto Crypto) *MirModule {
return &MirModule{crypto: crypto}
}
func (c *MirModule) ApplyEvents(eventsIn *events.EventList) (*events.EventList, error) {
return modules.ApplyEventsConcurrently(eventsIn, c.ApplyEvent)
}
func (c *MirModule) ApplyEvent(event *eventpb.Event) (*events.EventList, error) {
switch e := event.Type.(type) {
case *eventpb.Event_Init:
// no actions on init
return events.EmptyList(), nil
case *eventpb.Event_SignRequest:
// Compute a signature over the provided data and produce a SignResult event.
signature, err := c.crypto.Sign(e.SignRequest.Data)
if err != nil {
return nil, err
}
return events.ListOf(
events.SignResult(t.ModuleID(e.SignRequest.Origin.Module), signature, e.SignRequest.Origin),
), nil
case *eventpb.Event_VerifyNodeSigs:
// Verify a batch of node signatures
// Convenience variables
verifyEvent := e.VerifyNodeSigs
results := make([]bool, len(verifyEvent.Data))
errors := make([]string, len(verifyEvent.Data))
allOK := true
// Verify each signature.
for i, data := range verifyEvent.Data {
err := c.crypto.Verify(data.Data, verifyEvent.Signatures[i], t.NodeID(verifyEvent.NodeIds[i]))
if err == nil {
results[i] = true
errors[i] = ""
} else {
results[i] = false
errors[i] = err.Error()
allOK = false
}
}
// Return result event
return events.ListOf(events.NodeSigsVerified(
t.ModuleID(verifyEvent.Origin.Module),
results,
errors,
t.NodeIDSlice(verifyEvent.NodeIds),
verifyEvent.Origin,
allOK,
)), nil
default:
// Complain about all other incoming event types.
return nil, fmt.Errorf("unexpected type of MirModule event: %T", event.Type)
}
}
// The ImplementsModule method only serves the purpose of indicating that this is a Module and must not be called.
func (c *MirModule) ImplementsModule() {}