예제 #1
0
    List <PartyBattler> TestAllPartyBattles(out short[,,] winLossCounts)
    {
        int speciesCount = species.species.Count;
        int partyCount   = speciesCount * (speciesCount - 1) * (speciesCount - 2);

        _allParties = new PartyIds[partyCount];

        // Naive loop to build all possible parties. We can do something fancier,
        // but this is simple and clear and fast enough for a few thousand cases.
        int tallied = 0;

        for (int first = 0; first < speciesCount; first++)
        {
            for (int second = 0; second < speciesCount; second++)
            {
                if (second == first)
                {
                    continue;
                }
                for (int third = 0; third < speciesCount; third++)
                {
                    if (third == first || third == second)
                    {
                        continue;
                    }
                    _allParties[tallied++] = new PartyIds(first, second, third);
                }
            }
        }

        winLossCounts = new short[partyCount, partyCount, 2];

        var battlers = new List <PartyBattler>(threadCount);

        int   partiesCovered   = 0;
        float partiesPerThread = partyCount / (float)threadCount;

        for (int i = 0; i < threadCount; i++)
        {
            int next = Mathf.RoundToInt(partiesPerThread * (i + 1));
            battlers.Add(new PartyBattler(
                             _allParties,
                             species.species,
                             winLossCounts,
                             partiesCovered,
                             next));
            partiesCovered = next;
        }

        UnityEngine.Assertions.Assert.AreEqual(partyCount, partiesCovered, "Bad loop math! You didn't cover all the parties!");

        return(battlers);
    }
예제 #2
0
        private void SetupCandidateList()
        {
            int n = PartyIds.Count();

            var sampleList = randUtils.PickRandomElements(
                PartyIds.ToList(), (int)(C * Math.Sqrt(n) * Math.Log(n, 2)));

            int candStr = AlmostEverywhere();

            Broadcast(sampleList, new QsMessage <int>()
            {
                Data = candStr
            });

            throw new NotImplementedException();
        }
예제 #3
0
        public void SetupRandGenStep()
        {
            Debug.Assert(PartyIds.Count > TRUSTED_PARTY_COUNT);

            RandGenQuorum = new Quorum(NextQuorumNumber++, PartyIds.Take(5).ToArray());
            if (RandGenQuorum.HasMember(Me.Id))
            {
                BigZp myRandom = new BigZp(Prime, Me.SafeRandGen.Next(Prime));
                ExecuteSubProtocol(new RandomGenProtocol(Me, RandGenQuorum, myRandom, Prime));
            }
            else
            {
                // receive the rand broadcast
                ExecuteSubProtocol(new MajorityFilteringProtocol <BigZp>(Me, PartyIds, RandGenQuorum.Members.ToList(), ProtocolIdGenerator.GenericIdentifier(1)));
                Stage = 2;
            }
        }
예제 #4
0
        public void SetupInputDistribution()
        {
            int         myPosition      = Quorum.GetPositionOf(PartyIds, Me.Id);
            GateAddress myInputGateAddr = SortNetwork.FirstGateForWire[myPosition];
            Quorum      myInputQuorum   = GateQuorumMapping[myInputGateAddr.Gate];

            Protocol sortValueDistribution = new SharingProtocol(Me, Me.Id, myInputQuorum, SortValue, Prime,
                                                                 ProtocolIdGenerator.GateInputSharingIdentifier(myInputGateAddr.Gate.TopologicalRank, 2 * myInputGateAddr.Port));

            Protocol secretDistribution = new SharingProtocol(Me, Me.Id, myInputQuorum, Secret, Prime,
                                                              ProtocolIdGenerator.GateInputSharingIdentifier(myInputGateAddr.Gate.TopologicalRank, 2 * myInputGateAddr.Port + 1));

            List <Protocol> inputProtocols = new List <Protocol>();

            inputProtocols.Add(sortValueDistribution);
            inputProtocols.Add(secretDistribution);

            InputProtocolMapping = new Dictionary <InputGateAddress, Tuple <ulong, ulong> >();

            for (int i = 0; i < PartyIds.Count; i++)
            {
                var gateAddr = SortNetwork.FirstGateForWire[i];
                foreach (Quorum q in MyQuorums)
                {
                    if (GateQuorumMapping[gateAddr.Gate] == q)
                    {
                        if (i == myPosition)
                        {
                            InputProtocolMapping[gateAddr] = new Tuple <ulong, ulong>(sortValueDistribution.ProtocolId, secretDistribution.ProtocolId);
                        }
                        else
                        {
                            ulong sortRecvId  = ProtocolIdGenerator.GateInputSharingIdentifier(gateAddr.Gate.TopologicalRank, 2 * gateAddr.Port);
                            ulong shareRecvId = ProtocolIdGenerator.GateInputSharingIdentifier(gateAddr.Gate.TopologicalRank, 2 * gateAddr.Port + 1);
                            InputProtocolMapping[gateAddr] = new Tuple <ulong, ulong>(sortRecvId, shareRecvId);
                            // I need to receive for this gate
                            inputProtocols.Add(new SharingProtocol(Me, PartyIds.ElementAt(i), q, null, Prime, sortRecvId));
                            inputProtocols.Add(new SharingProtocol(Me, PartyIds.ElementAt(i), q, null, Prime, shareRecvId));
                        }
                    }
                }
            }

            ExecuteSubProtocols(inputProtocols);
        }
예제 #5
0
        public override void HandleMessage(int fromId, Msg msg)
        {
            Debug.Assert(msg.Type == MsgType.SubProtocolCompleted);

            var completedMsg = (SubProtocolCompletedMsg)msg;

            switch (Stage)
            {
            case 0:
                // reconstruct the rand we just created
                ExecuteSubProtocol(new ReconstructionProtocol(Me, RandGenQuorum, (Share <BigZp>)completedMsg.SingleResult));
                break;

            case 1:
                ProtocolRandom = (BigZp)completedMsg.SingleResult;
                ExecuteSubProtocol(new MajorityFilteringProtocol <BigZp>(Me, PartyIds, PartyIds.Skip(TRUSTED_PARTY_COUNT).ToArray(),
                                                                         ProtocolRandom, ProtocolIdGenerator.GenericIdentifier(1)));
                break;

            case 2:
                if (!RandGenQuorum.HasMember(Me.Id))
                {
                    ProtocolRandom = (BigZp)completedMsg.SingleResult;
                }

                GenerateQuorums();
                GenerateGateQuorumAssignment();

                SetupInputDistribution();
                break;

            case 3:
                UnpackCircuitInputs(completedMsg.Result);
                SetupQuorumExecutions();
                break;

            case 4:
                UnpackCircuitResults((completedMsg.SingleResult as SubProtocolCompletedMsg).Result);
                SetupReconstruction();
                break;

            case 5:
                UnpackReconstruction(completedMsg.Result);
                SetupResultBroadcast();
                break;

            case 6:
                CollectResults(completedMsg.Result);
                IsCompleted = true;
                break;
            }
            Stage++;
        }