Innovative Entropy Production Method

    I am chaos. I am the substance from which your artists and scientists build rhythms. I am the spirit with which your children and clowns laugh in happy anarchy. I am chaos. I am alive, and I tell you that you are free.
    - Eris, the goddess of chaos, contention and confusion.

    Order is impossible without disorder. Programmers often dive into chaos in search of ingenious thoughts, random numbers and magic coefficients that every self-respecting application should have. In this article I will tell you how to get high-quality samples of randomness from practically nothing, which you can, for example, share with your chaos friends.

    Warning for lawyers, politicians, and other robots: this is a half-joke article.

    Input data


    To get chaos, we need two files, which we will call “Seed of Chaos” and “Earth”. Seed of Chaos - a small file from which chaos will be born. Earth - a file of arbitrary size, which will set the volume of the resulting entropy, well, and slightly increase the randomness of the result. In principle, the Earth could have been dispensed with, but, just in case, it is better to use it - the peculiarity of random data is that it is difficult to distinguish it from nonrandom data. Suddenly we have some zeros generated? A complete, working file guarantees that as a result there will always be at least something real.

    Seed of Chaos


    Choosing the right candidate for the role of the Seed of Chaos is a difficult task. The file must have good initial entropy, and besides, you must sincerely hate it, because it will, in the future, undergo terrible painful changes. Good indicators of randomness in images, moreover, they are scattered all over the Internet. If you don’t have access to the Internet, it is likely that your chaos-devouring application will be launched on the computer of a person who watches and saves funny pictures instead of living an eventful life, therefore, do not worry.

    Ideal Chaos Seed Files





    We use the DMCA logo. Now you need to do something with it, somehow turn it from an orderly set of bits to a messy one. The family of pseudo-random functions copes well with this task. By their definition, there is no effective algorithm that would distinguish their output from the output of a function that generates a truly random sequence of bits - exactly what you need.

    One of my favorite cryptographic ciphers - AES - is a pseudo-random conversion, therefore, we will use it.

    We are not interested in any cryptographic stability at all, we only need to turn the Seed of Chaos into the infinite stream of chance as possible.

    Take the first 16 bytes of the Seed as the key for AES. After that, we simply encrypt the entire file in counter mode, always 5 times in a row. If we need more entropy than is contained in the Seed of Chaos, we simply encrypt it 5 more times in a row. So simple, without any random data input, you can get an unlimited source of quality chaos! The most important thing is to delete the source file after turning it into pure entropy and not tell anyone what we used, because otherwise anyone can get exactly the same chaos as we do. Chaos will cease to be chaos and will have to be thrown away. In order not to always throw out the generated randomness in such cases, we plant the Seed somewhere.

    Land


    You can use any file as the Earth. If you think about it, you could have done without it, but there are at least 5 reasons why you can’t do without Earth. Ideal candidates for the role are various compressed data, for example, audio and video recordings - they are quite large and are often found on users' computers.

    Mikhalkov’s films have great entropy and, in some places, that same Earth. It would be nice to use them to demonstrate the concept, but I do not have a single copy purchased. Therefore, we use the composition of the musician Brad Sucks , specifically - ” You're not going anywhere" Brad should not be offended, because he uploaded his music under the CC BY-SA license, along with all the source codes. Moreover, unlike many of the other candidates considered in the Earth, his music does not sucks at all.

    We will plant the Seed in the Earth with the help of a reliable and beloved by all cryptographers logical function - XOR. If you mix random data with not very random - you get random data! Moreover, no matter how much random data we mix with random data, the output will still be random data. Homeopath's dream. In addition, this function is the basis of a one-time notepad - the only absolutely stable cipher known at the moment.

    Program


    Takes 20 lines in python if you have pyaes .

    The code
    from pyaes import AESModeOfOperationCTR as Cipher
    from os import sys
    with open(sys.argv[1], "rb") as TheSeedOfChaosFile:
        key = TheSeedOfChaosFile.read(16)
        cypher = Cipher(key)
        TheSeedOfChaosFile.seek(0)
        TheSeedOfChaos = TheSeedOfChaosFile.read()
    with open(sys.argv[2], "rb") as TheEarthFile:
        with open(sys.argv[3], "wb") as TheEntropyFile:
            chunk = TheEarthFile.read(len(TheSeedOfChaos))
            while chunk:
                for i in range(5):
                    TheSeedOfChaos = cypher.encrypt(TheSeedOfChaos)
                chunk = [x ^ y for x, y in zip(chunk, TheSeedOfChaos[0:len(chunk)])]
                TheEntropyFile.write(bytearray(chunk))
                chunk = TheEarthFile.read(len(TheSeedOfChaos))
    

    usage: python script.py seed_file earth_file output_file
    Here is the result of mixing Brad's song with the DMCA logo. Download or make it yourself and make sure the conclusion is completely random! Only 20 lines turn any two files into precious chaos.

    pyaes works and is beautiful in its simplicity, but it is the slowest that I have ever imported into my interpreter. I would be glad if someone likes the idea and rewrites it in a faster version. If someone creates an online service for exchanging the purest entropy for the glory of Eris - I understand that I lived not in vain.

    Analysis


    Obviously, the randomness of the source files will be less than the final ones. Both the machine and the person will not understand what happened on the way out. Just in case, we will write a small script that reads the statistics of the files, and check:

    Script source code
    import sys
    import math
    with open(sys.argv[1], "rb") as file:
        data = file.read()
    filesize = len(data)
    frequencies = [0]*256
    for byte in data:
        frequencies[byte] += 1
    frequencies = [freq / float(filesize) for freq in frequencies]
    entropy = -sum([freq * math.log(freq, 2) for freq in frequencies])
    mean = sum(frequencies) / len(frequencies)
    stddev = [(freq - mean) ** 2 for freq in frequencies]
    stddev = math.sqrt(sum(stddev) / len(stddev))
    print('Shannon:')
    print(entropy)
    print('Mean: (ideal: ' + str(1/256) + ')')
    print(mean)
    print('StdDev:')
    print(stddev)
    

    Statistics of the original Seed of Chaos
    Shannon:
    7.942118575257812
    Mean: (ideal: 0.00390625)
    0.003906250000000003
    StdDev:
    0.0011614891032870488

    Source Earth statistics
    Shannon:
    7.973164196428091
    Mean: (ideal: 0.00390625)
    0.0039062499999999987
    StdDev:
    0.0008704476953482593

    Result Statistics
    Shannon:
    7.999936528333672
    Mean: (ideal: 0.00390625)
    0.0039062499999999983
    StdDev:
    3.665423289519401e-05

    The result has an almost perfect entropy and a very low probability spread of various bytes. Always check your entropy, especially before sending it to someone. Perhaps your chaos is not chaotic enough and a person will not be happy with such a gift.

    Conclusion


    In this simple way, from the files that are often found in the household, you can quickly get a couple of gigabytes of very random bytes. The main thing is to remove the source of the Seed of Chaos, otherwise, the attacker will be able to get the same chaos as yours, ruining the whole idea.

    The resulting chaos flows are suitable for overwriting files that you previously deleted on the disk (for this, it is advisable to store and delete chaos only as the demand for disk space increases), come in handy in various applications that require a lot of random data, moreover, quickly and also thanks to good statistical indicators are a wonderful offering to the follower of discordianism.
    Well ... why not shelter a little sheer mess?

    Also popular now: