Ejemplo n.º 1
0
        private static void TestTwoCycle(int i1, int i2, byte[] permutation)
        {
            Array.Copy(Identity, permutation, 64);
            permutation[i1] = (byte)i2;
            permutation[i2] = (byte)i1;
            PermutationNetwork pnwk = new PermutationNetwork(permutation);

            Assert.AreEqual(1UL << i2, pnwk.Permute(1UL << i1));
            Assert.AreEqual(1UL << i1, pnwk.Permute(1UL << i2));
        }
Ejemplo n.º 2
0
        /*
         * public static void SetupSimpleCircuitEvaluation(Quorum quorum)
         * {
         *  int n = quorum.Size;
         *  var polyDeg = (int)Math.Ceiling(n / 3.0) - 1;
         *
         *  Debug.Assert((n & (n - 1)) == 0); // is power of 2
         *
         *  network = new LPSortingNetwork(n);
         *
         *  IList<BigZp>[] shares = new IList<BigZp>[n];
         *
         *  for (int i = 0; i < n; i++)
         *      shares[i] = BigShamirSharing.Share(new BigZp(prime, 500 - 2*i), n, polyDeg);
         *
         *  foreach (var id in quorum.Members)
         *  {
         *      Dictionary<InputGateAddress, Share<BigZp>> inShares = new Dictionary<InputGateAddress, Share<BigZp>>();
         *
         *      int i = 0;
         *      foreach (var inAddr in network.Circuit.InputAddrs)
         *      {
         *          inShares[inAddr] = new Share<BigZp>(shares[i][id]);
         *          i++;
         *      }
         *
         *      TestParty<IDictionary<OutputGateAddress, Share<BigZp>>> party = new TestParty<IDictionary<OutputGateAddress, Share<BigZp>>>();
         *      party.UnderTest = new SecureGroupCircuitEvaluation(party, quorum.Clone() as Quorum, network.Circuit, inShares);
         *      NetSimulator.RegisterParty(party);
         *  }
         * }
         */
        public static void SetupMultiQuorumCircuitEvaluation(Quorum bigQuorum)
        {
            int n = bigQuorum.Size;

            int qSize = n / 2;

            var polyDeg = (int)Math.Ceiling(qSize / 3.0) - 1;

            var quorums = new List <Quorum>();

            quorums.Add(new Quorum(0, 0, qSize));
            quorums.Add(new Quorum(1, qSize, 2 * qSize));

            Debug.Assert((n & (n - 1)) == 0); // is power of 2

            network = new LPSortingNetwork(n);
            //network = SortingNetworkFactory.CreateButterflyTournamentRound(n);

            network.CollapsePermutationGates();

            IList <BigZp>[] shares = new IList <BigZp> [n];

            for (int i = 0; i < n; i++)
            {
                shares[i] = BigShamirSharing.Share(new BigZp(prime, 500 - 2 * i), qSize, polyDeg);
            }

            Dictionary <Gate, Quorum> gqmapping = new Dictionary <Gate, Quorum>();

            for (int i = 0; i < network.Circuit.TopologicalOrder.Count; i++)
            {
                gqmapping[network.Circuit.TopologicalOrder[i]] = quorums[i];
            }

            foreach (var id in bigQuorum.Members)
            {
                Dictionary <InputGateAddress, Share <BigZp> > inShares = new Dictionary <InputGateAddress, Share <BigZp> >();

                int i = 0;
                foreach (var inAddr in network.Circuit.InputAddrs)
                {
                    inShares[inAddr] = new Share <BigZp>(shares[i][id % 4]);
                    i++;
                }

                TestParty <IDictionary <OutputGateAddress, Share <BigZp> > > party = new TestParty <IDictionary <OutputGateAddress, Share <BigZp> > >();
                Quorum[] quorumsClone = quorums.Select(a => a.Clone() as Quorum).ToArray();

                party.UnderTest =
                    new SecureMultiQuorumCircuitEvaluation <Share <BigZp> >(party, quorumsClone[id / qSize], quorumsClone,
                                                                            ProtocolIdGenerator.GenericIdentifier(0), network.Circuit, inShares, new BigZpShareGateEvaluationFactory(prime), gqmapping, prime);

                NetSimulator.RegisterParty(party);
            }
        }
Ejemplo n.º 3
0
        public MultiPartyShufflingProtocol(Party me, SortedSet <int> members, ulong protocolId, BigZp secret, BigInteger prime)
            : base(me, members, protocolId)
        {
            Prime  = prime;
            Secret = secret;

            SortValue = new BigZp(prime, me.SafeRandGen.Next(prime));

            GateProtocolEvaluationFactory = new TaggedBigZpShareGateEvaluationFactory(Prime);

            SortNetwork = new LPSortingNetwork(members.Count);
            SortNetwork.CollapsePermutationGates();
        }
