r/learnprogramming • • Apr 28 '22

If everything is ultimately binary, does't that just make programs a specific number?

I did the CS50 course a while ago and they covered binary in there. I did binary back in 2006 when I was at college too so understood the logic behind it already. It's either on or off!

When a C program compiles, it goes through it's phases and compiles to assembly and then ultimately machine code i.e binary.

Let's say our program is 5,000,000 pages of 1's and 0's. All of those 1's and 0's make up a single number....albeit a super long number.

Does that mean that the entire Linux operating system, for example, can be represented by an actual number?

If so, why are we not storing programs as a number because that would take up less space than the 5,000 pages of 1's and 0's.

110101000101011111011011111110111100 = 57000443836 in decimal. So why don't we store programs in decimal and convert that to 1's and 0's in real time?

Storage would be much less. Is that how compression works?

These may be crazy questions but it just entered my head earlier when thinking about what the binary of a program can represent.

Even shorter than decimal is HEX.... So why don't we store programs in Hex?

Any help appreciated on this one so I can wrap my head around it.

1.7k Upvotes

346 comments sorted by

View all comments

Show parent comments

51

u/captainAwesomePants Apr 29 '22

Yes.

I'll take it that you completely understand that files are just bits and bytes, and bits and bytes can represent anything, including one giant number. So you understand that a large number can just be a representation of a file, and it follows naturally that one could make a gzip file containing the source code and convert that into a number like the one above.

Your confusion then is how the hell somebody could specifically aim to make that number be prime. That's a great thing to wonder about, and the answer is very clever. You see, gzip is special because if you add a few bytes to the end of it, nothing changes. It still decodes to the same thing. The bytes at the end are completely ignored. So, let's say you start with the gzip file you want, and then you add 2 bytes of zeroes at the end. Now turn that into a number. Now do this:

  1. Check if the number is prime. If so, we did it! Stop.

  2. Increase number by 1.

  3. Goto 1

Pretty good chance that one of the next 65536 or so numbers is gonna be prime.

Here's the creator's explanation of how he worked it out: http://fatphil.org/maths/illegal1.html

30

u/twbluenaxela Apr 29 '22 edited Apr 29 '22

That is absolutely insane. Thank you so much for taking the time to write this up.

So like, there is a prime number out there with every password I will ever have, every comment I will ever write, every picture I've ever taken.

That is crazy.

26

u/captainAwesomePants Apr 29 '22

There is a prime number out there containing a photo of your true love and the greatest poem that the greatest poet could ever write about them.

4

u/drake588 Apr 29 '22 edited Apr 29 '22

Why do you keep talking about prime numbers? What is the significance of prime? Why is that important? Can't a regular number divisible by 2 or 3 be just as good? Why are you so certain the photo is a prime number? Is there some computing law that says only prime numbers can become code?

2

u/erevos33 Apr 29 '22

Not computing law. Mathematical law. A prime number is divisible by 1 and itself, only. Thats it. So a prime number is basically unique. You cant divide it by any other number. The number 24 is divisible by 1,2,3,4,6,8,12,24. The number 23 only by 1 and 23. So if i encode a message into a number, which one do you think you can tell with certainty that you have found to be correct? The one with many divisors or the one with the unique divisor?

And i am not an expert on primes and math and programming. But keep in mind that programming is basically math (thats how the computer understands it).

4

u/drake588 Apr 29 '22

So if i encode a message into a number, which one do you think you can tell with certainty that you have found to be correct? The one with many divisors or the one with the unique divisor?

You're gonna have to explain that better because I still don't understand why that code NEEDS to be prime. I understand what a prime number is lol.. but not why code has to be prime. Why are you even looking at divisors in the first place? None of this makes sense. Why can't a picture represent the number 6500?

2

u/WatTheFork Apr 29 '22

I'm just formulating an hypothesis, the answer could be a problem with ambiguity. Almost all programs are deterministic (I say almost because you can create algorithms that do not necessarily will work 100%, unfortunately, I don't remember the name right now).

Say that you create a program A and translate this program to a number, imagine the number 8. A third person could take this number and have 3 different programs out of it corresponding to the numbers 2, 4, and 8 Which one of those are your program? Which one of those programs does what your program do? This person wouldn't know.

1

u/danquandt Apr 29 '22

Well, 8. The program is encoded in 8. What does it matter that it has divisors? Why would you divide it in the first place?

2

u/thetruffleking Apr 29 '22

I think what they are getting at is that a composite number could represent multiple programs, which could create problems with decoding it into the specific program you’re looking for.

