private static void RunSecureComputationParty(int startPort, int numberOfParties, int localPartyId, BitArray localInput, BitArray expectedOutput) { using (IMultiPartyNetworkSession session = TestNetworkSession.EstablishMultiParty(localPartyId, numberOfParties)) { using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { IObliviousTransfer obliviousTransfer = new NaorPinkasObliviousTransfer( new SecurityParameters(47, 23, 4, 1, 1), cryptoContext ); IMultiplicativeSharing multiplicativeSharing = new ObliviousTransferMultiplicativeSharing( obliviousTransfer, cryptoContext ); GMWSecureComputation computation = new GMWSecureComputation(session, multiplicativeSharing, cryptoContext); SetIntersectionCircuitRecorder circuitRecorder = new SetIntersectionCircuitRecorder(numberOfParties, localInput.Length); CircuitBuilder circuitBuilder = new CircuitBuilder(); circuitRecorder.Record(circuitBuilder); ForwardCircuit circuit = new ForwardCircuit(circuitBuilder.CreateCircuit()); BitArray output = computation.EvaluateAsync(circuit, circuitRecorder.InputMapping, circuitRecorder.OutputMapping, localInput).Result; CollectionAssert.AreEqual( expectedOutput, output, "Incorrect output {0} (should be {1}).", output.ToBinaryString(), expectedOutput.ToBinaryString() ); } } }
private static void RunSecureComputationParty(int localPartyId, BitArray localInput) { using (IMultiPartyNetworkSession session = CreateLocalSession(localPartyId, StartPort, NumberOfParties)) { using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { IObliviousTransfer obliviousTransfer = new NaorPinkasObliviousTransfer( SecurityParameters.CreateDefault768Bit(), cryptoContext ); IMultiplicativeSharing multiplicativeSharing = new ObliviousTransferMultiplicativeSharing( obliviousTransfer, cryptoContext ); GMWSecureComputation computation = new GMWSecureComputation(session, multiplicativeSharing, cryptoContext); Stopwatch stopwatch = Stopwatch.StartNew(); SetIntersectionSecureProgram secureProgram = new SetIntersectionSecureProgram(NumberOfParties, NumberOfElements); object[] outputPrimitives = secureProgram.EvaluateAsync(computation, new[] { localInput }).Result; BitArray intersection = (BitArray)outputPrimitives[0]; BigInteger count = (BigInteger)outputPrimitives[1]; stopwatch.Stop(); Console.WriteLine(); Console.WriteLine("Completed protocol as {0} in {1} ms.", session.LocalParty.Name, stopwatch.ElapsedMilliseconds); Console.WriteLine(" Local input: {0}", localInput.ToBinaryString()); Console.WriteLine(" Computed intersection: {0}", intersection.ToBinaryString()); Console.WriteLine(" Computed number of matches: {0}", count); } } }
private void RunObliviousTransferParty1oo2Resumed() { const int numberOfInvocations = 3; const int numberOfOptions = 2; Pair <byte[]>[] options = new Pair <byte[]> [numberOfInvocations]; options = new Pair <byte[]>[] { new Pair <byte[]>(TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s)).ToArray()), new Pair <byte[]>(TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s.ToLower())).ToArray()), new Pair <byte[]>(TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s.ToUpper())).ToArray()), }; using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { using (ITwoPartyNetworkSession session = TestNetworkSession.EstablishTwoParty()) { ITwoChoicesObliviousTransferChannel baseOT = new StatelessTwoChoicesObliviousTransferChannel(new InsecureObliviousTransfer(), session.Channel); ITwoChoicesObliviousTransferChannel obliviousTransfer = new TwoChoicesExtendedObliviousTransferChannel(baseOT, 8, cryptoContext); if (session.LocalParty.Id == 0) { for (int i = 0; i < 2; ++i) { obliviousTransfer.SendAsync(options, numberOfInvocations, 6).Wait(); } } else { PairIndexArray[] allIndices = new[] { new PairIndexArray(new[] { 0, 1, 0 }), new PairIndexArray(new[] { 1, 0, 0 }) }; for (int i = 0; i < 2; ++i) { PairIndexArray indices = allIndices[i]; byte[][] results = obliviousTransfer.ReceiveAsync(indices, numberOfInvocations, 6).Result; Assert.IsNotNull(results, "Result is null."); Assert.AreEqual(numberOfInvocations, results.Length, "Result does not match the correct number of invocations."); for (int j = 0; j < numberOfInvocations; ++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]]) ); } } } } } }
private byte[][] RunObliviousTransferReceiverParty(BitArray selectionBits) { using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { using (var session = TestNetworkSession.EstablishTwoParty()) { ITwoChoicesObliviousTransferChannel baseOT = new StatelessTwoChoicesObliviousTransferChannel(new InsecureObliviousTransfer(), session.Channel); ITwoChoicesCorrelatedObliviousTransferChannel obliviousTransfer = new TwoChoicesCorrelatedExtendedObliviousTransferChannel(baseOT, 8, cryptoContext); return(obliviousTransfer.ReceiveAsync(selectionBits, selectionBits.Length, NumberOfMessageBytes).Result); } } }
private Pair <byte[]>[] RunObliviousTransferSenderParty(int numberOfInvocations) { using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { using (ITwoPartyNetworkSession session = TestNetworkSession.EstablishTwoParty()) { ITwoChoicesObliviousTransferChannel baseOT = new StatelessTwoChoicesObliviousTransferChannel(new InsecureObliviousTransfer(), session.Channel); ITwoChoicesRandomObliviousTransferChannel obliviousTransfer = new TwoChoicesRandomExtendedObliviousTransferChannel(baseOT, 8, cryptoContext); return(obliviousTransfer.SendAsync(numberOfInvocations, NumberOfMessageBytes).Result); } } }
public void TestInvoke() { using CryptoContext cryptoContext = CryptoContext.CreateDefault(); RandomOracle oracle = new HashRandomOracle(cryptoContext.HashAlgorithmProvider); byte[] firstQuery = { 235, 12, 13, 72, 138, 13, 62, 13, 39, 147, 198, 173, 23, 87, 27, 99 }; byte[] secondQuery = { 84, 23, 123, 85, 62, 28, 54, 98, 187, 238, 18, 5, 78, 1, 78, 243 }; EnumerableAssert.AreNotEqual( oracle.Invoke(secondQuery).Take(10), oracle.Invoke(firstQuery).Take(10) ); }
private Pair <byte[]>[] RunObliviousTransferSenderParty(byte[][] correlationStrings) { using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { using (var session = TestNetworkSession.EstablishTwoParty()) { ITwoChoicesObliviousTransferChannel baseOT = new StatelessTwoChoicesObliviousTransferChannel(new InsecureObliviousTransfer(), session.Channel); ITwoChoicesCorrelatedObliviousTransferChannel obliviousTransfer = new TwoChoicesCorrelatedExtendedObliviousTransferChannel(baseOT, 8, cryptoContext); return(obliviousTransfer.SendAsync( correlationStrings, correlationStrings.Length, NumberOfMessageBytes).Result); } } }
private static async Task RunSecureComputationPartyAsync(int localPartyId, BitArray localInput) { Console.WriteLine($"Starting party {localPartyId}..."); using IMultiPartyNetworkSession session = await TcpMultiPartyNetworkSession.EstablishLoopbackAsync( new Party (localPartyId), StartPort, NumberOfParties ); using CryptoContext cryptoContext = CryptoContext.CreateDefault(); IObliviousTransfer obliviousTransfer = new NaorPinkasObliviousTransfer( SecurityParameters.CreateDefault768Bit(), cryptoContext ); IMultiplicativeSharing multiplicativeSharing = new ObliviousTransferMultiplicativeSharing( obliviousTransfer, cryptoContext ); SecretSharingSecureComputation computation = new SecretSharingSecureComputation( session, multiplicativeSharing, cryptoContext ); Stopwatch stopwatch = Stopwatch.StartNew(); SetIntersectionSecureProgram secureProgram = new SetIntersectionSecureProgram(NumberOfParties, NumberOfElements); object[] outputPrimitives = await secureProgram.EvaluateAsync(computation, new object[] { localInput }); BitArray intersection = (BitArray)outputPrimitives[0]; BigInteger count = (BigInteger)outputPrimitives[1]; stopwatch.Stop(); Console.WriteLine(); Console.WriteLine($"Completed protocol as {session.LocalParty.Name} in {stopwatch.ElapsedMilliseconds} ms."); Console.WriteLine($" Local input: {localInput.ToBinaryString()}"); Console.WriteLine($" Computed intersection: {intersection.ToBinaryString()}"); Console.WriteLine($" Computed number of matches: {count}"); Console.WriteLine(); Console.WriteLine("Press any key to exit."); Console.ReadKey(true); }
public void TestInvoke() { using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { RandomOracle oracle = new HashRandomOracle(cryptoContext.HashAlgorithm); byte[] firstQuery = { 235, 12, 13, 72, 138, 13, 62, 13, 39, 147, 198, 173, 23, 87, 27, 99 }; byte[] secondQuery = { 84, 23, 123, 85, 62, 28, 54, 98, 187, 238, 18, 5, 78, 1, 78, 243 }; byte[] firstResponse = oracle.Invoke(firstQuery).Take(10).ToArray(); byte[] secondResponse = oracle.Invoke(secondQuery).Take(10).ToArray(); CollectionAssert.AreNotEqual(firstResponse, secondResponse); } }
private void RunObliviousTransferParty1oo2() { const int numberOfInvocations = 3; const int numberOfOptions = 2; Pair <byte[]>[] options = new Pair <byte[]> [numberOfInvocations]; options = new Pair <byte[]>[] { new Pair <byte[]>(TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s)).ToArray()), new Pair <byte[]>(TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s.ToLower())).ToArray()), new Pair <byte[]>(TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s.ToUpper())).ToArray()), }; using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { IStatelessTwoChoicesObliviousTransfer obliviousTransfer = new NaorPinkasObliviousTransfer( SecurityParameters.CreateDefault768Bit(), cryptoContext ); using (ITwoPartyNetworkSession session = TestNetworkSession.EstablishTwoParty()) { if (session.LocalParty.Id == 0) { obliviousTransfer.SendAsync(session.Channel, options, numberOfInvocations, 6).Wait(); } else { PairIndexArray indices = new PairIndexArray(new[] { 0, 1, 0 }); byte[][] results = obliviousTransfer.ReceiveAsync(session.Channel, indices, numberOfInvocations, 6).Result; Assert.IsNotNull(results, "Result is null."); Assert.AreEqual(numberOfInvocations, results.Length, "Result does not match the correct number of invocations."); for (int j = 0; j < numberOfInvocations; ++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]]) ); } } } } }
private Pair <Pair <byte[]>[]> RunResumedObliviousTransferSenderParty(byte[][] firstCorrelationStrings, byte[][] secondCorrelationStrings) { using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { using (var session = TestNetworkSession.EstablishTwoParty()) { ITwoChoicesObliviousTransferChannel baseOT = new StatelessTwoChoicesObliviousTransferChannel(new InsecureObliviousTransfer(), session.Channel); ITwoChoicesCorrelatedObliviousTransferChannel obliviousTransfer = new TwoChoicesCorrelatedExtendedObliviousTransferChannel(baseOT, 8, cryptoContext); Pair <byte[]>[] firstRoundResults = obliviousTransfer.SendAsync( firstCorrelationStrings, firstCorrelationStrings.Length, NumberOfMessageBytes).Result; Pair <byte[]>[] secondRoundResults = obliviousTransfer.SendAsync( secondCorrelationStrings, secondCorrelationStrings.Length, NumberOfMessageBytes).Result; return(new Pair <Pair <byte[]>[]>(firstRoundResults, secondRoundResults)); } } }
private Pair <byte[][]> RunResumedObliviousTransferReceiverParty(BitArray firstRoundSelectionBits, BitArray secondRoundSelectionBits) { using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { using (ITwoPartyNetworkSession session = TestNetworkSession.EstablishTwoParty()) { ITwoChoicesObliviousTransferChannel baseOT = new StatelessTwoChoicesObliviousTransferChannel(new InsecureObliviousTransfer(), session.Channel); ITwoChoicesRandomObliviousTransferChannel obliviousTransfer = new TwoChoicesRandomExtendedObliviousTransferChannel(baseOT, 8, cryptoContext); byte[][] firstRoundResults = obliviousTransfer.ReceiveAsync( firstRoundSelectionBits, firstRoundSelectionBits.Length, NumberOfMessageBytes).Result; byte[][] secondRoundResults = obliviousTransfer.ReceiveAsync( secondRoundSelectionBits, secondRoundSelectionBits.Length, NumberOfMessageBytes).Result; return(new Pair <byte[][]>(firstRoundResults, secondRoundResults)); } } }
private void RunInsecureObliviousTransferParty1oo6() { const int numberOfInvocations = 4; const int numberOfOptions = 6; byte[][][] options = new byte[][][] { TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s)).ToArray(), TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s.ToLower())).ToArray(), TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s.ToUpper())).ToArray(), TestOptions.Take(numberOfOptions).Select(s => Encoding.ASCII.GetBytes(s.ToUpper())).ToArray(), }; using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { InsecureObliviousTransfer obliviousTransfer = new InsecureObliviousTransfer(); using (ITwoPartyNetworkSession session = TestNetworkSession.EstablishTwoParty()) { if (session.LocalParty.Id == 0) { obliviousTransfer.SendAsync(session.Channel, options, numberOfOptions, numberOfInvocations, 6).Wait(); } else { int[] indices = new[] { 4, 1, 3, 5 }; byte[][] results = obliviousTransfer.ReceiveAsync(session.Channel, indices, numberOfOptions, numberOfInvocations, 6).Result; Assert.IsNotNull(results, "Result is null."); Assert.AreEqual(numberOfInvocations, results.Length, "Result does not match the correct number of invocations."); for (int j = 0; j < numberOfInvocations; ++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]]) ); } } } } }
public void TestThreadsafeMultipleInstancesSameHash() { using (CryptoContext cryptoContext = CryptoContext.CreateDefault()) { RandomOracle[] oracle = new[] { new HashRandomOracle(cryptoContext.HashAlgorithmProvider.CreateThreadsafe()), new HashRandomOracle(cryptoContext.HashAlgorithmProvider.CreateThreadsafe()) }; byte[] query = new[] { (byte)0x34, (byte)0x2f, (byte)0xab, (byte)0x25, (byte)0x33 }; byte[] expected = oracle[0].Invoke(query).Take(20).ToArray(); int count = 1000; Parallel.For(0, 2, i => { for (int j = 0; j < count; ++j) { byte[] result = oracle[i].Invoke(query).Take(20).ToArray(); CollectionAssert.AreEqual(expected, result); } }); } }
private static void PerformSecureComputation(IMultiPartyNetworkSession session, BitArray expectedOutput) { BitArray localInput = Inputs[session.LocalParty.Id]; using CryptoContext cryptoContext = CryptoContext.CreateDefault(); IObliviousTransfer obliviousTransfer = new NaorPinkasObliviousTransfer( new SecurityParameters(47, 23, 4, 1, 1), cryptoContext ); IMultiplicativeSharing multiplicativeSharing = new ObliviousTransferMultiplicativeSharing( obliviousTransfer, cryptoContext ); SecretSharingSecureComputation computation = new SecretSharingSecureComputation( session, multiplicativeSharing, cryptoContext ); SetIntersectionCircuitRecorder circuitRecorder = new SetIntersectionCircuitRecorder( session.NumberOfParties, localInput.Length ); CircuitBuilder circuitBuilder = new CircuitBuilder(); circuitRecorder.Record(circuitBuilder); ForwardCircuit circuit = new ForwardCircuit(circuitBuilder.CreateCircuit()); BitArray actualOutput = computation .EvaluateAsync(circuit, circuitRecorder.InputMapping, circuitRecorder.OutputMapping, localInput) .Result; EnumerableAssert.AreEqual( expectedOutput, actualOutput ); }
private static void PerformObliviousTransfer(ITwoPartyNetworkSession session) { Quadruple <byte[]>[] options = { 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 ); 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]]) ); } } }