MpcLib.Circuits.LPSortingCalculationCache.generatePi C# (CSharp) Method

generatePi() public method

public generatePi ( int length ) : int[]
length int
return int[]
        public int[] generatePi(int length)
        {
            Debug.Assert(length < MAX_SIZE);
            if (PermutationCache[length] != null)
                return PermutationCache[length];

            PopulateBetaCacheOuter(length, MIDDLE);

            int powLength = 1 << length;

            List<Tuple<int, int>> sortList = new List<Tuple<int, int>>();
            for (int i = 0; i < powLength; i++)
            {
                int sortVal = (int)Math.Floor(powLength * BetaCache[length, MIDDLE][i]);
                sortList.Add(new Tuple<int, int>(sortVal, i));
            }

            // sort in ascending order
            sortList.Sort((x, y) =>
            {
                int result = y.Item1.CompareTo(x.Item1);
                return (result == 0) ? y.Item2.CompareTo(x.Item2) : result;
            });

            int[] permutation = new int[powLength];

            for (int i = 0; i < powLength; i++)
            {
                permutation[sortList[i].Item2] = i;
            }
            PermutationCache[length] = permutation;

            return permutation;
        }

Usage Example

Example #1
0
        // Lemma 4.1
        private PermutationNetwork CreateBlockCorrectionNetwork(int blockBitLength, int unsortedness)
        {
            PermutationNetwork pn = new PermutationNetwork(1 << blockBitLength);

            SortedSet <int> ySet = new SortedSet <int>(CalculationCache.generateY(blockBitLength));

            Debug.Assert(ySet.Count <= 1 << unsortedness);
            // augment Y with arbitrary elements
            int ySize     = 1 << (unsortedness + 1);
            int blockSize = 1 << blockBitLength;

            Random rand = new Random(0);

            while (ySet.Count < ySize)
            {
                ySet.Add(rand.Next(blockSize));
            }

            // here our implementation differs from the paper.  The paper first to extract Y then order the X by the permutation pi.
            // Instead, we will order all of the inputs by the permutation pi, then map Y using the permutation pi and move X to the top of the block
            // and Y to the bottom so that we can unshuffle X and add Y.

            int[] pi = CalculationCache.generatePi(blockBitLength);

            pn.AppendGate(new PermutationGate(pi), 0);

            int[] mappedY = new int[ySize];
            int   i       = 0;

            foreach (var yElem in ySet)
            {
                mappedY[i++] = pi[yElem];
            }

            pn.AppendGate(PermutationGateFactory.CreateSplitGate(blockSize, mappedY, false), 0);

            // we now want to unshuffle X into 2^(l+1) groups and add one element of Y to each group

            pn.AppendGate(PermutationGateFactory.CreateUnshuffleGate(blockSize - ySize, ySize), 0);

            pn.AppendGate(PermutationGateFactory.CreateMultiGroupInserterGate(blockSize, (blockSize / ySize) - 1, ySize), 0);

            var treeInsertion = SortingNetworkFactory.CreateBinaryTreeInsertion(blockSize / ySize);

            // use binary tree insertion to insert the elemnt we just added to each group
            for (int j = 0; j < ySize; j++)
            {
                pn.AppendNetwork(treeInsertion.Clone() as PermutationNetwork, j * blockSize / ySize);
            }

            // now shuffle all of the lists back together
            pn.AppendGate(PermutationGateFactory.CreateShuffleGate(blockSize, ySize), 0);

            return(pn);
        }