Just my guess, tho.

1

u/erevos33 Apr 29 '22

I am not a programmer but think of the unique aspect mentioned. and combine it with mathematical operations.

A picture that can be identified by a unique number is better than a picture that can be identified by many. We are looking at divisors, since you asked, because everything is math. A picture that uses a prime number as a key for encoding has only that key as a point of attack. Thats the cyber security aspect.

As to why a program or a picture cannot be an even? Uniqueness.

I am afraid i am not knowledgeable enough to explain to you the why, i understand it enough but cant convey the why in a simple way. Maybe this article will help a bit:

https://codecoda.com/en/blog/entry/prime-numbers-and-the-importance-to-modern-life

2

u/danquandt Apr 29 '22

You completely missed the point of the story. The fact that the number is prime is irrelevant to the fact that it contained the code, and in fact the code had to be deliberately altered to make it prime. The point is that any number, prime or not, can be converted into a binary sequence, which can then be decoded into other representations (such as ASCII) which may represent code, or a photo, or any kind of data. The number of divisors doesn't matter because the decimal representation doesn't matter, it's just a different way of reading the same binary data.

1

u/ravanbak Apr 30 '22

Software engineer here, 100% agree with you. Even a prime number could contain multiple programs. I don't see the relevance of prime numbers at all in this context.

0

u/[deleted] Apr 29 '22

[deleted]

1

u/erevos33 Apr 29 '22

What?

To get to 7, you can only go 1*7.

To go to 24 tou can go any number of ways:

2x12, 3x8, 4x6 to name a few.

You dont have a unique key, there are more than one key that opens the same door.

0

u/[deleted] Apr 29 '22

[deleted]

1

u/erevos33 Apr 29 '22

As i said elsewhere, i am sadly not an expert so my knowledge is limited. I kinda get it but not good enough to explain to others , so maybe i shouldnt have tried.

Also, i didnt express myself clearly.

In math, knowing the factors of a number allows you to find that number even if you dont know the number. If i know a hidden number has only 2 factors, say 2 and 4, then i can easily deduce that this number is 8.

Now to combine with programming, say you hide a number and ask me to find it.

If you hide a number and I know its even, I can try plugging in multiples of 2 and see if they work. Or, if i can, i can deduce some additional factors, and make my guesses more educated.

But, if you hide a prime number, im out of luck. Since the prime number has no factors, i can only try random numbers and maybe i will get it.

The numerical base for the numbers doesnt matter. Be it binary , decimal or hex, its only a representation.

8

u/Dranks Apr 29 '22

Can i just say i love when people are happy about things? Like, the amazement you have at this makes me happy

3

u/[deleted] Apr 29 '22

so why did he use gzip, when the source code could just be a text file? because of this property (being able to append other data which will be ignored so it can become a prime number and get archived on that list)?

7

u/TheMcDucky Apr 29 '22

Compression -> Smaller file -> Easier to find in a prime.
You wouldn't need to gzip it to pad it with garbage data.

1

u/[deleted] Apr 29 '22

ah ok thanks, yes that makes total sense!

1

u/laser2ce Apr 29 '22

From the website:

  • ~~bignum representing the .gz file~~ * 256 + X

  • where X is an odd number between 1 and 255. i.e I wanted to append just one byte to the .gz file.

  • However, this search failed, so I changed the target to be 2 extra bytes:

  • ~~bignum representing the .gz file~~ * 65536 + X, for X in 1 to 65535

Can someone tell me why when appending one or two bytes, they multiply the bignum by 28 and 216 ?

I know it's 2 raised to the number of bits but why? I don't understand why those specific numbers.

2

u/captainAwesomePants Apr 29 '22

If you add a zero to the end of a decimal number, it gets 10 times bigger. 43 with a zero becomes 430. If you add 3 zeroes, it gets 100 times bigger, or 103 times bigger: 43 becomes 43,000.

For binary numbers, adding a zero multiplies it by 2 instead of 10. 101010 (42) becomes 1010100 (84). Adding two bytes of zeroes on the end is 16 zeroes, so you're multiplying by two 16 times: 216.

2

u/laser2ce Apr 29 '22

Oooo okay, it's the decimal number you get when adding 1 or 2 null bytes to its binary representation. And he started with the lowest possible numbers because they take less time to prove.

2

u/captainAwesomePants Apr 29 '22

Right, he was hoping something between 1011...011100000000 and 1011...011111111111 would be prime, but it wasn't, so he added 8 more zeroes and tried again.