Esempio n. 1
0
        private async Task <BigInteger[]> ReadGroupElements(IMessageChannel channel, int numberOfGroupElements)
        {
            MessageDecomposer message = new MessageDecomposer(await channel.ReadMessageAsync());

            BigInteger[] groupElements = new BigInteger[numberOfGroupElements];
            for (int i = 0; i < numberOfGroupElements; ++i)
            {
                int    length             = message.ReadInt();
                byte[] packedGroupElement = message.ReadBuffer(length);
                groupElements[i] = new BigInteger(packedGroupElement);
            }

            return(groupElements);
        }
Esempio n. 2
0
        private async Task <byte[][][]> ReadOptions(IMessageChannel channel, int numberOfOptions, int numberOfInvocations, int numberOfMessageBytes)
        {
            MessageDecomposer message = new MessageDecomposer(await channel.ReadMessageAsync());

            byte[][][] options = new byte[numberOfInvocations][][];
            for (int j = 0; j < numberOfInvocations; ++j)
            {
                options[j] = new byte[numberOfOptions][];
                for (int i = 0; i < numberOfOptions; ++i)
                {
                    options[j][i] = message.ReadBuffer(numberOfMessageBytes);
                }
            }

            return(options);
        }
        protected override async Task <byte[][]> GeneralizedReceiveAsync(IMessageChannel channel, QuadrupleIndexArray selectionIndices, int numberOfInvocations, int numberOfMessageBytes)
        {
            byte[] packedOptions = await channel.ReadMessageAsync();

            if (packedOptions.Length != 4 * numberOfInvocations * numberOfMessageBytes)
            {
                throw new DesynchronizationException("Received incorrect number of options.");
            }

            byte[][] selectedMessages = new byte[numberOfInvocations][];
            for (int i = 0; i < numberOfInvocations; ++i)
            {
                selectedMessages[i] = new byte[numberOfMessageBytes];
                Buffer.BlockCopy(packedOptions, (4 * i + selectionIndices[i]) * numberOfMessageBytes, selectedMessages[i], 0, numberOfMessageBytes);
            }

            return(selectedMessages);
        }
Esempio n. 4
0
        public async Task <BitArray> ReceiveAsync(IMessageChannel channel, QuadrupleIndexArray selectionIndices, int numberOfInvocations)
        {
            if (selectionIndices.Length != numberOfInvocations)
            {
                throw new ArgumentException("Provided selection indices must match the specified number of invocations.", nameof(selectionIndices));
            }

            if (_receiverBatch == null || _nextReceiverInstanceId + numberOfInvocations > _receiverBatch.NumberOfInstances)
            {
                throw new InvalidOperationException("Not enough preprocessed receiver data available.");
            }


            QuadrupleIndexArray deltaSelectionIndices = new QuadrupleIndexArray(numberOfInvocations);

            for (int i = 0; i < numberOfInvocations; ++i)
            {
                int deltaSelectionIndex = (_receiverBatch.GetSelectionIndex(_nextReceiverInstanceId + i) - selectionIndices[i] + 4) % 4;
                deltaSelectionIndices[i] = deltaSelectionIndex;
            }

            await channel.WriteMessageAsync(deltaSelectionIndices.ToBytes());

            byte[] packedMaskedOptionQuadruples = await channel.ReadMessageAsync();

            if (packedMaskedOptionQuadruples.Length != BitQuadrupleArray.RequiredBytes(numberOfInvocations))
            {
                throw new DesynchronizationException("Received incorrect number of masked option quadruples.");
            }

            BitQuadrupleArray maskedOptionQuadruples = BitQuadrupleArray.FromBytes(packedMaskedOptionQuadruples, numberOfInvocations);

            BitArray selectedBits = new BitArray(numberOfInvocations);

            for (int i = 0; i < numberOfInvocations; ++i)
            {
                BitQuadruple maskedOptions = maskedOptionQuadruples[i];
                selectedBits[i] = maskedOptions[selectionIndices[i]] ^ _receiverBatch.GetSelectedOption(_nextReceiverInstanceId + i);
            }

            _nextReceiverInstanceId += numberOfInvocations;

            return(selectedBits);
        }
Esempio n. 5
0
        public async Task SendAsync(IMessageChannel channel, BitQuadrupleArray options, int numberOfInvocations)
        {
            if (options.Length != numberOfInvocations)
            {
                throw new ArgumentException("Provided options must match the specified number of invocations.", nameof(options));
            }

            if (_senderBatch == null || _nextSenderInstanceId + numberOfInvocations > _senderBatch.NumberOfInstances)
            {
                throw new InvalidOperationException("Not enough preprocessed sender data available.");
            }

            byte[] packedDeltaSelectionIndices = await channel.ReadMessageAsync();

            if (packedDeltaSelectionIndices.Length != QuadrupleIndexArray.RequiredBytes(numberOfInvocations))
            {
                throw new DesynchronizationException("Received incorrect number of delta selection indices.");
            }

            QuadrupleIndexArray deltaSelectionIndices = QuadrupleIndexArray.FromBytes(packedDeltaSelectionIndices, numberOfInvocations);

            BitQuadrupleArray maskedOptionQuadruples = new BitQuadrupleArray(numberOfInvocations);

            for (int i = 0; i < numberOfInvocations; ++i)
            {
                int          deltaSelectionIndex = deltaSelectionIndices[i];
                BitQuadruple preprocessedOptions = _senderBatch.GetOptions(_nextSenderInstanceId + i);
                BitQuadruple unmaskedOptions     = options[i];
                BitQuadruple maskedOptions       = new BitQuadruple(
                    unmaskedOptions[0] ^ preprocessedOptions[(0 + deltaSelectionIndex) % 4],
                    unmaskedOptions[1] ^ preprocessedOptions[(1 + deltaSelectionIndex) % 4],
                    unmaskedOptions[2] ^ preprocessedOptions[(2 + deltaSelectionIndex) % 4],
                    unmaskedOptions[3] ^ preprocessedOptions[(3 + deltaSelectionIndex) % 4]
                    );

                maskedOptionQuadruples[i] = maskedOptions;
            }

            await channel.WriteMessageAsync(maskedOptionQuadruples.ToBytes());

            _nextSenderInstanceId += numberOfInvocations;
        }
Esempio n. 6
0
        public static async Task <byte[][][]> ReadOptionsAsync(IMessageChannel channel, int numberOfOptions, int numberOfInvocations, int numberOfMessageBytes)
        {
            byte[] messageBuffer = await channel.ReadMessageAsync();

            byte[][][] result = new byte[numberOfInvocations][][];
            Parallel.For(0, numberOfInvocations, i =>
            {
                result[i] = new byte[numberOfOptions][];
                for (int j = 0; j < numberOfOptions; ++j)
                {
                    result[i][j] = new byte[numberOfMessageBytes];
                    Buffer.BlockCopy(
                        messageBuffer,
                        (numberOfOptions * numberOfMessageBytes) * i + j * numberOfMessageBytes,
                        result[i][j],
                        0,
                        numberOfMessageBytes
                        );
                }
            });
            return(result);
        }