📜 ⬆️ ⬇️

Innovative entropy production method

I am chaos. I am the substance of your build and rhythms. I love you and your clowns laugh in happy anarchy. I am chaos. I am alive and I tell you that you are free.
- Eris, the goddess of chaos, discord 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 virtually nothing, which you can, for example, share with your chaos friends.

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

Input data


To obtain chaos, we need two files, which we will call “Seed of Chaos” and “Earth”. The seed of Chaos is 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, it would be possible to do without Earth, but, just in case, it is better to use it - a feature of random data is that they are difficult to distinguish from non-random. Suddenly, we will generate some zeros? Integral, working, the file guarantees that as a result there will always be at least something material.
')

Chaos Seed


Choosing the right candidate for the Seed of Chaos is a challenge. The file must have good initial entropy, besides, you must hate it sincerely, because it will later undergo terrible painful changes. Good indicators of randomness in images, in addition, they are lying around the Internet. If you do not have access to the Internet, it is likely that your devouring chaos application will run on the computer of a person who watches and saves funny pictures instead of living a rich life, so do not worry.

Ideal for the role of the Seed of Chaos





Use the DMCA logo. Now you need to do something with it, somehow turn it from a decent set of bits into a messy one. The pseudo-random function family does a good job with this task. By their definition, there is no effective algorithm that distinguishes their output from the output of a function that generates a true random bit sequence — exactly what is needed.

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

We are not interested in any cryptographic resistance at all, we just need to turn the Seed of Chaos into the most endless stream of randomness.

Take the first 16 bytes of Seed as the key for AES. After that, just encrypt the entire file in counter mode, be sure to 5 times in a row. If we need more entropy than is contained in the Seed of Chaos - we just encrypt it 5 more times in a row. So simply, without having any random data at the entrance, 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 to 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 away the generated randomness in such cases, sow the seed somewhere.

Land


As the Earth, you can use any file. If you think about it, you could do without it, but there are at least 5 reasons why you cannot do without the Earth in any way. 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.

Films of Mikhalkov have great entropy and, in some places, are still 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 puts his music under the license CC BY-SA, along with all the sources. Moreover, unlike many other candidates considered for the Earth, his music does not sucks at all.

We will plant the Seed into the Earth with the help of a logical function that is reliable and loved by all cryptographers - XOR. If you mix random data with not very random data, random data will be obtained! Moreover, no matter how random the data we do not mix with random data, the output will still be random data. The dream of a homeopath. In addition, this function is the basis of a one-time pad - the only absolutely strong cipher known at the moment.

Program


Takes 20 lines on python if you have pyaes .

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 the Brad song with the DMCA logo. Download or make it yourself and make sure that the output is completely random! Only 20 lines transform any two files into precious chaos.

pyaes works and is beautiful in its simplicity, but this is the slowest thing I've ever imported into my interpreter. I would be glad if someone likes the idea and rewrite it in a faster version. If someone creates an online service for the exchange of the purest entropy to the glory of Eris, I will understand that I lived not in vain.

Analysis


Obviously, the randomness of the source files will be less than the final ones. Both the car and the man will not understand what happened at the exit. Just in case, we will write a small script that counts 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) 

Source Chaos Seed Statistics
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 almost perfect entropy and a very low probability run for the appearance of different bytes. Always check your entropy, especially before sending it to someone. Perhaps your chaos is not chaotic enough and the person will not be happy with such a gift.

Conclusion


In this simple way, from 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 Chaos Seed, otherwise the attacker could get the same chaos as you do, spoiling the whole idea.

The resulting chaos streams are suitable for mashing previously deleted files on the disk (for this, chaos is desirable to be stored and deleted only as the need for disk space increases), useful in various application programs that require a lot of random data, and, quickly, as well as thanks to good statistical indicators are a wonderful offering to a follower of discordianism.
Well ... why not shelter a bit of pure mess?

Source: https://habr.com/ru/post/229473/


All Articles