# script for visualizing Bitcoin's overflow bug in 2010
print 50*"-"
print "check spec. values (unsigned/signed long long)"
print 50*"-"

print "%30s%20d" % ("max. int (64bit, unsigned) : ", 2**64-1)
print "%30s%20d" % ("2^63 : ", 2**63)
# out1: literal
out1 = 9223372036854275808;
print "%30s%20d" % (("out1 (literal,%s) : " % type(out1)), out1)
# out2: read in from hex code; identical to literal
out2 = int("7ffffffffff85ee0", 16);
print "%30s%20d" % ("out2 (conversion from hex) : ", out2)
dirSum = out1 + out2
print "%30s%20d" % (("out1+out2 (%s) : " % type(dirSum)), dirSum)
print "%30s%20d" % ("2^64 : ", 2**64)
print "%30s%20d" % ("-(2^64 - out1 - out2) : ", 
                    -(2**64 - out1 - out2))

print 50*"-"
print "imitate original bitcoin formula"
print 50*"-"

# outgoing numbers
nValueOut = 0
print "%30s%20d" % (("nValueOut=0 (%s) : " % type(nValueOut)), 
                     nValueOut)
nValueOut += out1
print "%30s%20d" % ("nValueOut += out1 : ", nValueOut)
# imitate overflow by cutting off int-to-long border
nValueOut += out2 - 2**64
print "%30s%20d" % ("nValueOut += out2 : ", nValueOut)
nValueOut += 5051000000 
print "%30s%20d" % ("nValueOut += 5051000000 : ", nValueOut)

# incoming numbers
nValueIn = 0
print "%30s%20d" % ("nValueIn : ", nValueIn)
mineReward = 5000000000
print "%30s%20d" % ("mineReward : ", mineReward)
investment =   50000000
print "%30s%20d" % ("investment : ", investment)
nValueIn += mineReward
nValueIn += investment
print "%30s%20d" % ("nValueIn : ", nValueIn)

# summing all incoming and outgoing numbers
print "%30s%20d" % ("nValueIn-nValueOut : ", nValueIn-nValueOut);
