forked from ldsec/CS523-Project1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
104 lines (82 loc) · 2.63 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"fmt"
"github.com/ldsec/lattigo/ring"
"sync"
"time"
)
func main() {
var circuitID int
var testCircuit *TestCircuit
var centralized bool
flag.IntVar(&circuitID, "id", 1, fmt.Sprintf("ID between 1 and %d of the template circuit", len(TestCircuits)))
flag.BoolVar(¢ralized, "c", false, "Use a centralized generation of beaver triplets")
flag.Parse()
if circuitID <= 0 || circuitID > len(TestCircuits) {
panic(fmt.Sprintf("Invalid argument: ID must be between 1 and %d", len(TestCircuits)))
}
testCircuit = TestCircuits[circuitID-1]
beaverTriplets := make(map[PartyID]map[WireID]BeaverTriplet)
for peerID := range testCircuit.Peers {
beaverTriplets[peerID] = make(map[WireID]BeaverTriplet)
}
if centralized {
for _, op := range testCircuit.Circuit {
if triplets := op.BeaverTriplet(len(testCircuit.Peers)); triplets != nil {
for id, triplet := range triplets {
beaverTriplets[PartyID(id)][op.Output()] = triplet
}
}
}
}
wg := new(sync.WaitGroup)
wg.Add(len(testCircuit.Peers))
for partyID := range testCircuit.Peers {
go func(id PartyID) {
defer wg.Done()
partyInput := testCircuit.Inputs[id][GateID(id)]
// Create a local party
lp, err := NewLocalParty(id, testCircuit.Peers)
check(err)
// Create the network for the circuit
network, err := NewTCPNetwork(lp)
check(err)
// Connect the circuit network
err = network.Connect(lp)
check(err)
<-time.After(time.Second) // Leave time for others to connect
lp.BindNetwork(network)
if !centralized {
beaverProtocol := lp.NewBeaverProtocol(Params)
ComputeBeaverTripletHE(beaverProtocol, beaverTriplets, testCircuit.Circuit)
}
// Create a new circuit evaluation protocol
protocol := lp.NewProtocol(partyInput, testCircuit.Circuit, beaverTriplets[id])
// Evaluate the circuit
protocol.Run()
// Print output
fmt.Println(fmt.Sprintf("Peer %d ended computation with output %d.", protocol.ID, protocol.Output))
}(partyID)
}
wg.Wait()
}
// Use Beaver triplet generation protocol to generate our triplets
func ComputeBeaverTripletHE(beaverProtocol *BeaverProtocol, beaverTriplets map[PartyID]map[WireID]BeaverTriplet, circuit Circuit) {
var currIndex uint64 = 0
var triplet Triplets
for _, op := range circuit {
if op.IsMult() {
if currIndex%(1<<Params.LogN) == 0 {
beaverProtocol.Run()
triplet = beaverProtocol.BeaverTriplets
currIndex = 0
}
beaverTriplets[beaverProtocol.ID][op.Output()] = BeaverTriplet{
a: ring.NewUint(triplet.ai[currIndex]),
b: ring.NewUint(triplet.bi[currIndex]),
c: ring.NewUint(triplet.ci[currIndex]),
}
}
}
}