Exemplo n.º 1
0
 public void TestRequiredBytes()
 {
     Assert.AreEqual(0, BitQuadrupleArray.RequiredBytes(0));
     Assert.AreEqual(1, BitQuadrupleArray.RequiredBytes(1));
     Assert.AreEqual(1, BitQuadrupleArray.RequiredBytes(2));
     Assert.AreEqual(2, BitQuadrupleArray.RequiredBytes(3));
 }
        public Task <PreprocessedSenderBatch> PreprocessSenderAsync(IMessageChannel channel, int numberOfInvocations)
        {
            byte[]            randomOptionsBuffer = _randomNumberGenerator.GetBytes(BitQuadrupleArray.RequiredBytes(numberOfInvocations));
            BitQuadrupleArray randomOptions       = BitQuadrupleArray.FromBytes(randomOptionsBuffer, numberOfInvocations);

            return(_obliviousTransfer.SendAsync(channel, randomOptions, numberOfInvocations)
                   .ContinueWith(task => new PreprocessedSenderBatch(randomOptions)));
        }
Exemplo n.º 3
0
        private Quadruple <byte[]>[] ToOptionMessages(BitQuadrupleArray options)
        {
            Quadruple <byte[]>[] optionMessages = new Quadruple <byte[]> [options.Length];
            for (int i = 0; i < optionMessages.Length; ++i)
            {
                optionMessages[i] = new Quadruple <byte[]>(
                    new[] { (byte)options[i][0] },
                    new[] { (byte)options[i][1] },
                    new[] { (byte)options[i][2] },
                    new[] { (byte)options[i][3] }
                    );
            }

            return(optionMessages);
        }
Exemplo n.º 4
0
        public void TestByteConversion()
        {
            BitQuadrupleArray array = new BitQuadrupleArray(new[] {
                new BitQuadruple(Bit.Zero, Bit.One, Bit.One, Bit.Zero),
                new BitQuadruple(Bit.One, Bit.One, Bit.Zero, Bit.One),
                new BitQuadruple(Bit.Zero, Bit.One, Bit.Zero, Bit.One)
            });

            array[1] = new BitQuadruple(Bit.One, Bit.Zero, Bit.One, Bit.One);

            byte[] buffer = array.ToBytes();

            Assert.AreEqual(3, array.Length);
            Assert.AreEqual(2, buffer.Length);
            Assert.AreEqual((byte)Convert.ToInt32("11010110", 2), buffer[0]);
            Assert.AreEqual((byte)Convert.ToInt32("00001010", 2), buffer[1]);
        }
Exemplo n.º 5
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);
        }
        private async Task <BitArray> ComputeSenderSharesAsync(IMessageChannel channel, BitArray leftShares, BitArray rightShares, int numberOfInvocations)
        {
            BitArray          randomShares = _randomNumberGenerator.GetBits(numberOfInvocations);
            BitQuadrupleArray options      = new BitQuadrupleArray(numberOfInvocations);

            for (int i = 0; i < numberOfInvocations; ++i)
            {
                options[i] = new BitQuadruple(
                    randomShares[i],                                     // 00
                    randomShares[i] ^ leftShares[i],                     // 01
                    randomShares[i] ^ rightShares[i],                    // 10
                    randomShares[i] ^ leftShares[i] ^ rightShares[i]     // 11
                    );
            }

            await _obliviousTransfer.SendAsync(channel, options, numberOfInvocations);

            return(randomShares);
        }
Exemplo n.º 7
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;
        }
Exemplo n.º 8
0
 public Task SendAsync(IMessageChannel channel, BitQuadrupleArray options, int numberOfInvocations)
 {
     return(SendAsync(channel, ToOptionMessages(options), numberOfInvocations, 1));
 }
Exemplo n.º 9
0
 public PreprocessedSenderBatch(BitQuadrupleArray options)
 {
     _options = options;
 }