Skip to content

Commit

Permalink
Linear ip lottery 5
Browse files Browse the repository at this point in the history
  • Loading branch information
EggPool committed Jul 8, 2020
1 parent 3cc4b98 commit b30d4d8
Show file tree
Hide file tree
Showing 17 changed files with 7,401 additions and 5 deletions.
6 changes: 5 additions & 1 deletion chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
SIMULATION = "linear_ip_lottery"
SIMULATION = "linear_ip_lottery2"
SIMULATION = "linear_ip_lottery4"
SIMULATION = "linear_ip_lottery5"


DIR = pathlib.Path('./simulations/{}'.format(SIMULATION))
Expand All @@ -35,7 +36,10 @@ def process(file_name):
for line in fp:
if "DIVERGE" in line:
continue
_, consensus, ip_class, ip_count = line.strip().split(",")
try:
_, consensus, ip_class, ip_count = line.strip().split(",")
except:
continue
STATS["Total"] += 1
ip_count = int(ip_count)
if consensus == 'True':
Expand Down
35 changes: 31 additions & 4 deletions libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ def shuffle(cycle_hash: bytes):
# print(SHUFFLE_MAP)


def shuffle4(cycle_hash: bytes):
global SHUFFLE_MAP
SHUFFLE_MAP = []
random = Random(cycle_hash)
for i in range(4):
map = [i for i in range(256)]
random.shuffle(map)
SHUFFLE_MAP.append(list(map)) # copy, not reference
# print(SHUFFLE_MAP)


def linear_ip_score2(cycle_hash: bytes, identifier: bytes, ip: str) -> int:
"""
Nyzo Score computation from raw ip distance in linear space, with 2 first bytes being pseudo randomly shuffled
Expand Down Expand Up @@ -169,17 +180,33 @@ def linear_ip_score4(cycle_hash: bytes, identifier: bytes, ip: str) -> int:
ip_bytes_shuffle += (SHUFFLE_MAP[ip_bytes[3]]).to_bytes(1, byteorder='big')
ip_int = int.from_bytes(ip_bytes_shuffle, "big")
"""
ip_int = SHUFFLE_MAP[ip_bytes[0]] * 256 * 256 * 256\
ip_int = SHUFFLE_MAP[ip_bytes[0]] * 256 * 256 * 256\
+ SHUFFLE_MAP[ip_bytes[1]] * 256 * 256\
+ SHUFFLE_MAP[ip_bytes[2]] * 256 \
+ SHUFFLE_MAP[ip_bytes[3]]
# shuffled = socket.inet_ntoa(ip_bytes_shuffle)
hash_int = int.from_bytes(cycle_hash[:4], "big")
score = abs(hash_int - ip_int)
# print(ip, ip_bytes, ip_bytes_shuffle, shuffled, cycle_hash[:4].hex(), hash_int, score)
# sys.exit()
return score


def linear_ip_score5(cycle_hash: bytes, identifier: bytes, ip: str) -> int:
"""
Nyzo Score computation from raw ip distance in linear space, with all bytes being pseudo randomly shuffled with a different permutation map each
"""
score = sys.maxsize
if ip == '':
return score

ip_bytes = socket.inet_aton(ip)
ip_int = SHUFFLE_MAP[0][ip_bytes[0]] * 256 * 256 * 256\
+ SHUFFLE_MAP[1][ip_bytes[1]] * 256 * 256\
+ SHUFFLE_MAP[2][ip_bytes[2]] * 256 \
+ SHUFFLE_MAP[3][ip_bytes[3]]
hash_int = int.from_bytes(cycle_hash[:4], "big")
score = abs(hash_int - ip_int)
return score


def random_hash() -> bytes:
"""Let say this is a cycle hash"""
random = getrandbits(1024).to_bytes(1024//8, byteorder='big')
Expand Down
1 change: 1 addition & 0 deletions simu8.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- first 4 bytes of hash are converted to an int.
- ip is converted to a 4 bytes int as well
- all 4 bytes of ip are shuffled via a pseudo random lookup table, fed from the hash, to account for non uniform density in public ip v4
- same table is used for all 4 bytes in turn.
- closest eligible ip is voted for
"""
Expand Down
44 changes: 44 additions & 0 deletions simu9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Simulation with lottery in shuffled linear ip space
- random hash (not questioned) - sha256 of a bitstring - 32 bytes
- first 4 bytes of hash are converted to an int.
- ip is converted to a 4 bytes int as well
- all 4 bytes of ip are shuffled via a pseudo random lookup table, fed from the hash, to account for non uniform density in public ip v4
- 4 different tables are used for every ip byte, to get the most possible uniform repartition, while not shuffling outside of c-class borders.
- closest eligible ip is voted for
"""


from libs.utils import random_hash
from libs.utils import linear_ip_score5, shuffle4
from libs.nodesreader import NodesReader


if __name__ == "__main__":
readers = []
for i in range(5):
readers.append(NodesReader("NODES/nodes.{}".format(i+1)))
# save_whois()

for test in range(10000):
cycle_hash = random_hash()
shuffle4(cycle_hash)
# print("Run {}".format(test))
winners = []
for i in range(5):
winner = readers[i].winner(cycle_hash, scoring=linear_ip_score5)
ip_class = readers[i].verifiers[winner][1]
ip_class_count = readers[i].ip_classes[ip_class][0]
# print(winner.hex(), ip_class, ip_class_count)
winners.append(winner)
# exit()
full = True
for i in range(4):
if winners[4] != winners[i]:
full = False
print(cycle_hash.hex(), 'DIVERGE')
print("{},{},{},{}".format(test, full, ip_class, ip_class_count))


25 changes: 25 additions & 0 deletions simu9b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Many draws on a single "nodes" file, to analyse odds and bias.
"""


from libs.utils import random_hash
from libs.utils import linear_ip_score5, shuffle4
from libs.nodesreader import NodesReader


if __name__ == "__main__":
reader = NodesReader("NODES/nodes.1")
# save_whois()

for test in range(100000):
cycle_hash = random_hash()
shuffle4(cycle_hash)
# print("Run {}".format(test))
winner = reader.winner(cycle_hash, scoring=linear_ip_score5)
ip, ip_class = reader.verifiers[winner][:2]
ip_class_count = reader.ip_classes[ip_class][0]
print("{},{},{}".format(ip, ip_class, ip_class_count))


Loading

0 comments on commit b30d4d8

Please sign in to comment.