2 # This file is part of GNUnet
3 # (C) 2013, 2018 Christian Grothoff (and other contributing authors)
5 # GNUnet is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as published
7 # by the Free Software Foundation, either version 3 of the License,
8 # or (at your option) any later version.
10 # GNUnet is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # SPDX-License-Identifier: AGPL3.0-or-later
22 from math import ceil, log, floor
26 """ count the bits set in n"""
37 def simulate(k, n, verbose):
39 largest_arc = int(2**ceil(log(n, 2))) // 2
40 num_ghosts = (2 * largest_arc) - n
42 print("we have", num_ghosts, "ghost peers")
43 # n.b. all peers with idx<k are evil
44 peers = list(range(n))
45 info = [1 << x for x in range(n)]
49 if bsc(info[x]) < n-k:
57 while arc <= largest_arc:
59 print("-- subround --")
60 new_info = [x for x in info]
61 for peer_physical in range(n):
62 peer_logical = peers[peer_physical]
64 partner_logical = (peer_logical + arc) % n
65 partner_physical = peers.index(partner_logical)
66 if peer_physical < k or partner_physical < k:
68 print("bad peer in connection", peer_physical, "--", partner_physical)
70 if peer_logical & arc == 0:
73 print(peer_physical, "connects to", partner_physical)
74 peer_type = "outgoing"
75 if peer_logical < num_ghosts:
76 # we have a ghost, check if the peer who connects
77 # to our ghost is actually outgoing
78 ghost_partner_logical = (peer_logical - arc) % n
79 if ghost_partner_logical & arc == 0:
80 peer_type = peer_type + ", ghost incoming"
81 new_info[peer_physical] = new_info[peer_physical] | info[peer_physical] | info[partner_physical]
82 new_info[partner_physical] = new_info[partner_physical] | info[peer_physical] | info[partner_physical]
84 peer_type = "incoming"
86 print("type of", str(peer_physical) + ":", peer_type)
94 if __name__ == "__main__":
95 parser = argparse.ArgumentParser()
96 parser.add_argument("k", metavar="k", type=int, help="#(bad peers)")
97 parser.add_argument("n", metavar="n", type=int, help="#(all peers)")
98 parser.add_argument("r", metavar="r", type=int, help="#(rounds)")
99 parser.add_argument('--verbose', '-v', action='count')
101 args = parser.parse_args()
103 for n in range(0, args.r):
104 sum += simulate(args.k, args.n, args.verbose)