Ejemplo n.º 4
0
        public MultiPartySortingProtocol(Party me, SortedSet <int> members, ulong protocolId, BigZp secret, BigInteger prime)
            : base(me, members, protocolId)
        {
            Prime  = prime;
            Secret = secret;

            GateProtocolEvaluationFactory = new BigZpShareGateEvaluationFactory(prime);

            SortNetwork = new LPSortingNetwork(members.Count);

            /*
             * SortNetwork = SortingNetworkFactory.CreateButterflyTournamentRound(members.Count);
             * SortNetwork.AppendNetwork(SortingNetworkFactory.CreateButterflyTournamentRound(members.Count), 0);
             */
            SortNetwork.CollapsePermutationGates();
        }
        public void Test0_PermutationNetwork()
        {
            byte[] _IPTranspose = new byte[]
            {
                57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
                61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7,
                56, 48, 40, 32, 24, 16, 8, 0, 58, 50, 42, 34, 26, 18, 10, 2,
                60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6
            };
            byte[] _IPTransposeReversed = new byte[64];
            for (int i = 0; i < 64; ++i)
            {
                _IPTransposeReversed[i] = _IPTranspose[63 - i];
            }
            byte[] _IPTransposeInversed = new byte[64];
            for (int i = 0; i < 64; ++i)
            {
                _IPTransposeInversed[_IPTranspose[i]] = (byte)i;
            }
            byte[] _IPTransposeInversedReversed = new byte[64];
            for (int i = 0; i < 64; ++i)
            {
                _IPTransposeInversedReversed[_IPTranspose[63 - i]] = (byte)i;
            }
            var network = new PermutationNetwork(_IPTransposeInversedReversed);


            Random random = new Random(123);

            byte[] buf = new byte[8];
            for (int i = 0; i < 10000; ++i)
            {
                random.NextBytes(buf);
                ulong value    = BitConverter.ToUInt64(buf, 0);
                ulong expected = Bitops.BitPermutation(value, _IPTranspose);

                ulong actual = network.Permute(value);

                Assert.AreEqual(expected, actual);
            }
        }
Ejemplo n.º 6
0
        public void TestPermutationNetwork()
        {
            byte[] permutation = new byte[64];

            // Identity permutation
            for (byte i = 0; i < 64; i++)
            {
                Identity[i] = i;
            }

            for (int i1 = 0; i1 < 62; i1++)
            {
                for (int i2 = i1 + 1; i2 < 64; i2++)
                {
                    TestTwoCycle(i1, i2, permutation);
                }
            }

            Array.Copy(Identity, permutation, 64);

            PermutationNetwork pnwk = new PermutationNetwork(permutation);

            Assert.AreEqual(56ul, pnwk.Permute(56));

            // Swap unit and two's bits
            permutation[0] = 1;
            permutation[1] = 0;
            pnwk           = new PermutationNetwork(permutation);
            Assert.AreEqual(1ul, pnwk.Permute(2));
            Assert.AreEqual(2ul, pnwk.Permute(1));
            Assert.AreEqual(3ul, pnwk.Permute(3));
            Assert.AreEqual(5ul, pnwk.Permute(6));
            Assert.AreEqual(6ul, pnwk.Permute(5));

            // Cycle the bottom four bits
            permutation[0] = 1;
            permutation[1] = 2;
            permutation[2] = 3;
            permutation[3] = 0;
            pnwk           = new PermutationNetwork(permutation);
            Assert.AreEqual(2ul, pnwk.Permute(1));
            Assert.AreEqual(4ul, pnwk.Permute(2));
            Assert.AreEqual(8ul, pnwk.Permute(4));
            Assert.AreEqual(1ul, pnwk.Permute(8));
            Assert.AreEqual(3ul, pnwk.Permute(9));

            // Random permutation
            Random rnd = new Random(0);

            for (int i = 0; i < 63; i++)
            {
                int  swapIndex = rnd.Next(64);
                byte b         = permutation[i];
                permutation[i]         = permutation[swapIndex];
                permutation[swapIndex] = b;
            }
            pnwk = new PermutationNetwork(permutation);
            ulong testVal   = ((ulong)rnd.Next() << 32) | (uint)rnd.Next();
            ulong resultVal = pnwk.Permute(testVal);

            TestPerm(permutation, testVal, resultVal);
        }