/
pb.go
88 lines (72 loc) · 2.32 KB
/
pb.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
82
83
84
85
86
87
88
package party //provable broadcast
import (
"bytes"
"context"
"log"
"github.com/DyCAPSTeam/DyCAPS/pkg/core"
"github.com/DyCAPSTeam/DyCAPS/pkg/protobuf"
kyberbls "github.com/drand/kyber-bls12381"
"github.com/drand/kyber/sign/tbls"
"golang.org/x/crypto/sha3"
)
//Sender is run by the sender of a instance of provable broadcast
func Sender(ctx context.Context, p *HonestParty, ID []byte, value []byte, validation []byte) ([]byte, bool) {
tblsScheme := tbls.NewThresholdSchemeOnG1(kyberbls.NewBLS12381Suite())
valueMessage := core.Encapsulation("Value", ID, p.PID, &protobuf.Value{
Value: value,
Validation: validation,
})
p.Broadcast(valueMessage)
sigs := [][]byte{}
h := sha3.Sum512(value)
var buf bytes.Buffer
buf.Write([]byte("Echo"))
buf.Write(ID)
buf.Write(h[:])
sm := buf.Bytes()
for {
select {
case <-ctx.Done():
return nil, false
case m := <-p.GetMessage("Echo", ID):
payload := core.Decapsulation("Echo", m).(*protobuf.Echo)
err := tblsScheme.VerifyPartial(p.SigPK, sm, payload.Sigshare) //verifyshare("Echo"||ID||h)
if err == nil {
sigs = append(sigs, payload.Sigshare)
if len(sigs) > int(2*p.F) {
signature, _ := tblsScheme.Recover(p.SigPK, sm, sigs, int(2*p.F+1), int(p.N))
return signature, true
}
}
}
}
}
//Receiver is run by the receiver of a instance of provable broadcast
func Receiver(ctx context.Context, p *HonestParty, sender uint32, ID []byte, validator func(*HonestParty, []byte, uint32, []byte, []byte) error) ([]byte, []byte, bool) {
select {
case <-ctx.Done():
return nil, nil, false
case m := <-p.GetMessage("Value", ID):
//TODO:check sender == m.Sender
payload := (core.Decapsulation("Value", m)).(*protobuf.Value)
if validator != nil {
err2 := validator(p, ID, sender, payload.Value, payload.Validation)
if err2 != nil {
log.Fatalln(err2)
return nil, nil, false //sender is dishonest
}
}
h := sha3.Sum512(payload.Value)
var buf bytes.Buffer
buf.Write([]byte("Echo"))
buf.Write(ID)
buf.Write(h[:])
sm := buf.Bytes()
sigShare, _ := tbls.NewThresholdSchemeOnG1(kyberbls.NewBLS12381Suite()).Sign(p.SigSK, sm) //sign("Echo"||ID||h)
echoMessage := core.Encapsulation("Echo", ID, p.PID, &protobuf.Echo{
Sigshare: sigShare,
})
p.Send(echoMessage, sender)
return payload.Value, payload.Validation, true
}
}