Rating:


#
# If we assume that we already know secrets 1..R-1, and we have (L,R,V) test interval ending at R,
# it is trivial to compute secret for R.
# secret[R] = xor( secret[L], ..., secret[R-1], V )
#
# We iteratively add tests to our data structure indexed by R.
#
# If there is already test ending at position R, it is possible to split the longer one into new, non-overlapping tests.
#

from pwn import *

p = remote("2020.redpwnc.tf", 31458)
# f = open("expohash.in", "r")

lines = 0
checks = []

while True:
l,r,c = line.strip().split()
checks.append( [int(l), int(r), int(c)] )
lines += 1
if lines == 100000:
break

checksums = dict()

def insert_interval(check):

while True:
if check in checksums:
# print("Checksum index %d is already used." % check)
# print(check)
# print(checksums[check])

prev = checksums[check]
# find shorter one
if check > prev:
# new one is shorter, replace it for current index
checksums[check] = check
# split previous one and re-insert it
new = prev, check-1, prev ^ check
check = new
elif check < prev:
# previous one is shorter, split the new one
new = check, prev-1, prev ^ check
check = new
else:
# print("Exact same interval?")
# print(prev)
# print(check)
assert prev == check
return False
else:
checksums[check] = check
return True

for check in checks:
insert_interval(check)
print(len(checksums))

secret = {}
# lets generate secret integers
for i in range(1,100001):
# do we have checksum for this position?
if i in checksums:
c = 0
for j in range(checksums[i], i):
c = c ^ secret[j]
secret[i] = checksums[i] ^ c
else:
# no tests, just use any random number..
secret[i] = 0x1337
print(i, secret[i])
p.sendline( str(secret[i]) )