public static void SetupBitwiseAndProtocol(Quorum quorum) { int n = quorum.Size; var polyDeg = (int)Math.Ceiling(n / 3.0) - 1; var sharesA = BigShamirSharing.Share(new BigZp(prime, 0), n, polyDeg); var sharesB = BigShamirSharing.Share(new BigZp(prime, 1), n, polyDeg); var sharesC = BigShamirSharing.Share(new BigZp(prime, 0), n, polyDeg); var sharesD = BigShamirSharing.Share(new BigZp(prime, 0), n, polyDeg); var sharesE = BigShamirSharing.Share(new BigZp(prime, 1), n, polyDeg); var sharesF = BigShamirSharing.Share(new BigZp(prime, 1), n, polyDeg); for (int i = 0; i < n; i++) { Quorum q = quorum.Clone() as Quorum; TestParty <List <Share <BigZp> > > party = new TestParty <List <Share <BigZp> > >(); party.UnderTest = new BitwiseOperationProtocol(party, q, MakeList(sharesA[i], sharesB[i], sharesC[i]), MakeList(sharesD[i], sharesE[i], sharesF[i]), new SharedBitAnd.ProtocolFactory(party, q)); NetSimulator.RegisterParty(party); } }
public override void HandleMessage(int fromId, Msg msg) { if (Stage == 0) { ReceiveShareStage(msg); if (SharesReceived == EvalGate.InputCount) { StartGateEvaluation(); } } else if (Stage == 1) { Debug.Assert(msg.Type == MsgType.SubProtocolCompleted); var completedMsg = (SubProtocolCompletedMsg)msg; CollectGateEvaluation(completedMsg); SendShareStage(); } else if (Stage == 2) { // we loopback any shares that go to me in a different quorum. Debug.Assert(msg is SubProtocolCompletedMsg); SubProtocolCompletedMsg completedMsg = (SubProtocolCompletedMsg)msg; foreach (var loopback in OutputLoopbacksNeeded) { ulong counterpartEvalId = ProtocolIdGenerator.GateEvalIdentifier(loopback.Value.Gate.TopologicalRank); T loopbackVal = (T)completedMsg.Result[loopback.Key]; NetSimulator.Loopback(Me.Id, counterpartEvalId, new LoopbackMsg <T>(loopbackVal, loopback.Value.Port)); } IsCompleted = true; } }
public static void ReconstructDictionary(Quorum q, OutputGateAddress[] ordering, int qSize) { List <BigZp> result = new List <BigZp>(); foreach (OutputGateAddress outAddr in ordering) { if (outAddr == null) { continue; } BigZp[] shares = new BigZp[qSize]; int j = 0; foreach (var id in q.Members) { Protocol <IDictionary <OutputGateAddress, Share <BigZp> > > p = (NetSimulator.GetParty(id) as TestParty <IDictionary <OutputGateAddress, Share <BigZp> > >).UnderTest; if (p.Result.ContainsKey(outAddr)) { shares[j++] = p.Result[outAddr].Value; } } result.Add(BigShamirSharing.Recombine(new List <BigZp>(shares), (int)Math.Ceiling(qSize / 3.0) - 1, prime)); } Console.WriteLine("Result: " + string.Join(" ", result)); }
/* * prime = prime20; * Main(32); * NetSimulator.Reset(); * * prime = prime30; * Main(32); * NetSimulator.Reset(); * * prime = prime40; * Main(32); * NetSimulator.Reset(); * * prime = prime50; * Main(32); * NetSimulator.Reset(); */ public static void Main(int n) // number of parties { Debug.Assert(NumTheoryUtils.MillerRabin(prime, 5) == false); // must be a prime // Create an MPC network, add parties, and init them with random inputs NetSimulator.Init(seed); //seed StaticRandom.Init(seed + 1); //seed + 1 Quorum q = new Quorum(0, 0, n); SetupMps(n); //SetupMultiQuorumCircuitEvaluation(q); Console.WriteLine(n + " parties initialized. Running simulation...\n"); // run the simulator var elapsedTime = Timex.Run(() => NetSimulator.Run()); // CheckMps(n); //ReconstructDictionary(q, network.LastGateForWire, 4); Console.WriteLine("Simulation finished. Checking results...\n"); Console.WriteLine("# parties = " + n); Console.WriteLine("# msgs sent = " + NetSimulator.SentMessageCount.ToString("0.##E+00")); Console.WriteLine("# bits sent = " + (NetSimulator.SentByteCount * 8).ToString("0.##E+00")); Console.WriteLine("Rounds = " + NetSimulator.RoundCount + "\n"); Console.WriteLine("Key size = " + NumTheoryUtils.GetBitLength2(prime) + " bits"); Console.WriteLine("Seed = " + seed + "\n"); Console.WriteLine("Elapsed time = " + elapsedTime.ToString("hh':'mm':'ss'.'fff") + "\n"); }
public static void CheckMps(int n) { for (int i = 0; i < n; i++) { MpsParty party = (MpsParty)NetSimulator.GetParty(i); Console.WriteLine("Party " + i + ": " + string.Join(" ", party.Results)); } }
/* * public static void SetupSimpleCircuitEvaluation(Quorum quorum) * { * int n = quorum.Size; * var polyDeg = (int)Math.Ceiling(n / 3.0) - 1; * * Debug.Assert((n & (n - 1)) == 0); // is power of 2 * * network = new LPSortingNetwork(n); * * IList<BigZp>[] shares = new IList<BigZp>[n]; * * for (int i = 0; i < n; i++) * shares[i] = BigShamirSharing.Share(new BigZp(prime, 500 - 2*i), n, polyDeg); * * foreach (var id in quorum.Members) * { * Dictionary<InputGateAddress, Share<BigZp>> inShares = new Dictionary<InputGateAddress, Share<BigZp>>(); * * int i = 0; * foreach (var inAddr in network.Circuit.InputAddrs) * { * inShares[inAddr] = new Share<BigZp>(shares[i][id]); * i++; * } * * TestParty<IDictionary<OutputGateAddress, Share<BigZp>>> party = new TestParty<IDictionary<OutputGateAddress, Share<BigZp>>>(); * party.UnderTest = new SecureGroupCircuitEvaluation(party, quorum.Clone() as Quorum, network.Circuit, inShares); * NetSimulator.RegisterParty(party); * } * } */ public static void SetupMultiQuorumCircuitEvaluation(Quorum bigQuorum) { int n = bigQuorum.Size; int qSize = n / 2; var polyDeg = (int)Math.Ceiling(qSize / 3.0) - 1; var quorums = new List <Quorum>(); quorums.Add(new Quorum(0, 0, qSize)); quorums.Add(new Quorum(1, qSize, 2 * qSize)); Debug.Assert((n & (n - 1)) == 0); // is power of 2 network = new LPSortingNetwork(n); //network = SortingNetworkFactory.CreateButterflyTournamentRound(n); network.CollapsePermutationGates(); IList <BigZp>[] shares = new IList <BigZp> [n]; for (int i = 0; i < n; i++) { shares[i] = BigShamirSharing.Share(new BigZp(prime, 500 - 2 * i), qSize, polyDeg); } Dictionary <Gate, Quorum> gqmapping = new Dictionary <Gate, Quorum>(); for (int i = 0; i < network.Circuit.TopologicalOrder.Count; i++) { gqmapping[network.Circuit.TopologicalOrder[i]] = quorums[i]; } foreach (var id in bigQuorum.Members) { Dictionary <InputGateAddress, Share <BigZp> > inShares = new Dictionary <InputGateAddress, Share <BigZp> >(); int i = 0; foreach (var inAddr in network.Circuit.InputAddrs) { inShares[inAddr] = new Share <BigZp>(shares[i][id % 4]); i++; } TestParty <IDictionary <OutputGateAddress, Share <BigZp> > > party = new TestParty <IDictionary <OutputGateAddress, Share <BigZp> > >(); Quorum[] quorumsClone = quorums.Select(a => a.Clone() as Quorum).ToArray(); party.UnderTest = new SecureMultiQuorumCircuitEvaluation <Share <BigZp> >(party, quorumsClone[id / qSize], quorumsClone, ProtocolIdGenerator.GenericIdentifier(0), network.Circuit, inShares, new BigZpShareGateEvaluationFactory(prime), gqmapping, prime); NetSimulator.RegisterParty(party); } }
public static void SetupBitwiseRandomGeneration(Quorum quorum) { int n = quorum.Size; for (int i = 0; i < n; i++) { TestParty <List <Share <BigZp> > > party = new TestParty <List <Share <BigZp> > >(); party.UnderTest = new RandomBitwiseGenProtocol(party, quorum, prime, 15); NetSimulator.RegisterParty(party); } }
public static void SetupRandomBitGenProtocol(Quorum quorum) { int n = quorum.Size; for (int i = 0; i < n; i++) { TestParty <Share <BigZp> > party = new TestParty <Share <BigZp> >(); party.UnderTest = new RandomBitGenProtocol(party, quorum.Clone() as Quorum, prime); NetSimulator.RegisterParty(party); } }
private void Reshare(BigZp secret) { Debug.Assert(Prime == secret.Prime); IList <BigZp> coeffs = null; var shares = BigShamirSharing.Share(secret, FinalShareCount, NewPolyDeg, out coeffs); MG[] witnesses = null; MG commitment = null; if (PolyCommit != null) { commitment = BigShamirSharing.GenerateCommitment(FinalShareCount, coeffs.ToArray(), Prime, ref witnesses, (Quorums[TO] as ByzantineQuorum).PolyCommit); // fake random generation round for (int i = 0; i < Quorums[TO].Size; i++) { NetSimulator.FakeMulticast(Quorums[TO].Size, secret.Size); } // fake commitment and challenge response NetSimulator.FakeMulticast(Quorums[TO].Size, secret.Size); NetSimulator.FakeMulticast(Quorums[TO].Size, secret.Size); } else { witnesses = new MG[FinalShareCount]; } QuorumBroadcast(new CommitMsg(commitment), TO); // create the share messages if (PolyCommit != null) { Debug.Assert(PolyCommit.VerifyEval(commitment, new BigZp(Prime, 2), shares[1], witnesses[1])); } Debug.Assert(BigShamirSharing.Recombine(shares, NewPolyDeg, Prime) == secret); int numSent = 0; //int delay = (PolyCommit != null) ? 1 : 0; foreach (var toMember in Quorums[TO].Members) { for (int i = 0; i < FinalSharesPerParty; i++) { Send(toMember, new ShareWitnessMsg <BigZp>(shares[numSent], witnesses[numSent])); numSent++; } } }
public static void SetupCompareAndSwap(Quorum quorum) { int n = quorum.Size; var polyDeg = (int)Math.Ceiling(n / 3.0) - 1; var sharesA = BigShamirSharing.Share(new BigZp(prime, 440), n, polyDeg); var sharesB = BigShamirSharing.Share(new BigZp(prime, 450), n, polyDeg); for (int i = 0; i < n; i++) { TestParty <Share <BigZp>[]> party = new TestParty <Share <BigZp>[]>(); party.UnderTest = new CompareAndSwapProtocol(party, quorum.Clone() as Quorum, new Share <BigZp>(sharesA[i]), new Share <BigZp>(sharesB[i])); NetSimulator.RegisterParty(party); } }
public static void Reconstruct(Quorum q) { BigZp[] shares = new BigZp[q.Size]; int i = 0; foreach (var id in q.Members) { shares[i++] = (NetSimulator.GetParty(id) as TestParty <Share <BigZp> >).UnderTest.Result.Value; } var val = BigShamirSharing.Recombine(new List <BigZp>(shares), (int)Math.Ceiling(q.Size / 3.0) - 1, prime); Console.WriteLine("Output: " + val); }
public static void SetupLessThan(Quorum quorum) { int n = quorum.Size; var polyDeg = (int)Math.Ceiling(n / 3.0) - 1; var sharesA = BigShamirSharing.Share(new BigZp(prime, 700), n, polyDeg); var sharesB = BigShamirSharing.Share(new BigZp(prime, 549), n, polyDeg); for (int i = 0; i < n; i++) { TestParty <Share <BigZp> > party = new TestParty <Share <BigZp> >(); party.UnderTest = new ShareLessThanProtocol(party, quorum, new Share <BigZp>(sharesA[i]), new Share <BigZp>(sharesB[i])); NetSimulator.RegisterParty(party); } }
public static void SetupMps2(int n) { List <BigZp> inputs = new List <BigZp>(); for (int i = 0; i < n; i++) { inputs.Add(new BigZp(prime, StaticRandom.Next(prime))); } // Console.WriteLine("Inputs: " + string.Join(" ", inputs)); for (int i = 0; i < n; i++) { NetSimulator.RegisterParty(new MpsParty(n, inputs[i])); } }
public static void SetupLeastSignificantBit(Quorum quorum) { int n = quorum.Size; var input = new BigZp(prime, 2); var polyDeg = (int)Math.Ceiling(n / 3.0) - 1; var shares = BigShamirSharing.Share(input, n, polyDeg); for (int i = 0; i < n; i++) { TestParty <Share <BigZp> > party = new TestParty <Share <BigZp> >(); party.UnderTest = new LeastSignificantBitProtocol(party, quorum, new Share <BigZp>(shares[i])); NetSimulator.RegisterParty(party); } }
public static void SetupReconstructionProtocol(Quorum quorum) { int n = quorum.Size; var input = new BigZp(prime, 20); var polyDeg = (int)Math.Ceiling(n / 3.0) - 1; var shares = BigShamirSharing.Share(input, n, polyDeg); for (int i = 0; i < n; i++) { TestParty <BigZp> party = new TestParty <BigZp>(); ReconstructionProtocol rp = new ReconstructionProtocol(party, quorum, new Share <BigZp>(shares[i])); party.UnderTest = rp; NetSimulator.RegisterParty(party); } }
public static void SetupBitCompositionProtocol(Quorum quorum) { int n = quorum.Size; var polyDeg = (int)Math.Ceiling(n / 3.0) - 1; var sharesA = BigShamirSharing.Share(new BigZp(prime, 0), n, polyDeg); var sharesB = BigShamirSharing.Share(new BigZp(prime, 1), n, polyDeg); var sharesC = BigShamirSharing.Share(new BigZp(prime, 1), n, polyDeg); for (int i = 0; i < n; i++) { TestParty <Share <BigZp> > party = new TestParty <Share <BigZp> >(); party.UnderTest = new BitCompositionProtocol(party, quorum, MakeList(sharesA[i], sharesB[i], sharesC[i]), prime); NetSimulator.RegisterParty(party); } }
public static void SetupPrefixOrProtocol(Quorum quorum) { int n = quorum.Size; var polyDeg = (int)Math.Ceiling(n / 3.0) - 1; var sharesA = BigShamirSharing.Share(new BigZp(prime, 0), n, polyDeg); var sharesB = BigShamirSharing.Share(new BigZp(prime, 0), n, polyDeg); var sharesC = BigShamirSharing.Share(new BigZp(prime, 0), n, polyDeg); var sharesD = BigShamirSharing.Share(new BigZp(prime, 0), n, polyDeg); for (int i = 0; i < n; i++) { TestParty <List <Share <BigZp> > > party = new TestParty <List <Share <BigZp> > >(); party.UnderTest = new PrefixOperationProtocol(party, quorum, MakeList(sharesA[i], sharesB[i], sharesC[i], sharesD[i]), new SharedBitOr.ProtocolFactory(party, quorum)); NetSimulator.RegisterParty(party); } }
public static void ReconstructTuple(Quorum q) { BigZp[] shares1 = new BigZp[q.Size]; BigZp[] shares2 = new BigZp[q.Size]; int i = 0; foreach (var id in q.Members) { var result = (NetSimulator.GetParty(id) as TestParty <Tuple <Share <BigZp>, Share <BigZp> > >).UnderTest.Result; shares1[i] = result.Item1.Value; shares2[i] = result.Item2.Value; i++; } var val1 = BigShamirSharing.Recombine(new List <BigZp>(shares1), (int)Math.Ceiling(q.Size / 3.0) - 1, prime); var val2 = BigShamirSharing.Recombine(new List <BigZp>(shares2), (int)Math.Ceiling(q.Size / 3.0) - 1, prime); Console.WriteLine(val1 + " " + val2); }
public static void SetupMps(int n) { List <BigZp> inputs = new List <BigZp>(); for (int i = 0; i < n; i++) { inputs.Add(new BigZp(prime, StaticRandom.Next(prime))); } /* * Console.WriteLine("Inputs: " + string.Join(" ", inputs)); * var sorted = inputs.Select(zp => zp.Value).ToList(); * sorted.Sort(); * * Console.WriteLine("Expected Output: " + string.Join(" ", sorted)); */ for (int i = 0; i < n; i++) { NetSimulator.RegisterParty(new MpsParty(n, inputs[i])); } }
public static void ReconstructBitwise(Quorum q, int bitCount) { List <BigZp> result = new List <BigZp>(); for (int i = bitCount - 1; i >= 0; i--) { BigZp[] shares = new BigZp[q.Size]; int j = 0; foreach (var id in q.Members) { shares[j++] = (NetSimulator.GetParty(id) as TestParty <List <Share <BigZp> > >).UnderTest.Result[i].Value; } result.Add(BigShamirSharing.Recombine(new List <BigZp>(shares), (int)Math.Ceiling(q.Size / 3.0) - 1, prime)); } foreach (var bit in result) { Console.Write(bit); } Console.WriteLine(); }
private void SendShareStage() { List <Protocol> reshareProtocols = new List <Protocol>(); OutputLoopbacksNeeded = new Dictionary <ulong, InputGateAddress>(); for (int i = 0; i < EvalGate.OutputCount; i++) { // figure out where this share is going to InputGateAddress counterpartGateAddr; if (Circuit.OutputConnectionCounterparties.TryGetValue(EvalGate.GetLocalOutputAddress(i), out counterpartGateAddr)) { var counterpartGate = counterpartGateAddr.Gate; var counterpartQuorum = GateQuorumMapping[counterpartGate]; bool needReshare; bool needLoopback; if (counterpartQuorum == EvalQuorum) { needLoopback = true; needReshare = false; } else if (counterpartQuorum.HasMember(Me.Id)) { needLoopback = true; needReshare = true; } else { needLoopback = false; needReshare = true; } if (needLoopback && !needReshare) { // loop a message back to the other protocol ulong counterpartEvalId = ProtocolIdGenerator.GateEvalIdentifier(counterpartGate.TopologicalRank); NetSimulator.Loopback(Me.Id, counterpartEvalId, new LoopbackMsg <T>(OutputShares[i], counterpartGateAddr.Port)); } if (needReshare) { Protocol reshareProtocol = ProtocolFactory.GetResharingProtocol(Me, EvalQuorum, counterpartQuorum, OutputShares[i], counterpartGate.TopologicalRank, counterpartGateAddr.Port); reshareProtocols.Add(reshareProtocol); if (needLoopback) { OutputLoopbacksNeeded[reshareProtocol.ProtocolId] = counterpartGateAddr; } } } else { // this was a circuit output Result[EvalGate.GetLocalOutputAddress(i)] = OutputShares[i]; } } Stage = 2; if (reshareProtocols.Count > 0) { ExecuteSubProtocols(reshareProtocols); } else { IsCompleted = true; } }
public static void Main(string[] args) { // no quorums all primes: MultiPartyShufflingProtocol.EVALUATION_QUORUM_COUNT = 1; MultiPartyShufflingProtocol.EVALUATION_QUORUM_SIZE = 8; /* * prime = prime10; * Main(8); * NetSimulator.Reset(); * * prime = prime20; * Main(8); * NetSimulator.Reset(); * * prime = prime30; * Main(8); * NetSimulator.Reset(); * * prime = prime40; * Main(8); * NetSimulator.Reset(); * * prime = prime50; * Main(8); * NetSimulator.Reset(); * * MultiPartyShufflingProtocol.EVALUATION_QUORUM_SIZE = 16; * * prime = prime10; * Main(16); * NetSimulator.Reset(); * * prime = prime20; * Main(16); * NetSimulator.Reset(); * * prime = prime30; * Main(16); * NetSimulator.Reset(); * * prime = prime40; * Main(16); * NetSimulator.Reset(); * * prime = prime50; * Main(16); * NetSimulator.Reset(); * * MultiPartyShufflingProtocol.EVALUATION_QUORUM_SIZE = 32; * * prime = prime10; * Main(32); * NetSimulator.Reset(); * * // with log n quorums of size 8 * * MultiPartyShufflingProtocol.EVALUATION_QUORUM_COUNT = 4; * MultiPartyShufflingProtocol.EVALUATION_QUORUM_SIZE = 8; * * prime = prime30; * Main(16); * NetSimulator.Reset(); * * MultiPartyShufflingProtocol.EVALUATION_QUORUM_COUNT = 8; * * prime = prime30; * Main(32); * NetSimulator.Reset(); * * MultiPartyShufflingProtocol.EVALUATION_QUORUM_COUNT = 16; * * prime = prime30; * Main(64); * * MultiPartyShufflingProtocol.EVALUATION_QUORUM_COUNT = 1; * * prime = prime160; * Main(8); * NetSimulator.Reset(); */ MultiPartyShufflingProtocol.EVALUATION_QUORUM_COUNT = 8; MultiPartyShufflingProtocol.EVALUATION_QUORUM_SIZE = 8; prime = prime10; Main(32); NetSimulator.Reset(); prime = prime20; Main(32); NetSimulator.Reset(); prime = prime30; Main(32); NetSimulator.Reset(); prime = prime40; Main(32); NetSimulator.Reset(); prime = prime50; Main(32); NetSimulator.Reset(); Console.ReadLine(); Console.ReadLine(); Console.ReadLine(); }