コード例 #1
0
        public override void HandleMessage(int fromId, Msg msg)
        {
            Debug.Assert(msg is SubProtocolCompletedMsg);

            SubProtocolCompletedMsg completedMsg = msg as SubProtocolCompletedMsg;

            switch (Stage)
            {
            case 0:
                BitwiseRand = (List <Share <BigZp> >)completedMsg.SingleResult;
                ExecuteSubProtocol(new BitCompositionProtocol(Me, Quorum, BitwiseRand, Prime));
                break;

            case 1:
                Rand = (Share <BigZp>)completedMsg.SingleResult;
                ExecuteSubProtocol(new ShareAdditionProtocol(Me, Quorum, Rand, Share));
                break;

            case 2:
                PaddedShare = (Share <BigZp>)completedMsg.SingleResult;
                ExecuteSubProtocol(new ReconstructionProtocol(Me, Quorum, PaddedShare));
                break;

            case 3:
                RevealedPadded = (BigZp)completedMsg.SingleResult;
                BigZp lowBit = new BigZp(Prime, RevealedPadded.Value.IsEven ? 0 : 1);
                ExecuteSubProtocol(new SharedBitXor(Me, Quorum, new Share <BigZp>(lowBit, true), BitwiseRand[0]));
                break;

            case 4:
                X = (Share <BigZp>)completedMsg.SingleResult;

                var bitwiseRevealedPadded = NumTheoryUtils.GetBitDecomposition(RevealedPadded.Value, Prime, BitwiseRand.Count);

                var bitwiseRevealedPaddedShares = new List <Share <BigZp> >();
                foreach (var bit in bitwiseRevealedPadded)
                {
                    bitwiseRevealedPaddedShares.Add(new Share <BigZp>(bit, true));
                }

                ExecuteSubProtocol(new BitwiseLessThanProtocol(Me, Quorum, bitwiseRevealedPaddedShares, BitwiseRand));
                break;

            case 5:
                Y = (Share <BigZp>)completedMsg.SingleResult;
                ExecuteSubProtocol(new SharedBitXor(Me, Quorum, X, Y));
                break;

            case 6:
                Result      = (Share <BigZp>)completedMsg.SingleResult;
                IsCompleted = true;
                break;
            }

            Stage++;
        }
コード例 #2
0
ファイル: RandomGeneration.cs プロジェクト: mahdiz/mpclib
        public override void HandleMessage(int fromId, Msg msg)
        {
            Debug.Assert(msg is SubProtocolCompletedMsg);

            SubProtocolCompletedMsg completedMsg = msg as SubProtocolCompletedMsg;

            switch (Stage)
            {
            case 0:
                Result = completedMsg.ResultList.Cast <Share <BigZp> >().ToList();
                if (Max.IsPowerOfTwo)
                {
                    // we automatically know the number we generated is in the range
                    IsCompleted = true;
                }
                else
                {
                    var maxBits       = NumTheoryUtils.GetBitDecomposition(Max, Prime);
                    var maxBitsShares = new List <Share <BigZp> >();
                    foreach (var bit in maxBits)
                    {
                        maxBitsShares.Add(new Share <BigZp>(bit, true));
                    }

                    ExecuteSubProtocol(new BitwiseLessThanProtocol(Me, Quorum, Result, maxBitsShares));
                    Stage++;
                }
                break;

            case 1:
                // Reveal (r < p)
                ExecuteSubProtocol(new ReconstructionProtocol(Me, Quorum, (Share <BigZp>)completedMsg.SingleResult));
                Stage++;
                break;

            case 2:
                // If (r < p), then we're done. Otherwise, we should retry since the generated random value is not in the field.
                if (((BigZp)completedMsg.SingleResult).Value == 1)
                {
                    IsCompleted = true;
                }
                else
                {
                    // try again :(
                    Result.Clear();
                    Start();
                }
                break;
            }
        }