public override void HandleMessage(int fromId, Msg msg) { if (Stage == 0) { ReceiveShareStage(msg); if (SharesReceived == EvalGate.InputCount) { StartGateEvaluation(); } } else if (Stage == 1) { Debug.Assert(msg.Type == MsgType.SubProtocolCompleted); var completedMsg = (SubProtocolCompletedMsg)msg; CollectGateEvaluation(completedMsg); SendShareStage(); } else if (Stage == 2) { // we loopback any shares that go to me in a different quorum. Debug.Assert(msg is SubProtocolCompletedMsg); SubProtocolCompletedMsg completedMsg = (SubProtocolCompletedMsg)msg; foreach (var loopback in OutputLoopbacksNeeded) { ulong counterpartEvalId = ProtocolIdGenerator.GateEvalIdentifier(loopback.Value.Gate.TopologicalRank); T loopbackVal = (T)completedMsg.Result[loopback.Key]; NetSimulator.Loopback(Me.Id, counterpartEvalId, new LoopbackMsg <T>(loopbackVal, loopback.Value.Port)); } IsCompleted = true; } }
private void SendShareStage() { List <Protocol> reshareProtocols = new List <Protocol>(); OutputLoopbacksNeeded = new Dictionary <ulong, InputGateAddress>(); for (int i = 0; i < EvalGate.OutputCount; i++) { // figure out where this share is going to InputGateAddress counterpartGateAddr; if (Circuit.OutputConnectionCounterparties.TryGetValue(EvalGate.GetLocalOutputAddress(i), out counterpartGateAddr)) { var counterpartGate = counterpartGateAddr.Gate; var counterpartQuorum = GateQuorumMapping[counterpartGate]; bool needReshare; bool needLoopback; if (counterpartQuorum == EvalQuorum) { needLoopback = true; needReshare = false; } else if (counterpartQuorum.HasMember(Me.Id)) { needLoopback = true; needReshare = true; } else { needLoopback = false; needReshare = true; } if (needLoopback && !needReshare) { // loop a message back to the other protocol ulong counterpartEvalId = ProtocolIdGenerator.GateEvalIdentifier(counterpartGate.TopologicalRank); NetSimulator.Loopback(Me.Id, counterpartEvalId, new LoopbackMsg <T>(OutputShares[i], counterpartGateAddr.Port)); } if (needReshare) { Protocol reshareProtocol = ProtocolFactory.GetResharingProtocol(Me, EvalQuorum, counterpartQuorum, OutputShares[i], counterpartGate.TopologicalRank, counterpartGateAddr.Port); reshareProtocols.Add(reshareProtocol); if (needLoopback) { OutputLoopbacksNeeded[reshareProtocol.ProtocolId] = counterpartGateAddr; } } } else { // this was a circuit output Result[EvalGate.GetLocalOutputAddress(i)] = OutputShares[i]; } } Stage = 2; if (reshareProtocols.Count > 0) { ExecuteSubProtocols(reshareProtocols); } else { IsCompleted = true; } }