Ejemplo n.º 1
0
        /*
         *  prime = prime20;
         *  Main(32);
         *  NetSimulator.Reset();
         *
         *  prime = prime30;
         *  Main(32);
         *  NetSimulator.Reset();
         *
         *  prime = prime40;
         *  Main(32);
         *  NetSimulator.Reset();
         *
         *  prime = prime50;
         *  Main(32);
         *  NetSimulator.Reset();
         */

        public static void Main(int n)                                   // number of parties
        {
            Debug.Assert(NumTheoryUtils.MillerRabin(prime, 5) == false); // must be a prime

            // Create an MPC network, add parties, and init them with random inputs
            NetSimulator.Init(seed);     //seed
            StaticRandom.Init(seed + 1); //seed + 1

            Quorum q = new Quorum(0, 0, n);

            SetupMps(n);

            //SetupMultiQuorumCircuitEvaluation(q);

            Console.WriteLine(n + " parties initialized. Running simulation...\n");
            // run the simulator
            var elapsedTime = Timex.Run(() => NetSimulator.Run());

            // CheckMps(n);
            //ReconstructDictionary(q, network.LastGateForWire, 4);

            Console.WriteLine("Simulation finished.  Checking results...\n");

            Console.WriteLine("# parties    = " + n);
            Console.WriteLine("# msgs sent  = " + NetSimulator.SentMessageCount.ToString("0.##E+00"));
            Console.WriteLine("# bits sent  = " + (NetSimulator.SentByteCount * 8).ToString("0.##E+00"));
            Console.WriteLine("Rounds       = " + NetSimulator.RoundCount + "\n");
            Console.WriteLine("Key size     = " + NumTheoryUtils.GetBitLength2(prime) + " bits");
            Console.WriteLine("Seed         = " + seed + "\n");
            Console.WriteLine("Elapsed time = " + elapsedTime.ToString("hh':'mm':'ss'.'fff") + "\n");
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
0
 public static void Init(int seed)
 {
     StaticRandom.Init(seed);
     des     = new EventSimulator <EventQueue>();
     parties = new Dictionary <int, Party>();
 }
Ejemplo n.º 4
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (stopped)
            {
                dgvStats.Rows.Clear();
                //chPlots.Series.Clear();
                StaticRandom.Init(seed);
                Simulator.Reset();

                #region UI Initialization
                btnStart.Text  = "Stop";
                cbLogY.Enabled = false;
                stopped        = stopRequest = exitRequest = logIncConsidered = false;

                //// Set up plots
                //chPlots.Series.Add(new Series(cbThirsty.Text));
                //chPlots.Series.Add(new Series(cbm.Text));
                //chPlots.Series.Add(new Series(cbb.Text));
                //chPlots.Series.Add(new Series(cbN.Text));
                //chPlots.Series.Add(new Series(cbDistMessageCount.Text));
                //chPlots.Series.Add(new Series(cbEmailCount.Text));
                //chPlots.Series.Add(new Series(cbTime.Text));
                //chPlots.Series.Add(new Series(cbmm.Text));
                //chPlots.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;
                //chPlots.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.LightGray;
                //chPlots.ChartAreas[0].AxisX.LabelStyle.Font = plotScreenFont;
                //chPlots.ChartAreas[0].AxisY.LabelStyle.Font = plotScreenFont;
                //chPlots.ChartAreas[0].AxisX.TitleFont = plotScreenFont;
                //chPlots.ChartAreas[0].AxisY.TitleFont = plotScreenFont;
                //chPlots.Legends[0].Font = plotScreenFont;
                //chPlots.Legends[0].IsDockedInsideChartArea = true;
                //chPlots.ApplyPaletteColors();

                //foreach (var series in chPlots.Series)
                //{
                //    series.BorderWidth = plotThickness;
                //    series.ChartType = SeriesChartType.Line;
                //    series.MarkerSize = plotMarkerSize;
                //}
                //chPlots.Series[0].MarkerStyle = MarkerStyle.Square;
                //chPlots.Series[1].MarkerStyle = MarkerStyle.Triangle;
                //chPlots.Series[2].MarkerStyle = MarkerStyle.Diamond;
                //chPlots.Series[3].MarkerStyle = MarkerStyle.Circle;

                //chPlots.Series[5].Color = chPlots.Series[2].Color;
                //chPlots.Series[5].MarkerStyle = MarkerStyle.Circle;

                //chPlots.Series[6].Color = chPlots.Series[2].Color;
                //chPlots.Series[6].MarkerStyle = MarkerStyle.Circle;
                //chPlots.Series[7].Color = chPlots.Series[3].Color;
                //chPlots.Series[7].MarkerStyle = MarkerStyle.Square;
                UpdatePlots();
                Application.DoEvents();
                #endregion

                int n     = (int)Math.Pow(2, tbUserCount.Value);
                int t_max = rbMultipleRuns.Checked ? t_max = tbBadCountMax.Value : tbCorruptCount.Value + 1;

                for (int t = tbCorruptCount.Value; t < t_max && !stopRequest; t += 5)
                {
                    Simulator.Reset();

                    // Create the distributors
                    var dists = new List <Distributor>();

                    for (int i = 1; i < tbDistCount.Value; i++)
                    {
                        dists.Add(new Distributor());
                    }

                    var leader = new LeaderDistributor(dists.Select(d => d.Id).ToList(),
                                                       rbBnb.Checked ? DistributeAlgorithm.BallsAndBins : DistributeAlgorithm.Matrix, seed);
                    dists.Insert(0, leader);

                    var distIds = dists.Select(d => d.Pseudonym).ToList();

                    // Add honest users
                    for (int i = 0; i < n - t; i++)
                    {
                        new User(distIds);
                    }

                    // Add corrupt users
                    var attackModel = AttackModel.Aggressive;
                    if (rbPrudent.Checked)
                    {
                        attackModel = AttackModel.Prudent;
                    }
                    else if (rbStochastic.Checked)
                    {
                        attackModel = AttackModel.Stochastic;
                    }

                    var censor = new Censor(dists, attackModel, tbStochastic.Value / 40.0, seed);
                    censor.AddCorruptUsers(t);

                    // Create a sufficient number of bridges
                    repeatCount = tbC.Value == 0 ? 1 : tbC.Value * (int)Math.Ceiling(Math.Log(n, 2));
                    var bridges = new List <Bridge>();
                    for (int i = 0; i < (10 * t + 32) * repeatCount; i++)    // See the bridge cost lemma in the paper
                    {
                        bridges.Add(new Bridge(distIds));
                    }

                    leader.BridgePseudonyms = bridges.Select(b => b.Pseudonym).ToList();
                    runLog             = new RunLog();
                    leader.OnRoundEnd += OnRoundEnd;
                    leader.OnRunEnd   += OnRunEnd;

                    leader.Run(repeatCount);

                    logIncConsidered = cbLogY.Checked;
                    cbLogY.Enabled   = true;
                    if (rbMultipleRuns.Checked)
                    {
                        // Add one point to the plot for this run for each plot
                        //chPlots.Series[cbTime.Text].Points.AddXY(t, runLog.RoundsCount);

                        var N = runLog.Sum(r => r.BlockedCount) + runLog.Last().BridgeCount;
                        //chPlots.Series[cbmm.Text].Points.AddXY(t, N);
                    }
                }

                //for (int x = 8; x < 12 && !stopRequest; x++)
                //{
                //    blockedSoFar = 0;
                //    Simulator.Reset();

                //    int n = (int)Math.Pow(2, x);
                //    int t = n / 2;
                //    // Create the distributors
                //    var dists = new List<Distributor>();

                //    for (int i = 1; i < tbDistCount.Value; i++)
                //        dists.Add(new Distributor());

                //    var leader = new LeaderDistributor(dists.Select(d => d.Id).ToList(),
                //        rbBnb.Checked ? DistributeAlgorithm.BallsAndBins : DistributeAlgorithm.Matrix, seed);
                //    dists.Insert(0, leader);

                //    var distIds = dists.Select(d => d.Pseudonym).ToList();

                //    // Add honest users
                //    for (int i = 0; i < n - t; i++)
                //        new User(distIds);

                //    // Add corrupt users
                //    var attackModel = AttackModel.Aggressive;
                //    if (rbPrudent.Checked)
                //        attackModel = AttackModel.Prudent;
                //    else if (rbStochastic.Checked)
                //        attackModel = AttackModel.Stochastic;

                //    var censor = new Censor(dists, attackModel, tbStochastic.Value / 40.0, seed);
                //    censor.AddCorruptUsers(t);

                //    // Create a sufficient number of bridges
                //    repeatCount = tbC.Value == 0 ? 1 : tbC.Value * (int)Math.Ceiling(Math.Log(n, 2));
                //    var bridges = new List<Bridge>();
                //    for (int i = 0; i < (8 * t - 2) * repeatCount; i++)
                //        bridges.Add(new Bridge(distIds));

                //    leader.BridgePseudonyms = bridges.Select(b => b.Pseudonym).ToList();
                //    runLog = new RunLog();
                //    leader.OnRoundEnd += OnRoundEnd;

                //    leader.Run(repeatCount);

                //    logIncConsidered = cbLogY.Checked;
                //    cbLogY.Enabled = true;

                //    chPlots.Series[cbEmailCount.Text].Points.AddXY(n, (double)runLog.Sum(r => r.EmailCount) / n);

                //}
            }
            else
            {
                stopRequest = true;
            }

            btnStart.Text = "Start";
            stopped       = true;

            if (exitRequest)
            {
                Close();
            }
        }