public Task <PreprocessedReceiverBatch> PreprocessReceiverAsync(IMessageChannel channel, int numberOfInvocations)
        {
            byte[] randomSelectionIndicesBuffer        = _randomNumberGenerator.GetBytes(QuadrupleIndexArray.RequiredBytes(numberOfInvocations));
            QuadrupleIndexArray randomSelectionIndices = QuadrupleIndexArray.FromBytes(randomSelectionIndicesBuffer, numberOfInvocations);

            return(_obliviousTransfer.ReceiveAsync(channel, randomSelectionIndices, numberOfInvocations)
                   .ContinueWith(task => new PreprocessedReceiverBatch(randomSelectionIndices, task.Result)));
        }
Beispiel #2
0
 public void TestRequiredBytes()
 {
     Assert.AreEqual(0, QuadrupleIndexArray.RequiredBytes(0));
     Assert.AreEqual(1, QuadrupleIndexArray.RequiredBytes(1));
     Assert.AreEqual(1, QuadrupleIndexArray.RequiredBytes(3));
     Assert.AreEqual(1, QuadrupleIndexArray.RequiredBytes(4));
     Assert.AreEqual(2, QuadrupleIndexArray.RequiredBytes(5));
 }
Beispiel #3
0
        public Task <byte[][]> ReceiveAsync(IMessageChannel channel, QuadrupleIndexArray selectionIndices, int numberOfInvocations, int numberOfMessageBytes)
        {
            if (selectionIndices.Length != numberOfInvocations)
            {
                throw new ArgumentException("Provided selection indices must match the specified number of invocations.", nameof(selectionIndices));
            }

            return(GeneralizedReceiveAsync(channel, selectionIndices, numberOfInvocations, numberOfMessageBytes));
        }
        public PreprocessedReceiverBatch(QuadrupleIndexArray selectionIndices, BitArray selectedOptions)
        {
            if (selectionIndices.Length != selectedOptions.Length)
            {
                throw new ArgumentException("Number of selection indices and selected options does not match.");
            }

            _selectionIndices = selectionIndices;
            _selectedOptions  = selectedOptions;
        }
        private Task <BitArray> ComputeReceiverSharesAsync(IMessageChannel channel, BitArray leftShares, BitArray rightShares, int numberOfInvocations)
        {
            QuadrupleIndexArray selectionIndices = new QuadrupleIndexArray(numberOfInvocations);

            for (int i = 0; i < numberOfInvocations; ++i)
            {
                selectionIndices[i] = 2 * (byte)leftShares[i] + (byte)rightShares[i];
            }

            return(_obliviousTransfer.ReceiveAsync(channel, selectionIndices, numberOfInvocations));
        }
Beispiel #6
0
        public void TestByteConversion()
        {
            QuadrupleIndexArray array = new QuadrupleIndexArray(new int[] { 3, 1, 2, 1, 3, 0, 1 });

            array[2] = 0;
            array[5] = 2;

            byte[] buffer = array.ToBytes();

            Assert.AreEqual(7, array.Length);
            Assert.AreEqual(2, buffer.Length);
            Assert.AreEqual((byte)Convert.ToInt32("01000111", 2), buffer[0]);
            Assert.AreEqual((byte)Convert.ToInt32("00011011", 2), buffer[1]);
        }
Beispiel #7
0
        private void RunObliviousTransferParty()
        {
            Quadruple <byte[]>[] options = new Quadruple <byte[]> [3];
            options = new Quadruple <byte[]>[]
            {
                new Quadruple <byte[]>(TestOptions.Select(s => Encoding.ASCII.GetBytes(s)).ToArray()),
                new Quadruple <byte[]>(TestOptions.Select(s => Encoding.ASCII.GetBytes(s.ToLower())).ToArray()),
                new Quadruple <byte[]>(TestOptions.Select(s => Encoding.ASCII.GetBytes(s.ToUpper())).ToArray()),
            };

            using (CryptoContext cryptoContext = CryptoContext.CreateDefault())
            {
                IGeneralizedObliviousTransfer obliviousTransfer = new NaorPinkasObliviousTransfer(
                    SecurityParameters.CreateDefault768Bit(),
                    cryptoContext
                    );

                using (ITwoPartyNetworkSession session = TestNetworkSession.EstablishTwoParty())
                {
                    if (session.LocalParty.Id == 0)
                    {
                        obliviousTransfer.SendAsync(session.Channel, options, 3, 6).Wait();
                    }
                    else
                    {
                        QuadrupleIndexArray indices = new QuadrupleIndexArray(new[] { 0, 3, 2 });
                        byte[][]            results = obliviousTransfer.ReceiveAsync(session.Channel, indices, 3, 6).Result;

                        Assert.IsNotNull(results, "Result is null.");
                        Assert.AreEqual(3, results.Length, "Result does not match the correct number of invocations.");

                        for (int j = 0; j < 3; ++j)
                        {
                            CollectionAssert.AreEqual(
                                results[j],
                                options[j][indices[j]],
                                "Incorrect message content {0} (should be {1}).",
                                Encoding.ASCII.GetString(results[j]),
                                Encoding.ASCII.GetString(options[j][indices[j]])
                                );
                        }
                    }
                }
            }
        }
