Пример #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
        protected override Zp GetRecombinedResult(IList <Zp> recvList, int prime)
        {
            // Scan recvList - if there are null elements replace them arbitrarily to Zp with zero value
            for (int i = 0; i < recvList.Count; i++)
            {
                if (recvList[i] == null)
                {
                    recvList[i] = new Zp(prime, 0);
                }
            }

            var xVlaues = new List <Zp>();
            int w       = NumTheoryUtils.GetFieldMinimumPrimitive(prime);

            for (int i = 0; i < recvList.Count; i++)
            {
                xVlaues.Add(new Zp(prime, NumTheoryUtils.ModPow(w, i, prime)));
            }

            // Should call Welch-Berlekamp Decoder to fix error at last stage
            var fixedShares = WelchBerlekampDecoder.Decode(xVlaues, recvList, PolynomialDeg, PolynomialDeg, prime);

            if (fixedShares == null)
            {
                throw new Exception("There were more then polynomialDegree = " + PolynomialDeg + " Cheaters - cannot extract results.");
            }

            return(ShamirSharing.Recombine(fixedShares, PolynomialDeg, prime, true));
        }
Пример #3
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]));
                }
            });
        }
Пример #4
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);
        }
Пример #5
0
        //public override void Receive<BrideAssignment>(int fromNode, BrideAssignment message, MessageType type)
        //{
        //    throw new NotImplementedException();
        //}

        public override void ReceiveEmail(long fromPseudonym, object message, MessageType type)
        {
            if (type == MessageType.UserAssignments)   // From a distributor: Here's a list of bridge id share for you.
            {
                var assignments = message as List <UserAssignment>;
                if (distributorPseudonyms.Count == 1)
                {
                    foreach (var a in assignments)
                    {
                        Bridges.Add(Simulator.GetNode <Bridge>((int)a.BridgeShare.Value));
                    }
                }
                else
                {
                    foreach (var a in assignments)
                    {
                        if (!BridgeShares.ContainsKey(a.BridgePseudonym))
                        {
                            BridgeShares[a.BridgePseudonym] = new List <Zp>();
                        }

                        BridgeShares[a.BridgePseudonym].Add(a.BridgeShare);

                        if (BridgeShares[a.BridgePseudonym].Count == distributorPseudonyms.Count)
                        {
                            // We have enough number of shares to reconstruct the bridge ID
                            int bridgeId = (int)ShamirSharing.Reconstruct(BridgeShares[a.BridgePseudonym],
                                                                          Simulator.PolynomialDegree, Simulator.Prime).Value;

                            Debug.Assert(Simulator.GetNodes <Bridge>().Any(b => b.Id == bridgeId), "Invalid bridge ID reconstructed from shares.");
                            Bridges.Add(Simulator.GetNode <Bridge>(bridgeId));
                            BridgeShares[a.BridgePseudonym] = null;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("Invalid message received.");
            }
        }
Пример #6
0
        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();
        }
Пример #7
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);
                    }
                });
            });
        }
Пример #8
0
 // TODO: Mahdi: Should this method be virtual?
 protected virtual Zp GetRecombinedResult(IList <Zp> recvList, int prime)
 {
     return(ShamirSharing.Recombine(recvList, PolynomialDeg, prime));
 }