예제 #1
0
        public override void ReceiveEmail(long fromPseudonym, object message, MessageType type)
        {
            if (type == MessageType.JoinRequest)
            {
                // Join the protocol.
                var distCount = distPseudonyms.Count;
                if (distCount == 1)
                {
                    SendEmail(distPseudonyms[0],
                              new BridgeJoinMessage(Pseudonym, new Zp(Simulator.Prime, Id)),
                              MessageType.BridgeJoin);
                }
                else
                {
                    // Secret share my id among all distributors
                    var shares = ShamirSharing.Share(new Zp(Simulator.Prime, Id),
                                                     distCount, Simulator.PolynomialDegree);

                    Debug.Assert(shares.Count == distCount);
                    Debug.Assert(Id == ShamirSharing.Reconstruct(shares,
                                                                 Simulator.PolynomialDegree, Simulator.Prime).Value,
                                 "The secret is not reconstructible! There is probably an overflow in polynomial evaluation of the sharing phase in Shamir sharing.");

                    for (int i = 0; i < distCount; i++)
                    {
                        SendEmail(distPseudonyms[i], new BridgeJoinMessage(Pseudonym, shares[i]), MessageType.BridgeJoin);
                    }
                }
            }
            else
            {
                throw new Exception("Invalid email received.");
            }
        }
예제 #2
0
        /// <summary>
        /// Implementation according to GRR.
        /// </summary>
        public virtual void RunReductionRandomization(Zp oldSecret)
        {
            // randomize the coeficients
            // generate a t degree polynomial, hi(x),
            // with a free coef that equals 'ab' and create share for users from it.
            var sharesValues = ShamirSharing.Share(oldSecret, NumParties, PolynomialDeg);
            var shareMsgs    = new List <ShareMsg <Zp> >();

            foreach (var shareValue in sharesValues)
            {
                shareMsgs.Add(new ShareMsg <Zp>(new Share <Zp>(shareValue), Stage.RandomizationReceive));
            }

            // send to the j-th user hi(j) and receive from every other k player hk(i)
            Send(shareMsgs);

            OnReceive((int)Stage.RandomizationReceive,
                      delegate(List <Msg> shares)
            {
                var vanderFirstRow =
                    ZpMatrix.GetSymmetricVanderMondeMatrix(NumParties, Prime)
                    .Transpose.Inverse.GetMatrixRow(0);

                // Calculate the value of the polynomial H(x) at i = H(i) as defined at GRR
                var tempSecret = new Zp(Prime, 0);
                for (int i = 0; i < NumParties; i++)
                {
                    tempSecret.Add(((shares[i] as ShareMsg <Zp>).Share as Share <Zp>).Value.Mul(vanderFirstRow[i]));
                }
            });
        }
예제 #3
0
        protected void ShareSecret(Zp secret, IList <int> players, DkmsKey key)
        {
            var shares = ShamirSharing.Share(secret, players.Count, players.Count - 1);

            var shareMsgs = new List <ShareMsg <Zp> >();

            foreach (var share in shares)
            {
                shareMsgs.Add(new ShareMsg <Zp>(new Share <Zp>(share), key));
            }

            Send(players, shareMsgs);
        }
예제 #4
0
파일: Program.cs 프로젝트: mahdiz/mpclib
        static void Main(string[] args)
        {
            Console.WriteLine("Started.");
            StaticRandom.Init(seed);

            int quorumSize = 20;
            int degree     = quorumSize / 3;

            var secret      = new Zp(Prime, 3);
            var shareMatrix = ZpMatrix.GetIdentityMatrix(quorumSize, Prime);

            // create the initial shares
            var initalShares = ShamirSharing.Share(secret, quorumSize, degree);

            for (var i = 0; i < quorumSize; i++)
            {
                IList <Zp> reshares = QuorumSharing.CreateReshares(initalShares[i], quorumSize, degree);

                for (var j = 0; j < quorumSize; j++)
                {
                    shareMatrix.SetMatrixCell(j, i, reshares[j]);
                }
            }

            // combine the reshares
            List <Zp> finalShares = new List <Zp>();

            for (var i = 0; i < quorumSize; i++)
            {
                Zp finalShare = QuorumSharing.CombineReshares(shareMatrix.GetMatrixRow(i), quorumSize, Prime);
                finalShares.Add(finalShare);
            }

            // combine the shares
            Zp final = ShamirSharing.Recombine(finalShares, degree, Prime);

            Console.WriteLine(final.Value);
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
예제 #5
0
        public override void Run()
        {
            // secret-share my input among all parties
            var sharesValues = ShamirSharing.Share(Input, NumParties, PolynomialDeg);
            var shareMsgs    = new List <ShareMsg <Zp> >();

            foreach (var shareValue in sharesValues)
            {
                shareMsgs.Add(new ShareMsg <Zp>(new Share <Zp>(shareValue), Stage.InputReceive));
            }

            Send(shareMsgs);

            OnReceive((int)Stage.InputReceive,
                      delegate(List <Msg> shares)
            {
                int k = 1;                              // TODO: temp only - needed to be index of gate
                foreach (var gate in Circuit.Gates)
                {
                    RunGateComputation(gate, GetZps(shares.Cast <ShareMsg <Zp> >()), k + ".");
                    k++;
                }
                var resultList = new SortedDictionary <int, Zp>();
                FilterPlayers(PartyIds);                                        // remove unwanted players if necessary

                // share the result with all players
                SendToAll(new ShareMsg <Zp>(new Share <Zp>(Circuit.Output), Stage.ResultReceive));

                OnReceive((int)Stage.ResultReceive,
                          delegate(List <Msg> resMsgs)
                {
                    Result = GetRecombinedResult(GetZps(resMsgs.OrderBy(s => s.SenderId).Cast <ShareMsg <Zp> >()), Input.Prime);
                    if (MpcFinish != null)
                    {
                        MpcFinish(StateKey);
                    }
                });
            });
        }