Beispiel #8
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);
        }
Beispiel #9
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;
        }
Beispiel #10
0
 protected abstract Task <byte[][]> GeneralizedReceiveAsync(IMessageChannel channel, QuadrupleIndexArray selectionIndices, int numberOfInvocations, int numberOfMessageBytes);
Beispiel #11
0
 public Task <BitArray> ReceiveAsync(IMessageChannel channel, QuadrupleIndexArray selectionIndices, int numberOfInvocations)
 {
     return(ReceiveAsync(channel, selectionIndices, numberOfInvocations, 1)
            .ContinueWith(task => FromResultMessages(task.Result)));
 }
Beispiel #12
0
        protected override async Task <byte[][]> GeneralizedReceiveAsync(IMessageChannel channel, QuadrupleIndexArray selectionIndices, int numberOfInvocations, int numberOfMessageBytes)
        {
#if DEBUG
            Stopwatch stopwatch = Stopwatch.StartNew();
#endif

            Quadruple <BigInteger> listOfCs = new Quadruple <BigInteger>(await ReadGroupElements(channel, 4));

#if DEBUG
            stopwatch.Stop();
            Console.WriteLine("[Receiver] Reading values for c took {0} ms.", stopwatch.ElapsedMilliseconds);
            stopwatch.Restart();
#endif

            BigInteger[] listOfBetas = new BigInteger[numberOfInvocations];
            BigInteger[] listOfDs    = new BigInteger[numberOfInvocations];

            Parallel.For(0, numberOfInvocations, j =>
            {
                listOfDs[j] = GenerateGroupElement(out listOfBetas[j]);
                if (selectionIndices[j] > 0)
                {
                    listOfDs[j] = (listOfCs[selectionIndices[j]] * Invert(listOfDs[j])) % _parameters.P;
                }
            });

#if DEBUG
            stopwatch.Stop();
            Console.WriteLine("[Receiver] Generating and d took {0} ms.", stopwatch.ElapsedMilliseconds);
            stopwatch.Restart();
#endif

            Task writeDsTask = WriteGroupElements(channel, listOfDs);
            Task <Quadruple <byte[]>[]> readMaskedOptionsTask = ReadOptions(channel, numberOfInvocations, numberOfMessageBytes);

            BigInteger[] listOfEs = new BigInteger[numberOfInvocations];
            Parallel.For(0, numberOfInvocations, j =>
            {
                int i       = selectionIndices[j];
                listOfEs[j] = BigInteger.ModPow(listOfCs[0], listOfBetas[j], _parameters.P);
            });

            await Task.WhenAll(writeDsTask, readMaskedOptionsTask);

            Quadruple <byte[]>[] maskedOptions = readMaskedOptionsTask.Result;

#if DEBUG
            stopwatch.Stop();
            Console.WriteLine("[Receiver] Computing e, sending d and reading masked options took {0} ms.", stopwatch.ElapsedMilliseconds);
            stopwatch.Restart();
#endif

            byte[][] selectedOptions = new byte[numberOfInvocations][];
            Parallel.For(0, numberOfInvocations, j =>
            {
                int i              = selectionIndices[j];
                BigInteger e       = listOfEs[j];
                selectedOptions[j] = MaskOption(maskedOptions[j][i], e, j, i);
            });

#if DEBUG
            stopwatch.Stop();
            Console.WriteLine("[Receiver] Unmasking result took {0} ms.", stopwatch.ElapsedMilliseconds);
#endif

            return(selectedOptions);
        }
        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);
        }